UNPKG

@div-int/typedfsm

Version:

A TypeScript finite state machine library

146 lines 5.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Typed; (function (Typed) { class Transition { constructor(fsm, fromState, fromAction, toState, toAction) { this._fsm = fsm; this._fromState = fromState; this._fromAction = fromAction; this._toState = toState; this._toAction = toAction; } get fromState() { return this._fromState; } get fromAction() { return this._fromAction; } get toState() { return this._toState; } get toAction() { return this._toAction; } to(toState, toAction) { if (this._fsm.isTransition(this.fromState, toState)) { return this; } if (this._toState === null) { this._toState = toState; this._toAction = toAction; this._fsm.transitions.push(this); return this; } this._fsm.from(this.fromState, this.fromAction).to(toState, toAction); return this; } toFrom(toFromState, toAction) { if (!this._fsm.isTransition(toFromState, this.fromState)) { this._fsm .from(toFromState, toAction) .to(this.fromState, this.fromAction); } if (!this._fsm.isTransition(this.fromState, toFromState)) { this._fsm .from(this.fromState, this.fromAction) .to(toFromState, toAction); } return this; } } class FSM { constructor(defaultState) { this._defaultState = defaultState; this._currentState = defaultState; } get defaultState() { return this._defaultState; } get currentState() { return this._currentState; } get transitions() { this._transitions = this._transitions ? this._transitions : []; return this._transitions; } set OnPreChange(onPreChange) { this._onPreChange = onPreChange; } set OnPostChange(onPostChange) { this._onPostChange = onPostChange; } reset() { this._currentState = this.defaultState; } findTransition(fromState, toState) { let result; this.transitions.every((value) => { if (value.fromState === fromState && value.toState === toState) { result = value; return false; } return true; }); return result; } isTransition(fromState, toState) { return !(this.findTransition(fromState, toState) == null); } findAction(fromState, toAction) { let result; this.transitions.every((value) => { if (value.fromState === fromState && value.toAction === toAction) { result = value; return false; } return true; }); return result; } isAction(fromState, toAction) { return !(this.findAction(fromState, toAction) == null); } canChange(changeState) { return this.isTransition(this.currentState, changeState); } change(changeState) { if (this._onPreChange) { if (!this._onPreChange(this.currentState, changeState, null)) { return this.currentState; } } let foundTransition; if ((foundTransition = this.findTransition(this.currentState, changeState))) { if (this._onPostChange) { this._onPostChange(this.currentState, changeState, foundTransition.toAction); } return (this._currentState = changeState); } return new Error(`Can't change from ${this.currentState} to ${changeState}`); } canDo(doAction) { return this.isAction(this.currentState, doAction); } do(doAction) { if (this._onPreChange) { if (!this._onPreChange(this.currentState, null, doAction)) { return this.currentState; } } let foundAction; if ((foundAction = this.findAction(this.currentState, doAction))) { if (this._onPostChange) { this._onPostChange(this.currentState, foundAction.toState, doAction); } return (this._currentState = foundAction.toState); } return new Error(`Can't perform action ${doAction} with state ${this.currentState}`); } from(fromState, fromAction) { return new Transition(this, fromState, fromAction, null, null); } } Typed.FSM = FSM; })(Typed = exports.Typed || (exports.Typed = {})); //# sourceMappingURL=typedfsm.js.map