Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

finity

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

finity - npm Package Compare versions

Comparing version 0.4.5 to 0.5.0

CODE_OF_CONDUCT.md

9

index.d.ts

@@ -34,10 +34,4 @@ declare const Finity: {

export interface Thenable<V> {
then<R>(onFulfilled?: (value: V) => R | Thenable<R>, onRejected?: (reason: any) => R | Thenable<R>): Thenable<R>;
then<R>(onFulfilled?: (value: V) => R | Thenable<R>, onRejected?: (reason: any) => void): Thenable<R>;
catch<R>(onRejected?: (reason: any) => R | Thenable<R>): Thenable<R>;
}
export type AsyncOperation<S, E> = (state: S, context: Context<S, E>) => Promise<any>;
export type AsyncOperation<S, E> = (state: S, context: Context<S, E>) => Thenable<any>;
export interface StateConfigurator<S, E> extends BaseConfigurator<S, E>, StateMachineConfigurator<S, E> {

@@ -49,2 +43,3 @@ onEnter(action: StateAction<S, E>): StateConfigurator<S, E>;

do(asyncOperation: AsyncOperation<S, E>): AsyncConfigurator<S, E>;
submachine<S2>(submachineConfig: Configuration<S2, E>): StateConfigurator<S, E>;
}

@@ -51,0 +46,0 @@

@@ -17,6 +17,2 @@ 'use strict';

var _deepCopy = require('../utils/deepCopy');
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -36,9 +32,9 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(AsyncActionConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (AsyncActionConfigurator.__proto__ || Object.getPrototypeOf(AsyncActionConfigurator)).call(this, parent));
_this.config = {
action: action
action: action,
successTrigger: new _TriggerConfigurator2.default(_this),
failureTrigger: new _TriggerConfigurator2.default(_this)
};
_this.successConfigurator = new _TriggerConfigurator2.default(_this);
_this.failureConfigurator = new _TriggerConfigurator2.default(_this);
return _this;

@@ -50,3 +46,3 @@ }

value: function onSuccess() {
return this.successConfigurator;
return this.config.successTrigger;
}

@@ -56,12 +52,4 @@ }, {

value: function onFailure() {
return this.failureConfigurator;
return this.config.failureTrigger;
}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
var config = (0, _deepCopy2.default)(this.config);
config.onSuccess = this.successConfigurator.internalGetConfig();
config.onFailure = this.failureConfigurator.internalGetConfig();
return config;
}
}]);

@@ -68,0 +56,0 @@

@@ -1,2 +0,2 @@

"use strict";
'use strict';

@@ -7,4 +7,12 @@ Object.defineProperty(exports, "__esModule", {

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');
var _mapValues2 = _interopRequireDefault(_mapValues);
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"); } }

@@ -20,3 +28,3 @@

_createClass(BaseConfigurator, [{
key: "getAncestor",
key: 'getAncestor',
value: function getAncestor(type) {

@@ -28,2 +36,22 @@ if (this.parent) {

}
}, {
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);
}
}]);

@@ -30,0 +58,0 @@

@@ -13,6 +13,2 @@ 'use strict';

var _deepCopy = require('../utils/deepCopy');
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -32,3 +28,3 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(GlobalConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (GlobalConfigurator.__proto__ || Object.getPrototypeOf(GlobalConfigurator)).call(this, parent));

@@ -75,7 +71,2 @@ _this.config = {

}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
return (0, _deepCopy2.default)(this.config);
}
}]);

@@ -82,0 +73,0 @@

@@ -8,15 +8,38 @@ 'use strict';

var _StateMachineConfigurator2 = require('./StateMachineConfigurator');
var _StateMachineConfigurator = require('./StateMachineConfigurator');
var _StateMachineConfigurator3 = _interopRequireDefault(_StateMachineConfigurator2);
var _StateMachineConfigurator2 = _interopRequireDefault(_StateMachineConfigurator);
var _setUpDelegation = require('./setUpDelegation');
var _GlobalConfigurator = require('./GlobalConfigurator');
var _setUpDelegation2 = _interopRequireDefault(_setUpDelegation);
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator);
var _StateConfigurator = require('./StateConfigurator');
var _StateConfigurator2 = _interopRequireDefault(_StateConfigurator);
var _TriggerConfigurator = require('./TriggerConfigurator');
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator);
var _TransitionConfigurator = require('./TransitionConfigurator');
var _TransitionConfigurator2 = _interopRequireDefault(_TransitionConfigurator);
var _AsyncActionConfigurator = require('./AsyncActionConfigurator');
var _AsyncActionConfigurator2 = _interopRequireDefault(_AsyncActionConfigurator);
var _delegateToAncestor = require('./delegateToAncestor');
var _delegateToAncestor2 = _interopRequireDefault(_delegateToAncestor);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.StateMachineConfigurator = _StateMachineConfigurator3.default;
exports.StateMachineConfigurator = _StateMachineConfigurator2.default; /* eslint-disable import/prefer-default-export */
(0, _setUpDelegation2.default)();
(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);

@@ -25,8 +25,2 @@ 'use strict';

var _ConfiguratorHelper = require('./ConfiguratorHelper');
var _deepCopy = require('../utils/deepCopy');
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -46,11 +40,12 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(StateConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (StateConfigurator.__proto__ || Object.getPrototypeOf(StateConfigurator)).call(this, parent));
_this.config = {
entryActions: [],
exitActions: []
exitActions: [],
events: Object.create(null),
timers: [],
asyncActions: [],
submachine: null
};
_this.eventConfigurators = Object.create(null);
_this.timerConfigurators = [];
_this.asyncActionConfigurators = [];
return _this;

@@ -74,6 +69,6 @@ }

value: function on(event) {
if (!this.eventConfigurators[event]) {
this.eventConfigurators[event] = new _TriggerConfigurator2.default(this);
if (!this.config.events[event]) {
this.config.events[event] = new _TriggerConfigurator2.default(this);
}
return this.eventConfigurators[event];
return this.config.events[event];
}

@@ -84,3 +79,3 @@ }, {

var timerConfigurator = new _TimerConfigurator2.default(this, timeout);
this.timerConfigurators.push(timerConfigurator);
this.config.timers.push(timerConfigurator);
return timerConfigurator;

@@ -92,13 +87,10 @@ }

var asyncActionConfigurator = new _AsyncActionConfigurator2.default(this, asyncAction);
this.asyncActionConfigurators.push(asyncActionConfigurator);
this.config.asyncActions.push(asyncActionConfigurator);
return asyncActionConfigurator;
}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
var config = (0, _deepCopy2.default)(this.config);
config.events = (0, _ConfiguratorHelper.mapToConfig)(this.eventConfigurators);
config.timers = (0, _ConfiguratorHelper.mapToConfig)(this.timerConfigurators);
config.asyncActions = (0, _ConfiguratorHelper.mapToConfig)(this.asyncActionConfigurators);
return config;
key: 'submachine',
value: function submachine(submachineConfig) {
this.config.submachine = submachineConfig;
return this;
}

@@ -105,0 +97,0 @@ }]);

@@ -21,16 +21,6 @@ 'use strict';

var _StateMachine = require('../core/StateMachine');
var _HierarchicalStateMachine = require('../core/HierarchicalStateMachine');
var _StateMachine2 = _interopRequireDefault(_StateMachine);
var _HierarchicalStateMachine2 = _interopRequireDefault(_HierarchicalStateMachine);
var _ConfiguratorHelper = require('./ConfiguratorHelper');
var _deepCopy = require('../utils/deepCopy');
var _deepCopy2 = _interopRequireDefault(_deepCopy);
var _merge = require('../utils/merge');
var _merge2 = _interopRequireDefault(_merge);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -50,9 +40,9 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(StateMachineConfigurator).call(this));
var _this = _possibleConstructorReturn(this, (StateMachineConfigurator.__proto__ || Object.getPrototypeOf(StateMachineConfigurator)).call(this));
_this.config = {
initialState: null
global: new _GlobalConfigurator2.default(_this),
initialState: null,
states: Object.create(null)
};
_this.globalConfigurator = new _GlobalConfigurator2.default(_this);
_this.stateConfigurators = Object.create(null);
return _this;

@@ -64,3 +54,3 @@ }

value: function global() {
return this.globalConfigurator;
return this.config.global;
}

@@ -76,6 +66,6 @@ }, {

value: function state(_state) {
if (!this.stateConfigurators[_state]) {
this.stateConfigurators[_state] = new _StateConfigurator2.default(this);
if (!this.config.states[_state]) {
this.config.states[_state] = new _StateConfigurator2.default(this);
}
return this.stateConfigurators[_state];
return this.config.states[_state];
}

@@ -85,5 +75,3 @@ }, {

value: function getConfig() {
var config = (0, _deepCopy2.default)(this.config);
config.states = (0, _ConfiguratorHelper.mapToConfig)(this.stateConfigurators);
return (0, _merge2.default)(config, this.globalConfigurator.internalGetConfig());
return this.buildConfig();
}

@@ -94,3 +82,3 @@ }, {

var config = this.getConfig();
return _StateMachine2.default.start(config);
return _HierarchicalStateMachine2.default.start(config);
}

@@ -97,0 +85,0 @@ }]);

