You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a middleware that needs to response to event from the hardware.
this HW events needs to fire different actions that changes the redux state.
I already have a stream setup. How would I subscribe to this stream in the middleware?
Thank you
The text was updated successfully, but these errors were encountered:
Hey there -- sorry for the delay. Very busy work/travel times.
For this case, I'd recommend checking out the redux_epics package. It will provide a nice way to merge Streams with Middleware.
In your case, you could either supply the Stream to your Middleware / Epic function via the constructor, or dispatch an action that contains the Stream.
Using a constructor would look something like this:
class MyStreamMiddleware extends MiddlewareClass<AppState> {
final Stream<String> myStream;
StreamSubscription<String> _subscription;
MyStreamMiddleware(this.myStream);
@override
void call(Store<AppState> store, dynamic action, NextDispatcher next) {
if (action is ListenToStreamAction) {
_subscription = stream.listen((data) {
store.dispatch(DataReceivedAction(data));
})
} else if (action is StopListeningAction) {
_subscription?.cancel();
}
next(action);
}
}
I have a middleware that needs to response to event from the hardware.
this HW events needs to fire different actions that changes the redux state.
I already have a stream setup. How would I subscribe to this stream in the middleware?
Thank you
The text was updated successfully, but these errors were encountered: