xstate 5.0.0-beta.9 → 5.0.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/README.md +10 -8
- package/actions/dist/xstate-actions.cjs.js +19 -5
- package/actions/dist/xstate-actions.cjs.mjs +3 -15
- package/actions/dist/xstate-actions.development.cjs.js +21 -0
- package/actions/dist/xstate-actions.development.cjs.mjs +13 -0
- package/actions/dist/xstate-actions.development.esm.js +3 -0
- package/actions/dist/xstate-actions.esm.js +3 -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 +621 -4
- package/actors/dist/xstate-actors.cjs.mjs +1 -8
- package/actors/dist/xstate-actors.development.cjs.js +624 -0
- package/actors/dist/xstate-actors.development.cjs.mjs +8 -0
- package/actors/dist/xstate-actors.development.esm.js +615 -0
- package/actors/dist/xstate-actors.esm.js +615 -2
- package/actors/dist/xstate-actors.umd.min.js +1 -1
- package/actors/dist/xstate-actors.umd.min.js.map +1 -1
- package/dev/dist/xstate-dev.cjs.js +45 -4
- package/{dist/index-0f3fdf0c.cjs.prod.js → dev/dist/xstate-dev.development.cjs.js} +6 -7
- package/dev/dist/xstate-dev.development.cjs.mjs +5 -0
- package/{dist/index-50bd0aff.esm.js → dev/dist/xstate-dev.development.esm.js} +6 -8
- package/dev/dist/xstate-dev.esm.js +42 -1
- package/dev/dist/xstate-dev.umd.min.js +1 -1
- package/dev/dist/xstate-dev.umd.min.js.map +1 -1
- package/dist/declarations/src/SimulatedClock.d.ts +1 -1
- package/dist/declarations/src/State.d.ts +47 -73
- package/dist/declarations/src/StateMachine.d.ts +29 -57
- package/dist/declarations/src/StateNode.d.ts +36 -33
- package/dist/declarations/src/actions/assign.d.ts +11 -2
- package/dist/declarations/src/actions/cancel.d.ts +7 -3
- package/dist/declarations/src/actions/enqueueActions.d.ts +32 -0
- package/dist/declarations/src/actions/log.d.ts +7 -3
- package/dist/declarations/src/actions/raise.d.ts +7 -2
- package/dist/declarations/src/actions/send.d.ts +14 -36
- package/dist/declarations/src/actions/spawnChild.d.ts +29 -0
- package/dist/declarations/src/actions/stopChild.d.ts +18 -0
- package/dist/declarations/src/actions.d.ts +8 -48
- package/dist/declarations/src/actors/callback.d.ts +91 -8
- package/dist/declarations/src/actors/index.d.ts +6 -28
- package/dist/declarations/src/actors/observable.d.ts +101 -18
- package/dist/declarations/src/actors/promise.d.ts +80 -10
- package/dist/declarations/src/actors/transition.d.ts +64 -9
- package/dist/declarations/src/constants.d.ts +3 -0
- package/dist/declarations/src/createMachine.d.ts +20 -0
- package/dist/declarations/src/dev/index.d.ts +6 -6
- package/dist/declarations/src/guards.d.ts +41 -8
- package/dist/declarations/src/index.d.ts +18 -23
- package/dist/declarations/src/interpreter.d.ts +149 -41
- package/dist/declarations/src/setup.d.ts +51 -0
- package/dist/declarations/src/spawn.d.ts +23 -2
- package/dist/declarations/src/stateUtils.d.ts +30 -45
- package/dist/declarations/src/system.d.ts +26 -2
- package/dist/declarations/src/typegenTypes.d.ts +34 -22
- package/dist/declarations/src/types.d.ts +527 -669
- package/dist/declarations/src/utils.d.ts +15 -52
- package/dist/declarations/src/waitFor.d.ts +2 -2
- package/dist/log-22e678c5.esm.js +364 -0
- package/dist/log-5e226275.cjs.js +372 -0
- package/dist/log-641cd926.development.cjs.js +394 -0
- package/dist/log-f196f85f.development.esm.js +386 -0
- package/dist/raise-34e25c2c.cjs.js +2368 -0
- package/dist/raise-62704519.development.cjs.js +2422 -0
- package/dist/raise-89c581c4.development.esm.js +2371 -0
- package/dist/raise-8bc422d1.esm.js +2317 -0
- package/dist/xstate.cjs.js +728 -4
- package/dist/xstate.cjs.mjs +9 -9
- package/dist/xstate.development.cjs.js +737 -0
- package/dist/xstate.development.cjs.mjs +39 -0
- package/dist/xstate.development.esm.js +699 -0
- package/dist/xstate.esm.js +561 -770
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/guards/dist/xstate-guards.cjs.js +12 -5
- package/guards/dist/xstate-guards.cjs.mjs +1 -2
- package/guards/dist/xstate-guards.development.cjs.js +14 -0
- package/guards/dist/xstate-guards.development.cjs.mjs +7 -0
- package/guards/dist/xstate-guards.development.esm.js +2 -0
- package/guards/dist/xstate-guards.esm.js +2 -2
- package/guards/dist/xstate-guards.umd.min.js +1 -1
- package/guards/dist/xstate-guards.umd.min.js.map +1 -1
- package/package.json +53 -1
- package/actions/dist/xstate-actions.cjs.dev.js +0 -32
- package/actions/dist/xstate-actions.cjs.prod.js +0 -32
- package/actions/dynamicAction.ts +0 -42
- package/actors/dist/xstate-actors.cjs.dev.js +0 -22
- package/actors/dist/xstate-actors.cjs.prod.js +0 -22
- package/dev/dist/xstate-dev.cjs.dev.js +0 -11
- package/dev/dist/xstate-dev.cjs.prod.js +0 -11
- package/dist/actions-b6357569.cjs.dev.js +0 -4437
- package/dist/actions-bd4a184d.cjs.prod.js +0 -4423
- package/dist/actions-de434a04.esm.js +0 -4348
- package/dist/declarations/actions/dynamicAction.d.ts +0 -5
- package/dist/declarations/src/Machine.d.ts +0 -4
- package/dist/declarations/src/Mailbox.d.ts +0 -12
- package/dist/declarations/src/actionTypes.d.ts +0 -16
- package/dist/declarations/src/actions/choose.d.ts +0 -3
- package/dist/declarations/src/actions/invoke.d.ts +0 -3
- package/dist/declarations/src/actions/pure.d.ts +0 -6
- package/dist/declarations/src/actions/stop.d.ts +0 -7
- package/dist/declarations/src/environment.d.ts +0 -1
- package/dist/declarations/src/mapState.d.ts +0 -3
- package/dist/declarations/src/memo.d.ts +0 -2
- package/dist/index-ebaab3c9.cjs.dev.js +0 -52
- package/dist/xstate.cjs.dev.js +0 -950
- package/dist/xstate.cjs.prod.js +0 -947
- package/guards/dist/xstate-guards.cjs.dev.js +0 -15
- package/guards/dist/xstate-guards.cjs.prod.js +0 -15
|
@@ -0,0 +1,624 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var guards_dist_xstateGuards = require('../../dist/raise-62704519.development.cjs.js');
|
|
6
|
+
require('../../dev/dist/xstate-dev.development.cjs.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns actor logic given a transition function and its initial state.
|
|
10
|
+
*
|
|
11
|
+
* A “transition function” is a function that takes the current `state` and received `event` object as arguments, and returns the next state, similar to a reducer.
|
|
12
|
+
*
|
|
13
|
+
* Actors created from transition logic (“transition actors”) can:
|
|
14
|
+
*
|
|
15
|
+
* - Receive events
|
|
16
|
+
* - Emit snapshots of its state
|
|
17
|
+
*
|
|
18
|
+
* The transition function’s `state` is used as its transition actor’s `context`.
|
|
19
|
+
*
|
|
20
|
+
* Note that the "state" for a transition function is provided by the initial state argument, and is not the same as the State object of an actor or a state within a machine configuration.
|
|
21
|
+
*
|
|
22
|
+
* @param transition The transition function used to describe the transition logic. It should return the next state given the current state and event. It receives the following arguments:
|
|
23
|
+
* - `state` - the current state.
|
|
24
|
+
* - `event` - the received event.
|
|
25
|
+
* - `actorScope` - the actor scope object, with properties like `self` and `system`.
|
|
26
|
+
* @param initialContext The initial state of the transition function, either an object representing the state, or a function which returns a state object. If a function, it will receive as its only argument an object with the following properties:
|
|
27
|
+
* - `input` - the `input` provided to its parent transition actor.
|
|
28
|
+
* - `self` - a reference to its parent transition actor.
|
|
29
|
+
* @see {@link https://stately.ai/docs/input | Input docs} for more information about how input is passed
|
|
30
|
+
* @returns Actor logic
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const transitionLogic = fromTransition(
|
|
35
|
+
* (state, event) => {
|
|
36
|
+
* if (event.type === 'increment') {
|
|
37
|
+
* return {
|
|
38
|
+
* ...state,
|
|
39
|
+
* count: state.count + 1,
|
|
40
|
+
* };
|
|
41
|
+
* }
|
|
42
|
+
* return state;
|
|
43
|
+
* },
|
|
44
|
+
* { count: 0 },
|
|
45
|
+
* );
|
|
46
|
+
*
|
|
47
|
+
* const transitionActor = createActor(transitionLogic);
|
|
48
|
+
* transitionActor.subscribe((snapshot) => {
|
|
49
|
+
* console.log(snapshot);
|
|
50
|
+
* });
|
|
51
|
+
* transitionActor.start();
|
|
52
|
+
* // => {
|
|
53
|
+
* // status: 'active',
|
|
54
|
+
* // context: { count: 0 },
|
|
55
|
+
* // ...
|
|
56
|
+
* // }
|
|
57
|
+
*
|
|
58
|
+
* transitionActor.send({ type: 'increment' });
|
|
59
|
+
* // => {
|
|
60
|
+
* // status: 'active',
|
|
61
|
+
* // context: { count: 1 },
|
|
62
|
+
* // ...
|
|
63
|
+
* // }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
function fromTransition(transition, initialContext) {
|
|
67
|
+
return {
|
|
68
|
+
config: transition,
|
|
69
|
+
transition: (snapshot, event, actorScope) => {
|
|
70
|
+
return {
|
|
71
|
+
...snapshot,
|
|
72
|
+
context: transition(snapshot.context, event, actorScope)
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
getInitialSnapshot: (_, input) => {
|
|
76
|
+
return {
|
|
77
|
+
status: 'active',
|
|
78
|
+
output: undefined,
|
|
79
|
+
error: undefined,
|
|
80
|
+
context: typeof initialContext === 'function' ? initialContext({
|
|
81
|
+
input
|
|
82
|
+
}) : initialContext
|
|
83
|
+
};
|
|
84
|
+
},
|
|
85
|
+
getPersistedSnapshot: snapshot => snapshot,
|
|
86
|
+
restoreSnapshot: snapshot => snapshot
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const instanceStates = /* #__PURE__ */new WeakMap();
|
|
91
|
+
/**
|
|
92
|
+
* An actor logic creator which returns callback logic as defined by a callback function.
|
|
93
|
+
*
|
|
94
|
+
* @remarks
|
|
95
|
+
* Useful for subscription-based or other free-form logic that can send events back to the parent actor.
|
|
96
|
+
*
|
|
97
|
+
* Actors created from callback logic (“callback actors”) can:
|
|
98
|
+
* - Receive events via the `receive` function
|
|
99
|
+
* - Send events to the parent actor via the `sendBack` function
|
|
100
|
+
*
|
|
101
|
+
* Callback actors are a bit different from other actors in that they:
|
|
102
|
+
* - Do not work with `onDone`
|
|
103
|
+
* - Do not produce a snapshot using `.getSnapshot()`
|
|
104
|
+
* - Do not emit values when used with `.subscribe()`
|
|
105
|
+
* - Can not be stopped with `.stop()`
|
|
106
|
+
*
|
|
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
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* const callbackLogic = fromCallback(({ sendBack, receive }) => {
|
|
123
|
+
* let lockStatus = 'unlocked';
|
|
124
|
+
*
|
|
125
|
+
* const handler = (event) => {
|
|
126
|
+
* if (lockStatus === 'locked') {
|
|
127
|
+
* return;
|
|
128
|
+
* }
|
|
129
|
+
* sendBack(event);
|
|
130
|
+
* };
|
|
131
|
+
*
|
|
132
|
+
* receive((event) => {
|
|
133
|
+
* if (event.type === 'lock') {
|
|
134
|
+
* lockStatus = 'locked';
|
|
135
|
+
* } else if (event.type === 'unlock') {
|
|
136
|
+
* lockStatus = 'unlocked';
|
|
137
|
+
* }
|
|
138
|
+
* });
|
|
139
|
+
*
|
|
140
|
+
* document.body.addEventListener('click', handler);
|
|
141
|
+
*
|
|
142
|
+
* return () => {
|
|
143
|
+
* document.body.removeEventListener('click', handler);
|
|
144
|
+
* };
|
|
145
|
+
* });
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
function fromCallback(invokeCallback) {
|
|
149
|
+
const logic = {
|
|
150
|
+
config: invokeCallback,
|
|
151
|
+
start: (state, actorScope) => {
|
|
152
|
+
const {
|
|
153
|
+
self,
|
|
154
|
+
system
|
|
155
|
+
} = actorScope;
|
|
156
|
+
const callbackState = {
|
|
157
|
+
receivers: undefined,
|
|
158
|
+
dispose: undefined
|
|
159
|
+
};
|
|
160
|
+
instanceStates.set(self, callbackState);
|
|
161
|
+
callbackState.dispose = invokeCallback({
|
|
162
|
+
input: state.input,
|
|
163
|
+
system,
|
|
164
|
+
self,
|
|
165
|
+
sendBack: event => {
|
|
166
|
+
if (self.getSnapshot().status === 'stopped') {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (self._parent) {
|
|
170
|
+
system._relay(self, self._parent, event);
|
|
171
|
+
}
|
|
172
|
+
},
|
|
173
|
+
receive: listener => {
|
|
174
|
+
callbackState.receivers ??= new Set();
|
|
175
|
+
callbackState.receivers.add(listener);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
},
|
|
179
|
+
transition: (state, event, actorScope) => {
|
|
180
|
+
const callbackState = instanceStates.get(actorScope.self);
|
|
181
|
+
if (event.type === guards_dist_xstateGuards.XSTATE_STOP) {
|
|
182
|
+
state = {
|
|
183
|
+
...state,
|
|
184
|
+
status: 'stopped',
|
|
185
|
+
error: undefined
|
|
186
|
+
};
|
|
187
|
+
callbackState.dispose?.();
|
|
188
|
+
return state;
|
|
189
|
+
}
|
|
190
|
+
callbackState.receivers?.forEach(receiver => receiver(event));
|
|
191
|
+
return state;
|
|
192
|
+
},
|
|
193
|
+
getInitialSnapshot: (_, input) => {
|
|
194
|
+
return {
|
|
195
|
+
status: 'active',
|
|
196
|
+
output: undefined,
|
|
197
|
+
error: undefined,
|
|
198
|
+
input
|
|
199
|
+
};
|
|
200
|
+
},
|
|
201
|
+
getPersistedSnapshot: snapshot => snapshot,
|
|
202
|
+
restoreSnapshot: snapshot => snapshot
|
|
203
|
+
};
|
|
204
|
+
return logic;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const XSTATE_OBSERVABLE_NEXT = 'xstate.observable.next';
|
|
208
|
+
const XSTATE_OBSERVABLE_ERROR = 'xstate.observable.error';
|
|
209
|
+
const XSTATE_OBSERVABLE_COMPLETE = 'xstate.observable.complete';
|
|
210
|
+
/**
|
|
211
|
+
* Observable actor logic is described by an observable stream of values. Actors created from observable logic (“observable actors”) can:
|
|
212
|
+
*
|
|
213
|
+
* - Emit snapshots of the observable’s emitted value
|
|
214
|
+
*
|
|
215
|
+
* The observable’s emitted value is used as its observable actor’s `context`.
|
|
216
|
+
*
|
|
217
|
+
* Sending events to observable actors will have no effect.
|
|
218
|
+
*
|
|
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
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* import { fromObservable, createActor } from 'xstate'
|
|
229
|
+
* import { interval } from 'rxjs';
|
|
230
|
+
*
|
|
231
|
+
* const logic = fromObservable((obj) => interval(1000));
|
|
232
|
+
*
|
|
233
|
+
* const actor = createActor(logic);
|
|
234
|
+
*
|
|
235
|
+
* actor.subscribe((snapshot) => {
|
|
236
|
+
* console.log(snapshot.context);
|
|
237
|
+
* });
|
|
238
|
+
*
|
|
239
|
+
* actor.start();
|
|
240
|
+
* // At every second:
|
|
241
|
+
* // Logs 0
|
|
242
|
+
* // Logs 1
|
|
243
|
+
* // Logs 2
|
|
244
|
+
* // ...
|
|
245
|
+
* ```
|
|
246
|
+
*
|
|
247
|
+
* @see {@link https://rxjs.dev} for documentation on RxJS Observable and observable creators.
|
|
248
|
+
* @see {@link Subscribable} interface in XState, which is based on and compatible with RxJS Observable.
|
|
249
|
+
*/
|
|
250
|
+
function fromObservable(observableCreator) {
|
|
251
|
+
// TODO: add event types
|
|
252
|
+
const logic = {
|
|
253
|
+
config: observableCreator,
|
|
254
|
+
transition: (snapshot, event, {
|
|
255
|
+
self,
|
|
256
|
+
id,
|
|
257
|
+
defer,
|
|
258
|
+
system
|
|
259
|
+
}) => {
|
|
260
|
+
if (snapshot.status !== 'active') {
|
|
261
|
+
return snapshot;
|
|
262
|
+
}
|
|
263
|
+
switch (event.type) {
|
|
264
|
+
case XSTATE_OBSERVABLE_NEXT:
|
|
265
|
+
{
|
|
266
|
+
const newSnapshot = {
|
|
267
|
+
...snapshot,
|
|
268
|
+
context: event.data
|
|
269
|
+
};
|
|
270
|
+
return newSnapshot;
|
|
271
|
+
}
|
|
272
|
+
case XSTATE_OBSERVABLE_ERROR:
|
|
273
|
+
return {
|
|
274
|
+
...snapshot,
|
|
275
|
+
status: 'error',
|
|
276
|
+
error: event.data,
|
|
277
|
+
input: undefined,
|
|
278
|
+
_subscription: undefined
|
|
279
|
+
};
|
|
280
|
+
case XSTATE_OBSERVABLE_COMPLETE:
|
|
281
|
+
return {
|
|
282
|
+
...snapshot,
|
|
283
|
+
status: 'done',
|
|
284
|
+
input: undefined,
|
|
285
|
+
_subscription: undefined
|
|
286
|
+
};
|
|
287
|
+
case guards_dist_xstateGuards.XSTATE_STOP:
|
|
288
|
+
snapshot._subscription.unsubscribe();
|
|
289
|
+
return {
|
|
290
|
+
...snapshot,
|
|
291
|
+
status: 'stopped',
|
|
292
|
+
input: undefined,
|
|
293
|
+
_subscription: undefined
|
|
294
|
+
};
|
|
295
|
+
default:
|
|
296
|
+
return snapshot;
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
getInitialSnapshot: (_, input) => {
|
|
300
|
+
return {
|
|
301
|
+
status: 'active',
|
|
302
|
+
output: undefined,
|
|
303
|
+
error: undefined,
|
|
304
|
+
context: undefined,
|
|
305
|
+
input,
|
|
306
|
+
_subscription: undefined
|
|
307
|
+
};
|
|
308
|
+
},
|
|
309
|
+
start: (state, {
|
|
310
|
+
self,
|
|
311
|
+
system
|
|
312
|
+
}) => {
|
|
313
|
+
if (state.status === 'done') {
|
|
314
|
+
// Do not restart a completed observable
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
state._subscription = observableCreator({
|
|
318
|
+
input: state.input,
|
|
319
|
+
system,
|
|
320
|
+
self
|
|
321
|
+
}).subscribe({
|
|
322
|
+
next: value => {
|
|
323
|
+
system._relay(self, self, {
|
|
324
|
+
type: XSTATE_OBSERVABLE_NEXT,
|
|
325
|
+
data: value
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
error: err => {
|
|
329
|
+
system._relay(self, self, {
|
|
330
|
+
type: XSTATE_OBSERVABLE_ERROR,
|
|
331
|
+
data: err
|
|
332
|
+
});
|
|
333
|
+
},
|
|
334
|
+
complete: () => {
|
|
335
|
+
system._relay(self, self, {
|
|
336
|
+
type: XSTATE_OBSERVABLE_COMPLETE
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
},
|
|
341
|
+
getPersistedSnapshot: ({
|
|
342
|
+
_subscription,
|
|
343
|
+
...state
|
|
344
|
+
}) => state,
|
|
345
|
+
restoreSnapshot: state => ({
|
|
346
|
+
...state,
|
|
347
|
+
_subscription: undefined
|
|
348
|
+
})
|
|
349
|
+
};
|
|
350
|
+
return logic;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Creates event observable logic that listens to an observable that delivers event objects.
|
|
355
|
+
*
|
|
356
|
+
* 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:
|
|
357
|
+
*
|
|
358
|
+
* - Implicitly send events to its parent actor
|
|
359
|
+
* - Emit snapshots of its emitted event objects
|
|
360
|
+
*
|
|
361
|
+
* Sending events to event observable actors will have no effect.
|
|
362
|
+
*
|
|
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
|
+
* @example
|
|
372
|
+
* ```ts
|
|
373
|
+
* import {
|
|
374
|
+
* fromEventObservable,
|
|
375
|
+
* Subscribable,
|
|
376
|
+
* EventObject,
|
|
377
|
+
* createMachine,
|
|
378
|
+
* createActor
|
|
379
|
+
* } from 'xstate';
|
|
380
|
+
* import { fromEvent } from 'rxjs';
|
|
381
|
+
*
|
|
382
|
+
* const mouseClickLogic = fromEventObservable(() =>
|
|
383
|
+
* fromEvent(document.body, 'click') as Subscribable<EventObject>
|
|
384
|
+
* );
|
|
385
|
+
*
|
|
386
|
+
* const canvasMachine = createMachine({
|
|
387
|
+
* invoke: {
|
|
388
|
+
* // Will send mouse `click` events to the canvas actor
|
|
389
|
+
* src: mouseClickLogic,
|
|
390
|
+
* }
|
|
391
|
+
* });
|
|
392
|
+
*
|
|
393
|
+
* const canvasActor = createActor(canvasMachine);
|
|
394
|
+
* canvasActor.start();
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
function fromEventObservable(lazyObservable) {
|
|
398
|
+
// TODO: event types
|
|
399
|
+
const logic = {
|
|
400
|
+
config: lazyObservable,
|
|
401
|
+
transition: (state, event) => {
|
|
402
|
+
if (state.status !== 'active') {
|
|
403
|
+
return state;
|
|
404
|
+
}
|
|
405
|
+
switch (event.type) {
|
|
406
|
+
case XSTATE_OBSERVABLE_ERROR:
|
|
407
|
+
return {
|
|
408
|
+
...state,
|
|
409
|
+
status: 'error',
|
|
410
|
+
error: event.data,
|
|
411
|
+
input: undefined,
|
|
412
|
+
_subscription: undefined
|
|
413
|
+
};
|
|
414
|
+
case XSTATE_OBSERVABLE_COMPLETE:
|
|
415
|
+
return {
|
|
416
|
+
...state,
|
|
417
|
+
status: 'done',
|
|
418
|
+
input: undefined,
|
|
419
|
+
_subscription: undefined
|
|
420
|
+
};
|
|
421
|
+
case guards_dist_xstateGuards.XSTATE_STOP:
|
|
422
|
+
state._subscription.unsubscribe();
|
|
423
|
+
return {
|
|
424
|
+
...state,
|
|
425
|
+
status: 'stopped',
|
|
426
|
+
input: undefined,
|
|
427
|
+
_subscription: undefined
|
|
428
|
+
};
|
|
429
|
+
default:
|
|
430
|
+
return state;
|
|
431
|
+
}
|
|
432
|
+
},
|
|
433
|
+
getInitialSnapshot: (_, input) => {
|
|
434
|
+
return {
|
|
435
|
+
status: 'active',
|
|
436
|
+
output: undefined,
|
|
437
|
+
error: undefined,
|
|
438
|
+
context: undefined,
|
|
439
|
+
input,
|
|
440
|
+
_subscription: undefined
|
|
441
|
+
};
|
|
442
|
+
},
|
|
443
|
+
start: (state, {
|
|
444
|
+
self,
|
|
445
|
+
system
|
|
446
|
+
}) => {
|
|
447
|
+
if (state.status === 'done') {
|
|
448
|
+
// Do not restart a completed observable
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
state._subscription = lazyObservable({
|
|
452
|
+
input: state.input,
|
|
453
|
+
system,
|
|
454
|
+
self
|
|
455
|
+
}).subscribe({
|
|
456
|
+
next: value => {
|
|
457
|
+
if (self._parent) {
|
|
458
|
+
system._relay(self, self._parent, value);
|
|
459
|
+
}
|
|
460
|
+
},
|
|
461
|
+
error: err => {
|
|
462
|
+
system._relay(self, self, {
|
|
463
|
+
type: XSTATE_OBSERVABLE_ERROR,
|
|
464
|
+
data: err
|
|
465
|
+
});
|
|
466
|
+
},
|
|
467
|
+
complete: () => {
|
|
468
|
+
system._relay(self, self, {
|
|
469
|
+
type: XSTATE_OBSERVABLE_COMPLETE
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
});
|
|
473
|
+
},
|
|
474
|
+
getPersistedSnapshot: ({
|
|
475
|
+
_subscription,
|
|
476
|
+
...snapshot
|
|
477
|
+
}) => snapshot,
|
|
478
|
+
restoreSnapshot: snapshot => ({
|
|
479
|
+
...snapshot,
|
|
480
|
+
_subscription: undefined
|
|
481
|
+
})
|
|
482
|
+
};
|
|
483
|
+
return logic;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
const XSTATE_PROMISE_RESOLVE = 'xstate.promise.resolve';
|
|
487
|
+
const XSTATE_PROMISE_REJECT = 'xstate.promise.reject';
|
|
488
|
+
/**
|
|
489
|
+
* An actor logic creator which returns promise logic as defined by an async process that resolves or rejects after some time.
|
|
490
|
+
*
|
|
491
|
+
* Actors created from promise actor logic (“promise actors”) can:
|
|
492
|
+
* - Emit the resolved value of the promise
|
|
493
|
+
* - Output the resolved value of the promise
|
|
494
|
+
*
|
|
495
|
+
* Sending events to promise actors will have no effect.
|
|
496
|
+
*
|
|
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
|
+
* @example
|
|
505
|
+
* ```ts
|
|
506
|
+
* const promiseLogic = fromPromise(async () => {
|
|
507
|
+
* const result = await fetch('https://example.com/...')
|
|
508
|
+
* .then((data) => data.json());
|
|
509
|
+
*
|
|
510
|
+
* return result;
|
|
511
|
+
* });
|
|
512
|
+
*
|
|
513
|
+
* const promiseActor = createActor(promiseLogic);
|
|
514
|
+
* promiseActor.subscribe((snapshot) => {
|
|
515
|
+
* console.log(snapshot);
|
|
516
|
+
* });
|
|
517
|
+
* promiseActor.start();
|
|
518
|
+
* // => {
|
|
519
|
+
* // output: undefined,
|
|
520
|
+
* // status: 'active'
|
|
521
|
+
* // ...
|
|
522
|
+
* // }
|
|
523
|
+
*
|
|
524
|
+
* // After promise resolves
|
|
525
|
+
* // => {
|
|
526
|
+
* // output: { ... },
|
|
527
|
+
* // status: 'done',
|
|
528
|
+
* // ...
|
|
529
|
+
* // }
|
|
530
|
+
* ```
|
|
531
|
+
*/
|
|
532
|
+
function fromPromise(promiseCreator) {
|
|
533
|
+
// TODO: add event types
|
|
534
|
+
const logic = {
|
|
535
|
+
config: promiseCreator,
|
|
536
|
+
transition: (state, event) => {
|
|
537
|
+
if (state.status !== 'active') {
|
|
538
|
+
return state;
|
|
539
|
+
}
|
|
540
|
+
switch (event.type) {
|
|
541
|
+
case XSTATE_PROMISE_RESOLVE:
|
|
542
|
+
{
|
|
543
|
+
const resolvedValue = event.data;
|
|
544
|
+
return {
|
|
545
|
+
...state,
|
|
546
|
+
status: 'done',
|
|
547
|
+
output: resolvedValue,
|
|
548
|
+
input: undefined
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
case XSTATE_PROMISE_REJECT:
|
|
552
|
+
return {
|
|
553
|
+
...state,
|
|
554
|
+
status: 'error',
|
|
555
|
+
error: event.data,
|
|
556
|
+
input: undefined
|
|
557
|
+
};
|
|
558
|
+
case guards_dist_xstateGuards.XSTATE_STOP:
|
|
559
|
+
return {
|
|
560
|
+
...state,
|
|
561
|
+
status: 'stopped',
|
|
562
|
+
input: undefined
|
|
563
|
+
};
|
|
564
|
+
default:
|
|
565
|
+
return state;
|
|
566
|
+
}
|
|
567
|
+
},
|
|
568
|
+
start: (state, {
|
|
569
|
+
self,
|
|
570
|
+
system
|
|
571
|
+
}) => {
|
|
572
|
+
// TODO: determine how to allow customizing this so that promises
|
|
573
|
+
// can be restarted if necessary
|
|
574
|
+
if (state.status !== 'active') {
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
const resolvedPromise = Promise.resolve(promiseCreator({
|
|
578
|
+
input: state.input,
|
|
579
|
+
system,
|
|
580
|
+
self
|
|
581
|
+
}));
|
|
582
|
+
resolvedPromise.then(response => {
|
|
583
|
+
if (self.getSnapshot().status !== 'active') {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
system._relay(self, self, {
|
|
587
|
+
type: XSTATE_PROMISE_RESOLVE,
|
|
588
|
+
data: response
|
|
589
|
+
});
|
|
590
|
+
}, errorData => {
|
|
591
|
+
if (self.getSnapshot().status !== 'active') {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
system._relay(self, self, {
|
|
595
|
+
type: XSTATE_PROMISE_REJECT,
|
|
596
|
+
data: errorData
|
|
597
|
+
});
|
|
598
|
+
});
|
|
599
|
+
},
|
|
600
|
+
getInitialSnapshot: (_, input) => {
|
|
601
|
+
return {
|
|
602
|
+
status: 'active',
|
|
603
|
+
output: undefined,
|
|
604
|
+
error: undefined,
|
|
605
|
+
input
|
|
606
|
+
};
|
|
607
|
+
},
|
|
608
|
+
getPersistedSnapshot: snapshot => snapshot,
|
|
609
|
+
restoreSnapshot: snapshot => snapshot
|
|
610
|
+
};
|
|
611
|
+
return logic;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const emptyLogic = fromTransition(_ => undefined, undefined);
|
|
615
|
+
function createEmptyActor() {
|
|
616
|
+
return guards_dist_xstateGuards.createActor(emptyLogic);
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
exports.createEmptyActor = createEmptyActor;
|
|
620
|
+
exports.fromCallback = fromCallback;
|
|
621
|
+
exports.fromEventObservable = fromEventObservable;
|
|
622
|
+
exports.fromObservable = fromObservable;
|
|
623
|
+
exports.fromPromise = fromPromise;
|
|
624
|
+
exports.fromTransition = fromTransition;
|