reducer 0.6.0 copy "reducer: ^0.6.0" to clipboard
reducer: ^0.6.0 copied to clipboard

Generates actions and a reducer from a set of pure functions.

example/reducer_example.dart

import 'package:meta/meta.dart';
import 'package:reducer/reducer.dart';

void main() {
  var state = CounterState(0);
  const reducer = CounterReducer();
  state = reducer(state, const CounterAddAction(value: 5));
  state = reducer(state, const CounterResetAction());
}

@immutable
class CounterState {
  final int count;
  const CounterState(this.count);
}

class CounterReducer extends Reducer<CounterState, CounterAction>
    with _CounterReducer {
  const CounterReducer();
  CounterState add(CounterState previousState, {int value}) {
    return CounterState(previousState.count + value);
  }

  CounterState reset(CounterState previousState) {
    return CounterState(0);
  }
}

/** The following can be generated */

mixin _CounterReducer {
  CounterState call(CounterState previousState, CounterAction action) {
    final reducer = this as CounterReducer;
    if (action is CounterAddAction) {
      return reducer.add(
        previousState,
        value: action.value,
      );
    }
    if (action is CounterResetAction) {
      return reducer.reset(
        previousState,
      );
    }

    return previousState;
  }

  bool operator [](Object action) {
    final actionType = action is Type ? action : action.runtimeType;
    return const [
      CounterAddAction,
      CounterResetAction,
    ].contains(actionType);
  }
}

abstract class CounterAction {
  const CounterAction();
}

class CounterAddAction extends CounterAction {
  final int value;
  const CounterAddAction({
    this.value,
  });
}

class CounterResetAction extends CounterAction {
  const CounterResetAction();
}
1
likes
40
pub points
0%
popularity

Publisher

unverified uploader

Generates actions and a reducer from a set of pure functions.

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

meta

More

Packages that depend on reducer