brianegan / dart_redux_epics

Redux.dart middleware for handling actions using Dart Streams
MIT License
141 stars 22 forks source link

takeUntil action make stream unusable the second time #27

Closed exilonX closed 5 years ago

exilonX commented 5 years ago

I am using flutter with redux and for handling stream data I'm using redux-epics.

Like this:

Stream<dynamic> getDataEpic(Stream<dynamic> actions, EpicStore<AppState> store) {
  return Observable(actions)
      .ofType(TypeToken<RequestDataAction>())
      .flatMap((RequestDataAction action) {
        return getDataStream()
          .map(
              (data) => UpdateStateWithData(data)
          );
      })
      .takeUntil(actions.where((action) => action is CancelGetDataAction));
}

// getDataStream() - is a stream that returns some stuff... 

In my screen in onInit I call store.dispatch(RequestDataAction()) and onDispose I call store.dispatch(CancelGetDataAction()) which destroys the Observable altogether, so the next time I go to this screen if I call store.dispatch(RequestDataAction()) my stream is not sending data, actually the entire Observable is dead!

How can I solve this problem? As far as I can see the problem is takeUntil because I completely closes the observable..

brianegan commented 5 years ago

Try moving the takeUntil into the flatMap just after the getStreamData().map(). This will cancel the inner observable rather than the outer observable when the action is received.

exilonX commented 5 years ago

Yeah now it sounds quite obvious! Thanks a lot!