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