xstate 4.20.0 → 4.23.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.
Files changed (94) hide show
  1. package/CHANGELOG.md +205 -0
  2. package/dist/xstate.interpreter.js +1 -1
  3. package/dist/xstate.js +1 -1
  4. package/dist/xstate.web.js +2 -2
  5. package/es/Actor.d.ts +6 -5
  6. package/es/Actor.js +22 -7
  7. package/es/Machine.d.ts +10 -5
  8. package/es/Machine.js +3 -6
  9. package/es/State.js +3 -5
  10. package/es/StateNode.d.ts +4 -3
  11. package/es/StateNode.js +34 -28
  12. package/es/_virtual/_tslib.js +59 -73
  13. package/es/actionTypes.js +3 -2
  14. package/es/actions.d.ts +1 -1
  15. package/es/actions.js +51 -37
  16. package/es/behaviors.d.ts +37 -0
  17. package/es/behaviors.js +65 -0
  18. package/es/constants.js +2 -1
  19. package/es/devTools.js +1 -1
  20. package/es/environment.js +2 -1
  21. package/es/index.js +3 -1
  22. package/es/interpreter.d.ts +15 -8
  23. package/es/interpreter.js +48 -22
  24. package/es/invokeUtils.js +4 -3
  25. package/es/mapState.js +1 -1
  26. package/es/match.js +1 -1
  27. package/es/model.d.ts +2 -36
  28. package/es/model.types.d.ts +37 -0
  29. package/es/registry.js +2 -1
  30. package/es/scheduler.js +2 -1
  31. package/es/schema.js +1 -1
  32. package/es/serviceScope.js +1 -3
  33. package/es/stateUtils.d.ts +1 -0
  34. package/es/stateUtils.js +14 -8
  35. package/es/types.d.ts +35 -9
  36. package/es/types.js +1 -1
  37. package/es/utils.d.ts +3 -2
  38. package/es/utils.js +4 -40
  39. package/lib/Actor.d.ts +25 -24
  40. package/lib/Actor.js +87 -53
  41. package/lib/Machine.d.ts +17 -12
  42. package/lib/Machine.js +14 -14
  43. package/lib/SimulatedClock.d.ts +16 -16
  44. package/lib/State.d.ts +108 -108
  45. package/lib/State.js +246 -236
  46. package/lib/StateNode.d.ts +279 -278
  47. package/lib/StateNode.js +1535 -1350
  48. package/lib/_virtual/_tslib.js +81 -0
  49. package/lib/actionTypes.d.ts +19 -19
  50. package/lib/actionTypes.js +43 -23
  51. package/lib/actions.d.ts +138 -138
  52. package/lib/actions.js +465 -387
  53. package/lib/behaviors.d.ts +37 -0
  54. package/lib/behaviors.js +69 -0
  55. package/lib/constants.d.ts +5 -5
  56. package/lib/constants.js +13 -7
  57. package/lib/devTools.d.ts +15 -15
  58. package/lib/devTools.js +37 -26
  59. package/lib/each.d.ts +3 -3
  60. package/lib/environment.d.ts +1 -1
  61. package/lib/environment.js +7 -4
  62. package/lib/index.d.ts +30 -30
  63. package/lib/index.js +67 -57
  64. package/lib/interpreter.d.ts +205 -198
  65. package/lib/interpreter.js +1307 -1052
  66. package/lib/invoke.d.ts +10 -10
  67. package/lib/invokeUtils.d.ts +6 -6
  68. package/lib/invokeUtils.js +40 -37
  69. package/lib/json.d.ts +30 -30
  70. package/lib/mapState.d.ts +3 -3
  71. package/lib/mapState.js +31 -32
  72. package/lib/match.d.ts +8 -8
  73. package/lib/match.js +33 -47
  74. package/lib/model.d.ts +4 -38
  75. package/lib/model.js +5 -1
  76. package/lib/model.types.d.ts +37 -0
  77. package/lib/model.types.js +2 -0
  78. package/lib/patterns.d.ts +13 -13
  79. package/lib/registry.d.ts +8 -8
  80. package/lib/registry.js +21 -18
  81. package/lib/scheduler.d.ts +16 -16
  82. package/lib/scheduler.js +79 -70
  83. package/lib/schema.d.ts +1 -1
  84. package/lib/schema.js +6 -4
  85. package/lib/scxml.d.ts +5 -5
  86. package/lib/serviceScope.d.ts +3 -3
  87. package/lib/serviceScope.js +16 -12
  88. package/lib/stateUtils.d.ts +14 -13
  89. package/lib/stateUtils.js +232 -190
  90. package/lib/types.d.ts +928 -902
  91. package/lib/types.js +29 -29
  92. package/lib/utils.d.ts +68 -67
  93. package/lib/utils.js +530 -529
  94. package/package.json +5 -5
