Files
30-seconds-of-code/node_modules/xstate/es/State.js
2019-08-20 15:52:05 +02:00

200 lines
6.5 KiB
JavaScript

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;
};
import { ActionTypes } from './types';
import { EMPTY_ACTIVITY_MAP } from './constants';
import { matchesState, keys, isString } from './utils';
export function stateValuesEqual(a, b) {
if (a === b) {
return true;
}
if (a === undefined || b === undefined) {
return false;
}
if (isString(a) || isString(b)) {
return a === b;
}
var aKeys = keys(a);
var bKeys = keys(b);
return aKeys.length === bKeys.length && aKeys.every(function (key) {
return stateValuesEqual(a[key], b[key]);
});
}
var State = /** @class */ /*#__PURE__*/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 = 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 || 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: 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: 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 (isString(stateValue)) {
return [stateValue];
}
var valueKeys = 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 matchesState(parentStateValue, this.value);
};
return State;
}();
export { State };
//# sourceMappingURL=State.js.map