dart_mutate

A state machine written in dart.

Examples

Example

class GoToA extends Event {}

class GoToB extends Event {}

class GoToC extends Event {}

class Start extends State {}

class A extends State {}

class B extends State {}

class C extends State {}

Future main() async {
  final dispatcher = Dispatcher(Start())
    ..from<Start>()
        .on<GoToA>((state, event) => A())
        .on<GoToC>((state, event) => C())
    ..from<A>().on<GoToB>((state, event) async {
      print(' .start');
      await Future.delayed(Duration(seconds: 1));
      print(' .end');
      return B();
    })
    ..from<B>().on<GoToC>((state, event) => C())
    ..end<C>();

  dispatcher.state.listen((s) {
    if (!dispatcher.isCompleted) {
      print('$s --> ' + dispatcher.supportedMutations.join("|"));
      print('next ?');
      final event = stdin.readLineSync().toLowerCase().trim();
      switch (event) {
        case 'a':
          dispatcher.dispatch(GoToA());
          break;
        case 'b':
          dispatcher.dispatch(GoToB());
          break;
        case 'c':
          dispatcher.dispatch(GoToC());
          break;
      }
    }
  });

  try {
    final last = await dispatcher.complete();
    print("Finished (${last.runtimeType})");
  } catch (e) {
    print("Error ($e)");
  }
}

Libraries

mutation