A library for finite state machine realization in Dart. Inspired by Tinder StateMachine library.

This library is a fork base on https://github.com/ookami-kb/fsm.


A simple usage example (using dfunc library for generating sealed classes):

import 'package:dfunc/dfunc.dart';
import 'package:mno_fsm/fsm.dart';

part 'fsm_example.g.dart';

void main() {
  final machine = StateMachine<State, Event, SideEffect>.create((g) => g
    ..state<Solid>((b) => b
          (Solid s, OnMelted e) => b.transitionTo(Liquid(), LogMelted())))
    ..state<Liquid>((b) => b
      ..onEnter((s) => print('Entering ${s.runtimeType} state'))
      ..onExit((s) => print('Exiting ${s.runtimeType} state'))
          (Liquid s, OnFroze e) => b.transitionTo(Solid(), LogFrozen()))
          (Liquid s, OnVaporized e) => b.transitionTo(Gas(), LogVaporized())))
    ..state<Gas>((b) => b
          (Gas s, OnCondensed e) => b.transitionTo(Liquid(), LogCondensed())))
    ..onTransition((t) => t.match((v) => print(v.sideEffect), (_) {})));

  print(machine.currentState is Solid); // TRUE

  print(machine.currentState is Liquid); // TRUE

  print(machine.currentState is Solid); // TRUE

abstract class State with _$State {}

class Solid extends State {}

class Liquid extends State {}

class Gas extends State {}

abstract class Event with _$Event {}

class OnMelted extends Event {}

class OnFroze extends Event {}

class OnVaporized extends Event {}

class OnCondensed extends Event {}

abstract class SideEffect with _$SideEffect {}

class LogMelted extends SideEffect {}

class LogFrozen extends SideEffect {}

class LogVaporized extends SideEffect {}

class LogCondensed extends SideEffect {}

Features and bugs

Please file feature requests and bugs at the issue tracker.