xstate 6.0.0-alpha.22 → 6.0.0-alpha.24

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.
Files changed (34) hide show
  1. package/dist/{StateMachine-610fe929.cjs.js → StateMachine-0d7aa36b.cjs.js} +65 -64
  2. package/dist/{StateMachine-1888d034.development.cjs.js → StateMachine-27ce068a.development.cjs.js} +65 -64
  3. package/dist/{StateMachine-ddb5d007.esm.js → StateMachine-4f3ebd99.esm.js} +65 -64
  4. package/dist/{StateMachine-9fad7a2b.development.esm.js → StateMachine-f3d5f2d8.development.esm.js} +65 -64
  5. package/dist/declarations/src/StateNode.d.ts +2 -2
  6. package/dist/declarations/src/createMachineFromConfig.d.ts +1 -0
  7. package/dist/declarations/src/spawn.d.ts +3 -3
  8. package/dist/declarations/src/stateUtils.d.ts +3 -1
  9. package/dist/declarations/src/types.d.ts +32 -10
  10. package/dist/declarations/src/types.v6.d.ts +10 -8
  11. package/dist/declarations/src/utils.d.ts +1 -0
  12. package/dist/{index-434fac23.cjs.js → index-0ef8d20d.cjs.js} +204 -40
  13. package/dist/{index-44d8c2d1.development.esm.js → index-1a180503.development.esm.js} +200 -40
  14. package/dist/{index-41c0375c.development.cjs.js → index-46c96e83.development.cjs.js} +204 -40
  15. package/dist/{index-2fe11873.esm.js → index-cf2e8954.esm.js} +200 -40
  16. package/dist/xstate-actors.cjs.js +1 -1
  17. package/dist/xstate-actors.development.cjs.js +1 -1
  18. package/dist/xstate-actors.development.esm.js +1 -1
  19. package/dist/xstate-actors.esm.js +1 -1
  20. package/dist/xstate-actors.umd.min.js +1 -1
  21. package/dist/xstate-actors.umd.min.js.map +1 -1
  22. package/dist/xstate-graph.cjs.js +9 -13
  23. package/dist/xstate-graph.development.cjs.js +9 -13
  24. package/dist/xstate-graph.development.esm.js +9 -13
  25. package/dist/xstate-graph.esm.js +9 -13
  26. package/dist/xstate-graph.umd.min.js +1 -1
  27. package/dist/xstate-graph.umd.min.js.map +1 -1
  28. package/dist/xstate.cjs.js +8 -4
  29. package/dist/xstate.development.cjs.js +8 -4
  30. package/dist/xstate.development.esm.js +10 -6
  31. package/dist/xstate.esm.js +10 -6
  32. package/dist/xstate.umd.min.js +1 -1
  33. package/dist/xstate.umd.min.js.map +1 -1
  34. package/package.json +1 -1
@@ -1,45 +1,18 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-434fac23.cjs.js');
3
+ var dist_xstateActors = require('./index-0ef8d20d.cjs.js');
4
4
 
