Giter VIP home page Giter VIP logo

smjs's Introduction

A small state machine library

This is a javascript port of this library. The difference is this retains a tiny feature set of the original python implementation providing only the core and some other things. The user can extend it to their needs if they so desire.

Usage

import { StateMachine } from './state_machine.js';
class Model {
  constructor() {
    this.machine = new StateMachine(
      this,
      ['asleep', 'hangingOut', 'hungry', 'sweaty', 'savingTheWorld'],
      'asleep'
    );
    this.machine.addTransition({
      trigger: 'wakeUp',
      source: 'asleep',
      dest: 'hangingOut',
    });
    this.machine.addTransition({ trigger: 'nap', source: '*', dest: 'asleep' });
  }
}

The core is just a addTransition function to add state transitions (these can also be added as a parameter to the StateMachine constructor. The signature of the function is:

/**
 * @param {string} trigger - trigger function name.
 * @param {string} source - valid source state.
 * @param {string} dest - valid destination state.
 * @param {string} before - function to run before state transition.
 * @param {string} after - function to run after state transition.
 * @param {string[]} conditions - abort if any of the functions returns false.
 * @param {string[]} unless - abort if any of the functions returns true.
 */
export type Transition = {
  trigger: string;
  source: string;
  dest: string;
  before?: string;
  after?: string;
  conditions?: string[];
  unless?: string[];
};

Check the typescript declaration file for more details. Example programs can be found in the examples directory

For every state someState methods such as:

  • isSomeState (is the current state someState)
  • onEnterSomeState (fired every time we enter someState, except at the very beginning)
  • onExitSomeState (fired every time we leave someState) are added on initialization. It is recommended to not use spaces between state names to avoid awkward method calls at runtime (this.isSomeState() instead of this['isSome State'])

Credits

pytransitions/transitions for the state machine core and also some examples

LICENSE

MIT

smjs's People

Contributors

s1dsq 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.