tfsm
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

TFSM

Build Status

typed finite state machine for typescript

install

TODO

usage

setup a state machine

import { StateMachine } from '../src/state-machine';

enum States {
    ON,
    OFF,
    BROKE
}

enum Events {
    TOGGLE,
    KICK
}

let fsm = new StateMachine<States, Events>({
    initial: States.OFF,
    events: [
        { name: Events.TOGGLE, from: States.ON, to: States.OFF },
        { name: Events.TOGGLE, from: States.OFF, to: States.ON },
        { name: Events.KICK, from: [States.OFF, States.ON], to: States.BROKE }
    ]
});

trigger events and check current state

// trigger an event
fsm.input(Events.TOGGLE); // return true on success, false otherwise

// check state
fsm.is(States.ON); // => true or false

// reset state machine to initial
fsm.reset()

Examples

see files in tests/

Build & Run tests

# build
$ tsc
# run tests
$ ./run-tests.sh

Roadmap

  • Event-less style state machine(use statemachine.go(state) to direct transfer from state to state)
  • Queries like canGo and canInput
  • Async transaction
  • Automatically export state transfer map

Package Sidebar

Install

npm i tfsm

Weekly Downloads

0

Version

0.1.0

License

BSD

Last publish

Collaborators

  • zhfuzzy