wrapReduce method

  1. @override
Reducer<St> wrapReduce(
  1. Reducer<St> reduce
)
override

You may wrap the reducer to allow for some pre- or post-processing. For example, if you want to prevent an async reducer to change the current state, if the current state has already changed since when the reducer started:

Reducer<St> wrapReduce(Reducer<St> reduce) => () async {
   var oldState = state;
   AppState? newState = await reduce();
   return identical(oldState, state) ? newState : null;
};

Note: If you return a function that returns a Future, the action will be ASYNC. If you return a function that returns St, the action will be SYNC only if the before and reduce methods are also SYNC.

Implementation

@override
Reducer<St> wrapReduce(Reducer<St> reduce) => () async {
      FutureOr<St?> newState;
      try {
        newState = reduce();
        if (newState is Future) newState = await newState;
      } catch (error) {
        _attempts++;
        if ((maxRetries >= 0) && (_attempts > maxRetries)) rethrow;

        var currentDelay = nextDelay();
        await Future.delayed(currentDelay);
        return wrapReduce(reduce)();
      }
      return newState;
    };