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