xstate 5.0.0-beta.44 → 5.0.0-beta.46

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 (51) hide show
  1. package/actions/dist/xstate-actions.cjs.js +2 -2
  2. package/actions/dist/xstate-actions.development.cjs.js +2 -2
  3. package/actions/dist/xstate-actions.development.esm.js +2 -2
  4. package/actions/dist/xstate-actions.esm.js +2 -2
  5. package/actions/dist/xstate-actions.umd.min.js +1 -1
  6. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  7. package/actors/dist/xstate-actors.cjs.js +134 -9
  8. package/actors/dist/xstate-actors.development.cjs.js +134 -9
  9. package/actors/dist/xstate-actors.development.esm.js +134 -9
  10. package/actors/dist/xstate-actors.esm.js +134 -9
  11. package/actors/dist/xstate-actors.umd.min.js +1 -1
  12. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  13. package/dist/declarations/src/State.d.ts +18 -28
  14. package/dist/declarations/src/StateMachine.d.ts +11 -29
  15. package/dist/declarations/src/StateNode.d.ts +8 -6
  16. package/dist/declarations/src/actions/choose.d.ts +3 -3
  17. package/dist/declarations/src/actions/pure.d.ts +4 -4
  18. package/dist/declarations/src/actors/observable.d.ts +80 -4
  19. package/dist/declarations/src/actors/transition.d.ts +53 -4
  20. package/dist/declarations/src/createMachine.d.ts +5 -0
  21. package/dist/declarations/src/guards.d.ts +26 -4
  22. package/dist/declarations/src/index.d.ts +3 -2
  23. package/dist/declarations/src/interpreter.d.ts +1 -0
  24. package/dist/declarations/src/setup.d.ts +35 -0
  25. package/dist/declarations/src/stateUtils.d.ts +7 -7
  26. package/dist/declarations/src/types.d.ts +54 -25
  27. package/dist/declarations/src/utils.d.ts +2 -1
  28. package/dist/{raise-5854eaca.esm.js → raise-1682abb7.esm.js} +99 -135
  29. package/dist/{raise-fb6f017b.cjs.js → raise-a1d3d7e9.cjs.js} +100 -136
  30. package/dist/{raise-ed700d14.development.cjs.js → raise-a9e7e31c.development.cjs.js} +100 -136
  31. package/dist/{raise-348cc74e.development.esm.js → raise-fa23c2b9.development.esm.js} +99 -135
  32. package/dist/{send-53e5693c.cjs.js → send-2fa3a204.cjs.js} +24 -28
  33. package/dist/{send-00466e37.development.cjs.js → send-5b256a89.development.cjs.js} +24 -28
  34. package/dist/{send-a0193bdb.development.esm.js → send-9acdf858.development.esm.js} +24 -28
  35. package/dist/{send-b7b4befa.esm.js → send-a237e4e8.esm.js} +24 -28
  36. package/dist/xstate.cjs.js +102 -92
  37. package/dist/xstate.cjs.mjs +2 -0
  38. package/dist/xstate.development.cjs.js +102 -92
  39. package/dist/xstate.development.cjs.mjs +2 -0
  40. package/dist/xstate.development.esm.js +97 -89
  41. package/dist/xstate.esm.js +97 -89
  42. package/dist/xstate.umd.min.js +1 -1
  43. package/dist/xstate.umd.min.js.map +1 -1
  44. package/guards/dist/xstate-guards.cjs.js +1 -1
  45. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  46. package/guards/dist/xstate-guards.development.esm.js +1 -1
  47. package/guards/dist/xstate-guards.esm.js +1 -1
  48. package/guards/dist/xstate-guards.umd.min.js +1 -1
  49. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  50. package/package.json +1 -1
  51. package/dist/declarations/src/Machine.d.ts +0 -5
@@ -2,17 +2,66 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('../../dist/raise-fb6f017b.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-a1d3d7e9.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.cjs.js');
7
7
 
