xstate 5.32.1 → 6.0.0-alpha.2

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 (119) hide show
  1. package/README.md +7 -8
  2. package/dist/StateMachine-2dcdb1f5.esm.js +720 -0
  3. package/dist/StateMachine-2ea0a7fa.development.esm.js +737 -0
  4. package/dist/StateMachine-64813f46.cjs.js +723 -0
  5. package/dist/StateMachine-8d22a79c.development.cjs.js +740 -0
  6. package/dist/declarations/src/State.d.ts +11 -13
  7. package/dist/declarations/src/StateMachine.d.ts +27 -21
  8. package/dist/declarations/src/StateNode.d.ts +18 -54
  9. package/dist/declarations/src/actions.d.ts +14 -9
  10. package/dist/declarations/src/actors/callback.d.ts +42 -16
  11. package/dist/declarations/src/actors/index.d.ts +9 -6
  12. package/dist/declarations/src/actors/listener.d.ts +18 -0
  13. package/dist/declarations/src/actors/logic.d.ts +105 -0
  14. package/dist/declarations/src/actors/observable.d.ts +72 -27
  15. package/dist/declarations/src/actors/promise.d.ts +133 -48
  16. package/dist/declarations/src/actors/subscription.d.ts +22 -0
  17. package/dist/declarations/src/atom.d.ts +81 -0
  18. package/dist/declarations/src/createActor.d.ts +31 -33
  19. package/dist/declarations/src/createMachine.d.ts +35 -7
  20. package/dist/declarations/src/createMachineFromConfig.d.ts +176 -0
  21. package/dist/declarations/src/graph/graph.d.ts +4 -2
  22. package/dist/declarations/src/index.d.ts +12 -8
  23. package/dist/declarations/src/inspection.d.ts +84 -30
  24. package/dist/declarations/src/mapState.d.ts +4 -2
  25. package/dist/declarations/src/schema.types.d.ts +90 -0
  26. package/dist/declarations/src/serialize.d.ts +44 -0
  27. package/dist/declarations/src/setup.d.ts +251 -135
  28. package/dist/declarations/src/spawn.d.ts +6 -31
  29. package/dist/declarations/src/stateUtils.d.ts +21 -19
  30. package/dist/declarations/src/system.d.ts +8 -7
  31. package/dist/declarations/src/transition.d.ts +5 -5
  32. package/dist/declarations/src/types.d.ts +405 -405
  33. package/dist/declarations/src/types.v6.d.ts +392 -0
  34. package/dist/declarations/src/utils.d.ts +4 -3
  35. package/dist/index-93edd3bd.esm.js +4909 -0
  36. package/dist/index-9cb3b3a0.cjs.js +4968 -0
  37. package/dist/index-e61170ba.development.esm.js +4948 -0
  38. package/dist/index-f6ef20d1.development.cjs.js +5007 -0
  39. package/dist/xstate-actors.cjs.js +12 -838
  40. package/dist/xstate-actors.cjs.mjs +10 -5
  41. package/dist/xstate-actors.development.cjs.js +12 -838
  42. package/dist/xstate-actors.development.cjs.mjs +10 -5
  43. package/dist/xstate-actors.development.esm.js +1 -836
  44. package/dist/xstate-actors.esm.js +1 -836
  45. package/dist/xstate-actors.umd.min.js +1 -1
  46. package/dist/xstate-actors.umd.min.js.map +1 -1
  47. package/dist/xstate-graph.cjs.js +18 -29
  48. package/dist/xstate-graph.development.cjs.js +18 -29
  49. package/dist/xstate-graph.development.esm.js +15 -26
  50. package/dist/xstate-graph.esm.js +15 -26
  51. package/dist/xstate-graph.umd.min.js +1 -1
  52. package/dist/xstate-graph.umd.min.js.map +1 -1
  53. package/dist/xstate.cjs.js +1316 -112
  54. package/dist/xstate.cjs.mjs +22 -22
  55. package/dist/xstate.development.cjs.js +1316 -112
  56. package/dist/xstate.development.cjs.mjs +22 -22
  57. package/dist/xstate.development.esm.js +1278 -76
  58. package/dist/xstate.esm.js +1278 -76
  59. package/dist/xstate.umd.min.js +1 -1
  60. package/dist/xstate.umd.min.js.map +1 -1
  61. package/package.json +3 -47
  62. package/dist/StateMachine-45e79c35.esm.js +0 -560
  63. package/dist/StateMachine-be2e51a3.development.cjs.js +0 -569
  64. package/dist/StateMachine-e0178d48.development.esm.js +0 -566
  65. package/dist/StateMachine-e6732977.cjs.js +0 -563
  66. package/dist/assign-2aa58daa.esm.js +0 -127
  67. package/dist/assign-927227d6.development.cjs.js +0 -135
  68. package/dist/assign-e8f2bd75.cjs.js +0 -129
  69. package/dist/assign-f338cee3.development.esm.js +0 -133
  70. package/dist/declarations/src/actions/assign.d.ts +0 -45
  71. package/dist/declarations/src/actions/cancel.d.ts +0 -39
  72. package/dist/declarations/src/actions/emit.d.ts +0 -44
  73. package/dist/declarations/src/actions/enqueueActions.d.ts +0 -57
  74. package/dist/declarations/src/actions/log.d.ts +0 -16
  75. package/dist/declarations/src/actions/raise.d.ts +0 -21
  76. package/dist/declarations/src/actions/send.d.ts +0 -42
  77. package/dist/declarations/src/actions/spawnChild.d.ts +0 -34
  78. package/dist/declarations/src/actions/stopChild.d.ts +0 -19
  79. package/dist/declarations/src/actors/transition.d.ts +0 -143
  80. package/dist/declarations/src/dev/index.d.ts +0 -14
  81. package/dist/declarations/src/guards.d.ts +0 -128
  82. package/dist/log-410e5e55.development.esm.js +0 -429
  83. package/dist/log-7776fcf2.cjs.js +0 -409
  84. package/dist/log-c2e11c01.development.cjs.js +0 -437
  85. package/dist/log-fa75ec2d.esm.js +0 -401
  86. package/dist/raise-74097812.development.esm.js +0 -2823
  87. package/dist/raise-7c948725.cjs.js +0 -2816
  88. package/dist/raise-964cd4e9.development.cjs.js +0 -2878
  89. package/dist/raise-e974d1c9.esm.js +0 -2761
  90. package/dist/xstate-actions.cjs.d.mts +0 -2
  91. package/dist/xstate-actions.cjs.d.ts +0 -2
  92. package/dist/xstate-actions.cjs.js +0 -23
  93. package/dist/xstate-actions.cjs.mjs +0 -14
  94. package/dist/xstate-actions.development.cjs.js +0 -23
  95. package/dist/xstate-actions.development.cjs.mjs +0 -14
  96. package/dist/xstate-actions.development.esm.js +0 -4
  97. package/dist/xstate-actions.esm.js +0 -4
  98. package/dist/xstate-actions.umd.min.js +0 -2
  99. package/dist/xstate-actions.umd.min.js.map +0 -1
  100. package/dist/xstate-dev.cjs.d.mts +0 -2
  101. package/dist/xstate-dev.cjs.d.ts +0 -2
  102. package/dist/xstate-dev.cjs.js +0 -48
  103. package/dist/xstate-dev.cjs.mjs +0 -5
  104. package/dist/xstate-dev.development.cjs.js +0 -51
  105. package/dist/xstate-dev.development.cjs.mjs +0 -5
  106. package/dist/xstate-dev.development.esm.js +0 -45
  107. package/dist/xstate-dev.esm.js +0 -42
  108. package/dist/xstate-dev.umd.min.js +0 -2
  109. package/dist/xstate-dev.umd.min.js.map +0 -1
  110. package/dist/xstate-guards.cjs.d.mts +0 -2
  111. package/dist/xstate-guards.cjs.d.ts +0 -2
  112. package/dist/xstate-guards.cjs.js +0 -14
  113. package/dist/xstate-guards.cjs.mjs +0 -7
  114. package/dist/xstate-guards.development.cjs.js +0 -14
  115. package/dist/xstate-guards.development.cjs.mjs +0 -7
  116. package/dist/xstate-guards.development.esm.js +0 -2
  117. package/dist/xstate-guards.esm.js +0 -2
  118. package/dist/xstate-guards.umd.min.js +0 -2
  119. package/dist/xstate-guards.umd.min.js.map +0 -1
