brianegan / dart_redux_epics

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

How to create an epic that does not dispatch an Action #24

Closed hongminzhu closed 5 years ago

hongminzhu commented 5 years ago

Hi, first of all thanks for the library!

I'm still new to flutter and I am trying to take some concepts over from Angular (Ngrx) and was stuck on how to create epics that does not dispatch any actions.

In Ngrx you would have to write an effect with dispatch = false, I was wondering if I can achieve the same behavior using epics in flutter.

Thanks in advance!

brianegan commented 5 years ago

Hey there -- so sorry, I missed a couple of these issues on this repo :/

I'll have to take a look at the NgRx tools which make this easier. In general, you need to return a Stream that does not emit any values nor does it close! I generally use the NeverStream() from RxDart for this purpose.

// An effect that prints all actions to the console, but does not dispatch anything
Stream<dynamic> printActionEffect(Stream<dynamic> actions, EpicStore<State> store) {
  actions.listen(print)

  // Return a Stream that never emits nor closes
  return NeverStream();
}
hongminzhu commented 5 years ago

@brianegan Thanks for the response, that's exactly what I'm looking for.

Basically in Ngrx one would do something like this for convenience. It is a little easier to use and the code reads a bit better.

  @Effect({ dispatch: false })
  logActions$ = this.actions$.pipe(tap(action => console.log(action)));