radio/libjs/utils/stateMachine.js

34 lines
854 B
JavaScript
Raw Permalink Normal View History

2018-12-17 17:15:58 +00:00
"use strict";
var Subscribable = require("./subscribable");
var StateMachine = Subscribable.inherit({
className: "StateMachine",
constructor: function (initialState, graph) {
Subscribable.fn.constructor.call(this);
this._state = initialState;
this._graph = graph;
},
manipulation: function (name) {
var newState = this._graph[this._state][name];
if (newState) {
var oldState = this._state;
this._state = newState;
this.trigger("stateChanged", {
newState: newState,
manipulation: name,
oldState: oldState
});
} else {
this.trigger("stateMissed");
}
},
state: function () {
return this._state;
}
});
module.exports = StateMachine;