Coder Social home page Coder Social logo

easy-fsm's Introduction

easy-fsm

An easy-to-use finite state machine system for JavaScript.

install

npm install easy-fsm

usage

const fsm = require('easy-fsm')

let machine = fsm.create({
  initial: 'init',
  states: {
    init: {
      on: {
        load: 'loading'
      }
    },
    loading: {
      on: {
        loaded: 'ready'
      }
    },
    ready: {
      on: {
        reload: 'loading'
      }
    }
  }
})

machine.onEnterLoading = () => console.log('onEnterLoading')
machine.onLoading = () => console.log('onLoading')
machine.onLeaveLoading = () => console.log('onLeaveLoading')

console.log(machine.getState()) // init

await machine.fire('load')
console.log(machine.getState()) // loading

await machine.fire('loaded')
console.log(machine.getState()) // ready

await machine.fire('reload')
console.log(machine.getState()) // loading

APIs

You can use fsm.create(options) to create a finite state machine.

  • fire(event, [...args])

    Fire an event on current state.

  • canFire(event)

    Detect if the specified event can be fired.

  • getState()

    Get the current state.

  • getLastState()

    Get the last state.

Every State has three actions:

  • onEnterState()
  • OnState()
  • OnLeaveState()

Note: The action names will be converted to camel-style, for example, a state named loading_some_files will corresponds to action onEnterLoadingSomeFiles, onLoadingSomeFiles and onLeaveLoadingSomeFiles.

easy-fsm's People

Contributors

oldj avatar

Watchers

James Cloos 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.