@@ -7,6 +7,2 @@ 'use strict';

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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _TriggerConfigurator2 = require('./TriggerConfigurator');

@@ -16,6 +12,2 @@

var _merge = require('../utils/merge');
var _merge2 = _interopRequireDefault(_merge);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -35,17 +27,8 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TrimerConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (TrimerConfigurator.__proto__ || Object.getPrototypeOf(TrimerConfigurator)).call(this, parent));
_this.config = {
timeout: timeout
};
_this.config.timeout = timeout;
return _this;
}
_createClass(TrimerConfigurator, [{
key: 'internalGetConfig',
value: function internalGetConfig() {
return (0, _merge2.default)(_get(Object.getPrototypeOf(TrimerConfigurator.prototype), 'internalGetConfig', this).call(this), this.config);
}
}]);
return TrimerConfigurator;

@@ -52,0 +35,0 @@ }(_TriggerConfigurator3.default);

@@ -13,6 +13,2 @@ 'use strict';

var _deepCopy = require('../utils/deepCopy');
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -32,3 +28,3 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TransitionConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (TransitionConfigurator.__proto__ || Object.getPrototypeOf(TransitionConfigurator)).call(this, parent));

@@ -56,7 +52,2 @@ _this.config = {

}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
return (0, _deepCopy2.default)(this.config);
}
}]);

@@ -63,0 +54,0 @@

@@ -17,4 +17,2 @@ 'use strict';

var _ConfiguratorHelper = require('./ConfiguratorHelper');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -34,5 +32,7 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TriggerConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (TriggerConfigurator.__proto__ || Object.getPrototypeOf(TriggerConfigurator)).call(this, parent));
_this.transitionConfigurators = [];
_this.config = {
transitions: []
};
return _this;

@@ -60,12 +60,5 @@ }

var transitionConfigurator = new _TransitionConfigurator2.default(this, targetState, isInternal);
this.transitionConfigurators.push(transitionConfigurator);
this.config.transitions.push(transitionConfigurator);
return transitionConfigurator;
}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
return {
transitions: (0, _ConfiguratorHelper.mapToConfig)(this.transitionConfigurators)
};
}
}]);

@@ -72,0 +65,0 @@

@@ -7,13 +7,13 @@ 'use strict';

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
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 _AsyncActionSubscription = require('./AsyncActionSubscription');
var _invokeEach = require('../utils/invokeEach');
var _AsyncActionSubscription2 = _interopRequireDefault(_AsyncActionSubscription);
var _invokeEach2 = _interopRequireDefault(_invokeEach);
var _executeHandlers = require('../utils/executeHandlers');
var _merge = require('../utils/merge');
var _executeHandlers2 = _interopRequireDefault(_executeHandlers);
var _merge2 = _interopRequireDefault(_merge);