@@ -2,844 +2,18 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var dist_xstateGuards = require('./raise-964cd4e9.development.cjs.js');
6
- require('./xstate-dev.development.cjs.js');
5
+ var dist_xstateActors = require('./index-f6ef20d1.development.cjs.js');
7
6
 
8
- /**
9
- * Represents an actor created by `fromTransition`.
10
- *
11
- * The type of `self` within the actor's logic.
12
- *
13
- * @example
14
- *
15
- * ```ts
16
- * import {
17
- * fromTransition,
18
- * createActor,
19
- * type AnyActorSystem
20
- * } from 'xstate';
21
- *
22
- * //* The actor's stored context.
23
- * type Context = {
24
- * // The current count.
25
- * count: number;
26
- * // The amount to increase `count` by.
27
- * step: number;
28
- * };
29
- * // The events the actor receives.
30
- * type Event = { type: 'increment' };
31
- * // The actor's input.
32
- * type Input = { step?: number };
33
- *
34
- * // Actor logic that increments `count` by `step` when it receives an event of
35
- * // type `increment`.
36
- * const logic = fromTransition<Context, Event, AnyActorSystem, Input>(
37
- * (state, event, actorScope) => {
38
- * actorScope.self;
39
- * // ^? TransitionActorRef<Context, Event>
40
- *
41
- * if (event.type === 'increment') {
42
- * return {
43
- * ...state,
44
- * count: state.count + state.step
45
- * };
46
- * }
47
- * return state;
48
- * },
49
- * ({ input, self }) => {
50
- * self;
51
- * // ^? TransitionActorRef<Context, Event>
52
- *
53
- * return {
54
- * count: 0,
55
- * step: input.step ?? 1
56
- * };
57
- * }
58
- * );
59
- *
60
- * const actor = createActor(logic, { input: { step: 10 } });
61
- * // ^? TransitionActorRef<Context, Event>
62
- * ```
63
- *
64
- * @see {@link fromTransition}
65
- */
66
7
 
