brianegan / redux_thunk

Redux Middleware for handling functions as actions
MIT License
88 stars 8 forks source link

Handling stream subscriptions #21

Closed elvisgn closed 3 years ago

elvisgn commented 3 years ago

Hi, I'm building an app using flutter, firebase, redux, redux_thunk.

I'm not sure if middleware is even the right place, but currently I'm invoking a backend service streamChat from my middleware, that returns a stream of chat messages from firestore.

void Function(
  Store<AppState> store,
  LoadGroupSucceededAction action,
  NextDispatcher next,
) _loadChatMessages() {
  return (store, action, next) async {
    next(action);
    _s.streamChat(action.groupId).listen((e) {
      store.dispatch(LoadChatsSucceededAction(e));
    }).onError((error) {
      store.dispatch(LoadChatsFailedAction(error));
    });
  };
}

I know how to create a subscription to the listener within the function, but i'm confused on how to reach the subscription and cancel it on user logout. Right now an exception is getting thrown.

Kindly advise on best practice. Thanks.

elvisgn commented 3 years ago

Hi, Realizing that this cannot be handled in thunk middleware, using redux_epics instead.