React Saga remove specific action from the actionChannel if its waiting in queue

Viewed 42

I have the following problem: With react saga i create an action channel and listen "requested" actions then put them into the action channel. With fork I create workers (with amount of WORKER_COUNT) which listen on that channel. This workers do call a function which takes time to finish it. I want to take action out of queue, if user wants to cancel the action before it finishes. How can I fix that problem ?

Take it as example, I have 2 workers, and user dispatched 3 "requested" actions at same time, as for first 2 actions dispatched, doDownload function would be in progress, the last action would be waiting in queue and user cancels that last action dispatched, I need to take then that last action from the actionChannel so it will not be proceeded with one of the two workers

How can i fix that problem?

function* handleRequest(streamChannel: Channel<unknown>) {
  while (true) {
    const payload = (yield* take(streamChannel)) as requestPayload;
    yield* call(doDownload, payload);
  }
}

function* watchRequest() {
  const chan = yield* call(channel);

  for (var i = 0; i < WORKER_COUNT; i++) {
    yield* fork(handleRequest, chan);
  }

  while (true) {
    const {payload} = yield* take(requested);
    yield* put(chan, payload);
  }
}
0 Answers
Related