5
- function createSpawner(actorScope, {
6
- machine,
7
- context
8
- }, event, spawnedChildren) {
9
- const spawn = (src, options) => {
10
- if (typeof src === 'string') {
11
- const logic = dist_xstateActors.resolveReferencedActor(machine, src);
12
- if (!logic) {
13
- throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
14
- }
15
- const actor = actorScope.system.createActorRef(logic, {
16
- id: options?.id,
17
- parent: actorScope.self,
18
- syncSnapshot: options?.syncSnapshot,
19
- input: typeof options?.input === 'function' ? options.input({
20
- context,
21
- event,
22
- self: actorScope.self
23
- }) : options?.input,
24
- src,
25
- registryKey: options?.registryKey
26
- });
27
- spawnedChildren[actor.id] = actor;
28
- return actor;
29
- } else {
30
- const actor = actorScope.system.createActorRef(src, {
31
- id: options?.id,
32
- parent: actorScope.self,
33
- syncSnapshot: options?.syncSnapshot,
34
- input: options?.input,
35
- src,
36
- registryKey: options?.registryKey
37
- });
38
- return actor;
39
- }
40
- };
5
+ function createSpawner(actorScope, actorSources, spawnedChildren) {
41
6
  return (src, options) => {
42
- const actor = spawn(src, options); // TODO: fix types
7
+ const referencedSrc = Object.entries(actorSources).find(([, logic]) => logic === src)?.[0];
8
+ const actor = actorScope.system.createActorRef(src, {
9
+ id: options?.id,
10
+ parent: actorScope.self,
11
+ syncSnapshot: options?.syncSnapshot,
12
+ input: options?.input,
13
+ src: referencedSrc ?? src,
14
+ registryKey: options?.registryKey
15
+ });
43
16
  spawnedChildren[actor.id] = actor;
44
17
  return actor;
45
18
  };
@@ -211,8 +184,8 @@ class StateNode {
211
184
  */
212
185
  this.meta = void 0;
213
186
  /**
214
- * The output data sent with the "xstate.done.state._id_" event if this is a
215
- * final state node.
187
+ * The output data sent with the `xstate.done.state` event if this is a final
188
+ * state node.
216
189
  */
217
190
  this.output = void 0;
218
191
  /**
@@ -329,8 +302,7 @@ class StateNode {
329
302
 
330
303
  /** @internal */
331
304
  next(snapshot, event, self) {
332
- const eventType = event.type;
333
- const candidates = memo(this, `candidates-${eventType}`, () => dist_xstateActors.getCandidates(this, eventType));
305
+ const candidates = memo(this, `candidates-${dist_xstateActors.getEventDescriptorKey(event)}`, () => dist_xstateActors.getCandidates(this, event));
334
306
  for (const candidate of candidates) {
335
307
  const guardPassed = dist_xstateActors.evaluateCandidate(candidate, event, snapshot, this, self);
336
308
  if (guardPassed) {
@@ -371,6 +343,9 @@ function mapTransitionConfigs(transitionsConfig, mapper) {
371
343
  }
372
344
  function formatTransitions(stateNode) {
373
345
  const transitions = new Map();
346
+ const addTransitions = (descriptor, additions) => {
347
+ transitions.set(descriptor, [...(transitions.get(descriptor) ?? []), ...additions]);
348
+ };
374
349
  if (stateNode.config.on) {
375
350
  for (const descriptor of Object.keys(stateNode.config.on)) {
376
351
  if (descriptor === dist_xstateActors.NULL_EVENT) {
@@ -381,20 +356,30 @@ function formatTransitions(stateNode) {
381
356
  }
382
357
  }
383
358
  if (stateNode.config.onDone) {
384
- const descriptor = `xstate.done.state.${stateNode.id}`;
385
- transitions.set(descriptor, mapTransitionConfigs(stateNode.config.onDone, transition => dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
359
+ const descriptor = 'xstate.done.state';
360
+ addTransitions(descriptor, mapTransitionConfigs(stateNode.config.onDone, transition => dist_xstateActors.formatTransition(stateNode, descriptor, {
361
+ ...transition,
362
+ matches: {
363
+ ...transition.matches,
364
+ stateId: stateNode.id
365
+ }
366
+ })));
386
367
  }
387
368
  if (stateNode.config.onError) {
388
369
  const descriptor = 'xstate.error.*';
389
370
  transitions.set(descriptor, mapTransitionConfigs(stateNode.config.onError, transition => dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
390
371
  }
391
- const createCancelInvokeTimeoutTransition = (descriptor, timeoutEventType) => dist_xstateActors.formatTransition(stateNode, descriptor, {
372
+ const createCancelInvokeTimeoutTransition = (descriptor, timeoutEventId, actorId) => dist_xstateActors.formatTransition(stateNode, descriptor, {
373
+ matches: {
374
+ actorId
375
+ },
376
+ _eventMatcher: (event, snapshot) => dist_xstateActors.matchesActorSession(event, snapshot, actorId),
392
377
  to: (_args, enq) => {
393
- enq.cancel(timeoutEventType);
378
+ enq.cancel(timeoutEventId);
394
379
  return {};
395
380
  }
396
381
  });
397
- const formatInvokeCompletionTransition = (descriptor, transitionConfig, timeoutEventType) => {
382
+ const formatInvokeCompletionTransition = (descriptor, transitionConfig, timeoutEventId) => {
398
383
  const {
399
384
  target,
400
385
  to,
@@ -425,11 +410,11 @@ function formatTransitions(stateNode) {
425
410
  });
426
411
  const result = to(args, trackingEnqueue);
427
412
  if (result !== undefined || didEnqueue) {
428
- enq.cancel(timeoutEventType);
413
+ enq.cancel(timeoutEventId);
429
414
  }
430
415
  return result;
431
416
  }
432
- enq.cancel(timeoutEventType);
417
+ enq.cancel(timeoutEventId);
433
418
  return {
434
419
  target,
435
420
  reenter
@@ -438,25 +423,39 @@ function formatTransitions(stateNode) {
438
423
  });
439
424
  };
440
425
  for (const invokeDef of stateNode.invoke) {
441
- const invokeTimeoutEventType = invokeDef.timeout !== undefined ? dist_xstateActors.createInvokeTimeoutEvent(invokeDef.id).type : undefined;
426
+ const withInvokeMatch = transition => ({
427
+ ...transition,
428
+ matches: {
429
+ ...transition.matches,
430
+ actorId: invokeDef.id
431
+ },
432
+ _eventMatcher: (event, snapshot) => dist_xstateActors.matchesActorSession(event, snapshot, invokeDef.id)
433
+ });
434
+ const invokeTimeoutEventId = invokeDef.timeout !== undefined ? dist_xstateActors.createInvokeTimeoutEventId(invokeDef.id) : undefined;
442
435
  if (invokeDef.onDone) {
443
- const descriptor = `xstate.done.actor.${invokeDef.id}`;
444
- const invokeDoneTransitions = mapTransitionConfigs(invokeDef.onDone, transition => invokeTimeoutEventType ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventType) : dist_xstateActors.formatTransition(stateNode, descriptor, transition));
445
- if (invokeTimeoutEventType) {
446
- invokeDoneTransitions.push(createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventType));
436
+ const descriptor = 'xstate.done.actor';
437
+ const invokeDoneTransitions = mapTransitionConfigs(invokeDef.onDone, rawTransition => {
438
+ const transition = withInvokeMatch(rawTransition);
439
+ return invokeTimeoutEventId ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventId) : dist_xstateActors.formatTransition(stateNode, descriptor, transition);
440
+ });
441
+ if (invokeTimeoutEventId) {
442
+ invokeDoneTransitions.push(createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventId, invokeDef.id));
447
443
  }
448
- transitions.set(descriptor, invokeDoneTransitions);
449
- } else if (invokeTimeoutEventType) {
450
- const descriptor = `xstate.done.actor.${invokeDef.id}`;
451
- transitions.set(descriptor, [createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventType)]);
444
+ addTransitions(descriptor, invokeDoneTransitions);
445
+ } else if (invokeTimeoutEventId) {
446
+ const descriptor = 'xstate.done.actor';
447
+ addTransitions(descriptor, [createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventId, invokeDef.id)]);
452
448
  }
453
449
  if (invokeDef.onError) {
454
- const descriptor = `xstate.error.actor.${invokeDef.id}`;
455
- transitions.set(descriptor, mapTransitionConfigs(invokeDef.onError, transition => invokeTimeoutEventType ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventType) : dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
450
+ const descriptor = 'xstate.error.actor';
451
+ addTransitions(descriptor, mapTransitionConfigs(invokeDef.onError, rawTransition => {
452
+ const transition = withInvokeMatch(rawTransition);
453
+ return invokeTimeoutEventId ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventId) : dist_xstateActors.formatTransition(stateNode, descriptor, transition);
454
+ }));
456
455
  }
457
456
  if (invokeDef.onSnapshot) {
458
- const descriptor = `xstate.snapshot.${invokeDef.id}`;
459
- transitions.set(descriptor, mapTransitionConfigs(invokeDef.onSnapshot, transition => dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
457
+ const descriptor = 'xstate.snapshot.actor';
458
+ addTransitions(descriptor, mapTransitionConfigs(invokeDef.onSnapshot, transition => dist_xstateActors.formatTransition(stateNode, descriptor, withInvokeMatch(transition))));
460
459
  }
461
460
  }
462
461
  for (const delayedTransition of stateNode.after) {
@@ -804,7 +803,7 @@ class StateMachine {
804
803
  }, this, actorScope.self);
805
804
  if (typeof context === 'function') {
806
805
  const children = {};
807
- const spawn = createSpawner(actorScope, preInitial, initEvent, children);
806
+ const spawn = createSpawner(actorScope, this.implementations.actorSources, children);
808
807
  const resolvedContext = context({
809
808
  spawn,
810
809
  input: initEvent.input,
@@ -912,7 +911,8 @@ class StateMachine {
912
911
  const src = actorData.src;
913
912
  const logic = typeof src === 'string' ? dist_xstateActors.resolveReferencedActor(this, src) : src;
914
913
  if (!logic) {
915
- continue;
914
+ const sourceId = typeof src === 'string' ? src : '<unknown>';
915
+ throw new Error(`Unable to restore child actor '${actorId}': child source '${sourceId}' is not provided in machine '${this.id}'.`);
916
916
  }
917
917
  const actor = resolvedActorScope.system.createActorRef(logic, {
918
918
  id: actorId,
@@ -920,6 +920,7 @@ class StateMachine {
920
920
  syncSnapshot: actorData.syncSnapshot,
921
921
  snapshot: childState,
922
922
  src,
923
+ _sessionId: actorData.sessionId,
923
924
  registryKey: actorData.registryKey
924
925
  });
925
926
  // Mark so `start()` knows to start this child (freshly invoked/spawned
@@ -1,45 +1,18 @@
1
1
  'use strict';
2
2
 
3
- var dist_xstateActors = require('./index-41c0375c.development.cjs.js');
3
+ var dist_xstateActors = require('./index-46c96e83.development.cjs.js');
4
4
 
5
- function createSpawner(actorScope, {
6
- machine,
7
- context
8
- }, event, spawnedChildren) {
9
- const spawn = (src, options) => {
10
- if (typeof src === 'string') {
11
- const logic = dist_xstateActors.resolveReferencedActor(machine, src);
12
- if (!logic) {
13
- throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
14
- }
15
- const actor = actorScope.system.createActorRef(logic, {
16
- id: options?.id,
17
- parent: actorScope.self,
18
- syncSnapshot: options?.syncSnapshot,
19
- input: typeof options?.input === 'function' ? options.input({
20
- context,
21
- event,
22
- self: actorScope.self
23
- }) : options?.input,
24
- src,
25
- registryKey: options?.registryKey
26
- });
27
- spawnedChildren[actor.id] = actor;
28
- return actor;
29
- } else {
30
- const actor = actorScope.system.createActorRef(src, {
31
- id: options?.id,
32
- parent: actorScope.self,
33
- syncSnapshot: options?.syncSnapshot,
34
- input: options?.input,
35
- src,
36
- registryKey: options?.registryKey
37
- });
38
- return actor;
39
- }
40
- };
5
+ function createSpawner(actorScope, actorSources, spawnedChildren) {
41
6
  return (src, options) => {
42
- const actor = spawn(src, options); // TODO: fix types
7
+ const referencedSrc = Object.entries(actorSources).find(([, logic]) => logic === src)?.[0];
8
+ const actor = actorScope.system.createActorRef(src, {
9
+ id: options?.id,
10
+ parent: actorScope.self,
11
+ syncSnapshot: options?.syncSnapshot,
12
+ input: options?.input,
13
+ src: referencedSrc ?? src,
14
+ registryKey: options?.registryKey
15
+ });
43
16
  spawnedChildren[actor.id] = actor;
44
17
  return actor;
45
18
  };
@@ -212,8 +185,8 @@ class StateNode {
212
185
  */
213
186
  this.meta = void 0;
214
187
  /**
215
- * The output data sent with the "xstate.done.state._id_" event if this is a
216
- * final state node.
188
+ * The output data sent with the `xstate.done.state` event if this is a final
189
+ * state node.
217
190
  */
218
191
  this.output = void 0;
219
192
  /**
@@ -330,8 +303,7 @@ class StateNode {
330
303
 
331
304
  /** @internal */
332
305
  next(snapshot, event, self) {
333
- const eventType = event.type;
334
- const candidates = memo(this, `candidates-${eventType}`, () => dist_xstateActors.getCandidates(this, eventType));
306
+ const candidates = memo(this, `candidates-${dist_xstateActors.getEventDescriptorKey(event)}`, () => dist_xstateActors.getCandidates(this, event));
335
307
  for (const candidate of candidates) {
336
308
  const guardPassed = dist_xstateActors.evaluateCandidate(candidate, event, snapshot, this, self);
337
309
  if (guardPassed) {
@@ -389,6 +361,9 @@ function mapTransitionConfigs(transitionsConfig, mapper) {
389
361
  }
390
362
  function formatTransitions(stateNode) {
391
363
  const transitions = new Map();
364
+ const addTransitions = (descriptor, additions) => {
365
+ transitions.set(descriptor, [...(transitions.get(descriptor) ?? []), ...additions]);
366
+ };
392
367
  if (stateNode.config.on) {
393
368
  for (const descriptor of Object.keys(stateNode.config.on)) {
394
369
  if (descriptor === dist_xstateActors.NULL_EVENT) {
@@ -399,20 +374,30 @@ function formatTransitions(stateNode) {
399
374
  }
400
375
  }
401
376
  if (stateNode.config.onDone) {
402
- const descriptor = `xstate.done.state.${stateNode.id}`;
403
- transitions.set(descriptor, mapTransitionConfigs(stateNode.config.onDone, transition => dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
377
+ const descriptor = 'xstate.done.state';
378
+ addTransitions(descriptor, mapTransitionConfigs(stateNode.config.onDone, transition => dist_xstateActors.formatTransition(stateNode, descriptor, {
379
+ ...transition,
380
+ matches: {
381
+ ...transition.matches,
382
+ stateId: stateNode.id
383
+ }
384
+ })));
404
385
  }
405
386
  if (stateNode.config.onError) {
406
387
  const descriptor = 'xstate.error.*';
407
388
  transitions.set(descriptor, mapTransitionConfigs(stateNode.config.onError, transition => dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
408
389
  }
409
- const createCancelInvokeTimeoutTransition = (descriptor, timeoutEventType) => dist_xstateActors.formatTransition(stateNode, descriptor, {
390
+ const createCancelInvokeTimeoutTransition = (descriptor, timeoutEventId, actorId) => dist_xstateActors.formatTransition(stateNode, descriptor, {
391
+ matches: {
392
+ actorId
393
+ },
394
+ _eventMatcher: (event, snapshot) => dist_xstateActors.matchesActorSession(event, snapshot, actorId),
410
395
  to: (_args, enq) => {
411
- enq.cancel(timeoutEventType);
396
+ enq.cancel(timeoutEventId);
412
397
  return {};
413
398
  }
414
399
  });
415
- const formatInvokeCompletionTransition = (descriptor, transitionConfig, timeoutEventType) => {
400
+ const formatInvokeCompletionTransition = (descriptor, transitionConfig, timeoutEventId) => {
416
401
  const {
417
402
  target,
418
403
  to,
@@ -443,11 +428,11 @@ function formatTransitions(stateNode) {
443
428
  });
444
429
  const result = to(args, trackingEnqueue);
445
430
  if (result !== undefined || didEnqueue) {
446
- enq.cancel(timeoutEventType);
431
+ enq.cancel(timeoutEventId);
447
432
  }
448
433
  return result;
449
434
  }
450
- enq.cancel(timeoutEventType);
435
+ enq.cancel(timeoutEventId);
451
436
  return {
452
437
  target,
453
438
  reenter
@@ -456,25 +441,39 @@ function formatTransitions(stateNode) {
456
441
  });
457
442
  };
458
443
  for (const invokeDef of stateNode.invoke) {
459
- const invokeTimeoutEventType = invokeDef.timeout !== undefined ? dist_xstateActors.createInvokeTimeoutEvent(invokeDef.id).type : undefined;
444
+ const withInvokeMatch = transition => ({
445
+ ...transition,
446
+ matches: {
447
+ ...transition.matches,
448
+ actorId: invokeDef.id
449
+ },
450
+ _eventMatcher: (event, snapshot) => dist_xstateActors.matchesActorSession(event, snapshot, invokeDef.id)
451
+ });
452
+ const invokeTimeoutEventId = invokeDef.timeout !== undefined ? dist_xstateActors.createInvokeTimeoutEventId(invokeDef.id) : undefined;
460
453
  if (invokeDef.onDone) {
461
- const descriptor = `xstate.done.actor.${invokeDef.id}`;
462
- const invokeDoneTransitions = mapTransitionConfigs(invokeDef.onDone, transition => invokeTimeoutEventType ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventType) : dist_xstateActors.formatTransition(stateNode, descriptor, transition));
463
- if (invokeTimeoutEventType) {
464
- invokeDoneTransitions.push(createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventType));
454
+ const descriptor = 'xstate.done.actor';
455
+ const invokeDoneTransitions = mapTransitionConfigs(invokeDef.onDone, rawTransition => {
456
+ const transition = withInvokeMatch(rawTransition);
457
+ return invokeTimeoutEventId ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventId) : dist_xstateActors.formatTransition(stateNode, descriptor, transition);
458
+ });
459
+ if (invokeTimeoutEventId) {
460
+ invokeDoneTransitions.push(createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventId, invokeDef.id));
465
461
  }
466
- transitions.set(descriptor, invokeDoneTransitions);
467
- } else if (invokeTimeoutEventType) {
468
- const descriptor = `xstate.done.actor.${invokeDef.id}`;
469
- transitions.set(descriptor, [createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventType)]);
462
+ addTransitions(descriptor, invokeDoneTransitions);
463
+ } else if (invokeTimeoutEventId) {
464
+ const descriptor = 'xstate.done.actor';
465
+ addTransitions(descriptor, [createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventId, invokeDef.id)]);
470
466
  }
471
467
  if (invokeDef.onError) {
472
- const descriptor = `xstate.error.actor.${invokeDef.id}`;
473
- transitions.set(descriptor, mapTransitionConfigs(invokeDef.onError, transition => invokeTimeoutEventType ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventType) : dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
468
+ const descriptor = 'xstate.error.actor';
469
+ addTransitions(descriptor, mapTransitionConfigs(invokeDef.onError, rawTransition => {
470
+ const transition = withInvokeMatch(rawTransition);
471
+ return invokeTimeoutEventId ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventId) : dist_xstateActors.formatTransition(stateNode, descriptor, transition);
472
+ }));
474
473
  }
475
474
  if (invokeDef.onSnapshot) {
476
- const descriptor = `xstate.snapshot.${invokeDef.id}`;
477
- transitions.set(descriptor, mapTransitionConfigs(invokeDef.onSnapshot, transition => dist_xstateActors.formatTransition(stateNode, descriptor, transition)));
475
+ const descriptor = 'xstate.snapshot.actor';
476
+ addTransitions(descriptor, mapTransitionConfigs(invokeDef.onSnapshot, transition => dist_xstateActors.formatTransition(stateNode, descriptor, withInvokeMatch(transition))));
478
477
  }
479
478
  }
480
479
  for (const delayedTransition of stateNode.after) {
@@ -833,7 +832,7 @@ class StateMachine {
833
832
  }, this, actorScope.self);
834
833
  if (typeof context === 'function') {
835
834
  const children = {};
836
- const spawn = createSpawner(actorScope, preInitial, initEvent, children);
835
+ const spawn = createSpawner(actorScope, this.implementations.actorSources, children);
837
836
  const resolvedContext = context({
838
837
  spawn,
839
838
  input: initEvent.input,
@@ -941,7 +940,8 @@ class StateMachine {
941
940
  const src = actorData.src;
942
941
  const logic = typeof src === 'string' ? dist_xstateActors.resolveReferencedActor(this, src) : src;
943
942
  if (!logic) {
944
- continue;
943
+ const sourceId = typeof src === 'string' ? src : '<unknown>';
944
+ throw new Error(`Unable to restore child actor '${actorId}': child source '${sourceId}' is not provided in machine '${this.id}'.`);
945
945
  }
946
946
  const actor = resolvedActorScope.system.createActorRef(logic, {
947
947
  id: actorId,
@@ -949,6 +949,7 @@ class StateMachine {
949
949
  syncSnapshot: actorData.syncSnapshot,
950
950
  snapshot: childState,
951
951
  src,
952
+ _sessionId: actorData.sessionId,
952
953
  registryKey: actorData.registryKey
953
954
  });
954
955
  // Mark so `start()` knows to start this child (freshly invoked/spawned
@@ -1,43 +1,16 @@
1
- import { M as resolveReferencedActor, N as setSnapshotActorRef, O as getSnapshotActorRef, P as createSnapshotSystem, q as isMachineSnapshot, h as createActor, S as STATE_DELIMITER, Q as mapValues, t as toArray, R as createInvokeId, U as getDelayedTransitions, V as evaluateCandidate, W as formatTransition, Y as NULL_EVENT, Z as toTransitionConfigArray, _ as createInvokeTimeoutEvent, $ as getCandidates, a0 as formatRouteTransitions, a1 as resolveStateValue, a2 as getAllStateNodes, s as getStateNodes, a3 as createMachineSnapshot, a4 as isInFinalState, j as macrostep, a5 as cloneMachineSnapshot, a6 as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, a7 as getTransitionResult, a8 as hasEffect, a9 as createErrorPlatformEvent, r as resolveActionsWithContext, k as createInitEvent, l as createSpawnEffect, n as initialMicrostep, aa as toStatePath, ab as isStateId, ac as getStateNodeByPath, ad as getPersistedSnapshot, ae as $$ACTOR_TYPE } from './index-2fe11873.esm.js';
1
+ import { M as setSnapshotActorRef, N as getSnapshotActorRef, O as createSnapshotSystem, q as isMachineSnapshot, h as createActor, S as STATE_DELIMITER, P as mapValues, t as toArray, Q as createInvokeId, R as getDelayedTransitions, U as getEventDescriptorKey, V as evaluateCandidate, W as formatTransition, Y as NULL_EVENT, Z as toTransitionConfigArray, _ as createInvokeTimeoutEventId, $ as getCandidates, a0 as matchesActorSession, a1 as formatRouteTransitions, a2 as resolveStateValue, a3 as getAllStateNodes, s as getStateNodes, a4 as createMachineSnapshot, a5 as isInFinalState, j as macrostep, a6 as cloneMachineSnapshot, a7 as transitionNode, e as XSTATE_TIMER, m as matchesEventDescriptor, a8 as getTransitionResult, a9 as hasEffect, aa as createErrorPlatformEvent, r as resolveActionsWithContext, k as createInitEvent, l as createSpawnEffect, n as initialMicrostep, ab as toStatePath, ac as isStateId, ad as getStateNodeByPath, ae as getPersistedSnapshot, af as resolveReferencedActor, ag as $$ACTOR_TYPE } from './index-cf2e8954.esm.js';
2
2
 
3
- function createSpawner(actorScope, {
4
- machine,
5
- context
6
- }, event, spawnedChildren) {
7
- const spawn = (src, options) => {
8
- if (typeof src === 'string') {
9
- const logic = resolveReferencedActor(machine, src);
10
- if (!logic) {
11
- throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
12
- }
13
- const actor = actorScope.system.createActorRef(logic, {
14
- id: options?.id,
15
- parent: actorScope.self,
16
- syncSnapshot: options?.syncSnapshot,
17
- input: typeof options?.input === 'function' ? options.input({
18
- context,
19
- event,
20
- self: actorScope.self
21
- }) : options?.input,
22
- src,
23
- registryKey: options?.registryKey
24
- });
25
- spawnedChildren[actor.id] = actor;
26
- return actor;
27
- } else {
28
- const actor = actorScope.system.createActorRef(src, {
29
- id: options?.id,
30
- parent: actorScope.self,
31
- syncSnapshot: options?.syncSnapshot,
32
- input: options?.input,
33
- src,
34
- registryKey: options?.registryKey
35
- });
36
- return actor;
37
- }
38
- };
3
+ function createSpawner(actorScope, actorSources, spawnedChildren) {
39
4
  return (src, options) => {
40
- const actor = spawn(src, options); // TODO: fix types
5
+ const referencedSrc = Object.entries(actorSources).find(([, logic]) => logic === src)?.[0];
6
+ const actor = actorScope.system.createActorRef(src, {
7
+ id: options?.id,
8
+ parent: actorScope.self,
9
+ syncSnapshot: options?.syncSnapshot,
10
+ input: options?.input,
11
+ src: referencedSrc ?? src,
12
+ registryKey: options?.registryKey
13
+ });
41
14
  spawnedChildren[actor.id] = actor;
42
15
  return actor;
43
16
  };
@@ -209,8 +182,8 @@ class StateNode {
209
182
  */
210
183
  this.meta = void 0;
211
184
  /**
212
- * The output data sent with the "xstate.done.state._id_" event if this is a
213
- * final state node.
185
+ * The output data sent with the `xstate.done.state` event if this is a final
186
+ * state node.
214
187
  */
215
188
  this.output = void 0;
216
189
  /**
@@ -327,8 +300,7 @@ class StateNode {
327
300
 
328
301
  /** @internal */
329
302
  next(snapshot, event, self) {
330
- const eventType = event.type;
331
- const candidates = memo(this, `candidates-${eventType}`, () => getCandidates(this, eventType));
303
+ const candidates = memo(this, `candidates-${getEventDescriptorKey(event)}`, () => getCandidates(this, event));
332
304
  for (const candidate of candidates) {
333
305
  const guardPassed = evaluateCandidate(candidate, event, snapshot, this, self);
334
306
  if (guardPassed) {
@@ -369,6 +341,9 @@ function mapTransitionConfigs(transitionsConfig, mapper) {
369
341
  }
370
342
  function formatTransitions(stateNode) {
371
343
  const transitions = new Map();
344
+ const addTransitions = (descriptor, additions) => {
345
+ transitions.set(descriptor, [...(transitions.get(descriptor) ?? []), ...additions]);
346
+ };
372
347
  if (stateNode.config.on) {
373
348
  for (const descriptor of Object.keys(stateNode.config.on)) {
374
349
  if (descriptor === NULL_EVENT) {
@@ -379,20 +354,30 @@ function formatTransitions(stateNode) {
379
354
  }
380
355
  }
381
356
  if (stateNode.config.onDone) {
382
- const descriptor = `xstate.done.state.${stateNode.id}`;
383
- transitions.set(descriptor, mapTransitionConfigs(stateNode.config.onDone, transition => formatTransition(stateNode, descriptor, transition)));
357
+ const descriptor = 'xstate.done.state';
358
+ addTransitions(descriptor, mapTransitionConfigs(stateNode.config.onDone, transition => formatTransition(stateNode, descriptor, {
359
+ ...transition,
360
+ matches: {
361
+ ...transition.matches,
362
+ stateId: stateNode.id
363
+ }
364
+ })));
384
365
  }
385
366
  if (stateNode.config.onError) {
386
367
  const descriptor = 'xstate.error.*';
387
368
  transitions.set(descriptor, mapTransitionConfigs(stateNode.config.onError, transition => formatTransition(stateNode, descriptor, transition)));
388
369
  }
389
- const createCancelInvokeTimeoutTransition = (descriptor, timeoutEventType) => formatTransition(stateNode, descriptor, {
370
+ const createCancelInvokeTimeoutTransition = (descriptor, timeoutEventId, actorId) => formatTransition(stateNode, descriptor, {
371
+ matches: {
372
+ actorId
373
+ },
374
+ _eventMatcher: (event, snapshot) => matchesActorSession(event, snapshot, actorId),
390
375
  to: (_args, enq) => {
391
- enq.cancel(timeoutEventType);
376
+ enq.cancel(timeoutEventId);
392
377
  return {};
393
378
  }
394
379
  });
395
- const formatInvokeCompletionTransition = (descriptor, transitionConfig, timeoutEventType) => {
380
+ const formatInvokeCompletionTransition = (descriptor, transitionConfig, timeoutEventId) => {
396
381
  const {
397
382
  target,
398
383
  to,
@@ -423,11 +408,11 @@ function formatTransitions(stateNode) {
423
408
  });
424
409
  const result = to(args, trackingEnqueue);
425
410
  if (result !== undefined || didEnqueue) {
426
- enq.cancel(timeoutEventType);
411
+ enq.cancel(timeoutEventId);
427
412
  }
428
413
  return result;
429
414
  }
430
- enq.cancel(timeoutEventType);
415
+ enq.cancel(timeoutEventId);
431
416
  return {
432
417
  target,
433
418
  reenter
@@ -436,25 +421,39 @@ function formatTransitions(stateNode) {
436
421
  });
437
422
  };
438
423
  for (const invokeDef of stateNode.invoke) {
439
- const invokeTimeoutEventType = invokeDef.timeout !== undefined ? createInvokeTimeoutEvent(invokeDef.id).type : undefined;
424
+ const withInvokeMatch = transition => ({
425
+ ...transition,
426
+ matches: {
427
+ ...transition.matches,
428
+ actorId: invokeDef.id
429
+ },
430
+ _eventMatcher: (event, snapshot) => matchesActorSession(event, snapshot, invokeDef.id)
431
+ });
432
+ const invokeTimeoutEventId = invokeDef.timeout !== undefined ? createInvokeTimeoutEventId(invokeDef.id) : undefined;
440
433
  if (invokeDef.onDone) {
441
- const descriptor = `xstate.done.actor.${invokeDef.id}`;
442
- const invokeDoneTransitions = mapTransitionConfigs(invokeDef.onDone, transition => invokeTimeoutEventType ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventType) : formatTransition(stateNode, descriptor, transition));
443
- if (invokeTimeoutEventType) {
444
- invokeDoneTransitions.push(createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventType));
434
+ const descriptor = 'xstate.done.actor';
435
+ const invokeDoneTransitions = mapTransitionConfigs(invokeDef.onDone, rawTransition => {
436
+ const transition = withInvokeMatch(rawTransition);
437
+ return invokeTimeoutEventId ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventId) : formatTransition(stateNode, descriptor, transition);
438
+ });
439
+ if (invokeTimeoutEventId) {
440
+ invokeDoneTransitions.push(createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventId, invokeDef.id));
445
441
  }
446
- transitions.set(descriptor, invokeDoneTransitions);
447
- } else if (invokeTimeoutEventType) {
448
- const descriptor = `xstate.done.actor.${invokeDef.id}`;
449
- transitions.set(descriptor, [createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventType)]);
442
+ addTransitions(descriptor, invokeDoneTransitions);
443
+ } else if (invokeTimeoutEventId) {
444
+ const descriptor = 'xstate.done.actor';
445
+ addTransitions(descriptor, [createCancelInvokeTimeoutTransition(descriptor, invokeTimeoutEventId, invokeDef.id)]);
450
446
  }
451
447
  if (invokeDef.onError) {
452
- const descriptor = `xstate.error.actor.${invokeDef.id}`;
453
- transitions.set(descriptor, mapTransitionConfigs(invokeDef.onError, transition => invokeTimeoutEventType ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventType) : formatTransition(stateNode, descriptor, transition)));
448
+ const descriptor = 'xstate.error.actor';
449
+ addTransitions(descriptor, mapTransitionConfigs(invokeDef.onError, rawTransition => {
450
+ const transition = withInvokeMatch(rawTransition);
451
+ return invokeTimeoutEventId ? formatInvokeCompletionTransition(descriptor, transition, invokeTimeoutEventId) : formatTransition(stateNode, descriptor, transition);
452
+ }));
454
453
  }
455
454
  if (invokeDef.onSnapshot) {
456
- const descriptor = `xstate.snapshot.${invokeDef.id}`;
457
- transitions.set(descriptor, mapTransitionConfigs(invokeDef.onSnapshot, transition => formatTransition(stateNode, descriptor, transition)));
455
+ const descriptor = 'xstate.snapshot.actor';
456
+ addTransitions(descriptor, mapTransitionConfigs(invokeDef.onSnapshot, transition => formatTransition(stateNode, descriptor, withInvokeMatch(transition))));
458
457
  }
459
458
  }
460
459
  for (const delayedTransition of stateNode.after) {
@@ -802,7 +801,7 @@ class StateMachine {
802
801
  }, this, actorScope.self);
803
802
  if (typeof context === 'function') {
804
803
  const children = {};
805
- const spawn = createSpawner(actorScope, preInitial, initEvent, children);
804
+ const spawn = createSpawner(actorScope, this.implementations.actorSources, children);
806
805
  const resolvedContext = context({
807
806
  spawn,
808
807
  input: initEvent.input,
@@ -910,7 +909,8 @@ class StateMachine {
910
909
  const src = actorData.src;
911
910
  const logic = typeof src === 'string' ? resolveReferencedActor(this, src) : src;
912
911
  if (!logic) {
913
- continue;
912
+ const sourceId = typeof src === 'string' ? src : '<unknown>';
913
+ throw new Error(`Unable to restore child actor '${actorId}': child source '${sourceId}' is not provided in machine '${this.id}'.`);
914
914
  }
915
915
  const actor = resolvedActorScope.system.createActorRef(logic, {
916
916
  id: actorId,
@@ -918,6 +918,7 @@ class StateMachine {
918
918
  syncSnapshot: actorData.syncSnapshot,
919
919
  snapshot: childState,
920
920
  src,
921
+ _sessionId: actorData.sessionId,
921
922
  registryKey: actorData.registryKey
922
923
  });
923
924
  // Mark so `start()` knows to start this child (freshly invoked/spawned