xstate 5.14.0 → 5.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) 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 +272 -65
  8. package/actors/dist/xstate-actors.development.cjs.js +272 -65
  9. package/actors/dist/xstate-actors.development.esm.js +272 -65
  10. package/actors/dist/xstate-actors.esm.js +272 -65
  11. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  12. package/dist/declarations/src/State.d.ts +23 -27
  13. package/dist/declarations/src/StateMachine.d.ts +19 -31
  14. package/dist/declarations/src/StateNode.d.ts +29 -51
  15. package/dist/declarations/src/actions/assign.d.ts +29 -27
  16. package/dist/declarations/src/actions/cancel.d.ts +28 -22
  17. package/dist/declarations/src/actions/emit.d.ts +33 -33
  18. package/dist/declarations/src/actions/enqueueActions.d.ts +21 -18
  19. package/dist/declarations/src/actions/log.d.ts +5 -4
  20. package/dist/declarations/src/actions/send.d.ts +5 -3
  21. package/dist/declarations/src/actors/callback.d.ts +65 -26
  22. package/dist/declarations/src/actors/index.d.ts +4 -4
  23. package/dist/declarations/src/actors/observable.d.ts +65 -22
  24. package/dist/declarations/src/actors/promise.d.ts +86 -9
  25. package/dist/declarations/src/actors/transition.d.ts +89 -15
  26. package/dist/declarations/src/assert.d.ts +21 -20
  27. package/dist/declarations/src/createActor.d.ts +51 -42
  28. package/dist/declarations/src/createMachine.d.ts +40 -54
  29. package/dist/declarations/src/getNextSnapshot.d.ts +27 -24
  30. package/dist/declarations/src/guards.d.ts +67 -66
  31. package/dist/declarations/src/index.d.ts +1 -2
  32. package/dist/declarations/src/inspection.d.ts +1 -0
  33. package/dist/declarations/src/setup.d.ts +1 -2
  34. package/dist/declarations/src/spawn.d.ts +2 -5
  35. package/dist/declarations/src/stateUtils.d.ts +5 -10
  36. package/dist/declarations/src/toPromise.d.ts +1 -0
  37. package/dist/declarations/src/types.d.ts +164 -189
  38. package/dist/declarations/src/waitFor.d.ts +9 -9
  39. package/dist/{log-c943e6aa.development.esm.js → log-17f4495d.development.esm.js} +115 -117
  40. package/dist/{log-505687fd.development.cjs.js → log-31321d85.development.cjs.js} +115 -117
  41. package/dist/{log-b87cb6bd.esm.js → log-8320f5e6.esm.js} +115 -117
  42. package/dist/{log-7ae0ddf8.cjs.js → log-f9587b82.cjs.js} +115 -117
  43. package/dist/{raise-4e39e875.esm.js → raise-2cfe6b8f.esm.js} +164 -154
  44. package/dist/{raise-0f400094.development.esm.js → raise-7d030497.development.esm.js} +164 -154
  45. package/dist/{raise-f79d2832.cjs.js → raise-a6298350.cjs.js} +164 -154
  46. package/dist/{raise-0cd7e521.development.cjs.js → raise-bad6a97b.development.cjs.js} +164 -154
  47. package/dist/xstate.cjs.js +134 -157
  48. package/dist/xstate.development.cjs.js +134 -157
  49. package/dist/xstate.development.esm.js +136 -159
  50. package/dist/xstate.esm.js +136 -159
  51. package/dist/xstate.umd.min.js +1 -1
  52. package/dist/xstate.umd.min.js.map +1 -1
  53. package/guards/dist/xstate-guards.cjs.js +1 -1
  54. package/guards/dist/xstate-guards.development.cjs.js +1 -1
  55. package/guards/dist/xstate-guards.development.esm.js +1 -1
  56. package/guards/dist/xstate-guards.esm.js +1 -1
  57. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  58. package/package.json +1 -1
  59. package/dist/declarations/src/typegenTypes.d.ts +0 -168