package/es/utils.js CHANGED
@@ -5,7 +5,6 @@ import { IS_PRODUCTION } from './environment.js';
5
5
  function keys(value) {
6
6
  return Object.keys(value);
7
7
  }
8
-
9
8
  function matchesState(parentStateId, childStateId, delimiter) {
10
9
  if (delimiter === void 0) {
11
10
  delimiter = STATE_DELIMITER;
@@ -35,7 +34,6 @@ function matchesState(parentStateId, childStateId, delimiter) {
35
34
  return matchesState(parentStateValue[key], childStateValue[key]);
36
35
  });
37
36
  }
38
-
39
37
  function getEventType(event) {
40
38
  try {
41
39
  return isString(event) || typeof event === 'number' ? "" + event : event.type;
@@ -43,7 +41,6 @@ function getEventType(event) {
43
41
  throw new Error('Events must be strings or objects with a string event.type property.');
44
42
  }
45
43
  }
46
-
47
44
  function toStatePath(stateId, delimiter) {
48
45
  try {
49
46
  if (isArray(stateId)) {
@@ -55,11 +52,9 @@ function toStatePath(stateId, delimiter) {
55
52
  throw new Error("'" + stateId + "' is not a valid state path.");
56
53
  }
57
54
  }
58
-
59
55
  function isStateLike(state) {
60
56
  return typeof state === 'object' && 'value' in state && 'context' in state && 'event' in state && '_event' in state;
61
57
  }
62
-
63
58
  function toStateValue(stateValue, delimiter) {
64
59
  if (isStateLike(stateValue)) {
65
60
  return stateValue.value;
@@ -76,7 +71,6 @@ function toStateValue(stateValue, delimiter) {
76
71
  var statePath = toStatePath(stateValue, delimiter);
77
72
  return pathToStateValue(statePath);
78
73
  }
79
-
80
74
  function pathToStateValue(statePath) {
81
75
  if (statePath.length === 1) {
82
76
  return statePath[0];
@@ -96,7 +90,6 @@ function pathToStateValue(statePath) {
96
90
 
97
91
  return value;
98
92
  }
99
-
100
93
  function mapValues(collection, iteratee) {
101
94
  var result = {};
102
95
  var collectionKeys = keys(collection);
@@ -108,7 +101,6 @@ function mapValues(collection, iteratee) {
108
101
 
109
102
  return result;
110
103
  }
111
-
112
104
  function mapFilterValues(collection, iteratee, predicate) {
113
105
  var e_1, _a;
114
106
 
@@ -144,7 +136,6 @@ function mapFilterValues(collection, iteratee, predicate) {
144
136
  * @param props The deep path to the prop of the desired value
145
137
  */
146
138
 
147
-
148
139
  var path = function (props) {
149
140
  return function (object) {
150
141
  var e_2, _a;
@@ -176,7 +167,6 @@ var path = function (props) {
176
167
  * @param props The deep path to the prop of the desired value
177
168
  */
178
169
 
179
-
180
170
  function nestedPath(props, accessorProp) {
181
171
  return function (object) {
182
172
  var e_3, _a;
@@ -203,7 +193,6 @@ function nestedPath(props, accessorProp) {
203
193
  return result;
204
194
  };
205
195
  }
206
-
207
196
  function toStatePaths(stateValue) {
208
197
  if (!stateValue) {
209
198
  return [[]];
@@ -226,13 +215,11 @@ function toStatePaths(stateValue) {
226
215
  }));
227
216
  return result;
228
217
  }
229
-
230
218
  function flatten(array) {
231
219
  var _a;
232
220
 
233
221
  return (_a = []).concat.apply(_a, __spreadArray([], __read(array)));
234
222
  }
235
-
236
223
  function toArrayStrict(value) {
237
224
  if (isArray(value)) {
238
225
  return value;
@@ -240,7 +227,6 @@ function toArrayStrict(value) {
240
227
 
241
228
  return [value];
242
229
  }
243
-
244
230
  function toArray(value) {
245
231
  if (value === undefined) {
246
232
  return [];
@@ -248,7 +234,6 @@ function toArray(value) {
248
234
 
249
235
  return toArrayStrict(value);
250
236
  }
251
-
252
237
  function mapContext(mapper, context, _event) {
253
238
  var e_5, _a;
254
239
 
@@ -283,11 +268,9 @@ function mapContext(mapper, context, _event) {
283
268
 
284
269
  return result;
285
270
  }
286
-
287
271
  function isBuiltInEvent(eventType) {
288
272
  return /^(done|error)\./.test(eventType);
289
273
  }
290
-
291
274
  function isPromiseLike(value) {
292
275
  if (value instanceof Promise) {
293
276
  return true;
@@ -300,7 +283,9 @@ function isPromiseLike(value) {
300
283
 
301
284
  return false;
302
285
  }
303
-
286
+ function isBehavior(value) {
287
+ return value !== null && typeof value === 'object' && 'transition' in value && typeof value.transition === 'function';
288
+ }
304
289
  function partition(items, predicate) {
305
290
  var e_6, _a;
306
291
 
@@ -332,7 +317,6 @@ function partition(items, predicate) {
332
317
 
333
318
  return [truthy, falsy];
334
319
  }
335
-
336
320
  function updateHistoryStates(hist, stateValue) {
337
321
  return mapValues(hist.states, function (subHist, key) {
338
322
  if (!subHist) {
@@ -351,14 +335,12 @@ function updateHistoryStates(hist, stateValue) {
351
335
  };
352
336
  });
353
337
  }
354
-
355
338
  function updateHistoryValue(hist, stateValue) {
356
339
  return {
357
340
  current: stateValue,
358
341
  states: updateHistoryStates(hist, stateValue)
359
342
  };
360
343
  }
361
-
362
344
  function updateContext(context, _event, assignActions, state) {
363
345
  if (!IS_PRODUCTION) {
364
346
  warn(!!context, 'Attempting to update undefined context');
@@ -402,7 +384,6 @@ function updateContext(context, _event, assignActions, state) {
402
384
  return updatedContext;
403
385
  } // tslint:disable-next-line:no-empty
404
386
 
405
-
406
387
  var warn = function () {};
407
388
 
408
389
  if (!IS_PRODUCTION) {
@@ -425,16 +406,13 @@ if (!IS_PRODUCTION) {
425
406
  }
426
407
  };
427
408
  }
428
-
429
409
  function isArray(value) {
430
410
  return Array.isArray(value);
431
411
  } // tslint:disable-next-line:ban-types
432
412
 
433
-
434
413
  function isFunction(value) {
435
414
  return typeof value === 'function';
436
415
  }
437
-
438
416
  function isString(value) {
439
417
  return typeof value === 'string';
440
418
  } // export function memoizedGetter<T, TP extends { prototype: object }>(
@@ -449,7 +427,6 @@ function isString(value) {
449
427
  // });
450
428
  // }
451
429
 
452
-
453
430
  function toGuard(condition, guardMap) {
454
431
  if (!condition) {
455
432
  return undefined;
@@ -473,7 +450,6 @@ function toGuard(condition, guardMap) {
473
450
 
474
451
  return condition;
475
452
  }
476
-
477
453
  function isObservable(value) {
478
454
  try {
479
455
  return 'subscribe' in value && isFunction(value.subscribe);
@@ -481,11 +457,9 @@ function isObservable(value) {
481
457
  return false;
482
458
  }
483
459
  }
484
-
485
460
  var symbolObservable = /*#__PURE__*/function () {
486
461
  return typeof Symbol === 'function' && Symbol.observable || '@@observable';
487
462
  }();
488
-
489
463
  function isMachine(value) {
490
464
  try {
491
465
  return '__xstatenode' in value;
@@ -493,11 +467,9 @@ function isMachine(value) {
493
467
  return false;
494
468
  }
495
469
  }
496
-
497
470
  function isActor(value) {
498
471
  return !!value && typeof value.send === 'function';
499
472
  }
500
-
501
473
  var uniqueId = /*#__PURE__*/function () {
502
474
  var currentId = 0;
503
475
  return function () {
@@ -505,7 +477,6 @@ var uniqueId = /*#__PURE__*/function () {
505
477
  return currentId.toString(16);
506
478
  };
507
479
  }();
508
-
509
480
  function toEventObject(event, payload // id?: TEvent['type']
510
481
  ) {
511
482
  if (isString(event) || typeof event === 'number') {
@@ -516,7 +487,6 @@ function toEventObject(event, payload // id?: TEvent['type']
516
487
 
517
488
  return event;
518
489
  }
519
-
520
490
  function toSCXMLEvent(event, scxmlEvent) {
521
491
  if (!isString(event) && '$$type' in event && event.$$type === 'scxml') {
522
492
  return event;
@@ -530,7 +500,6 @@ function toSCXMLEvent(event, scxmlEvent) {
530
500
  type: 'external'
531
501
  }, scxmlEvent);
532
502
  }
533
-
534
503
  function toTransitionConfigArray(event, configLike) {
535
504
  var transitions = toArrayStrict(configLike).map(function (transitionLike) {
536
505
  if (typeof transitionLike === 'undefined' || typeof transitionLike === 'string' || isMachine(transitionLike)) {
@@ -546,7 +515,6 @@ function toTransitionConfigArray(event, configLike) {
546
515
  });
547
516
  return transitions;
548
517
  }
549
-
550
518
  function normalizeTarget(target) {
551
519
  if (target === undefined || target === TARGETLESS_KEY) {
552
520
  return undefined;
@@ -554,7 +522,6 @@ function normalizeTarget(target) {
554
522
 
555
523
  return toArray(target);
556
524
  }
557
-
558
525
  function reportUnhandledExceptionOnInvocation(originalError, currentError, id) {
559
526
  if (!IS_PRODUCTION) {
560
527
  var originalStackTrace = originalError.stack ? " Stacktrace was '" + originalError.stack + "'" : '';
@@ -569,7 +536,6 @@ function reportUnhandledExceptionOnInvocation(originalError, currentError, id) {
569
536
  }
570
537
  }
571
538
  }
572
-
573
539
  function evaluateGuard(machine, guard, context, _event, state) {
574
540
  var guards = machine.options.guards;
575
541
  var guardMeta = {
@@ -590,7 +556,6 @@ function evaluateGuard(machine, guard, context, _event, state) {
590
556
 
591
557
  return condFn(context, _event.data, guardMeta);
592
558
  }
593
-
594
559
  function toInvokeSource(src) {
595
560
  if (typeof src === 'string') {
596
561
  return {
@@ -600,7 +565,6 @@ function toInvokeSource(src) {
600
565
 
601
566
  return src;
602
567
  }
603
-
604
568
  function toObserver(nextHandler, errorHandler, completionHandler) {
605
569
  if (typeof nextHandler === 'object') {
606
570
  return nextHandler;
@@ -617,4 +581,4 @@ function toObserver(nextHandler, errorHandler, completionHandler) {
617
581
  };
618
582
  }
619
583
 
620
- export { evaluateGuard, flatten, getEventType, isActor, isArray, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toObserver, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
584
+ export { evaluateGuard, flatten, getEventType, isActor, isArray, isBehavior, isBuiltInEvent, isFunction, isMachine, isObservable, isPromiseLike, isStateLike, isString, keys, mapContext, mapFilterValues, mapValues, matchesState, nestedPath, normalizeTarget, partition, path, pathToStateValue, reportUnhandledExceptionOnInvocation, symbolObservable, toArray, toArrayStrict, toEventObject, toGuard, toInvokeSource, toObserver, toSCXMLEvent, toStatePath, toStatePaths, toStateValue, toTransitionConfigArray, uniqueId, updateContext, updateHistoryStates, updateHistoryValue, warn };
package/lib/Actor.d.ts CHANGED
@@ -1,25 +1,26 @@
1
- import { EventObject, Subscribable, InvokeDefinition, AnyEventObject, StateMachine, Spawnable, SCXML } from './types';
2
- import { ActorRef, SpawnedActorRef } from '.';
3
- export interface Actor<TContext = any, TEvent extends EventObject = AnyEventObject> extends Subscribable<TContext> {
4
- id: string;
5
- send: (event: TEvent) => any;
6
- stop?: () => any | undefined;
7
- toJSON: () => {
8
- id: string;
9
- };
10
- meta?: InvokeDefinition<TContext, TEvent>;
11
- state?: any;
12
- deferred?: boolean;
13
- }
14
- export declare function createNullActor(id: string): SpawnedActorRef<any>;
15
- /**
16
- * Creates a deferred actor that is able to be invoked given the provided
17
- * invocation information in its `.meta` value.
18
- *
19
- * @param invokeDefinition The meta information needed to invoke the actor.
20
- */
21
- export declare function createInvocableActor<TC, TE extends EventObject>(invokeDefinition: InvokeDefinition<TC, TE>, machine: StateMachine<TC, any, TE, any>, context: TC, _event: SCXML.Event<TE>): SpawnedActorRef<any>;
22
- export declare function createDeferredActor(entity: Spawnable, id: string, data?: any): SpawnedActorRef<any, undefined>;
23
- export declare function isActor(item: any): item is ActorRef<any>;
24
- export declare function isSpawnedActor(item: any): item is SpawnedActorRef<any>;
1
+ import { EventObject, Subscribable, InvokeDefinition, AnyEventObject, StateMachine, Spawnable, SCXML } from './types';
2
+ import { ActorRef, BaseActorRef } from '.';
3
+ export interface Actor<TContext = any, TEvent extends EventObject = AnyEventObject> extends Subscribable<TContext> {
4
+ id: string;
5
+ send: (event: TEvent) => any;
6
+ stop?: () => any | undefined;
7
+ toJSON: () => {
8
+ id: string;
9
+ };
10
+ meta?: InvokeDefinition<TContext, TEvent>;
11
+ state?: any;
12
+ deferred?: boolean;
13
+ }
14
+ export declare function createNullActor(id: string): ActorRef<any>;
15
+ /**
16
+ * Creates a deferred actor that is able to be invoked given the provided
17
+ * invocation information in its `.meta` value.
18
+ *
19
+ * @param invokeDefinition The meta information needed to invoke the actor.
20
+ */
21
+ export declare function createInvocableActor<TC, TE extends EventObject>(invokeDefinition: InvokeDefinition<TC, TE>, machine: StateMachine<TC, any, TE, any>, context: TC, _event: SCXML.Event<TE>): ActorRef<any>;
22
+ export declare function createDeferredActor(entity: Spawnable, id: string, data?: any): ActorRef<any, undefined>;
23
+ export declare function isActor(item: any): item is ActorRef<any>;
24
+ export declare function isSpawnedActor(item: any): item is ActorRef<any>;
25
+ export declare function toActorRef<TEvent extends EventObject, TEmitted = any, TActorRefLike extends BaseActorRef<TEvent> = BaseActorRef<TEvent>>(actorRefLike: TActorRefLike): ActorRef<TEvent, TEmitted>;
25
26
  //# sourceMappingURL=Actor.d.ts.map
package/lib/Actor.js CHANGED
@@ -1,65 +1,99 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isSpawnedActor = exports.isActor = exports.createDeferredActor = exports.createInvocableActor = exports.createNullActor = void 0;
4
- var utils_1 = require("./utils");
5
- var serviceScope = require("./serviceScope");
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var _tslib = require('./_virtual/_tslib.js');
6
+ var utils = require('./utils.js');
7
+ var serviceScope = require('./serviceScope.js');
8
+
6
9
  function createNullActor(id) {
7
- return {
8
- id: id,
9
- send: function () { return void 0; },
10
- subscribe: function () { return ({
11
- unsubscribe: function () { return void 0; }
12
- }); },
13
- getSnapshot: function () { return undefined; },
14
- toJSON: function () { return ({
15
- id: id
16
- }); }
17
- };
10
+ return {
11
+ id: id,
12
+ send: function () {
13
+ return void 0;
14
+ },
15
+ subscribe: function () {
16
+ return {
17
+ unsubscribe: function () {
18
+ return void 0;
19
+ }
20
+ };
21
+ },
22
+ getSnapshot: function () {
23
+ return undefined;
24
+ },
25
+ toJSON: function () {
26
+ return {
27
+ id: id
28
+ };
29
+ }
30
+ };
18
31
  }
19
- exports.createNullActor = createNullActor;
20
- /**
21
- * Creates a deferred actor that is able to be invoked given the provided
22
- * invocation information in its `.meta` value.
23
- *
24
- * @param invokeDefinition The meta information needed to invoke the actor.
32
+ /**
33
+ * Creates a deferred actor that is able to be invoked given the provided
34
+ * invocation information in its `.meta` value.
35
+ *
36
+ * @param invokeDefinition The meta information needed to invoke the actor.
25
37
  */
38
+
26
39
  function createInvocableActor(invokeDefinition, machine, context, _event) {
27
- var _a;
28
- var invokeSrc = utils_1.toInvokeSource(invokeDefinition.src);
29
- var serviceCreator = (_a = machine === null || machine === void 0 ? void 0 : machine.options.services) === null || _a === void 0 ? void 0 : _a[invokeSrc.type];
30
- var resolvedData = invokeDefinition.data
31
- ? utils_1.mapContext(invokeDefinition.data, context, _event)
32
- : undefined;
33
- var tempActor = serviceCreator
34
- ? createDeferredActor(serviceCreator, invokeDefinition.id, resolvedData)
35
- : createNullActor(invokeDefinition.id);
36
- // @ts-ignore
37
- tempActor.meta = invokeDefinition;
38
- return tempActor;
40
+ var _a;
41
+
42
+ var invokeSrc = utils.toInvokeSource(invokeDefinition.src);
43
+ var serviceCreator = (_a = machine === null || machine === void 0 ? void 0 : machine.options.services) === null || _a === void 0 ? void 0 : _a[invokeSrc.type];
44
+ var resolvedData = invokeDefinition.data ? utils.mapContext(invokeDefinition.data, context, _event) : undefined;
45
+ var tempActor = serviceCreator ? createDeferredActor(serviceCreator, invokeDefinition.id, resolvedData) : createNullActor(invokeDefinition.id); // @ts-ignore
46
+
47
+ tempActor.meta = invokeDefinition;
48
+ return tempActor;
39
49
  }
40
- exports.createInvocableActor = createInvocableActor;
41
50
  function createDeferredActor(entity, id, data) {
42
- var tempActor = createNullActor(id);
43
- // @ts-ignore
44
- tempActor.deferred = true;
45
- if (utils_1.isMachine(entity)) {
46
- // "mute" the existing service scope so potential spawned actors within the `.initialState` stay deferred here
47
- // @ts-ignore
48
- tempActor.state = serviceScope.provide(undefined, function () { return (data ? entity.withContext(data) : entity).initialState; });
49
- }
50
- return tempActor;
51
+ var tempActor = createNullActor(id); // @ts-ignore
52
+
53
+ tempActor.deferred = true;
54
+
55
+ if (utils.isMachine(entity)) {
56
+ // "mute" the existing service scope so potential spawned actors within the `.initialState` stay deferred here
57
+ var initialState_1 = tempActor.state = serviceScope.provide(undefined, function () {
58
+ return (data ? entity.withContext(data) : entity).initialState;
59
+ });
60
+
61
+ tempActor.getSnapshot = function () {
62
+ return initialState_1;
63
+ };
64
+ }
65
+
66
+ return tempActor;
51
67
  }
52
- exports.createDeferredActor = createDeferredActor;
53
68
  function isActor(item) {
54
- try {
55
- return typeof item.send === 'function';
56
- }
57
- catch (e) {
58
- return false;
59
- }
69
+ try {
70
+ return typeof item.send === 'function';
71
+ } catch (e) {
72
+ return false;
73
+ }
60
74
  }
61
- exports.isActor = isActor;
62
75
  function isSpawnedActor(item) {
63
- return isActor(item) && 'id' in item;
76
+ return isActor(item) && 'id' in item;
77
+ }
78
+ function toActorRef(actorRefLike) {
79
+ return _tslib.__assign({
80
+ subscribe: function () {
81
+ return {
82
+ unsubscribe: function () {
83
+ return void 0;
84
+ }
85
+ };
86
+ },
87
+ id: 'anonymous',
88
+ getSnapshot: function () {
89
+ return undefined;
90
+ }
91
+ }, actorRefLike);
64
92
  }
93
+
94
+ exports.createDeferredActor = createDeferredActor;
95
+ exports.createInvocableActor = createInvocableActor;
96
+ exports.createNullActor = createNullActor;
97
+ exports.isActor = isActor;
65
98
  exports.isSpawnedActor = isSpawnedActor;
99
+ exports.toActorRef = toActorRef;
package/lib/Machine.d.ts CHANGED
@@ -1,13 +1,18 @@
1
- import { StateMachine, MachineOptions, DefaultContext, MachineConfig, StateSchema, EventObject, AnyEventObject, Typestate } from './types';
2
- import { Model, ModelContextFrom, ModelEventsFrom } from './model';
3
- export declare function Machine<TContext = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, any, TEvent>;
4
- export declare function Machine<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, TStateSchema, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, TStateSchema, TEvent>;
5
- export declare function createMachine<TModel extends Model<any, any, any>, TContext = ModelContextFrom<TModel>, TEvent extends EventObject = ModelEventsFrom<TModel>, TTypestate extends Typestate<TContext> = {
6
- value: any;
7
- context: TContext;
8
- }>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
9
- export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> = {
10
- value: any;
11
- context: TContext;
12
- }>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
1
+ import { StateMachine, MachineOptions, DefaultContext, MachineConfig, StateSchema, EventObject, AnyEventObject, Typestate, EventFrom } from './types';
2
+ import { Model, ModelContextFrom } from './model.types';
3
+ /**
4
+ * @deprecated Use `createMachine(...)` instead.
5
+ */
6
+ export declare function Machine<TContext = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, any, TEvent>;
7
+ export declare function Machine<TContext = DefaultContext, TStateSchema extends StateSchema = any, TEvent extends EventObject = AnyEventObject>(config: MachineConfig<TContext, TStateSchema, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>, initialContext?: TContext): StateMachine<TContext, TStateSchema, TEvent>;
8
+ export declare function createMachine<TModel extends Model<any, any, any>, TContext = ModelContextFrom<TModel>, TEvent extends EventObject = EventFrom<TModel>, TTypestate extends Typestate<TContext> = {
9
+ value: any;
10
+ context: TContext;
11
+ }>(config: MachineConfig<TContext, any, TEvent> & {
12
+ context: TContext;
13
+ }, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
14
+ export declare function createMachine<TContext, TEvent extends EventObject = AnyEventObject, TTypestate extends Typestate<TContext> = {
15
+ value: any;
16
+ context: TContext;
17
+ }>(config: TContext extends Model<any, any, any> ? never : MachineConfig<TContext, any, TEvent>, options?: Partial<MachineOptions<TContext, TEvent>>): StateMachine<TContext, any, TEvent, TTypestate>;
13
18
  //# sourceMappingURL=Machine.d.ts.map
package/lib/Machine.js CHANGED
@@ -1,19 +1,19 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createMachine = exports.Machine = void 0;
4
- var StateNode_1 = require("./StateNode");
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var StateNode = require('./StateNode.js');
6
+
5
7
  function Machine(config, options, initialContext) {
6
- if (initialContext === void 0) { initialContext = config.context; }
7
- var resolvedInitialContext = typeof initialContext === 'function'
8
- ? initialContext()
9
- : initialContext;
10
- return new StateNode_1.StateNode(config, options, resolvedInitialContext);
8
+ if (initialContext === void 0) {
9
+ initialContext = config.context;
10
+ }
11
+
12
+ return new StateNode.StateNode(config, options, initialContext);
11
13
  }
12
- exports.Machine = Machine;
13
14
  function createMachine(config, options) {
14
- var resolvedInitialContext = typeof config.context === 'function'
15
- ? config.context()
16
- : config.context;
17
- return new StateNode_1.StateNode(config, options, resolvedInitialContext);
15
+ return new StateNode.StateNode(config, options);
18
16
  }
17
+
18
+ exports.Machine = Machine;
19
19
  exports.createMachine = createMachine;
@@ -1,17 +1,17 @@
1
- import { Clock } from './interpreter';
2
- export interface SimulatedClock extends Clock {
3
- start(speed: number): void;
4
- increment(ms: number): void;
5
- set(ms: number): void;
6
- }
7
- export declare class SimulatedClock implements SimulatedClock {
8
- private timeouts;
9
- private _now;
10
- private _id;
11
- now(): number;
12
- private getId;
13
- setTimeout(fn: (...args: any[]) => void, timeout: number): number;
14
- clearTimeout(id: number): void;
15
- private flushTimeouts;
16
- }
1
+ import { Clock } from './interpreter';
2
+ export interface SimulatedClock extends Clock {
3
+ start(speed: number): void;
4
+ increment(ms: number): void;
5
+ set(ms: number): void;
6
+ }
7
+ export declare class SimulatedClock implements SimulatedClock {
8
+ private timeouts;
9
+ private _now;
10
+ private _id;
11
+ now(): number;
12
+ private getId;
13
+ setTimeout(fn: (...args: any[]) => void, timeout: number): number;
14
+ clearTimeout(id: number): void;
15
+ private flushTimeouts;
16
+ }
17
17
  //# sourceMappingURL=SimulatedClock.d.ts.map