xstate 5.32.2 → 6.0.0-alpha.10

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