reducer 0.6.0

  • Readme
  • Changelog
  • Example
  • Installing
  • new63

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

Install #

To use [built_reducer], you will need your typical [build_runner]/code-generator setup.
First, install [build_runner] and [built_reducer] by adding them to your pubspec.yaml file:

# pubspec.yaml
dependencies:
  reducer:

dev_dependencies:
  build_runner:
  built_reducer:

This install three packages:

  • build_runner, the tool to run code-generators
  • [built_reducer], the code generator
  • reducer, a package containing base types for [built_reducer].

Usage #

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

part 'counter.g.dart';

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

class CounterReducer extends Reducer<CounterState, CounterAction>
    with _CounterReducer {
  const CounterReducer();

  static CounterState add(CounterState previousState, {@required int value}) {
    return CounterState(previousState.count + value);
  }

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

From there, to run the code generator, you have two possibilities:

  • flutter pub run build_runner build, if your package depends on Flutter
  • pub run build_runner build otherwise

Then use the reducer from the actions :

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

Resulting file #

You have a result in the example folder.

Why using a reducer instead of methods directly. #

Since each call is represented with an instance, you have more possibilities :

  • Reproducing a sequence of calls.
  • Adding middlewares
  • Serializing calls
  • Redux

0.6.0 #

  • Updated example

0.5.1 #

  • Updated doc

0.5.0 #

  • Initial version

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();
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  reducer: ^0.6.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:reducer/reducer.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
26
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Jul 9, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
meta ^1.1.8 1.2.2 1.3.0-nullsafety
Dev dependencies
pedantic ^1.8.0
test ^1.6.0