Comparing version 0.5.0 to 0.5.1
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _BaseConfigurator2 = require('./BaseConfigurator'); | ||
@@ -31,3 +27,3 @@ | ||
var _this = _possibleConstructorReturn(this, (AsyncActionConfigurator.__proto__ || Object.getPrototypeOf(AsyncActionConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
@@ -42,14 +38,10 @@ _this.config = { | ||
_createClass(AsyncActionConfigurator, [{ | ||
key: 'onSuccess', | ||
value: function onSuccess() { | ||
return this.config.successTrigger; | ||
} | ||
}, { | ||
key: 'onFailure', | ||
value: function onFailure() { | ||
return this.config.failureTrigger; | ||
} | ||
}]); | ||
AsyncActionConfigurator.prototype.onSuccess = function onSuccess() { | ||
return this.config.successTrigger; | ||
}; | ||
AsyncActionConfigurator.prototype.onFailure = function onFailure() { | ||
return this.config.failureTrigger; | ||
}; | ||
return AsyncActionConfigurator; | ||
@@ -56,0 +48,0 @@ }(_BaseConfigurator3.default); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _mapValues = require('../utils/mapValues'); | ||
@@ -26,31 +22,27 @@ | ||
_createClass(BaseConfigurator, [{ | ||
key: 'getAncestor', | ||
value: function getAncestor(type) { | ||
if (this.parent) { | ||
return this.parent instanceof type ? this.parent : this.parent.getAncestor(type); | ||
} | ||
return null; | ||
BaseConfigurator.prototype.getAncestor = function getAncestor(type) { | ||
if (this.parent) { | ||
return this.parent instanceof type ? this.parent : this.parent.getAncestor(type); | ||
} | ||
}, { | ||
key: 'buildConfig', | ||
value: function buildConfig() { | ||
var mapper = function mapper(value) { | ||
if (!value) { | ||
return value; | ||
} | ||
if (value instanceof BaseConfigurator) { | ||
return value.buildConfig(); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(mapper); | ||
} | ||
if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') { | ||
return (0, _mapValues2.default)(value, mapper); | ||
} | ||
return null; | ||
}; | ||
BaseConfigurator.prototype.buildConfig = function buildConfig() { | ||
var mapper = function mapper(value) { | ||
if (!value) { | ||
return value; | ||
}; | ||
return (0, _mapValues2.default)(this.config, mapper); | ||
} | ||
}]); | ||
} | ||
if (value instanceof BaseConfigurator) { | ||
return value.buildConfig(); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(mapper); | ||
} | ||
if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') { | ||
return (0, _mapValues2.default)(value, mapper); | ||
} | ||
return value; | ||
}; | ||
return (0, _mapValues2.default)(this.config, mapper); | ||
}; | ||
@@ -57,0 +49,0 @@ return BaseConfigurator; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.default = delegateToAncestor; | ||
@@ -7,0 +5,0 @@ function delegateToAncestor(constructor, ancestorConstructor) { |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _BaseConfigurator2 = require('./BaseConfigurator'); | ||
@@ -27,3 +23,3 @@ | ||
var _this = _possibleConstructorReturn(this, (GlobalConfigurator.__proto__ || Object.getPrototypeOf(GlobalConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
@@ -40,34 +36,27 @@ _this.config = { | ||
_createClass(GlobalConfigurator, [{ | ||
key: 'onStateEnter', | ||
value: function onStateEnter(hook) { | ||
this.config.stateEnterHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onStateExit', | ||
value: function onStateExit(hook) { | ||
this.config.stateExitHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onStateChange', | ||
value: function onStateChange(hook) { | ||
this.config.stateChangeHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onTransition', | ||
value: function onTransition(hook) { | ||
this.config.transitionHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onUnhandledEvent', | ||
value: function onUnhandledEvent(hook) { | ||
this.config.unhandledEventHooks.push(hook); | ||
return this; | ||
} | ||
}]); | ||
GlobalConfigurator.prototype.onStateEnter = function onStateEnter(hook) { | ||
this.config.stateEnterHooks.push(hook); | ||
return this; | ||
}; | ||
GlobalConfigurator.prototype.onStateExit = function onStateExit(hook) { | ||
this.config.stateExitHooks.push(hook); | ||
return this; | ||
}; | ||
GlobalConfigurator.prototype.onStateChange = function onStateChange(hook) { | ||
this.config.stateChangeHooks.push(hook); | ||
return this; | ||
}; | ||
GlobalConfigurator.prototype.onTransition = function onTransition(hook) { | ||
this.config.transitionHooks.push(hook); | ||
return this; | ||
}; | ||
GlobalConfigurator.prototype.onUnhandledEvent = function onUnhandledEvent(hook) { | ||
this.config.unhandledEventHooks.push(hook); | ||
return this; | ||
}; | ||
return GlobalConfigurator; | ||
@@ -74,0 +63,0 @@ }(_BaseConfigurator3.default); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.StateMachineConfigurator = undefined; | ||
@@ -7,0 +5,0 @@ |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _BaseConfigurator2 = require('./BaseConfigurator'); | ||
@@ -39,3 +35,3 @@ | ||
var _this = _possibleConstructorReturn(this, (StateConfigurator.__proto__ || Object.getPrototypeOf(StateConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
@@ -53,44 +49,36 @@ _this.config = { | ||
_createClass(StateConfigurator, [{ | ||
key: 'onEnter', | ||
value: function onEnter(action) { | ||
this.config.entryActions.push(action); | ||
return this; | ||
StateConfigurator.prototype.onEnter = function onEnter(action) { | ||
this.config.entryActions.push(action); | ||
return this; | ||
}; | ||
StateConfigurator.prototype.onExit = function onExit(action) { | ||
this.config.exitActions.push(action); | ||
return this; | ||
}; | ||
StateConfigurator.prototype.on = function on(event) { | ||
if (!this.config.events[event]) { | ||
this.config.events[event] = new _TriggerConfigurator2.default(this); | ||
} | ||
}, { | ||
key: 'onExit', | ||
value: function onExit(action) { | ||
this.config.exitActions.push(action); | ||
return this; | ||
} | ||
}, { | ||
key: 'on', | ||
value: function on(event) { | ||
if (!this.config.events[event]) { | ||
this.config.events[event] = new _TriggerConfigurator2.default(this); | ||
} | ||
return this.config.events[event]; | ||
} | ||
}, { | ||
key: 'onTimeout', | ||
value: function onTimeout(timeout) { | ||
var timerConfigurator = new _TimerConfigurator2.default(this, timeout); | ||
this.config.timers.push(timerConfigurator); | ||
return timerConfigurator; | ||
} | ||
}, { | ||
key: 'do', | ||
value: function _do(asyncAction) { | ||
var asyncActionConfigurator = new _AsyncActionConfigurator2.default(this, asyncAction); | ||
this.config.asyncActions.push(asyncActionConfigurator); | ||
return asyncActionConfigurator; | ||
} | ||
}, { | ||
key: 'submachine', | ||
value: function submachine(submachineConfig) { | ||
this.config.submachine = submachineConfig; | ||
return this; | ||
} | ||
}]); | ||
return this.config.events[event]; | ||
}; | ||
StateConfigurator.prototype.onTimeout = function onTimeout(timeout) { | ||
var timerConfigurator = new _TimerConfigurator2.default(this, timeout); | ||
this.config.timers.push(timerConfigurator); | ||
return timerConfigurator; | ||
}; | ||
StateConfigurator.prototype.do = function _do(asyncAction) { | ||
var asyncActionConfigurator = new _AsyncActionConfigurator2.default(this, asyncAction); | ||
this.config.asyncActions.push(asyncActionConfigurator); | ||
return asyncActionConfigurator; | ||
}; | ||
StateConfigurator.prototype.submachine = function submachine(submachineConfig) { | ||
this.config.submachine = submachineConfig; | ||
return this; | ||
}; | ||
return StateConfigurator; | ||
@@ -97,0 +85,0 @@ }(_BaseConfigurator3.default); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _BaseConfigurator2 = require('./BaseConfigurator'); | ||
@@ -39,3 +35,3 @@ | ||
var _this = _possibleConstructorReturn(this, (StateMachineConfigurator.__proto__ || Object.getPrototypeOf(StateMachineConfigurator)).call(this)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this)); | ||
@@ -50,34 +46,27 @@ _this.config = { | ||
_createClass(StateMachineConfigurator, [{ | ||
key: 'global', | ||
value: function global() { | ||
return this.config.global; | ||
StateMachineConfigurator.prototype.global = function global() { | ||
return this.config.global; | ||
}; | ||
StateMachineConfigurator.prototype.initialState = function initialState(state) { | ||
this.config.initialState = state; | ||
return this.state(state); | ||
}; | ||
StateMachineConfigurator.prototype.state = function state(_state) { | ||
if (!this.config.states[_state]) { | ||
this.config.states[_state] = new _StateConfigurator2.default(this); | ||
} | ||
}, { | ||
key: 'initialState', | ||
value: function initialState(state) { | ||
this.config.initialState = state; | ||
return this.state(state); | ||
} | ||
}, { | ||
key: 'state', | ||
value: function state(_state) { | ||
if (!this.config.states[_state]) { | ||
this.config.states[_state] = new _StateConfigurator2.default(this); | ||
} | ||
return this.config.states[_state]; | ||
} | ||
}, { | ||
key: 'getConfig', | ||
value: function getConfig() { | ||
return this.buildConfig(); | ||
} | ||
}, { | ||
key: 'start', | ||
value: function start() { | ||
var config = this.getConfig(); | ||
return _HierarchicalStateMachine2.default.start(config); | ||
} | ||
}]); | ||
return this.config.states[_state]; | ||
}; | ||
StateMachineConfigurator.prototype.getConfig = function getConfig() { | ||
return this.buildConfig(); | ||
}; | ||
StateMachineConfigurator.prototype.start = function start() { | ||
var config = this.getConfig(); | ||
return _HierarchicalStateMachine2.default.start(config); | ||
}; | ||
return StateMachineConfigurator; | ||
@@ -84,0 +73,0 @@ }(_BaseConfigurator3.default); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -25,3 +23,3 @@ var _TriggerConfigurator2 = require('./TriggerConfigurator'); | ||
var _this = _possibleConstructorReturn(this, (TrimerConfigurator.__proto__ || Object.getPrototypeOf(TrimerConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _TriggerConfigurator.call(this, parent)); | ||
@@ -28,0 +26,0 @@ _this.config.timeout = timeout; |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _BaseConfigurator2 = require('./BaseConfigurator'); | ||
@@ -27,3 +23,3 @@ | ||
var _this = _possibleConstructorReturn(this, (TransitionConfigurator.__proto__ || Object.getPrototypeOf(TransitionConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
@@ -39,16 +35,12 @@ _this.config = { | ||
_createClass(TransitionConfigurator, [{ | ||
key: 'withAction', | ||
value: function withAction(action) { | ||
this.config.actions.push(action); | ||
return this; | ||
} | ||
}, { | ||
key: 'withCondition', | ||
value: function withCondition(condition) { | ||
this.config.condition = condition; | ||
return this; | ||
} | ||
}]); | ||
TransitionConfigurator.prototype.withAction = function withAction(action) { | ||
this.config.actions.push(action); | ||
return this; | ||
}; | ||
TransitionConfigurator.prototype.withCondition = function withCondition(condition) { | ||
this.config.condition = condition; | ||
return this; | ||
}; | ||
return TransitionConfigurator; | ||
@@ -55,0 +47,0 @@ }(_BaseConfigurator3.default); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _BaseConfigurator2 = require('./BaseConfigurator'); | ||
@@ -31,3 +27,3 @@ | ||
var _this = _possibleConstructorReturn(this, (TriggerConfigurator.__proto__ || Object.getPrototypeOf(TriggerConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
@@ -40,26 +36,20 @@ _this.config = { | ||
_createClass(TriggerConfigurator, [{ | ||
key: 'transitionTo', | ||
value: function transitionTo(targetState) { | ||
return this.transition(targetState); | ||
} | ||
}, { | ||
key: 'selfTransition', | ||
value: function selfTransition() { | ||
return this.transition(null); | ||
} | ||
}, { | ||
key: 'internalTransition', | ||
value: function internalTransition() { | ||
return this.transition(null, true); | ||
} | ||
}, { | ||
key: 'transition', | ||
value: function transition(targetState, isInternal) { | ||
var transitionConfigurator = new _TransitionConfigurator2.default(this, targetState, isInternal); | ||
this.config.transitions.push(transitionConfigurator); | ||
return transitionConfigurator; | ||
} | ||
}]); | ||
TriggerConfigurator.prototype.transitionTo = function transitionTo(targetState) { | ||
return this.transition(targetState); | ||
}; | ||
TriggerConfigurator.prototype.selfTransition = function selfTransition() { | ||
return this.transition(null); | ||
}; | ||
TriggerConfigurator.prototype.internalTransition = function internalTransition() { | ||
return this.transition(null, true); | ||
}; | ||
TriggerConfigurator.prototype.transition = function transition(targetState, isInternal) { | ||
var transitionConfigurator = new _TransitionConfigurator2.default(this, targetState, isInternal); | ||
this.config.transitions.push(transitionConfigurator); | ||
return transitionConfigurator; | ||
}; | ||
return TriggerConfigurator; | ||
@@ -66,0 +56,0 @@ }(_BaseConfigurator3.default); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _StateMachine = require('./StateMachine'); | ||
@@ -30,84 +26,74 @@ | ||
_createClass(HierarchicalStateMachine, [{ | ||
key: 'getCurrentState', | ||
value: function getCurrentState() { | ||
return this.currentStateMachine.getCurrentState(); | ||
HierarchicalStateMachine.start = function start(config) { | ||
var taskScheduler = new _TaskScheduler2.default(); | ||
var rootStateMachine = void 0; | ||
var createContext = function createContext(stateMachine) { | ||
return { | ||
stateMachine: new HierarchicalStateMachine(rootStateMachine, stateMachine, taskScheduler) | ||
}; | ||
}; | ||
rootStateMachine = new _StateMachine2.default(config, taskScheduler, createContext); | ||
taskScheduler.execute(function () { | ||
return rootStateMachine.start(); | ||
}); | ||
return new HierarchicalStateMachine(rootStateMachine, rootStateMachine, taskScheduler); | ||
}; | ||
HierarchicalStateMachine.prototype.getCurrentState = function getCurrentState() { | ||
return this.currentStateMachine.getCurrentState(); | ||
}; | ||
HierarchicalStateMachine.prototype.getSubmachine = function getSubmachine() { | ||
var submachine = this.currentStateMachine.getSubmachine(); | ||
if (submachine) { | ||
return new HierarchicalStateMachine(this.rootStateMachine, submachine, this.taskScheduler); | ||
} | ||
}, { | ||
key: 'getSubmachine', | ||
value: function getSubmachine() { | ||
var submachine = this.currentStateMachine.getSubmachine(); | ||
if (submachine) { | ||
return new HierarchicalStateMachine(this.rootStateMachine, submachine, this.taskScheduler); | ||
return null; | ||
}; | ||
HierarchicalStateMachine.prototype.getStateHierarchy = function getStateHierarchy() { | ||
return this.getStateMachines().map(function (stateMachine) { | ||
return stateMachine.getCurrentState(); | ||
}); | ||
}; | ||
HierarchicalStateMachine.prototype.canHandle = function canHandle(event, eventPayload) { | ||
var stateMachines = this.getStateMachines(); | ||
for (var i = stateMachines.length - 1; i >= 0; i--) { | ||
if (stateMachines[i].canHandle(event, eventPayload)) { | ||
return true; | ||
} | ||
return null; | ||
} | ||
}, { | ||
key: 'getStateHierarchy', | ||
value: function getStateHierarchy() { | ||
return this.getStateMachines().map(function (stateMachine) { | ||
return stateMachine.getCurrentState(); | ||
}); | ||
} | ||
}, { | ||
key: 'canHandle', | ||
value: function canHandle(event, eventPayload) { | ||
var stateMachines = this.getStateMachines(); | ||
return false; | ||
}; | ||
HierarchicalStateMachine.prototype.handle = function handle(event, eventPayload) { | ||
var _this = this; | ||
this.taskScheduler.enqueue(function () { | ||
var stateMachines = _this.getStateMachines(); | ||
for (var i = stateMachines.length - 1; i >= 0; i--) { | ||
if (stateMachines[i].canHandle(event, eventPayload)) { | ||
return true; | ||
if (stateMachines[i].tryHandle(event, eventPayload)) { | ||
return; | ||
} | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: 'handle', | ||
value: function handle(event, eventPayload) { | ||
var _this = this; | ||
_this.currentStateMachine.handleUnhandledEvent(event, eventPayload); | ||
}); | ||
return this; | ||
}; | ||
this.taskScheduler.enqueue(function () { | ||
var stateMachines = _this.getStateMachines(); | ||
for (var i = stateMachines.length - 1; i >= 0; i--) { | ||
if (stateMachines[i].tryHandle(event, eventPayload)) { | ||
return; | ||
} | ||
} | ||
_this.currentStateMachine.handleUnhandledEvent(event, eventPayload); | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: 'getStateMachines', | ||
value: function getStateMachines() { | ||
var stateMachines = []; | ||
var stateMachine = this.rootStateMachine; | ||
do { | ||
stateMachines.push(stateMachine); | ||
stateMachine = stateMachine.getSubmachine(); | ||
} while (stateMachine); | ||
return stateMachines; | ||
} | ||
}, { | ||
key: 'toString', | ||
value: function toString() { | ||
return 'StateMachine(currentState: ' + this.getCurrentState() + ')'; | ||
} | ||
}], [{ | ||
key: 'start', | ||
value: function start(config) { | ||
var taskScheduler = new _TaskScheduler2.default(); | ||
var rootStateMachine = void 0; | ||
var createContext = function createContext(stateMachine) { | ||
return { | ||
stateMachine: new HierarchicalStateMachine(rootStateMachine, stateMachine, taskScheduler) | ||
}; | ||
}; | ||
rootStateMachine = new _StateMachine2.default(config, taskScheduler, createContext); | ||
taskScheduler.execute(function () { | ||
return rootStateMachine.start(); | ||
}); | ||
return new HierarchicalStateMachine(rootStateMachine, rootStateMachine, taskScheduler); | ||
} | ||
}]); | ||
HierarchicalStateMachine.prototype.getStateMachines = function getStateMachines() { | ||
var stateMachines = []; | ||
var stateMachine = this.rootStateMachine; | ||
do { | ||
stateMachines.push(stateMachine); | ||
stateMachine = stateMachine.getSubmachine(); | ||
} while (stateMachine); | ||
return stateMachines; | ||
}; | ||
HierarchicalStateMachine.prototype.toString = function toString() { | ||
return 'StateMachine(currentState: ' + this.getCurrentState() + ')'; | ||
}; | ||
return HierarchicalStateMachine; | ||
@@ -114,0 +100,0 @@ }(); |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _invokeEach = require('../utils/invokeEach'); | ||
@@ -49,259 +45,232 @@ | ||
_createClass(StateMachine, [{ | ||
key: 'getCurrentState', | ||
value: function getCurrentState() { | ||
return this.currentState; | ||
} | ||
}, { | ||
key: 'canHandle', | ||
value: function canHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
StateMachine.prototype.getCurrentState = function getCurrentState() { | ||
return this.currentState; | ||
}; | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
return !!this.getFirstAllowedTransitionForEvent(context); | ||
StateMachine.prototype.canHandle = function canHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
}, { | ||
key: 'tryHandle', | ||
value: function tryHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
var transitionConfig = this.getFirstAllowedTransitionForEvent(context); | ||
if (transitionConfig) { | ||
this.executeTransition(transitionConfig, context); | ||
return true; | ||
} | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
return !!this.getFirstAllowedTransitionForEvent(context); | ||
}; | ||
StateMachine.prototype.tryHandle = function tryHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
}, { | ||
key: 'handleUnhandledEvent', | ||
value: function handleUnhandledEvent(event, eventPayload) { | ||
if (this.config.global.unhandledEventHooks.length > 0) { | ||
(0, _invokeEach2.default)(this.config.global.unhandledEventHooks, event, this.currentState, this.createContextWithEvent(event, eventPayload)); | ||
} else { | ||
throw new Error('Unhandled event \'' + event + '\' in state \'' + this.currentState + '\'.'); | ||
} | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
var transitionConfig = this.getFirstAllowedTransitionForEvent(context); | ||
if (transitionConfig) { | ||
this.executeTransition(transitionConfig, context); | ||
return true; | ||
} | ||
}, { | ||
key: 'isStarted', | ||
value: function isStarted() { | ||
return this.currentState !== null; | ||
return false; | ||
}; | ||
StateMachine.prototype.handleUnhandledEvent = function handleUnhandledEvent(event, eventPayload) { | ||
if (this.config.global.unhandledEventHooks.length > 0) { | ||
(0, _invokeEach2.default)(this.config.global.unhandledEventHooks, event, this.currentState, this.createContextWithEvent(event, eventPayload)); | ||
} else { | ||
throw new Error('Unhandled event \'' + event + '\' in state \'' + this.currentState + '\'.'); | ||
} | ||
}, { | ||
key: 'start', | ||
value: function start() { | ||
if (!this.isStarted()) { | ||
this.enterState(this.config.initialState, this.createContext()); | ||
} | ||
}; | ||
StateMachine.prototype.isStarted = function isStarted() { | ||
return this.currentState !== null; | ||
}; | ||
StateMachine.prototype.start = function start() { | ||
if (!this.isStarted()) { | ||
this.enterState(this.config.initialState, this.createContext()); | ||
} | ||
}, { | ||
key: 'stop', | ||
value: function stop() { | ||
if (this.isStarted()) { | ||
this.exitState(this.createContext()); | ||
this.currentState = null; | ||
} | ||
}; | ||
StateMachine.prototype.stop = function stop() { | ||
if (this.isStarted()) { | ||
this.exitState(this.createContext()); | ||
this.currentState = null; | ||
} | ||
}, { | ||
key: 'getSubmachine', | ||
value: function getSubmachine() { | ||
return this.isStarted() ? this.submachines[this.currentState] : null; | ||
}; | ||
StateMachine.prototype.getSubmachine = function getSubmachine() { | ||
return this.isStarted() ? this.submachines[this.currentState] : null; | ||
}; | ||
StateMachine.prototype.executeTransition = function executeTransition(transitionConfig, context) { | ||
if (!transitionConfig.isInternal) { | ||
this.exitState(context); | ||
} | ||
}, { | ||
key: 'executeTransition', | ||
value: function executeTransition(transitionConfig, context) { | ||
if (!transitionConfig.isInternal) { | ||
this.exitState(context); | ||
} | ||
var nextState = transitionConfig.targetState !== null ? transitionConfig.targetState : this.currentState; | ||
var nextState = transitionConfig.targetState !== null ? transitionConfig.targetState : this.currentState; | ||
(0, _invokeEach2.default)(this.config.global.transitionHooks, this.currentState, nextState, context); | ||
(0, _invokeEach2.default)(transitionConfig.actions, this.currentState, nextState, context); | ||
(0, _invokeEach2.default)(this.config.global.transitionHooks, this.currentState, nextState, context); | ||
(0, _invokeEach2.default)(transitionConfig.actions, this.currentState, nextState, context); | ||
if (!transitionConfig.isInternal) { | ||
this.enterState(nextState, context); | ||
} | ||
if (!transitionConfig.isInternal) { | ||
this.enterState(nextState, context); | ||
} | ||
}, { | ||
key: 'enterState', | ||
value: function enterState(state, context) { | ||
(0, _invokeEach2.default)(this.config.global.stateEnterHooks, state, context); | ||
}; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.entryActions, state, context); | ||
} | ||
StateMachine.prototype.enterState = function enterState(state, context) { | ||
(0, _invokeEach2.default)(this.config.global.stateEnterHooks, state, context); | ||
if (this.currentState !== null && this.currentState !== state) { | ||
(0, _invokeEach2.default)(this.config.global.stateChangeHooks, this.currentState, state, context); | ||
} | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.entryActions, state, context); | ||
} | ||
try { | ||
this.startAsyncActions(state, context); | ||
this.startTimers(state); | ||
this.startSubmachines(state); | ||
} catch (error) { | ||
this.stopTimers(); | ||
this.cancelAsyncActions(); | ||
throw error; | ||
} | ||
if (this.currentState !== null && this.currentState !== state) { | ||
(0, _invokeEach2.default)(this.config.global.stateChangeHooks, this.currentState, state, context); | ||
} | ||
this.currentState = state; | ||
} | ||
}, { | ||
key: 'exitState', | ||
value: function exitState(context) { | ||
this.stopSubmachines(); | ||
try { | ||
this.startAsyncActions(state, context); | ||
this.startTimers(state); | ||
this.startSubmachines(state); | ||
} catch (error) { | ||
this.stopTimers(); | ||
this.cancelAsyncActions(); | ||
throw error; | ||
} | ||
(0, _invokeEach2.default)(this.config.global.stateExitHooks, this.currentState, context); | ||
this.currentState = state; | ||
}; | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.exitActions, this.currentState, context); | ||
} | ||
} | ||
}, { | ||
key: 'startAsyncActions', | ||
value: function startAsyncActions(state, context) { | ||
var _this = this; | ||
StateMachine.prototype.exitState = function exitState(context) { | ||
this.stopSubmachines(); | ||
this.stopTimers(); | ||
this.cancelAsyncActions(); | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
stateConfig.asyncActions.forEach(function (asyncActionConfig) { | ||
return _this.startAsyncAction(asyncActionConfig, state, context); | ||
}); | ||
} | ||
(0, _invokeEach2.default)(this.config.global.stateExitHooks, this.currentState, context); | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.exitActions, this.currentState, context); | ||
} | ||
}, { | ||
key: 'startAsyncAction', | ||
value: function startAsyncAction(asyncActionConfig, state, context) { | ||
var action = asyncActionConfig.action, | ||
successTrigger = asyncActionConfig.successTrigger, | ||
failureTrigger = asyncActionConfig.failureTrigger; | ||
}; | ||
var handleComplete = this.handleAsyncActionComplete; | ||
action(state, context).then(function (result) { | ||
return handleComplete(successTrigger, { result: result }); | ||
}, function (error) { | ||
return handleComplete(failureTrigger, { error: error }); | ||
StateMachine.prototype.startAsyncActions = function startAsyncActions(state, context) { | ||
var _this = this; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
stateConfig.asyncActions.forEach(function (asyncActionConfig) { | ||
return _this.startAsyncAction(asyncActionConfig, state, context); | ||
}); | ||
this.asyncActionCancelers = this.asyncActionCancelers || []; | ||
this.asyncActionCancelers.push(function () { | ||
handleComplete = noop; | ||
}); | ||
} | ||
}, { | ||
key: 'cancelAsyncActions', | ||
value: function cancelAsyncActions() { | ||
if (this.asyncActionCancelers) { | ||
(0, _invokeEach2.default)(this.asyncActionCancelers); | ||
this.asyncActionCancelers = null; | ||
} | ||
}; | ||
StateMachine.prototype.startAsyncAction = function startAsyncAction(asyncActionConfig, state, context) { | ||
var action = asyncActionConfig.action, | ||
successTrigger = asyncActionConfig.successTrigger, | ||
failureTrigger = asyncActionConfig.failureTrigger; | ||
var handleComplete = this.handleAsyncActionComplete; | ||
action(state, context).then(function (result) { | ||
return handleComplete(successTrigger, { result: result }); | ||
}, function (error) { | ||
return handleComplete(failureTrigger, { error: error }); | ||
}); | ||
this.asyncActionCancelers = this.asyncActionCancelers || []; | ||
this.asyncActionCancelers.push(function () { | ||
handleComplete = noop; | ||
}); | ||
}; | ||
StateMachine.prototype.cancelAsyncActions = function cancelAsyncActions() { | ||
if (this.asyncActionCancelers) { | ||
(0, _invokeEach2.default)(this.asyncActionCancelers); | ||
this.asyncActionCancelers = null; | ||
} | ||
}, { | ||
key: 'handleAsyncActionComplete', | ||
value: function handleAsyncActionComplete(triggerConfig, additionalContext) { | ||
var context = (0, _merge2.default)(this.createContext(), additionalContext); | ||
this.executeTrigger(triggerConfig, context); | ||
}; | ||
StateMachine.prototype.handleAsyncActionComplete = function handleAsyncActionComplete(triggerConfig, additionalContext) { | ||
var context = (0, _merge2.default)(this.createContext(), additionalContext); | ||
this.executeTrigger(triggerConfig, context); | ||
}; | ||
StateMachine.prototype.startTimers = function startTimers(state) { | ||
var _this2 = this; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.timers.length > 0) { | ||
this.timerIDs = stateConfig.timers.map(function (timerConfig) { | ||
return setTimeout(_this2.handleTimeout, timerConfig.timeout, timerConfig); | ||
}); | ||
} | ||
}, { | ||
key: 'startTimers', | ||
value: function startTimers(state) { | ||
var _this2 = this; | ||
}; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.timers.length > 0) { | ||
this.timerIDs = stateConfig.timers.map(function (timerConfig) { | ||
return setTimeout(_this2.handleTimeout, timerConfig.timeout, timerConfig); | ||
}); | ||
} | ||
StateMachine.prototype.stopTimers = function stopTimers() { | ||
if (this.timerIDs) { | ||
this.timerIDs.forEach(clearTimeout); | ||
this.timerIDs = null; | ||
} | ||
}, { | ||
key: 'stopTimers', | ||
value: function stopTimers() { | ||
if (this.timerIDs) { | ||
this.timerIDs.forEach(clearTimeout); | ||
this.timerIDs = null; | ||
}; | ||
StateMachine.prototype.handleTimeout = function handleTimeout(timerConfig) { | ||
this.executeTrigger(timerConfig, this.createContext()); | ||
}; | ||
StateMachine.prototype.startSubmachines = function startSubmachines(state) { | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.submachine) { | ||
if (!this.submachines[state]) { | ||
this.submachines[state] = new StateMachine(stateConfig.submachine, this.taskScheduler, this.contextFactory); | ||
} | ||
this.submachines[state].start(); | ||
} | ||
}, { | ||
key: 'handleTimeout', | ||
value: function handleTimeout(timerConfig) { | ||
this.executeTrigger(timerConfig, this.createContext()); | ||
}; | ||
StateMachine.prototype.stopSubmachines = function stopSubmachines() { | ||
var submachine = this.submachines[this.currentState]; | ||
if (submachine) { | ||
submachine.stop(); | ||
} | ||
}, { | ||
key: 'startSubmachines', | ||
value: function startSubmachines(state) { | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.submachine) { | ||
if (!this.submachines[state]) { | ||
this.submachines[state] = new StateMachine(stateConfig.submachine, this.taskScheduler, this.contextFactory); | ||
} | ||
this.submachines[state].start(); | ||
} | ||
}; | ||
StateMachine.prototype.createContext = function createContext() { | ||
return this.contextFactory(this); | ||
}; | ||
StateMachine.prototype.createContextWithEvent = function createContextWithEvent(event, eventPayload) { | ||
var context = this.createContext(); | ||
context.event = event; | ||
if (eventPayload !== undefined) { | ||
context.eventPayload = eventPayload; | ||
} | ||
}, { | ||
key: 'stopSubmachines', | ||
value: function stopSubmachines() { | ||
var submachine = this.submachines[this.currentState]; | ||
if (submachine) { | ||
submachine.stop(); | ||
return context; | ||
}; | ||
StateMachine.getFirstAllowedTransition = function getFirstAllowedTransition(transitions, context) { | ||
for (var i = 0; i < transitions.length; i++) { | ||
if (!transitions[i].condition || transitions[i].condition(context)) { | ||
return transitions[i]; | ||
} | ||
} | ||
}, { | ||
key: 'createContext', | ||
value: function createContext() { | ||
return this.contextFactory(this); | ||
} | ||
}, { | ||
key: 'createContextWithEvent', | ||
value: function createContextWithEvent(event, eventPayload) { | ||
var context = this.createContext(); | ||
context.event = event; | ||
if (eventPayload !== undefined) { | ||
context.eventPayload = eventPayload; | ||
} | ||
return context; | ||
} | ||
}, { | ||
key: 'getFirstAllowedTransitionForEvent', | ||
value: function getFirstAllowedTransitionForEvent(context) { | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (!stateConfig) { | ||
return null; | ||
} | ||
return null; | ||
}; | ||
var eventConfig = stateConfig.events[context.event]; | ||
return eventConfig ? StateMachine.getFirstAllowedTransition(eventConfig.transitions, context) : null; | ||
StateMachine.prototype.getFirstAllowedTransitionForEvent = function getFirstAllowedTransitionForEvent(context) { | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (!stateConfig) { | ||
return null; | ||
} | ||
}, { | ||
key: 'executeTrigger', | ||
value: function executeTrigger(triggerConfig, context) { | ||
var _this3 = this; | ||
this.taskScheduler.execute(function () { | ||
var transitionConfig = StateMachine.getFirstAllowedTransition(triggerConfig.transitions, context); | ||
if (transitionConfig) { | ||
_this3.executeTransition(transitionConfig, context); | ||
} | ||
}); | ||
} | ||
}], [{ | ||
key: 'getFirstAllowedTransition', | ||
value: function getFirstAllowedTransition(transitions, context) { | ||
for (var i = 0; i < transitions.length; i++) { | ||
if (!transitions[i].condition || transitions[i].condition(context)) { | ||
return transitions[i]; | ||
} | ||
var eventConfig = stateConfig.events[context.event]; | ||
return eventConfig ? StateMachine.getFirstAllowedTransition(eventConfig.transitions, context) : null; | ||
}; | ||
StateMachine.prototype.executeTrigger = function executeTrigger(triggerConfig, context) { | ||
var _this3 = this; | ||
this.taskScheduler.execute(function () { | ||
var transitionConfig = StateMachine.getFirstAllowedTransition(triggerConfig.transitions, context); | ||
if (transitionConfig) { | ||
_this3.executeTransition(transitionConfig, context); | ||
} | ||
return null; | ||
} | ||
}]); | ||
}); | ||
}; | ||
@@ -308,0 +277,0 @@ return StateMachine; |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -19,33 +15,29 @@ | ||
_createClass(TaskScheduler, [{ | ||
key: 'enqueue', | ||
value: function enqueue(task) { | ||
if (this.isBusy) { | ||
this.queue.push(task); | ||
} else { | ||
this.execute(task); | ||
} | ||
TaskScheduler.prototype.enqueue = function enqueue(task) { | ||
if (this.isBusy) { | ||
this.queue.push(task); | ||
} else { | ||
this.execute(task); | ||
} | ||
}, { | ||
key: 'execute', | ||
value: function execute(task) { | ||
if (this.isBusy) { | ||
throw new Error('Cannot execute task because another task is already running.'); | ||
}; | ||
TaskScheduler.prototype.execute = function execute(task) { | ||
if (this.isBusy) { | ||
throw new Error('Cannot execute task because another task is already running.'); | ||
} | ||
this.isBusy = true; | ||
try { | ||
task(); | ||
while (this.queue.length > 0) { | ||
var nextTask = this.queue.shift(); | ||
nextTask(); | ||
} | ||
this.isBusy = true; | ||
try { | ||
task(); | ||
while (this.queue.length > 0) { | ||
var nextTask = this.queue.shift(); | ||
nextTask(); | ||
} | ||
} finally { | ||
// Clean up | ||
if (this.queue.length > 0) { | ||
this.queue = []; | ||
} | ||
this.isBusy = false; | ||
} finally { | ||
// Clean up | ||
if (this.queue.length > 0) { | ||
this.queue = []; | ||
} | ||
this.isBusy = false; | ||
} | ||
}]); | ||
}; | ||
@@ -52,0 +44,0 @@ return TaskScheduler; |
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
@@ -7,0 +5,0 @@ var _configuration = require('./configuration'); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.default = invokeEach; | ||
@@ -7,0 +5,0 @@ function invokeEach(fns) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.default = mapValues; | ||
@@ -7,0 +5,0 @@ function mapValues(obj, callback) { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.__esModule = true; | ||
exports.default = merge; | ||
@@ -7,0 +5,0 @@ function merge(target, source) { |
{ | ||
"name": "finity", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "A finite state machine library for Node.js and the browser with a friendly configuration DSL", | ||
@@ -33,23 +33,24 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-cli": "^6.18.0", | ||
"babel-core": "^6.21.0", | ||
"babel-register": "^6.18.0", | ||
"babel-preset-es2015": "^6.18.0", | ||
"babel-preset-stage-0": "^6.16.0", | ||
"babel-loader": "^6.2.10", | ||
"jasmine": "^2.5.2", | ||
"jasmine-spec-reporter": "^3.0.0", | ||
"eslint": "^3.13.0", | ||
"eslint-config-airbnb": "^13.0.0", | ||
"babel-cli": "^6.23.0", | ||
"babel-core": "^6.23.1", | ||
"babel-register": "^6.23.0", | ||
"babel-preset-es2015": "^6.22.0", | ||
"babel-preset-stage-0": "^6.22.0", | ||
"babel-loader": "^6.3.2", | ||
"jasmine": "^2.5.3", | ||
"jasmine-spec-reporter": "^3.2.0", | ||
"eslint": "^3.16.1", | ||
"eslint-config-airbnb": "^14.1.0", | ||
"eslint-plugin-import": "^2.2.0", | ||
"eslint-plugin-react": "^6.8.0", | ||
"eslint-plugin-jsx-a11y": "^2.2.3", | ||
"eslint-plugin-react": "^6.10.0", | ||
"eslint-plugin-jsx-a11y": "^4.0.0", | ||
"babel-eslint": "^7.1.1", | ||
"isparta": "^4.0.0", | ||
"coveralls": "^2.11.15", | ||
"rimraf": "^2.5.4", | ||
"webpack": "^1.14.0", | ||
"coveralls": "^2.11.16", | ||
"rimraf": "^2.6.0", | ||
"webpack": "^2.2.1", | ||
"cross-env": "^3.1.4", | ||
"typescript": "^2.1.4", | ||
"shx": "^0.2.2" | ||
"typescript": "^2.2.1", | ||
"shx": "^0.2.2", | ||
"gzip-size-cli": "^2.0.0" | ||
}, | ||
@@ -59,5 +60,5 @@ "scripts": { | ||
"build:commonjs": "babel src --out-dir lib && shx cp src/index.commonjs.js lib/index.js", | ||
"build:umd": "cross-env NODE_ENV=development webpack src/index.commonjs.js umd/Finity.js", | ||
"build:umd:min": "cross-env NODE_ENV=production webpack -p src/index.commonjs.js umd/Finity.min.js", | ||
"build": "npm run clean && npm run build:commonjs && npm run build:umd && npm run build:umd:min", | ||
"build:umd": "webpack src/index.commonjs.js umd/Finity.js", | ||
"build:umd:min": "webpack -p src/index.commonjs.js umd/Finity.min.js", | ||
"build": "npm run clean && npm run build:commonjs && npm run build:umd && npm run build:umd:min && gzip-size umd/Finity.min.js", | ||
"test:typings": "tsc", | ||
@@ -64,0 +65,0 @@ "test": "node test/support/jasmineRunner.js && npm run test:typings", |
@@ -6,2 +6,4 @@ # finity | ||
[![Coverage Status](https://coveralls.io/repos/nickuraltsev/finity/badge.svg?branch=master&service=github)](https://coveralls.io/github/nickuraltsev/finity?branch=master) | ||
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nickuraltsev/finity/master/LICENSE) | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/nickuraltsev/finity.svg)](https://greenkeeper.io/) | ||
@@ -20,2 +22,3 @@ A finite state machine library for Node.js and the browser with a friendly configuration DSL. | ||
- No external dependencies | ||
- 3.6 kB minified and gzipped | ||
- TypeScript typings | ||
@@ -25,3 +28,3 @@ | ||
Using [npm](https://www.npmjs.org/): | ||
Install `finity` using [npm](https://www.npmjs.org/): | ||
@@ -32,2 +35,12 @@ ``` | ||
Then you can import it using ES2015 or CommonJS modules: | ||
```js | ||
// ES2015 | ||
import Finity from 'finity'; | ||
// CommonJS | ||
const Finity = require('finity'); | ||
``` | ||
The UMD build is available on [unpkg](https://unpkg.com/): | ||
@@ -42,4 +55,2 @@ | ||
```javascript | ||
import Finity from 'finity'; | ||
const worker = Finity | ||
@@ -46,0 +57,0 @@ .configure() |
@@ -8,25 +8,6 @@ # Roadmap | ||
- [x] Promise triggers | ||
- [ ] Hierarchical state machines *(in progress)* | ||
- [x] Hierarchical state machines | ||
- [ ] Ignoring events | ||
- [ ] Persistence support | ||
### Hierarchical state machines | ||
```javascript | ||
const submachineConfig = Finity | ||
.configure() | ||
.initialState('substate2A') | ||
.on('event2').transitionTo('substate2B') | ||
.getConfig(); | ||
const stateMachine = Finity | ||
.configure() | ||
.initialState('state1') | ||
.on('event1').transitionTo('state2') | ||
.state('state2') | ||
.on('event3').transitionTo('state3') | ||
.submachine(submachineConfig) | ||
.start(); | ||
``` | ||
### Ignoring events | ||
@@ -33,0 +14,0 @@ |
1864
umd/Finity.js
@@ -24,5 +24,5 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
@@ -34,3 +34,3 @@ | ||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
/******/ module.l = true; | ||
@@ -48,2 +48,28 @@ /******/ // Return the exports of the module | ||
/******/ // identity function for calling harmony imports with the correct context | ||
/******/ __webpack_require__.i = function(value) { return value; }; | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ // __webpack_public_path__ | ||
@@ -53,3 +79,3 @@ /******/ __webpack_require__.p = ""; | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ return __webpack_require__(__webpack_require__.s = 17); | ||
/******/ }) | ||
@@ -59,1241 +85,1123 @@ /************************************************************************/ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
/* eslint-disable */ | ||
'use strict'; | ||
"use strict"; | ||
var Finity = __webpack_require__(9).default; | ||
var merge = __webpack_require__(8).default; | ||
merge(exports, Finity); | ||
exports.__esModule = true; | ||
// Allow the use of the default import syntax in TypeScript (import Finity from 'finity') | ||
exports.default = Finity; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _mapValues = __webpack_require__(16); | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
var _mapValues2 = _interopRequireDefault(_mapValues); | ||
'use strict'; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var BaseConfigurator = function () { | ||
function BaseConfigurator(parent) { | ||
_classCallCheck(this, BaseConfigurator); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
this.parent = parent; | ||
} | ||
var _mapValues = __webpack_require__(17); | ||
BaseConfigurator.prototype.getAncestor = function getAncestor(type) { | ||
if (this.parent) { | ||
return this.parent instanceof type ? this.parent : this.parent.getAncestor(type); | ||
} | ||
return null; | ||
}; | ||
var _mapValues2 = _interopRequireDefault(_mapValues); | ||
BaseConfigurator.prototype.buildConfig = function buildConfig() { | ||
var mapper = function mapper(value) { | ||
if (!value) { | ||
return value; | ||
} | ||
if (value instanceof BaseConfigurator) { | ||
return value.buildConfig(); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(mapper); | ||
} | ||
if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') { | ||
return (0, _mapValues2.default)(value, mapper); | ||
} | ||
return value; | ||
}; | ||
return (0, _mapValues2.default)(this.config, mapper); | ||
}; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
return BaseConfigurator; | ||
}(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
exports.default = BaseConfigurator; | ||
var BaseConfigurator = function () { | ||
function BaseConfigurator(parent) { | ||
_classCallCheck(this, BaseConfigurator); | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
this.parent = parent; | ||
} | ||
"use strict"; | ||
_createClass(BaseConfigurator, [{ | ||
key: 'getAncestor', | ||
value: function getAncestor(type) { | ||
if (this.parent) { | ||
return this.parent instanceof type ? this.parent : this.parent.getAncestor(type); | ||
} | ||
return null; | ||
} | ||
}, { | ||
key: 'buildConfig', | ||
value: function buildConfig() { | ||
var mapper = function mapper(value) { | ||
if (!value) { | ||
return value; | ||
} | ||
if (value instanceof BaseConfigurator) { | ||
return value.buildConfig(); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.map(mapper); | ||
} | ||
if (value && (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') { | ||
return (0, _mapValues2.default)(value, mapper); | ||
} | ||
return value; | ||
}; | ||
return (0, _mapValues2.default)(this.config, mapper); | ||
} | ||
}]); | ||
return BaseConfigurator; | ||
}(); | ||
exports.__esModule = true; | ||
exports.default = BaseConfigurator; | ||
var _BaseConfigurator2 = __webpack_require__(0); | ||
/***/ }, | ||
/* 2 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
'use strict'; | ||
var _TransitionConfigurator = __webpack_require__(6); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _TransitionConfigurator2 = _interopRequireDefault(_TransitionConfigurator); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _BaseConfigurator2 = __webpack_require__(1); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
var _TransitionConfigurator = __webpack_require__(6); | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var _TransitionConfigurator2 = _interopRequireDefault(_TransitionConfigurator); | ||
var TriggerConfigurator = function (_BaseConfigurator) { | ||
_inherits(TriggerConfigurator, _BaseConfigurator); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function TriggerConfigurator(parent) { | ||
_classCallCheck(this, TriggerConfigurator); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
_this.config = { | ||
transitions: [] | ||
}; | ||
return _this; | ||
} | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
TriggerConfigurator.prototype.transitionTo = function transitionTo(targetState) { | ||
return this.transition(targetState); | ||
}; | ||
var TriggerConfigurator = function (_BaseConfigurator) { | ||
_inherits(TriggerConfigurator, _BaseConfigurator); | ||
TriggerConfigurator.prototype.selfTransition = function selfTransition() { | ||
return this.transition(null); | ||
}; | ||
function TriggerConfigurator(parent) { | ||
_classCallCheck(this, TriggerConfigurator); | ||
TriggerConfigurator.prototype.internalTransition = function internalTransition() { | ||
return this.transition(null, true); | ||
}; | ||
var _this = _possibleConstructorReturn(this, (TriggerConfigurator.__proto__ || Object.getPrototypeOf(TriggerConfigurator)).call(this, parent)); | ||
TriggerConfigurator.prototype.transition = function transition(targetState, isInternal) { | ||
var transitionConfigurator = new _TransitionConfigurator2.default(this, targetState, isInternal); | ||
this.config.transitions.push(transitionConfigurator); | ||
return transitionConfigurator; | ||
}; | ||
_this.config = { | ||
transitions: [] | ||
}; | ||
return _this; | ||
} | ||
return TriggerConfigurator; | ||
}(_BaseConfigurator3.default); | ||
_createClass(TriggerConfigurator, [{ | ||
key: 'transitionTo', | ||
value: function transitionTo(targetState) { | ||
return this.transition(targetState); | ||
} | ||
}, { | ||
key: 'selfTransition', | ||
value: function selfTransition() { | ||
return this.transition(null); | ||
} | ||
}, { | ||
key: 'internalTransition', | ||
value: function internalTransition() { | ||
return this.transition(null, true); | ||
} | ||
}, { | ||
key: 'transition', | ||
value: function transition(targetState, isInternal) { | ||
var transitionConfigurator = new _TransitionConfigurator2.default(this, targetState, isInternal); | ||
this.config.transitions.push(transitionConfigurator); | ||
return transitionConfigurator; | ||
} | ||
}]); | ||
exports.default = TriggerConfigurator; | ||
return TriggerConfigurator; | ||
}(_BaseConfigurator3.default); | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
exports.default = TriggerConfigurator; | ||
"use strict"; | ||
/***/ }, | ||
exports.__esModule = true; | ||
exports.default = merge; | ||
function merge(target, source) { | ||
Object.keys(source).forEach(function (key) { | ||
target[key] = source[key]; | ||
}); | ||
return target; | ||
} | ||
/***/ }), | ||
/* 3 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
exports.__esModule = true; | ||
var _BaseConfigurator2 = __webpack_require__(1); | ||
var _BaseConfigurator2 = __webpack_require__(0); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
var _TriggerConfigurator = __webpack_require__(2); | ||
var _TriggerConfigurator = __webpack_require__(1); | ||
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator); | ||
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var AsyncActionConfigurator = function (_BaseConfigurator) { | ||
_inherits(AsyncActionConfigurator, _BaseConfigurator); | ||
var AsyncActionConfigurator = function (_BaseConfigurator) { | ||
_inherits(AsyncActionConfigurator, _BaseConfigurator); | ||
function AsyncActionConfigurator(parent, action) { | ||
_classCallCheck(this, AsyncActionConfigurator); | ||
function AsyncActionConfigurator(parent, action) { | ||
_classCallCheck(this, AsyncActionConfigurator); | ||
var _this = _possibleConstructorReturn(this, (AsyncActionConfigurator.__proto__ || Object.getPrototypeOf(AsyncActionConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
_this.config = { | ||
action: action, | ||
successTrigger: new _TriggerConfigurator2.default(_this), | ||
failureTrigger: new _TriggerConfigurator2.default(_this) | ||
}; | ||
return _this; | ||
} | ||
_this.config = { | ||
action: action, | ||
successTrigger: new _TriggerConfigurator2.default(_this), | ||
failureTrigger: new _TriggerConfigurator2.default(_this) | ||
}; | ||
return _this; | ||
} | ||
_createClass(AsyncActionConfigurator, [{ | ||
key: 'onSuccess', | ||
value: function onSuccess() { | ||
return this.config.successTrigger; | ||
} | ||
}, { | ||
key: 'onFailure', | ||
value: function onFailure() { | ||
return this.config.failureTrigger; | ||
} | ||
}]); | ||
AsyncActionConfigurator.prototype.onSuccess = function onSuccess() { | ||
return this.config.successTrigger; | ||
}; | ||
return AsyncActionConfigurator; | ||
}(_BaseConfigurator3.default); | ||
AsyncActionConfigurator.prototype.onFailure = function onFailure() { | ||
return this.config.failureTrigger; | ||
}; | ||
exports.default = AsyncActionConfigurator; | ||
return AsyncActionConfigurator; | ||
}(_BaseConfigurator3.default); | ||
/***/ }, | ||
exports.default = AsyncActionConfigurator; | ||
/***/ }), | ||
/* 4 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
exports.__esModule = true; | ||
var _BaseConfigurator2 = __webpack_require__(1); | ||
var _BaseConfigurator2 = __webpack_require__(0); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var GlobalConfigurator = function (_BaseConfigurator) { | ||
_inherits(GlobalConfigurator, _BaseConfigurator); | ||
var GlobalConfigurator = function (_BaseConfigurator) { | ||
_inherits(GlobalConfigurator, _BaseConfigurator); | ||
function GlobalConfigurator(parent) { | ||
_classCallCheck(this, GlobalConfigurator); | ||
function GlobalConfigurator(parent) { | ||
_classCallCheck(this, GlobalConfigurator); | ||
var _this = _possibleConstructorReturn(this, (GlobalConfigurator.__proto__ || Object.getPrototypeOf(GlobalConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
_this.config = { | ||
stateEnterHooks: [], | ||
stateExitHooks: [], | ||
stateChangeHooks: [], | ||
transitionHooks: [], | ||
unhandledEventHooks: [] | ||
}; | ||
return _this; | ||
} | ||
_this.config = { | ||
stateEnterHooks: [], | ||
stateExitHooks: [], | ||
stateChangeHooks: [], | ||
transitionHooks: [], | ||
unhandledEventHooks: [] | ||
}; | ||
return _this; | ||
} | ||
_createClass(GlobalConfigurator, [{ | ||
key: 'onStateEnter', | ||
value: function onStateEnter(hook) { | ||
this.config.stateEnterHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onStateExit', | ||
value: function onStateExit(hook) { | ||
this.config.stateExitHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onStateChange', | ||
value: function onStateChange(hook) { | ||
this.config.stateChangeHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onTransition', | ||
value: function onTransition(hook) { | ||
this.config.transitionHooks.push(hook); | ||
return this; | ||
} | ||
}, { | ||
key: 'onUnhandledEvent', | ||
value: function onUnhandledEvent(hook) { | ||
this.config.unhandledEventHooks.push(hook); | ||
return this; | ||
} | ||
}]); | ||
GlobalConfigurator.prototype.onStateEnter = function onStateEnter(hook) { | ||
this.config.stateEnterHooks.push(hook); | ||
return this; | ||
}; | ||
return GlobalConfigurator; | ||
}(_BaseConfigurator3.default); | ||
GlobalConfigurator.prototype.onStateExit = function onStateExit(hook) { | ||
this.config.stateExitHooks.push(hook); | ||
return this; | ||
}; | ||
exports.default = GlobalConfigurator; | ||
GlobalConfigurator.prototype.onStateChange = function onStateChange(hook) { | ||
this.config.stateChangeHooks.push(hook); | ||
return this; | ||
}; | ||
/***/ }, | ||
GlobalConfigurator.prototype.onTransition = function onTransition(hook) { | ||
this.config.transitionHooks.push(hook); | ||
return this; | ||
}; | ||
GlobalConfigurator.prototype.onUnhandledEvent = function onUnhandledEvent(hook) { | ||
this.config.unhandledEventHooks.push(hook); | ||
return this; | ||
}; | ||
return GlobalConfigurator; | ||
}(_BaseConfigurator3.default); | ||
exports.default = GlobalConfigurator; | ||
/***/ }), | ||
/* 5 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
exports.__esModule = true; | ||
var _BaseConfigurator2 = __webpack_require__(1); | ||
var _BaseConfigurator2 = __webpack_require__(0); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
var _TriggerConfigurator = __webpack_require__(2); | ||
var _TriggerConfigurator = __webpack_require__(1); | ||
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator); | ||
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator); | ||
var _TimerConfigurator = __webpack_require__(11); | ||
var _TimerConfigurator = __webpack_require__(10); | ||
var _TimerConfigurator2 = _interopRequireDefault(_TimerConfigurator); | ||
var _TimerConfigurator2 = _interopRequireDefault(_TimerConfigurator); | ||
var _AsyncActionConfigurator = __webpack_require__(3); | ||
var _AsyncActionConfigurator = __webpack_require__(3); | ||
var _AsyncActionConfigurator2 = _interopRequireDefault(_AsyncActionConfigurator); | ||
var _AsyncActionConfigurator2 = _interopRequireDefault(_AsyncActionConfigurator); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var StateConfigurator = function (_BaseConfigurator) { | ||
_inherits(StateConfigurator, _BaseConfigurator); | ||
var StateConfigurator = function (_BaseConfigurator) { | ||
_inherits(StateConfigurator, _BaseConfigurator); | ||
function StateConfigurator(parent) { | ||
_classCallCheck(this, StateConfigurator); | ||
function StateConfigurator(parent) { | ||
_classCallCheck(this, StateConfigurator); | ||
var _this = _possibleConstructorReturn(this, (StateConfigurator.__proto__ || Object.getPrototypeOf(StateConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
_this.config = { | ||
entryActions: [], | ||
exitActions: [], | ||
events: Object.create(null), | ||
timers: [], | ||
asyncActions: [], | ||
submachine: null | ||
}; | ||
return _this; | ||
} | ||
_this.config = { | ||
entryActions: [], | ||
exitActions: [], | ||
events: Object.create(null), | ||
timers: [], | ||
asyncActions: [], | ||
submachine: null | ||
}; | ||
return _this; | ||
} | ||
_createClass(StateConfigurator, [{ | ||
key: 'onEnter', | ||
value: function onEnter(action) { | ||
this.config.entryActions.push(action); | ||
return this; | ||
} | ||
}, { | ||
key: 'onExit', | ||
value: function onExit(action) { | ||
this.config.exitActions.push(action); | ||
return this; | ||
} | ||
}, { | ||
key: 'on', | ||
value: function on(event) { | ||
if (!this.config.events[event]) { | ||
this.config.events[event] = new _TriggerConfigurator2.default(this); | ||
} | ||
return this.config.events[event]; | ||
} | ||
}, { | ||
key: 'onTimeout', | ||
value: function onTimeout(timeout) { | ||
var timerConfigurator = new _TimerConfigurator2.default(this, timeout); | ||
this.config.timers.push(timerConfigurator); | ||
return timerConfigurator; | ||
} | ||
}, { | ||
key: 'do', | ||
value: function _do(asyncAction) { | ||
var asyncActionConfigurator = new _AsyncActionConfigurator2.default(this, asyncAction); | ||
this.config.asyncActions.push(asyncActionConfigurator); | ||
return asyncActionConfigurator; | ||
} | ||
}, { | ||
key: 'submachine', | ||
value: function submachine(submachineConfig) { | ||
this.config.submachine = submachineConfig; | ||
return this; | ||
} | ||
}]); | ||
StateConfigurator.prototype.onEnter = function onEnter(action) { | ||
this.config.entryActions.push(action); | ||
return this; | ||
}; | ||
return StateConfigurator; | ||
}(_BaseConfigurator3.default); | ||
StateConfigurator.prototype.onExit = function onExit(action) { | ||
this.config.exitActions.push(action); | ||
return this; | ||
}; | ||
exports.default = StateConfigurator; | ||
StateConfigurator.prototype.on = function on(event) { | ||
if (!this.config.events[event]) { | ||
this.config.events[event] = new _TriggerConfigurator2.default(this); | ||
} | ||
return this.config.events[event]; | ||
}; | ||
/***/ }, | ||
StateConfigurator.prototype.onTimeout = function onTimeout(timeout) { | ||
var timerConfigurator = new _TimerConfigurator2.default(this, timeout); | ||
this.config.timers.push(timerConfigurator); | ||
return timerConfigurator; | ||
}; | ||
StateConfigurator.prototype.do = function _do(asyncAction) { | ||
var asyncActionConfigurator = new _AsyncActionConfigurator2.default(this, asyncAction); | ||
this.config.asyncActions.push(asyncActionConfigurator); | ||
return asyncActionConfigurator; | ||
}; | ||
StateConfigurator.prototype.submachine = function submachine(submachineConfig) { | ||
this.config.submachine = submachineConfig; | ||
return this; | ||
}; | ||
return StateConfigurator; | ||
}(_BaseConfigurator3.default); | ||
exports.default = StateConfigurator; | ||
/***/ }), | ||
/* 6 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
exports.__esModule = true; | ||
var _BaseConfigurator2 = __webpack_require__(1); | ||
var _BaseConfigurator2 = __webpack_require__(0); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var TransitionConfigurator = function (_BaseConfigurator) { | ||
_inherits(TransitionConfigurator, _BaseConfigurator); | ||
var TransitionConfigurator = function (_BaseConfigurator) { | ||
_inherits(TransitionConfigurator, _BaseConfigurator); | ||
function TransitionConfigurator(parent, targetState, isInternal) { | ||
_classCallCheck(this, TransitionConfigurator); | ||
function TransitionConfigurator(parent, targetState, isInternal) { | ||
_classCallCheck(this, TransitionConfigurator); | ||
var _this = _possibleConstructorReturn(this, (TransitionConfigurator.__proto__ || Object.getPrototypeOf(TransitionConfigurator)).call(this, parent)); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this, parent)); | ||
_this.config = { | ||
targetState: targetState, | ||
isInternal: targetState === null && isInternal, | ||
actions: [], | ||
condition: null | ||
}; | ||
return _this; | ||
} | ||
_this.config = { | ||
targetState: targetState, | ||
isInternal: targetState === null && isInternal, | ||
actions: [], | ||
condition: null | ||
}; | ||
return _this; | ||
} | ||
_createClass(TransitionConfigurator, [{ | ||
key: 'withAction', | ||
value: function withAction(action) { | ||
this.config.actions.push(action); | ||
return this; | ||
} | ||
}, { | ||
key: 'withCondition', | ||
value: function withCondition(condition) { | ||
this.config.condition = condition; | ||
return this; | ||
} | ||
}]); | ||
TransitionConfigurator.prototype.withAction = function withAction(action) { | ||
this.config.actions.push(action); | ||
return this; | ||
}; | ||
return TransitionConfigurator; | ||
}(_BaseConfigurator3.default); | ||
TransitionConfigurator.prototype.withCondition = function withCondition(condition) { | ||
this.config.condition = condition; | ||
return this; | ||
}; | ||
exports.default = TransitionConfigurator; | ||
return TransitionConfigurator; | ||
}(_BaseConfigurator3.default); | ||
/***/ }, | ||
exports.default = TransitionConfigurator; | ||
/***/ }), | ||
/* 7 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
exports.__esModule = true; | ||
var _StateMachine = __webpack_require__(14); | ||
var _StateMachine = __webpack_require__(13); | ||
var _StateMachine2 = _interopRequireDefault(_StateMachine); | ||
var _StateMachine2 = _interopRequireDefault(_StateMachine); | ||
var _TaskScheduler = __webpack_require__(15); | ||
var _TaskScheduler = __webpack_require__(14); | ||
var _TaskScheduler2 = _interopRequireDefault(_TaskScheduler); | ||
var _TaskScheduler2 = _interopRequireDefault(_TaskScheduler); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var HierarchicalStateMachine = function () { | ||
function HierarchicalStateMachine(rootStateMachine, currentStateMachine, taskScheduler) { | ||
_classCallCheck(this, HierarchicalStateMachine); | ||
var HierarchicalStateMachine = function () { | ||
function HierarchicalStateMachine(rootStateMachine, currentStateMachine, taskScheduler) { | ||
_classCallCheck(this, HierarchicalStateMachine); | ||
this.rootStateMachine = rootStateMachine; | ||
this.currentStateMachine = currentStateMachine; | ||
this.taskScheduler = taskScheduler; | ||
} | ||
this.rootStateMachine = rootStateMachine; | ||
this.currentStateMachine = currentStateMachine; | ||
this.taskScheduler = taskScheduler; | ||
} | ||
_createClass(HierarchicalStateMachine, [{ | ||
key: 'getCurrentState', | ||
value: function getCurrentState() { | ||
return this.currentStateMachine.getCurrentState(); | ||
} | ||
}, { | ||
key: 'getSubmachine', | ||
value: function getSubmachine() { | ||
var submachine = this.currentStateMachine.getSubmachine(); | ||
if (submachine) { | ||
return new HierarchicalStateMachine(this.rootStateMachine, submachine, this.taskScheduler); | ||
} | ||
return null; | ||
} | ||
}, { | ||
key: 'getStateHierarchy', | ||
value: function getStateHierarchy() { | ||
return this.getStateMachines().map(function (stateMachine) { | ||
return stateMachine.getCurrentState(); | ||
}); | ||
} | ||
}, { | ||
key: 'canHandle', | ||
value: function canHandle(event, eventPayload) { | ||
var stateMachines = this.getStateMachines(); | ||
for (var i = stateMachines.length - 1; i >= 0; i--) { | ||
if (stateMachines[i].canHandle(event, eventPayload)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: 'handle', | ||
value: function handle(event, eventPayload) { | ||
var _this = this; | ||
HierarchicalStateMachine.start = function start(config) { | ||
var taskScheduler = new _TaskScheduler2.default(); | ||
var rootStateMachine = void 0; | ||
var createContext = function createContext(stateMachine) { | ||
return { | ||
stateMachine: new HierarchicalStateMachine(rootStateMachine, stateMachine, taskScheduler) | ||
}; | ||
}; | ||
rootStateMachine = new _StateMachine2.default(config, taskScheduler, createContext); | ||
taskScheduler.execute(function () { | ||
return rootStateMachine.start(); | ||
}); | ||
return new HierarchicalStateMachine(rootStateMachine, rootStateMachine, taskScheduler); | ||
}; | ||
this.taskScheduler.enqueue(function () { | ||
var stateMachines = _this.getStateMachines(); | ||
for (var i = stateMachines.length - 1; i >= 0; i--) { | ||
if (stateMachines[i].tryHandle(event, eventPayload)) { | ||
return; | ||
} | ||
} | ||
_this.currentStateMachine.handleUnhandledEvent(event, eventPayload); | ||
}); | ||
return this; | ||
} | ||
}, { | ||
key: 'getStateMachines', | ||
value: function getStateMachines() { | ||
var stateMachines = []; | ||
var stateMachine = this.rootStateMachine; | ||
do { | ||
stateMachines.push(stateMachine); | ||
stateMachine = stateMachine.getSubmachine(); | ||
} while (stateMachine); | ||
return stateMachines; | ||
} | ||
}, { | ||
key: 'toString', | ||
value: function toString() { | ||
return 'StateMachine(currentState: ' + this.getCurrentState() + ')'; | ||
} | ||
}], [{ | ||
key: 'start', | ||
value: function start(config) { | ||
var taskScheduler = new _TaskScheduler2.default(); | ||
var rootStateMachine = void 0; | ||
var createContext = function createContext(stateMachine) { | ||
return { | ||
stateMachine: new HierarchicalStateMachine(rootStateMachine, stateMachine, taskScheduler) | ||
}; | ||
}; | ||
rootStateMachine = new _StateMachine2.default(config, taskScheduler, createContext); | ||
taskScheduler.execute(function () { | ||
return rootStateMachine.start(); | ||
}); | ||
return new HierarchicalStateMachine(rootStateMachine, rootStateMachine, taskScheduler); | ||
} | ||
}]); | ||
HierarchicalStateMachine.prototype.getCurrentState = function getCurrentState() { | ||
return this.currentStateMachine.getCurrentState(); | ||
}; | ||
return HierarchicalStateMachine; | ||
}(); | ||
HierarchicalStateMachine.prototype.getSubmachine = function getSubmachine() { | ||
var submachine = this.currentStateMachine.getSubmachine(); | ||
if (submachine) { | ||
return new HierarchicalStateMachine(this.rootStateMachine, submachine, this.taskScheduler); | ||
} | ||
return null; | ||
}; | ||
exports.default = HierarchicalStateMachine; | ||
HierarchicalStateMachine.prototype.getStateHierarchy = function getStateHierarchy() { | ||
return this.getStateMachines().map(function (stateMachine) { | ||
return stateMachine.getCurrentState(); | ||
}); | ||
}; | ||
/***/ }, | ||
HierarchicalStateMachine.prototype.canHandle = function canHandle(event, eventPayload) { | ||
var stateMachines = this.getStateMachines(); | ||
for (var i = stateMachines.length - 1; i >= 0; i--) { | ||
if (stateMachines[i].canHandle(event, eventPayload)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
HierarchicalStateMachine.prototype.handle = function handle(event, eventPayload) { | ||
var _this = this; | ||
this.taskScheduler.enqueue(function () { | ||
var stateMachines = _this.getStateMachines(); | ||
for (var i = stateMachines.length - 1; i >= 0; i--) { | ||
if (stateMachines[i].tryHandle(event, eventPayload)) { | ||
return; | ||
} | ||
} | ||
_this.currentStateMachine.handleUnhandledEvent(event, eventPayload); | ||
}); | ||
return this; | ||
}; | ||
HierarchicalStateMachine.prototype.getStateMachines = function getStateMachines() { | ||
var stateMachines = []; | ||
var stateMachine = this.rootStateMachine; | ||
do { | ||
stateMachines.push(stateMachine); | ||
stateMachine = stateMachine.getSubmachine(); | ||
} while (stateMachine); | ||
return stateMachines; | ||
}; | ||
HierarchicalStateMachine.prototype.toString = function toString() { | ||
return 'StateMachine(currentState: ' + this.getCurrentState() + ')'; | ||
}; | ||
return HierarchicalStateMachine; | ||
}(); | ||
exports.default = HierarchicalStateMachine; | ||
/***/ }), | ||
/* 8 */ | ||
/***/ function(module, exports) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = merge; | ||
function merge(target, source) { | ||
Object.keys(source).forEach(function (key) { | ||
target[key] = source[key]; | ||
}); | ||
return target; | ||
} | ||
/***/ }, | ||
exports.__esModule = true; | ||
var _configuration = __webpack_require__(12); | ||
var _HierarchicalStateMachine = __webpack_require__(7); | ||
var _HierarchicalStateMachine2 = _interopRequireDefault(_HierarchicalStateMachine); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var Finity = { | ||
configure: function configure() { | ||
return new _configuration.StateMachineConfigurator(); | ||
}, | ||
start: function start(config) { | ||
return _HierarchicalStateMachine2.default.start(config); | ||
} | ||
}; | ||
exports.default = Finity; | ||
/***/ }), | ||
/* 9 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _configuration = __webpack_require__(13); | ||
exports.__esModule = true; | ||
var _HierarchicalStateMachine = __webpack_require__(7); | ||
var _BaseConfigurator2 = __webpack_require__(0); | ||
var _HierarchicalStateMachine2 = _interopRequireDefault(_HierarchicalStateMachine); | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _GlobalConfigurator = __webpack_require__(4); | ||
var Finity = { | ||
configure: function configure() { | ||
return new _configuration.StateMachineConfigurator(); | ||
}, | ||
start: function start(config) { | ||
return _HierarchicalStateMachine2.default.start(config); | ||
} | ||
}; | ||
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator); | ||
exports.default = Finity; | ||
var _StateConfigurator = __webpack_require__(5); | ||
/***/ }, | ||
/* 10 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
var _StateConfigurator2 = _interopRequireDefault(_StateConfigurator); | ||
'use strict'; | ||
var _HierarchicalStateMachine = __webpack_require__(7); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _HierarchicalStateMachine2 = _interopRequireDefault(_HierarchicalStateMachine); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _BaseConfigurator2 = __webpack_require__(1); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
var _GlobalConfigurator = __webpack_require__(4); | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator); | ||
var StateMachineConfigurator = function (_BaseConfigurator) { | ||
_inherits(StateMachineConfigurator, _BaseConfigurator); | ||
var _StateConfigurator = __webpack_require__(5); | ||
function StateMachineConfigurator() { | ||
_classCallCheck(this, StateMachineConfigurator); | ||
var _StateConfigurator2 = _interopRequireDefault(_StateConfigurator); | ||
var _this = _possibleConstructorReturn(this, _BaseConfigurator.call(this)); | ||
var _HierarchicalStateMachine = __webpack_require__(7); | ||
_this.config = { | ||
global: new _GlobalConfigurator2.default(_this), | ||
initialState: null, | ||
states: Object.create(null) | ||
}; | ||
return _this; | ||
} | ||
var _HierarchicalStateMachine2 = _interopRequireDefault(_HierarchicalStateMachine); | ||
StateMachineConfigurator.prototype.global = function global() { | ||
return this.config.global; | ||
}; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
StateMachineConfigurator.prototype.initialState = function initialState(state) { | ||
this.config.initialState = state; | ||
return this.state(state); | ||
}; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
StateMachineConfigurator.prototype.state = function state(_state) { | ||
if (!this.config.states[_state]) { | ||
this.config.states[_state] = new _StateConfigurator2.default(this); | ||
} | ||
return this.config.states[_state]; | ||
}; | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
StateMachineConfigurator.prototype.getConfig = function getConfig() { | ||
return this.buildConfig(); | ||
}; | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
StateMachineConfigurator.prototype.start = function start() { | ||
var config = this.getConfig(); | ||
return _HierarchicalStateMachine2.default.start(config); | ||
}; | ||
var StateMachineConfigurator = function (_BaseConfigurator) { | ||
_inherits(StateMachineConfigurator, _BaseConfigurator); | ||
return StateMachineConfigurator; | ||
}(_BaseConfigurator3.default); | ||
function StateMachineConfigurator() { | ||
_classCallCheck(this, StateMachineConfigurator); | ||
exports.default = StateMachineConfigurator; | ||
var _this = _possibleConstructorReturn(this, (StateMachineConfigurator.__proto__ || Object.getPrototypeOf(StateMachineConfigurator)).call(this)); | ||
/***/ }), | ||
/* 10 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
_this.config = { | ||
global: new _GlobalConfigurator2.default(_this), | ||
initialState: null, | ||
states: Object.create(null) | ||
}; | ||
return _this; | ||
} | ||
"use strict"; | ||
_createClass(StateMachineConfigurator, [{ | ||
key: 'global', | ||
value: function global() { | ||
return this.config.global; | ||
} | ||
}, { | ||
key: 'initialState', | ||
value: function initialState(state) { | ||
this.config.initialState = state; | ||
return this.state(state); | ||
} | ||
}, { | ||
key: 'state', | ||
value: function state(_state) { | ||
if (!this.config.states[_state]) { | ||
this.config.states[_state] = new _StateConfigurator2.default(this); | ||
} | ||
return this.config.states[_state]; | ||
} | ||
}, { | ||
key: 'getConfig', | ||
value: function getConfig() { | ||
return this.buildConfig(); | ||
} | ||
}, { | ||
key: 'start', | ||
value: function start() { | ||
var config = this.getConfig(); | ||
return _HierarchicalStateMachine2.default.start(config); | ||
} | ||
}]); | ||
return StateMachineConfigurator; | ||
}(_BaseConfigurator3.default); | ||
exports.__esModule = true; | ||
exports.default = StateMachineConfigurator; | ||
var _TriggerConfigurator2 = __webpack_require__(1); | ||
/***/ }, | ||
/* 11 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
var _TriggerConfigurator3 = _interopRequireDefault(_TriggerConfigurator2); | ||
'use strict'; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _TriggerConfigurator2 = __webpack_require__(2); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
var _TriggerConfigurator3 = _interopRequireDefault(_TriggerConfigurator2); | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var TrimerConfigurator = function (_TriggerConfigurator) { | ||
_inherits(TrimerConfigurator, _TriggerConfigurator); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function TrimerConfigurator(parent, timeout) { | ||
_classCallCheck(this, TrimerConfigurator); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
var _this = _possibleConstructorReturn(this, _TriggerConfigurator.call(this, parent)); | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
_this.config.timeout = timeout; | ||
return _this; | ||
} | ||
var TrimerConfigurator = function (_TriggerConfigurator) { | ||
_inherits(TrimerConfigurator, _TriggerConfigurator); | ||
return TrimerConfigurator; | ||
}(_TriggerConfigurator3.default); | ||
function TrimerConfigurator(parent, timeout) { | ||
_classCallCheck(this, TrimerConfigurator); | ||
exports.default = TrimerConfigurator; | ||
var _this = _possibleConstructorReturn(this, (TrimerConfigurator.__proto__ || Object.getPrototypeOf(TrimerConfigurator)).call(this, parent)); | ||
/***/ }), | ||
/* 11 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
_this.config.timeout = timeout; | ||
return _this; | ||
} | ||
"use strict"; | ||
return TrimerConfigurator; | ||
}(_TriggerConfigurator3.default); | ||
exports.default = TrimerConfigurator; | ||
exports.__esModule = true; | ||
exports.default = delegateToAncestor; | ||
function delegateToAncestor(constructor, ancestorConstructor) { | ||
var prototype = constructor.prototype; | ||
var ancestorPrototype = ancestorConstructor.prototype; | ||
Object.getOwnPropertyNames(ancestorPrototype).filter(function (name) { | ||
return !prototype[name] && ancestorPrototype[name] instanceof Function && ancestorPrototype[name] !== ancestorConstructor; | ||
}).forEach(function (name) { | ||
// eslint-disable-next-line func-names | ||
prototype[name] = function () { | ||
var method = ancestorPrototype[name]; | ||
/***/ }, | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
return method.apply(this.getAncestor(ancestorConstructor), args); | ||
}; | ||
}); | ||
} | ||
/***/ }), | ||
/* 12 */ | ||
/***/ function(module, exports) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = delegateToAncestor; | ||
function delegateToAncestor(constructor, ancestorConstructor) { | ||
var prototype = constructor.prototype; | ||
var ancestorPrototype = ancestorConstructor.prototype; | ||
Object.getOwnPropertyNames(ancestorPrototype).filter(function (name) { | ||
return !prototype[name] && ancestorPrototype[name] instanceof Function && ancestorPrototype[name] !== ancestorConstructor; | ||
}).forEach(function (name) { | ||
// eslint-disable-next-line func-names | ||
prototype[name] = function () { | ||
var method = ancestorPrototype[name]; | ||
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
exports.__esModule = true; | ||
exports.StateMachineConfigurator = undefined; | ||
return method.apply(this.getAncestor(ancestorConstructor), args); | ||
}; | ||
}); | ||
} | ||
var _StateMachineConfigurator = __webpack_require__(9); | ||
/***/ }, | ||
var _StateMachineConfigurator2 = _interopRequireDefault(_StateMachineConfigurator); | ||
var _GlobalConfigurator = __webpack_require__(4); | ||
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator); | ||
var _StateConfigurator = __webpack_require__(5); | ||
var _StateConfigurator2 = _interopRequireDefault(_StateConfigurator); | ||
var _TriggerConfigurator = __webpack_require__(1); | ||
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator); | ||
var _TransitionConfigurator = __webpack_require__(6); | ||
var _TransitionConfigurator2 = _interopRequireDefault(_TransitionConfigurator); | ||
var _AsyncActionConfigurator = __webpack_require__(3); | ||
var _AsyncActionConfigurator2 = _interopRequireDefault(_AsyncActionConfigurator); | ||
var _delegateToAncestor = __webpack_require__(11); | ||
var _delegateToAncestor2 = _interopRequireDefault(_delegateToAncestor); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
exports.StateMachineConfigurator = _StateMachineConfigurator2.default; /* eslint-disable import/prefer-default-export */ | ||
(0, _delegateToAncestor2.default)(_GlobalConfigurator2.default, _StateMachineConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_StateConfigurator2.default, _StateMachineConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_TransitionConfigurator2.default, _StateConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_TransitionConfigurator2.default, _TriggerConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_TransitionConfigurator2.default, _AsyncActionConfigurator2.default); | ||
/***/ }), | ||
/* 13 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.StateMachineConfigurator = undefined; | ||
var _StateMachineConfigurator = __webpack_require__(10); | ||
exports.__esModule = true; | ||
var _StateMachineConfigurator2 = _interopRequireDefault(_StateMachineConfigurator); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _GlobalConfigurator = __webpack_require__(4); | ||
var _invokeEach = __webpack_require__(15); | ||
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator); | ||
var _invokeEach2 = _interopRequireDefault(_invokeEach); | ||
var _StateConfigurator = __webpack_require__(5); | ||
var _merge = __webpack_require__(2); | ||
var _StateConfigurator2 = _interopRequireDefault(_StateConfigurator); | ||
var _merge2 = _interopRequireDefault(_merge); | ||
var _TriggerConfigurator = __webpack_require__(2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _TransitionConfigurator = __webpack_require__(6); | ||
var noop = function noop() {}; | ||
var _TransitionConfigurator2 = _interopRequireDefault(_TransitionConfigurator); | ||
var StateMachine = function () { | ||
function StateMachine(config, taskScheduler, contextFactory) { | ||
_classCallCheck(this, StateMachine); | ||
var _AsyncActionConfigurator = __webpack_require__(3); | ||
if (config === undefined || config === null) { | ||
throw new Error('Configuration must be specified.'); | ||
} | ||
if ((typeof config === 'undefined' ? 'undefined' : _typeof(config)) !== 'object') { | ||
throw new Error('Configuration must be an object.'); | ||
} | ||
if (config.initialState === undefined || config.initialState === null) { | ||
throw new Error('Initial state must be specified.'); | ||
} | ||
this.config = config; | ||
this.taskScheduler = taskScheduler; | ||
this.contextFactory = contextFactory; | ||
this.currentState = null; | ||
this.submachines = Object.create(null); | ||
this.timerIDs = null; | ||
this.asyncActionCancelers = null; | ||
this.handleAsyncActionComplete = this.handleAsyncActionComplete.bind(this); | ||
this.handleTimeout = this.handleTimeout.bind(this); | ||
} | ||
var _AsyncActionConfigurator2 = _interopRequireDefault(_AsyncActionConfigurator); | ||
StateMachine.prototype.getCurrentState = function getCurrentState() { | ||
return this.currentState; | ||
}; | ||
var _delegateToAncestor = __webpack_require__(12); | ||
StateMachine.prototype.canHandle = function canHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
var _delegateToAncestor2 = _interopRequireDefault(_delegateToAncestor); | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
return !!this.getFirstAllowedTransitionForEvent(context); | ||
}; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
StateMachine.prototype.tryHandle = function tryHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
exports.StateMachineConfigurator = _StateMachineConfigurator2.default; /* eslint-disable import/prefer-default-export */ | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
var transitionConfig = this.getFirstAllowedTransitionForEvent(context); | ||
if (transitionConfig) { | ||
this.executeTransition(transitionConfig, context); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
(0, _delegateToAncestor2.default)(_GlobalConfigurator2.default, _StateMachineConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_StateConfigurator2.default, _StateMachineConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_TransitionConfigurator2.default, _StateConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_TransitionConfigurator2.default, _TriggerConfigurator2.default); | ||
(0, _delegateToAncestor2.default)(_TransitionConfigurator2.default, _AsyncActionConfigurator2.default); | ||
StateMachine.prototype.handleUnhandledEvent = function handleUnhandledEvent(event, eventPayload) { | ||
if (this.config.global.unhandledEventHooks.length > 0) { | ||
(0, _invokeEach2.default)(this.config.global.unhandledEventHooks, event, this.currentState, this.createContextWithEvent(event, eventPayload)); | ||
} else { | ||
throw new Error('Unhandled event \'' + event + '\' in state \'' + this.currentState + '\'.'); | ||
} | ||
}; | ||
/***/ }, | ||
/* 14 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
StateMachine.prototype.isStarted = function isStarted() { | ||
return this.currentState !== null; | ||
}; | ||
'use strict'; | ||
StateMachine.prototype.start = function start() { | ||
if (!this.isStarted()) { | ||
this.enterState(this.config.initialState, this.createContext()); | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
StateMachine.prototype.stop = function stop() { | ||
if (this.isStarted()) { | ||
this.exitState(this.createContext()); | ||
this.currentState = null; | ||
} | ||
}; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
StateMachine.prototype.getSubmachine = function getSubmachine() { | ||
return this.isStarted() ? this.submachines[this.currentState] : null; | ||
}; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
StateMachine.prototype.executeTransition = function executeTransition(transitionConfig, context) { | ||
if (!transitionConfig.isInternal) { | ||
this.exitState(context); | ||
} | ||
var _invokeEach = __webpack_require__(16); | ||
var nextState = transitionConfig.targetState !== null ? transitionConfig.targetState : this.currentState; | ||
var _invokeEach2 = _interopRequireDefault(_invokeEach); | ||
(0, _invokeEach2.default)(this.config.global.transitionHooks, this.currentState, nextState, context); | ||
(0, _invokeEach2.default)(transitionConfig.actions, this.currentState, nextState, context); | ||
var _merge = __webpack_require__(8); | ||
if (!transitionConfig.isInternal) { | ||
this.enterState(nextState, context); | ||
} | ||
}; | ||
var _merge2 = _interopRequireDefault(_merge); | ||
StateMachine.prototype.enterState = function enterState(state, context) { | ||
(0, _invokeEach2.default)(this.config.global.stateEnterHooks, state, context); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.entryActions, state, context); | ||
} | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
if (this.currentState !== null && this.currentState !== state) { | ||
(0, _invokeEach2.default)(this.config.global.stateChangeHooks, this.currentState, state, context); | ||
} | ||
var noop = function noop() {}; | ||
try { | ||
this.startAsyncActions(state, context); | ||
this.startTimers(state); | ||
this.startSubmachines(state); | ||
} catch (error) { | ||
this.stopTimers(); | ||
this.cancelAsyncActions(); | ||
throw error; | ||
} | ||
var StateMachine = function () { | ||
function StateMachine(config, taskScheduler, contextFactory) { | ||
_classCallCheck(this, StateMachine); | ||
this.currentState = state; | ||
}; | ||
if (config === undefined || config === null) { | ||
throw new Error('Configuration must be specified.'); | ||
} | ||
if ((typeof config === 'undefined' ? 'undefined' : _typeof(config)) !== 'object') { | ||
throw new Error('Configuration must be an object.'); | ||
} | ||
if (config.initialState === undefined || config.initialState === null) { | ||
throw new Error('Initial state must be specified.'); | ||
} | ||
this.config = config; | ||
this.taskScheduler = taskScheduler; | ||
this.contextFactory = contextFactory; | ||
this.currentState = null; | ||
this.submachines = Object.create(null); | ||
this.timerIDs = null; | ||
this.asyncActionCancelers = null; | ||
this.handleAsyncActionComplete = this.handleAsyncActionComplete.bind(this); | ||
this.handleTimeout = this.handleTimeout.bind(this); | ||
} | ||
StateMachine.prototype.exitState = function exitState(context) { | ||
this.stopSubmachines(); | ||
this.stopTimers(); | ||
this.cancelAsyncActions(); | ||
_createClass(StateMachine, [{ | ||
key: 'getCurrentState', | ||
value: function getCurrentState() { | ||
return this.currentState; | ||
} | ||
}, { | ||
key: 'canHandle', | ||
value: function canHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
(0, _invokeEach2.default)(this.config.global.stateExitHooks, this.currentState, context); | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
return !!this.getFirstAllowedTransitionForEvent(context); | ||
} | ||
}, { | ||
key: 'tryHandle', | ||
value: function tryHandle(event, eventPayload) { | ||
if (!this.isStarted()) { | ||
return false; | ||
} | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.exitActions, this.currentState, context); | ||
} | ||
}; | ||
var context = this.createContextWithEvent(event, eventPayload); | ||
var transitionConfig = this.getFirstAllowedTransitionForEvent(context); | ||
if (transitionConfig) { | ||
this.executeTransition(transitionConfig, context); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}, { | ||
key: 'handleUnhandledEvent', | ||
value: function handleUnhandledEvent(event, eventPayload) { | ||
if (this.config.global.unhandledEventHooks.length > 0) { | ||
(0, _invokeEach2.default)(this.config.global.unhandledEventHooks, event, this.currentState, this.createContextWithEvent(event, eventPayload)); | ||
} else { | ||
throw new Error('Unhandled event \'' + event + '\' in state \'' + this.currentState + '\'.'); | ||
} | ||
} | ||
}, { | ||
key: 'isStarted', | ||
value: function isStarted() { | ||
return this.currentState !== null; | ||
} | ||
}, { | ||
key: 'start', | ||
value: function start() { | ||
if (!this.isStarted()) { | ||
this.enterState(this.config.initialState, this.createContext()); | ||
} | ||
} | ||
}, { | ||
key: 'stop', | ||
value: function stop() { | ||
if (this.isStarted()) { | ||
this.exitState(this.createContext()); | ||
this.currentState = null; | ||
} | ||
} | ||
}, { | ||
key: 'getSubmachine', | ||
value: function getSubmachine() { | ||
return this.isStarted() ? this.submachines[this.currentState] : null; | ||
} | ||
}, { | ||
key: 'executeTransition', | ||
value: function executeTransition(transitionConfig, context) { | ||
if (!transitionConfig.isInternal) { | ||
this.exitState(context); | ||
} | ||
StateMachine.prototype.startAsyncActions = function startAsyncActions(state, context) { | ||
var _this = this; | ||
var nextState = transitionConfig.targetState !== null ? transitionConfig.targetState : this.currentState; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
stateConfig.asyncActions.forEach(function (asyncActionConfig) { | ||
return _this.startAsyncAction(asyncActionConfig, state, context); | ||
}); | ||
} | ||
}; | ||
(0, _invokeEach2.default)(this.config.global.transitionHooks, this.currentState, nextState, context); | ||
(0, _invokeEach2.default)(transitionConfig.actions, this.currentState, nextState, context); | ||
StateMachine.prototype.startAsyncAction = function startAsyncAction(asyncActionConfig, state, context) { | ||
var action = asyncActionConfig.action, | ||
successTrigger = asyncActionConfig.successTrigger, | ||
failureTrigger = asyncActionConfig.failureTrigger; | ||
if (!transitionConfig.isInternal) { | ||
this.enterState(nextState, context); | ||
} | ||
} | ||
}, { | ||
key: 'enterState', | ||
value: function enterState(state, context) { | ||
(0, _invokeEach2.default)(this.config.global.stateEnterHooks, state, context); | ||
var handleComplete = this.handleAsyncActionComplete; | ||
action(state, context).then(function (result) { | ||
return handleComplete(successTrigger, { result: result }); | ||
}, function (error) { | ||
return handleComplete(failureTrigger, { error: error }); | ||
}); | ||
this.asyncActionCancelers = this.asyncActionCancelers || []; | ||
this.asyncActionCancelers.push(function () { | ||
handleComplete = noop; | ||
}); | ||
}; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.entryActions, state, context); | ||
} | ||
StateMachine.prototype.cancelAsyncActions = function cancelAsyncActions() { | ||
if (this.asyncActionCancelers) { | ||
(0, _invokeEach2.default)(this.asyncActionCancelers); | ||
this.asyncActionCancelers = null; | ||
} | ||
}; | ||
if (this.currentState !== null && this.currentState !== state) { | ||
(0, _invokeEach2.default)(this.config.global.stateChangeHooks, this.currentState, state, context); | ||
} | ||
StateMachine.prototype.handleAsyncActionComplete = function handleAsyncActionComplete(triggerConfig, additionalContext) { | ||
var context = (0, _merge2.default)(this.createContext(), additionalContext); | ||
this.executeTrigger(triggerConfig, context); | ||
}; | ||
try { | ||
this.startAsyncActions(state, context); | ||
this.startTimers(state); | ||
this.startSubmachines(state); | ||
} catch (error) { | ||
this.stopTimers(); | ||
this.cancelAsyncActions(); | ||
throw error; | ||
} | ||
StateMachine.prototype.startTimers = function startTimers(state) { | ||
var _this2 = this; | ||
this.currentState = state; | ||
} | ||
}, { | ||
key: 'exitState', | ||
value: function exitState(context) { | ||
this.stopSubmachines(); | ||
this.stopTimers(); | ||
this.cancelAsyncActions(); | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.timers.length > 0) { | ||
this.timerIDs = stateConfig.timers.map(function (timerConfig) { | ||
return setTimeout(_this2.handleTimeout, timerConfig.timeout, timerConfig); | ||
}); | ||
} | ||
}; | ||
(0, _invokeEach2.default)(this.config.global.stateExitHooks, this.currentState, context); | ||
StateMachine.prototype.stopTimers = function stopTimers() { | ||
if (this.timerIDs) { | ||
this.timerIDs.forEach(clearTimeout); | ||
this.timerIDs = null; | ||
} | ||
}; | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (stateConfig) { | ||
(0, _invokeEach2.default)(stateConfig.exitActions, this.currentState, context); | ||
} | ||
} | ||
}, { | ||
key: 'startAsyncActions', | ||
value: function startAsyncActions(state, context) { | ||
var _this = this; | ||
StateMachine.prototype.handleTimeout = function handleTimeout(timerConfig) { | ||
this.executeTrigger(timerConfig, this.createContext()); | ||
}; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig) { | ||
stateConfig.asyncActions.forEach(function (asyncActionConfig) { | ||
return _this.startAsyncAction(asyncActionConfig, state, context); | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'startAsyncAction', | ||
value: function startAsyncAction(asyncActionConfig, state, context) { | ||
var action = asyncActionConfig.action, | ||
successTrigger = asyncActionConfig.successTrigger, | ||
failureTrigger = asyncActionConfig.failureTrigger; | ||
StateMachine.prototype.startSubmachines = function startSubmachines(state) { | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.submachine) { | ||
if (!this.submachines[state]) { | ||
this.submachines[state] = new StateMachine(stateConfig.submachine, this.taskScheduler, this.contextFactory); | ||
} | ||
this.submachines[state].start(); | ||
} | ||
}; | ||
var handleComplete = this.handleAsyncActionComplete; | ||
action(state, context).then(function (result) { | ||
return handleComplete(successTrigger, { result: result }); | ||
}, function (error) { | ||
return handleComplete(failureTrigger, { error: error }); | ||
}); | ||
this.asyncActionCancelers = this.asyncActionCancelers || []; | ||
this.asyncActionCancelers.push(function () { | ||
handleComplete = noop; | ||
}); | ||
} | ||
}, { | ||
key: 'cancelAsyncActions', | ||
value: function cancelAsyncActions() { | ||
if (this.asyncActionCancelers) { | ||
(0, _invokeEach2.default)(this.asyncActionCancelers); | ||
this.asyncActionCancelers = null; | ||
} | ||
} | ||
}, { | ||
key: 'handleAsyncActionComplete', | ||
value: function handleAsyncActionComplete(triggerConfig, additionalContext) { | ||
var context = (0, _merge2.default)(this.createContext(), additionalContext); | ||
this.executeTrigger(triggerConfig, context); | ||
} | ||
}, { | ||
key: 'startTimers', | ||
value: function startTimers(state) { | ||
var _this2 = this; | ||
StateMachine.prototype.stopSubmachines = function stopSubmachines() { | ||
var submachine = this.submachines[this.currentState]; | ||
if (submachine) { | ||
submachine.stop(); | ||
} | ||
}; | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.timers.length > 0) { | ||
this.timerIDs = stateConfig.timers.map(function (timerConfig) { | ||
return setTimeout(_this2.handleTimeout, timerConfig.timeout, timerConfig); | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'stopTimers', | ||
value: function stopTimers() { | ||
if (this.timerIDs) { | ||
this.timerIDs.forEach(clearTimeout); | ||
this.timerIDs = null; | ||
} | ||
} | ||
}, { | ||
key: 'handleTimeout', | ||
value: function handleTimeout(timerConfig) { | ||
this.executeTrigger(timerConfig, this.createContext()); | ||
} | ||
}, { | ||
key: 'startSubmachines', | ||
value: function startSubmachines(state) { | ||
var stateConfig = this.config.states[state]; | ||
if (stateConfig && stateConfig.submachine) { | ||
if (!this.submachines[state]) { | ||
this.submachines[state] = new StateMachine(stateConfig.submachine, this.taskScheduler, this.contextFactory); | ||
} | ||
this.submachines[state].start(); | ||
} | ||
} | ||
}, { | ||
key: 'stopSubmachines', | ||
value: function stopSubmachines() { | ||
var submachine = this.submachines[this.currentState]; | ||
if (submachine) { | ||
submachine.stop(); | ||
} | ||
} | ||
}, { | ||
key: 'createContext', | ||
value: function createContext() { | ||
return this.contextFactory(this); | ||
} | ||
}, { | ||
key: 'createContextWithEvent', | ||
value: function createContextWithEvent(event, eventPayload) { | ||
var context = this.createContext(); | ||
context.event = event; | ||
if (eventPayload !== undefined) { | ||
context.eventPayload = eventPayload; | ||
} | ||
return context; | ||
} | ||
}, { | ||
key: 'getFirstAllowedTransitionForEvent', | ||
value: function getFirstAllowedTransitionForEvent(context) { | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (!stateConfig) { | ||
return null; | ||
} | ||
StateMachine.prototype.createContext = function createContext() { | ||
return this.contextFactory(this); | ||
}; | ||
var eventConfig = stateConfig.events[context.event]; | ||
return eventConfig ? StateMachine.getFirstAllowedTransition(eventConfig.transitions, context) : null; | ||
} | ||
}, { | ||
key: 'executeTrigger', | ||
value: function executeTrigger(triggerConfig, context) { | ||
var _this3 = this; | ||
StateMachine.prototype.createContextWithEvent = function createContextWithEvent(event, eventPayload) { | ||
var context = this.createContext(); | ||
context.event = event; | ||
if (eventPayload !== undefined) { | ||
context.eventPayload = eventPayload; | ||
} | ||
return context; | ||
}; | ||
this.taskScheduler.execute(function () { | ||
var transitionConfig = StateMachine.getFirstAllowedTransition(triggerConfig.transitions, context); | ||
if (transitionConfig) { | ||
_this3.executeTransition(transitionConfig, context); | ||
} | ||
}); | ||
} | ||
}], [{ | ||
key: 'getFirstAllowedTransition', | ||
value: function getFirstAllowedTransition(transitions, context) { | ||
for (var i = 0; i < transitions.length; i++) { | ||
if (!transitions[i].condition || transitions[i].condition(context)) { | ||
return transitions[i]; | ||
} | ||
} | ||
return null; | ||
} | ||
}]); | ||
StateMachine.getFirstAllowedTransition = function getFirstAllowedTransition(transitions, context) { | ||
for (var i = 0; i < transitions.length; i++) { | ||
if (!transitions[i].condition || transitions[i].condition(context)) { | ||
return transitions[i]; | ||
} | ||
} | ||
return null; | ||
}; | ||
return StateMachine; | ||
}(); | ||
StateMachine.prototype.getFirstAllowedTransitionForEvent = function getFirstAllowedTransitionForEvent(context) { | ||
var stateConfig = this.config.states[this.currentState]; | ||
if (!stateConfig) { | ||
return null; | ||
} | ||
exports.default = StateMachine; | ||
var eventConfig = stateConfig.events[context.event]; | ||
return eventConfig ? StateMachine.getFirstAllowedTransition(eventConfig.transitions, context) : null; | ||
}; | ||
/***/ }, | ||
/* 15 */ | ||
/***/ function(module, exports) { | ||
StateMachine.prototype.executeTrigger = function executeTrigger(triggerConfig, context) { | ||
var _this3 = this; | ||
'use strict'; | ||
this.taskScheduler.execute(function () { | ||
var transitionConfig = StateMachine.getFirstAllowedTransition(triggerConfig.transitions, context); | ||
if (transitionConfig) { | ||
_this3.executeTransition(transitionConfig, context); | ||
} | ||
}); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
return StateMachine; | ||
}(); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
exports.default = StateMachine; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
/***/ }), | ||
/* 14 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
var TaskScheduler = function () { | ||
function TaskScheduler() { | ||
_classCallCheck(this, TaskScheduler); | ||
"use strict"; | ||
this.queue = []; | ||
this.isBusy = false; | ||
} | ||
_createClass(TaskScheduler, [{ | ||
key: 'enqueue', | ||
value: function enqueue(task) { | ||
if (this.isBusy) { | ||
this.queue.push(task); | ||
} else { | ||
this.execute(task); | ||
} | ||
} | ||
}, { | ||
key: 'execute', | ||
value: function execute(task) { | ||
if (this.isBusy) { | ||
throw new Error('Cannot execute task because another task is already running.'); | ||
} | ||
this.isBusy = true; | ||
try { | ||
task(); | ||
while (this.queue.length > 0) { | ||
var nextTask = this.queue.shift(); | ||
nextTask(); | ||
} | ||
} finally { | ||
// Clean up | ||
if (this.queue.length > 0) { | ||
this.queue = []; | ||
} | ||
this.isBusy = false; | ||
} | ||
} | ||
}]); | ||
exports.__esModule = true; | ||
return TaskScheduler; | ||
}(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
exports.default = TaskScheduler; | ||
var TaskScheduler = function () { | ||
function TaskScheduler() { | ||
_classCallCheck(this, TaskScheduler); | ||
/***/ }, | ||
this.queue = []; | ||
this.isBusy = false; | ||
} | ||
TaskScheduler.prototype.enqueue = function enqueue(task) { | ||
if (this.isBusy) { | ||
this.queue.push(task); | ||
} else { | ||
this.execute(task); | ||
} | ||
}; | ||
TaskScheduler.prototype.execute = function execute(task) { | ||
if (this.isBusy) { | ||
throw new Error('Cannot execute task because another task is already running.'); | ||
} | ||
this.isBusy = true; | ||
try { | ||
task(); | ||
while (this.queue.length > 0) { | ||
var nextTask = this.queue.shift(); | ||
nextTask(); | ||
} | ||
} finally { | ||
// Clean up | ||
if (this.queue.length > 0) { | ||
this.queue = []; | ||
} | ||
this.isBusy = false; | ||
} | ||
}; | ||
return TaskScheduler; | ||
}(); | ||
exports.default = TaskScheduler; | ||
/***/ }), | ||
/* 15 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
exports.__esModule = true; | ||
exports.default = invokeEach; | ||
function invokeEach(fns) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
fns.forEach(function (fn) { | ||
return fn.apply(undefined, args); | ||
}); | ||
} | ||
/***/ }), | ||
/* 16 */ | ||
/***/ function(module, exports) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = invokeEach; | ||
function invokeEach(fns) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
fns.forEach(function (fn) { | ||
return fn.apply(undefined, args); | ||
}); | ||
} | ||
exports.__esModule = true; | ||
exports.default = mapValues; | ||
function mapValues(obj, callback) { | ||
var prototype = Object.getPrototypeOf(obj); | ||
var result = Object.create(prototype); | ||
Object.keys(obj).forEach(function (key) { | ||
result[key] = callback(obj[key]); | ||
}); | ||
return result; | ||
} | ||
/***/ }, | ||
/***/ }), | ||
/* 17 */ | ||
/***/ function(module, exports) { | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
"use strict"; | ||
/* eslint-disable */ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = mapValues; | ||
function mapValues(obj, callback) { | ||
var prototype = Object.getPrototypeOf(obj); | ||
var result = Object.create(prototype); | ||
Object.keys(obj).forEach(function (key) { | ||
result[key] = callback(obj[key]); | ||
}); | ||
return result; | ||
} | ||
/***/ } | ||
/******/ ]) | ||
}); | ||
; | ||
var Finity = __webpack_require__(8).default; | ||
var merge = __webpack_require__(2).default; | ||
merge(exports, Finity); | ||
// Allow the use of the default import syntax in TypeScript (import Finity from 'finity') | ||
exports.default = Finity; | ||
/***/ }) | ||
/******/ ]); | ||
}); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Finity=e():t.Finity=e()}(this,function(){return function(t){function e(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return t[r].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";var r=n(9).default,i=n(8).default;i(e,r),e.default=r},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),a=n(17),c=r(a),s=function(){function t(e){i(this,t),this.parent=e}return u(t,[{key:"getAncestor",value:function(t){return this.parent?this.parent instanceof t?this.parent:this.parent.getAncestor(t):null}},{key:"buildConfig",value:function(){var e=function e(n){return n?n instanceof t?n.buildConfig():Array.isArray(n)?n.map(e):n&&"object"===("undefined"==typeof n?"undefined":o(n))?(0,c.default)(n,e):n:n};return(0,c.default)(this.config,e)}}]),t}();e.default=s},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),c=n(1),s=r(c),f=n(6),l=r(f),h=function(t){function e(t){i(this,e);var n=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return n.config={transitions:[]},n}return u(e,t),a(e,[{key:"transitionTo",value:function(t){return this.transition(t)}},{key:"selfTransition",value:function(){return this.transition(null)}},{key:"internalTransition",value:function(){return this.transition(null,!0)}},{key:"transition",value:function(t,e){var n=new l.default(this,t,e);return this.config.transitions.push(n),n}}]),e}(s.default);e.default=h},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),c=n(1),s=r(c),f=n(2),l=r(f),h=function(t){function e(t,n){i(this,e);var r=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return r.config={action:n,successTrigger:new l.default(r),failureTrigger:new l.default(r)},r}return u(e,t),a(e,[{key:"onSuccess",value:function(){return this.config.successTrigger}},{key:"onFailure",value:function(){return this.config.failureTrigger}}]),e}(s.default);e.default=h},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),c=n(1),s=r(c),f=function(t){function e(t){i(this,e);var n=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return n.config={stateEnterHooks:[],stateExitHooks:[],stateChangeHooks:[],transitionHooks:[],unhandledEventHooks:[]},n}return u(e,t),a(e,[{key:"onStateEnter",value:function(t){return this.config.stateEnterHooks.push(t),this}},{key:"onStateExit",value:function(t){return this.config.stateExitHooks.push(t),this}},{key:"onStateChange",value:function(t){return this.config.stateChangeHooks.push(t),this}},{key:"onTransition",value:function(t){return this.config.transitionHooks.push(t),this}},{key:"onUnhandledEvent",value:function(t){return this.config.unhandledEventHooks.push(t),this}}]),e}(s.default);e.default=f},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),c=n(1),s=r(c),f=n(2),l=r(f),h=n(11),y=r(h),p=n(3),d=r(p),b=function(t){function e(t){i(this,e);var n=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return n.config={entryActions:[],exitActions:[],events:Object.create(null),timers:[],asyncActions:[],submachine:null},n}return u(e,t),a(e,[{key:"onEnter",value:function(t){return this.config.entryActions.push(t),this}},{key:"onExit",value:function(t){return this.config.exitActions.push(t),this}},{key:"on",value:function(t){return this.config.events[t]||(this.config.events[t]=new l.default(this)),this.config.events[t]}},{key:"onTimeout",value:function(t){var e=new y.default(this,t);return this.config.timers.push(e),e}},{key:"do",value:function(t){var e=new d.default(this,t);return this.config.asyncActions.push(e),e}},{key:"submachine",value:function(t){return this.config.submachine=t,this}}]),e}(s.default);e.default=b},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),c=n(1),s=r(c),f=function(t){function e(t,n,r){i(this,e);var u=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return u.config={targetState:n,isInternal:null===n&&r,actions:[],condition:null},u}return u(e,t),a(e,[{key:"withAction",value:function(t){return this.config.actions.push(t),this}},{key:"withCondition",value:function(t){return this.config.condition=t,this}}]),e}(s.default);e.default=f},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var o=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),u=n(14),a=r(u),c=n(15),s=r(c),f=function(){function t(e,n,r){i(this,t),this.rootStateMachine=e,this.currentStateMachine=n,this.taskScheduler=r}return o(t,[{key:"getCurrentState",value:function(){return this.currentStateMachine.getCurrentState()}},{key:"getSubmachine",value:function(){var e=this.currentStateMachine.getSubmachine();return e?new t(this.rootStateMachine,e,this.taskScheduler):null}},{key:"getStateHierarchy",value:function(){return this.getStateMachines().map(function(t){return t.getCurrentState()})}},{key:"canHandle",value:function(t,e){for(var n=this.getStateMachines(),r=n.length-1;r>=0;r--)if(n[r].canHandle(t,e))return!0;return!1}},{key:"handle",value:function(t,e){var n=this;return this.taskScheduler.enqueue(function(){for(var r=n.getStateMachines(),i=r.length-1;i>=0;i--)if(r[i].tryHandle(t,e))return;n.currentStateMachine.handleUnhandledEvent(t,e)}),this}},{key:"getStateMachines",value:function(){var t=[],e=this.rootStateMachine;do t.push(e),e=e.getSubmachine();while(e);return t}},{key:"toString",value:function(){return"StateMachine(currentState: "+this.getCurrentState()+")"}}],[{key:"start",value:function(e){var n=new s.default,r=void 0,i=function(e){return{stateMachine:new t(r,e,n)}};return r=new a.default(e,n,i),n.execute(function(){return r.start()}),new t(r,r,n)}}]),t}();e.default=f},function(t,e){"use strict";function n(t,e){return Object.keys(e).forEach(function(n){t[n]=e[n]}),t}Object.defineProperty(e,"__esModule",{value:!0}),e.default=n},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(e,"__esModule",{value:!0});var i=n(13),o=n(7),u=r(o),a={configure:function(){return new i.StateMachineConfigurator},start:function(t){return u.default.start(t)}};e.default=a},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var a=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),c=n(1),s=r(c),f=n(4),l=r(f),h=n(5),y=r(h),p=n(7),d=r(p),b=function(t){function e(){i(this,e);var t=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this));return t.config={global:new l.default(t),initialState:null,states:Object.create(null)},t}return u(e,t),a(e,[{key:"global",value:function(){return this.config.global}},{key:"initialState",value:function(t){return this.config.initialState=t,this.state(t)}},{key:"state",value:function(t){return this.config.states[t]||(this.config.states[t]=new y.default(this)),this.config.states[t]}},{key:"getConfig",value:function(){return this.buildConfig()}},{key:"start",value:function(){var t=this.getConfig();return d.default.start(t)}}]),e}(s.default);e.default=b},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var a=n(2),c=r(a),s=function(t){function e(t,n){i(this,e);var r=o(this,(e.__proto__||Object.getPrototypeOf(e)).call(this,t));return r.config.timeout=n,r}return u(e,t),e}(c.default);e.default=s},function(t,e){"use strict";function n(t,e){var n=t.prototype,r=e.prototype;Object.getOwnPropertyNames(r).filter(function(t){return!n[t]&&r[t]instanceof Function&&r[t]!==e}).forEach(function(t){n[t]=function(){for(var n=r[t],i=arguments.length,o=Array(i),u=0;u<i;u++)o[u]=arguments[u];return n.apply(this.getAncestor(e),o)}})}Object.defineProperty(e,"__esModule",{value:!0}),e.default=n},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}Object.defineProperty(e,"__esModule",{value:!0}),e.StateMachineConfigurator=void 0;var i=n(10),o=r(i),u=n(4),a=r(u),c=n(5),s=r(c),f=n(2),l=r(f),h=n(6),y=r(h),p=n(3),d=r(p),b=n(12),v=r(b);e.StateMachineConfigurator=o.default,(0,v.default)(a.default,o.default),(0,v.default)(s.default,o.default),(0,v.default)(y.default,s.default),(0,v.default)(y.default,l.default),(0,v.default)(y.default,d.default)},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),a=n(16),c=r(a),s=n(8),f=r(s),l=function(){},h=function(){function t(e,n,r){if(i(this,t),void 0===e||null===e)throw new Error("Configuration must be specified.");if("object"!==("undefined"==typeof e?"undefined":o(e)))throw new Error("Configuration must be an object.");if(void 0===e.initialState||null===e.initialState)throw new Error("Initial state must be specified.");this.config=e,this.taskScheduler=n,this.contextFactory=r,this.currentState=null,this.submachines=Object.create(null),this.timerIDs=null,this.asyncActionCancelers=null,this.handleAsyncActionComplete=this.handleAsyncActionComplete.bind(this),this.handleTimeout=this.handleTimeout.bind(this)}return u(t,[{key:"getCurrentState",value:function(){return this.currentState}},{key:"canHandle",value:function(t,e){if(!this.isStarted())return!1;var n=this.createContextWithEvent(t,e);return!!this.getFirstAllowedTransitionForEvent(n)}},{key:"tryHandle",value:function(t,e){if(!this.isStarted())return!1;var n=this.createContextWithEvent(t,e),r=this.getFirstAllowedTransitionForEvent(n);return!!r&&(this.executeTransition(r,n),!0)}},{key:"handleUnhandledEvent",value:function(t,e){if(!(this.config.global.unhandledEventHooks.length>0))throw new Error("Unhandled event '"+t+"' in state '"+this.currentState+"'.");(0,c.default)(this.config.global.unhandledEventHooks,t,this.currentState,this.createContextWithEvent(t,e))}},{key:"isStarted",value:function(){return null!==this.currentState}},{key:"start",value:function(){this.isStarted()||this.enterState(this.config.initialState,this.createContext())}},{key:"stop",value:function(){this.isStarted()&&(this.exitState(this.createContext()),this.currentState=null)}},{key:"getSubmachine",value:function(){return this.isStarted()?this.submachines[this.currentState]:null}},{key:"executeTransition",value:function(t,e){t.isInternal||this.exitState(e);var n=null!==t.targetState?t.targetState:this.currentState;(0,c.default)(this.config.global.transitionHooks,this.currentState,n,e),(0,c.default)(t.actions,this.currentState,n,e),t.isInternal||this.enterState(n,e)}},{key:"enterState",value:function(t,e){(0,c.default)(this.config.global.stateEnterHooks,t,e);var n=this.config.states[t];n&&(0,c.default)(n.entryActions,t,e),null!==this.currentState&&this.currentState!==t&&(0,c.default)(this.config.global.stateChangeHooks,this.currentState,t,e);try{this.startAsyncActions(t,e),this.startTimers(t),this.startSubmachines(t)}catch(t){throw this.stopTimers(),this.cancelAsyncActions(),t}this.currentState=t}},{key:"exitState",value:function(t){this.stopSubmachines(),this.stopTimers(),this.cancelAsyncActions(),(0,c.default)(this.config.global.stateExitHooks,this.currentState,t);var e=this.config.states[this.currentState];e&&(0,c.default)(e.exitActions,this.currentState,t)}},{key:"startAsyncActions",value:function(t,e){var n=this,r=this.config.states[t];r&&r.asyncActions.forEach(function(r){return n.startAsyncAction(r,t,e)})}},{key:"startAsyncAction",value:function(t,e,n){var r=t.action,i=t.successTrigger,o=t.failureTrigger,u=this.handleAsyncActionComplete;r(e,n).then(function(t){return u(i,{result:t})},function(t){return u(o,{error:t})}),this.asyncActionCancelers=this.asyncActionCancelers||[],this.asyncActionCancelers.push(function(){u=l})}},{key:"cancelAsyncActions",value:function(){this.asyncActionCancelers&&((0,c.default)(this.asyncActionCancelers),this.asyncActionCancelers=null)}},{key:"handleAsyncActionComplete",value:function(t,e){var n=(0,f.default)(this.createContext(),e);this.executeTrigger(t,n)}},{key:"startTimers",value:function(t){var e=this,n=this.config.states[t];n&&n.timers.length>0&&(this.timerIDs=n.timers.map(function(t){return setTimeout(e.handleTimeout,t.timeout,t)}))}},{key:"stopTimers",value:function(){this.timerIDs&&(this.timerIDs.forEach(clearTimeout),this.timerIDs=null)}},{key:"handleTimeout",value:function(t){this.executeTrigger(t,this.createContext())}},{key:"startSubmachines",value:function(e){var n=this.config.states[e];n&&n.submachine&&(this.submachines[e]||(this.submachines[e]=new t(n.submachine,this.taskScheduler,this.contextFactory)),this.submachines[e].start())}},{key:"stopSubmachines",value:function(){var t=this.submachines[this.currentState];t&&t.stop()}},{key:"createContext",value:function(){return this.contextFactory(this)}},{key:"createContextWithEvent",value:function(t,e){var n=this.createContext();return n.event=t,void 0!==e&&(n.eventPayload=e),n}},{key:"getFirstAllowedTransitionForEvent",value:function(e){var n=this.config.states[this.currentState];if(!n)return null;var r=n.events[e.event];return r?t.getFirstAllowedTransition(r.transitions,e):null}},{key:"executeTrigger",value:function(e,n){var r=this;this.taskScheduler.execute(function(){var i=t.getFirstAllowedTransition(e.transitions,n);i&&r.executeTransition(i,n)})}}],[{key:"getFirstAllowedTransition",value:function(t,e){for(var n=0;n<t.length;n++)if(!t[n].condition||t[n].condition(e))return t[n];return null}}]),t}();e.default=h},function(t,e){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!0});var r=function(){function t(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}return function(e,n,r){return n&&t(e.prototype,n),r&&t(e,r),e}}(),i=function(){function t(){n(this,t),this.queue=[],this.isBusy=!1}return r(t,[{key:"enqueue",value:function(t){this.isBusy?this.queue.push(t):this.execute(t)}},{key:"execute",value:function(t){if(this.isBusy)throw new Error("Cannot execute task because another task is already running.");this.isBusy=!0;try{for(t();this.queue.length>0;){var e=this.queue.shift();e()}}finally{this.queue.length>0&&(this.queue=[]),this.isBusy=!1}}}]),t}();e.default=i},function(t,e){"use strict";function n(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;r<e;r++)n[r-1]=arguments[r];t.forEach(function(t){return t.apply(void 0,n)})}Object.defineProperty(e,"__esModule",{value:!0}),e.default=n},function(t,e){"use strict";function n(t,e){var n=Object.getPrototypeOf(t),r=Object.create(n);return Object.keys(t).forEach(function(n){r[n]=e(t[n])}),r}Object.defineProperty(e,"__esModule",{value:!0}),e.default=n}])}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Finity=e():t.Finity=e()}(this,function(){return function(t){function e(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=17)}([function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}e.__esModule=!0;var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u=n(16),s=o(u),a=function(){function t(e){r(this,t),this.parent=e}return t.prototype.getAncestor=function(t){return this.parent?this.parent instanceof t?this.parent:this.parent.getAncestor(t):null},t.prototype.buildConfig=function(){var e=function e(n){return n?n instanceof t?n.buildConfig():Array.isArray(n)?n.map(e):n&&"object"===("undefined"==typeof n?"undefined":i(n))?(0,s.default)(n,e):n:n};return(0,s.default)(this.config,e)},t}();e.default=a},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.__esModule=!0;var s=n(0),a=o(s),c=n(6),f=o(c),l=function(t){function e(n){r(this,e);var o=i(this,t.call(this,n));return o.config={transitions:[]},o}return u(e,t),e.prototype.transitionTo=function(t){return this.transition(t)},e.prototype.selfTransition=function(){return this.transition(null)},e.prototype.internalTransition=function(){return this.transition(null,!0)},e.prototype.transition=function(t,e){var n=new f.default(this,t,e);return this.config.transitions.push(n),n},e}(a.default);e.default=l},function(t,e,n){"use strict";function o(t,e){return Object.keys(e).forEach(function(n){t[n]=e[n]}),t}e.__esModule=!0,e.default=o},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.__esModule=!0;var s=n(0),a=o(s),c=n(1),f=o(c),l=function(t){function e(n,o){r(this,e);var u=i(this,t.call(this,n));return u.config={action:o,successTrigger:new f.default(u),failureTrigger:new f.default(u)},u}return u(e,t),e.prototype.onSuccess=function(){return this.config.successTrigger},e.prototype.onFailure=function(){return this.config.failureTrigger},e}(a.default);e.default=l},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.__esModule=!0;var s=n(0),a=o(s),c=function(t){function e(n){r(this,e);var o=i(this,t.call(this,n));return o.config={stateEnterHooks:[],stateExitHooks:[],stateChangeHooks:[],transitionHooks:[],unhandledEventHooks:[]},o}return u(e,t),e.prototype.onStateEnter=function(t){return this.config.stateEnterHooks.push(t),this},e.prototype.onStateExit=function(t){return this.config.stateExitHooks.push(t),this},e.prototype.onStateChange=function(t){return this.config.stateChangeHooks.push(t),this},e.prototype.onTransition=function(t){return this.config.transitionHooks.push(t),this},e.prototype.onUnhandledEvent=function(t){return this.config.unhandledEventHooks.push(t),this},e}(a.default);e.default=c},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.__esModule=!0;var s=n(0),a=o(s),c=n(1),f=o(c),l=n(10),h=o(l),p=n(3),y=o(p),d=function(t){function e(n){r(this,e);var o=i(this,t.call(this,n));return o.config={entryActions:[],exitActions:[],events:Object.create(null),timers:[],asyncActions:[],submachine:null},o}return u(e,t),e.prototype.onEnter=function(t){return this.config.entryActions.push(t),this},e.prototype.onExit=function(t){return this.config.exitActions.push(t),this},e.prototype.on=function(t){return this.config.events[t]||(this.config.events[t]=new f.default(this)),this.config.events[t]},e.prototype.onTimeout=function(t){var e=new h.default(this,t);return this.config.timers.push(e),e},e.prototype.do=function(t){var e=new y.default(this,t);return this.config.asyncActions.push(e),e},e.prototype.submachine=function(t){return this.config.submachine=t,this},e}(a.default);e.default=d},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.__esModule=!0;var s=n(0),a=o(s),c=function(t){function e(n,o,u){r(this,e);var s=i(this,t.call(this,n));return s.config={targetState:o,isInternal:null===o&&u,actions:[],condition:null},s}return u(e,t),e.prototype.withAction=function(t){return this.config.actions.push(t),this},e.prototype.withCondition=function(t){return this.config.condition=t,this},e}(a.default);e.default=c},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}e.__esModule=!0;var i=n(13),u=o(i),s=n(14),a=o(s),c=function(){function t(e,n,o){r(this,t),this.rootStateMachine=e,this.currentStateMachine=n,this.taskScheduler=o}return t.start=function(e){var n=new a.default,o=void 0,r=function(e){return{stateMachine:new t(o,e,n)}};return o=new u.default(e,n,r),n.execute(function(){return o.start()}),new t(o,o,n)},t.prototype.getCurrentState=function(){return this.currentStateMachine.getCurrentState()},t.prototype.getSubmachine=function(){var e=this.currentStateMachine.getSubmachine();return e?new t(this.rootStateMachine,e,this.taskScheduler):null},t.prototype.getStateHierarchy=function(){return this.getStateMachines().map(function(t){return t.getCurrentState()})},t.prototype.canHandle=function(t,e){for(var n=this.getStateMachines(),o=n.length-1;o>=0;o--)if(n[o].canHandle(t,e))return!0;return!1},t.prototype.handle=function(t,e){var n=this;return this.taskScheduler.enqueue(function(){for(var o=n.getStateMachines(),r=o.length-1;r>=0;r--)if(o[r].tryHandle(t,e))return;n.currentStateMachine.handleUnhandledEvent(t,e)}),this},t.prototype.getStateMachines=function(){var t=[],e=this.rootStateMachine;do t.push(e),e=e.getSubmachine();while(e);return t},t.prototype.toString=function(){return"StateMachine(currentState: "+this.getCurrentState()+")"},t}();e.default=c},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}e.__esModule=!0;var r=n(12),i=n(7),u=o(i),s={configure:function(){return new r.StateMachineConfigurator},start:function(t){return u.default.start(t)}};e.default=s},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.__esModule=!0;var s=n(0),a=o(s),c=n(4),f=o(c),l=n(5),h=o(l),p=n(7),y=o(p),d=function(t){function e(){r(this,e);var n=i(this,t.call(this));return n.config={global:new f.default(n),initialState:null,states:Object.create(null)},n}return u(e,t),e.prototype.global=function(){return this.config.global},e.prototype.initialState=function(t){return this.config.initialState=t,this.state(t)},e.prototype.state=function(t){return this.config.states[t]||(this.config.states[t]=new h.default(this)),this.config.states[t]},e.prototype.getConfig=function(){return this.buildConfig()},e.prototype.start=function(){var t=this.getConfig();return y.default.start(t)},e}(a.default);e.default=d},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}function u(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}e.__esModule=!0;var s=n(1),a=o(s),c=function(t){function e(n,o){r(this,e);var u=i(this,t.call(this,n));return u.config.timeout=o,u}return u(e,t),e}(a.default);e.default=c},function(t,e,n){"use strict";function o(t,e){var n=t.prototype,o=e.prototype;Object.getOwnPropertyNames(o).filter(function(t){return!n[t]&&o[t]instanceof Function&&o[t]!==e}).forEach(function(t){n[t]=function(){for(var n=o[t],r=arguments.length,i=Array(r),u=0;u<r;u++)i[u]=arguments[u];return n.apply(this.getAncestor(e),i)}})}e.__esModule=!0,e.default=o},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}e.__esModule=!0,e.StateMachineConfigurator=void 0;var r=n(9),i=o(r),u=n(4),s=o(u),a=n(5),c=o(a),f=n(1),l=o(f),h=n(6),p=o(h),y=n(3),d=o(y),b=n(11),g=o(b);e.StateMachineConfigurator=i.default,(0,g.default)(s.default,i.default),(0,g.default)(c.default,i.default),(0,g.default)(p.default,c.default),(0,g.default)(p.default,l.default),(0,g.default)(p.default,d.default)},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}e.__esModule=!0;var i="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},u=n(15),s=o(u),a=n(2),c=o(a),f=function(){},l=function(){function t(e,n,o){if(r(this,t),void 0===e||null===e)throw new Error("Configuration must be specified.");if("object"!==("undefined"==typeof e?"undefined":i(e)))throw new Error("Configuration must be an object.");if(void 0===e.initialState||null===e.initialState)throw new Error("Initial state must be specified.");this.config=e,this.taskScheduler=n,this.contextFactory=o,this.currentState=null,this.submachines=Object.create(null),this.timerIDs=null,this.asyncActionCancelers=null,this.handleAsyncActionComplete=this.handleAsyncActionComplete.bind(this),this.handleTimeout=this.handleTimeout.bind(this)}return t.prototype.getCurrentState=function(){return this.currentState},t.prototype.canHandle=function(t,e){if(!this.isStarted())return!1;var n=this.createContextWithEvent(t,e);return!!this.getFirstAllowedTransitionForEvent(n)},t.prototype.tryHandle=function(t,e){if(!this.isStarted())return!1;var n=this.createContextWithEvent(t,e),o=this.getFirstAllowedTransitionForEvent(n);return!!o&&(this.executeTransition(o,n),!0)},t.prototype.handleUnhandledEvent=function(t,e){if(!(this.config.global.unhandledEventHooks.length>0))throw new Error("Unhandled event '"+t+"' in state '"+this.currentState+"'.");(0,s.default)(this.config.global.unhandledEventHooks,t,this.currentState,this.createContextWithEvent(t,e))},t.prototype.isStarted=function(){return null!==this.currentState},t.prototype.start=function(){this.isStarted()||this.enterState(this.config.initialState,this.createContext())},t.prototype.stop=function(){this.isStarted()&&(this.exitState(this.createContext()),this.currentState=null)},t.prototype.getSubmachine=function(){return this.isStarted()?this.submachines[this.currentState]:null},t.prototype.executeTransition=function(t,e){t.isInternal||this.exitState(e);var n=null!==t.targetState?t.targetState:this.currentState;(0,s.default)(this.config.global.transitionHooks,this.currentState,n,e),(0,s.default)(t.actions,this.currentState,n,e),t.isInternal||this.enterState(n,e)},t.prototype.enterState=function(t,e){(0,s.default)(this.config.global.stateEnterHooks,t,e);var n=this.config.states[t];n&&(0,s.default)(n.entryActions,t,e),null!==this.currentState&&this.currentState!==t&&(0,s.default)(this.config.global.stateChangeHooks,this.currentState,t,e);try{this.startAsyncActions(t,e),this.startTimers(t),this.startSubmachines(t)}catch(t){throw this.stopTimers(),this.cancelAsyncActions(),t}this.currentState=t},t.prototype.exitState=function(t){this.stopSubmachines(),this.stopTimers(),this.cancelAsyncActions(),(0,s.default)(this.config.global.stateExitHooks,this.currentState,t);var e=this.config.states[this.currentState];e&&(0,s.default)(e.exitActions,this.currentState,t)},t.prototype.startAsyncActions=function(t,e){var n=this,o=this.config.states[t];o&&o.asyncActions.forEach(function(o){return n.startAsyncAction(o,t,e)})},t.prototype.startAsyncAction=function(t,e,n){var o=t.action,r=t.successTrigger,i=t.failureTrigger,u=this.handleAsyncActionComplete;o(e,n).then(function(t){return u(r,{result:t})},function(t){return u(i,{error:t})}),this.asyncActionCancelers=this.asyncActionCancelers||[],this.asyncActionCancelers.push(function(){u=f})},t.prototype.cancelAsyncActions=function(){this.asyncActionCancelers&&((0,s.default)(this.asyncActionCancelers),this.asyncActionCancelers=null)},t.prototype.handleAsyncActionComplete=function(t,e){var n=(0,c.default)(this.createContext(),e);this.executeTrigger(t,n)},t.prototype.startTimers=function(t){var e=this,n=this.config.states[t];n&&n.timers.length>0&&(this.timerIDs=n.timers.map(function(t){return setTimeout(e.handleTimeout,t.timeout,t)}))},t.prototype.stopTimers=function(){this.timerIDs&&(this.timerIDs.forEach(clearTimeout),this.timerIDs=null)},t.prototype.handleTimeout=function(t){this.executeTrigger(t,this.createContext())},t.prototype.startSubmachines=function(e){var n=this.config.states[e];n&&n.submachine&&(this.submachines[e]||(this.submachines[e]=new t(n.submachine,this.taskScheduler,this.contextFactory)),this.submachines[e].start())},t.prototype.stopSubmachines=function(){var t=this.submachines[this.currentState];t&&t.stop()},t.prototype.createContext=function(){return this.contextFactory(this)},t.prototype.createContextWithEvent=function(t,e){var n=this.createContext();return n.event=t,void 0!==e&&(n.eventPayload=e),n},t.getFirstAllowedTransition=function(t,e){for(var n=0;n<t.length;n++)if(!t[n].condition||t[n].condition(e))return t[n];return null},t.prototype.getFirstAllowedTransitionForEvent=function(e){var n=this.config.states[this.currentState];if(!n)return null;var o=n.events[e.event];return o?t.getFirstAllowedTransition(o.transitions,e):null},t.prototype.executeTrigger=function(e,n){var o=this;this.taskScheduler.execute(function(){var r=t.getFirstAllowedTransition(e.transitions,n);r&&o.executeTransition(r,n)})},t}();e.default=l},function(t,e,n){"use strict";function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}e.__esModule=!0;var r=function(){function t(){o(this,t),this.queue=[],this.isBusy=!1}return t.prototype.enqueue=function(t){this.isBusy?this.queue.push(t):this.execute(t)},t.prototype.execute=function(t){if(this.isBusy)throw new Error("Cannot execute task because another task is already running.");this.isBusy=!0;try{for(t();this.queue.length>0;){var e=this.queue.shift();e()}}finally{this.queue.length>0&&(this.queue=[]),this.isBusy=!1}},t}();e.default=r},function(t,e,n){"use strict";function o(t){for(var e=arguments.length,n=Array(e>1?e-1:0),o=1;o<e;o++)n[o-1]=arguments[o];t.forEach(function(t){return t.apply(void 0,n)})}e.__esModule=!0,e.default=o},function(t,e,n){"use strict";function o(t,e){var n=Object.getPrototypeOf(t),o=Object.create(n);return Object.keys(t).forEach(function(n){o[n]=e(t[n])}),o}e.__esModule=!0,e.default=o},function(t,e,n){"use strict";var o=n(8).default,r=n(2).default;r(e,o),e.default=o}])}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
510
0
123660
22
1687