@@ -24,4 +24,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var noop = function noop() {};
var StateMachine = function () {
function StateMachine(config) {
function StateMachine(config, taskScheduler, contextFactory) {
_classCallCheck(this, StateMachine);

@@ -39,7 +41,9 @@

this.config = config;
this.taskScheduler = taskScheduler;
this.contextFactory = contextFactory;
this.currentState = null;
this.isBusy = false;
this.eventQueue = [];
this.submachines = Object.create(null);
this.timerIDs = null;
this.asyncActionSubscriptions = null;
this.asyncActionCancelers = null;
this.handleAsyncActionComplete = this.handleAsyncActionComplete.bind(this);
this.handleTimeout = this.handleTimeout.bind(this);

@@ -49,19 +53,2 @@ }

_createClass(StateMachine, [{
key: 'start',
value: function start() {
var _this = this;
if (!this.isStarted()) {
this.execute(function () {
return _this.enterState(_this.config.initialState, _this.createContext());
});
}
return this;
}
}, {
key: 'isStarted',
value: function isStarted() {
return this.currentState !== null;
}
}, {
key: 'getCurrentState',

@@ -77,81 +64,56 @@ value: function getCurrentState() {

}
var context = this.createContext(event, eventPayload);
return !!this.getTransitionForEvent(context);
}
}, {
key: 'handle',
value: function handle(event, eventPayload) {
var _this2 = this;
if (!this.isBusy) {
this.execute(function () {
return _this2.processEvent(event, eventPayload);
});
} else {
this.eventQueue.push({ event: event, eventPayload: eventPayload });
}
return this;
var context = this.createContextWithEvent(event, eventPayload);
return !!this.getFirstAllowedTransitionForEvent(context);
}
}, {
key: 'execute',
value: function execute(operation) {
if (this.isBusy) {
throw new Error('Operation cannot be executed because another operation is in progress.');
key: 'tryHandle',
value: function tryHandle(event, eventPayload) {
if (!this.isStarted()) {
return false;
}
this.isBusy = true;
try {
operation();
// Process all events
while (this.eventQueue.length > 0) {
var _eventQueue$shift = this.eventQueue.shift();
var event = _eventQueue$shift.event;
var eventPayload = _eventQueue$shift.eventPayload;
this.processEvent(event, eventPayload);
}
} finally {
// Clean up
if (this.eventQueue.length > 0) {
this.eventQueue = [];
}
this.isBusy = false;
var context = this.createContextWithEvent(event, eventPayload);
var transitionConfig = this.getFirstAllowedTransitionForEvent(context);
if (transitionConfig) {
this.executeTransition(transitionConfig, context);
return true;
}
return false;
}
}, {
key: 'processEvent',
value: function processEvent(event, eventPayload) {
var context = this.createContext(event, eventPayload);
var transitionConfig = this.getTransitionForEvent(context);
if (transitionConfig) {
this.executeTransition(transitionConfig, context);
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 {
this.handleUnhandledEvent(context);
throw new Error('Unhandled event \'' + event + '\' in state \'' + this.currentState + '\'.');
}
}
}, {
key: 'createContext',
value: function createContext(event, eventPayload) {
var context = { stateMachine: this };
if (event !== undefined) {
context.event = event;
key: 'isStarted',
value: function isStarted() {
return this.currentState !== null;
}
}, {
key: 'start',
value: function start() {
if (!this.isStarted()) {
this.enterState(this.config.initialState, this.createContext());
}
if (eventPayload !== undefined) {
context.eventPayload = eventPayload;
}
return context;
}
}, {
key: 'getTransitionForEvent',
value: function getTransitionForEvent(context) {
var stateConfig = this.config.states[this.currentState];
if (!stateConfig) {
return null;
key: 'stop',
value: function stop() {
if (this.isStarted()) {
this.exitState(this.createContext());
this.currentState = null;
}
var eventConfig = stateConfig.events[context.event];
return eventConfig ? this.selectTransition(eventConfig.transitions, context) : null;
}
}, {
key: 'getSubmachine',
value: function getSubmachine() {
return this.isStarted() ? this.submachines[this.currentState] : null;
}
}, {
key: 'executeTransition',

@@ -165,4 +127,4 @@ value: function executeTransition(transitionConfig, context) {

(0, _executeHandlers2.default)(this.config.transitionHooks, this.currentState, nextState, context);
(0, _executeHandlers2.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);

@@ -174,29 +136,26 @@ if (!transitionConfig.isInternal) {

}, {
key: 'handleUnhandledEvent',
value: function handleUnhandledEvent(context) {
if (this.config.unhandledEventHooks.length > 0) {
(0, _executeHandlers2.default)(this.config.unhandledEventHooks, context.event, this.currentState, context);
} else {
throw new Error('Unhandled event \'' + context.event + '\' in state \'' + this.currentState + '\'.');
}
}
}, {
key: 'enterState',
value: function enterState(state, context) {
(0, _executeHandlers2.default)(this.config.stateEnterHooks, state, context);
(0, _invokeEach2.default)(this.config.global.stateEnterHooks, state, context);
var stateConfig = this.config.states[state];
if (stateConfig) {
(0, _executeHandlers2.default)(stateConfig.entryActions, state, context);
(0, _invokeEach2.default)(stateConfig.entryActions, state, context);
}
if (this.currentState !== null && this.currentState !== state) {
(0, _executeHandlers2.default)(this.config.stateChangeHooks, this.currentState, state, context);
(0, _invokeEach2.default)(this.config.global.stateChangeHooks, this.currentState, state, context);
}
try {
this.startAsyncActions(state, context);
this.startTimers(state);
this.startSubmachines(state);
} catch (error) {
this.stopTimers();
this.cancelAsyncActions();
throw error;
}
this.currentState = state;
this.startAsyncActions(context);
this.startTimers();
}

@@ -206,11 +165,11 @@ }, {

value: function exitState(context) {
this.stopSubmachines();
this.stopTimers();
this.cancelAsyncActions();
this.cancelAsyncActionSubscriptions();
(0, _invokeEach2.default)(this.config.global.stateExitHooks, this.currentState, context);
(0, _executeHandlers2.default)(this.config.stateExitHooks, this.currentState, context);
var stateConfig = this.config.states[this.currentState];
if (stateConfig) {
(0, _executeHandlers2.default)(stateConfig.exitActions, this.currentState, context);
(0, _invokeEach2.default)(stateConfig.exitActions, this.currentState, context);
}

@@ -220,9 +179,9 @@ }

key: 'startAsyncActions',
value: function startAsyncActions(context) {
var _this3 = this;
value: function startAsyncActions(state, context) {
var _this = this;
var stateConfig = this.config.states[this.currentState];
var stateConfig = this.config.states[state];
if (stateConfig) {
stateConfig.asyncActions.forEach(function (asyncActionConfig) {
return _this3.startAsyncAction(asyncActionConfig, context);
return _this.startAsyncAction(asyncActionConfig, state, context);
});

@@ -233,51 +192,41 @@ }

key: 'startAsyncAction',
value: function startAsyncAction(asyncActionConfig, context) {
var _this4 = this;
value: function startAsyncAction(asyncActionConfig, state, context) {
var action = asyncActionConfig.action,
successTrigger = asyncActionConfig.successTrigger,
failureTrigger = asyncActionConfig.failureTrigger;
var action = asyncActionConfig.action;
var subscription = new _AsyncActionSubscription2.default(function (result) {
return _this4.handleAsyncActionSuccess(asyncActionConfig.onSuccess, result);
var handleComplete = this.handleAsyncActionComplete;
action(state, context).then(function (result) {
return handleComplete(successTrigger, { result: result });
}, function (error) {
return _this4.handleAsyncActionFailure(asyncActionConfig.onFailure, error);
return handleComplete(failureTrigger, { error: error });
});
action(this.currentState, context).then(subscription.onSuccess, subscription.onFailure);
this.asyncActionSubscriptions = this.asyncActionSubscriptions || [];
this.asyncActionSubscriptions.push(subscription);
this.asyncActionCancelers = this.asyncActionCancelers || [];
this.asyncActionCancelers.push(function () {
handleComplete = noop;
});
}
}, {
key: 'cancelAsyncActionSubscriptions',
value: function cancelAsyncActionSubscriptions() {
if (this.asyncActionSubscriptions) {
this.asyncActionSubscriptions.forEach(function (subscription) {
return subscription.cancel();
});
this.asyncActionSubscriptions = null;
key: 'cancelAsyncActions',
value: function cancelAsyncActions() {
if (this.asyncActionCancelers) {
(0, _invokeEach2.default)(this.asyncActionCancelers);
this.asyncActionCancelers = null;
}
}
}, {
key: 'handleAsyncActionSuccess',
value: function handleAsyncActionSuccess(triggerConfig, result) {
var context = this.createContext();
context.result = result;
key: 'handleAsyncActionComplete',
value: function handleAsyncActionComplete(triggerConfig, additionalContext) {
var context = (0, _merge2.default)(this.createContext(), additionalContext);
this.executeTrigger(triggerConfig, context);
}
}, {
key: 'handleAsyncActionFailure',
value: function handleAsyncActionFailure(triggerConfig, error) {
var context = this.createContext();
context.error = error;
this.executeTrigger(triggerConfig, context);
}
}, {
key: 'startTimers',
value: function startTimers() {
var _this5 = this;
value: function startTimers(state) {
var _this2 = this;
var stateConfig = this.config.states[this.currentState];
var stateConfig = this.config.states[state];
if (stateConfig && stateConfig.timers.length > 0) {
this.timerIDs = stateConfig.timers.map(function (timerConfig) {
return setTimeout(_this5.handleTimeout, timerConfig.timeout, timerConfig);
return setTimeout(_this2.handleTimeout, timerConfig.timeout, timerConfig);
});

@@ -300,16 +249,61 @@ }

}, {
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;
}
var eventConfig = stateConfig.events[context.event];
return eventConfig ? StateMachine.getFirstAllowedTransition(eventConfig.transitions, context) : null;
}
}, {
key: 'executeTrigger',
value: function executeTrigger(triggerConfig, context) {
var _this6 = this;
var _this3 = this;
this.execute(function () {
var transitionConfig = _this6.selectTransition(triggerConfig.transitions, context);
this.taskScheduler.execute(function () {
var transitionConfig = StateMachine.getFirstAllowedTransition(triggerConfig.transitions, context);
if (transitionConfig) {
_this6.executeTransition(transitionConfig, context);
_this3.executeTransition(transitionConfig, context);
}
});
}
}, {
key: 'selectTransition',
value: function selectTransition(transitions, context) {
}], [{
key: 'getFirstAllowedTransition',
value: function getFirstAllowedTransition(transitions, context) {
for (var i = 0; i < transitions.length; i++) {

@@ -322,7 +316,2 @@ if (!transitions[i].condition || transitions[i].condition(context)) {

}
}], [{
key: 'start',
value: function start(config) {
return new StateMachine(config).start();
}
}]);

@@ -329,0 +318,0 @@

@@ -0,28 +1,10 @@

/* eslint-disable */
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.configure = configure;
exports.start = start;
var Finity = require('./Finity').default;
var merge = require('./utils/merge').default;
var _configuration = require('./configuration');
merge(exports, Finity);
var _StateMachine = require('./core/StateMachine');
var _StateMachine2 = _interopRequireDefault(_StateMachine);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function configure() {
return new _configuration.StateMachineConfigurator();
}
function start(config) {
return _StateMachine2.default.start(config);
}
exports.default = {
configure: configure,
start: start
};
// Allow the use of the default import syntax in TypeScript (import Finity from 'finity')
exports.default = Finity;
{
"name": "finity",
"version": "0.4.5",
"version": "0.5.0",
"description": "A finite state machine library for Node.js and the browser with a friendly configuration DSL",

@@ -31,33 +31,33 @@ "keywords": [

"main": "lib/index.js",
"typings": "index.d.ts",
"types": "index.d.ts",
"devDependencies": {
"babel-cli": "^6.9.0",
"babel-core": "^6.9.0",
"babel-register": "^6.9.0",
"babel-preset-es2015": "^6.9.0",
"babel-preset-stage-0": "^6.5.0",
"babel-loader": "^6.2.4",
"jasmine": "^2.4.1",
"jasmine-spec-reporter": "^2.4.0",
"eslint": "^2.10.2",
"eslint-config-airbnb": "^9.0.1",
"eslint-plugin-import": "^1.8.0",
"eslint-plugin-react": "^5.1.1",
"eslint-plugin-jsx-a11y": "^1.2.0",
"babel-eslint": "^6.0.4",
"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",
"eslint-plugin-import": "^2.2.0",
"eslint-plugin-react": "^6.8.0",
"eslint-plugin-jsx-a11y": "^2.2.3",
"babel-eslint": "^7.1.1",
"isparta": "^4.0.0",
"coveralls": "^2.11.9",
"rimraf": "^2.5.2",
"webpack": "^1.13.1",
"cross-env": "^1.0.8",
"typings": "^1.0.4",
"typescript": "^1.8.10"
"coveralls": "^2.11.15",
"rimraf": "^2.5.4",
"webpack": "^1.14.0",
"cross-env": "^3.1.4",
"typescript": "^2.1.4",
"shx": "^0.2.2"
},
"scripts": {
"clean": "rimraf lib && rimraf umd",
"build:commonjs": "babel src --out-dir lib",
"build:umd": "cross-env NODE_ENV=development webpack src/index.js umd/Finity.js",
"build:umd:min": "cross-env NODE_ENV=production webpack -p src/index.js umd/Finity.min.js",
"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",
"test:typings": "typings install && tsc",
"test:typings": "tsc",
"test": "node test/support/jasmineRunner.js && npm run test:typings",

@@ -64,0 +64,0 @@ "coverage": "babel-node node_modules/isparta/bin/isparta cover --root src --report lcovonly test/support/jasmineRunner.js",

@@ -8,3 +8,3 @@ # Roadmap

- [x] Promise triggers
- [ ] Hierarchical state machines
- [ ] Hierarchical state machines *(in progress)*
- [ ] Ignoring events

@@ -16,12 +16,16 @@ - [ ] Persistence support

```javascript
.state('state1')
.on('eventA').transitionTo('state2')
.submachine()
.rememberLastState()
.initialState('substate1')
.on('eventB').transitionTo('substate2')
.state('substate2')
.on('eventC').transitionTo('substate3')
.parent()
.state('state2')
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();
```

@@ -44,3 +48,3 @@

- [x] Entry, exit, and transition action parameters
- [ ] Project website
- [ ] Project website *(in progress)*
- [ ] Clarify that final states don't need to be explicitly defined

@@ -47,0 +51,0 @@ - [ ] Execution order of entry, exit, and transition actions and global hooks

@@ -57,36 +57,19 @@ (function webpackUniversalModuleDefinition(root, factory) {

/* eslint-disable */
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.configure = configure;
exports.start = start;
var Finity = __webpack_require__(9).default;
var merge = __webpack_require__(8).default;
var _configuration = __webpack_require__(14);
merge(exports, Finity);
var _StateMachine = __webpack_require__(10);
// Allow the use of the default import syntax in TypeScript (import Finity from 'finity')
exports.default = Finity;
var _StateMachine2 = _interopRequireDefault(_StateMachine);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function configure() {
return new _configuration.StateMachineConfigurator();
}
function start(config) {
return _StateMachine2.default.start(config);
}
exports.default = {
configure: configure,
start: start
};
/***/ },
/* 1 */
/***/ function(module, exports) {
/***/ function(module, exports, __webpack_require__) {
"use strict";
'use strict';

@@ -97,4 +80,12 @@ Object.defineProperty(exports, "__esModule", {

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 = __webpack_require__(17);
var _mapValues2 = _interopRequireDefault(_mapValues);
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"); } }

@@ -110,3 +101,3 @@

_createClass(BaseConfigurator, [{
key: "getAncestor",
key: 'getAncestor',
value: function getAncestor(type) {

@@ -118,2 +109,22 @@ if (this.parent) {

}
}, {
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);
}
}]);

@@ -136,77 +147,2 @@

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
exports.default = deepCopy;
var _mapValues = __webpack_require__(11);
var _mapValues2 = _interopRequireDefault(_mapValues);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function deepCopy(value) {
if (!value) {
return value;
}
if (Array.isArray(value)) {
return value.map(deepCopy);
}
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
return (0, _mapValues2.default)(value, deepCopy);
}
return value;
}
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.delegateToAncestor = delegateToAncestor;
exports.mapToConfig = mapToConfig;
var _mapValues = __webpack_require__(11);
var _mapValues2 = _interopRequireDefault(_mapValues);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function delegateToAncestor(type, ancestorType) {
Object.getOwnPropertyNames(ancestorType.prototype).filter(function (name) {
return !type.prototype[name] && ancestorType.prototype[name] instanceof Function && ancestorType.prototype[name] !== ancestorType.prototype.constructor;
}).forEach(function (name) {
type.prototype[name] = function () {
var method = ancestorType.prototype[name];
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return method.apply(this.getAncestor(ancestorType), args);
};
});
}
var internalGetConfig = function internalGetConfig(configurator) {
return configurator.internalGetConfig();
};
function mapToConfig(configurators) {
return Array.isArray(configurators) ? configurators.map(internalGetConfig) : (0, _mapValues2.default)(configurators, internalGetConfig);
}
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
'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; }; }();

@@ -218,8 +154,6 @@

var _TransitionConfigurator = __webpack_require__(9);
var _TransitionConfigurator = __webpack_require__(6);
var _TransitionConfigurator2 = _interopRequireDefault(_TransitionConfigurator);
var _ConfiguratorHelper = __webpack_require__(3);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -239,5 +173,7 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TriggerConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (TriggerConfigurator.__proto__ || Object.getPrototypeOf(TriggerConfigurator)).call(this, parent));
_this.transitionConfigurators = [];
_this.config = {
transitions: []
};
return _this;

@@ -265,12 +201,5 @@ }

var transitionConfigurator = new _TransitionConfigurator2.default(this, targetState, isInternal);
this.transitionConfigurators.push(transitionConfigurator);
this.config.transitions.push(transitionConfigurator);
return transitionConfigurator;
}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
return {
transitions: (0, _ConfiguratorHelper.mapToConfig)(this.transitionConfigurators)
};
}
}]);

@@ -284,3 +213,3 @@

/***/ },
/* 5 */
/* 3 */
/***/ function(module, exports, __webpack_require__) {

@@ -300,10 +229,6 @@

var _TriggerConfigurator = __webpack_require__(4);
var _TriggerConfigurator = __webpack_require__(2);
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator);
var _deepCopy = __webpack_require__(2);
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -323,9 +248,9 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(AsyncActionConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (AsyncActionConfigurator.__proto__ || Object.getPrototypeOf(AsyncActionConfigurator)).call(this, parent));
_this.config = {
action: action
action: action,
successTrigger: new _TriggerConfigurator2.default(_this),
failureTrigger: new _TriggerConfigurator2.default(_this)
};
_this.successConfigurator = new _TriggerConfigurator2.default(_this);
_this.failureConfigurator = new _TriggerConfigurator2.default(_this);
return _this;

@@ -337,3 +262,3 @@ }

value: function onSuccess() {
return this.successConfigurator;
return this.config.successTrigger;
}

@@ -343,12 +268,4 @@ }, {

value: function onFailure() {
return this.failureConfigurator;
return this.config.failureTrigger;
}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
var config = (0, _deepCopy2.default)(this.config);
config.onSuccess = this.successConfigurator.internalGetConfig();
config.onFailure = this.failureConfigurator.internalGetConfig();
return config;
}
}]);

@@ -362,3 +279,3 @@

/***/ },
/* 6 */
/* 4 */
/***/ function(module, exports, __webpack_require__) {

@@ -378,6 +295,2 @@

var _deepCopy = __webpack_require__(2);
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -397,3 +310,3 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(GlobalConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (GlobalConfigurator.__proto__ || Object.getPrototypeOf(GlobalConfigurator)).call(this, parent));

@@ -440,7 +353,2 @@ _this.config = {

}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
return (0, _deepCopy2.default)(this.config);
}
}]);

@@ -454,3 +362,3 @@

/***/ },
/* 7 */
/* 5 */
/***/ function(module, exports, __webpack_require__) {

@@ -470,20 +378,14 @@

var _TriggerConfigurator = __webpack_require__(4);
var _TriggerConfigurator = __webpack_require__(2);
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator);
var _TimerConfigurator = __webpack_require__(13);
var _TimerConfigurator = __webpack_require__(11);
var _TimerConfigurator2 = _interopRequireDefault(_TimerConfigurator);
var _AsyncActionConfigurator = __webpack_require__(5);
var _AsyncActionConfigurator = __webpack_require__(3);
var _AsyncActionConfigurator2 = _interopRequireDefault(_AsyncActionConfigurator);
var _ConfiguratorHelper = __webpack_require__(3);
var _deepCopy = __webpack_require__(2);
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -503,11 +405,12 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(StateConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (StateConfigurator.__proto__ || Object.getPrototypeOf(StateConfigurator)).call(this, parent));
_this.config = {
entryActions: [],
exitActions: []
exitActions: [],
events: Object.create(null),
timers: [],
asyncActions: [],
submachine: null
};
_this.eventConfigurators = Object.create(null);
_this.timerConfigurators = [];
_this.asyncActionConfigurators = [];
return _this;

@@ -531,6 +434,6 @@ }

value: function on(event) {
if (!this.eventConfigurators[event]) {
this.eventConfigurators[event] = new _TriggerConfigurator2.default(this);
if (!this.config.events[event]) {
this.config.events[event] = new _TriggerConfigurator2.default(this);
}
return this.eventConfigurators[event];
return this.config.events[event];
}

@@ -541,3 +444,3 @@ }, {

var timerConfigurator = new _TimerConfigurator2.default(this, timeout);
this.timerConfigurators.push(timerConfigurator);
this.config.timers.push(timerConfigurator);
return timerConfigurator;

@@ -549,13 +452,10 @@ }

var asyncActionConfigurator = new _AsyncActionConfigurator2.default(this, asyncAction);
this.asyncActionConfigurators.push(asyncActionConfigurator);
this.config.asyncActions.push(asyncActionConfigurator);
return asyncActionConfigurator;
}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
var config = (0, _deepCopy2.default)(this.config);
config.events = (0, _ConfiguratorHelper.mapToConfig)(this.eventConfigurators);
config.timers = (0, _ConfiguratorHelper.mapToConfig)(this.timerConfigurators);
config.asyncActions = (0, _ConfiguratorHelper.mapToConfig)(this.asyncActionConfigurators);
return config;
key: 'submachine',
value: function submachine(submachineConfig) {
this.config.submachine = submachineConfig;
return this;
}

@@ -570,3 +470,3 @@ }]);

/***/ },
/* 8 */
/* 6 */
/***/ function(module, exports, __webpack_require__) {

@@ -586,26 +486,242 @@

var _GlobalConfigurator = __webpack_require__(6);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator);
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _StateConfigurator = __webpack_require__(7);
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 _StateConfigurator2 = _interopRequireDefault(_StateConfigurator);
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 _StateMachine = __webpack_require__(10);
var TransitionConfigurator = function (_BaseConfigurator) {
_inherits(TransitionConfigurator, _BaseConfigurator);
function TransitionConfigurator(parent, targetState, isInternal) {
_classCallCheck(this, TransitionConfigurator);
var _this = _possibleConstructorReturn(this, (TransitionConfigurator.__proto__ || Object.getPrototypeOf(TransitionConfigurator)).call(this, parent));
_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;
}
}]);
return TransitionConfigurator;
}(_BaseConfigurator3.default);
exports.default = TransitionConfigurator;
/***/ },
/* 7 */
/***/ function(module, exports, __webpack_require__) {
'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; }; }();
var _StateMachine = __webpack_require__(14);
var _StateMachine2 = _interopRequireDefault(_StateMachine);
var _ConfiguratorHelper = __webpack_require__(3);
var _TaskScheduler = __webpack_require__(15);
var _deepCopy = __webpack_require__(2);
var _TaskScheduler2 = _interopRequireDefault(_TaskScheduler);
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _merge = __webpack_require__(12);
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _merge2 = _interopRequireDefault(_merge);
var HierarchicalStateMachine = function () {
function HierarchicalStateMachine(rootStateMachine, currentStateMachine, taskScheduler) {
_classCallCheck(this, HierarchicalStateMachine);
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;
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);
}
}]);
return HierarchicalStateMachine;
}();
exports.default = HierarchicalStateMachine;
/***/ },
/* 8 */
/***/ function(module, exports) {
"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;
}
/***/ },
/* 9 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _configuration = __webpack_require__(13);
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;
/***/ },
/* 10 */
/***/ function(module, exports, __webpack_require__) {
'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; }; }();
var _BaseConfigurator2 = __webpack_require__(1);
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2);
var _GlobalConfigurator = __webpack_require__(4);
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator);
var _StateConfigurator = __webpack_require__(5);
var _StateConfigurator2 = _interopRequireDefault(_StateConfigurator);
var _HierarchicalStateMachine = __webpack_require__(7);
var _HierarchicalStateMachine2 = _interopRequireDefault(_HierarchicalStateMachine);
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"); } }

