davegravy
davegravy

Reputation: 934

redux-saga race between "take"s with payload conditional

I have

yield race([
  take('ACTION_ONE'),
  take('ACTION_TWO')
])

but I want to specify that the condition for ACTION_ONE finishing is that the action payload has a specific value. How can I do this?

Upvotes: 3

Views: 2088

Answers (2)

Alexej
Alexej

Reputation: 640

You can pass a callback function in the take.

const { action1, action2 } = yield race({
  action1: take(action => action.type === 'ACTION_ONE' && action.value === value),
  action2: take(action => action.type === 'ACTION_TWO' && action.value === value),
})

if (action1) {
   console.log('action1 fired')
}

if (action2) {
   console.log('action2 fired')
}

Upvotes: 3

Osmel Mora
Osmel Mora

Reputation: 76

Probably you need to define a new saga to handle the 'ACTION_ONE' and put it in the race. Something like this:

function* actionOneSaga() {
  let notFound = true;
  while(notFound) {
    const action = yield take('ACTION_ONE');
    if (action.payload === 'SOME_VALUE') {
      doSomethingWithYourValue(action.payload);
      notFound = false;
    }
  }
}

Then you can do:

yield race([
  take(actionOneSaga),
  take('ACTION_TWO')
])

Upvotes: 2

Related Questions