Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypedReducer is not extendable #63

Open
Oleh-Sv opened this issue Aug 5, 2020 · 0 comments
Open

TypedReducer is not extendable #63

Oleh-Sv opened this issue Aug 5, 2020 · 0 comments

Comments

@Oleh-Sv
Copy link

Oleh-Sv commented Aug 5, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant