useBlocComparativeListener<BLOC extends BlocBase<S> , S> function
void
useBlocComparativeListener<BLOC extends BlocBase<S> , S>(
- BLOC bloc,
- BlocComparativeListener<
BLOC, S> listener, { - required BlocComparativeListenerCondition<
S> listenWhen,
Implementation
void useBlocComparativeListener<BLOC extends BlocBase<S>, S>(
BLOC bloc,
BlocComparativeListener<BLOC, S> listener, {
required BlocComparativeListenerCondition<S> listenWhen,
}) {
final currentState = useRef(bloc.state);
final context = useContext();
useEffect(
() {
final subscription = bloc.stream.where(
(nextState) {
final shouldInvokeAction = listenWhen(currentState.value, nextState);
currentState.value = nextState;
return shouldInvokeAction;
},
).listen((state) {
return listener(
bloc,
state,
context,
);
});
return subscription.cancel;
},
[bloc],
);
}