67
- /**
68
- * Returns actor logic given a transition function and its initial state.
69
- *
70
- * A “transition function” is a function that takes the current `state` and
71
- * received `event` object as arguments, and returns the next state, similar to
72
- * a reducer.
73
- *
74
- * Actors created from transition logic (“transition actors”) can:
75
- *
76
- * - Receive events
77
- * - Emit snapshots of its state
78
- *
79
- * The transition function’s `state` is used as its transition actor’s
80
- * `context`.
81
- *
82
- * Note that the "state" for a transition function is provided by the initial
83
- * state argument, and is not the same as the State object of an actor or a
84
- * state within a machine configuration.
85
- *
86
- * @example
87
- *
88
- * ```ts
89
- * const transitionLogic = fromTransition(
90
- * (state, event) => {
91
- * if (event.type === 'increment') {
92
- * return {
93
- * ...state,
94
- * count: state.count + 1
95
- * };
96
- * }
97
- * return state;
98
- * },
99
- * { count: 0 }
100
- * );
101
- *
102
- * const transitionActor = createActor(transitionLogic);
103
- * transitionActor.subscribe((snapshot) => {
104
- * console.log(snapshot);
105
- * });
106
- * transitionActor.start();
107
- * // => {
108
- * // status: 'active',
109
- * // context: { count: 0 },
110
- * // ...
111
- * // }
112
- *
113
- * transitionActor.send({ type: 'increment' });
114
- * // => {
115
- * // status: 'active',
116
- * // context: { count: 1 },
117
- * // ...
118
- * // }
119
- * ```
120
- *
121
- * @param transition The transition function used to describe the transition
122
- * logic. It should return the next state given the current state and event.
123
- * It receives the following arguments:
124
- *
125
- * - `state` - the current state.
126
- * - `event` - the received event.
127
- * - `actorScope` - the actor scope object, with properties like `self` and
128
- * `system`.
129
- *
130
- * @param initialContext The initial state of the transition function, either an
131
- * object representing the state, or a function which returns a state object.
132
- * If a function, it will receive as its only argument an object with the
133
- * following properties:
134
- *
135
- * - `input` - the `input` provided to its parent transition actor.
136
- * - `self` - a reference to its parent transition actor.
137
- *
138
- * @returns Actor logic
139
- * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
140
- */
141
- function fromTransition(transition, initialContext) {
142
- return {
143
- config: transition,
144
- transition: (snapshot, event, actorScope) => {
145
- return {
146
- ...snapshot,
147
- context: transition(snapshot.context, event, actorScope)
148
- };
149
- },
150
- getInitialSnapshot: (_, input) => {
151
- return {
152
- status: 'active',
153
- output: undefined,
154
- error: undefined,
155
- context: typeof initialContext === 'function' ? initialContext({
156
- input
157
- }) : initialContext
158
- };
159
- },
160
- getPersistedSnapshot: snapshot => snapshot,
161
- restoreSnapshot: snapshot => snapshot
162
- };
163
- }
164
8
 
