fluttercommunity / redux.dart

Redux for Dart
https://pub.dev/packages/redux
MIT License
515 stars 61 forks source link

TypedReducer is not extendable #63

Open Oleh-Sv opened 4 years ago

Oleh-Sv commented 4 years ago

Current TypedReducer looks:

class TypedReducer<State, Action> implements ReducerClass<State> {
  /// A [Reducer] function that only accepts an action of a specific type
  final State Function(State state, Action action) reducer;

  /// Creates a reducer that will only be executed if the dispatched action
  /// matches the [Action] type.
  TypedReducer(this.reducer);

  @override
  State call(State state, dynamic action) {
    if (action is Action) {
      return reducer(state, action);
    }

    return state;
  }
}

I prefer to extend TypedReducer instead pass function. It looks more convenient and by this reason I am using next version of TypedReducer:

abstract class TypedReducer<State, Action> implements ReducerClass<State> {
  factory TypedReducer(State Function(State state, Action action) reducer) =>
      _TypedReducer<State, Action>(reducer);

  TypedReducer.default();

  @override
  State call(State state, dynamic action) {
    if (action is Action) {
      return reduce(state, action);
    }

    return state;
  }

  State reduce(State state, Action action);
}

class _TypedReducer<State, Action> extends TypedReducer<State, Action> {
  final State Function(State state, Action action) reducer;

  _TypedReducer(this.reducer) : super.default();

  @override
  State reduce(State state, Action action) => reducer(state, action);
}

It doesn't make any conflict with current code and I think this version can be helpful for other users of this library.