1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | var actors_dist_xstateActors = require('../actors/dist/xstate-actors.development.cjs.js');
|
6 | var guards_dist_xstateGuards = require('./raise-52a00e8f.development.cjs.js');
|
7 | var log = require('./log-6380a394.development.cjs.js');
|
8 | require('../dev/dist/xstate-dev.development.cjs.js');
|
9 |
|
10 | class SimulatedClock {
|
11 | constructor() {
|
12 | this.timeouts = new Map();
|
13 | this._now = 0;
|
14 | this._id = 0;
|
15 | this._flushing = false;
|
16 | this._flushingInvalidated = false;
|
17 | }
|
18 | now() {
|
19 | return this._now;
|
20 | }
|
21 | getId() {
|
22 | return this._id++;
|
23 | }
|
24 | setTimeout(fn, timeout) {
|
25 | this._flushingInvalidated = this._flushing;
|
26 | const id = this.getId();
|
27 | this.timeouts.set(id, {
|
28 | start: this.now(),
|
29 | timeout,
|
30 | fn
|
31 | });
|
32 | return id;
|
33 | }
|
34 | clearTimeout(id) {
|
35 | this._flushingInvalidated = this._flushing;
|
36 | this.timeouts.delete(id);
|
37 | }
|
38 | set(time) {
|
39 | if (this._now > time) {
|
40 | throw new Error('Unable to travel back in time');
|
41 | }
|
42 | this._now = time;
|
43 | this.flushTimeouts();
|
44 | }
|
45 | flushTimeouts() {
|
46 | if (this._flushing) {
|
47 | this._flushingInvalidated = true;
|
48 | return;
|
49 | }
|
50 | this._flushing = true;
|
51 | const sorted = [...this.timeouts].sort(([_idA, timeoutA], [_idB, timeoutB]) => {
|
52 | const endA = timeoutA.start + timeoutA.timeout;
|
53 | const endB = timeoutB.start + timeoutB.timeout;
|
54 | return endB > endA ? -1 : 1;
|
55 | });
|
56 | for (const [id, timeout] of sorted) {
|
57 | if (this._flushingInvalidated) {
|
58 | this._flushingInvalidated = false;
|
59 | this._flushing = false;
|
60 | this.flushTimeouts();
|
61 | return;
|
62 | }
|
63 | if (this.now() - timeout.start >= timeout.timeout) {
|
64 | this.timeouts.delete(id);
|
65 | timeout.fn.call(null);
|
66 | }
|
67 | }
|
68 | this._flushing = false;
|
69 | }
|
70 | increment(ms) {
|
71 | this._now += ms;
|
72 | this.flushTimeouts();
|
73 | }
|
74 | }
|
75 |
|
76 | const cache = new WeakMap();
|
77 | function memo(object, key, fn) {
|
78 | let memoizedData = cache.get(object);
|
79 | if (!memoizedData) {
|
80 | memoizedData = {
|
81 | [key]: fn()
|
82 | };
|
83 | cache.set(object, memoizedData);
|
84 | } else if (!(key in memoizedData)) {
|
85 | memoizedData[key] = fn();
|
86 | }
|
87 | return memoizedData[key];
|
88 | }
|
89 |
|
90 | const EMPTY_OBJECT = {};
|
91 | const toSerializableAction = action => {
|
92 | if (typeof action === 'string') {
|
93 | return {
|
94 | type: action
|
95 | };
|
96 | }
|
97 | if (typeof action === 'function') {
|
98 | if ('resolve' in action) {
|
99 | return {
|
100 | type: action.type
|
101 | };
|
102 | }
|
103 | return {
|
104 | type: action.name
|
105 | };
|
106 | }
|
107 | return action;
|
108 | };
|
109 | class StateNode {
|
110 | constructor(
|
111 | /**
|
112 | * The raw config used to create the machine.
|
113 | */
|
114 | config, options) {
|
115 | this.config = config;
|
116 | |
117 |
|
118 |
|
119 | this.key = void 0;
|
120 | |
121 |
|
122 |
|
123 | this.id = void 0;
|
124 | |
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | this.type = void 0;
|
134 | |
135 |
|
136 |
|
137 | this.path = void 0;
|
138 | |
139 |
|
140 |
|
141 | this.states = void 0;
|
142 | |
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | this.history = void 0;
|
149 | |
150 |
|
151 |
|
152 | this.entry = void 0;
|
153 | |
154 |
|
155 |
|
156 | this.exit = void 0;
|
157 | |
158 |
|
159 |
|
160 | this.parent = void 0;
|
161 | |
162 |
|
163 |
|
164 | this.machine = void 0;
|
165 | |
166 |
|
167 |
|
168 | this.meta = void 0;
|
169 | |
170 |
|
171 |
|
172 | this.output = void 0;
|
173 | |
174 |
|
175 |
|
176 | this.order = -1;
|
177 | this.description = void 0;
|
178 | this.tags = [];
|
179 | this.transitions = void 0;
|
180 | this.always = void 0;
|
181 | this.parent = options._parent;
|
182 | this.key = options._key;
|
183 | this.machine = options._machine;
|
184 | this.path = this.parent ? this.parent.path.concat(this.key) : [];
|
185 | this.id = this.config.id || [this.machine.id, ...this.path].join(guards_dist_xstateGuards.STATE_DELIMITER);
|
186 | this.type = this.config.type || (this.config.states && Object.keys(this.config.states).length ? 'compound' : this.config.history ? 'history' : 'atomic');
|
187 | this.description = this.config.description;
|
188 | this.order = this.machine.idMap.size;
|
189 | this.machine.idMap.set(this.id, this);
|
190 | this.states = this.config.states ? guards_dist_xstateGuards.mapValues(this.config.states, (stateConfig, key) => {
|
191 | const stateNode = new StateNode(stateConfig, {
|
192 | _parent: this,
|
193 | _key: key,
|
194 | _machine: this.machine
|
195 | });
|
196 | return stateNode;
|
197 | }) : EMPTY_OBJECT;
|
198 | if (this.type === 'compound' && !this.config.initial) {
|
199 | throw new Error(`No initial state specified for compound state node "#${this.id}". Try adding { initial: "${Object.keys(this.states)[0]}" } to the state config.`);
|
200 | }
|
201 |
|
202 |
|
203 | this.history = this.config.history === true ? 'shallow' : this.config.history || false;
|
204 | this.entry = guards_dist_xstateGuards.toArray(this.config.entry).slice();
|
205 | this.exit = guards_dist_xstateGuards.toArray(this.config.exit).slice();
|
206 | this.meta = this.config.meta;
|
207 | this.output = this.type === 'final' || !this.parent ? this.config.output : undefined;
|
208 | this.tags = guards_dist_xstateGuards.toArray(config.tags).slice();
|
209 | }
|
210 |
|
211 |
|
212 | _initialize() {
|
213 | this.transitions = guards_dist_xstateGuards.formatTransitions(this);
|
214 | if (this.config.always) {
|
215 | this.always = guards_dist_xstateGuards.toTransitionConfigArray(this.config.always).map(t => guards_dist_xstateGuards.formatTransition(this, guards_dist_xstateGuards.NULL_EVENT, t));
|
216 | }
|
217 | Object.keys(this.states).forEach(key => {
|
218 | this.states[key]._initialize();
|
219 | });
|
220 | }
|
221 |
|
222 | |
223 |
|
224 |
|
225 | get definition() {
|
226 | return {
|
227 | id: this.id,
|
228 | key: this.key,
|
229 | version: this.machine.version,
|
230 | type: this.type,
|
231 | initial: this.initial ? {
|
232 | target: this.initial.target,
|
233 | source: this,
|
234 | actions: this.initial.actions.map(toSerializableAction),
|
235 | eventType: null,
|
236 | reenter: false,
|
237 | toJSON: () => ({
|
238 | target: this.initial.target.map(t => `#${t.id}`),
|
239 | source: `#${this.id}`,
|
240 | actions: this.initial.actions.map(toSerializableAction),
|
241 | eventType: null
|
242 | })
|
243 | } : undefined,
|
244 | history: this.history,
|
245 | states: guards_dist_xstateGuards.mapValues(this.states, state => {
|
246 | return state.definition;
|
247 | }),
|
248 | on: this.on,
|
249 | transitions: [...this.transitions.values()].flat().map(t => ({
|
250 | ...t,
|
251 | actions: t.actions.map(toSerializableAction)
|
252 | })),
|
253 | entry: this.entry.map(toSerializableAction),
|
254 | exit: this.exit.map(toSerializableAction),
|
255 | meta: this.meta,
|
256 | order: this.order || -1,
|
257 | output: this.output,
|
258 | invoke: this.invoke,
|
259 | description: this.description,
|
260 | tags: this.tags
|
261 | };
|
262 | }
|
263 |
|
264 |
|
265 | toJSON() {
|
266 | return this.definition;
|
267 | }
|
268 |
|
269 | |
270 |
|
271 |
|
272 | get invoke() {
|
273 | return memo(this, 'invoke', () => guards_dist_xstateGuards.toArray(this.config.invoke).map((invokeConfig, i) => {
|
274 | const {
|
275 | src,
|
276 | systemId
|
277 | } = invokeConfig;
|
278 | const resolvedId = invokeConfig.id ?? guards_dist_xstateGuards.createInvokeId(this.id, i);
|
279 | const resolvedSrc = typeof src === 'string' ? src : `xstate.invoke.${guards_dist_xstateGuards.createInvokeId(this.id, i)}`;
|
280 | return {
|
281 | ...invokeConfig,
|
282 | src: resolvedSrc,
|
283 | id: resolvedId,
|
284 | systemId: systemId,
|
285 | toJSON() {
|
286 | const {
|
287 | onDone,
|
288 | onError,
|
289 | ...invokeDefValues
|
290 | } = invokeConfig;
|
291 | return {
|
292 | ...invokeDefValues,
|
293 | type: 'xstate.invoke',
|
294 | src: resolvedSrc,
|
295 | id: resolvedId
|
296 | };
|
297 | }
|
298 | };
|
299 | }));
|
300 | }
|
301 |
|
302 | |
303 |
|
304 |
|
305 | get on() {
|
306 | return memo(this, 'on', () => {
|
307 | const transitions = this.transitions;
|
308 | return [...transitions].flatMap(([descriptor, t]) => t.map(t => [descriptor, t])).reduce((map, [descriptor, transition]) => {
|
309 | map[descriptor] = map[descriptor] || [];
|
310 | map[descriptor].push(transition);
|
311 | return map;
|
312 | }, {});
|
313 | });
|
314 | }
|
315 | get after() {
|
316 | return memo(this, 'delayedTransitions', () => guards_dist_xstateGuards.getDelayedTransitions(this));
|
317 | }
|
318 | get initial() {
|
319 | return memo(this, 'initial', () => guards_dist_xstateGuards.formatInitialTransition(this, this.config.initial));
|
320 | }
|
321 |
|
322 |
|
323 | next(snapshot, event) {
|
324 | const eventType = event.type;
|
325 | const actions = [];
|
326 | let selectedTransition;
|
327 | const candidates = memo(this, `candidates-${eventType}`, () => guards_dist_xstateGuards.getCandidates(this, eventType));
|
328 | for (const candidate of candidates) {
|
329 | const {
|
330 | guard
|
331 | } = candidate;
|
332 | const resolvedContext = snapshot.context;
|
333 | let guardPassed = false;
|
334 | try {
|
335 | guardPassed = !guard || guards_dist_xstateGuards.evaluateGuard(guard, resolvedContext, event, snapshot);
|
336 | } catch (err) {
|
337 | const guardType = typeof guard === 'string' ? guard : typeof guard === 'object' ? guard.type : undefined;
|
338 | throw new Error(`Unable to evaluate guard ${guardType ? `'${guardType}' ` : ''}in transition for event '${eventType}' in state node '${this.id}':\n${err.message}`);
|
339 | }
|
340 | if (guardPassed) {
|
341 | actions.push(...candidate.actions);
|
342 | selectedTransition = candidate;
|
343 | break;
|
344 | }
|
345 | }
|
346 | return selectedTransition ? [selectedTransition] : undefined;
|
347 | }
|
348 |
|
349 | |
350 |
|
351 |
|
352 | get events() {
|
353 | return memo(this, 'events', () => {
|
354 | const {
|
355 | states
|
356 | } = this;
|
357 | const events = new Set(this.ownEvents);
|
358 | if (states) {
|
359 | for (const stateId of Object.keys(states)) {
|
360 | const state = states[stateId];
|
361 | if (state.states) {
|
362 | for (const event of state.events) {
|
363 | events.add(`${event}`);
|
364 | }
|
365 | }
|
366 | }
|
367 | }
|
368 | return Array.from(events);
|
369 | });
|
370 | }
|
371 |
|
372 | |
373 |
|
374 |
|
375 |
|
376 |
|
377 | get ownEvents() {
|
378 | const events = new Set([...this.transitions.keys()].filter(descriptor => {
|
379 | return this.transitions.get(descriptor).some(transition => !(!transition.target && !transition.actions.length && !transition.reenter));
|
380 | }));
|
381 | return Array.from(events);
|
382 | }
|
383 | }
|
384 |
|
385 | const STATE_IDENTIFIER = '#';
|
386 | class StateMachine {
|
387 | constructor(
|
388 | /**
|
389 | * The raw config used to create the machine.
|
390 | */
|
391 | config, implementations) {
|
392 | this.config = config;
|
393 | |
394 |
|
395 |
|
396 | this.version = void 0;
|
397 | this.schemas = void 0;
|
398 | this.implementations = void 0;
|
399 |
|
400 | this.__xstatenode = true;
|
401 |
|
402 | this.idMap = new Map();
|
403 | this.root = void 0;
|
404 | this.id = void 0;
|
405 | this.states = void 0;
|
406 | this.events = void 0;
|
407 | |
408 |
|
409 |
|
410 | this.__TResolvedTypesMeta = void 0;
|
411 | this.id = config.id || '(machine)';
|
412 | this.implementations = {
|
413 | actors: implementations?.actors ?? {},
|
414 | actions: implementations?.actions ?? {},
|
415 | delays: implementations?.delays ?? {},
|
416 | guards: implementations?.guards ?? {}
|
417 | };
|
418 | this.version = this.config.version;
|
419 | this.schemas = this.config.schemas;
|
420 | this.transition = this.transition.bind(this);
|
421 | this.getInitialSnapshot = this.getInitialSnapshot.bind(this);
|
422 | this.getPersistedSnapshot = this.getPersistedSnapshot.bind(this);
|
423 | this.restoreSnapshot = this.restoreSnapshot.bind(this);
|
424 | this.start = this.start.bind(this);
|
425 | this.root = new StateNode(config, {
|
426 | _key: this.id,
|
427 | _machine: this
|
428 | });
|
429 | this.root._initialize();
|
430 | this.states = this.root.states;
|
431 | this.events = this.root.events;
|
432 | if (!('output' in this.root) && Object.values(this.states).some(state => state.type === 'final' && 'output' in state)) {
|
433 | console.warn('Missing `machine.output` declaration (top-level final state with output detected)');
|
434 | }
|
435 | }
|
436 |
|
437 | |
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 | provide(implementations) {
|
447 | const {
|
448 | actions,
|
449 | guards,
|
450 | actors,
|
451 | delays
|
452 | } = this.implementations;
|
453 | return new StateMachine(this.config, {
|
454 | actions: {
|
455 | ...actions,
|
456 | ...implementations.actions
|
457 | },
|
458 | guards: {
|
459 | ...guards,
|
460 | ...implementations.guards
|
461 | },
|
462 | actors: {
|
463 | ...actors,
|
464 | ...implementations.actors
|
465 | },
|
466 | delays: {
|
467 | ...delays,
|
468 | ...implementations.delays
|
469 | }
|
470 | });
|
471 | }
|
472 | resolveState(config) {
|
473 | const resolvedStateValue = guards_dist_xstateGuards.resolveStateValue(this.root, config.value);
|
474 | const nodeSet = guards_dist_xstateGuards.getAllStateNodes(guards_dist_xstateGuards.getStateNodes(this.root, resolvedStateValue));
|
475 | return guards_dist_xstateGuards.createMachineSnapshot({
|
476 | _nodes: [...nodeSet],
|
477 | context: config.context || {},
|
478 | children: {},
|
479 | status: guards_dist_xstateGuards.isInFinalState(nodeSet, this.root) ? 'done' : config.status || 'active',
|
480 | output: config.output,
|
481 | error: config.error,
|
482 | historyValue: config.historyValue
|
483 | }, this);
|
484 | }
|
485 |
|
486 | |
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | transition(snapshot, event, actorScope) {
|
494 | return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope).snapshot;
|
495 | }
|
496 |
|
497 | |
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 | microstep(snapshot, event, actorScope) {
|
505 | return guards_dist_xstateGuards.macrostep(snapshot, event, actorScope).microstates;
|
506 | }
|
507 | getTransitionData(snapshot, event) {
|
508 | return guards_dist_xstateGuards.transitionNode(this.root, snapshot.value, snapshot, event) || [];
|
509 | }
|
510 |
|
511 | |
512 |
|
513 |
|
514 |
|
515 | getPreInitialState(actorScope, initEvent, internalQueue) {
|
516 | const {
|
517 | context
|
518 | } = this.config;
|
519 | const preInitial = guards_dist_xstateGuards.createMachineSnapshot({
|
520 | context: typeof context !== 'function' && context ? context : {},
|
521 | _nodes: [this.root],
|
522 | children: {},
|
523 | status: 'active'
|
524 | }, this);
|
525 | if (typeof context === 'function') {
|
526 | const assignment = ({
|
527 | spawn,
|
528 | event,
|
529 | self
|
530 | }) => context({
|
531 | spawn,
|
532 | input: event.input,
|
533 | self
|
534 | });
|
535 | return guards_dist_xstateGuards.resolveActionsAndContext(preInitial, initEvent, actorScope, [log.assign(assignment)], internalQueue);
|
536 | }
|
537 | return preInitial;
|
538 | }
|
539 |
|
540 | |
541 |
|
542 |
|
543 | getInitialSnapshot(actorScope, input) {
|
544 | const initEvent = guards_dist_xstateGuards.createInitEvent(input);
|
545 | const internalQueue = [];
|
546 | const preInitialState = this.getPreInitialState(actorScope, initEvent, internalQueue);
|
547 | const nextState = guards_dist_xstateGuards.microstep([{
|
548 | target: [...guards_dist_xstateGuards.getInitialStateNodes(this.root)],
|
549 | source: this.root,
|
550 | reenter: true,
|
551 | actions: [],
|
552 | eventType: null,
|
553 | toJSON: null
|
554 | }], preInitialState, actorScope, initEvent, true, internalQueue);
|
555 | const {
|
556 | snapshot: macroState
|
557 | } = guards_dist_xstateGuards.macrostep(nextState, initEvent, actorScope, internalQueue);
|
558 | return macroState;
|
559 | }
|
560 | start(snapshot) {
|
561 | Object.values(snapshot.children).forEach(child => {
|
562 | if (child.getSnapshot().status === 'active') {
|
563 | child.start();
|
564 | }
|
565 | });
|
566 | }
|
567 | getStateNodeById(stateId) {
|
568 | const fullPath = guards_dist_xstateGuards.toStatePath(stateId);
|
569 | const relativePath = fullPath.slice(1);
|
570 | const resolvedStateId = guards_dist_xstateGuards.isStateId(fullPath[0]) ? fullPath[0].slice(STATE_IDENTIFIER.length) : fullPath[0];
|
571 | const stateNode = this.idMap.get(resolvedStateId);
|
572 | if (!stateNode) {
|
573 | throw new Error(`Child state node '#${resolvedStateId}' does not exist on machine '${this.id}'`);
|
574 | }
|
575 | return guards_dist_xstateGuards.getStateNodeByPath(stateNode, relativePath);
|
576 | }
|
577 | get definition() {
|
578 | return this.root.definition;
|
579 | }
|
580 | toJSON() {
|
581 | return this.definition;
|
582 | }
|
583 | getPersistedSnapshot(snapshot, options) {
|
584 | return guards_dist_xstateGuards.getPersistedSnapshot(snapshot, options);
|
585 | }
|
586 | restoreSnapshot(snapshot, _actorScope) {
|
587 | const children = {};
|
588 | const snapshotChildren = snapshot.children;
|
589 | Object.keys(snapshotChildren).forEach(actorId => {
|
590 | const actorData = snapshotChildren[actorId];
|
591 | const childState = actorData.snapshot;
|
592 | const src = actorData.src;
|
593 | const logic = typeof src === 'string' ? guards_dist_xstateGuards.resolveReferencedActor(this, src) : src;
|
594 | if (!logic) {
|
595 | return;
|
596 | }
|
597 | const actorRef = guards_dist_xstateGuards.createActor(logic, {
|
598 | id: actorId,
|
599 | parent: _actorScope.self,
|
600 | syncSnapshot: actorData.syncSnapshot,
|
601 | snapshot: childState,
|
602 | src,
|
603 | systemId: actorData.systemId
|
604 | });
|
605 | children[actorId] = actorRef;
|
606 | });
|
607 | const restoredSnapshot = guards_dist_xstateGuards.createMachineSnapshot({
|
608 | ...snapshot,
|
609 | children,
|
610 | _nodes: Array.from(guards_dist_xstateGuards.getAllStateNodes(guards_dist_xstateGuards.getStateNodes(this.root, snapshot.value)))
|
611 | }, this);
|
612 | let seen = new Set();
|
613 | function reviveContext(contextPart, children) {
|
614 | if (seen.has(contextPart)) {
|
615 | return;
|
616 | }
|
617 | seen.add(contextPart);
|
618 | for (let key in contextPart) {
|
619 | const value = contextPart[key];
|
620 | if (value && typeof value === 'object') {
|
621 | if ('xstate$$type' in value && value.xstate$$type === guards_dist_xstateGuards.$$ACTOR_TYPE) {
|
622 | contextPart[key] = children[value.id];
|
623 | continue;
|
624 | }
|
625 | reviveContext(value, children);
|
626 | }
|
627 | }
|
628 | }
|
629 | reviveContext(restoredSnapshot.context, children);
|
630 | return restoredSnapshot;
|
631 | }
|
632 | }
|
633 |
|
634 | const defaultWaitForOptions = {
|
635 | timeout: Infinity
|
636 | };
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 |
|
657 |
|
658 |
|
659 | function waitFor(actorRef, predicate, options) {
|
660 | const resolvedOptions = {
|
661 | ...defaultWaitForOptions,
|
662 | ...options
|
663 | };
|
664 | return new Promise((res, rej) => {
|
665 | let done = false;
|
666 | if (resolvedOptions.timeout < 0) {
|
667 | console.error('`timeout` passed to `waitFor` is negative and it will reject its internal promise immediately.');
|
668 | }
|
669 | const handle = resolvedOptions.timeout === Infinity ? undefined : setTimeout(() => {
|
670 | sub.unsubscribe();
|
671 | rej(new Error(`Timeout of ${resolvedOptions.timeout} ms exceeded`));
|
672 | }, resolvedOptions.timeout);
|
673 | const dispose = () => {
|
674 | clearTimeout(handle);
|
675 | done = true;
|
676 | sub?.unsubscribe();
|
677 | };
|
678 | function checkEmitted(emitted) {
|
679 | if (predicate(emitted)) {
|
680 | dispose();
|
681 | res(emitted);
|
682 | }
|
683 | }
|
684 | let sub;
|
685 |
|
686 |
|
687 | checkEmitted(actorRef.getSnapshot());
|
688 | if (done) {
|
689 | return;
|
690 | }
|
691 | sub = actorRef.subscribe({
|
692 | next: checkEmitted,
|
693 | error: err => {
|
694 | dispose();
|
695 | rej(err);
|
696 | },
|
697 | complete: () => {
|
698 | dispose();
|
699 | rej(new Error(`Actor terminated without satisfying predicate`));
|
700 | }
|
701 | });
|
702 | if (done) {
|
703 | sub.unsubscribe();
|
704 | }
|
705 | });
|
706 | }
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 | function createMachine(config, implementations) {
|
752 | return new StateMachine(config, implementations);
|
753 | }
|
754 |
|
755 |
|
756 | function createInertActorScope(actorLogic) {
|
757 | const self = guards_dist_xstateGuards.createActor(actorLogic);
|
758 | const inertActorScope = {
|
759 | self,
|
760 | defer: () => {},
|
761 | id: '',
|
762 | logger: () => {},
|
763 | sessionId: '',
|
764 | stopChild: () => {},
|
765 | system: self.system,
|
766 | emit: () => {}
|
767 | };
|
768 | return inertActorScope;
|
769 | }
|
770 | function getInitialSnapshot(actorLogic, ...[input]) {
|
771 | const actorScope = createInertActorScope(actorLogic);
|
772 | return actorLogic.getInitialSnapshot(actorScope, input);
|
773 | }
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 | function getNextSnapshot(actorLogic, snapshot, event) {
|
805 | const inertActorScope = createInertActorScope(actorLogic);
|
806 | inertActorScope.self._snapshot = snapshot;
|
807 | return actorLogic.transition(snapshot, event, inertActorScope);
|
808 | }
|
809 |
|
810 |
|
811 |
|
812 | function setup({
|
813 | schemas,
|
814 | actors,
|
815 | actions,
|
816 | guards,
|
817 | delays
|
818 | }) {
|
819 | return {
|
820 | createMachine: config => createMachine({
|
821 | ...config,
|
822 | schemas
|
823 | }, {
|
824 | actors,
|
825 | actions,
|
826 | guards,
|
827 | delays
|
828 | })
|
829 | };
|
830 | }
|
831 |
|
832 |
|
833 |
|
834 |
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 |
|
844 |
|
845 |
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 |
|
852 |
|
853 |
|
854 | function toPromise(actor) {
|
855 | return new Promise((resolve, reject) => {
|
856 | actor.subscribe({
|
857 | complete: () => {
|
858 | resolve(actor.getSnapshot().output);
|
859 | },
|
860 | error: reject
|
861 | });
|
862 | });
|
863 | }
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 | function assertEvent(event, type) {
|
888 | const types = guards_dist_xstateGuards.toArray(type);
|
889 | if (!types.includes(event.type)) {
|
890 | const typesText = types.length === 1 ? `type "${types[0]}"` : `one of types "${types.join('", "')}"`;
|
891 | throw new Error(`Expected event ${JSON.stringify(event)} to have ${typesText}`);
|
892 | }
|
893 | }
|
894 |
|
895 | exports.createEmptyActor = actors_dist_xstateActors.createEmptyActor;
|
896 | exports.fromCallback = actors_dist_xstateActors.fromCallback;
|
897 | exports.fromEventObservable = actors_dist_xstateActors.fromEventObservable;
|
898 | exports.fromObservable = actors_dist_xstateActors.fromObservable;
|
899 | exports.fromPromise = actors_dist_xstateActors.fromPromise;
|
900 | exports.fromTransition = actors_dist_xstateActors.fromTransition;
|
901 | exports.Actor = guards_dist_xstateGuards.Actor;
|
902 | exports.__unsafe_getAllOwnEventDescriptors = guards_dist_xstateGuards.getAllOwnEventDescriptors;
|
903 | exports.and = guards_dist_xstateGuards.and;
|
904 | exports.cancel = guards_dist_xstateGuards.cancel;
|
905 | exports.createActor = guards_dist_xstateGuards.createActor;
|
906 | exports.getStateNodes = guards_dist_xstateGuards.getStateNodes;
|
907 | exports.interpret = guards_dist_xstateGuards.interpret;
|
908 | exports.isMachineSnapshot = guards_dist_xstateGuards.isMachineSnapshot;
|
909 | exports.matchesState = guards_dist_xstateGuards.matchesState;
|
910 | exports.not = guards_dist_xstateGuards.not;
|
911 | exports.or = guards_dist_xstateGuards.or;
|
912 | exports.pathToStateValue = guards_dist_xstateGuards.pathToStateValue;
|
913 | exports.raise = guards_dist_xstateGuards.raise;
|
914 | exports.spawnChild = guards_dist_xstateGuards.spawnChild;
|
915 | exports.stateIn = guards_dist_xstateGuards.stateIn;
|
916 | exports.stop = guards_dist_xstateGuards.stop;
|
917 | exports.stopChild = guards_dist_xstateGuards.stopChild;
|
918 | exports.toObserver = guards_dist_xstateGuards.toObserver;
|
919 | exports.SpecialTargets = log.SpecialTargets;
|
920 | exports.assign = log.assign;
|
921 | exports.emit = log.emit;
|
922 | exports.enqueueActions = log.enqueueActions;
|
923 | exports.forwardTo = log.forwardTo;
|
924 | exports.log = log.log;
|
925 | exports.sendParent = log.sendParent;
|
926 | exports.sendTo = log.sendTo;
|
927 | exports.SimulatedClock = SimulatedClock;
|
928 | exports.StateMachine = StateMachine;
|
929 | exports.StateNode = StateNode;
|
930 | exports.assertEvent = assertEvent;
|
931 | exports.createMachine = createMachine;
|
932 | exports.getInitialSnapshot = getInitialSnapshot;
|
933 | exports.getNextSnapshot = getNextSnapshot;
|
934 | exports.setup = setup;
|
935 | exports.toPromise = toPromise;
|
936 | exports.waitFor = waitFor;
|