@@ -4,6 +4,40 @@ export type CallbackSnapshot<TInput> = Snapshot<undefined> & {
4
4
  input: TInput;
5
5
  };
6
6
  export type CallbackActorLogic<TEvent extends EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<CallbackSnapshot<TInput>, TEvent, TInput, AnyActorSystem, TEmitted>;
7
+ /**
8
+ * Represents an actor created by `fromCallback`.
9
+ *
10
+ * The type of `self` within the actor's logic.
11
+ *
12
+ * @example
13
+ *
14
+ * ```ts
15
+ * import { fromCallback, createActor } from 'xstate';
16
+ *
17
+ * // The events the actor receives.
18
+ * type Event = { type: 'someEvent' };
19
+ * // The actor's input.
20
+ * type Input = { name: string };
21
+ *
22
+ * // Actor logic that logs whenever it receives an event of type `someEvent`.
23
+ * const logic = fromCallback<Event, Input>(({ self, input, receive }) => {
24
+ * self;
25
+ * // ^? CallbackActorRef<Event, Input>
26
+ *
27
+ * receive((event) => {
28
+ * if (event.type === 'someEvent') {
29
+ * console.log(`${input.name}: received "someEvent" event`);
30
+ * // logs 'myActor: received "someEvent" event'
31
+ * }
32
+ * });
33
+ * });
34
+ *
35
+ * const actor = createActor(logic, { input: { name: 'myActor' } });
36
+ * // ^? CallbackActorRef<Event, Input>
37
+ * ```
38
+ *
39
+ * @see {@link fromCallback}
40
+ */
7
41
  export type CallbackActorRef<TEvent extends EventObject, TInput = NonReducibleUnknown> = ActorRefFrom<CallbackActorLogic<TEvent, TInput>>;
