reduxsaga / redux_saga

Redux Saga library for Dart and Flutter
MIT License
39 stars 7 forks source link

TakeLeading stops listening actions after cancelling a saga task #29

Closed qasim90 closed 1 year ago

qasim90 commented 1 year ago

I'm trying to cancel a saga task. The task cancellation works well but the problem is that no more saga tasks are triggered for the same action once a cancel occurs. Below is my cancel setup following Task cancellation docs.

  yield Try(() sync* {
      ...

  }, Catch: (e, s) sync* {
    logger.e(e);
  }, Finally: () sync* {
    // Do the needful cleanup if get turn task is cancelled by user
    var cancelled = Result<bool>();
    yield Cancelled(result: cancelled);
    if (cancelled.value!) {
      // Close the task as normal success operation
      yield Put(GetTurnSuccessAction());
      yield Put(ResetLoadingAction(key: loadingKey));
    }
  });

_watchGetTurn() sync* {
  var getTurnTask = Result<Task>();
  yield TakeLeading(_handleGetTurn, pattern: GetTurnAction, result: getTurnTask);

  // Listen to turn page close action.
  yield Take(pattern: PendingTurnClosedAction);

  // Cancel ongoing GetTurn saga
  yield Cancel([getTurnTask.value!]);
}
bilal-uslu commented 1 year ago

I recommend you to check the Fork model, https://github.com/reduxsaga/redux_saga/blob/master/doc/advanced/ForkModel.md You need to spawned a detached task for the TakeLeading, otherwise Cancel will cancel it at the same time. Check https://pub.dev/documentation/redux_saga/latest/redux_saga/TakeLeading.html there is a detached optional parameter, which you may need to set true for your case.