How to cancel running saga on action dispatch, while using takeEvery

Viewed 27

I need to run a saga every time an action is performed - but also cancel it when a logout action is dispatched. I tried using the while pattern described in the documentation, but this is only firing on the first action dispatch (but it's cancelling as expected).

export default function* rootSaga() {
 while (yield take(actionTypes.sagas.RUN_COURSES_SAGA)) {
       
  const syncTask = yield fork(processCourseLibraryFiles)
  
  yield take(actionTypes.auth.LOGOUT_USER)

  yield cancel(syncTask)
 }
}

I tried replacing this is takeEvery, which fixes the problem of it only firing once - but now I'm stuck on how to cancel each of these sagas on logout action.

export default function* rootSaga() {
  yield takeEvery('actionName', fn) 

  // some way to cancel these sagas on action
}

Appreciate any insights!

0 Answers
Related