xstate 5.13.2 → 5.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/actions/dist/xstate-actions.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.esm.js +2 -2
- package/actions/dist/xstate-actions.esm.js +2 -2
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +289 -79
- package/actors/dist/xstate-actors.development.cjs.js +289 -79
- package/actors/dist/xstate-actors.development.esm.js +289 -79
- package/actors/dist/xstate-actors.esm.js +289 -79
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/State.d.ts +21 -25
- package/dist/declarations/src/StateMachine.d.ts +18 -30
- package/dist/declarations/src/StateNode.d.ts +29 -51
- package/dist/declarations/src/actions/assign.d.ts +29 -27
- package/dist/declarations/src/actions/cancel.d.ts +28 -22
- package/dist/declarations/src/actions/emit.d.ts +33 -33
- package/dist/declarations/src/actions/enqueueActions.d.ts +18 -16
- package/dist/declarations/src/actions/log.d.ts +5 -4
- package/dist/declarations/src/actions/send.d.ts +5 -3
- package/dist/declarations/src/actors/callback.d.ts +69 -29
- package/dist/declarations/src/actors/index.d.ts +4 -4
- package/dist/declarations/src/actors/observable.d.ts +74 -29
- package/dist/declarations/src/actors/promise.d.ts +91 -13
- package/dist/declarations/src/actors/transition.d.ts +90 -16
- package/dist/declarations/src/assert.d.ts +21 -20
- package/dist/declarations/src/createActor.d.ts +51 -42
- package/dist/declarations/src/createMachine.d.ts +40 -54
- package/dist/declarations/src/getNextSnapshot.d.ts +27 -24
- package/dist/declarations/src/guards.d.ts +67 -66
- package/dist/declarations/src/index.d.ts +0 -1
- package/dist/declarations/src/inspection.d.ts +1 -0
- package/dist/declarations/src/setup.d.ts +1 -2
- package/dist/declarations/src/spawn.d.ts +2 -5
- package/dist/declarations/src/stateUtils.d.ts +5 -10
- package/dist/declarations/src/system.d.ts +2 -2
- package/dist/declarations/src/toPromise.d.ts +1 -0
- package/dist/declarations/src/types.d.ts +169 -192
- package/dist/declarations/src/waitFor.d.ts +9 -9
- package/dist/{log-c447a931.esm.js → log-63de2429.esm.js} +112 -117
- package/dist/{log-5b14d850.cjs.js → log-b8c93ee3.cjs.js} +112 -117
- package/dist/{log-d06dd00b.development.cjs.js → log-d2c282d6.development.cjs.js} +112 -117
- package/dist/{log-b3b03961.development.esm.js → log-e9953143.development.esm.js} +112 -117
- package/dist/{raise-f406edbd.esm.js → raise-2cfe6b8f.esm.js} +302 -284
- package/dist/{raise-150d5679.development.esm.js → raise-7d030497.development.esm.js} +302 -284
- package/dist/{raise-ff8990f7.cjs.js → raise-a6298350.cjs.js} +302 -284
- package/dist/{raise-bf7a8462.development.cjs.js → raise-bad6a97b.development.cjs.js} +302 -284
- package/dist/xstate.cjs.js +134 -157
- package/dist/xstate.development.cjs.js +134 -157
- package/dist/xstate.development.esm.js +136 -159
- package/dist/xstate.esm.js +136 -159
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/dist/declarations/src/typegenTypes.d.ts +0 -168
|
@@ -57,8 +57,8 @@ const XSTATE_ERROR = 'xstate.error';
|
|
|
57
57
|
const XSTATE_STOP = 'xstate.stop';
|
|
58
58
|
|
|
59
59
|
/**
|
|
60
|
-
* Returns an event that represents an implicit event that
|
|
61
|
-
*
|
|
60
|
+
* Returns an event that represents an implicit event that is sent after the
|
|
61
|
+
* specified `delay`.
|
|
62
62
|
*
|
|
63
63
|
* @param delayRef The delay in milliseconds
|
|
64
64
|
* @param id The state node ID where this event is handled
|
|
@@ -70,8 +70,8 @@ function createAfterEvent(delayRef, id) {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
|
-
* Returns an event that represents that a final state node
|
|
74
|
-
*
|
|
73
|
+
* Returns an event that represents that a final state node has been reached in
|
|
74
|
+
* the parent state node.
|
|
75
75
|
*
|
|
76
76
|
* @param id The final state node's parent state node `id`
|
|
77
77
|
* @param output The data to pass into the event
|
|
@@ -86,8 +86,8 @@ function createDoneStateEvent(id, output) {
|
|
|
86
86
|
/**
|
|
87
87
|
* Returns an event that represents that an invoked service has terminated.
|
|
88
88
|
*
|
|
89
|
-
* An invoked service is terminated when it has reached a top-level final state
|
|
90
|
-
* but not when it is canceled.
|
|
89
|
+
* An invoked service is terminated when it has reached a top-level final state
|
|
90
|
+
* node, but not when it is canceled.
|
|
91
91
|
*
|
|
92
92
|
* @param invokeId The invoked service ID
|
|
93
93
|
* @param output The data to pass into the event
|
|
@@ -95,13 +95,15 @@ function createDoneStateEvent(id, output) {
|
|
|
95
95
|
function createDoneActorEvent(invokeId, output) {
|
|
96
96
|
return {
|
|
97
97
|
type: `xstate.done.actor.${invokeId}`,
|
|
98
|
-
output
|
|
98
|
+
output,
|
|
99
|
+
actorId: invokeId
|
|
99
100
|
};
|
|
100
101
|
}
|
|
101
102
|
function createErrorActorEvent(id, error) {
|
|
102
103
|
return {
|
|
103
104
|
type: `xstate.error.actor.${id}`,
|
|
104
|
-
error
|
|
105
|
+
error,
|
|
106
|
+
actorId: id
|
|
105
107
|
};
|
|
106
108
|
}
|
|
107
109
|
function createInitEvent(input) {
|
|
@@ -112,11 +114,12 @@ function createInitEvent(input) {
|
|
|
112
114
|
}
|
|
113
115
|
|
|
114
116
|
/**
|
|
115
|
-
* This function makes sure that unhandled errors are thrown in a separate
|
|
116
|
-
* It allows those errors to be detected by global error handlers and
|
|
117
|
-
* without interrupting our own stack of
|
|
117
|
+
* This function makes sure that unhandled errors are thrown in a separate
|
|
118
|
+
* macrotask. It allows those errors to be detected by global error handlers and
|
|
119
|
+
* reported to bug tracking services without interrupting our own stack of
|
|
120
|
+
* execution.
|
|
118
121
|
*
|
|
119
|
-
* @param err
|
|
122
|
+
* @param err Error to be thrown
|
|
120
123
|
*/
|
|
121
124
|
function reportUnhandledError(err) {
|
|
122
125
|
setTimeout(() => {
|
|
@@ -126,134 +129,6 @@ function reportUnhandledError(err) {
|
|
|
126
129
|
|
|
127
130
|
const symbolObservable = (() => typeof Symbol === 'function' && Symbol.observable || '@@observable')();
|
|
128
131
|
|
|
129
|
-
function createScheduledEventId(actorRef, id) {
|
|
130
|
-
return `${actorRef.sessionId}.${id}`;
|
|
131
|
-
}
|
|
132
|
-
let idCounter = 0;
|
|
133
|
-
function createSystem(rootActor, options) {
|
|
134
|
-
const children = new Map();
|
|
135
|
-
const keyedActors = new Map();
|
|
136
|
-
const reverseKeyedActors = new WeakMap();
|
|
137
|
-
const inspectionObservers = new Set();
|
|
138
|
-
const timerMap = {};
|
|
139
|
-
const {
|
|
140
|
-
clock,
|
|
141
|
-
logger
|
|
142
|
-
} = options;
|
|
143
|
-
const scheduler = {
|
|
144
|
-
schedule: (source, target, event, delay, id = Math.random().toString(36).slice(2)) => {
|
|
145
|
-
const scheduledEvent = {
|
|
146
|
-
source,
|
|
147
|
-
target,
|
|
148
|
-
event,
|
|
149
|
-
delay,
|
|
150
|
-
id,
|
|
151
|
-
startedAt: Date.now()
|
|
152
|
-
};
|
|
153
|
-
const scheduledEventId = createScheduledEventId(source, id);
|
|
154
|
-
system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
|
|
155
|
-
const timeout = clock.setTimeout(() => {
|
|
156
|
-
delete timerMap[scheduledEventId];
|
|
157
|
-
delete system._snapshot._scheduledEvents[scheduledEventId];
|
|
158
|
-
system._relay(source, target, event);
|
|
159
|
-
}, delay);
|
|
160
|
-
timerMap[scheduledEventId] = timeout;
|
|
161
|
-
},
|
|
162
|
-
cancel: (source, id) => {
|
|
163
|
-
const scheduledEventId = createScheduledEventId(source, id);
|
|
164
|
-
const timeout = timerMap[scheduledEventId];
|
|
165
|
-
delete timerMap[scheduledEventId];
|
|
166
|
-
delete system._snapshot._scheduledEvents[scheduledEventId];
|
|
167
|
-
clock.clearTimeout(timeout);
|
|
168
|
-
},
|
|
169
|
-
cancelAll: actorRef => {
|
|
170
|
-
for (const scheduledEventId in system._snapshot._scheduledEvents) {
|
|
171
|
-
const scheduledEvent = system._snapshot._scheduledEvents[scheduledEventId];
|
|
172
|
-
if (scheduledEvent.source === actorRef) {
|
|
173
|
-
scheduler.cancel(actorRef, scheduledEvent.id);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
const sendInspectionEvent = event => {
|
|
179
|
-
if (!inspectionObservers.size) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
const resolvedInspectionEvent = {
|
|
183
|
-
...event,
|
|
184
|
-
rootId: rootActor.sessionId
|
|
185
|
-
};
|
|
186
|
-
inspectionObservers.forEach(observer => observer.next?.(resolvedInspectionEvent));
|
|
187
|
-
};
|
|
188
|
-
const system = {
|
|
189
|
-
_snapshot: {
|
|
190
|
-
_scheduledEvents: (options?.snapshot && options.snapshot.scheduler) ?? {}
|
|
191
|
-
},
|
|
192
|
-
_bookId: () => `x:${idCounter++}`,
|
|
193
|
-
_register: (sessionId, actorRef) => {
|
|
194
|
-
children.set(sessionId, actorRef);
|
|
195
|
-
return sessionId;
|
|
196
|
-
},
|
|
197
|
-
_unregister: actorRef => {
|
|
198
|
-
children.delete(actorRef.sessionId);
|
|
199
|
-
const systemId = reverseKeyedActors.get(actorRef);
|
|
200
|
-
if (systemId !== undefined) {
|
|
201
|
-
keyedActors.delete(systemId);
|
|
202
|
-
reverseKeyedActors.delete(actorRef);
|
|
203
|
-
}
|
|
204
|
-
},
|
|
205
|
-
get: systemId => {
|
|
206
|
-
return keyedActors.get(systemId);
|
|
207
|
-
},
|
|
208
|
-
_set: (systemId, actorRef) => {
|
|
209
|
-
const existing = keyedActors.get(systemId);
|
|
210
|
-
if (existing && existing !== actorRef) {
|
|
211
|
-
throw new Error(`Actor with system ID '${systemId}' already exists.`);
|
|
212
|
-
}
|
|
213
|
-
keyedActors.set(systemId, actorRef);
|
|
214
|
-
reverseKeyedActors.set(actorRef, systemId);
|
|
215
|
-
},
|
|
216
|
-
inspect: observer => {
|
|
217
|
-
inspectionObservers.add(observer);
|
|
218
|
-
},
|
|
219
|
-
_sendInspectionEvent: sendInspectionEvent,
|
|
220
|
-
_relay: (source, target, event) => {
|
|
221
|
-
system._sendInspectionEvent({
|
|
222
|
-
type: '@xstate.event',
|
|
223
|
-
sourceRef: source,
|
|
224
|
-
actorRef: target,
|
|
225
|
-
event
|
|
226
|
-
});
|
|
227
|
-
target._send(event);
|
|
228
|
-
},
|
|
229
|
-
scheduler,
|
|
230
|
-
getSnapshot: () => {
|
|
231
|
-
return {
|
|
232
|
-
_scheduledEvents: {
|
|
233
|
-
...system._snapshot._scheduledEvents
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
},
|
|
237
|
-
start: () => {
|
|
238
|
-
const scheduledEvents = system._snapshot._scheduledEvents;
|
|
239
|
-
system._snapshot._scheduledEvents = {};
|
|
240
|
-
for (const scheduledId in scheduledEvents) {
|
|
241
|
-
const {
|
|
242
|
-
source,
|
|
243
|
-
target,
|
|
244
|
-
event,
|
|
245
|
-
delay,
|
|
246
|
-
id
|
|
247
|
-
} = scheduledEvents[scheduledId];
|
|
248
|
-
scheduler.schedule(source, target, event, delay, id);
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
_clock: clock,
|
|
252
|
-
_logger: logger
|
|
253
|
-
};
|
|
254
|
-
return system;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
132
|
function matchesState(parentStateId, childStateId) {
|
|
258
133
|
const parentStateValue = toStateValue(parentStateId);
|
|
259
134
|
const childStateValue = toStateValue(childStateId);
|
|
@@ -411,6 +286,140 @@ function getAllOwnEventDescriptors(snapshot) {
|
|
|
411
286
|
return [...new Set([...snapshot._nodes.flatMap(sn => sn.ownEvents)])];
|
|
412
287
|
}
|
|
413
288
|
|
|
289
|
+
function createScheduledEventId(actorRef, id) {
|
|
290
|
+
return `${actorRef.sessionId}.${id}`;
|
|
291
|
+
}
|
|
292
|
+
let idCounter = 0;
|
|
293
|
+
function createSystem(rootActor, options) {
|
|
294
|
+
const children = new Map();
|
|
295
|
+
const keyedActors = new Map();
|
|
296
|
+
const reverseKeyedActors = new WeakMap();
|
|
297
|
+
const inspectionObservers = new Set();
|
|
298
|
+
const timerMap = {};
|
|
299
|
+
const {
|
|
300
|
+
clock,
|
|
301
|
+
logger
|
|
302
|
+
} = options;
|
|
303
|
+
const scheduler = {
|
|
304
|
+
schedule: (source, target, event, delay, id = Math.random().toString(36).slice(2)) => {
|
|
305
|
+
const scheduledEvent = {
|
|
306
|
+
source,
|
|
307
|
+
target,
|
|
308
|
+
event,
|
|
309
|
+
delay,
|
|
310
|
+
id,
|
|
311
|
+
startedAt: Date.now()
|
|
312
|
+
};
|
|
313
|
+
const scheduledEventId = createScheduledEventId(source, id);
|
|
314
|
+
system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
|
|
315
|
+
const timeout = clock.setTimeout(() => {
|
|
316
|
+
delete timerMap[scheduledEventId];
|
|
317
|
+
delete system._snapshot._scheduledEvents[scheduledEventId];
|
|
318
|
+
system._relay(source, target, event);
|
|
319
|
+
}, delay);
|
|
320
|
+
timerMap[scheduledEventId] = timeout;
|
|
321
|
+
},
|
|
322
|
+
cancel: (source, id) => {
|
|
323
|
+
const scheduledEventId = createScheduledEventId(source, id);
|
|
324
|
+
const timeout = timerMap[scheduledEventId];
|
|
325
|
+
delete timerMap[scheduledEventId];
|
|
326
|
+
delete system._snapshot._scheduledEvents[scheduledEventId];
|
|
327
|
+
clock.clearTimeout(timeout);
|
|
328
|
+
},
|
|
329
|
+
cancelAll: actorRef => {
|
|
330
|
+
for (const scheduledEventId in system._snapshot._scheduledEvents) {
|
|
331
|
+
const scheduledEvent = system._snapshot._scheduledEvents[scheduledEventId];
|
|
332
|
+
if (scheduledEvent.source === actorRef) {
|
|
333
|
+
scheduler.cancel(actorRef, scheduledEvent.id);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
const sendInspectionEvent = event => {
|
|
339
|
+
if (!inspectionObservers.size) {
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const resolvedInspectionEvent = {
|
|
343
|
+
...event,
|
|
344
|
+
rootId: rootActor.sessionId
|
|
345
|
+
};
|
|
346
|
+
inspectionObservers.forEach(observer => observer.next?.(resolvedInspectionEvent));
|
|
347
|
+
};
|
|
348
|
+
const system = {
|
|
349
|
+
_snapshot: {
|
|
350
|
+
_scheduledEvents: (options?.snapshot && options.snapshot.scheduler) ?? {}
|
|
351
|
+
},
|
|
352
|
+
_bookId: () => `x:${idCounter++}`,
|
|
353
|
+
_register: (sessionId, actorRef) => {
|
|
354
|
+
children.set(sessionId, actorRef);
|
|
355
|
+
return sessionId;
|
|
356
|
+
},
|
|
357
|
+
_unregister: actorRef => {
|
|
358
|
+
children.delete(actorRef.sessionId);
|
|
359
|
+
const systemId = reverseKeyedActors.get(actorRef);
|
|
360
|
+
if (systemId !== undefined) {
|
|
361
|
+
keyedActors.delete(systemId);
|
|
362
|
+
reverseKeyedActors.delete(actorRef);
|
|
363
|
+
}
|
|
364
|
+
},
|
|
365
|
+
get: systemId => {
|
|
366
|
+
return keyedActors.get(systemId);
|
|
367
|
+
},
|
|
368
|
+
_set: (systemId, actorRef) => {
|
|
369
|
+
const existing = keyedActors.get(systemId);
|
|
370
|
+
if (existing && existing !== actorRef) {
|
|
371
|
+
throw new Error(`Actor with system ID '${systemId}' already exists.`);
|
|
372
|
+
}
|
|
373
|
+
keyedActors.set(systemId, actorRef);
|
|
374
|
+
reverseKeyedActors.set(actorRef, systemId);
|
|
375
|
+
},
|
|
376
|
+
inspect: observerOrFn => {
|
|
377
|
+
const observer = toObserver(observerOrFn);
|
|
378
|
+
inspectionObservers.add(observer);
|
|
379
|
+
return {
|
|
380
|
+
unsubscribe() {
|
|
381
|
+
inspectionObservers.delete(observer);
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
},
|
|
385
|
+
_sendInspectionEvent: sendInspectionEvent,
|
|
386
|
+
_relay: (source, target, event) => {
|
|
387
|
+
system._sendInspectionEvent({
|
|
388
|
+
type: '@xstate.event',
|
|
389
|
+
sourceRef: source,
|
|
390
|
+
actorRef: target,
|
|
391
|
+
event
|
|
392
|
+
});
|
|
393
|
+
target._send(event);
|
|
394
|
+
},
|
|
395
|
+
scheduler,
|
|
396
|
+
getSnapshot: () => {
|
|
397
|
+
return {
|
|
398
|
+
_scheduledEvents: {
|
|
399
|
+
...system._snapshot._scheduledEvents
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
},
|
|
403
|
+
start: () => {
|
|
404
|
+
const scheduledEvents = system._snapshot._scheduledEvents;
|
|
405
|
+
system._snapshot._scheduledEvents = {};
|
|
406
|
+
for (const scheduledId in scheduledEvents) {
|
|
407
|
+
const {
|
|
408
|
+
source,
|
|
409
|
+
target,
|
|
410
|
+
event,
|
|
411
|
+
delay,
|
|
412
|
+
id
|
|
413
|
+
} = scheduledEvents[scheduledId];
|
|
414
|
+
scheduler.schedule(source, target, event, delay, id);
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
_clock: clock,
|
|
418
|
+
_logger: logger
|
|
419
|
+
};
|
|
420
|
+
return system;
|
|
421
|
+
}
|
|
422
|
+
|
|
414
423
|
const $$ACTOR_TYPE = 1;
|
|
415
424
|
// those values are currently used by @xstate/react directly so it's important to keep the assigned values in sync
|
|
416
425
|
let ProcessingStatus = /*#__PURE__*/function (ProcessingStatus) {
|
|
@@ -433,29 +442,29 @@ const defaultOptions = {
|
|
|
433
442
|
};
|
|
434
443
|
|
|
435
444
|
/**
|
|
436
|
-
* An Actor is a running process that can receive events, send events and change
|
|
445
|
+
* An Actor is a running process that can receive events, send events and change
|
|
446
|
+
* its behavior based on the events it receives, which can cause effects outside
|
|
447
|
+
* of the actor. When you run a state machine, it becomes an actor.
|
|
437
448
|
*/
|
|
438
449
|
class Actor {
|
|
439
450
|
/**
|
|
440
|
-
* Creates a new actor instance for the given logic with the provided options,
|
|
451
|
+
* Creates a new actor instance for the given logic with the provided options,
|
|
452
|
+
* if any.
|
|
441
453
|
*
|
|
442
454
|
* @param logic The logic to create an actor from
|
|
443
455
|
* @param options Actor options
|
|
444
456
|
*/
|
|
445
457
|
constructor(logic, options) {
|
|
446
458
|
this.logic = logic;
|
|
447
|
-
/**
|
|
448
|
-
* The current internal state of the actor.
|
|
449
|
-
*/
|
|
459
|
+
/** The current internal state of the actor. */
|
|
450
460
|
this._snapshot = void 0;
|
|
451
461
|
/**
|
|
452
|
-
* The clock that is responsible for setting and clearing timeouts, such as
|
|
462
|
+
* The clock that is responsible for setting and clearing timeouts, such as
|
|
463
|
+
* delayed events and transitions.
|
|
453
464
|
*/
|
|
454
465
|
this.clock = void 0;
|
|
455
466
|
this.options = void 0;
|
|
456
|
-
/**
|
|
457
|
-
* The unique identifier for this actor relative to its parent.
|
|
458
|
-
*/
|
|
467
|
+
/** The unique identifier for this actor relative to its parent. */
|
|
459
468
|
this.id = void 0;
|
|
460
469
|
this.mailbox = new Mailbox(this._process.bind(this));
|
|
461
470
|
this.observers = new Set();
|
|
@@ -471,13 +480,9 @@ class Actor {
|
|
|
471
480
|
// TODO: add typings for system
|
|
472
481
|
this._actorScope = void 0;
|
|
473
482
|
this._systemId = void 0;
|
|
474
|
-
/**
|
|
475
|
-
* The globally unique process ID for this invocation.
|
|
476
|
-
*/
|
|
483
|
+
/** The globally unique process ID for this invocation. */
|
|
477
484
|
this.sessionId = void 0;
|
|
478
|
-
/**
|
|
479
|
-
* The system to which this actor belongs.
|
|
480
|
-
*/
|
|
485
|
+
/** The system to which this actor belongs. */
|
|
481
486
|
this.system = void 0;
|
|
482
487
|
this._doneEvent = void 0;
|
|
483
488
|
this.src = void 0;
|
|
@@ -641,11 +646,15 @@ class Actor {
|
|
|
641
646
|
* Subscribe an observer to an actor’s snapshot values.
|
|
642
647
|
*
|
|
643
648
|
* @remarks
|
|
644
|
-
* The observer will receive the actor’s snapshot value when it is emitted.
|
|
649
|
+
* The observer will receive the actor’s snapshot value when it is emitted.
|
|
650
|
+
* The observer can be:
|
|
651
|
+
*
|
|
645
652
|
* - A plain function that receives the latest snapshot, or
|
|
646
|
-
* - An observer object whose `.next(snapshot)` method receives the latest
|
|
653
|
+
* - An observer object whose `.next(snapshot)` method receives the latest
|
|
654
|
+
* snapshot
|
|
647
655
|
*
|
|
648
656
|
* @example
|
|
657
|
+
*
|
|
649
658
|
* ```ts
|
|
650
659
|
* // Observer as a plain function
|
|
651
660
|
* const subscription = actor.subscribe((snapshot) => {
|
|
@@ -654,6 +663,7 @@ class Actor {
|
|
|
654
663
|
* ```
|
|
655
664
|
*
|
|
656
665
|
* @example
|
|
666
|
+
*
|
|
657
667
|
* ```ts
|
|
658
668
|
* // Observer as an object
|
|
659
669
|
* const subscription = actor.subscribe({
|
|
@@ -665,13 +675,16 @@ class Actor {
|
|
|
665
675
|
* },
|
|
666
676
|
* complete() {
|
|
667
677
|
* // ...
|
|
668
|
-
* }
|
|
678
|
+
* }
|
|
669
679
|
* });
|
|
670
680
|
* ```
|
|
671
681
|
*
|
|
672
|
-
* The return value of `actor.subscribe(observer)` is a subscription object
|
|
682
|
+
* The return value of `actor.subscribe(observer)` is a subscription object
|
|
683
|
+
* that has an `.unsubscribe()` method. You can call
|
|
684
|
+
* `subscription.unsubscribe()` to unsubscribe the observer:
|
|
673
685
|
*
|
|
674
686
|
* @example
|
|
687
|
+
*
|
|
675
688
|
* ```ts
|
|
676
689
|
* const subscription = actor.subscribe((snapshot) => {
|
|
677
690
|
* // ...
|
|
@@ -681,9 +694,12 @@ class Actor {
|
|
|
681
694
|
* subscription.unsubscribe();
|
|
682
695
|
* ```
|
|
683
696
|
*
|
|
684
|
-
* When the actor is stopped, all of its observers will automatically be
|
|
697
|
+
* When the actor is stopped, all of its observers will automatically be
|
|
698
|
+
* unsubscribed.
|
|
685
699
|
*
|
|
686
|
-
* @param observer - Either a plain function that receives the latest
|
|
700
|
+
* @param observer - Either a plain function that receives the latest
|
|
701
|
+
* snapshot, or an observer object whose `.next(snapshot)` method receives
|
|
702
|
+
* the latest snapshot
|
|
687
703
|
*/
|
|
688
704
|
|
|
689
705
|
subscribe(nextListenerOrObserver, errorListener, completeListener) {
|
|
@@ -736,9 +752,7 @@ class Actor {
|
|
|
736
752
|
};
|
|
737
753
|
}
|
|
738
754
|
|
|
739
|
-
/**
|
|
740
|
-
* Starts the Actor from the initial state
|
|
741
|
-
*/
|
|
755
|
+
/** Starts the Actor from the initial state */
|
|
742
756
|
start() {
|
|
743
757
|
if (this._processingStatus === ProcessingStatus.Running) {
|
|
744
758
|
// Do not restart the service if it is already started
|
|
@@ -854,9 +868,7 @@ class Actor {
|
|
|
854
868
|
return this;
|
|
855
869
|
}
|
|
856
870
|
|
|
857
|
-
/**
|
|
858
|
-
* Stops the Actor and unsubscribe all listeners.
|
|
859
|
-
*/
|
|
871
|
+
/** Stops the Actor and unsubscribe all listeners. */
|
|
860
872
|
stop() {
|
|
861
873
|
if (this._parent) {
|
|
862
874
|
throw new Error('A non-root actor cannot be stopped directly.');
|
|
@@ -928,9 +940,7 @@ class Actor {
|
|
|
928
940
|
return this;
|
|
929
941
|
}
|
|
930
942
|
|
|
931
|
-
/**
|
|
932
|
-
* @internal
|
|
933
|
-
*/
|
|
943
|
+
/** @internal */
|
|
934
944
|
_send(event) {
|
|
935
945
|
if (this._processingStatus === ProcessingStatus.Stopped) {
|
|
936
946
|
// do nothing
|
|
@@ -976,10 +986,11 @@ class Actor {
|
|
|
976
986
|
* @remarks
|
|
977
987
|
* The internal state can be persisted from any actor, not only machines.
|
|
978
988
|
*
|
|
979
|
-
* Note that the persisted state is not the same as the snapshot from
|
|
989
|
+
* Note that the persisted state is not the same as the snapshot from
|
|
990
|
+
* {@link Actor.getSnapshot}. Persisted state represents the internal state of
|
|
991
|
+
* the actor, while snapshots represent the actor's last emitted value.
|
|
980
992
|
*
|
|
981
993
|
* Can be restored with {@link ActorOptions.state}
|
|
982
|
-
*
|
|
983
994
|
* @see https://stately.ai/docs/persistence
|
|
984
995
|
*/
|
|
985
996
|
|
|
@@ -996,11 +1007,11 @@ class Actor {
|
|
|
996
1007
|
* @remarks
|
|
997
1008
|
* The snapshot represent an actor's last emitted value.
|
|
998
1009
|
*
|
|
999
|
-
* When an actor receives an event, its internal state may change.
|
|
1000
|
-
*
|
|
1001
|
-
*
|
|
1002
|
-
* Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
|
|
1010
|
+
* When an actor receives an event, its internal state may change. An actor
|
|
1011
|
+
* may emit a snapshot when a state transition occurs.
|
|
1003
1012
|
*
|
|
1013
|
+
* Note that some actors, such as callback actors generated with
|
|
1014
|
+
* `fromCallback`, will not emit snapshots.
|
|
1004
1015
|
* @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
|
|
1005
1016
|
* @see {@link Actor.getPersistedSnapshot} to persist the internal state of an actor (which is more than just a snapshot).
|
|
1006
1017
|
*/
|
|
@@ -1012,13 +1023,16 @@ class Actor {
|
|
|
1012
1023
|
}
|
|
1013
1024
|
}
|
|
1014
1025
|
/**
|
|
1015
|
-
* Creates a new actor instance for the given actor logic with the provided
|
|
1026
|
+
* Creates a new actor instance for the given actor logic with the provided
|
|
1027
|
+
* options, if any.
|
|
1016
1028
|
*
|
|
1017
1029
|
* @remarks
|
|
1018
|
-
* When you create an actor from actor logic via `createActor(logic)`, you
|
|
1019
|
-
*
|
|
1020
|
-
*
|
|
1030
|
+
* When you create an actor from actor logic via `createActor(logic)`, you
|
|
1031
|
+
* implicitly create an actor system where the created actor is the root actor.
|
|
1032
|
+
* Any actors spawned from this root actor and its descendants are part of that
|
|
1033
|
+
* actor system.
|
|
1021
1034
|
* @example
|
|
1035
|
+
*
|
|
1022
1036
|
* ```ts
|
|
1023
1037
|
* import { createActor } from 'xstate';
|
|
1024
1038
|
* import { someActorLogic } from './someActorLogic.ts';
|
|
@@ -1040,7 +1054,10 @@ class Actor {
|
|
|
1040
1054
|
* actor.stop();
|
|
1041
1055
|
* ```
|
|
1042
1056
|
*
|
|
1043
|
-
* @param logic - The actor logic to create an actor from. For a state machine
|
|
1057
|
+
* @param logic - The actor logic to create an actor from. For a state machine
|
|
1058
|
+
* actor logic creator, see {@link createMachine}. Other actor logic creators
|
|
1059
|
+
* include {@link fromCallback}, {@link fromEventObservable},
|
|
1060
|
+
* {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
|
|
1044
1061
|
* @param options - Actor options
|
|
1045
1062
|
*/
|
|
1046
1063
|
function createActor(logic, ...[options]) {
|
|
@@ -1048,15 +1065,14 @@ function createActor(logic, ...[options]) {
|
|
|
1048
1065
|
}
|
|
1049
1066
|
|
|
1050
1067
|
/**
|
|
1051
|
-
* Creates a new Interpreter instance for the given machine with the provided
|
|
1068
|
+
* Creates a new Interpreter instance for the given machine with the provided
|
|
1069
|
+
* options, if any.
|
|
1052
1070
|
*
|
|
1053
1071
|
* @deprecated Use `createActor` instead
|
|
1054
1072
|
*/
|
|
1055
1073
|
const interpret = createActor;
|
|
1056
1074
|
|
|
1057
|
-
/**
|
|
1058
|
-
* @deprecated Use `Actor` instead.
|
|
1059
|
-
*/
|
|
1075
|
+
/** @deprecated Use `Actor` instead. */
|
|
1060
1076
|
|
|
1061
1077
|
function resolveCancel(_, snapshot, actionArgs, actionParams, {
|
|
1062
1078
|
sendId
|
|
@@ -1070,30 +1086,36 @@ function executeCancel(actorScope, resolvedSendId) {
|
|
|
1070
1086
|
});
|
|
1071
1087
|
}
|
|
1072
1088
|
/**
|
|
1073
|
-
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The
|
|
1074
|
-
* will not send its event or execute, unless the
|
|
1089
|
+
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The
|
|
1090
|
+
* canceled `sendTo(...)` action will not send its event or execute, unless the
|
|
1091
|
+
* `delay` has already elapsed before `cancel(...)` is called.
|
|
1075
1092
|
*
|
|
1076
|
-
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1077
|
-
*
|
|
1078
1093
|
* @example
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1094
|
+
*
|
|
1095
|
+
* ```ts
|
|
1096
|
+
* import { createMachine, sendTo, cancel } from 'xstate';
|
|
1097
|
+
*
|
|
1098
|
+
* const machine = createMachine({
|
|
1099
|
+
* // ...
|
|
1100
|
+
* on: {
|
|
1101
|
+
* sendEvent: {
|
|
1102
|
+
* actions: sendTo(
|
|
1103
|
+
* 'some-actor',
|
|
1104
|
+
* { type: 'someEvent' },
|
|
1105
|
+
* {
|
|
1106
|
+
* id: 'some-id',
|
|
1107
|
+
* delay: 1000
|
|
1108
|
+
* }
|
|
1109
|
+
* )
|
|
1110
|
+
* },
|
|
1111
|
+
* cancelEvent: {
|
|
1112
|
+
* actions: cancel('some-id')
|
|
1113
|
+
* }
|
|
1114
|
+
* }
|
|
1115
|
+
* });
|
|
1116
|
+
* ```
|
|
1117
|
+
*
|
|
1118
|
+
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1097
1119
|
*/
|
|
1098
1120
|
function cancel(sendId) {
|
|
1099
1121
|
function cancel(args, params) {
|
|
@@ -1275,30 +1297,33 @@ function checkNot(snapshot, {
|
|
|
1275
1297
|
}
|
|
1276
1298
|
|
|
1277
1299
|
/**
|
|
1278
|
-
* Higher-order guard that evaluates to `true` if the `guard` passed to it
|
|
1300
|
+
* Higher-order guard that evaluates to `true` if the `guard` passed to it
|
|
1301
|
+
* evaluates to `false`.
|
|
1279
1302
|
*
|
|
1280
1303
|
* @category Guards
|
|
1281
1304
|
* @example
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
*
|
|
1305
|
+
*
|
|
1306
|
+
* ```ts
|
|
1307
|
+
* import { setup, not } from 'xstate';
|
|
1308
|
+
*
|
|
1309
|
+
* const machine = setup({
|
|
1310
|
+
* guards: {
|
|
1311
|
+
* someNamedGuard: () => false
|
|
1312
|
+
* }
|
|
1313
|
+
* }).createMachine({
|
|
1314
|
+
* on: {
|
|
1315
|
+
* someEvent: {
|
|
1316
|
+
* guard: not('someNamedGuard'),
|
|
1317
|
+
* actions: () => {
|
|
1318
|
+
* // will be executed if guard in `not(...)`
|
|
1319
|
+
* // evaluates to `false`
|
|
1320
|
+
* }
|
|
1321
|
+
* }
|
|
1322
|
+
* }
|
|
1323
|
+
* });
|
|
1324
|
+
* ```
|
|
1325
|
+
*
|
|
1326
|
+
* @returns A guard
|
|
1302
1327
|
*/
|
|
1303
1328
|
function not(guard) {
|
|
1304
1329
|
function not(args, params) {
|
|
@@ -1325,28 +1350,27 @@ function checkAnd(snapshot, {
|
|
|
1325
1350
|
*
|
|
1326
1351
|
* @category Guards
|
|
1327
1352
|
* @example
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
```
|
|
1353
|
+
*
|
|
1354
|
+
* ```ts
|
|
1355
|
+
* import { setup, and } from 'xstate';
|
|
1356
|
+
*
|
|
1357
|
+
* const machine = setup({
|
|
1358
|
+
* guards: {
|
|
1359
|
+
* someNamedGuard: () => true
|
|
1360
|
+
* }
|
|
1361
|
+
* }).createMachine({
|
|
1362
|
+
* on: {
|
|
1363
|
+
* someEvent: {
|
|
1364
|
+
* guard: and([({ context }) => context.value > 0, 'someNamedGuard']),
|
|
1365
|
+
* actions: () => {
|
|
1366
|
+
* // will be executed if all guards in `and(...)`
|
|
1367
|
+
* // evaluate to true
|
|
1368
|
+
* }
|
|
1369
|
+
* }
|
|
1370
|
+
* }
|
|
1371
|
+
* });
|
|
1372
|
+
* ```
|
|
1373
|
+
*
|
|
1350
1374
|
* @returns A guard action object
|
|
1351
1375
|
*/
|
|
1352
1376
|
function and(guards) {
|
|
@@ -1369,33 +1393,32 @@ function checkOr(snapshot, {
|
|
|
1369
1393
|
}
|
|
1370
1394
|
|
|
1371
1395
|
/**
|
|
1372
|
-
* Higher-order guard that evaluates to `true` if any of the `guards` passed to
|
|
1373
|
-
* evaluate to `true`.
|
|
1396
|
+
* Higher-order guard that evaluates to `true` if any of the `guards` passed to
|
|
1397
|
+
* it evaluate to `true`.
|
|
1374
1398
|
*
|
|
1375
1399
|
* @category Guards
|
|
1376
1400
|
* @example
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
```
|
|
1401
|
+
*
|
|
1402
|
+
* ```ts
|
|
1403
|
+
* import { setup, or } from 'xstate';
|
|
1404
|
+
*
|
|
1405
|
+
* const machine = setup({
|
|
1406
|
+
* guards: {
|
|
1407
|
+
* someNamedGuard: () => true
|
|
1408
|
+
* }
|
|
1409
|
+
* }).createMachine({
|
|
1410
|
+
* on: {
|
|
1411
|
+
* someEvent: {
|
|
1412
|
+
* guard: or([({ context }) => context.value > 0, 'someNamedGuard']),
|
|
1413
|
+
* actions: () => {
|
|
1414
|
+
* // will be executed if any of the guards in `or(...)`
|
|
1415
|
+
* // evaluate to true
|
|
1416
|
+
* }
|
|
1417
|
+
* }
|
|
1418
|
+
* }
|
|
1419
|
+
* });
|
|
1420
|
+
* ```
|
|
1421
|
+
*
|
|
1399
1422
|
* @returns A guard action object
|
|
1400
1423
|
*/
|
|
1401
1424
|
function or(guards) {
|
|
@@ -1580,9 +1603,7 @@ function getCandidates(stateNode, receivedEventType) {
|
|
|
1580
1603
|
return candidates;
|
|
1581
1604
|
}
|
|
1582
1605
|
|
|
1583
|
-
/**
|
|
1584
|
-
* All delayed transitions from the config.
|
|
1585
|
-
*/
|
|
1606
|
+
/** All delayed transitions from the config. */
|
|
1586
1607
|
function getDelayedTransitions(stateNode) {
|
|
1587
1608
|
const afterConfig = stateNode.config.after;
|
|
1588
1609
|
if (!afterConfig) {
|
|
@@ -1774,9 +1795,7 @@ function getInitialStateNodes(stateNode) {
|
|
|
1774
1795
|
iter(stateNode);
|
|
1775
1796
|
return set;
|
|
1776
1797
|
}
|
|
1777
|
-
/**
|
|
1778
|
-
* Returns the child state node from its relative `stateKey`, or throws.
|
|
1779
|
-
*/
|
|
1798
|
+
/** Returns the child state node from its relative `stateKey`, or throws. */
|
|
1780
1799
|
function getStateNode(stateNode, stateKey) {
|
|
1781
1800
|
if (isStateId(stateKey)) {
|
|
1782
1801
|
return stateNode.machine.getStateNodeById(stateKey);
|
|
@@ -2017,9 +2036,7 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
|
|
|
2017
2036
|
return true;
|
|
2018
2037
|
}
|
|
2019
2038
|
|
|
2020
|
-
/**
|
|
2021
|
-
* https://www.w3.org/TR/scxml/#microstepProcedure
|
|
2022
|
-
*/
|
|
2039
|
+
/** https://www.w3.org/TR/scxml/#microstepProcedure */
|
|
2023
2040
|
function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
|
|
2024
2041
|
if (!transitions.length) {
|
|
2025
2042
|
return currentSnapshot;
|
|
@@ -2432,7 +2449,8 @@ function selectEventlessTransitions(nextState, event) {
|
|
|
2432
2449
|
}
|
|
2433
2450
|
|
|
2434
2451
|
/**
|
|
2435
|
-
* Resolves a partial state value with its full representation in the state
|
|
2452
|
+
* Resolves a partial state value with its full representation in the state
|
|
2453
|
+
* node's machine.
|
|
2436
2454
|
*
|
|
2437
2455
|
* @param stateValue The partial state value to resolve.
|
|
2438
2456
|
*/
|