192 lines
6.6 KiB
JavaScript
192 lines
6.6 KiB
JavaScript
"use strict";
|
|
var __read = (this && this.__read) || function (o, n) {
|
|
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
if (!m) return o;
|
|
var i = m.call(o), r, ar = [], e;
|
|
try {
|
|
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
}
|
|
catch (error) { e = { error: error }; }
|
|
finally {
|
|
try {
|
|
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
}
|
|
finally { if (e) throw e.error; }
|
|
}
|
|
return ar;
|
|
};
|
|
var __spread = (this && this.__spread) || function () {
|
|
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
|
|
return ar;
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var types_1 = require("./types");
|
|
var constants_1 = require("./constants");
|
|
var utils_1 = require("./utils");
|
|
function stateValuesEqual(a, b) {
|
|
if (a === b) {
|
|
return true;
|
|
}
|
|
if (a === undefined || b === undefined) {
|
|
return false;
|
|
}
|
|
if (utils_1.isString(a) || utils_1.isString(b)) {
|
|
return a === b;
|
|
}
|
|
var aKeys = utils_1.keys(a);
|
|
var bKeys = utils_1.keys(b);
|
|
return (aKeys.length === bKeys.length &&
|
|
aKeys.every(function (key) { return stateValuesEqual(a[key], b[key]); }));
|
|
}
|
|
exports.stateValuesEqual = stateValuesEqual;
|
|
var State = /** @class */ (function () {
|
|
/**
|
|
* Creates a new State instance.
|
|
* @param value The state value
|
|
* @param context The extended state
|
|
* @param historyValue The tree representing historical values of the state nodes
|
|
* @param history The previous state
|
|
* @param actions An array of action objects to execute as side-effects
|
|
* @param activities A mapping of activities and whether they are started (`true`) or stopped (`false`).
|
|
* @param meta
|
|
* @param events Internal event queue. Should be empty with run-to-completion semantics.
|
|
* @param tree
|
|
*/
|
|
function State(config) {
|
|
this.actions = [];
|
|
this.activities = constants_1.EMPTY_ACTIVITY_MAP;
|
|
this.meta = {};
|
|
this.events = [];
|
|
this.value = config.value;
|
|
this.context = config.context;
|
|
this.event = config.event;
|
|
this.historyValue = config.historyValue;
|
|
this.history = config.history;
|
|
this.actions = config.actions || [];
|
|
this.activities = config.activities || constants_1.EMPTY_ACTIVITY_MAP;
|
|
this.meta = config.meta || {};
|
|
this.events = config.events || [];
|
|
Object.defineProperty(this, 'tree', {
|
|
value: config.tree,
|
|
enumerable: false
|
|
});
|
|
this.matches = this.matches.bind(this);
|
|
this.toStrings = this.toStrings.bind(this);
|
|
}
|
|
/**
|
|
* Creates a new State instance for the given `stateValue` and `context`.
|
|
* @param stateValue
|
|
* @param context
|
|
*/
|
|
State.from = function (stateValue, context) {
|
|
if (stateValue instanceof State) {
|
|
if (stateValue.context !== context) {
|
|
return new State({
|
|
value: stateValue.value,
|
|
context: context,
|
|
event: stateValue.event,
|
|
historyValue: stateValue.historyValue,
|
|
history: stateValue.history,
|
|
actions: [],
|
|
activities: stateValue.activities,
|
|
meta: {},
|
|
events: [],
|
|
tree: stateValue.tree
|
|
});
|
|
}
|
|
return stateValue;
|
|
}
|
|
var event = { type: types_1.ActionTypes.Init };
|
|
return new State({
|
|
value: stateValue,
|
|
context: context,
|
|
event: event,
|
|
historyValue: undefined,
|
|
history: undefined,
|
|
actions: [],
|
|
activities: undefined,
|
|
meta: undefined,
|
|
events: []
|
|
});
|
|
};
|
|
/**
|
|
* Creates a new State instance for the given `config`.
|
|
* @param config The state config
|
|
*/
|
|
State.create = function (config) {
|
|
return new State(config);
|
|
};
|
|
/**
|
|
* Creates a new `State` instance for the given `stateValue` and `context` with no actions (side-effects).
|
|
* @param stateValue
|
|
* @param context
|
|
*/
|
|
State.inert = function (stateValue, context) {
|
|
if (stateValue instanceof State) {
|
|
if (!stateValue.actions.length) {
|
|
return stateValue;
|
|
}
|
|
var event_1 = { type: types_1.ActionTypes.Init };
|
|
return new State({
|
|
value: stateValue.value,
|
|
context: context,
|
|
event: event_1,
|
|
historyValue: stateValue.historyValue,
|
|
history: stateValue.history,
|
|
activities: stateValue.activities,
|
|
tree: stateValue.tree
|
|
});
|
|
}
|
|
return State.from(stateValue, context);
|
|
};
|
|
Object.defineProperty(State.prototype, "inert", {
|
|
/**
|
|
* Returns a new `State` instance that is equal to this state no actions (side-effects).
|
|
*/
|
|
get: function () {
|
|
return State.inert(this, this.context);
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
Object.defineProperty(State.prototype, "nextEvents", {
|
|
/**
|
|
* The next events that will cause a transition from the current state.
|
|
*/
|
|
get: function () {
|
|
if (!this.tree) {
|
|
return [];
|
|
}
|
|
return this.tree.nextEvents;
|
|
},
|
|
enumerable: true,
|
|
configurable: true
|
|
});
|
|
/**
|
|
* Returns an array of all the string leaf state node paths.
|
|
* @param stateValue
|
|
* @param delimiter The character(s) that separate each subpath in the string state node path.
|
|
*/
|
|
State.prototype.toStrings = function (stateValue, delimiter) {
|
|
var _this = this;
|
|
if (stateValue === void 0) { stateValue = this.value; }
|
|
if (delimiter === void 0) { delimiter = '.'; }
|
|
if (utils_1.isString(stateValue)) {
|
|
return [stateValue];
|
|
}
|
|
var valueKeys = utils_1.keys(stateValue);
|
|
return valueKeys.concat.apply(valueKeys, __spread(valueKeys.map(function (key) {
|
|
return _this.toStrings(stateValue[key]).map(function (s) { return key + delimiter + s; });
|
|
})));
|
|
};
|
|
/**
|
|
* Whether the current state value is a subset of the given parent state value.
|
|
* @param parentStateValue
|
|
*/
|
|
State.prototype.matches = function (parentStateValue) {
|
|
return utils_1.matchesState(parentStateValue, this.value);
|
|
};
|
|
return State;
|
|
}());
|
|
exports.State = State;
|
|
//# sourceMappingURL=State.js.map
|