Code Monkey home page Code Monkey logo

rx-state-machine's Introduction

Pub

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

How to use

Define states, events and side effects:

abstract class State {}
class Open extends State {}
class Closed extends State {}


abstract class Event {}
class OnOpening extends Event {}
class OnClosing extends Event {}


abstract class SideEffect {
  void call(State state, Event event);
}

class MakeSomeNoise extends SideEffect {
  @override
  void call(State state, Event event) => print("Ka-chunk-creeeeeeak-squeekie-squeekie");
}

Initialize state machine and declare state transitions:

final RxStateMachine<State, Event, SideEffect> _stateMachine =
    RxStateMachine<State, Event, SideEffect>.create((g) => g
      ..initialState(Open())
      ..state<Open>((b) => b
        ..on<OnClosing>((state, event) {
          return b.transitionTo(Closed(), MakeSomeNoise());
        }))
      ..state<Closed>((b) => b
        ..on<OnOpening>((state, event) {
          return b.transitionTo(Open(), MakeSomeNoise());
        }))
      ..onTransition((transition) {
        if (transition is Valid) {
          final item = transition as Valid;
          print("Valid transition: from [${item.fromState}] to [${item.toState}] by [${item.event}]");
          if(item.sideEffect is SideEffect) {
            item.sideEffect(item.toState, item.event);
          }
        } else if (transition is Invalid) {
          final item = transition as Invalid;
          print("Invalid transition: from [${item.fromState}] by [${item.event}]");
        }
      })
);

Observe the machine and react to changes.

_stateMachine.states.listen((state) {
    if(state is Open && !_security.authenticated()) {
      _security.alarm();
    }
  });

Perform state transitions:

  _stateMachine.transition(new OnClosing());
  //...
  _stateMachine.transition(new OnOpening());

rx-state-machine's People

Contributors

mi-sch-ka avatar

Stargazers

 avatar Arnie Le Mesch avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.