xstate 5.13.2 → 5.15.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.
- package/actions/dist/xstate-actions.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.cjs.js +2 -2
- package/actions/dist/xstate-actions.development.esm.js +2 -2
- package/actions/dist/xstate-actions.esm.js +2 -2
- package/actions/dist/xstate-actions.umd.min.js +1 -1
- package/actions/dist/xstate-actions.umd.min.js.map +1 -1
- package/actors/dist/xstate-actors.cjs.js +289 -79
- package/actors/dist/xstate-actors.development.cjs.js +289 -79
- package/actors/dist/xstate-actors.development.esm.js +289 -79
- package/actors/dist/xstate-actors.esm.js +289 -79
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/declarations/src/State.d.ts +21 -25
- package/dist/declarations/src/StateMachine.d.ts +18 -30
- package/dist/declarations/src/StateNode.d.ts +29 -51
- package/dist/declarations/src/actions/assign.d.ts +29 -27
- package/dist/declarations/src/actions/cancel.d.ts +28 -22
- package/dist/declarations/src/actions/emit.d.ts +33 -33
- package/dist/declarations/src/actions/enqueueActions.d.ts +18 -16
- package/dist/declarations/src/actions/log.d.ts +5 -4
- package/dist/declarations/src/actions/send.d.ts +5 -3
- package/dist/declarations/src/actors/callback.d.ts +69 -29
- package/dist/declarations/src/actors/index.d.ts +4 -4
- package/dist/declarations/src/actors/observable.d.ts +74 -29
- package/dist/declarations/src/actors/promise.d.ts +91 -13
- package/dist/declarations/src/actors/transition.d.ts +90 -16
- package/dist/declarations/src/assert.d.ts +21 -20
- package/dist/declarations/src/createActor.d.ts +51 -42
- package/dist/declarations/src/createMachine.d.ts +40 -54
- package/dist/declarations/src/getNextSnapshot.d.ts +27 -24
- package/dist/declarations/src/guards.d.ts +67 -66
- package/dist/declarations/src/index.d.ts +0 -1
- package/dist/declarations/src/inspection.d.ts +1 -0
- package/dist/declarations/src/setup.d.ts +1 -2
- package/dist/declarations/src/spawn.d.ts +2 -5
- package/dist/declarations/src/stateUtils.d.ts +5 -10
- package/dist/declarations/src/system.d.ts +2 -2
- package/dist/declarations/src/toPromise.d.ts +1 -0
- package/dist/declarations/src/types.d.ts +169 -192
- package/dist/declarations/src/waitFor.d.ts +9 -9
- package/dist/{log-c447a931.esm.js → log-63de2429.esm.js} +112 -117
- package/dist/{log-5b14d850.cjs.js → log-b8c93ee3.cjs.js} +112 -117
- package/dist/{log-d06dd00b.development.cjs.js → log-d2c282d6.development.cjs.js} +112 -117
- package/dist/{log-b3b03961.development.esm.js → log-e9953143.development.esm.js} +112 -117
- package/dist/{raise-f406edbd.esm.js → raise-2cfe6b8f.esm.js} +302 -284
- package/dist/{raise-150d5679.development.esm.js → raise-7d030497.development.esm.js} +302 -284
- package/dist/{raise-ff8990f7.cjs.js → raise-a6298350.cjs.js} +302 -284
- package/dist/{raise-bf7a8462.development.cjs.js → raise-bad6a97b.development.cjs.js} +302 -284
- package/dist/xstate.cjs.js +134 -157
- package/dist/xstate.development.cjs.js +134 -157
- package/dist/xstate.development.esm.js +136 -159
- package/dist/xstate.esm.js +136 -159
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.cjs.js +1 -1
- package/guards/dist/xstate-guards.development.esm.js +1 -1
- package/guards/dist/xstate-guards.esm.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +1 -1
- package/dist/declarations/src/typegenTypes.d.ts +0 -168
|
@@ -3,65 +3,87 @@ import { ActorLogic, ActorRefFrom, AnyEventObject, EventObject, NonReducibleUnkn
|
|
|
3
3
|
export type CallbackSnapshot<TInput> = Snapshot<undefined> & {
|
|
4
4
|
input: TInput;
|
|
5
5
|
};
|
|
6
|
-
export type CallbackActorLogic<TEvent extends EventObject, TInput = NonReducibleUnknown> = ActorLogic<CallbackSnapshot<TInput>, TEvent, TInput, AnyActorSystem,
|
|
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;
|
|
10
44
|
}['bivarianceHack']) => void;
|
|
11
|
-
export type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEvent extends EventObject = AnyEventObject, TInput = NonReducibleUnknown> = ({ input, system, self, sendBack, receive }: {
|
|
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
|
-
*
|
|
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>;
|
|
63
|
+
emit: (emitted: TEmitted) => void;
|
|
34
64
|
}) => (() => void) | void;
|
|
35
65
|
/**
|
|
36
|
-
* An actor logic creator which returns callback logic as defined by a callback
|
|
66
|
+
* An actor logic creator which returns callback logic as defined by a callback
|
|
67
|
+
* function.
|
|
37
68
|
*
|
|
38
69
|
* @remarks
|
|
39
|
-
* Useful for subscription-based or other free-form logic that can send events
|
|
70
|
+
* Useful for subscription-based or other free-form logic that can send events
|
|
71
|
+
* back to the parent actor.
|
|
40
72
|
*
|
|
41
73
|
* Actors created from callback logic (“callback actors”) can:
|
|
74
|
+
*
|
|
42
75
|
* - Receive events via the `receive` function
|
|
43
76
|
* - Send events to the parent actor via the `sendBack` function
|
|
44
77
|
*
|
|
45
78
|
* Callback actors are a bit different from other actors in that they:
|
|
79
|
+
*
|
|
46
80
|
* - Do not work with `onDone`
|
|
47
81
|
* - Do not produce a snapshot using `.getSnapshot()`
|
|
48
82
|
* - Do not emit values when used with `.subscribe()`
|
|
49
83
|
* - Can not be stopped with `.stop()`
|
|
50
84
|
*
|
|
51
|
-
* @param invokeCallback - The callback function used to describe the callback logic
|
|
52
|
-
* The callback function is passed an object with the following properties:
|
|
53
|
-
* - `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
|
|
54
|
-
* - `sendBack` - A function that can send events back to the parent actor
|
|
55
|
-
* - `input` - Data that was provided to the callback actor
|
|
56
|
-
* - `self` - The parent actor of the callback actor
|
|
57
|
-
* - `system` - The actor system to which the callback actor belongs
|
|
58
|
-
* The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
|
|
59
|
-
* @see {@link InvokeCallback} for more information about the callback function and its object argument
|
|
60
|
-
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
|
61
|
-
|
|
62
|
-
* @returns Callback logic
|
|
63
|
-
*
|
|
64
85
|
* @example
|
|
86
|
+
*
|
|
65
87
|
* ```typescript
|
|
66
88
|
* const callbackLogic = fromCallback(({ sendBack, receive }) => {
|
|
67
89
|
* let lockStatus = 'unlocked';
|
|
@@ -88,5 +110,23 @@ export type InvokeCallback<TEvent extends EventObject = AnyEventObject, TSentEve
|
|
|
88
110
|
* };
|
|
89
111
|
* });
|
|
90
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
|
|
91
131
|
*/
|
|
92
|
-
export declare function fromCallback<TEvent extends EventObject, TInput = NonReducibleUnknown>(invokeCallback: InvokeCallback<TEvent, AnyEventObject, TInput>): CallbackActorLogic<TEvent, TInput>;
|
|
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>;
|
|
@@ -5,13 +5,46 @@ export type ObservableSnapshot<TContext, TInput extends NonReducibleUnknown> = S
|
|
|
5
5
|
input: TInput | undefined;
|
|
6
6
|
_subscription: Subscription | undefined;
|
|
7
7
|
};
|
|
8
|
-
export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown> = ActorLogic<ObservableSnapshot<TContext, TInput>, {
|
|
8
|
+
export type ObservableActorLogic<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject> = ActorLogic<ObservableSnapshot<TContext, TInput>, {
|
|
9
9
|
type: string;
|
|
10
10
|
[k: string]: unknown;
|
|
11
|
-
}, TInput, AnyActorSystem,
|
|
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
|
|
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,33 +74,39 @@ 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
|
*/
|
|
53
|
-
export declare function fromObservable<TContext, TInput extends NonReducibleUnknown>(observableCreator: ({ input, system }: {
|
|
89
|
+
export declare function fromObservable<TContext, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(observableCreator: ({ input, system, self }: {
|
|
54
90
|
input: TInput;
|
|
55
91
|
system: AnyActorSystem;
|
|
56
92
|
self: ObservableActorRef<TContext>;
|
|
57
|
-
|
|
93
|
+
emit: (emitted: TEmitted) => void;
|
|
94
|
+
}) => Subscribable<TContext>): ObservableActorLogic<TContext, TInput, TEmitted>;
|
|
58
95
|
/**
|
|
59
|
-
* Creates event observable logic that listens to an observable that delivers
|
|
96
|
+
* Creates event observable logic that listens to an observable that delivers
|
|
97
|
+
* event objects.
|
|
60
98
|
*
|
|
61
|
-
* Event observable actor logic is described by an observable stream of
|
|
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:
|
|
62
102
|
*
|
|
63
103
|
* - Implicitly send events to its parent actor
|
|
64
104
|
* - Emit snapshots of its emitted event objects
|
|
65
105
|
*
|
|
66
106
|
* Sending events to event observable actors will have no effect.
|
|
67
107
|
*
|
|
68
|
-
* @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
|
|
69
|
-
*
|
|
70
|
-
* - `input` - Data that was provided to the event observable actor
|
|
71
|
-
* - `self` - The parent actor
|
|
72
|
-
* - `system` - The actor system to which the event observable actor belongs.
|
|
73
|
-
*
|
|
74
|
-
* It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
|
|
75
|
-
*
|
|
76
108
|
* @example
|
|
109
|
+
*
|
|
77
110
|
* ```ts
|
|
78
111
|
* import {
|
|
79
112
|
* fromEventObservable,
|
|
@@ -84,23 +117,35 @@ export declare function fromObservable<TContext, TInput extends NonReducibleUnkn
|
|
|
84
117
|
* } from 'xstate';
|
|
85
118
|
* import { fromEvent } from 'rxjs';
|
|
86
119
|
*
|
|
87
|
-
* const mouseClickLogic = fromEventObservable(
|
|
88
|
-
* fromEvent(document.body, 'click') as Subscribable<EventObject>
|
|
120
|
+
* const mouseClickLogic = fromEventObservable(
|
|
121
|
+
* () => fromEvent(document.body, 'click') as Subscribable<EventObject>
|
|
89
122
|
* );
|
|
90
123
|
*
|
|
91
124
|
* const canvasMachine = createMachine({
|
|
92
125
|
* invoke: {
|
|
93
126
|
* // Will send mouse `click` events to the canvas actor
|
|
94
|
-
* src: mouseClickLogic
|
|
127
|
+
* src: mouseClickLogic
|
|
95
128
|
* }
|
|
96
129
|
* });
|
|
97
130
|
*
|
|
98
131
|
* const canvasActor = createActor(canvasMachine);
|
|
99
132
|
* canvasActor.start();
|
|
100
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.
|
|
101
145
|
*/
|
|
102
|
-
export declare function fromEventObservable<
|
|
146
|
+
export declare function fromEventObservable<TEvent extends EventObject, TInput extends NonReducibleUnknown, TEmitted extends EventObject = EventObject>(lazyObservable: ({ input, system, self, emit }: {
|
|
103
147
|
input: TInput;
|
|
104
148
|
system: AnyActorSystem;
|
|
105
|
-
self: ObservableActorRef<
|
|
106
|
-
|
|
149
|
+
self: ObservableActorRef<TEvent>;
|
|
150
|
+
emit: (emitted: TEmitted) => void;
|
|
151
|
+
}) => Subscribable<TEvent>): ObservableActorLogic<TEvent, TInput, TEmitted>;
|
|
@@ -3,24 +3,102 @@ import { ActorLogic, ActorRefFrom, EventObject, NonReducibleUnknown, Snapshot }
|
|
|
3
3
|
export type PromiseSnapshot<TOutput, TInput> = Snapshot<TOutput> & {
|
|
4
4
|
input: TInput | undefined;
|
|
5
5
|
};
|
|
6
|
-
export type PromiseActorLogic<TOutput, TInput = unknown> = ActorLogic<PromiseSnapshot<TOutput, TInput>, {
|
|
6
|
+
export type PromiseActorLogic<TOutput, TInput = unknown, TEmitted extends EventObject = EventObject> = ActorLogic<PromiseSnapshot<TOutput, TInput>, {
|
|
7
7
|
type: string;
|
|
8
8
|
[k: string]: unknown;
|
|
9
9
|
}, TInput, // input
|
|
10
|
-
AnyActorSystem,
|
|
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>>;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
+
*/
|
|
95
|
+
export declare function fromPromise<TOutput, TInput = NonReducibleUnknown, TEmitted extends EventObject = EventObject>(promiseCreator: ({ input, system, self, signal, emit }: {
|
|
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;
|
|
104
|
+
}) => PromiseLike<TOutput>): PromiseActorLogic<TOutput, TInput, TEmitted>;
|
|
@@ -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
|
|
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
|
|
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
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
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,8 +116,28 @@ 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
|
-
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>) => TContext, initialContext: TContext | (({ input, self }: {
|
|
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;
|
|
68
142
|
self: TransitionActorRef<TContext, TEvent>;
|
|
69
143
|
}) => TContext)): TransitionActorLogic<TContext, TEvent, TInput, TEmitted>;
|
|
@@ -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
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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;
|