createMiddleware method

Middleware<T> createMiddleware()

Middleware used for Redux which saves on each action.

Implementation

Middleware<T> createMiddleware() {
  Timer? _saveTimer;

  return (Store<T> store, dynamic action, NextDispatcher next) {
    next(action);

    if (shouldSave != null && shouldSave!(store, action) != true) {
      return;
    }

    // Save
    try {
      if (throttleDuration != null) {
        // Only create a new timer if the last one hasn't been run.
        if (_saveTimer?.isActive != true) {
          _saveTimer = Timer(throttleDuration!, () => save(store.state));
        }
      } else {
        save(store.state);
      }
    } catch (_) {}
  };
}