0.2.4 • Published 5 years ago

@desicochrane/machine v0.2.4

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

Machine

Coverage Status

Tiny dependency-free state-machine implementation in javascript.

Get Started

Basic:

  1. npm install @desicochrane/machine
  2. Define a new state machine specification

    import Machine, { Transition } from '@desicochrane/machine'
    
    const spec = {
        __start__: 'off',
        off: {
            click: Transition('on'),
        },
        on: {
            click: Transition('off'),
        }
    }
  3. Instantiate your machine with data:

    const m = Machine(spec)
  4. Use the machine

    console.log(m.state) // "off"
    
    m.click()
    
    console.log(m.state) // "on"
  5. Export to Dot file:

    import { Dot } from  '@desicochrane/machine'
    import fs from 'fs'
    
    const dot = Dot(spec)
    
    fs.writeFileSync('machine.dot', dot);
  6. Optionally pass in data

    import Machine, { Transition } from '@desicochrane/machine'
    
    const spec = {
        __start__: 'off',
        off: {
            click: Transition('on', (m, args) => {
                m.data.count += args
            }),
        },
        on: {
            click: Transition('off', (m, args) => {
                m.data.count -= args
            }),
        }
    }
    
    
    const m = Machine(spec, { count: 0 })
    
    m.click(3)
    console.log(m.data.count) // 3
    
    m.click(2)
    console.log(m.data.count) // 1
0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago