brianegan / dart_redux_epics

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

Stream, not action, is being sent to reducers #29

Closed gopowerrangers closed 5 years ago

gopowerrangers commented 5 years ago

Hi I am using the following code to sync to a Firebase real time database. After opening the Firebase stream, the Epic should send a new CreateTodoListFromStrings action to the reducers, which keeps the Redux TodoList synced with the database. image

I thought it would be enough to just return on Observable (as is shown in examples here https://medium.com/shift-studio/flutter-redux-and-firebase-cloud-firestore-in-sync-2c1accabdac4) and it was resolved into an action by the time it reaches the reducer. Other examples, including this repo's ReadMe, are returning a stream or Observable, so I assumed this pattern would work.

But according to the debugger, the reducers are receiving the Observable itself. As a result none of the reducers are activated, and the store is never updated. image

Am I not thinking about this the right way?

gopowerrangers commented 5 years ago

Resolved.

In the epic, I needed to remove the second return (before the _getFirebaseTodoStrings).

gopowerrangers commented 5 years ago

Never mind, it does not work, I was looking at the wrong thing.

But I did force it work using the following code image

Could someone explain why the code above works but not the code below? image

brianegan commented 5 years ago

Hey there -- the first example returns a Stream<CreateTodoListFromStrings> -- therefore, the epic will dispatch CreateTodoListFromStrings received from that Stream. The second example returns a Stream<Stream<CreateTodoListFromStrings>>. Therefore, the epic will emit the inner Stream<CreateTodoListFromStrings>.

To "unwrap" the inner stream, instead of using map, use flatMap. For example: .ofType().flatMap(...).

flatMap will subscribe to that inner Stream and emit the CreateTodoListFromStrings actions.

gopowerrangers commented 5 years ago

Yup that fixes it.