1 | import { __assign, __spreadArray, __read, __rest } from './_virtual/_tslib.js';
|
2 | import { EMPTY_ACTIVITY_MAP } from './constants.js';
|
3 | import { isString, matchesState, warn } from './utils.js';
|
4 | import { getMeta, nextEvents } from './stateUtils.js';
|
5 | import { initEvent } from './actions.js';
|
6 | import { IS_PRODUCTION } from './environment.js';
|
7 |
|
8 | function stateValuesEqual(a, b) {
|
9 | if (a === b) {
|
10 | return true;
|
11 | }
|
12 |
|
13 | if (a === undefined || b === undefined) {
|
14 | return false;
|
15 | }
|
16 |
|
17 | if (isString(a) || isString(b)) {
|
18 | return a === b;
|
19 | }
|
20 |
|
21 | var aKeys = Object.keys(a);
|
22 | var bKeys = Object.keys(b);
|
23 | return aKeys.length === bKeys.length && aKeys.every(function (key) {
|
24 | return stateValuesEqual(a[key], b[key]);
|
25 | });
|
26 | }
|
27 | function isStateConfig(state) {
|
28 | if (typeof state !== 'object' || state === null) {
|
29 | return false;
|
30 | }
|
31 |
|
32 | return 'value' in state && '_event' in state;
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | var isState = isStateConfig;
|
39 | function bindActionToState(action, state) {
|
40 | var exec = action.exec;
|
41 |
|
42 | var boundAction = __assign(__assign({}, action), {
|
43 | exec: exec !== undefined ? function () {
|
44 | return exec(state.context, state.event, {
|
45 | action: action,
|
46 | state: state,
|
47 | _event: state._event
|
48 | });
|
49 | } : undefined
|
50 | });
|
51 |
|
52 | return boundAction;
|
53 | }
|
54 |
|
55 | var State =
|
56 |
|
57 |
|
58 |
|
59 | function () {
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function State(config) {
|
73 | var _this = this;
|
74 |
|
75 | var _a;
|
76 |
|
77 | this.actions = [];
|
78 | this.activities = EMPTY_ACTIVITY_MAP;
|
79 | this.meta = {};
|
80 | this.events = [];
|
81 | this.value = config.value;
|
82 | this.context = config.context;
|
83 | this._event = config._event;
|
84 | this._sessionid = config._sessionid;
|
85 | this.event = this._event.data;
|
86 | this.historyValue = config.historyValue;
|
87 | this.history = config.history;
|
88 | this.actions = config.actions || [];
|
89 | this.activities = config.activities || EMPTY_ACTIVITY_MAP;
|
90 | this.meta = getMeta(config.configuration);
|
91 | this.events = config.events || [];
|
92 | this.matches = this.matches.bind(this);
|
93 | this.toStrings = this.toStrings.bind(this);
|
94 | this.configuration = config.configuration;
|
95 | this.transitions = config.transitions;
|
96 | this.children = config.children;
|
97 | this.done = !!config.done;
|
98 | this.tags = (_a = Array.isArray(config.tags) ? new Set(config.tags) : config.tags) !== null && _a !== void 0 ? _a : new Set();
|
99 | this.machine = config.machine;
|
100 | Object.defineProperty(this, 'nextEvents', {
|
101 | get: function () {
|
102 | return nextEvents(_this.configuration);
|
103 | }
|
104 | });
|
105 | }
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | State.from = function (stateValue, context) {
|
114 | if (stateValue instanceof State) {
|
115 | if (stateValue.context !== context) {
|
116 | return new State({
|
117 | value: stateValue.value,
|
118 | context: context,
|
119 | _event: stateValue._event,
|
120 | _sessionid: null,
|
121 | historyValue: stateValue.historyValue,
|
122 | history: stateValue.history,
|
123 | actions: [],
|
124 | activities: stateValue.activities,
|
125 | meta: {},
|
126 | events: [],
|
127 | configuration: [],
|
128 | transitions: [],
|
129 | children: {}
|
130 | });
|
131 | }
|
132 |
|
133 | return stateValue;
|
134 | }
|
135 |
|
136 | var _event = initEvent;
|
137 | return new State({
|
138 | value: stateValue,
|
139 | context: context,
|
140 | _event: _event,
|
141 | _sessionid: null,
|
142 | historyValue: undefined,
|
143 | history: undefined,
|
144 | actions: [],
|
145 | activities: undefined,
|
146 | meta: undefined,
|
147 | events: [],
|
148 | configuration: [],
|
149 | transitions: [],
|
150 | children: {}
|
151 | });
|
152 | };
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 | State.create = function (config) {
|
160 | return new State(config);
|
161 | };
|
162 | |
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | State.inert = function (stateValue, context) {
|
170 | if (stateValue instanceof State) {
|
171 | if (!stateValue.actions.length) {
|
172 | return stateValue;
|
173 | }
|
174 |
|
175 | var _event = initEvent;
|
176 | return new State({
|
177 | value: stateValue.value,
|
178 | context: context,
|
179 | _event: _event,
|
180 | _sessionid: null,
|
181 | historyValue: stateValue.historyValue,
|
182 | history: stateValue.history,
|
183 | activities: stateValue.activities,
|
184 | configuration: stateValue.configuration,
|
185 | transitions: [],
|
186 | children: {}
|
187 | });
|
188 | }
|
189 |
|
190 | return State.from(stateValue, context);
|
191 | };
|
192 | |
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | State.prototype.toStrings = function (stateValue, delimiter) {
|
200 | var _this = this;
|
201 |
|
202 | if (stateValue === void 0) {
|
203 | stateValue = this.value;
|
204 | }
|
205 |
|
206 | if (delimiter === void 0) {
|
207 | delimiter = '.';
|
208 | }
|
209 |
|
210 | if (isString(stateValue)) {
|
211 | return [stateValue];
|
212 | }
|
213 |
|
214 | var valueKeys = Object.keys(stateValue);
|
215 | return valueKeys.concat.apply(valueKeys, __spreadArray([], __read(valueKeys.map(function (key) {
|
216 | return _this.toStrings(stateValue[key], delimiter).map(function (s) {
|
217 | return key + delimiter + s;
|
218 | });
|
219 | })), false));
|
220 | };
|
221 |
|
222 | State.prototype.toJSON = function () {
|
223 | var _a = this;
|
224 | _a.configuration;
|
225 | _a.transitions;
|
226 | var tags = _a.tags;
|
227 | _a.machine;
|
228 | var jsonValues = __rest(_a, ["configuration", "transitions", "tags", "machine"]);
|
229 |
|
230 | return __assign(__assign({}, jsonValues), {
|
231 | tags: Array.from(tags)
|
232 | });
|
233 | };
|
234 |
|
235 | State.prototype.matches = function (parentStateValue) {
|
236 | return matchesState(parentStateValue, this.value);
|
237 | };
|
238 | |
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | State.prototype.hasTag = function (tag) {
|
245 | return this.tags.has(tag);
|
246 | };
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | State.prototype.can = function (event) {
|
258 | var _a;
|
259 |
|
260 | if (IS_PRODUCTION) {
|
261 | warn(!!this.machine, "state.can(...) used outside of a machine-created State object; this will always return false.");
|
262 | }
|
263 |
|
264 | var transitionData = (_a = this.machine) === null || _a === void 0 ? void 0 : _a.getTransitionData(this, event);
|
265 | return !!(transitionData === null || transitionData === void 0 ? void 0 : transitionData.transitions.length) &&
|
266 | transitionData.transitions.some(function (t) {
|
267 | return t.target !== undefined || t.actions.length;
|
268 | });
|
269 | };
|
270 |
|
271 | return State;
|
272 | }();
|
273 |
|
274 | export { State, bindActionToState, isState, isStateConfig, stateValuesEqual };
|