finitestate
Version:
1. All actions are functions which have a $$displayName property. 2. All states are strings 3. Json representation for SSR
132 lines (112 loc) • 5.16 kB
JavaScript
;
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 _has = require("lodash/has");
var _has2 = _interopRequireDefault(_has);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _state = Symbol();
var _value = Symbol();
var _actions = Symbol();
var FSM = function () {
function FSM(_ref) {
var _control__,
_this = this;
var startState = _ref.startState,
startValue = _ref.startValue,
transitions = _ref.transitions,
states = _ref.states,
transitionMap = _ref.transitionMap;
_classCallCheck(this, FSM);
this.__control__ = (_control__ = {}, _defineProperty(_control__, _state, undefined), _defineProperty(_control__, _value, undefined), _defineProperty(_control__, _actions, {}), _control__);
// grab options
this.states = states;
this.startState = startState;
this.startValue = Object.freeze(startValue);
this.transitions = transitions;
this.transitionMap = transitionMap;
// set private fields
this.__control__[_state] = this.startState;
this.__control__[_value] = Object.freeze(this.startValue);
// create corresponding actions for transitions
var actions = {};
this.transitions.forEach(function (transition) {
actions[transition] = function () {
//if the current state has a valid transition in the transitionMap
if (_this.isTransitionValid(transition)) {
var newState = _this.transitionMap[_this.__control__[_state]][transition];
var newValue = _this.__control__[_value];
var original = _this[transition];
if (typeof original === "function") {
var transformationFn = original.apply(undefined, arguments);
if (typeof transformationFn === "function") {
newValue = transformationFn(_this.__control__[_value]);
}
} else {
throw Error("Found transition of type " + (typeof transition === "undefined" ? "undefined" : _typeof(transition)) + ". Transition " + transition + " should be a function which returns a transformation function.");
}
var oldProps = {
state: _this.__control__[_state],
value: _this.__control__[_value]
};
var newProps = {
state: newState,
value: newValue
};
if (_this.shouldTransitionOccur(oldProps, newProps, transition)) {
_this.transitionWillOccur(oldProps, newProps, transition);
_this.__control__[_value] = Object.freeze(newValue);
_this.__control__[_state] = newState;
_this.transitionDidOccur(oldProps, newProps, transition);
} else {
console.debug("transition was not performed");
}
return _this.__control__[_value];
} else {
console.warn("Transition " + transition + " cannot be performed right now");
return _this.__control__[_value];
}
};
});
// freeze actions
this.__control__[_actions] = Object.freeze(actions);
}
_createClass(FSM, [{
key: "isTransitionValid",
value: function isTransitionValid(transition) {
return (0, _has2.default)(this.transitionMap[this.__control__[_state]], transition);
}
}, {
key: "shouldTransitionOccur",
value: function shouldTransitionOccur() {
return true;
}
}, {
key: "transitionWillOccur",
value: function transitionWillOccur() {}
}, {
key: "transitionDidOccur",
value: function transitionDidOccur() {}
}, {
key: "value",
get: function get() {
return this.__control__[_value];
}
}, {
key: "state",
get: function get() {
return this.__control__[_state];
}
}, {
key: "actions",
get: function get() {
return this.__control__[_actions];
}
}]);
return FSM;
}();
exports.default = FSM;