sjs-smdeprecated

State Machine

Usage no npm install needed!

<script type="module">
  import sjsSm from 'https://cdn.skypack.dev/sjs-sm';
</script>

README

State Machine

Getting Started

1. Install:

$ npm install sjs-sm

2 Using

import {StateMachine, State} from 'sjs-sm';

const States = {
    CLOSED: 'CLOSED',
    OPENED: 'OPENED',
    LOCKED: 'LOCKED'
};

const Actions = {
    CLOSE: 'CLOSE',
    OPEN: 'OPEN',
    LOCK: 'LOCK',
    UNLOCK: 'UNLOCK'
};

let door = new StateMachine();

let opened = new State(States.OPENED);
opened.addTransition(Actions.CLOSE, States.CLOSED);
door.addState(opened);

let closed = new State(States.CLOSED);
closed.addTransition(Actions.OPEN, States.OPENED);
door.addState(closed);

door.start(States.CLOSED); // initial are closed

door.goto(Actions.LOCK) // raise error

door.goto(Actions.OPEN); // try open it
assert(door.currentState().name === States.OPENED);

License

MIT