question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cancelling a task causes cancelling of every forked task?

See original GitHub issue

Hi, I’m still new to redux-saga and have the following problem:

I have a simple list of items in my UI (the “bookmarks” in the code below). When I delete one of them, I want to show an “undo” button and then wait 5 seconds for the the user’s action. If the user clicks on the undo button, the we dispatch the types.CANCEL_DELETE_BOOKMARK action, otherwise, the saga calls the API.

The issue I seem to have is that if I dispatch multiple types.DELETE_BOOKMARK in a quick succession, then a subsequent dispatching of types.CANCEL_DELETE_BOOKMARK cancels every performDelete, not just the one in the forked task.

function* performDelete(id) {
  try {
    yield call(delay, 5000);
    yield call(api.deleteBookmark, id);

    yield put({type: types.DELETE_BOOKMARK_SUCCESS, id});
  } catch(error) {
    yield put({type: types.DELETE_BOOKMARK_ERROR, error});
  }
}


function* deleteBookmark(act) {
  const { id } = act;
  yield put({type: types.REMOVE_BOOKMARK_FROM_UI, id});
  yield put({type: types.SHOW_UNDO, id});

  const { response, cancelDeleting } = yield race({
    delete: call(performDelete, id),
    cancelDeleting: take([types.CANCEL_DELETE_BOOKMARK, types.DELETE_BOOKMARK_ERROR])
  });

  if (cancelDeleting) {
    yield put({type: types.READD_BOOKMARK_TO_UI, id});
  }
  yield put({type: types.HIDE_UNDO, id});
}


export default function* rootSaga() {
  yield* takeEvery(types.DELETE_BOOKMARK, deleteBookmark)
}

I have also tried to fork and cancel the performDelete task, without the use of race, but that results in the same behavior.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

5reactions
jrvidalcommented, Aug 9, 2016

You’re right the DELETE_BOOKMARK_ERROR action is not the issue. I noticed it first for that action for some reason, but the case is the same for CANCEL_DELETE_BOOKMARK.

What you probably need is something like:

const { response, cancelDeleting } = yield race({
  delete: call(performDelete, id),
  cancelDeleting: take(action => (
    (action.type === types.DELETE_BOOKMARK_ERROR || action.type === types.CANCEL_DELETE_BOOKMARK) &&
    action.id === id
  ))
});

Otherwise CANCEL_DELETE_BOOKMARK affects all your races. There is nothing in your take referring to the particular bookmark you are dealing with.

0reactions
michalczaplinskicommented, Aug 9, 2016

That makes sense! Thanks a lot! I somehow had a completely wrong mental model and just assumed that each task only listens on actions that affect it. Only my 2nd day using the library, though 😃 Perhaps this could be made more clear in the docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Task Cancellation | Redux-Saga
In this section we'll review cancellation in more detail. Once a task is forked, you can abort its execution using yield cancel(task) ....
Read more >
Task cancellation | redux-saga
Once a task is forked, you can abort its execution using yield cancel(task) . Cancelling a running task will throw a SagaCancellationException inside...
Read more >
reactjs - Redux Saga - Unable to cancel task - Stack Overflow
it will run two independent instances of mySaga, each with its own task. If you cancel one, it doesn't cancel the other one....
Read more >
Tracking the source of cancellation in tasks - Async-SIG
The information “where it is cancelled” is less important as long as all the resources are properly released upon cancellation, and Python ...
Read more >
Task Cancellation | Microsoft Learn
Threading.Tasks.Task<TResult> classes support cancellation by using cancellation tokens. For more information, see Cancellation in Managed ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found