@barteh/machinize

a javascript library implements finite-state machine for application developement purpose

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
87May 24, 2021Jul 10, 2018Minified + gzip package size for @barteh/machinize in KB

Readme

Build Status Coverage Status

Machinize

a javascript library implements finite-state machine for application developement purpose

main features

  • change objects to machine automaticly.
  • define machine behavior once and create many instances.
  • automatic transitions with defined condition.
  • supports parametric observable (source state, destinition state or both) observable state changes based on rxjs.
  • supports blockable hooks contain(befor, after Enter and exit) from or to states.
  • data included access via this .
  • define auto transition by a condition.
  • imutable state.
  • actions that acces to this so data and parameters.
  • supports computed data .
  • many usefull features will be document soon


install

npm i @barteh/machinize

usage

import Machinize from "@barteh/machinize"
const machine = Machinize.init({
    name: "myMachine",
    data: {
        light: 55 // of 100
    },

    states: {
        "off": {
            description: "light is off"

        },
        "on": {
            description: "light is on",
            onEnter() { // auto off after 2s
                
                // setTimeout(() => {
                //     if (this.light < 60) 
                //         this.switchoff();
                // }, 1000);
            }
        }
      
    },
    transitions: {
        "switchon": {
            from: "off",
            to: "on"
        },
        "switchoff": {
            from: "on",
            to: "off",
            entryCondition(){
                return this.light<55;
            }
        }

    },
    actions: {
        do_on() {
            this.switchon();
        }
    }

});

const machinInstance = machine.createInstance({light: 31});

machinInstance
    .$observable()
    .subscribe(a => {
        console.log( a.message)
    })

machinInstance.do_on(); // light is on

setTimeout(() => {
    machinInstance.light=44; //light is off
}, 2000);

test

npm run test

build

npm run build

License: MIT 2017

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.