8
42
  export type Receiver<TEvent extends EventObject> = (listener: {
9
43
  bivarianceHack(event: TEvent): void;
@@ -11,58 +45,45 @@ export type Receiver<TEvent extends EventObject> = (listener: {
11
45
  export type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ({ input, system, self, sendBack, receive, emit }: {
12
46
  /**
13
47
  * Data that was provided to the callback actor
48
+ *
14
49
  * @see {@link https://stately.ai/docs/input | Input docs}
15
50
  */
16
51
  input: TInput;
17
- /**
18
- * The actor system to which the callback actor belongs
19
- */
52
+ /** The actor system to which the callback actor belongs */
20
53
  system: AnyActorSystem;
21
- /**
22
- * The parent actor of the callback actor
23
- */
54
+ /** The parent actor of the callback actor */
24
55
  self: CallbackActorRef<TEvent>;
25
- /**
26
- * A function that can send events back to the parent actor
27
- */
56
+ /** A function that can send events back to the parent actor */
28
57
  sendBack: (event: TSentEvent) => void;
29
58
  /**
30
- * A function that can be called with a listener function argument;
31
- * the listener is then called whenever events are received by the callback actor
59
+ * A function that can be called with a listener function argument; the
60
+ * listener is then called whenever events are received by the callback actor
32
61
  */
33
62
  receive: Receiver<TEvent>;
34
63
  emit: (emitted: TEmitted) => void;
35
64
  }) => (() => void) | void;
36
65
  /**
37
- * An actor logic creator which returns callback logic as defined by a callback function.
66
+ * An actor logic creator which returns callback logic as defined by a callback
67
+ * function.
38
68
  *
39
69
  * @remarks
40
- * Useful for subscription-based or other free-form logic that can send events back to the parent actor.
70
+ * Useful for subscription-based or other free-form logic that can send events
71
+ * back to the parent actor.
41
72
  *
42
73
  * Actors created from callback logic (“callback actors”) can:
74
+ *
43
75
  * - Receive events via the `receive` function
44
76
  * - Send events to the parent actor via the `sendBack` function
45
77
  *
46
78
  * Callback actors are a bit different from other actors in that they:
79
+ *
47
80
  * - Do not work with `onDone`
48
81
  * - Do not produce a snapshot using `.getSnapshot()`
49
82
  * - Do not emit values when used with `.subscribe()`
50
83
  * - Can not be stopped with `.stop()`
51
84
  *
52
- * @param invokeCallback - The callback function used to describe the callback logic
53
- * The callback function is passed an object with the following properties:
54
- * - `receive` - A function that can send events back to the parent actor; the listener is then called whenever events are received by the callback actor
55
- * - `sendBack` - A function that can send events back to the parent actor
56
- * - `input` - Data that was provided to the callback actor
57
- * - `self` - The parent actor of the callback actor
58
- * - `system` - The actor system to which the callback actor belongs
59
- * The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
60
- * @see {@link InvokeCallback} for more information about the callback function and its object argument
61
- * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
62
-
63
- * @returns Callback logic
64
- *
65
85
  * @example
86
+ *
66
87
  * ```typescript
67
88
  * const callbackLogic = fromCallback(({ sendBack, receive }) => {
68
89
  * let lockStatus = 'unlocked';
@@ -89,5 +110,23 @@ export type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEve
89
110
  * };
90
111
  * });
91
112
  * ```
113
+ *
114
+ * @param invokeCallback - The callback function used to describe the callback
115
+ * logic The callback function is passed an object with the following
116
+ * properties:
117
+ *
118
+ * - `receive` - A function that can send events back to the parent actor; the
119
+ * listener is then called whenever events are received by the callback
120
+ * actor
121
+ * - `sendBack` - A function that can send events back to the parent actor
122
+ * - `input` - Data that was provided to the callback actor
123
+ * - `self` - The parent actor of the callback actor
124
+ * - `system` - The actor system to which the callback actor belongs The callback
125
+ * function can (optionally) return a cleanup function, which is called
126
+ * when the actor is stopped.
127
+ *
128
+ * @returns Callback logic
129
+ * @see {@link InvokeCallback} for more information about the callback function and its object argument
130
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
92
131
  */
93
132
  export declare function fromCallback<TEvent extends EventObject, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(invokeCallback: InvokeCallback<TEvent, AnyEventObject, TInput, TEmitted>): CallbackActorLogic<TEvent, TInput, TEmitted>;
@@ -1,6 +1,6 @@
1
1
  import type { ActorRef, AnyEventObject, Snapshot } from "../types.js";
2
- export { fromCallback, type CallbackActorLogic, type CallbackSnapshot } from "./callback.js";
3
- export { fromEventObservable, fromObservable, type ObservableActorLogic, type ObservableSnapshot } from "./observable.js";
4
- export { fromPromise, type PromiseActorLogic, type PromiseSnapshot } from "./promise.js";
5
- export { fromTransition, type TransitionActorLogic, type TransitionSnapshot } from "./transition.js";
2
+ export { fromCallback, type CallbackActorLogic, type CallbackActorRef, type CallbackSnapshot } from "./callback.js";
3
+ export { fromEventObservable, fromObservable, type ObservableActorLogic, type ObservableActorRef, type ObservableSnapshot } from "./observable.js";
4
+ export { fromPromise, type PromiseActorLogic, type PromiseActorRef, type PromiseSnapshot } from "./promise.js";
5
+ export { fromTransition, type TransitionActorLogic, type TransitionActorRef, type TransitionSnapshot } from "./transition.js";
6
6
  export declare function createEmptyActor(): ActorRef<Snapshot<undefined>, AnyEventObject, AnyEventObject>;
@@ -9,9 +9,42 @@ export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown, T
9
9
  type: string;
10
10
  [k: string]: unknown;
11
11
  }, TInput, AnyActorSystem, TEmitted>;
12
+ /**
13
+ * Represents an actor created by `fromObservable` or `fromEventObservable`.
14
+ *
15
+ * The type of `self` within the actor's logic.
16
+ *
17
+ * @example
18
+ *
19
+ * ```ts
20
+ * import { fromObservable, createActor } from 'xstate';
21
+ * import { interval } from 'rxjs';
22
+ *
23
+ * // The type of the value observed by the actor's logic.
24
+ * type Context = number;
25
+ * // The actor's input.
26
+ * type Input = { period?: number };
27
+ *
28
+ * // Actor logic that observes a number incremented every `input.period`
29
+ * // milliseconds (default: 1_000).
30
+ * const logic = fromObservable<Context, Input>(({ input, self }) => {
31
+ * self;
32
+ * // ^? ObservableActorRef<Event, Input>
33
+ *
34
+ * return interval(input.period ?? 1_000);
35
+ * });
36
+ *
37
+ * const actor = createActor(logic, { input: { period: 2_000 } });
38
+ * // ^? ObservableActorRef<Event, Input>
39
+ * ```
40
+ *
41
+ * @see {@link fromObservable}
42
+ * @see {@link fromEventObservable}
43
+ */
12
44
  export type ObservableActorRef<TContext> = ActorRefFrom<ObservableActorLogic<TContext, any>>;
13
45
  /**
14
- * Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
46
+ * Observable actor logic is described by an observable stream of values. Actors
47
+ * created from observable logic (“observable actors”) can:
15
48
  *
16
49
  * - Emit snapshots of the observable’s emitted value
17
50
  *
@@ -19,16 +52,10 @@ export type ObservableActorRef<TContext> = ActorRefFrom<ObservableActorLogic<TCo
19
52
  *
20
53
  * Sending events to observable actors will have no effect.
21
54
  *
22
- * @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
23
- * - `input` - Data that was provided to the observable actor
24
- * - `self` - The parent actor
25
- * - `system` - The actor system to which the observable actor belongs
26
- *
27
- * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
28
- *
29
55
  * @example
56
+ *
30
57
  * ```ts
31
- * import { fromObservable, createActor } from 'xstate'
58
+ * import { fromObservable, createActor } from 'xstate';
32
59
  * import { interval } from 'rxjs';
33
60
  *
34
61
  * const logic = fromObservable((obj) => interval(1000));
@@ -47,6 +74,15 @@ export type ObservableActorRef<TContext> = ActorRefFrom<ObservableActorLogic<TCo
47
74
  * // ...
48
75
  * ```
49
76
  *
77
+ * @param observableCreator A function that creates an observable. It receives
78
+ * one argument, an object with the following properties:
79
+ *
80
+ * - `input` - Data that was provided to the observable actor
81
+ * - `self` - The parent actor
82
+ * - `system` - The actor system to which the observable actor belongs
83
+ *
84
+ * It should return a {@link Subscribable}, which is compatible with an RxJS
85
+ * Observable, although RxJS is not required to create them.
50
86
  * @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
51
87
  * @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
52
88
  */
@@ -57,24 +93,20 @@ export declare function fromObservable<TContext, TInput extends NonReducibleUnkn
57
93
  emit: (emitted: TEmitted) => void;
58
94
  }) => Subscribable<TContext>): ObservableActorLogic<TContext, TInput, TEmitted>;
