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);
|
|
@@ -408,6 +283,140 @@ function getAllOwnEventDescriptors(snapshot) {
|
|
|
408
283
|
return [...new Set([...snapshot._nodes.flatMap(sn => sn.ownEvents)])];
|
|
409
284
|
}
|
|
410
285
|
|
|
286
|
+
function createScheduledEventId(actorRef, id) {
|
|
287
|
+
return `${actorRef.sessionId}.${id}`;
|
|
288
|
+
}
|
|
289
|
+
let idCounter = 0;
|
|
290
|
+
function createSystem(rootActor, options) {
|
|
291
|
+
const children = new Map();
|
|
292
|
+
const keyedActors = new Map();
|
|
293
|
+
const reverseKeyedActors = new WeakMap();
|
|
294
|
+
const inspectionObservers = new Set();
|
|
295
|
+
const timerMap = {};
|
|
296
|
+
const {
|
|
297
|
+
clock,
|
|
298
|
+
logger
|
|
299
|
+
} = options;
|
|
300
|
+
const scheduler = {
|
|
301
|
+
schedule: (source, target, event, delay, id = Math.random().toString(36).slice(2)) => {
|
|
302
|
+
const scheduledEvent = {
|
|
303
|
+
source,
|
|
304
|
+
target,
|
|
305
|
+
event,
|
|
306
|
+
delay,
|
|
307
|
+
id,
|
|
308
|
+
startedAt: Date.now()
|
|
309
|
+
};
|
|
310
|
+
const scheduledEventId = createScheduledEventId(source, id);
|
|
311
|
+
system._snapshot._scheduledEvents[scheduledEventId] = scheduledEvent;
|
|
312
|
+
const timeout = clock.setTimeout(() => {
|
|
313
|
+
delete timerMap[scheduledEventId];
|
|
314
|
+
delete system._snapshot._scheduledEvents[scheduledEventId];
|
|
315
|
+
system._relay(source, target, event);
|
|
316
|
+
}, delay);
|
|
317
|
+
timerMap[scheduledEventId] = timeout;
|
|
318
|
+
},
|
|
319
|
+
cancel: (source, id) => {
|
|
320
|
+
const scheduledEventId = createScheduledEventId(source, id);
|
|
321
|
+
const timeout = timerMap[scheduledEventId];
|
|
322
|
+
delete timerMap[scheduledEventId];
|
|
323
|
+
delete system._snapshot._scheduledEvents[scheduledEventId];
|
|
324
|
+
clock.clearTimeout(timeout);
|
|
325
|
+
},
|
|
326
|
+
cancelAll: actorRef => {
|
|
327
|
+
for (const scheduledEventId in system._snapshot._scheduledEvents) {
|
|
328
|
+
const scheduledEvent = system._snapshot._scheduledEvents[scheduledEventId];
|
|
329
|
+
if (scheduledEvent.source === actorRef) {
|
|
330
|
+
scheduler.cancel(actorRef, scheduledEvent.id);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
const sendInspectionEvent = event => {
|
|
336
|
+
if (!inspectionObservers.size) {
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
const resolvedInspectionEvent = {
|
|
340
|
+
...event,
|
|
341
|
+
rootId: rootActor.sessionId
|
|
342
|
+
};
|
|
343
|
+
inspectionObservers.forEach(observer => observer.next?.(resolvedInspectionEvent));
|
|
344
|
+
};
|
|
345
|
+
const system = {
|
|
346
|
+
_snapshot: {
|
|
347
|
+
_scheduledEvents: (options?.snapshot && options.snapshot.scheduler) ?? {}
|
|
348
|
+
},
|
|
349
|
+
_bookId: () => `x:${idCounter++}`,
|
|
350
|
+
_register: (sessionId, actorRef) => {
|
|
351
|
+
children.set(sessionId, actorRef);
|
|
352
|
+
return sessionId;
|
|
353
|
+
},
|
|
354
|
+
_unregister: actorRef => {
|
|
355
|
+
children.delete(actorRef.sessionId);
|
|
356
|
+
const systemId = reverseKeyedActors.get(actorRef);
|
|
357
|
+
if (systemId !== undefined) {
|
|
358
|
+
keyedActors.delete(systemId);
|
|
359
|
+
reverseKeyedActors.delete(actorRef);
|
|
360
|
+
}
|
|
361
|
+
},
|
|
362
|
+
get: systemId => {
|
|
363
|
+
return keyedActors.get(systemId);
|
|
364
|
+
},
|
|
365
|
+
_set: (systemId, actorRef) => {
|
|
366
|
+
const existing = keyedActors.get(systemId);
|
|
367
|
+
if (existing && existing !== actorRef) {
|
|
368
|
+
throw new Error(`Actor with system ID '${systemId}' already exists.`);
|
|
369
|
+
}
|
|
370
|
+
keyedActors.set(systemId, actorRef);
|
|
371
|
+
reverseKeyedActors.set(actorRef, systemId);
|
|
372
|
+
},
|
|
373
|
+
inspect: observerOrFn => {
|
|
374
|
+
const observer = toObserver(observerOrFn);
|
|
375
|
+
inspectionObservers.add(observer);
|
|
376
|
+
return {
|
|
377
|
+
unsubscribe() {
|
|
378
|
+
inspectionObservers.delete(observer);
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
},
|
|
382
|
+
_sendInspectionEvent: sendInspectionEvent,
|
|
383
|
+
_relay: (source, target, event) => {
|
|
384
|
+
system._sendInspectionEvent({
|
|
385
|
+
type: '@xstate.event',
|
|
386
|
+
sourceRef: source,
|
|
387
|
+
actorRef: target,
|
|
388
|
+
event
|
|
389
|
+
});
|
|
390
|
+
target._send(event);
|
|
391
|
+
},
|
|
392
|
+
scheduler,
|
|
393
|
+
getSnapshot: () => {
|
|
394
|
+
return {
|
|
395
|
+
_scheduledEvents: {
|
|
396
|
+
...system._snapshot._scheduledEvents
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
},
|
|
400
|
+
start: () => {
|
|
401
|
+
const scheduledEvents = system._snapshot._scheduledEvents;
|
|
402
|
+
system._snapshot._scheduledEvents = {};
|
|
403
|
+
for (const scheduledId in scheduledEvents) {
|
|
404
|
+
const {
|
|
405
|
+
source,
|
|
406
|
+
target,
|
|
407
|
+
event,
|
|
408
|
+
delay,
|
|
409
|
+
id
|
|
410
|
+
} = scheduledEvents[scheduledId];
|
|
411
|
+
scheduler.schedule(source, target, event, delay, id);
|
|
412
|
+
}
|
|
413
|
+
},
|
|
414
|
+
_clock: clock,
|
|
415
|
+
_logger: logger
|
|
416
|
+
};
|
|
417
|
+
return system;
|
|
418
|
+
}
|
|
419
|
+
|
|
411
420
|
const $$ACTOR_TYPE = 1;
|
|
412
421
|
// those values are currently used by @xstate/react directly so it's important to keep the assigned values in sync
|
|
413
422
|
let ProcessingStatus = /*#__PURE__*/function (ProcessingStatus) {
|
|
@@ -430,29 +439,29 @@ const defaultOptions = {
|
|
|
430
439
|
};
|
|
431
440
|
|
|
432
441
|
/**
|
|
433
|
-
* An Actor is a running process that can receive events, send events and change
|
|
442
|
+
* An Actor is a running process that can receive events, send events and change
|
|
443
|
+
* its behavior based on the events it receives, which can cause effects outside
|
|
444
|
+
* of the actor. When you run a state machine, it becomes an actor.
|
|
434
445
|
*/
|
|
435
446
|
class Actor {
|
|
436
447
|
/**
|
|
437
|
-
* Creates a new actor instance for the given logic with the provided options,
|
|
448
|
+
* Creates a new actor instance for the given logic with the provided options,
|
|
449
|
+
* if any.
|
|
438
450
|
*
|
|
439
451
|
* @param logic The logic to create an actor from
|
|
440
452
|
* @param options Actor options
|
|
441
453
|
*/
|
|
442
454
|
constructor(logic, options) {
|
|
443
455
|
this.logic = logic;
|
|
444
|
-
/**
|
|
445
|
-
* The current internal state of the actor.
|
|
446
|
-
*/
|
|
456
|
+
/** The current internal state of the actor. */
|
|
447
457
|
this._snapshot = void 0;
|
|
448
458
|
/**
|
|
449
|
-
* The clock that is responsible for setting and clearing timeouts, such as
|
|
459
|
+
* The clock that is responsible for setting and clearing timeouts, such as
|
|
460
|
+
* delayed events and transitions.
|
|
450
461
|
*/
|
|
451
462
|
this.clock = void 0;
|
|
452
463
|
this.options = void 0;
|
|
453
|
-
/**
|
|
454
|
-
* The unique identifier for this actor relative to its parent.
|
|
455
|
-
*/
|
|
464
|
+
/** The unique identifier for this actor relative to its parent. */
|
|
456
465
|
this.id = void 0;
|
|
457
466
|
this.mailbox = new Mailbox(this._process.bind(this));
|
|
458
467
|
this.observers = new Set();
|
|
@@ -468,13 +477,9 @@ class Actor {
|
|
|
468
477
|
// TODO: add typings for system
|
|
469
478
|
this._actorScope = void 0;
|
|
470
479
|
this._systemId = void 0;
|
|
471
|
-
/**
|
|
472
|
-
* The globally unique process ID for this invocation.
|
|
473
|
-
*/
|
|
480
|
+
/** The globally unique process ID for this invocation. */
|
|
474
481
|
this.sessionId = void 0;
|
|
475
|
-
/**
|
|
476
|
-
* The system to which this actor belongs.
|
|
477
|
-
*/
|
|
482
|
+
/** The system to which this actor belongs. */
|
|
478
483
|
this.system = void 0;
|
|
479
484
|
this._doneEvent = void 0;
|
|
480
485
|
this.src = void 0;
|
|
@@ -638,11 +643,15 @@ class Actor {
|
|
|
638
643
|
* Subscribe an observer to an actor’s snapshot values.
|
|
639
644
|
*
|
|
640
645
|
* @remarks
|
|
641
|
-
* The observer will receive the actor’s snapshot value when it is emitted.
|
|
646
|
+
* The observer will receive the actor’s snapshot value when it is emitted.
|
|
647
|
+
* The observer can be:
|
|
648
|
+
*
|
|
642
649
|
* - A plain function that receives the latest snapshot, or
|
|
643
|
-
* - An observer object whose `.next(snapshot)` method receives the latest
|
|
650
|
+
* - An observer object whose `.next(snapshot)` method receives the latest
|
|
651
|
+
* snapshot
|
|
644
652
|
*
|
|
645
653
|
* @example
|
|
654
|
+
*
|
|
646
655
|
* ```ts
|
|
647
656
|
* // Observer as a plain function
|
|
648
657
|
* const subscription = actor.subscribe((snapshot) => {
|
|
@@ -651,6 +660,7 @@ class Actor {
|
|
|
651
660
|
* ```
|
|
652
661
|
*
|
|
653
662
|
* @example
|
|
663
|
+
*
|
|
654
664
|
* ```ts
|
|
655
665
|
* // Observer as an object
|
|
656
666
|
* const subscription = actor.subscribe({
|
|
@@ -662,13 +672,16 @@ class Actor {
|
|
|
662
672
|
* },
|
|
663
673
|
* complete() {
|
|
664
674
|
* // ...
|
|
665
|
-
* }
|
|
675
|
+
* }
|
|
666
676
|
* });
|
|
667
677
|
* ```
|
|
668
678
|
*
|
|
669
|
-
* The return value of `actor.subscribe(observer)` is a subscription object
|
|
679
|
+
* The return value of `actor.subscribe(observer)` is a subscription object
|
|
680
|
+
* that has an `.unsubscribe()` method. You can call
|
|
681
|
+
* `subscription.unsubscribe()` to unsubscribe the observer:
|
|
670
682
|
*
|
|
671
683
|
* @example
|
|
684
|
+
*
|
|
672
685
|
* ```ts
|
|
673
686
|
* const subscription = actor.subscribe((snapshot) => {
|
|
674
687
|
* // ...
|
|
@@ -678,9 +691,12 @@ class Actor {
|
|
|
678
691
|
* subscription.unsubscribe();
|
|
679
692
|
* ```
|
|
680
693
|
*
|
|
681
|
-
* When the actor is stopped, all of its observers will automatically be
|
|
694
|
+
* When the actor is stopped, all of its observers will automatically be
|
|
695
|
+
* unsubscribed.
|
|
682
696
|
*
|
|
683
|
-
* @param observer - Either a plain function that receives the latest
|
|
697
|
+
* @param observer - Either a plain function that receives the latest
|
|
698
|
+
* snapshot, or an observer object whose `.next(snapshot)` method receives
|
|
699
|
+
* the latest snapshot
|
|
684
700
|
*/
|
|
685
701
|
|
|
686
702
|
subscribe(nextListenerOrObserver, errorListener, completeListener) {
|
|
@@ -733,9 +749,7 @@ class Actor {
|
|
|
733
749
|
};
|
|
734
750
|
}
|
|
735
751
|
|
|
736
|
-
/**
|
|
737
|
-
* Starts the Actor from the initial state
|
|
738
|
-
*/
|
|
752
|
+
/** Starts the Actor from the initial state */
|
|
739
753
|
start() {
|
|
740
754
|
if (this._processingStatus === ProcessingStatus.Running) {
|
|
741
755
|
// Do not restart the service if it is already started
|
|
@@ -851,9 +865,7 @@ class Actor {
|
|
|
851
865
|
return this;
|
|
852
866
|
}
|
|
853
867
|
|
|
854
|
-
/**
|
|
855
|
-
* Stops the Actor and unsubscribe all listeners.
|
|
856
|
-
*/
|
|
868
|
+
/** Stops the Actor and unsubscribe all listeners. */
|
|
857
869
|
stop() {
|
|
858
870
|
if (this._parent) {
|
|
859
871
|
throw new Error('A non-root actor cannot be stopped directly.');
|
|
@@ -925,9 +937,7 @@ class Actor {
|
|
|
925
937
|
return this;
|
|
926
938
|
}
|
|
927
939
|
|
|
928
|
-
/**
|
|
929
|
-
* @internal
|
|
930
|
-
*/
|
|
940
|
+
/** @internal */
|
|
931
941
|
_send(event) {
|
|
932
942
|
if (this._processingStatus === ProcessingStatus.Stopped) {
|
|
933
943
|
return;
|
|
@@ -965,10 +975,11 @@ class Actor {
|
|
|
965
975
|
* @remarks
|
|
966
976
|
* The internal state can be persisted from any actor, not only machines.
|
|
967
977
|
*
|
|
968
|
-
* Note that the persisted state is not the same as the snapshot from
|
|
978
|
+
* Note that the persisted state is not the same as the snapshot from
|
|
979
|
+
* {@link Actor.getSnapshot}. Persisted state represents the internal state of
|
|
980
|
+
* the actor, while snapshots represent the actor's last emitted value.
|
|
969
981
|
*
|
|
970
982
|
* Can be restored with {@link ActorOptions.state}
|
|
971
|
-
*
|
|
972
983
|
* @see https://stately.ai/docs/persistence
|
|
973
984
|
*/
|
|
974
985
|
|
|
@@ -985,11 +996,11 @@ class Actor {
|
|
|
985
996
|
* @remarks
|
|
986
997
|
* The snapshot represent an actor's last emitted value.
|
|
987
998
|
*
|
|
988
|
-
* When an actor receives an event, its internal state may change.
|
|
989
|
-
*
|
|
990
|
-
*
|
|
991
|
-
* Note that some actors, such as callback actors generated with `fromCallback`, will not emit snapshots.
|
|
999
|
+
* When an actor receives an event, its internal state may change. An actor
|
|
1000
|
+
* may emit a snapshot when a state transition occurs.
|
|
992
1001
|
*
|
|
1002
|
+
* Note that some actors, such as callback actors generated with
|
|
1003
|
+
* `fromCallback`, will not emit snapshots.
|
|
993
1004
|
* @see {@link Actor.subscribe} to subscribe to an actor’s snapshot values.
|
|
994
1005
|
* @see {@link Actor.getPersistedSnapshot} to persist the internal state of an actor (which is more than just a snapshot).
|
|
995
1006
|
*/
|
|
@@ -998,13 +1009,16 @@ class Actor {
|
|
|
998
1009
|
}
|
|
999
1010
|
}
|
|
1000
1011
|
/**
|
|
1001
|
-
* Creates a new actor instance for the given actor logic with the provided
|
|
1012
|
+
* Creates a new actor instance for the given actor logic with the provided
|
|
1013
|
+
* options, if any.
|
|
1002
1014
|
*
|
|
1003
1015
|
* @remarks
|
|
1004
|
-
* When you create an actor from actor logic via `createActor(logic)`, you
|
|
1005
|
-
*
|
|
1006
|
-
*
|
|
1016
|
+
* When you create an actor from actor logic via `createActor(logic)`, you
|
|
1017
|
+
* implicitly create an actor system where the created actor is the root actor.
|
|
1018
|
+
* Any actors spawned from this root actor and its descendants are part of that
|
|
1019
|
+
* actor system.
|
|
1007
1020
|
* @example
|
|
1021
|
+
*
|
|
1008
1022
|
* ```ts
|
|
1009
1023
|
* import { createActor } from 'xstate';
|
|
1010
1024
|
* import { someActorLogic } from './someActorLogic.ts';
|
|
@@ -1026,7 +1040,10 @@ class Actor {
|
|
|
1026
1040
|
* actor.stop();
|
|
1027
1041
|
* ```
|
|
1028
1042
|
*
|
|
1029
|
-
* @param logic - The actor logic to create an actor from. For a state machine
|
|
1043
|
+
* @param logic - The actor logic to create an actor from. For a state machine
|
|
1044
|
+
* actor logic creator, see {@link createMachine}. Other actor logic creators
|
|
1045
|
+
* include {@link fromCallback}, {@link fromEventObservable},
|
|
1046
|
+
* {@link fromObservable}, {@link fromPromise}, and {@link fromTransition}.
|
|
1030
1047
|
* @param options - Actor options
|
|
1031
1048
|
*/
|
|
1032
1049
|
function createActor(logic, ...[options]) {
|
|
@@ -1034,15 +1051,14 @@ function createActor(logic, ...[options]) {
|
|
|
1034
1051
|
}
|
|
1035
1052
|
|
|
1036
1053
|
/**
|
|
1037
|
-
* Creates a new Interpreter instance for the given machine with the provided
|
|
1054
|
+
* Creates a new Interpreter instance for the given machine with the provided
|
|
1055
|
+
* options, if any.
|
|
1038
1056
|
*
|
|
1039
1057
|
* @deprecated Use `createActor` instead
|
|
1040
1058
|
*/
|
|
1041
1059
|
const interpret = createActor;
|
|
1042
1060
|
|
|
1043
|
-
/**
|
|
1044
|
-
* @deprecated Use `Actor` instead.
|
|
1045
|
-
*/
|
|
1061
|
+
/** @deprecated Use `Actor` instead. */
|
|
1046
1062
|
|
|
1047
1063
|
function resolveCancel(_, snapshot, actionArgs, actionParams, {
|
|
1048
1064
|
sendId
|
|
@@ -1056,30 +1072,36 @@ function executeCancel(actorScope, resolvedSendId) {
|
|
|
1056
1072
|
});
|
|
1057
1073
|
}
|
|
1058
1074
|
/**
|
|
1059
|
-
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The
|
|
1060
|
-
* will not send its event or execute, unless the
|
|
1075
|
+
* Cancels a delayed `sendTo(...)` action that is waiting to be executed. The
|
|
1076
|
+
* canceled `sendTo(...)` action will not send its event or execute, unless the
|
|
1077
|
+
* `delay` has already elapsed before `cancel(...)` is called.
|
|
1061
1078
|
*
|
|
1062
|
-
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1063
|
-
*
|
|
1064
1079
|
* @example
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1080
|
+
*
|
|
1081
|
+
* ```ts
|
|
1082
|
+
* import { createMachine, sendTo, cancel } from 'xstate';
|
|
1083
|
+
*
|
|
1084
|
+
* const machine = createMachine({
|
|
1085
|
+
* // ...
|
|
1086
|
+
* on: {
|
|
1087
|
+
* sendEvent: {
|
|
1088
|
+
* actions: sendTo(
|
|
1089
|
+
* 'some-actor',
|
|
1090
|
+
* { type: 'someEvent' },
|
|
1091
|
+
* {
|
|
1092
|
+
* id: 'some-id',
|
|
1093
|
+
* delay: 1000
|
|
1094
|
+
* }
|
|
1095
|
+
* )
|
|
1096
|
+
* },
|
|
1097
|
+
* cancelEvent: {
|
|
1098
|
+
* actions: cancel('some-id')
|
|
1099
|
+
* }
|
|
1100
|
+
* }
|
|
1101
|
+
* });
|
|
1102
|
+
* ```
|
|
1103
|
+
*
|
|
1104
|
+
* @param sendId The `id` of the `sendTo(...)` action to cancel.
|
|
1083
1105
|
*/
|
|
1084
1106
|
function cancel(sendId) {
|
|
1085
1107
|
function cancel(args, params) {
|
|
@@ -1247,30 +1269,33 @@ function checkNot(snapshot, {
|
|
|
1247
1269
|
}
|
|
1248
1270
|
|
|
1249
1271
|
/**
|
|
1250
|
-
* Higher-order guard that evaluates to `true` if the `guard` passed to it
|
|
1272
|
+
* Higher-order guard that evaluates to `true` if the `guard` passed to it
|
|
1273
|
+
* evaluates to `false`.
|
|
1251
1274
|
*
|
|
1252
1275
|
* @category Guards
|
|
1253
1276
|
* @example
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
*
|
|
1277
|
+
*
|
|
1278
|
+
* ```ts
|
|
1279
|
+
* import { setup, not } from 'xstate';
|
|
1280
|
+
*
|
|
1281
|
+
* const machine = setup({
|
|
1282
|
+
* guards: {
|
|
1283
|
+
* someNamedGuard: () => false
|
|
1284
|
+
* }
|
|
1285
|
+
* }).createMachine({
|
|
1286
|
+
* on: {
|
|
1287
|
+
* someEvent: {
|
|
1288
|
+
* guard: not('someNamedGuard'),
|
|
1289
|
+
* actions: () => {
|
|
1290
|
+
* // will be executed if guard in `not(...)`
|
|
1291
|
+
* // evaluates to `false`
|
|
1292
|
+
* }
|
|
1293
|
+
* }
|
|
1294
|
+
* }
|
|
1295
|
+
* });
|
|
1296
|
+
* ```
|
|
1297
|
+
*
|
|
1298
|
+
* @returns A guard
|
|
1274
1299
|
*/
|
|
1275
1300
|
function not(guard) {
|
|
1276
1301
|
function not(args, params) {
|
|
@@ -1295,28 +1320,27 @@ function checkAnd(snapshot, {
|
|
|
1295
1320
|
*
|
|
1296
1321
|
* @category Guards
|
|
1297
1322
|
* @example
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
```
|
|
1323
|
+
*
|
|
1324
|
+
* ```ts
|
|
1325
|
+
* import { setup, and } from 'xstate';
|
|
1326
|
+
*
|
|
1327
|
+
* const machine = setup({
|
|
1328
|
+
* guards: {
|
|
1329
|
+
* someNamedGuard: () => true
|
|
1330
|
+
* }
|
|
1331
|
+
* }).createMachine({
|
|
1332
|
+
* on: {
|
|
1333
|
+
* someEvent: {
|
|
1334
|
+
* guard: and([({ context }) => context.value > 0, 'someNamedGuard']),
|
|
1335
|
+
* actions: () => {
|
|
1336
|
+
* // will be executed if all guards in `and(...)`
|
|
1337
|
+
* // evaluate to true
|
|
1338
|
+
* }
|
|
1339
|
+
* }
|
|
1340
|
+
* }
|
|
1341
|
+
* });
|
|
1342
|
+
* ```
|
|
1343
|
+
*
|
|
1320
1344
|
* @returns A guard action object
|
|
1321
1345
|
*/
|
|
1322
1346
|
function and(guards) {
|
|
@@ -1337,33 +1361,32 @@ function checkOr(snapshot, {
|
|
|
1337
1361
|
}
|
|
1338
1362
|
|
|
1339
1363
|
/**
|
|
1340
|
-
* Higher-order guard that evaluates to `true` if any of the `guards` passed to
|
|
1341
|
-
* evaluate to `true`.
|
|
1364
|
+
* Higher-order guard that evaluates to `true` if any of the `guards` passed to
|
|
1365
|
+
* it evaluate to `true`.
|
|
1342
1366
|
*
|
|
1343
1367
|
* @category Guards
|
|
1344
1368
|
* @example
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
```
|
|
1369
|
+
*
|
|
1370
|
+
* ```ts
|
|
1371
|
+
* import { setup, or } from 'xstate';
|
|
1372
|
+
*
|
|
1373
|
+
* const machine = setup({
|
|
1374
|
+
* guards: {
|
|
1375
|
+
* someNamedGuard: () => true
|
|
1376
|
+
* }
|
|
1377
|
+
* }).createMachine({
|
|
1378
|
+
* on: {
|
|
1379
|
+
* someEvent: {
|
|
1380
|
+
* guard: or([({ context }) => context.value > 0, 'someNamedGuard']),
|
|
1381
|
+
* actions: () => {
|
|
1382
|
+
* // will be executed if any of the guards in `or(...)`
|
|
1383
|
+
* // evaluate to true
|
|
1384
|
+
* }
|
|
1385
|
+
* }
|
|
1386
|
+
* }
|
|
1387
|
+
* });
|
|
1388
|
+
* ```
|
|
1389
|
+
*
|
|
1367
1390
|
* @returns A guard action object
|
|
1368
1391
|
*/
|
|
1369
1392
|
function or(guards) {
|
|
@@ -1540,9 +1563,7 @@ function getCandidates(stateNode, receivedEventType) {
|
|
|
1540
1563
|
return candidates;
|
|
1541
1564
|
}
|
|
1542
1565
|
|
|
1543
|
-
/**
|
|
1544
|
-
* All delayed transitions from the config.
|
|
1545
|
-
*/
|
|
1566
|
+
/** All delayed transitions from the config. */
|
|
1546
1567
|
function getDelayedTransitions(stateNode) {
|
|
1547
1568
|
const afterConfig = stateNode.config.after;
|
|
1548
1569
|
if (!afterConfig) {
|
|
@@ -1729,9 +1750,7 @@ function getInitialStateNodes(stateNode) {
|
|
|
1729
1750
|
iter(stateNode);
|
|
1730
1751
|
return set;
|
|
1731
1752
|
}
|
|
1732
|
-
/**
|
|
1733
|
-
* Returns the child state node from its relative `stateKey`, or throws.
|
|
1734
|
-
*/
|
|
1753
|
+
/** Returns the child state node from its relative `stateKey`, or throws. */
|
|
1735
1754
|
function getStateNode(stateNode, stateKey) {
|
|
1736
1755
|
if (isStateId(stateKey)) {
|
|
1737
1756
|
return stateNode.machine.getStateNodeById(stateKey);
|
|
@@ -1972,9 +1991,7 @@ function areStateNodeCollectionsEqual(prevStateNodes, nextStateNodeSet) {
|
|
|
1972
1991
|
return true;
|
|
1973
1992
|
}
|
|
1974
1993
|
|
|
1975
|
-
/**
|
|
1976
|
-
* https://www.w3.org/TR/scxml/#microstepProcedure
|
|
1977
|
-
*/
|
|
1994
|
+
/** https://www.w3.org/TR/scxml/#microstepProcedure */
|
|
1978
1995
|
function microstep(transitions, currentSnapshot, actorScope, event, isInitial, internalQueue) {
|
|
1979
1996
|
if (!transitions.length) {
|
|
1980
1997
|
return currentSnapshot;
|
|
@@ -2384,7 +2401,8 @@ function selectEventlessTransitions(nextState, event) {
|
|
|
2384
2401
|
}
|
|
2385
2402
|
|
|
2386
2403
|
/**
|
|
2387
|
-
* Resolves a partial state value with its full representation in the state
|
|
2404
|
+
* Resolves a partial state value with its full representation in the state
|
|
2405
|
+
* node's machine.
|
|
2388
2406
|
*
|
|
2389
2407
|
* @param stateValue The partial state value to resolve.
|
|
2390
2408
|
*/
|