node-state-machine
Version:
State machine for NodeJS.
120 lines (99 loc) • 4.04 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; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Class representing a transition that has a certain trigger (function) and a
* destination node (string). It also has a list of middlewares which can be
* executed when the transition is executed.
* @author Jensen Bernard
*/
var Transition = function () {
/**
* Creates a new instance of a transition with the given checker/trigger,
* destination node and list of middlewares.
* @param checker {string} The trigger of the transition.
* @param destination {string} The destination of the transition.
* @param middlewares {array} The middlewares executed.
*/
function Transition(checker, destination) {
var middlewares = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
_classCallCheck(this, Transition);
this._checker = checker;
this._destination = destination;
this._middlewares = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = middlewares[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var middleware = _step.value;
this._middlewares.push(middleware);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
/**
* Checks whether or not the transition has middleswares. Returns true if
* and only if this._middlewares.length > 0.
* @return {boolean} Whether or not the transition has middlewares.
*/
_createClass(Transition, [{
key: "hasMiddlewares",
value: function hasMiddlewares() {
return this._middlewares.length > 0;
}
/**
* Returns the list of middlewares of the current transition. This list will
* be the same one that is passed as an argument in the constructor.
* @return {array} List of middlewares.
*/
}, {
key: "middlewares",
value: function middlewares() {
return this._middlewares;
}
/**
* Checks whether or not the current transition fires based on the checker/
* trigger that is provided when this transition was created.
* @return {boolean} Whether or not this transition should fire.
*/
}, {
key: "fires",
value: function fires(v) {
return this._checker(v);
}
/**
* Returns the destination of the current transition. This is the one that
* is provided as an argument when this transition is created.
* @return {string} The destination state of this transition.
*/
}, {
key: "destination",
value: function destination() {
return this._destination;
}
/**
* Returns a string representation of this transition.
* @return {string} A string representation of this transition.
*/
}, {
key: "toString",
value: function toString() {
return "(f() > " + this._destination + " (" + this._middlewares.length + "M))";
}
}]);
return Transition;
}();
module.exports.Transition = Transition;