59
95
  /**
60
- * Creates event observable logic that listens to an observable that delivers event objects.
96
+ * Creates event observable logic that listens to an observable that delivers
97
+ * event objects.
61
98
  *
62
- * 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:
99
+ * Event observable actor logic is described by an observable stream of
100
+ * {@link https://stately.ai/docs/transitions#event-objects | event objects}.
101
+ * Actors created from event observable logic (“event observable actors”) can:
63
102
  *
64
103
  * - Implicitly send events to its parent actor
65
104
  * - Emit snapshots of its emitted event objects
66
105
  *
67
106
  * Sending events to event observable actors will have no effect.
68
107
  *
69
- * @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
70
- *
71
- * - `input` - Data that was provided to the event observable actor
72
- * - `self` - The parent actor
73
- * - `system` - The actor system to which the event observable actor belongs.
74
- *
75
- * It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
76
- *
77
108
  * @example
109
+ *
78
110
  * ```ts
79
111
  * import {
80
112
  * fromEventObservable,
@@ -85,20 +117,31 @@ export declare function fromObservable<TContext, TInput extends NonReducibleUnkn
85
117
  * } from 'xstate';
86
118
  * import { fromEvent } from 'rxjs';
87
119
  *
88
- * const mouseClickLogic = fromEventObservable(() =>
89
- * fromEvent(document.body, 'click') as Subscribable<EventObject>
120
+ * const mouseClickLogic = fromEventObservable(
121
+ * () => fromEvent(document.body, 'click') as Subscribable<EventObject>
90
122
  * );
91
123
  *
92
124
  * const canvasMachine = createMachine({
93
125
  * invoke: {
94
126
  * // Will send mouse `click` events to the canvas actor
95
- * src: mouseClickLogic,
127
+ * src: mouseClickLogic
96
128
  * }
97
129
  * });
98
130
  *
99
131
  * const canvasActor = createActor(canvasMachine);
100
132
  * canvasActor.start();
101
133
  * ```
134
+ *
135
+ * @param lazyObservable A function that creates an observable that delivers
136
+ * event objects. It receives one argument, an object with the following
137
+ * properties:
138
+ *
139
+ * - `input` - Data that was provided to the event observable actor
140
+ * - `self` - The parent actor
141
+ * - `system` - The actor system to which the event observable actor belongs.
142
+ *
143
+ * It should return a {@link Subscribable}, which is compatible with an RxJS
144
+ * Observable, although RxJS is not required to create them.
102
145
  */
