n-behaviour
Finite State Machine / Behaviour management for nodejs and the browser.
Install
npm install --save n-behaviour
Usage
'use strict'; ;; { const affect = StateAFFECTED entity entity !== null && entity !== null ; const unaffect = StateNEW entity entity === null && entity === null ; const cancel = StateCANCELLED entity entity !== null ; const states = StateNEW: affect cancel StateAFFECTED: processed: StateCLOSED // shorthand when only setting the state unaffect cancel StateCANCELLED: {} StateCLOSED: {} ; super states entity entity ; } // Public API { return this; } { return this; } { return this; } { return this; }
And then, in your code:
;; const behaviour = ;const entity = ; // Immutable.js Record for instance console; // State.NEW entity = behaviour;console; // State.AFFECTED entity = ; entity = behaviour;console; // State.CLOSED
Checking if transition is conform afterwards (ie, for server-side validation of client-side state change before persistence). 0 and 1-hop state transitions are valid.
const ourEntity = db;const theirEntity = newrequestparamsentity; // Object decoded from JSON payload, for instance const behaviour = ;ifbehaviour throw 'Invalid state transition detected !';