8
8
  /**
9
- * Returns actor logic from a transition function and its initial state.
9
+ * Returns actor logic given a transition function and its initial state.
10
10
  *
11
- * A transition function is a function that takes the current state and an event and returns the next state.
11
+ * A transition function is a function that takes the current `state` and received `event` object as arguments, and returns the next state, similar to a reducer.
12
12
  *
13
- * @param transition The transition function that returns the next state given the current state and event.
14
- * @param initialContext The initial state of the transition function.
13
+ * Actors created from transition logic (“transition actors”) can:
14
+ *
15
+ * - Receive events
16
+ * - Emit snapshots of its state
17
+ *
18
+ * The transition function’s `state` is used as its transition actor’s `context`.
19
+ *
20
+ * Note that the "state" for a transition function is provided by the initial state argument, and is not the same as the State object of an actor or a state within a machine configuration.
21
+ *
22
+ * @param transition The transition function used to describe the transition logic. It should return the next state given the current state and event. It receives the following arguments:
23
+ * - `state` - the current state.
24
+ * - `event` - the received event.
25
+ * - `actorScope` - the actor scope object, with properties like `self` and `system`.
26
+ * @param initialContext The initial state of the transition function, either an object representing the state, or a function which returns a state object. If a function, it will receive as its only argument an object with the following properties:
27
+ * - `input` - the `input` provided to its parent transition actor.
28
+ * - `self` - a reference to its parent transition actor.
29
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
15
30
  * @returns Actor logic
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const transitionLogic = fromTransition(
35
+ * (state, event) => {
36
+ * if (event.type === 'increment') {
37
+ * return {
38
+ * ...state,
39
+ * count: state.count + 1,
40
+ * };
41
+ * }
42
+ * return state;
43
+ * },
44
+ * { count: 0 },
45
+ * );
46
+ *
47
+ * const transitionActor = createActor(transitionLogic);
48
+ * transitionActor.subscribe((snapshot) => {
49
+ * console.log(snapshot);
50
+ * });
51
+ * transitionActor.start();
52
+ * // => {
53
+ * // status: 'active',
54
+ * // context: { count: 0 },
55
+ * // ...
56
+ * // }
57
+ *
58
+ * transitionActor.send({ type: 'increment' });
59
+ * // => {
60
+ * // status: 'active',
61
+ * // context: { count: 1 },
62
+ * // ...
63
+ * // }
64
+ * ```
16
65
  */