103
146
  export declare function fromEventObservable<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(lazyObservable: ({ input, system, self, emit }: {
104
147
  input: TInput;
@@ -8,19 +8,96 @@ export type PromiseActorLogic<TOutput, TInput = unknown, TEmitted extends EventO
8
8
  [k: string]: unknown;
9
9
  }, TInput, // input
10
10
  AnyActorSystem, TEmitted>;
11
+ /**
12
+ * Represents an actor created by `fromPromise`.
13
+ *
14
+ * The type of `self` within the actor's logic.
15
+ *
16
+ * @example
17
+ *
18
+ * ```ts
19
+ * import { fromPromise, createActor } from 'xstate';
20
+ *
21
+ * // The actor's resolved output
22
+ * type Output = string;
23
+ * // The actor's input.
24
+ * type Input = { message: string };
25
+ *
26
+ * // Actor logic that fetches the url of an image of a cat saying `input.message`.
27
+ * const logic = fromPromise<Output, Input>(async ({ input, self }) => {
28
+ * self;
29
+ * // ^? PromiseActorRef<Output, Input>
30
+ *
31
+ * const data = await fetch(
32
+ * `https://cataas.com/cat/says/${input.message}`
33
+ * );
34
+ * const url = await data.json();
35
+ * return url;
36
+ * });
37
+ *
38
+ * const actor = createActor(logic, { input: { message: 'hello world' } });
39
+ * // ^? PromiseActorRef<Output, Input>
40
+ * ```
41
+ *
42
+ * @see {@link fromPromise}
43
+ */
11
44
  export type PromiseActorRef<TOutput> = ActorRefFrom<PromiseActorLogic<TOutput, unknown>>;
45
+ /**
46
+ * An actor logic creator which returns promise logic as defined by an async
47
+ * process that resolves or rejects after some time.
48
+ *
49
+ * Actors created from promise actor logic (“promise actors”) can:
50
+ *
51
+ * - Emit the resolved value of the promise
52
+ * - Output the resolved value of the promise
53
+ *
54
+ * Sending events to promise actors will have no effect.
55
+ *
56
+ * @example
57
+ *
58
+ * ```ts
59
+ * const promiseLogic = fromPromise(async () => {
60
+ * const result = await fetch('https://example.com/...').then((data) =>
61
+ * data.json()
62
+ * );
63
+ *
64
+ * return result;
65
+ * });
66
+ *
67
+ * const promiseActor = createActor(promiseLogic);
68
+ * promiseActor.subscribe((snapshot) => {
69
+ * console.log(snapshot);
70
+ * });
71
+ * promiseActor.start();
72
+ * // => {
73
+ * // output: undefined,
74
+ * // status: 'active'
75
+ * // ...
76
+ * // }
77
+ *
78
+ * // After promise resolves
79
+ * // => {
80
+ * // output: { ... },
81
+ * // status: 'done',
82
+ * // ...
83
+ * // }
84
+ * ```
85
+ *
86
+ * @param promiseCreator A function which returns a Promise, and accepts an
87
+ * object with the following properties:
88
+ *
89
+ * - `input` - Data that was provided to the promise actor
90
+ * - `self` - The parent actor of the promise actor
91
+ * - `system` - The actor system to which the promise actor belongs
92
+ *
93
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
94
+ */
12
95
  export declare function fromPromise<TOutput, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(promiseCreator: ({ input, system, self, signal, emit }: {
13
- /**
14
- * Data that was provided to the promise actor
15
- */
96
+ /** Data that was provided to the promise actor */
16
97
  input: TInput;
17
- /**
18
- * The actor system to which the promise actor belongs
19
- */
98
+ /** The actor system to which the promise actor belongs */
20
99
  system: AnyActorSystem;
21
- /**
22
- * The parent actor of the promise actor
23
- */
100
+ /** The parent actor of the promise actor */
24
101
  self: PromiseActorRef<TOutput>;
25
102
  signal: AbortSignal;
26
103
  emit: (emitted: TEmitted) => void;
@@ -4,44 +4,98 @@ export type TransitionSnapshot<TContext> = Snapshot<undefined> & {
4
4
  context: TContext;
5
5
  };
6
6
  export type TransitionActorLogic<TContext, TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<TransitionSnapshot<TContext>, TEvent, TInput, AnyActorSystem, TEmitted>;
7
+ /**
8
+ * Represents an actor created by `fromTransition`.
9
+ *
10
+ * The type of `self` within the actor's logic.
11
+ *
12
+ * @example
13
+ *
14
+ * ```ts
15
+ * import {
16
+ * fromTransition,
17
+ * createActor,
18
+ * type AnyActorSystem
19
+ * } from 'xstate';
20
+ *
21
+ * //* The actor's stored context.
22
+ * type Context = {
23
+ * // The current count.
24
+ * count: number;
25
+ * // The amount to increase `count` by.
26
+ * step: number;
27
+ * };
28
+ * // The events the actor receives.
29
+ * type Event = { type: 'increment' };
30
+ * // The actor's input.
31
+ * type Input = { step?: number };
32
+ *
33
+ * // Actor logic that increments `count` by `step` when it receives an event of
34
+ * // type `increment`.
35
+ * const logic = fromTransition<Context, Event, AnyActorSystem, Input>(
36
+ * (state, event, actorScope) => {
37
+ * actorScope.self;
38
+ * // ^? TransitionActorRef<Context, Event>
39
+ *
40
+ * if (event.type === 'increment') {
41
+ * return {
42
+ * ...state,
43
+ * count: state.count + state.step
44
+ * };
45
+ * }
46
+ * return state;
47
+ * },
48
+ * ({ input, self }) => {
49
+ * self;
50
+ * // ^? TransitionActorRef<Context, Event>
51
+ *
52
+ * return {
53
+ * count: 0,
54
+ * step: input.step ?? 1
55
+ * };
56
+ * }
57
+ * );
58
+ *
59
+ * const actor = createActor(logic, { input: { step: 10 } });
60
+ * // ^? TransitionActorRef<Context, Event>
61
+ * ```
62
+ *
63
+ * @see {@link fromTransition}
64
+ */
7
65
  export type TransitionActorRef<TContext, TEvent extends EventObject> = ActorRefFrom<TransitionActorLogic<TransitionSnapshot<TContext>, TEvent, unknown>>;
8
66
  /**
9
67
  * Returns actor logic given a transition function and its initial state.
10
68
  *
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.
69
+ * A “transition function” is a function that takes the current `state` and
70
+ * received `event` object as arguments, and returns the next state, similar to
71
+ * a reducer.
12
72
  *
13
73
  * Actors created from transition logic (“transition actors”) can:
14
74
  *
15
75
  * - Receive events
16
76
  * - Emit snapshots of its state
17
77
  *
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.
78
+ * The transition function’s `state` is used as its transition actor’s
79
+ * `context`.
21
80
  *
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
30
- * @returns Actor logic
81
+ * Note that the "state" for a transition function is provided by the initial
82
+ * state argument, and is not the same as the State object of an actor or a
83
+ * state within a machine configuration.
31
84
  *
32
85
  * @example
86
+ *
33
87
  * ```ts
34
88
  * const transitionLogic = fromTransition(
35
89
  * (state, event) => {
36
90
  * if (event.type === 'increment') {
37
91
  * return {
38
92
  * ...state,
39
- * count: state.count + 1,
93
+ * count: state.count + 1
40
94
  * };
41
95
  * }
42
96
  * return state;
43
97
  * },
44
- * { count: 0 },
98
+ * { count: 0 }
45
99
  * );
46
100
  *
47
101
  * const transitionActor = createActor(transitionLogic);
@@ -62,6 +116,26 @@ export type TransitionActorRef<TContext, TEvent extends EventObject> = ActorRefF
62
116
  * // ...
63
117
  * // }
64
118
  * ```
119
+ *
120
+ * @param transition The transition function used to describe the transition
121
+ * logic. It should return the next state given the current state and event.
122
+ * It receives the following arguments:
123
+ *
124
+ * - `state` - the current state.
125
+ * - `event` - the received event.
126
+ * - `actorScope` - the actor scope object, with properties like `self` and
127
+ * `system`.
128
+ *
129
+ * @param initialContext The initial state of the transition function, either an
130
+ * object representing the state, or a function which returns a state object.
131
+ * If a function, it will receive as its only argument an object with the
132
+ * following properties:
133
+ *
134
+ * - `input` - the `input` provided to its parent transition actor.
135
+ * - `self` - a reference to its parent transition actor.
136
+ *
137
+ * @returns Actor logic
138
+ * @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
65
139
  */
66
140
  export declare function fromTransition<TContext, TEvent extends EventObject, TSystem extends AnyActorSystem, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(transition: (snapshot: TContext, event: TEvent, actorScope: ActorScope<TransitionSnapshot<TContext>, TEvent, TSystem, TEmitted>) => TContext, initialContext: TContext | (({ input, self }: {
67
141
  input: TInput;
@@ -1,25 +1,26 @@
1
1
  import { EventObject } from "./types.js";
2
2
  /**
3
- * Asserts that the given event object is of the specified type or types.
4
- * Throws an error if the event object is not of the specified types.
5
- @example
6
-
7
- ```ts
8
- // ...
9
- entry: ({ event }) => {
10
- assertEvent(event, 'doNothing');
11
- // event is { type: 'doNothing' }
12
- },
13
- // ...
14
- exit: ({ event }) => {
15
- assertEvent(event, 'greet');
16
- // event is { type: 'greet'; message: string }
17
-
18
- assertEvent(event, ['greet', 'notify']);
19
- // event is { type: 'greet'; message: string }
20
- // or { type: 'notify'; message: string; level: 'info' | 'error' }
21
- },
22
- ```
3
+ * Asserts that the given event object is of the specified type or types. Throws
4
+ * an error if the event object is not of the specified types.
5
+ *
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * // ...
10
+ * entry: ({ event }) => {
11
+ * assertEvent(event, 'doNothing');
12
+ * // event is { type: 'doNothing' }
13
+ * },
14
+ * // ...
15
+ * exit: ({ event }) => {
16
+ * assertEvent(event, 'greet');
17
+ * // event is { type: 'greet'; message: string }
18
+ *
19
+ * assertEvent(event, ['greet', 'notify']);
20
+ * // event is { type: 'greet'; message: string }
21
+ * // or { type: 'notify'; message: string; level: 'info' | 'error' }
22
+ * },
23
+ * ```
23
24
  */
24
25
  export declare function assertEvent<TEvent extends EventObject, TAssertedType extends TEvent['type']>(event: TEvent, type: TAssertedType | TAssertedType[]): asserts event is TEvent & {
25
26
  type: TAssertedType;