@@ -623,9 +739,9 @@

var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(StateMachineConfigurator).call(this));
var _this = _possibleConstructorReturn(this, (StateMachineConfigurator.__proto__ || Object.getPrototypeOf(StateMachineConfigurator)).call(this));
_this.config = {
initialState: null
global: new _GlobalConfigurator2.default(_this),
initialState: null,
states: Object.create(null)
};
_this.globalConfigurator = new _GlobalConfigurator2.default(_this);
_this.stateConfigurators = Object.create(null);
return _this;

@@ -637,3 +753,3 @@ }

value: function global() {
return this.globalConfigurator;
return this.config.global;
}

@@ -649,6 +765,6 @@ }, {

value: function state(_state) {
if (!this.stateConfigurators[_state]) {
this.stateConfigurators[_state] = new _StateConfigurator2.default(this);
if (!this.config.states[_state]) {
this.config.states[_state] = new _StateConfigurator2.default(this);
}
return this.stateConfigurators[_state];
return this.config.states[_state];
}

@@ -658,5 +774,3 @@ }, {

value: function getConfig() {
var config = (0, _deepCopy2.default)(this.config);
config.states = (0, _ConfiguratorHelper.mapToConfig)(this.stateConfigurators);
return (0, _merge2.default)(config, this.globalConfigurator.internalGetConfig());
return this.buildConfig();
}

@@ -667,3 +781,3 @@ }, {

var config = this.getConfig();
return _StateMachine2.default.start(config);
return _HierarchicalStateMachine2.default.start(config);
}

@@ -678,3 +792,3 @@ }]);

