is yield take considered a non blocking call?

2k views Asked by At

Is this considered non blocking or can the first take be dropped/missed if action1 is dispatched multiple times?

function* nonBlockingSaga () {
    while (true) {
        yield take('action1');
        yield take('action2');
        yield take('action3');
    }
}
1

There are 1 answers

0
Vlemert On BEST ANSWER

Yes, if you dispatch action1, the saga will be blocked by take('action2') and you will miss any action1 actions until the saga loops around. To work around/solve this you can use an action channel. An action channel allows you to buffer the actions until your saga is ready to take them.

So in you example this would result in:

function* nonBlockingSaga () {
  const channel1 = yield actionChannel('action1');
  const channel2 = yield actionChannel('action2');
  const channel3 = yield actionChannel('action3');
  while (true) {
    yield take(channel1);
    yield take(channel2);
    yield take(channel3);
  }
}

This way you will never miss any of the actions.