17
66
  function fromTransition(transition, initialContext) {
18
67
  return {
@@ -169,6 +218,46 @@ function fromCallback(invokeCallback) {
169
218
  return logic;
170
219
  }
171
220
 
221
+ /**
222
+ * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
223
+ *
224
+ * - Emit snapshots of the observable’s emitted value
225
+ *
226
+ * The observable’s emitted value is used as its observable actor’s `context`.
227
+ *
228
+ * Sending events to observable actors will have no effect.
229
+ *
230
+ * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
231
+ * - `input` - Data that was provided to the observable actor
232
+ * - `self` - The parent actor
233
+ * - `system` - The actor system to which the observable actor belongs
234
+ *
235
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * import { fromObservable, createActor } from 'xstate'
240
+ * import { interval } from 'rxjs';
241
+ *
242
+ * const logic = fromObservable((obj) => interval(1000));
243
+ *
244
+ * const actor = createActor(logic);
245
+ *
246
+ * actor.subscribe((snapshot) => {
247
+ * console.log(snapshot.context);
248
+ * });
249
+ *
250
+ * actor.start();
251
+ * // At every second:
252
+ * // Logs 0
253
+ * // Logs 1
254
+ * // Logs 2
255
+ * // ...
256
+ * ```
257
+ *
258
+ * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
259
+ * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
260
+ */
172
261
  function fromObservable(observableCreator) {
173
262
  const nextEventType = '$$xstate.next';
174
263
  const errorEventType = '$$xstate.error';
@@ -277,12 +366,48 @@ function fromObservable(observableCreator) {
277
366
  }
278
367
 
279
368
  /**
280
- * Creates event observable logic that listens to an observable
281
- * that delivers event objects.
369
+ * Creates event observable logic that listens to an observable that delivers event objects.
282
370
  *
371
+ * Event observable actor logic is described by an observable stream of {@link https://stately.ai/docs/transitions#event-objects | event objects}. Actors created from event observable logic (“event observable actors”) can:
283
372
  *
284
- * @param lazyObservable A function that creates an observable
285
- * @returns Event observable logic
373
+ * - Implicitly send events to its parent actor
374
+ * - Emit snapshots of its emitted event objects
375
+ *
376
+ * Sending events to event observable actors will have no effect.
377
+ *
378
+ * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
379
+ *
380
+ * - `input` - Data that was provided to the event observable actor
381
+ * - `self` - The parent actor
382
+ * - `system` - The actor system to which the event observable actor belongs.
383
+ *
384
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
385
+ *
386
+ * @example
387
+ * ```ts
388
+ * import {
389
+ * fromEventObservable,
390
+ * Subscribable,
391
+ * EventObject,
392
+ * createMachine,
393
+ * createActor
394
+ * } from 'xstate';
395
+ * import { fromEvent } from 'rxjs';
396
+ *
397
+ * const mouseClickLogic = fromEventObservable(() =>
398
+ * fromEvent(document.body, 'click') as Subscribable<EventObject>
399
+ * );
400
+ *
401
+ * const canvasMachine = createMachine({
402
+ * invoke: {
403
+ * // Will send mouse `click` events to the canvas actor
404
+ * src: mouseClickLogic,
405
+ * }
406
+ * });
407
+ *
408
+ * const canvasActor = createActor(canvasMachine);
409
+ * canvasActor.start();
410
+ * ```
286
411
  */
287
412
 
288
413
  function fromEventObservable(lazyObservable) {
@@ -2,17 +2,66 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var guards_dist_xstateGuards = require('../../dist/raise-ed700d14.development.cjs.js');
5
+ var guards_dist_xstateGuards = require('../../dist/raise-a9e7e31c.development.cjs.js');
6
6
  require('../../dev/dist/xstate-dev.development.cjs.js');
7
7
 
8
8
  /**
9
- * Returns actor logic from a transition function and its initial state.
9
+ * Returns actor logic given a transition function and its initial state.
10
10
  *
11
- * A transition function is a function that takes the current state and an event and returns the next state.
11
+ * A transition function is a function that takes the current `state` and received `event` object as arguments, and returns the next state, similar to a reducer.
12
12
  *
13
- * @param transition The transition function that returns the next state given the current state and event.
14
- * @param initialContext The initial state of the transition function.
13
+ * Actors created from transition logic (“transition actors”) can:
14
+ *
15
+ * - Receive events
16
+ * - Emit snapshots of its state
17
+ *
18
+ * The transition function’s `state` is used as its transition actor’s `context`.
19
+ *
20
+ * Note that the "state" for a transition function is provided by the initial state argument, and is not the same as the State object of an actor or a state within a machine configuration.
21
+ *
22
+ * @param transition The transition function used to describe the transition logic. It should return the next state given the current state and event. It receives the following arguments:
23
+ * - `state` - the current state.
24
+ * - `event` - the received event.
25
+ * - `actorScope` - the actor scope object, with properties like `self` and `system`.
26
+ * @param initialContext The initial state of the transition function, either an object representing the state, or a function which returns a state object. If a function, it will receive as its only argument an object with the following properties:
27
+ * - `input` - the `input` provided to its parent transition actor.
28
+ * - `self` - a reference to its parent transition actor.
29
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
15
30
  * @returns Actor logic
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const transitionLogic = fromTransition(
35
+ * (state, event) => {
36
+ * if (event.type === 'increment') {
37
+ * return {
38
+ * ...state,
39
+ * count: state.count + 1,
40
+ * };
41
+ * }
42
+ * return state;
43
+ * },
44
+ * { count: 0 },
45
+ * );
46
+ *
47
+ * const transitionActor = createActor(transitionLogic);
48
+ * transitionActor.subscribe((snapshot) => {
49
+ * console.log(snapshot);
50
+ * });
51
+ * transitionActor.start();
52
+ * // => {
53
+ * // status: 'active',
54
+ * // context: { count: 0 },
55
+ * // ...
56
+ * // }
57
+ *
58
+ * transitionActor.send({ type: 'increment' });
59
+ * // => {
60
+ * // status: 'active',
61
+ * // context: { count: 1 },
62
+ * // ...
63
+ * // }
64
+ * ```
16
65
  */
17
66
  function fromTransition(transition, initialContext) {
18
67
  return {
@@ -169,6 +218,46 @@ function fromCallback(invokeCallback) {
169
218
  return logic;
170
219
  }
171
220
 
221
+ /**
222
+ * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
223
+ *
224
+ * - Emit snapshots of the observable’s emitted value
225
+ *
226
+ * The observable’s emitted value is used as its observable actor’s `context`.
227
+ *
228
+ * Sending events to observable actors will have no effect.
229
+ *
230
+ * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
231
+ * - `input` - Data that was provided to the observable actor
232
+ * - `self` - The parent actor
233
+ * - `system` - The actor system to which the observable actor belongs
234
+ *
235
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * import { fromObservable, createActor } from 'xstate'
240
+ * import { interval } from 'rxjs';
241
+ *
242
+ * const logic = fromObservable((obj) => interval(1000));
243
+ *
244
+ * const actor = createActor(logic);
245
+ *
246
+ * actor.subscribe((snapshot) => {
247
+ * console.log(snapshot.context);
248
+ * });
249
+ *
250
+ * actor.start();
251
+ * // At every second:
252
+ * // Logs 0
253
+ * // Logs 1
254
+ * // Logs 2
255
+ * // ...
256
+ * ```
257
+ *
258
+ * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
259
+ * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
260
+ */
172
261
  function fromObservable(observableCreator) {
173
262
  const nextEventType = '$$xstate.next';
174
263
  const errorEventType = '$$xstate.error';
@@ -277,12 +366,48 @@ function fromObservable(observableCreator) {
277
366
  }
278
367
 
279
368
  /**
280
- * Creates event observable logic that listens to an observable
281
- * that delivers event objects.
369
+ * Creates event observable logic that listens to an observable that delivers event objects.
282
370
  *
371
+ * Event observable actor logic is described by an observable stream of {@link https://stately.ai/docs/transitions#event-objects | event objects}. Actors created from event observable logic (“event observable actors”) can:
283
372
  *
284
- * @param lazyObservable A function that creates an observable
285
- * @returns Event observable logic
373
+ * - Implicitly send events to its parent actor
374
+ * - Emit snapshots of its emitted event objects
375
+ *
376
+ * Sending events to event observable actors will have no effect.
377
+ *
378
+ * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
379
+ *
380
+ * - `input` - Data that was provided to the event observable actor
381
+ * - `self` - The parent actor
382
+ * - `system` - The actor system to which the event observable actor belongs.
383
+ *
384
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
385
+ *
386
+ * @example
387
+ * ```ts
388
+ * import {
389
+ * fromEventObservable,
390
+ * Subscribable,
391
+ * EventObject,
392
+ * createMachine,
393
+ * createActor
394
+ * } from 'xstate';
395
+ * import { fromEvent } from 'rxjs';
396
+ *
397
+ * const mouseClickLogic = fromEventObservable(() =>
398
+ * fromEvent(document.body, 'click') as Subscribable<EventObject>
399
+ * );
400
+ *
401
+ * const canvasMachine = createMachine({
402
+ * invoke: {
403
+ * // Will send mouse `click` events to the canvas actor
404
+ * src: mouseClickLogic,
405
+ * }
406
+ * });
407
+ *
408
+ * const canvasActor = createActor(canvasMachine);
409
+ * canvasActor.start();
410
+ * ```
286
411
  */
287
412
 
288
413
  function fromEventObservable(lazyObservable) {
@@ -1,14 +1,63 @@
1
- import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-348cc74e.development.esm.js';
1
+ import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-fa23c2b9.development.esm.js';
2
2
  import '../../dev/dist/xstate-dev.development.esm.js';
3
3
 
4
4
  /**
5
- * Returns actor logic from a transition function and its initial state.
5
+ * Returns actor logic given a transition function and its initial state.
6
6
  *
7
- * A transition function is a function that takes the current state and an event and returns the next state.
7
+ * A transition function is a function that takes the current `state` and received `event` object as arguments, and returns the next state, similar to a reducer.
8
8
  *
9
- * @param transition The transition function that returns the next state given the current state and event.
10
- * @param initialContext The initial state of the transition function.
9
+ * Actors created from transition logic (“transition actors”) can:
10
+ *
11
+ * - Receive events
12
+ * - Emit snapshots of its state
13
+ *
14
+ * The transition function’s `state` is used as its transition actor’s `context`.
15
+ *
16
+ * Note that the "state" for a transition function is provided by the initial state argument, and is not the same as the State object of an actor or a state within a machine configuration.
17
+ *
18
+ * @param transition The transition function used to describe the transition logic. It should return the next state given the current state and event. It receives the following arguments:
19
+ * - `state` - the current state.
20
+ * - `event` - the received event.
21
+ * - `actorScope` - the actor scope object, with properties like `self` and `system`.
22
+ * @param initialContext The initial state of the transition function, either an object representing the state, or a function which returns a state object. If a function, it will receive as its only argument an object with the following properties:
23
+ * - `input` - the `input` provided to its parent transition actor.
24
+ * - `self` - a reference to its parent transition actor.
25
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
11
26
  * @returns Actor logic
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const transitionLogic = fromTransition(
31
+ * (state, event) => {
32
+ * if (event.type === 'increment') {
33
+ * return {
34
+ * ...state,
35
+ * count: state.count + 1,
36
+ * };
37
+ * }
38
+ * return state;
39
+ * },
40
+ * { count: 0 },
41
+ * );
42
+ *
43
+ * const transitionActor = createActor(transitionLogic);
44
+ * transitionActor.subscribe((snapshot) => {
45
+ * console.log(snapshot);
46
+ * });
47
+ * transitionActor.start();
48
+ * // => {
49
+ * // status: 'active',
50
+ * // context: { count: 0 },
51
+ * // ...
52
+ * // }
53
+ *
54
+ * transitionActor.send({ type: 'increment' });
55
+ * // => {
56
+ * // status: 'active',
57
+ * // context: { count: 1 },
58
+ * // ...
59
+ * // }
60
+ * ```
12
61
  */
13
62
  function fromTransition(transition, initialContext) {
14
63
  return {
@@ -165,6 +214,46 @@ function fromCallback(invokeCallback) {
165
214
  return logic;
166
215
  }
167
216
 
217
+ /**
218
+ * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
219
+ *
220
+ * - Emit snapshots of the observable’s emitted value
221
+ *
222
+ * The observable’s emitted value is used as its observable actor’s `context`.
223
+ *
224
+ * Sending events to observable actors will have no effect.
225
+ *
226
+ * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
227
+ * - `input` - Data that was provided to the observable actor
228
+ * - `self` - The parent actor
229
+ * - `system` - The actor system to which the observable actor belongs
230
+ *
231
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * import { fromObservable, createActor } from 'xstate'
236
+ * import { interval } from 'rxjs';
237
+ *
238
+ * const logic = fromObservable((obj) => interval(1000));
239
+ *
240
+ * const actor = createActor(logic);
241
+ *
242
+ * actor.subscribe((snapshot) => {
243
+ * console.log(snapshot.context);
244
+ * });
245
+ *
246
+ * actor.start();
247
+ * // At every second:
248
+ * // Logs 0
249
+ * // Logs 1
250
+ * // Logs 2
251
+ * // ...
252
+ * ```
253
+ *
254
+ * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
255
+ * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
256
+ */
168
257
  function fromObservable(observableCreator) {
169
258
  const nextEventType = '$$xstate.next';
170
259
  const errorEventType = '$$xstate.error';
@@ -273,12 +362,48 @@ function fromObservable(observableCreator) {
273
362
  }
274
363
 
275
364
  /**
276
- * Creates event observable logic that listens to an observable
277
- * that delivers event objects.
365
+ * Creates event observable logic that listens to an observable that delivers event objects.
278
366
  *
367
+ * Event observable actor logic is described by an observable stream of {@link https://stately.ai/docs/transitions#event-objects | event objects}. Actors created from event observable logic (“event observable actors”) can:
279
368
  *
280
- * @param lazyObservable A function that creates an observable
281
- * @returns Event observable logic
369
+ * - Implicitly send events to its parent actor
370
+ * - Emit snapshots of its emitted event objects
371
+ *
372
+ * Sending events to event observable actors will have no effect.
373
+ *
374
+ * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
375
+ *
376
+ * - `input` - Data that was provided to the event observable actor
377
+ * - `self` - The parent actor
378
+ * - `system` - The actor system to which the event observable actor belongs.
379
+ *
380
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * import {
385
+ * fromEventObservable,
386
+ * Subscribable,
387
+ * EventObject,
388
+ * createMachine,
389
+ * createActor
390
+ * } from 'xstate';
391
+ * import { fromEvent } from 'rxjs';
392
+ *
393
+ * const mouseClickLogic = fromEventObservable(() =>
394
+ * fromEvent(document.body, 'click') as Subscribable<EventObject>
395
+ * );
396
+ *
397
+ * const canvasMachine = createMachine({
398
+ * invoke: {
399
+ * // Will send mouse `click` events to the canvas actor
400
+ * src: mouseClickLogic,
401
+ * }
402
+ * });
403
+ *
404
+ * const canvasActor = createActor(canvasMachine);
405
+ * canvasActor.start();
406
+ * ```
282
407
  */
283
408
 
284
409
  function fromEventObservable(lazyObservable) {
@@ -1,14 +1,63 @@
1
- import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-5854eaca.esm.js';
1
+ import { X as XSTATE_STOP, C as createActor } from '../../dist/raise-1682abb7.esm.js';
2
2
  import '../../dev/dist/xstate-dev.esm.js';
3
3
 
4
4
  /**
5
- * Returns actor logic from a transition function and its initial state.
5
+ * Returns actor logic given a transition function and its initial state.
6
6
  *
7
- * A transition function is a function that takes the current state and an event and returns the next state.
7
+ * A transition function is a function that takes the current `state` and received `event` object as arguments, and returns the next state, similar to a reducer.
8
8
  *
9
- * @param transition The transition function that returns the next state given the current state and event.
10
- * @param initialContext The initial state of the transition function.
9
+ * Actors created from transition logic (“transition actors”) can:
10
+ *
11
+ * - Receive events
12
+ * - Emit snapshots of its state
13
+ *
14
+ * The transition function’s `state` is used as its transition actor’s `context`.
15
+ *
16
+ * Note that the "state" for a transition function is provided by the initial state argument, and is not the same as the State object of an actor or a state within a machine configuration.
17
+ *
18
+ * @param transition The transition function used to describe the transition logic. It should return the next state given the current state and event. It receives the following arguments:
19
+ * - `state` - the current state.
20
+ * - `event` - the received event.
21
+ * - `actorScope` - the actor scope object, with properties like `self` and `system`.
22
+ * @param initialContext The initial state of the transition function, either an object representing the state, or a function which returns a state object. If a function, it will receive as its only argument an object with the following properties:
23
+ * - `input` - the `input` provided to its parent transition actor.
24
+ * - `self` - a reference to its parent transition actor.
25
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
11
26
  * @returns Actor logic
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const transitionLogic = fromTransition(
31
+ * (state, event) => {
32
+ * if (event.type === 'increment') {
33
+ * return {
34
+ * ...state,
35
+ * count: state.count + 1,
36
+ * };
37
+ * }
38
+ * return state;
39
+ * },
40
+ * { count: 0 },
41
+ * );
42
+ *
43
+ * const transitionActor = createActor(transitionLogic);
44
+ * transitionActor.subscribe((snapshot) => {
45
+ * console.log(snapshot);
46
+ * });
47
+ * transitionActor.start();
48
+ * // => {
49
+ * // status: 'active',
50
+ * // context: { count: 0 },
51
+ * // ...
52
+ * // }
53
+ *
54
+ * transitionActor.send({ type: 'increment' });
55
+ * // => {
56
+ * // status: 'active',
57
+ * // context: { count: 1 },
58
+ * // ...
59
+ * // }
60
+ * ```
12
61
  */
13
62
  function fromTransition(transition, initialContext) {
14
63
  return {
@@ -165,6 +214,46 @@ function fromCallback(invokeCallback) {
165
214
  return logic;
166
215
  }
167
216
 
217
+ /**
218
+ * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
219
+ *
220
+ * - Emit snapshots of the observable’s emitted value
221
+ *
222
+ * The observable’s emitted value is used as its observable actor’s `context`.
223
+ *
224
+ * Sending events to observable actors will have no effect.
225
+ *
226
+ * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
227
+ * - `input` - Data that was provided to the observable actor
228
+ * - `self` - The parent actor
229
+ * - `system` - The actor system to which the observable actor belongs
230
+ *
231
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
232
+ *
233
+ * @example
234
+ * ```ts
235
+ * import { fromObservable, createActor } from 'xstate'
236
+ * import { interval } from 'rxjs';
237
+ *
238
+ * const logic = fromObservable((obj) => interval(1000));
239
+ *
240
+ * const actor = createActor(logic);
241
+ *
242
+ * actor.subscribe((snapshot) => {
243
+ * console.log(snapshot.context);
244
+ * });
245
+ *
246
+ * actor.start();
247
+ * // At every second:
248
+ * // Logs 0
249
+ * // Logs 1
250
+ * // Logs 2
251
+ * // ...
252
+ * ```
253
+ *
254
+ * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
255
+ * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
256
+ */
168
257
  function fromObservable(observableCreator) {
169
258
  const nextEventType = '$$xstate.next';
170
259
  const errorEventType = '$$xstate.error';
@@ -273,12 +362,48 @@ function fromObservable(observableCreator) {
273
362
  }
274
363
 
275
364
  /**
276
- * Creates event observable logic that listens to an observable
277
- * that delivers event objects.
365
+ * Creates event observable logic that listens to an observable that delivers event objects.
278
366
  *
367
+ * Event observable actor logic is described by an observable stream of {@link https://stately.ai/docs/transitions#event-objects | event objects}. Actors created from event observable logic (“event observable actors”) can:
279
368
  *
280
- * @param lazyObservable A function that creates an observable
281
- * @returns Event observable logic
369
+ * - Implicitly send events to its parent actor
370
+ * - Emit snapshots of its emitted event objects
371
+ *
372
+ * Sending events to event observable actors will have no effect.
373
+ *
374
+ * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
375
+ *
376
+ * - `input` - Data that was provided to the event observable actor
377
+ * - `self` - The parent actor
378
+ * - `system` - The actor system to which the event observable actor belongs.
379
+ *
380
+ * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * import {
385
+ * fromEventObservable,
386
+ * Subscribable,
387
+ * EventObject,
388
+ * createMachine,
389
+ * createActor
390
+ * } from 'xstate';
391
+ * import { fromEvent } from 'rxjs';
392
+ *
393
+ * const mouseClickLogic = fromEventObservable(() =>
394
+ * fromEvent(document.body, 'click') as Subscribable<EventObject>
395
+ * );
396
+ *
397
+ * const canvasMachine = createMachine({
398
+ * invoke: {
399
+ * // Will send mouse `click` events to the canvas actor
400
+ * src: mouseClickLogic,
401
+ * }
402
+ * });
403
+ *
404
+ * const canvasActor = createActor(canvasMachine);
405
+ * canvasActor.start();
406
+ * ```
282
407
  */
283
408
 
284
409
  function fromEventObservable(lazyObservable) {