/***/ },
/* 9 */
/* 11 */
/***/ function(module, exports, __webpack_require__) {

@@ -688,12 +802,6 @@

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 _TriggerConfigurator2 = __webpack_require__(2);
var _BaseConfigurator2 = __webpack_require__(1);
var _TriggerConfigurator3 = _interopRequireDefault(_TriggerConfigurator2);
var _BaseConfigurator3 = _interopRequireDefault(_BaseConfigurator2);
var _deepCopy = __webpack_require__(2);
var _deepCopy2 = _interopRequireDefault(_deepCopy);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

@@ -707,45 +815,50 @@

var TransitionConfigurator = function (_BaseConfigurator) {
_inherits(TransitionConfigurator, _BaseConfigurator);
var TrimerConfigurator = function (_TriggerConfigurator) {
_inherits(TrimerConfigurator, _TriggerConfigurator);
function TransitionConfigurator(parent, targetState, isInternal) {
_classCallCheck(this, TransitionConfigurator);
function TrimerConfigurator(parent, timeout) {
_classCallCheck(this, TrimerConfigurator);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TransitionConfigurator).call(this, parent));
var _this = _possibleConstructorReturn(this, (TrimerConfigurator.__proto__ || Object.getPrototypeOf(TrimerConfigurator)).call(this, parent));
_this.config = {
targetState: targetState,
isInternal: targetState === null && isInternal,
actions: [],
condition: null
};
_this.config.timeout = timeout;
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;
}
}, {
key: 'internalGetConfig',
value: function internalGetConfig() {
return (0, _deepCopy2.default)(this.config);
}
}]);
return TrimerConfigurator;
}(_TriggerConfigurator3.default);
return TransitionConfigurator;
}(_BaseConfigurator3.default);
exports.default = TrimerConfigurator;
exports.default = TransitionConfigurator;
/***/ },
/* 12 */
/***/ function(module, exports) {
"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];
}
return method.apply(this.getAncestor(ancestorConstructor), args);
};
});
}
/***/ },
/* 10 */
/* 13 */
/***/ function(module, exports, __webpack_require__) {

@@ -758,14 +871,63 @@

});
exports.StateMachineConfigurator = undefined;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _StateMachineConfigurator = __webpack_require__(10);
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__(2);
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__(12);
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);
/***/ },
/* 14 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: 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 _AsyncActionSubscription = __webpack_require__(16);
var _invokeEach = __webpack_require__(16);
var _AsyncActionSubscription2 = _interopRequireDefault(_AsyncActionSubscription);
var _invokeEach2 = _interopRequireDefault(_invokeEach);
var _executeHandlers = __webpack_require__(17);
var _merge = __webpack_require__(8);
var _executeHandlers2 = _interopRequireDefault(_executeHandlers);
var _merge2 = _interopRequireDefault(_merge);

@@ -776,4 +938,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var noop = function noop() {};
var StateMachine = function () {
function StateMachine(config) {
function StateMachine(config, taskScheduler, contextFactory) {
_classCallCheck(this, StateMachine);

@@ -791,7 +955,9 @@

this.config = config;
this.taskScheduler = taskScheduler;
this.contextFactory = contextFactory;
this.currentState = null;
this.isBusy = false;
this.eventQueue = [];
this.submachines = Object.create(null);
this.timerIDs = null;
this.asyncActionSubscriptions = null;
this.asyncActionCancelers = null;
this.handleAsyncActionComplete = this.handleAsyncActionComplete.bind(this);
this.handleTimeout = this.handleTimeout.bind(this);

@@ -801,19 +967,2 @@ }

_createClass(StateMachine, [{
key: 'start',
value: function start() {
var _this = this;
if (!this.isStarted()) {
this.execute(function () {
return _this.enterState(_this.config.initialState, _this.createContext());
});
}
return this;
}
}, {
key: 'isStarted',
value: function isStarted() {
return this.currentState !== null;
}
}, {
key: 'getCurrentState',

@@ -829,81 +978,56 @@ value: function getCurrentState() {

}
var context = this.createContext(event, eventPayload);
return !!this.getTransitionForEvent(context);
}
}, {
key: 'handle',
value: function handle(event, eventPayload) {
var _this2 = this;
if (!this.isBusy) {
this.execute(function () {
return _this2.processEvent(event, eventPayload);
});
} else {
this.eventQueue.push({ event: event, eventPayload: eventPayload });
}
return this;
var context = this.createContextWithEvent(event, eventPayload);
return !!this.getFirstAllowedTransitionForEvent(context);
}
}, {
key: 'execute',
value: function execute(operation) {
if (this.isBusy) {
throw new Error('Operation cannot be executed because another operation is in progress.');
key: 'tryHandle',
value: function tryHandle(event, eventPayload) {
if (!this.isStarted()) {
return false;
}
this.isBusy = true;
try {
operation();
// Process all events
while (this.eventQueue.length > 0) {
var _eventQueue$shift = this.eventQueue.shift();
var event = _eventQueue$shift.event;
var eventPayload = _eventQueue$shift.eventPayload;
this.processEvent(event, eventPayload);
}
} finally {
// Clean up
if (this.eventQueue.length > 0) {
this.eventQueue = [];
}
this.isBusy = false;
var context = this.createContextWithEvent(event, eventPayload);
var transitionConfig = this.getFirstAllowedTransitionForEvent(context);
if (transitionConfig) {
this.executeTransition(transitionConfig, context);
return true;
}
return false;
}
}, {
key: 'processEvent',
value: function processEvent(event, eventPayload) {
var context = this.createContext(event, eventPayload);
var transitionConfig = this.getTransitionForEvent(context);
if (transitionConfig) {
this.executeTransition(transitionConfig, context);
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 {
this.handleUnhandledEvent(context);
throw new Error('Unhandled event \'' + event + '\' in state \'' + this.currentState + '\'.');
}
}
}, {
key: 'createContext',
value: function createContext(event, eventPayload) {
var context = { stateMachine: this };
if (event !== undefined) {
context.event = event;
key: 'isStarted',
value: function isStarted() {
return this.currentState !== null;
}
}, {
key: 'start',
value: function start() {
if (!this.isStarted()) {
this.enterState(this.config.initialState, this.createContext());
}
if (eventPayload !== undefined) {
context.eventPayload = eventPayload;
}
return context;
}
}, {
key: 'getTransitionForEvent',
value: function getTransitionForEvent(context) {
var stateConfig = this.config.states[this.currentState];
if (!stateConfig) {
return null;
key: 'stop',
value: function stop() {
if (this.isStarted()) {
this.exitState(this.createContext());
this.currentState = null;
}
var eventConfig = stateConfig.events[context.event];
return eventConfig ? this.selectTransition(eventConfig.transitions, context) : null;
}
}, {
key: 'getSubmachine',
value: function getSubmachine() {
return this.isStarted() ? this.submachines[this.currentState] : null;
}
}, {
key: 'executeTransition',

@@ -917,4 +1041,4 @@ value: function executeTransition(transitionConfig, context) {

(0, _executeHandlers2.default)(this.config.transitionHooks, this.currentState, nextState, context);
(0, _executeHandlers2.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);

@@ -926,29 +1050,26 @@ if (!transitionConfig.isInternal) {

}, {
key: 'handleUnhandledEvent',
value: function handleUnhandledEvent(context) {
if (this.config.unhandledEventHooks.length > 0) {
(0, _executeHandlers2.default)(this.config.unhandledEventHooks, context.event, this.currentState, context);
} else {
throw new Error('Unhandled event \'' + context.event + '\' in state \'' + this.currentState + '\'.');
}
}
}, {
key: 'enterState',
value: function enterState(state, context) {
(0, _executeHandlers2.default)(this.config.stateEnterHooks, state, context);
(0, _invokeEach2.default)(this.config.global.stateEnterHooks, state, context);
var stateConfig = this.config.states[state];
if (stateConfig) {
(0, _executeHandlers2.default)(stateConfig.entryActions, state, context);
(0, _invokeEach2.default)(stateConfig.entryActions, state, context);
}
if (this.currentState !== null && this.currentState !== state) {
(0, _executeHandlers2.default)(this.config.stateChangeHooks, this.currentState, state, context);
(0, _invokeEach2.default)(this.config.global.stateChangeHooks, this.currentState, state, context);
}
try {
this.startAsyncActions(state, context);
this.startTimers(state);
this.startSubmachines(state);
} catch (error) {
this.stopTimers();
this.cancelAsyncActions();
throw error;
}
this.currentState = state;
this.startAsyncActions(context);
this.startTimers();
}

@@ -958,11 +1079,11 @@ }, {

value: function exitState(context) {
this.stopSubmachines();
this.stopTimers();
this.cancelAsyncActions();
this.cancelAsyncActionSubscriptions();
(0, _invokeEach2.default)(this.config.global.stateExitHooks, this.currentState, context);
(0, _executeHandlers2.default)(this.config.stateExitHooks, this.currentState, context);
var stateConfig = this.config.states[this.currentState];
if (stateConfig) {
(0, _executeHandlers2.default)(stateConfig.exitActions, this.currentState, context);
(0, _invokeEach2.default)(stateConfig.exitActions, this.currentState, context);
}

@@ -972,9 +1093,9 @@ }

key: 'startAsyncActions',
value: function startAsyncActions(context) {
var _this3 = this;
value: function startAsyncActions(state, context) {
var _this = this;
var stateConfig = this.config.states[this.currentState];
var stateConfig = this.config.states[state];
if (stateConfig) {
stateConfig.asyncActions.forEach(function (asyncActionConfig) {
return _this3.startAsyncAction(asyncActionConfig, context);
return _this.startAsyncAction(asyncActionConfig, state, context);
});

@@ -985,51 +1106,41 @@ }

key: 'startAsyncAction',
value: function startAsyncAction(asyncActionConfig, context) {
var _this4 = this;
value: function startAsyncAction(asyncActionConfig, state, context) {
var action = asyncActionConfig.action,
successTrigger = asyncActionConfig.successTrigger,
failureTrigger = asyncActionConfig.failureTrigger;
var action = asyncActionConfig.action;
var subscription = new _AsyncActionSubscription2.default(function (result) {
return _this4.handleAsyncActionSuccess(asyncActionConfig.onSuccess, result);
var handleComplete = this.handleAsyncActionComplete;
action(state, context).then(function (result) {
return handleComplete(successTrigger, { result: result });
}, function (error) {
return _this4.handleAsyncActionFailure(asyncActionConfig.onFailure, error);
return handleComplete(failureTrigger, { error: error });
});
action(this.currentState, context).then(subscription.onSuccess, subscription.onFailure);
this.asyncActionSubscriptions = this.asyncActionSubscriptions || [];
this.asyncActionSubscriptions.push(subscription);
this.asyncActionCancelers = this.asyncActionCancelers || [];
this.asyncActionCancelers.push(function () {
handleComplete = noop;
});
}
}, {
key: 'cancelAsyncActionSubscriptions',
value: function cancelAsyncActionSubscriptions() {
if (this.asyncActionSubscriptions) {
this.asyncActionSubscriptions.forEach(function (subscription) {
return subscription.cancel();
});
this.asyncActionSubscriptions = null;
key: 'cancelAsyncActions',
value: function cancelAsyncActions() {
if (this.asyncActionCancelers) {
(0, _invokeEach2.default)(this.asyncActionCancelers);
this.asyncActionCancelers = null;
}
}
}, {
key: 'handleAsyncActionSuccess',
value: function handleAsyncActionSuccess(triggerConfig, result) {
var context = this.createContext();
context.result = result;
key: 'handleAsyncActionComplete',
value: function handleAsyncActionComplete(triggerConfig, additionalContext) {
var context = (0, _merge2.default)(this.createContext(), additionalContext);
this.executeTrigger(triggerConfig, context);
}
}, {
key: 'handleAsyncActionFailure',
value: function handleAsyncActionFailure(triggerConfig, error) {
var context = this.createContext();
context.error = error;
this.executeTrigger(triggerConfig, context);
}
}, {
key: 'startTimers',
value: function startTimers() {
var _this5 = this;
value: function startTimers(state) {
var _this2 = this;
var stateConfig = this.config.states[this.currentState];
var stateConfig = this.config.states[state];
if (stateConfig && stateConfig.timers.length > 0) {
this.timerIDs = stateConfig.timers.map(function (timerConfig) {
return setTimeout(_this5.handleTimeout, timerConfig.timeout, timerConfig);
return setTimeout(_this2.handleTimeout, timerConfig.timeout, timerConfig);
});

@@ -1052,16 +1163,61 @@ }

}, {
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;
}
var eventConfig = stateConfig.events[context.event];
return eventConfig ? StateMachine.getFirstAllowedTransition(eventConfig.transitions, context) : null;
}
}, {
key: 'executeTrigger',
value: function executeTrigger(triggerConfig, context) {
var _this6 = this;
var _this3 = this;
this.execute(function () {
var transitionConfig = _this6.selectTransition(triggerConfig.transitions, context);
this.taskScheduler.execute(function () {
var transitionConfig = StateMachine.getFirstAllowedTransition(triggerConfig.transitions, context);
if (transitionConfig) {
_this6.executeTransition(transitionConfig, context);
_this3.executeTransition(transitionConfig, context);
}
});
}
}, {
key: 'selectTransition',
value: function selectTransition(transitions, context) {
}], [{
key: 'getFirstAllowedTransition',
value: function getFirstAllowedTransition(transitions, context) {
for (var i = 0; i < transitions.length; i++) {

@@ -1074,7 +1230,2 @@ if (!transitions[i].condition || transitions[i].condition(context)) {

}
}], [{
key: 'start',
value: function start(config) {
return new StateMachine(config).start();
}
}]);

@@ -1088,41 +1239,5 @@

/***/ },
/* 11 */
/* 15 */
/***/ function(module, exports) {
"use strict";
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;
}
/***/ },
/* 12 */
/***/ function(module, exports) {
"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;
}
/***/ },
/* 13 */
/***/ function(module, exports, __webpack_require__) {
'use strict';

@@ -1136,120 +1251,50 @@

var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _TriggerConfigurator2 = __webpack_require__(4);
var _TriggerConfigurator3 = _interopRequireDefault(_TriggerConfigurator2);
var _merge = __webpack_require__(12);
var _merge2 = _interopRequireDefault(_merge);
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 _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 TaskScheduler = function () {
function TaskScheduler() {
_classCallCheck(this, TaskScheduler);
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 TrimerConfigurator = function (_TriggerConfigurator) {
_inherits(TrimerConfigurator, _TriggerConfigurator);
function TrimerConfigurator(parent, timeout) {
_classCallCheck(this, TrimerConfigurator);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(TrimerConfigurator).call(this, parent));
_this.config = {
timeout: timeout
};
return _this;
this.queue = [];
this.isBusy = false;
}
_createClass(TrimerConfigurator, [{
key: 'internalGetConfig',
value: function internalGetConfig() {
return (0, _merge2.default)(_get(Object.getPrototypeOf(TrimerConfigurator.prototype), 'internalGetConfig', this).call(this), this.config);
_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;
}
}
}]);
return TrimerConfigurator;
}(_TriggerConfigurator3.default);
return TaskScheduler;
}();
exports.default = TrimerConfigurator;
exports.default = TaskScheduler;
/***/ },
/* 14 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.StateMachineConfigurator = undefined;
var _StateMachineConfigurator2 = __webpack_require__(8);
var _StateMachineConfigurator3 = _interopRequireDefault(_StateMachineConfigurator2);
var _setUpDelegation = __webpack_require__(15);
var _setUpDelegation2 = _interopRequireDefault(_setUpDelegation);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.StateMachineConfigurator = _StateMachineConfigurator3.default;
(0, _setUpDelegation2.default)();
/***/ },
/* 15 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = setUpDelegation;
var _StateMachineConfigurator = __webpack_require__(8);
var _StateMachineConfigurator2 = _interopRequireDefault(_StateMachineConfigurator);
var _GlobalConfigurator = __webpack_require__(6);
var _GlobalConfigurator2 = _interopRequireDefault(_GlobalConfigurator);
var _StateConfigurator = __webpack_require__(7);
var _StateConfigurator2 = _interopRequireDefault(_StateConfigurator);
var _TriggerConfigurator = __webpack_require__(4);
var _TriggerConfigurator2 = _interopRequireDefault(_TriggerConfigurator);
var _TransitionConfigurator = __webpack_require__(9);
var _TransitionConfigurator2 = _interopRequireDefault(_TransitionConfigurator);
var _AsyncActionConfigurator = __webpack_require__(5);
var _AsyncActionConfigurator2 = _interopRequireDefault(_AsyncActionConfigurator);
var _ConfiguratorHelper = __webpack_require__(3);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function setUpDelegation() {
(0, _ConfiguratorHelper.delegateToAncestor)(_GlobalConfigurator2.default, _StateMachineConfigurator2.default);
(0, _ConfiguratorHelper.delegateToAncestor)(_StateConfigurator2.default, _StateMachineConfigurator2.default);
(0, _ConfiguratorHelper.delegateToAncestor)(_TransitionConfigurator2.default, _StateConfigurator2.default);
(0, _ConfiguratorHelper.delegateToAncestor)(_TransitionConfigurator2.default, _TriggerConfigurator2.default);
(0, _ConfiguratorHelper.delegateToAncestor)(_TransitionConfigurator2.default, _AsyncActionConfigurator2.default);
}
/***/ },
/* 16 */

