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
|
@@ -2,46 +2,101 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var guards_dist_xstateGuards = require('../../dist/raise-
|
|
5
|
+
var guards_dist_xstateGuards = require('../../dist/raise-bad6a97b.development.cjs.js');
|
|
6
6
|
require('../../dev/dist/xstate-dev.development.cjs.js');
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Represents an actor created by `fromTransition`.
|
|
10
|
+
*
|
|
11
|
+
* The type of `self` within the actor's logic.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import {
|
|
17
|
+
* fromTransition,
|
|
18
|
+
* createActor,
|
|
19
|
+
* type AnyActorSystem
|
|
20
|
+
* } from 'xstate';
|
|
21
|
+
*
|
|
22
|
+
* //* The actor's stored context.
|
|
23
|
+
* type Context = {
|
|
24
|
+
* // The current count.
|
|
25
|
+
* count: number;
|
|
26
|
+
* // The amount to increase `count` by.
|
|
27
|
+
* step: number;
|
|
28
|
+
* };
|
|
29
|
+
* // The events the actor receives.
|
|
30
|
+
* type Event = { type: 'increment' };
|
|
31
|
+
* // The actor's input.
|
|
32
|
+
* type Input = { step?: number };
|
|
33
|
+
*
|
|
34
|
+
* // Actor logic that increments `count` by `step` when it receives an event of
|
|
35
|
+
* // type `increment`.
|
|
36
|
+
* const logic = fromTransition<Context, Event, AnyActorSystem, Input>(
|
|
37
|
+
* (state, event, actorScope) => {
|
|
38
|
+
* actorScope.self;
|
|
39
|
+
* // ^? TransitionActorRef<Context, Event>
|
|
40
|
+
*
|
|
41
|
+
* if (event.type === 'increment') {
|
|
42
|
+
* return {
|
|
43
|
+
* ...state,
|
|
44
|
+
* count: state.count + state.step
|
|
45
|
+
* };
|
|
46
|
+
* }
|
|
47
|
+
* return state;
|
|
48
|
+
* },
|
|
49
|
+
* ({ input, self }) => {
|
|
50
|
+
* self;
|
|
51
|
+
* // ^? TransitionActorRef<Context, Event>
|
|
52
|
+
*
|
|
53
|
+
* return {
|
|
54
|
+
* count: 0,
|
|
55
|
+
* step: input.step ?? 1
|
|
56
|
+
* };
|
|
57
|
+
* }
|
|
58
|
+
* );
|
|
59
|
+
*
|
|
60
|
+
* const actor = createActor(logic, { input: { step: 10 } });
|
|
61
|
+
* // ^? TransitionActorRef<Context, Event>
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* @see {@link fromTransition}
|
|
65
|
+
*/
|
|
66
|
+
|
|
8
67
|
/**
|
|
9
68
|
* Returns actor logic given a transition function and its initial state.
|
|
10
69
|
*
|
|
11
|
-
* A “transition function” is a function that takes the current `state` and
|
|
70
|
+
* A “transition function” is a function that takes the current `state` and
|
|
71
|
+
* received `event` object as arguments, and returns the next state, similar to
|
|
72
|
+
* a reducer.
|
|
12
73
|
*
|
|
13
74
|
* Actors created from transition logic (“transition actors”) can:
|
|
14
75
|
*
|
|
15
76
|
* - Receive events
|
|
16
77
|
* - Emit snapshots of its state
|
|
17
78
|
*
|
|
18
|
-
* The transition function’s `state` is used as its transition actor’s
|
|
79
|
+
* The transition function’s `state` is used as its transition actor’s
|
|
80
|
+
* `context`.
|
|
19
81
|
*
|
|
20
|
-
* Note that the "state" for a transition function is provided by the initial
|
|
21
|
-
*
|
|
22
|
-
*
|
|
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
|
|
82
|
+
* Note that the "state" for a transition function is provided by the initial
|
|
83
|
+
* state argument, and is not the same as the State object of an actor or a
|
|
84
|
+
* state within a machine configuration.
|
|
31
85
|
*
|
|
32
86
|
* @example
|
|
87
|
+
*
|
|
33
88
|
* ```ts
|
|
34
89
|
* const transitionLogic = fromTransition(
|
|
35
90
|
* (state, event) => {
|
|
36
91
|
* if (event.type === 'increment') {
|
|
37
92
|
* return {
|
|
38
93
|
* ...state,
|
|
39
|
-
* count: state.count + 1
|
|
94
|
+
* count: state.count + 1
|
|
40
95
|
* };
|
|
41
96
|
* }
|
|
42
97
|
* return state;
|
|
43
98
|
* },
|
|
44
|
-
* { count: 0 }
|
|
99
|
+
* { count: 0 }
|
|
45
100
|
* );
|
|
46
101
|
*
|
|
47
102
|
* const transitionActor = createActor(transitionLogic);
|
|
@@ -62,6 +117,26 @@ require('../../dev/dist/xstate-dev.development.cjs.js');
|
|
|
62
117
|
* // ...
|
|
63
118
|
* // }
|
|
64
119
|
* ```
|
|
120
|
+
*
|
|
121
|
+
* @param transition The transition function used to describe the transition
|
|
122
|
+
* logic. It should return the next state given the current state and event.
|
|
123
|
+
* It receives the following arguments:
|
|
124
|
+
*
|
|
125
|
+
* - `state` - the current state.
|
|
126
|
+
* - `event` - the received event.
|
|
127
|
+
* - `actorScope` - the actor scope object, with properties like `self` and
|
|
128
|
+
* `system`.
|
|
129
|
+
*
|
|
130
|
+
* @param initialContext The initial state of the transition function, either an
|
|
131
|
+
* object representing the state, or a function which returns a state object.
|
|
132
|
+
* If a function, it will receive as its only argument an object with the
|
|
133
|
+
* following properties:
|
|
134
|
+
*
|
|
135
|
+
* - `input` - the `input` provided to its parent transition actor.
|
|
136
|
+
* - `self` - a reference to its parent transition actor.
|
|
137
|
+
*
|
|
138
|
+
* @returns Actor logic
|
|
139
|
+
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
|
65
140
|
*/
|
|
66
141
|
function fromTransition(transition, initialContext) {
|
|
67
142
|
return {
|
|
@@ -88,36 +163,64 @@ function fromTransition(transition, initialContext) {
|
|
|
88
163
|
}
|
|
89
164
|
|
|
90
165
|
const instanceStates = /* #__PURE__ */new WeakMap();
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Represents an actor created by `fromCallback`.
|
|
169
|
+
*
|
|
170
|
+
* The type of `self` within the actor's logic.
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
*
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { fromCallback, createActor } from 'xstate';
|
|
176
|
+
*
|
|
177
|
+
* // The events the actor receives.
|
|
178
|
+
* type Event = { type: 'someEvent' };
|
|
179
|
+
* // The actor's input.
|
|
180
|
+
* type Input = { name: string };
|
|
181
|
+
*
|
|
182
|
+
* // Actor logic that logs whenever it receives an event of type `someEvent`.
|
|
183
|
+
* const logic = fromCallback<Event, Input>(({ self, input, receive }) => {
|
|
184
|
+
* self;
|
|
185
|
+
* // ^? CallbackActorRef<Event, Input>
|
|
186
|
+
*
|
|
187
|
+
* receive((event) => {
|
|
188
|
+
* if (event.type === 'someEvent') {
|
|
189
|
+
* console.log(`${input.name}: received "someEvent" event`);
|
|
190
|
+
* // logs 'myActor: received "someEvent" event'
|
|
191
|
+
* }
|
|
192
|
+
* });
|
|
193
|
+
* });
|
|
194
|
+
*
|
|
195
|
+
* const actor = createActor(logic, { input: { name: 'myActor' } });
|
|
196
|
+
* // ^? CallbackActorRef<Event, Input>
|
|
197
|
+
* ```
|
|
198
|
+
*
|
|
199
|
+
* @see {@link fromCallback}
|
|
200
|
+
*/
|
|
201
|
+
|
|
91
202
|
/**
|
|
92
|
-
* An actor logic creator which returns callback logic as defined by a callback
|
|
203
|
+
* An actor logic creator which returns callback logic as defined by a callback
|
|
204
|
+
* function.
|
|
93
205
|
*
|
|
94
206
|
* @remarks
|
|
95
|
-
* Useful for subscription-based or other free-form logic that can send events
|
|
207
|
+
* Useful for subscription-based or other free-form logic that can send events
|
|
208
|
+
* back to the parent actor.
|
|
96
209
|
*
|
|
97
210
|
* Actors created from callback logic (“callback actors”) can:
|
|
211
|
+
*
|
|
98
212
|
* - Receive events via the `receive` function
|
|
99
213
|
* - Send events to the parent actor via the `sendBack` function
|
|
100
214
|
*
|
|
101
215
|
* Callback actors are a bit different from other actors in that they:
|
|
216
|
+
*
|
|
102
217
|
* - Do not work with `onDone`
|
|
103
218
|
* - Do not produce a snapshot using `.getSnapshot()`
|
|
104
219
|
* - Do not emit values when used with `.subscribe()`
|
|
105
220
|
* - Can not be stopped with `.stop()`
|
|
106
221
|
*
|
|
107
|
-
* @param invokeCallback - The callback function used to describe the callback logic
|
|
108
|
-
* The callback function is passed an object with the following properties:
|
|
109
|
-
* - `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
|
|
110
|
-
* - `sendBack` - A function that can send events back to the parent actor
|
|
111
|
-
* - `input` - Data that was provided to the callback actor
|
|
112
|
-
* - `self` - The parent actor of the callback actor
|
|
113
|
-
* - `system` - The actor system to which the callback actor belongs
|
|
114
|
-
* The callback function can (optionally) return a cleanup function, which is called when the actor is stopped.
|
|
115
|
-
* @see {@link InvokeCallback} for more information about the callback function and its object argument
|
|
116
|
-
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
|
117
|
-
|
|
118
|
-
* @returns Callback logic
|
|
119
|
-
*
|
|
120
222
|
* @example
|
|
223
|
+
*
|
|
121
224
|
* ```typescript
|
|
122
225
|
* const callbackLogic = fromCallback(({ sendBack, receive }) => {
|
|
123
226
|
* let lockStatus = 'unlocked';
|
|
@@ -144,6 +247,24 @@ const instanceStates = /* #__PURE__ */new WeakMap();
|
|
|
144
247
|
* };
|
|
145
248
|
* });
|
|
146
249
|
* ```
|
|
250
|
+
*
|
|
251
|
+
* @param invokeCallback - The callback function used to describe the callback
|
|
252
|
+
* logic The callback function is passed an object with the following
|
|
253
|
+
* properties:
|
|
254
|
+
*
|
|
255
|
+
* - `receive` - A function that can send events back to the parent actor; the
|
|
256
|
+
* listener is then called whenever events are received by the callback
|
|
257
|
+
* actor
|
|
258
|
+
* - `sendBack` - A function that can send events back to the parent actor
|
|
259
|
+
* - `input` - Data that was provided to the callback actor
|
|
260
|
+
* - `self` - The parent actor of the callback actor
|
|
261
|
+
* - `system` - The actor system to which the callback actor belongs The callback
|
|
262
|
+
* function can (optionally) return a cleanup function, which is called
|
|
263
|
+
* when the actor is stopped.
|
|
264
|
+
*
|
|
265
|
+
* @returns Callback logic
|
|
266
|
+
* @see {@link InvokeCallback} for more information about the callback function and its object argument
|
|
267
|
+
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
|
147
268
|
*/
|
|
148
269
|
function fromCallback(invokeCallback) {
|
|
149
270
|
const logic = {
|
|
@@ -151,7 +272,8 @@ function fromCallback(invokeCallback) {
|
|
|
151
272
|
start: (state, actorScope) => {
|
|
152
273
|
const {
|
|
153
274
|
self,
|
|
154
|
-
system
|
|
275
|
+
system,
|
|
276
|
+
emit
|
|
155
277
|
} = actorScope;
|
|
156
278
|
const callbackState = {
|
|
157
279
|
receivers: undefined,
|
|
@@ -173,7 +295,8 @@ function fromCallback(invokeCallback) {
|
|
|
173
295
|
receive: listener => {
|
|
174
296
|
callbackState.receivers ??= new Set();
|
|
175
297
|
callbackState.receivers.add(listener);
|
|
176
|
-
}
|
|
298
|
+
},
|
|
299
|
+
emit
|
|
177
300
|
});
|
|
178
301
|
},
|
|
179
302
|
transition: (state, event, actorScope) => {
|
|
@@ -207,8 +330,43 @@ function fromCallback(invokeCallback) {
|
|
|
207
330
|
const XSTATE_OBSERVABLE_NEXT = 'xstate.observable.next';
|
|
208
331
|
const XSTATE_OBSERVABLE_ERROR = 'xstate.observable.error';
|
|
209
332
|
const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Represents an actor created by `fromObservable` or `fromEventObservable`.
|
|
336
|
+
*
|
|
337
|
+
* The type of `self` within the actor's logic.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
*
|
|
341
|
+
* ```ts
|
|
342
|
+
* import { fromObservable, createActor } from 'xstate';
|
|
343
|
+
* import { interval } from 'rxjs';
|
|
344
|
+
*
|
|
345
|
+
* // The type of the value observed by the actor's logic.
|
|
346
|
+
* type Context = number;
|
|
347
|
+
* // The actor's input.
|
|
348
|
+
* type Input = { period?: number };
|
|
349
|
+
*
|
|
350
|
+
* // Actor logic that observes a number incremented every `input.period`
|
|
351
|
+
* // milliseconds (default: 1_000).
|
|
352
|
+
* const logic = fromObservable<Context, Input>(({ input, self }) => {
|
|
353
|
+
* self;
|
|
354
|
+
* // ^? ObservableActorRef<Event, Input>
|
|
355
|
+
*
|
|
356
|
+
* return interval(input.period ?? 1_000);
|
|
357
|
+
* });
|
|
358
|
+
*
|
|
359
|
+
* const actor = createActor(logic, { input: { period: 2_000 } });
|
|
360
|
+
* // ^? ObservableActorRef<Event, Input>
|
|
361
|
+
* ```
|
|
362
|
+
*
|
|
363
|
+
* @see {@link fromObservable}
|
|
364
|
+
* @see {@link fromEventObservable}
|
|
365
|
+
*/
|
|
366
|
+
|
|
210
367
|
/**
|
|
211
|
-
* Observable actor logic is described by an observable stream of values. Actors
|
|
368
|
+
* Observable actor logic is described by an observable stream of values. Actors
|
|
369
|
+
* created from observable logic (“observable actors”) can:
|
|
212
370
|
*
|
|
213
371
|
* - Emit snapshots of the observable’s emitted value
|
|
214
372
|
*
|
|
@@ -216,16 +374,10 @@ const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
|
|
|
216
374
|
*
|
|
217
375
|
* Sending events to observable actors will have no effect.
|
|
218
376
|
*
|
|
219
|
-
* @param observableCreator A function that creates an observable. It receives one argument, an object with the following properties:
|
|
220
|
-
* - `input` - Data that was provided to the observable actor
|
|
221
|
-
* - `self` - The parent actor
|
|
222
|
-
* - `system` - The actor system to which the observable actor belongs
|
|
223
|
-
*
|
|
224
|
-
* It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
|
|
225
|
-
*
|
|
226
377
|
* @example
|
|
378
|
+
*
|
|
227
379
|
* ```ts
|
|
228
|
-
* import { fromObservable, createActor } from 'xstate'
|
|
380
|
+
* import { fromObservable, createActor } from 'xstate';
|
|
229
381
|
* import { interval } from 'rxjs';
|
|
230
382
|
*
|
|
231
383
|
* const logic = fromObservable((obj) => interval(1000));
|
|
@@ -244,6 +396,15 @@ const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
|
|
|
244
396
|
* // ...
|
|
245
397
|
* ```
|
|
246
398
|
*
|
|
399
|
+
* @param observableCreator A function that creates an observable. It receives
|
|
400
|
+
* one argument, an object with the following properties:
|
|
401
|
+
*
|
|
402
|
+
* - `input` - Data that was provided to the observable actor
|
|
403
|
+
* - `self` - The parent actor
|
|
404
|
+
* - `system` - The actor system to which the observable actor belongs
|
|
405
|
+
*
|
|
406
|
+
* It should return a {@link Subscribable}, which is compatible with an RxJS
|
|
407
|
+
* Observable, although RxJS is not required to create them.
|
|
247
408
|
* @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
|
|
248
409
|
* @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
|
|
249
410
|
*/
|
|
@@ -251,12 +412,7 @@ function fromObservable(observableCreator) {
|
|
|
251
412
|
// TODO: add event types
|
|
252
413
|
const logic = {
|
|
253
414
|
config: observableCreator,
|
|
254
|
-
transition: (snapshot, event
|
|
255
|
-
self,
|
|
256
|
-
id,
|
|
257
|
-
defer,
|
|
258
|
-
system
|
|
259
|
-
}) => {
|
|
415
|
+
transition: (snapshot, event) => {
|
|
260
416
|
if (snapshot.status !== 'active') {
|
|
261
417
|
return snapshot;
|
|
262
418
|
}
|
|
@@ -308,7 +464,8 @@ function fromObservable(observableCreator) {
|
|
|
308
464
|
},
|
|
309
465
|
start: (state, {
|
|
310
466
|
self,
|
|
311
|
-
system
|
|
467
|
+
system,
|
|
468
|
+
emit
|
|
312
469
|
}) => {
|
|
313
470
|
if (state.status === 'done') {
|
|
314
471
|
// Do not restart a completed observable
|
|
@@ -317,7 +474,8 @@ function fromObservable(observableCreator) {
|
|
|
317
474
|
state._subscription = observableCreator({
|
|
318
475
|
input: state.input,
|
|
319
476
|
system,
|
|
320
|
-
self
|
|
477
|
+
self,
|
|
478
|
+
emit
|
|
321
479
|
}).subscribe({
|
|
322
480
|
next: value => {
|
|
323
481
|
system._relay(self, self, {
|
|
@@ -351,24 +509,20 @@ function fromObservable(observableCreator) {
|
|
|
351
509
|
}
|
|
352
510
|
|
|
353
511
|
/**
|
|
354
|
-
* Creates event observable logic that listens to an observable that delivers
|
|
512
|
+
* Creates event observable logic that listens to an observable that delivers
|
|
513
|
+
* event objects.
|
|
355
514
|
*
|
|
356
|
-
* Event observable actor logic is described by an observable stream of
|
|
515
|
+
* Event observable actor logic is described by an observable stream of
|
|
516
|
+
* {@link https://stately.ai/docs/transitions#event-objects | event objects}.
|
|
517
|
+
* Actors created from event observable logic (“event observable actors”) can:
|
|
357
518
|
*
|
|
358
519
|
* - Implicitly send events to its parent actor
|
|
359
520
|
* - Emit snapshots of its emitted event objects
|
|
360
521
|
*
|
|
361
522
|
* Sending events to event observable actors will have no effect.
|
|
362
523
|
*
|
|
363
|
-
* @param lazyObservable A function that creates an observable that delivers event objects. It receives one argument, an object with the following properties:
|
|
364
|
-
*
|
|
365
|
-
* - `input` - Data that was provided to the event observable actor
|
|
366
|
-
* - `self` - The parent actor
|
|
367
|
-
* - `system` - The actor system to which the event observable actor belongs.
|
|
368
|
-
*
|
|
369
|
-
* It should return a {@link Subscribable}, which is compatible with an RxJS Observable, although RxJS is not required to create them.
|
|
370
|
-
*
|
|
371
524
|
* @example
|
|
525
|
+
*
|
|
372
526
|
* ```ts
|
|
373
527
|
* import {
|
|
374
528
|
* fromEventObservable,
|
|
@@ -379,20 +533,31 @@ function fromObservable(observableCreator) {
|
|
|
379
533
|
* } from 'xstate';
|
|
380
534
|
* import { fromEvent } from 'rxjs';
|
|
381
535
|
*
|
|
382
|
-
* const mouseClickLogic = fromEventObservable(
|
|
383
|
-
* fromEvent(document.body, 'click') as Subscribable<EventObject>
|
|
536
|
+
* const mouseClickLogic = fromEventObservable(
|
|
537
|
+
* () => fromEvent(document.body, 'click') as Subscribable<EventObject>
|
|
384
538
|
* );
|
|
385
539
|
*
|
|
386
540
|
* const canvasMachine = createMachine({
|
|
387
541
|
* invoke: {
|
|
388
542
|
* // Will send mouse `click` events to the canvas actor
|
|
389
|
-
* src: mouseClickLogic
|
|
543
|
+
* src: mouseClickLogic
|
|
390
544
|
* }
|
|
391
545
|
* });
|
|
392
546
|
*
|
|
393
547
|
* const canvasActor = createActor(canvasMachine);
|
|
394
548
|
* canvasActor.start();
|
|
395
549
|
* ```
|
|
550
|
+
*
|
|
551
|
+
* @param lazyObservable A function that creates an observable that delivers
|
|
552
|
+
* event objects. It receives one argument, an object with the following
|
|
553
|
+
* properties:
|
|
554
|
+
*
|
|
555
|
+
* - `input` - Data that was provided to the event observable actor
|
|
556
|
+
* - `self` - The parent actor
|
|
557
|
+
* - `system` - The actor system to which the event observable actor belongs.
|
|
558
|
+
*
|
|
559
|
+
* It should return a {@link Subscribable}, which is compatible with an RxJS
|
|
560
|
+
* Observable, although RxJS is not required to create them.
|
|
396
561
|
*/
|
|
397
562
|
function fromEventObservable(lazyObservable) {
|
|
398
563
|
// TODO: event types
|
|
@@ -442,7 +607,8 @@ function fromEventObservable(lazyObservable) {
|
|
|
442
607
|
},
|
|
443
608
|
start: (state, {
|
|
444
609
|
self,
|
|
445
|
-
system
|
|
610
|
+
system,
|
|
611
|
+
emit
|
|
446
612
|
}) => {
|
|
447
613
|
if (state.status === 'done') {
|
|
448
614
|
// Do not restart a completed observable
|
|
@@ -451,7 +617,8 @@ function fromEventObservable(lazyObservable) {
|
|
|
451
617
|
state._subscription = lazyObservable({
|
|
452
618
|
input: state.input,
|
|
453
619
|
system,
|
|
454
|
-
self
|
|
620
|
+
self,
|
|
621
|
+
emit
|
|
455
622
|
}).subscribe({
|
|
456
623
|
next: value => {
|
|
457
624
|
if (self._parent) {
|
|
@@ -485,27 +652,61 @@ function fromEventObservable(lazyObservable) {
|
|
|
485
652
|
|
|
486
653
|
const XSTATE_PROMISE_RESOLVE = 'xstate.promise.resolve';
|
|
487
654
|
const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
|
|
655
|
+
|
|
656
|
+
/**
|
|
657
|
+
* Represents an actor created by `fromPromise`.
|
|
658
|
+
*
|
|
659
|
+
* The type of `self` within the actor's logic.
|
|
660
|
+
*
|
|
661
|
+
* @example
|
|
662
|
+
*
|
|
663
|
+
* ```ts
|
|
664
|
+
* import { fromPromise, createActor } from 'xstate';
|
|
665
|
+
*
|
|
666
|
+
* // The actor's resolved output
|
|
667
|
+
* type Output = string;
|
|
668
|
+
* // The actor's input.
|
|
669
|
+
* type Input = { message: string };
|
|
670
|
+
*
|
|
671
|
+
* // Actor logic that fetches the url of an image of a cat saying `input.message`.
|
|
672
|
+
* const logic = fromPromise<Output, Input>(async ({ input, self }) => {
|
|
673
|
+
* self;
|
|
674
|
+
* // ^? PromiseActorRef<Output, Input>
|
|
675
|
+
*
|
|
676
|
+
* const data = await fetch(
|
|
677
|
+
* `https://cataas.com/cat/says/${input.message}`
|
|
678
|
+
* );
|
|
679
|
+
* const url = await data.json();
|
|
680
|
+
* return url;
|
|
681
|
+
* });
|
|
682
|
+
*
|
|
683
|
+
* const actor = createActor(logic, { input: { message: 'hello world' } });
|
|
684
|
+
* // ^? PromiseActorRef<Output, Input>
|
|
685
|
+
* ```
|
|
686
|
+
*
|
|
687
|
+
* @see {@link fromPromise}
|
|
688
|
+
*/
|
|
689
|
+
|
|
690
|
+
const controllerMap = new WeakMap();
|
|
691
|
+
|
|
488
692
|
/**
|
|
489
|
-
* An actor logic creator which returns promise logic as defined by an async
|
|
693
|
+
* An actor logic creator which returns promise logic as defined by an async
|
|
694
|
+
* process that resolves or rejects after some time.
|
|
490
695
|
*
|
|
491
696
|
* Actors created from promise actor logic (“promise actors”) can:
|
|
697
|
+
*
|
|
492
698
|
* - Emit the resolved value of the promise
|
|
493
699
|
* - Output the resolved value of the promise
|
|
494
700
|
*
|
|
495
701
|
* Sending events to promise actors will have no effect.
|
|
496
702
|
*
|
|
497
|
-
* @param promiseCreator
|
|
498
|
-
* A function which returns a Promise, and accepts an object with the following properties:
|
|
499
|
-
* - `input` - Data that was provided to the promise actor
|
|
500
|
-
* - `self` - The parent actor of the promise actor
|
|
501
|
-
* - `system` - The actor system to which the promise actor belongs
|
|
502
|
-
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
|
503
|
-
*
|
|
504
703
|
* @example
|
|
704
|
+
*
|
|
505
705
|
* ```ts
|
|
506
706
|
* const promiseLogic = fromPromise(async () => {
|
|
507
|
-
* const result = await fetch('https://example.com/...')
|
|
508
|
-
*
|
|
707
|
+
* const result = await fetch('https://example.com/...').then((data) =>
|
|
708
|
+
* data.json()
|
|
709
|
+
* );
|
|
509
710
|
*
|
|
510
711
|
* return result;
|
|
511
712
|
* });
|
|
@@ -528,9 +729,16 @@ const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
|
|
|
528
729
|
* // ...
|
|
529
730
|
* // }
|
|
530
731
|
* ```
|
|
732
|
+
*
|
|
733
|
+
* @param promiseCreator A function which returns a Promise, and accepts an
|
|
734
|
+
* object with the following properties:
|
|
735
|
+
*
|
|
736
|
+
* - `input` - Data that was provided to the promise actor
|
|
737
|
+
* - `self` - The parent actor of the promise actor
|
|
738
|
+
* - `system` - The actor system to which the promise actor belongs
|
|
739
|
+
*
|
|
740
|
+
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
|
531
741
|
*/
|
|
532
|
-
|
|
533
|
-
const controllerMap = new WeakMap();
|
|
534
742
|
function fromPromise(promiseCreator) {
|
|
535
743
|
const logic = {
|
|
536
744
|
config: promiseCreator,
|
|
@@ -571,7 +779,8 @@ function fromPromise(promiseCreator) {
|
|
|
571
779
|
},
|
|
572
780
|
start: (state, {
|
|
573
781
|
self,
|
|
574
|
-
system
|
|
782
|
+
system,
|
|
783
|
+
emit
|
|
575
784
|
}) => {
|
|
576
785
|
// TODO: determine how to allow customizing this so that promises
|
|
577
786
|
// can be restarted if necessary
|
|
@@ -584,7 +793,8 @@ function fromPromise(promiseCreator) {
|
|
|
584
793
|
input: state.input,
|
|
585
794
|
system,
|
|
586
795
|
self,
|
|
587
|
-
signal: controller.signal
|
|
796
|
+
signal: controller.signal,
|
|
797
|
+
emit
|
|
588
798
|
}));
|
|
589
799
|
resolvedPromise.then(response => {
|
|
590
800
|
if (self.getSnapshot().status !== 'active') {
|