Skip to content

ngzh/tfsm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tfsm Build Status npm version

Typed finite state machine for typescript. This is intended to be used with typescript, compared with original javascript version.

Usage

$ npm install tfsm --save

Setup a state machine

import { StateMachine } from 'tfsm';

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
# build & 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

About

typed finite state machine for typescript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published