165
- const instanceStates = /* #__PURE__ */new WeakMap();
166
-
167
- /**
168
- * Represents an actor created by `fromCallback`.
169
- *
170
- * The type of `self` within the actor's logic.
171
- *
172
- * @example
173
- *
174
- * ```ts
175
- * import { fromCallback, createActor } from 'xstate';
176
- *
177
- * // The events the actor receives.
178
- * type Event = { type: 'someEvent' };
179
- * // The actor's input.
180
- * type Input = { name: string };
181
- *
182
- * // Actor logic that logs whenever it receives an event of type `someEvent`.
183
- * const logic = fromCallback<Event, Input>(({ self, input, receive }) => {
184
- * self;
185
- * // ^? CallbackActorRef<Event, Input>
186
- *
187
- * receive((event) => {
188
- * if (event.type === 'someEvent') {
189
- * console.log(`${input.name}: received "someEvent" event`);
190
- * // logs 'myActor: received "someEvent" event'
191
- * }
192
- * });
193
- * });
194
- *
195
- * const actor = createActor(logic, { input: { name: 'myActor' } });
196
- * // ^? CallbackActorRef<Event, Input>
197
- * ```
198
- *
199
- * @see {@link fromCallback}
200
- */
201
-
202
- /**
203
- * An actor logic creator which returns callback logic as defined by a callback
204
- * function.
205
- *
206
- * @remarks
207
- * Useful for subscription-based or other free-form logic that can send events
208
- * back to the parent actor.
209
- *
210
- * Actors created from callback logic (“callback actors”) can:
211
- *
212
- * - Receive events via the `receive` function
213
- * - Send events to the parent actor via the `sendBack` function
214
- *
215
- * Callback actors are a bit different from other actors in that they:
216
- *
217
- * - Do not work with `onDone`
218
- * - Do not produce a snapshot using `.getSnapshot()`
219
- * - Do not emit values when used with `.subscribe()`
220
- * - Can not be stopped with `.stop()`
221
- *
222
- * @example
223
- *
224
- * ```typescript
225
- * const callbackLogic = fromCallback(({ sendBack, receive }) => {
226
- * let lockStatus = 'unlocked';
227
- *
228
- * const handler = (event) => {
229
- * if (lockStatus === 'locked') {
230
- * return;
231
- * }
232
- * sendBack(event);
233
- * };
234
- *
235
- * receive((event) => {
236
- * if (event.type === 'lock') {
237
- * lockStatus = 'locked';
238
- * } else if (event.type === 'unlock') {
239
- * lockStatus = 'unlocked';
240
- * }
241
- * });
242
- *
243
- * document.body.addEventListener('click', handler);
244
- *
245
- * return () => {
246
- * document.body.removeEventListener('click', handler);
247
- * };
248
- * });
249
- * ```
250
- *
251
- * @param callback - The callback function used to describe the callback logic
252
- * The callback function is passed an object with the following properties:
253
- *
254
- * - `receive` - A function that can send events back to the parent actor; the
255
- * listener is then called whenever events are received by the callback
256
- * actor
257
- * - `sendBack` - A function that can send events back to the parent actor
258
- * - `input` - Data that was provided to the callback actor
259
- * - `self` - The parent actor of the callback actor
260
- * - `system` - The actor system to which the callback actor belongs The callback
261
- * function can (optionally) return a cleanup function, which is called
262
- * when the actor is stopped.
263
- *
264
- * @returns Callback logic
265
- * @see {@link CallbackLogicFunction} for more information about the callback function and its object argument
266
- * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
267
- */
268
- function fromCallback(callback) {
269
- const logic = {
270
- config: callback,
271
- start: (state, actorScope) => {
272
- const {
273
- self,
274
- system,
275
- emit
276
- } = actorScope;
277
- const callbackState = {
278
- receivers: undefined,
279
- dispose: undefined
280
- };
281
- instanceStates.set(self, callbackState);
282
- callbackState.dispose = callback({
283
- input: state.input,
284
- system,
285
- self,
286
- sendBack: event => {
287
- if (self.getSnapshot().status === 'stopped') {
288
- return;
289
- }
290
- if (self._parent) {
291
- system._relay(self, self._parent, event);
292
- }
293
- },
294
- receive: listener => {
295
- callbackState.receivers ??= new Set();
296
- callbackState.receivers.add(listener);
297
- },
298
- emit
299
- });
300
- },
301
- transition: (state, event, actorScope) => {
302
- const callbackState = instanceStates.get(actorScope.self);
303
- if (event.type === dist_xstateGuards.XSTATE_STOP) {
304
- state = {
305
- ...state,
306
- status: 'stopped',
307
- error: undefined
308
- };
309
- instanceStates.delete(actorScope.self);
310
- callbackState.receivers?.clear();
311
- callbackState.dispose?.();
312
- return state;
313
- }
314
- callbackState.receivers?.forEach(receiver => receiver(event));
315
- return state;
316
- },
317
- getInitialSnapshot: (_, input) => {
318
- return {
319
- status: 'active',
320
- output: undefined,
321
- error: undefined,
322
- input
323
- };
324
- },
325
- getPersistedSnapshot: snapshot => snapshot,
326
- restoreSnapshot: snapshot => snapshot
327
- };
328
- return logic;
329
- }
330
-
331
- const XSTATE_OBSERVABLE_NEXT = 'xstate.observable.next';
332
- const XSTATE_OBSERVABLE_ERROR = 'xstate.observable.error';
333
- const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
334
-
335
- /**
336
- * Represents an actor created by `fromObservable` or `fromEventObservable`.
337
- *
338
- * The type of `self` within the actor's logic.
339
- *
340
- * @example
341
- *
342
- * ```ts
343
- * import { fromObservable, createActor } from 'xstate';
344
- * import { interval } from 'rxjs';
345
- *
346
- * // The type of the value observed by the actor's logic.
347
- * type Context = number;
348
- * // The actor's input.
349
- * type Input = { period?: number };
350
- *
351
- * // Actor logic that observes a number incremented every `input.period`
352
- * // milliseconds (default: 1_000).
353
- * const logic = fromObservable<Context, Input>(({ input, self }) => {
354
- * self;
355
- * // ^? ObservableActorRef<Event, Input>
356
- *
357
- * return interval(input.period ?? 1_000);
358
- * });
359
- *
360
- * const actor = createActor(logic, { input: { period: 2_000 } });
361
- * // ^? ObservableActorRef<Event, Input>
362
- * ```
363
- *
364
- * @see {@link fromObservable}
365
- * @see {@link fromEventObservable}
366
- */
367
-
368
- /**
369
- * Observable actor logic is described by an observable stream of values. Actors
370
- * created from observable logic (“observable actors”) can:
371
- *
372
- * - Emit snapshots of the observable’s emitted value
373
- *
374
- * The observable’s emitted value is used as its observable actor’s `context`.
375
- *
376
- * Sending events to observable actors will have no effect.
377
- *
378
- * @example
379
- *
380
- * ```ts
381
- * import { fromObservable, createActor } from 'xstate';
382
- * import { interval } from 'rxjs';
383
- *
384
- * const logic = fromObservable((obj) => interval(1000));
385
- *
386
- * const actor = createActor(logic);
387
- *
388
- * actor.subscribe((snapshot) => {
389
- * console.log(snapshot.context);
390
- * });
391
- *
392
- * actor.start();
393
- * // At every second:
394
- * // Logs 0
395
- * // Logs 1
396
- * // Logs 2
397
- * // ...
398
- * ```
399
- *
400
- * @param observableCreator A function that creates an observable. It receives
401
- * one argument, an object with the following properties:
402
- *
403
- * - `input` - Data that was provided to the observable actor
404
- * - `self` - The parent actor
405
- * - `system` - The actor system to which the observable actor belongs
406
- *
407
- * It should return a {@link Subscribable}, which is compatible with an RxJS
408
- * Observable, although RxJS is not required to create them.
409
- * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
410
- * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
411
- */
412
- function fromObservable(observableCreator) {
413
- // TODO: add event types
414
- const logic = {
415
- config: observableCreator,
416
- transition: (snapshot, event) => {
417
- if (snapshot.status !== 'active') {
418
- return snapshot;
419
- }
420
- switch (event.type) {
421
- case XSTATE_OBSERVABLE_NEXT:
422
- {
423
- const newSnapshot = {
424
- ...snapshot,
425
- context: event.data
426
- };
427
- return newSnapshot;
428
- }
429
- case XSTATE_OBSERVABLE_ERROR:
430
- return {
431
- ...snapshot,
432
- status: 'error',
433
- error: event.data,
434
- input: undefined,
435
- _subscription: undefined
436
- };
437
- case XSTATE_OBSERVABLE_COMPLETE:
438
- return {
439
- ...snapshot,
440
- status: 'done',
441
- input: undefined,
442
- _subscription: undefined
443
- };
444
- case dist_xstateGuards.XSTATE_STOP:
445
- snapshot._subscription.unsubscribe();
446
- return {
447
- ...snapshot,
448
- status: 'stopped',
449
- input: undefined,
450
- _subscription: undefined
451
- };
452
- default:
453
- return snapshot;
454
- }
455
- },
456
- getInitialSnapshot: (_, input) => {
457
- return {
458
- status: 'active',
459
- output: undefined,
460
- error: undefined,
461
- context: undefined,
462
- input,
463
- _subscription: undefined
464
- };
465
- },
466
- start: (state, {
467
- self,
468
- system,
469
- emit
470
- }) => {
471
- if (state.status === 'done') {
472
- // Do not restart a completed observable
473
- return;
474
- }
475
- state._subscription = observableCreator({
476
- input: state.input,
477
- system,
478
- self,
479
- emit
480
- }).subscribe({
481
- next: value => {
482
- system._relay(self, self, {
483
- type: XSTATE_OBSERVABLE_NEXT,
484
- data: value
485
- });
486
- },
487
- error: err => {
488
- system._relay(self, self, {
489
- type: XSTATE_OBSERVABLE_ERROR,
490
- data: err
491
- });
492
- },
493
- complete: () => {
494
- system._relay(self, self, {
495
- type: XSTATE_OBSERVABLE_COMPLETE
496
- });
497
- }
498
- });
499
- },
500
- getPersistedSnapshot: ({
501
- _subscription,
502
- ...state
503
- }) => state,
504
- restoreSnapshot: state => ({
505
- ...state,
506
- _subscription: undefined
507
- })
508
- };
509
- return logic;
510
- }
511
-
512
- /**
513
- * Creates event observable logic that listens to an observable that delivers
514
- * event objects.
515
- *
516
- * Event observable actor logic is described by an observable stream of
517
- * {@link https://stately.ai/docs/transitions#event-objects | event objects}.
518
- * Actors created from event observable logic (“event observable actors”) can:
519
- *
520
- * - Implicitly send events to its parent actor
521
- * - Emit snapshots of its emitted event objects
522
- *
523
- * Sending events to event observable actors will have no effect.
524
- *
525
- * @example
526
- *
527
- * ```ts
528
- * import {
529
- * fromEventObservable,
530
- * Subscribable,
531
- * EventObject,
532
- * createMachine,
533
- * createActor
534
- * } from 'xstate';
535
- * import { fromEvent } from 'rxjs';
536
- *
537
- * const mouseClickLogic = fromEventObservable(
538
- * () => fromEvent(document.body, 'click') as Subscribable<EventObject>
539
- * );
540
- *
541
- * const canvasMachine = createMachine({
542
- * invoke: {
543
- * // Will send mouse `click` events to the canvas actor
544
- * src: mouseClickLogic
545
- * }
546
- * });
547
- *
548
- * const canvasActor = createActor(canvasMachine);
549
- * canvasActor.start();
550
- * ```
551
- *
552
- * @param lazyObservable A function that creates an observable that delivers
553
- * event objects. It receives one argument, an object with the following
554
- * properties:
555
- *
556
- * - `input` - Data that was provided to the event observable actor
557
- * - `self` - The parent actor
558
- * - `system` - The actor system to which the event observable actor belongs.
559
- *
560
- * It should return a {@link Subscribable}, which is compatible with an RxJS
561
- * Observable, although RxJS is not required to create them.
562
- */
563
- function fromEventObservable(lazyObservable) {
564
- // TODO: event types
565
- const logic = {
566
- config: lazyObservable,
567
- transition: (state, event) => {
568
- if (state.status !== 'active') {
569
- return state;
570
- }
571
- switch (event.type) {
572
- case XSTATE_OBSERVABLE_ERROR:
573
- return {
574
- ...state,
575
- status: 'error',
576
- error: event.data,
577
- input: undefined,
578
- _subscription: undefined
579
- };
580
- case XSTATE_OBSERVABLE_COMPLETE:
581
- return {
582
- ...state,
583
- status: 'done',
584
- input: undefined,
585
- _subscription: undefined
586
- };
587
- case dist_xstateGuards.XSTATE_STOP:
588
- state._subscription.unsubscribe();
589
- return {
590
- ...state,
591
- status: 'stopped',
592
- input: undefined,
593
- _subscription: undefined
594
- };
595
- default:
596
- return state;
597
- }
598
- },
599
- getInitialSnapshot: (_, input) => {
600
- return {
601
- status: 'active',
602
- output: undefined,
603
- error: undefined,
604
- context: undefined,
605
- input,
606
- _subscription: undefined
607
- };
608
- },
609
- start: (state, {
610
- self,
611
- system,
612
- emit
613
- }) => {
614
- if (state.status === 'done') {
615
- // Do not restart a completed observable
616
- return;
617
- }
618
- state._subscription = lazyObservable({
619
- input: state.input,
620
- system,
621
- self,
622
- emit
623
- }).subscribe({
624
- next: value => {
625
- if (self._parent) {
626
- system._relay(self, self._parent, value);
627
- }
628
- },
629
- error: err => {
630
- system._relay(self, self, {
631
- type: XSTATE_OBSERVABLE_ERROR,
632
- data: err
633
- });
634
- },
635
- complete: () => {
636
- system._relay(self, self, {
637
- type: XSTATE_OBSERVABLE_COMPLETE
638
- });
639
- }
640
- });
641
- },
642
- getPersistedSnapshot: ({
643
- _subscription,
644
- ...snapshot
645
- }) => snapshot,
646
- restoreSnapshot: snapshot => ({
647
- ...snapshot,
648
- _subscription: undefined
649
- })
650
- };
651
- return logic;
652
- }
653
-
654
- const XSTATE_PROMISE_RESOLVE = 'xstate.promise.resolve';
655
- const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
656
-
657
- /**
658
- * Represents an actor created by `fromPromise`.
659
- *
660
- * The type of `self` within the actor's logic.
661
- *
662
- * @example
663
- *
664
- * ```ts
665
- * import { fromPromise, createActor } from 'xstate';
666
- *
667
- * // The actor's resolved output
668
- * type Output = string;
669
- * // The actor's input.
670
- * type Input = { message: string };
671
- *
672
- * // Actor logic that fetches the url of an image of a cat saying `input.message`.
673
- * const logic = fromPromise<Output, Input>(async ({ input, self }) => {
674
- * self;
675
- * // ^? PromiseActorRef<Output, Input>
676
- *
677
- * const data = await fetch(
678
- * `https://cataas.com/cat/says/${input.message}`
679
- * );
680
- * const url = await data.json();
681
- * return url;
682
- * });
683
- *
684
- * const actor = createActor(logic, { input: { message: 'hello world' } });
685
- * // ^? PromiseActorRef<Output, Input>
686
- * ```
687
- *
688
- * @see {@link fromPromise}
689
- */
690
-
691
- const controllerMap = new WeakMap();
692
-
693
- /**
694
- * An actor logic creator which returns promise logic as defined by an async
695
- * process that resolves or rejects after some time.
696
- *
697
- * Actors created from promise actor logic (“promise actors”) can:
698
- *
699
- * - Emit the resolved value of the promise
700
- * - Output the resolved value of the promise
701
- *
702
- * Sending events to promise actors will have no effect.
703
- *
704
- * @example
705
- *
706
- * ```ts
707
- * const promiseLogic = fromPromise(async () => {
708
- * const result = await fetch('https://example.com/...').then((data) =>
709
- * data.json()
710
- * );
711
- *
712
- * return result;
713
- * });
714
- *
715
- * const promiseActor = createActor(promiseLogic);
716
- * promiseActor.subscribe((snapshot) => {
717
- * console.log(snapshot);
718
- * });
719
- * promiseActor.start();
720
- * // => {
721
- * // output: undefined,
722
- * // status: 'active'
723
- * // ...
724
- * // }
725
- *
726
- * // After promise resolves
727
- * // => {
728
- * // output: { ... },
729
- * // status: 'done',
730
- * // ...
731
- * // }
732
- * ```
733
- *
734
- * @param promiseCreator A function which returns a Promise, and accepts an
735
- * object with the following properties:
736
- *
737
- * - `input` - Data that was provided to the promise actor
738
- * - `self` - The parent actor of the promise actor
739
- * - `system` - The actor system to which the promise actor belongs
740
- *
741
- * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
742
- */
743
- function fromPromise(promiseCreator) {
744
- const logic = {
745
- config: promiseCreator,
746
- transition: (state, event, scope) => {
747
- if (state.status !== 'active') {
748
- return state;
749
- }
750
- switch (event.type) {
751
- case XSTATE_PROMISE_RESOLVE:
752
- {
753
- const resolvedValue = event.data;
754
- return {
755
- ...state,
756
- status: 'done',
757
- output: resolvedValue,
758
- input: undefined
759
- };
760
- }
761
- case XSTATE_PROMISE_REJECT:
762
- return {
763
- ...state,
764
- status: 'error',
765
- error: event.data,
766
- input: undefined
767
- };
768
- case dist_xstateGuards.XSTATE_STOP:
769
- {
770
- controllerMap.get(scope.self)?.abort();
771
- controllerMap.delete(scope.self);
772
- return {
773
- ...state,
774
- status: 'stopped',
775
- input: undefined
776
- };
777
- }
778
- default:
779
- return state;
780
- }
781
- },
782
- start: (state, {
783
- self,
784
- system,
785
- emit
786
- }) => {
787
- // TODO: determine how to allow customizing this so that promises
788
- // can be restarted if necessary
789
- if (state.status !== 'active') {
790
- return;
791
- }
792
- const controller = new AbortController();
793
- controllerMap.set(self, controller);
794
- const resolvedPromise = Promise.resolve(promiseCreator({
795
- input: state.input,
796
- system,
797
- self,
798
- signal: controller.signal,
799
- emit
800
- }));
801
- resolvedPromise.then(response => {
802
- if (self.getSnapshot().status !== 'active') {
803
- return;
804
- }
805
- controllerMap.delete(self);
806
- system._relay(self, self, {
807
- type: XSTATE_PROMISE_RESOLVE,
808
- data: response
809
- });
810
- }, errorData => {
811
- if (self.getSnapshot().status !== 'active') {
812
- return;
813
- }
814
- controllerMap.delete(self);
815
- system._relay(self, self, {
816
- type: XSTATE_PROMISE_REJECT,
817
- data: errorData
818
- });
819
- });
820
- },
821
- getInitialSnapshot: (_, input) => {
822
- return {
823
- status: 'active',
824
- output: undefined,
825
- error: undefined,
826
- input
827
- };
828
- },
829
- getPersistedSnapshot: snapshot => snapshot,
830
- restoreSnapshot: snapshot => snapshot
831
- };
832
- return logic;
833
- }
834
-
835
- const emptyLogic = fromTransition(_ => undefined, undefined);
836
- function createEmptyActor() {
837
- return dist_xstateGuards.createActor(emptyLogic);
838
- }
839
-
840
- exports.createEmptyActor = createEmptyActor;
841
- exports.fromCallback = fromCallback;
842
- exports.fromEventObservable = fromEventObservable;
843
- exports.fromObservable = fromObservable;
844
- exports.fromPromise = fromPromise;
845
- exports.fromTransition = fromTransition;
9
+ exports.TimeoutError = dist_xstateActors.TimeoutError;
10
+ exports.createAsyncLogic = dist_xstateActors.createAsyncLogic;
11
+ exports.createCallbackLogic = dist_xstateActors.createCallbackLogic;
12
+ exports.createEmptyActor = dist_xstateActors.createEmptyActor;
13
+ exports.createEventObservableLogic = dist_xstateActors.createEventObservableLogic;
14
+ exports.createListenerLogic = dist_xstateActors.createListenerLogic;
15
+ exports.createLogic = dist_xstateActors.createLogic;
16
+ exports.createObservableLogic = dist_xstateActors.createObservableLogic;
17
+ exports.createSubscriptionLogic = dist_xstateActors.createSubscriptionLogic;
18
+ exports.listenerLogic = dist_xstateActors.listenerLogic;
19
+ exports.subscriptionLogic = dist_xstateActors.subscriptionLogic;