@@ -1263,44 +1308,13 @@ /***/ function(module, exports) {

});
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"); } }
var AsyncActionSubscription = function () {
function AsyncActionSubscription(successListener, failureListener) {
_classCallCheck(this, AsyncActionSubscription);
this.successListener = successListener;
this.failureListener = failureListener;
this.onSuccess = this.onSuccess.bind(this);
this.onFailure = this.onFailure.bind(this);
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];
}
_createClass(AsyncActionSubscription, [{
key: "onSuccess",
value: function onSuccess(result) {
if (this.successListener) {
this.successListener(result);
}
}
}, {
key: "onFailure",
value: function onFailure(error) {
if (this.failureListener) {
this.failureListener(error);
}
}
}, {
key: "cancel",
value: function cancel() {
this.successListener = null;
this.failureListener = null;
}
}]);
fns.forEach(function (fn) {
return fn.apply(undefined, args);
});
}
return AsyncActionSubscription;
}();
exports.default = AsyncActionSubscription;
/***/ },

@@ -1315,11 +1329,10 @@ /* 17 */

});
exports.default = executeHandlers;
function executeHandlers(handlers) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
handlers.forEach(function (handler) {
return handler.apply(undefined, args);
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;
}

@@ -1326,0 +1339,0 @@

@@ -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 o=n[r]={exports:{},id:r,loaded:!1};return t[r].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(){return new u.StateMachineConfigurator}function i(t){return s["default"].start(t)}Object.defineProperty(e,"__esModule",{value:!0}),e.configure=o,e.start=i;var u=n(14),a=n(10),s=r(a);e["default"]={configure:o,start:i}},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}}(),o=function(){function t(e){n(this,t),this.parent=e}return r(t,[{key:"getAncestor",value:function(t){return this.parent?this.parent instanceof t?this.parent:this.parent.getAncestor(t):null}}]),t}();e["default"]=o},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(t){return t?Array.isArray(t)?t.map(o):"object"===("undefined"==typeof t?"undefined":i(t))?(0,a["default"])(t,o):t:t}Object.defineProperty(e,"__esModule",{value:!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?"symbol":typeof t};e["default"]=o;var u=n(11),a=r(u)},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(t,e){Object.getOwnPropertyNames(e.prototype).filter(function(n){return!t.prototype[n]&&e.prototype[n]instanceof Function&&e.prototype[n]!==e.prototype.constructor}).forEach(function(n){t.prototype[n]=function(){for(var t=e.prototype[n],r=arguments.length,o=Array(r),i=0;r>i;i++)o[i]=arguments[i];return t.apply(this.getAncestor(e),o)}})}function i(t){return Array.isArray(t)?t.map(s):(0,a["default"])(t,s)}Object.defineProperty(e,"__esModule",{value:!0}),e.delegateToAncestor=o,e.mapToConfig=i;var u=n(11),a=r(u),s=function(t){return t.internalGetConfig()}},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(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)}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}}(),s=n(1),c=r(s),f=n(9),l=r(f),h=n(3),y=function(t){function e(t){o(this,e);var n=i(this,Object.getPrototypeOf(e).call(this,t));return n.transitionConfigurators=[],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.transitionConfigurators.push(n),n}},{key:"internalGetConfig",value:function(){return{transitions:(0,h.mapToConfig)(this.transitionConfigurators)}}}]),e}(c["default"]);e["default"]=y},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(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)}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}}(),s=n(1),c=r(s),f=n(4),l=r(f),h=n(2),y=r(h),p=function(t){function e(t,n){o(this,e);var r=i(this,Object.getPrototypeOf(e).call(this,t));return r.config={action:n},r.successConfigurator=new l["default"](r),r.failureConfigurator=new l["default"](r),r}return u(e,t),a(e,[{key:"onSuccess",value:function(){return this.successConfigurator}},{key:"onFailure",value:function(){return this.failureConfigurator}},{key:"internalGetConfig",value:function(){var t=(0,y["default"])(this.config);return t.onSuccess=this.successConfigurator.internalGetConfig(),t.onFailure=this.failureConfigurator.internalGetConfig(),t}}]),e}(c["default"]);e["default"]=p},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(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)}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}}(),s=n(1),c=r(s),f=n(2),l=r(f),h=function(t){function e(t){o(this,e);var n=i(this,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}},{key:"internalGetConfig",value:function(){return(0,l["default"])(this.config)}}]),e}(c["default"]);e["default"]=h},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(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)}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}}(),s=n(1),c=r(s),f=n(4),l=r(f),h=n(13),y=r(h),p=n(5),d=r(p),v=n(3),b=n(2),g=r(b),O=function(t){function e(t){o(this,e);var n=i(this,Object.getPrototypeOf(e).call(this,t));return n.config={entryActions:[],exitActions:[]},n.eventConfigurators=Object.create(null),n.timerConfigurators=[],n.asyncActionConfigurators=[],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.eventConfigurators[t]||(this.eventConfigurators[t]=new l["default"](this)),this.eventConfigurators[t]}},{key:"onTimeout",value:function(t){var e=new y["default"](this,t);return this.timerConfigurators.push(e),e}},{key:"do",value:function(t){var e=new d["default"](this,t);return this.asyncActionConfigurators.push(e),e}},{key:"internalGetConfig",value:function(){var t=(0,g["default"])(this.config);return t.events=(0,v.mapToConfig)(this.eventConfigurators),t.timers=(0,v.mapToConfig)(this.timerConfigurators),t.asyncActions=(0,v.mapToConfig)(this.asyncActionConfigurators),t}}]),e}(c["default"]);e["default"]=O},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(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)}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}}(),s=n(1),c=r(s),f=n(6),l=r(f),h=n(7),y=r(h),p=n(10),d=r(p),v=n(3),b=n(2),g=r(b),O=n(12),w=r(O),_=function(t){function e(){o(this,e);var t=i(this,Object.getPrototypeOf(e).call(this));return t.config={initialState:null},t.globalConfigurator=new l["default"](t),t.stateConfigurators=Object.create(null),t}return u(e,t),a(e,[{key:"global",value:function(){return this.globalConfigurator}},{key:"initialState",value:function(t){return this.config.initialState=t,this.state(t)}},{key:"state",value:function(t){return this.stateConfigurators[t]||(this.stateConfigurators[t]=new y["default"](this)),this.stateConfigurators[t]}},{key:"getConfig",value:function(){var t=(0,g["default"])(this.config);return t.states=(0,v.mapToConfig)(this.stateConfigurators),(0,w["default"])(t,this.globalConfigurator.internalGetConfig())}},{key:"start",value:function(){var t=this.getConfig();return d["default"].start(t)}}]),e}(c["default"]);e["default"]=_},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(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)}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}}(),s=n(1),c=r(s),f=n(2),l=r(f),h=function(t){function e(t,n,r){o(this,e);var u=i(this,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}},{key:"internalGetConfig",value:function(){return(0,l["default"])(this.config)}}]),e}(c["default"]);e["default"]=h},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}Object.defineProperty(e,"__esModule",{value:!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?"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),s=r(a),c=n(17),f=r(c),l=function(){function t(e){if(o(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.currentState=null,this.isBusy=!1,this.eventQueue=[],this.timerIDs=null,this.asyncActionSubscriptions=null,this.handleTimeout=this.handleTimeout.bind(this)}return u(t,[{key:"start",value:function(){var t=this;return this.isStarted()||this.execute(function(){return t.enterState(t.config.initialState,t.createContext())}),this}},{key:"isStarted",value:function(){return null!==this.currentState}},{key:"getCurrentState",value:function(){return this.currentState}},{key:"canHandle",value:function(t,e){if(!this.isStarted())return!1;var n=this.createContext(t,e);return!!this.getTransitionForEvent(n)}},{key:"handle",value:function(t,e){var n=this;return this.isBusy?this.eventQueue.push({event:t,eventPayload:e}):this.execute(function(){return n.processEvent(t,e)}),this}},{key:"execute",value:function(t){if(this.isBusy)throw new Error("Operation cannot be executed because another operation is in progress.");this.isBusy=!0;try{for(t();this.eventQueue.length>0;){var e=this.eventQueue.shift(),n=e.event,r=e.eventPayload;this.processEvent(n,r)}}finally{this.eventQueue.length>0&&(this.eventQueue=[]),this.isBusy=!1}}},{key:"processEvent",value:function(t,e){var n=this.createContext(t,e),r=this.getTransitionForEvent(n);r?this.executeTransition(r,n):this.handleUnhandledEvent(n)}},{key:"createContext",value:function(t,e){var n={stateMachine:this};return void 0!==t&&(n.event=t),void 0!==e&&(n.eventPayload=e),n}},{key:"getTransitionForEvent",value:function(t){var e=this.config.states[this.currentState];if(!e)return null;var n=e.events[t.event];return n?this.selectTransition(n.transitions,t):null}},{key:"executeTransition",value:function(t,e){t.isInternal||this.exitState(e);var n=null!==t.targetState?t.targetState:this.currentState;(0,f["default"])(this.config.transitionHooks,this.currentState,n,e),(0,f["default"])(t.actions,this.currentState,n,e),t.isInternal||this.enterState(n,e)}},{key:"handleUnhandledEvent",value:function(t){if(!(this.config.unhandledEventHooks.length>0))throw new Error("Unhandled event '"+t.event+"' in state '"+this.currentState+"'.");(0,f["default"])(this.config.unhandledEventHooks,t.event,this.currentState,t)}},{key:"enterState",value:function(t,e){(0,f["default"])(this.config.stateEnterHooks,t,e);var n=this.config.states[t];n&&(0,f["default"])(n.entryActions,t,e),null!==this.currentState&&this.currentState!==t&&(0,f["default"])(this.config.stateChangeHooks,this.currentState,t,e),this.currentState=t,this.startAsyncActions(e),this.startTimers()}},{key:"exitState",value:function(t){this.stopTimers(),this.cancelAsyncActionSubscriptions(),(0,f["default"])(this.config.stateExitHooks,this.currentState,t);var e=this.config.states[this.currentState];e&&(0,f["default"])(e.exitActions,this.currentState,t)}},{key:"startAsyncActions",value:function(t){var e=this,n=this.config.states[this.currentState];n&&n.asyncActions.forEach(function(n){return e.startAsyncAction(n,t)})}},{key:"startAsyncAction",value:function(t,e){var n=this,r=t.action,o=new s["default"](function(e){return n.handleAsyncActionSuccess(t.onSuccess,e)},function(e){return n.handleAsyncActionFailure(t.onFailure,e)});r(this.currentState,e).then(o.onSuccess,o.onFailure),this.asyncActionSubscriptions=this.asyncActionSubscriptions||[],this.asyncActionSubscriptions.push(o)}},{key:"cancelAsyncActionSubscriptions",value:function(){this.asyncActionSubscriptions&&(this.asyncActionSubscriptions.forEach(function(t){return t.cancel()}),this.asyncActionSubscriptions=null)}},{key:"handleAsyncActionSuccess",value:function(t,e){var n=this.createContext();n.result=e,this.executeTrigger(t,n)}},{key:"handleAsyncActionFailure",value:function(t,e){var n=this.createContext();n.error=e,this.executeTrigger(t,n)}},{key:"startTimers",value:function(){var t=this,e=this.config.states[this.currentState];e&&e.timers.length>0&&(this.timerIDs=e.timers.map(function(e){return setTimeout(t.handleTimeout,e.timeout,e)}))}},{key:"stopTimers",value:function(){this.timerIDs&&(this.timerIDs.forEach(clearTimeout),this.timerIDs=null)}},{key:"handleTimeout",value:function(t){this.executeTrigger(t,this.createContext())}},{key:"executeTrigger",value:function(t,e){var n=this;this.execute(function(){var r=n.selectTransition(t.transitions,e);r&&n.executeTransition(r,e)})}},{key:"selectTransition",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}}],[{key:"start",value:function(e){return new t(e).start()}}]),t}();e["default"]=l},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){"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}}function o(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)}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}}(),s=function p(t,e,n){null===t&&(t=Function.prototype);var r=Object.getOwnPropertyDescriptor(t,e);if(void 0===r){var o=Object.getPrototypeOf(t);return null===o?void 0:p(o,e,n)}if("value"in r)return r.value;var i=r.get;if(void 0!==i)return i.call(n)},c=n(4),f=r(c),l=n(12),h=r(l),y=function(t){function e(t,n){o(this,e);var r=i(this,Object.getPrototypeOf(e).call(this,t));return r.config={timeout:n},r}return u(e,t),a(e,[{key:"internalGetConfig",value:function(){return(0,h["default"])(s(Object.getPrototypeOf(e.prototype),"internalGetConfig",this).call(this),this.config)}}]),e}(f["default"]);e["default"]=y},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 o=n(8),i=r(o),u=n(15),a=r(u);e.StateMachineConfigurator=i["default"],(0,a["default"])()},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function o(){(0,b.delegateToAncestor)(s["default"],u["default"]),(0,b.delegateToAncestor)(f["default"],u["default"]),(0,b.delegateToAncestor)(p["default"],f["default"]),(0,b.delegateToAncestor)(p["default"],h["default"]),(0,b.delegateToAncestor)(p["default"],v["default"])}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o;var i=n(8),u=r(i),a=n(6),s=r(a),c=n(7),f=r(c),l=n(4),h=r(l),y=n(9),p=r(y),d=n(5),v=r(d),b=n(3)},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}}(),o=function(){function t(e,r){n(this,t),this.successListener=e,this.failureListener=r,this.onSuccess=this.onSuccess.bind(this),this.onFailure=this.onFailure.bind(this)}return r(t,[{key:"onSuccess",value:function(t){this.successListener&&this.successListener(t)}},{key:"onFailure",value:function(t){this.failureListener&&this.failureListener(t)}},{key:"cancel",value:function(){this.successListener=null,this.failureListener=null}}]),t}();e["default"]=o},function(t,e){"use strict";function n(t){for(var e=arguments.length,n=Array(e>1?e-1:0),r=1;e>r;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){"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}])});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc