Giter Site home page Giter Site logo

state-machine's Introduction

Minimalistic finite state machine

A small finite state machine implementation inspired by machina.js. Works in browsers and Node.js.

Usage

Module exposes a single FSM(states, ...mixins) class that you use to create state machine. The first argument is states object: it represents each possible state in which machine can exist. Each state contains input handlers which, when invoked, can do something and transition machine into another state. Each state may contain _enter() and _exit() handlers which invoked when machine enters or exits given state.

You can also pass objects as additional arguments to constructor, each object’s property will be mixed-in to FSM instance.

var myFsm = new FSM({
    // by default FSM transitions to `initial` state. If you need
    // another state to be initial, simply put into `initial` property
    // require state name:
    // initial: "my-state"
    initial: {
        _enter() {
            console.log('initialize');
            // invoke `next` handler of current state with additional arguments
            this.handle('next', {foo: 'bar'});
        },
        next(obj) {
            console.log('invoked with', obj.foo);
            // transition to `ready` state
            this.transition('ready');
        }
    },
    ready: {
        // if you simply need to transition to another state when
        // handler invoked, use state name instead of function
        login: 'authorize',
        logout() {
            console.log('bye-bye');
            this.transition('initial');
        }
    },
    authorize() {
        console.log('Enter your username')
    }
});

API

  • fsm.current — current state name of FSM
  • fsm.handle(action, ...args) — invoke action handler of current state, if exists. Additional arguments will be passed to action handler.
  • fsm.transition(state) — transitions to given state.

All FSM instances are event emitters and allows to subscribe/unsubscribe to FSM life cycle events via on, once, off methods.

Available events:

  • transition – emitted when machine is about to enter into new state. Emits object with {from, to, action} properties.
  • no-handler — emitted when action handler that is not available for current state was invoked. Emits object with {action, state} properties.
  • handling — emitted when action handler for current state is about to be invoked. Emits object with {action, state} properties.
  • handling — emitted after action handler for current state was invoked. Emits object with {action, state} properties.

state-machine's People

Contributors

sergeche avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

sprime01

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.