useBlocComparativeListener<BLOC extends BlocBase<S>, S> function

void useBlocComparativeListener<BLOC extends BlocBase<S>, S>(
  1. BLOC bloc,
  2. BlocComparativeListener<BLOC, S> listener, {
  3. 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],
  );
}