node-state-machine
Version:
State machine for NodeJS.
157 lines (131 loc) • 5.38 kB
JavaScript
;
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 _Transition = require('./Transition');
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Class representing a list of possible transitions that can be triggered
* by starting at some state. It holds a list of transitions with each its own
* trigger, but also a default transition in case no other transition is fired.
* @author Jensen Bernard
*/
var Transitions = function () {
/**
* Creates a new instance of transitions with an empty transitions list and
* no default transition.
*/
function Transitions() {
_classCallCheck(this, Transitions);
this._transitions = [];
this._default = undefined;
}
/**
* Returns the total amount of transitions in this instance. If there is a
* default transition, it will also be counted as one.
* @return {number} Amount of transitions.
*/
_createClass(Transitions, [{
key: 'totalTransitions',
value: function totalTransitions() {
var extra = this._default != undefined ? 1 : 0;
return this._transitions.length + extra;
}
/**
* Adds the given transition to the list of possible transitions.
* @param transition {transition} A transition that should be added.
*/
}, {
key: 'add',
value: function add(transition) {
this._transitions.push(transition);
}
/**
* Sets the default transition to the given state with the given middlewares.
* Note that if a default transition already exists, it will be overwritten.
* @param state {string} The destination state of the default transition.
* @param middlewares {array} The middlewares of the default transition.
*/
}, {
key: 'default',
value: function _default(state) {
var middlewares = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
this._default = new _Transition.Transition(function (v) {
return true;
}, state, middlewares);
}
/**
* Finds the transition that gets fired by the given value. Note that there
* should always be only one possible transition for every value in order to
* have a deterministic machine (with the exception of the default
* transition).
* @param v {string} The value that should fire a transition.
* @throws Error {error} Throws if there are multiple transitions found.
* @throws Error {error} Throws if no possible transitions found.
*/
}, {
key: 'find',
value: function find(v) {
var matches = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = this._transitions[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var transition = _step.value;
if (transition.fires(v)) matches.push(transition);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
if (matches.length > 1) throw new Error('Multiple transitions found!');
if (matches.length == 1) return matches[0];
if (this._default != undefined) return this._default;
throw new Error('No transitions found!');
}
/**
* Returns a string representation of this instance.
* @return {string} A string representation of this instance.
*/
}, {
key: 'toString',
value: function toString() {
var str = '';
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = this._transitions[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var transition = _step2.value;
str += transition.toString() + '\n';
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2.return) {
_iterator2.return();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
if (this._default != undefined) str += this._default.toString() + '\n';
return str;
}
}]);
return Transitions;
}();
module.exports.Transitions = Transitions;