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.
Files changed (107) hide show
  1. package/README.md +10 -8
  2. package/actions/dist/xstate-actions.cjs.js +19 -5
  3. package/actions/dist/xstate-actions.cjs.mjs +3 -15
  4. package/actions/dist/xstate-actions.development.cjs.js +21 -0
  5. package/actions/dist/xstate-actions.development.cjs.mjs +13 -0
  6. package/actions/dist/xstate-actions.development.esm.js +3 -0
  7. package/actions/dist/xstate-actions.esm.js +3 -2
  8. package/actions/dist/xstate-actions.umd.min.js +1 -1
  9. package/actions/dist/xstate-actions.umd.min.js.map +1 -1
  10. package/actors/dist/xstate-actors.cjs.js +621 -4
  11. package/actors/dist/xstate-actors.cjs.mjs +1 -8
  12. package/actors/dist/xstate-actors.development.cjs.js +624 -0
  13. package/actors/dist/xstate-actors.development.cjs.mjs +8 -0
  14. package/actors/dist/xstate-actors.development.esm.js +615 -0
  15. package/actors/dist/xstate-actors.esm.js +615 -2
  16. package/actors/dist/xstate-actors.umd.min.js +1 -1
  17. package/actors/dist/xstate-actors.umd.min.js.map +1 -1
  18. package/dev/dist/xstate-dev.cjs.js +45 -4
  19. package/{dist/index-0f3fdf0c.cjs.prod.js → dev/dist/xstate-dev.development.cjs.js} +6 -7
  20. package/dev/dist/xstate-dev.development.cjs.mjs +5 -0
  21. package/{dist/index-50bd0aff.esm.js → dev/dist/xstate-dev.development.esm.js} +6 -8
  22. package/dev/dist/xstate-dev.esm.js +42 -1
  23. package/dev/dist/xstate-dev.umd.min.js +1 -1
  24. package/dev/dist/xstate-dev.umd.min.js.map +1 -1
  25. package/dist/declarations/src/SimulatedClock.d.ts +1 -1
  26. package/dist/declarations/src/State.d.ts +47 -73
  27. package/dist/declarations/src/StateMachine.d.ts +29 -57
  28. package/dist/declarations/src/StateNode.d.ts +36 -33
  29. package/dist/declarations/src/actions/assign.d.ts +11 -2
  30. package/dist/declarations/src/actions/cancel.d.ts +7 -3
  31. package/dist/declarations/src/actions/enqueueActions.d.ts +32 -0
  32. package/dist/declarations/src/actions/log.d.ts +7 -3
  33. package/dist/declarations/src/actions/raise.d.ts +7 -2
  34. package/dist/declarations/src/actions/send.d.ts +14 -36
  35. package/dist/declarations/src/actions/spawnChild.d.ts +29 -0
  36. package/dist/declarations/src/actions/stopChild.d.ts +18 -0
  37. package/dist/declarations/src/actions.d.ts +8 -48
  38. package/dist/declarations/src/actors/callback.d.ts +91 -8
  39. package/dist/declarations/src/actors/index.d.ts +6 -28
  40. package/dist/declarations/src/actors/observable.d.ts +101 -18
  41. package/dist/declarations/src/actors/promise.d.ts +80 -10
  42. package/dist/declarations/src/actors/transition.d.ts +64 -9
  43. package/dist/declarations/src/constants.d.ts +3 -0
  44. package/dist/declarations/src/createMachine.d.ts +20 -0
  45. package/dist/declarations/src/dev/index.d.ts +6 -6
  46. package/dist/declarations/src/guards.d.ts +41 -8
  47. package/dist/declarations/src/index.d.ts +18 -23
  48. package/dist/declarations/src/interpreter.d.ts +149 -41
  49. package/dist/declarations/src/setup.d.ts +51 -0
  50. package/dist/declarations/src/spawn.d.ts +23 -2
  51. package/dist/declarations/src/stateUtils.d.ts +30 -45
  52. package/dist/declarations/src/system.d.ts +26 -2
  53. package/dist/declarations/src/typegenTypes.d.ts +34 -22
  54. package/dist/declarations/src/types.d.ts +527 -669
  55. package/dist/declarations/src/utils.d.ts +15 -52
  56. package/dist/declarations/src/waitFor.d.ts +2 -2
  57. package/dist/log-22e678c5.esm.js +364 -0
  58. package/dist/log-5e226275.cjs.js +372 -0
  59. package/dist/log-641cd926.development.cjs.js +394 -0
  60. package/dist/log-f196f85f.development.esm.js +386 -0
  61. package/dist/raise-34e25c2c.cjs.js +2368 -0
  62. package/dist/raise-62704519.development.cjs.js +2422 -0
  63. package/dist/raise-89c581c4.development.esm.js +2371 -0
  64. package/dist/raise-8bc422d1.esm.js +2317 -0
  65. package/dist/xstate.cjs.js +728 -4
  66. package/dist/xstate.cjs.mjs +9 -9
  67. package/dist/xstate.development.cjs.js +737 -0
  68. package/dist/xstate.development.cjs.mjs +39 -0
  69. package/dist/xstate.development.esm.js +699 -0
  70. package/dist/xstate.esm.js +561 -770
  71. package/dist/xstate.umd.min.js +1 -1
  72. package/dist/xstate.umd.min.js.map +1 -1
  73. package/guards/dist/xstate-guards.cjs.js +12 -5
  74. package/guards/dist/xstate-guards.cjs.mjs +1 -2
  75. package/guards/dist/xstate-guards.development.cjs.js +14 -0
  76. package/guards/dist/xstate-guards.development.cjs.mjs +7 -0
  77. package/guards/dist/xstate-guards.development.esm.js +2 -0
  78. package/guards/dist/xstate-guards.esm.js +2 -2
  79. package/guards/dist/xstate-guards.umd.min.js +1 -1
  80. package/guards/dist/xstate-guards.umd.min.js.map +1 -1
  81. package/package.json +53 -1
  82. package/actions/dist/xstate-actions.cjs.dev.js +0 -32
  83. package/actions/dist/xstate-actions.cjs.prod.js +0 -32
  84. package/actions/dynamicAction.ts +0 -42
  85. package/actors/dist/xstate-actors.cjs.dev.js +0 -22
  86. package/actors/dist/xstate-actors.cjs.prod.js +0 -22
  87. package/dev/dist/xstate-dev.cjs.dev.js +0 -11
  88. package/dev/dist/xstate-dev.cjs.prod.js +0 -11
  89. package/dist/actions-b6357569.cjs.dev.js +0 -4437
  90. package/dist/actions-bd4a184d.cjs.prod.js +0 -4423
  91. package/dist/actions-de434a04.esm.js +0 -4348
  92. package/dist/declarations/actions/dynamicAction.d.ts +0 -5
  93. package/dist/declarations/src/Machine.d.ts +0 -4
  94. package/dist/declarations/src/Mailbox.d.ts +0 -12
  95. package/dist/declarations/src/actionTypes.d.ts +0 -16
  96. package/dist/declarations/src/actions/choose.d.ts +0 -3
  97. package/dist/declarations/src/actions/invoke.d.ts +0 -3
  98. package/dist/declarations/src/actions/pure.d.ts +0 -6
  99. package/dist/declarations/src/actions/stop.d.ts +0 -7
  100. package/dist/declarations/src/environment.d.ts +0 -1
  101. package/dist/declarations/src/mapState.d.ts +0 -3
  102. package/dist/declarations/src/memo.d.ts +0 -2
  103. package/dist/index-ebaab3c9.cjs.dev.js +0 -52
  104. package/dist/xstate.cjs.dev.js +0 -950
  105. package/dist/xstate.cjs.prod.js +0 -947
  106. package/guards/dist/xstate-guards.cjs.dev.js +0 -15
  107. package/guards/dist/xstate-guards.cjs.prod.js +0 -15
@@ -0,0 +1,386 @@
1
+ import { R as ProcessingStatus, y as resolveReferencedActor, z as createActor, T as cloneMachineSnapshot, U as XSTATE_ERROR, V as createErrorActorEvent, e as evaluateGuard, L as cancel, M as raise, O as spawnChild, Q as stopChild } from './raise-89c581c4.development.esm.js';
2
+
3
+ function createSpawner(actorScope, {
4
+ machine,
5
+ context
6
+ }, event, spawnedChildren) {
7
+ const spawn = (src, options = {}) => {
8
+ const {
9
+ systemId,
10
+ input
11
+ } = options;
12
+ if (typeof src === 'string') {
13
+ const logic = resolveReferencedActor(machine, src);
14
+ if (!logic) {
15
+ throw new Error(`Actor logic '${src}' not implemented in machine '${machine.id}'`);
16
+ }
17
+ const actorRef = createActor(logic, {
18
+ id: options.id,
19
+ parent: actorScope.self,
20
+ syncSnapshot: options.syncSnapshot,
21
+ input: typeof input === 'function' ? input({
22
+ context,
23
+ event,
24
+ self: actorScope.self
25
+ }) : input,
26
+ src,
27
+ systemId
28
+ });
29
+ spawnedChildren[actorRef.id] = actorRef;
30
+ return actorRef;
31
+ } else {
32
+ const actorRef = createActor(src, {
33
+ id: options.id,
34
+ parent: actorScope.self,
35
+ syncSnapshot: options.syncSnapshot,
36
+ input: options.input,
37
+ src,
38
+ systemId
39
+ });
40
+ return actorRef;
41
+ }
42
+ };
43
+ return (src, options) => {
44
+ const actorRef = spawn(src, options); // TODO: fix types
45
+ spawnedChildren[actorRef.id] = actorRef;
46
+ actorScope.defer(() => {
47
+ if (actorRef._processingStatus === ProcessingStatus.Stopped) {
48
+ return;
49
+ }
50
+ actorRef.start();
51
+ });
52
+ return actorRef;
53
+ };
54
+ }
55
+
56
+ function resolveAssign(actorScope, snapshot, actionArgs, actionParams, {
57
+ assignment
58
+ }) {
59
+ if (!snapshot.context) {
60
+ throw new Error('Cannot assign to undefined `context`. Ensure that `context` is defined in the machine config.');
61
+ }
62
+ const spawnedChildren = {};
63
+ const assignArgs = {
64
+ context: snapshot.context,
65
+ event: actionArgs.event,
66
+ spawn: createSpawner(actorScope, snapshot, actionArgs.event, spawnedChildren),
67
+ self: actorScope?.self,
68
+ system: actorScope?.system
69
+ };
70
+ let partialUpdate = {};
71
+ if (typeof assignment === 'function') {
72
+ partialUpdate = assignment(assignArgs, actionParams);
73
+ } else {
74
+ for (const key of Object.keys(assignment)) {
75
+ const propAssignment = assignment[key];
76
+ partialUpdate[key] = typeof propAssignment === 'function' ? propAssignment(assignArgs, actionParams) : propAssignment;
77
+ }
78
+ }
79
+ const updatedContext = Object.assign({}, snapshot.context, partialUpdate);
80
+ return [cloneMachineSnapshot(snapshot, {
81
+ context: updatedContext,
82
+ children: Object.keys(spawnedChildren).length ? {
83
+ ...snapshot.children,
84
+ ...spawnedChildren
85
+ } : snapshot.children
86
+ })];
87
+ }
88
+ /**
89
+ * Updates the current context of the machine.
90
+ *
91
+ * @param assignment An object that represents the partial context to update.
92
+ */
93
+ function assign(assignment) {
94
+ function assign(args, params) {
95
+ {
96
+ throw new Error(`This isn't supposed to be called`);
97
+ }
98
+ }
99
+ assign.type = 'xstate.assign';
100
+ assign.assignment = assignment;
101
+ assign.resolve = resolveAssign;
102
+ return assign;
103
+ }
104
+
105
+ /**
106
+ *
107
+ * @remarks
108
+ *
109
+ * `T | unknown` reduces to `unknown` and that can be problematic when it comes to contextual typing.
110
+ * It especially is a problem when the union has a function member, like here:
111
+ *
112
+ * ```ts
113
+ * declare function test(cbOrVal: ((arg: number) => unknown) | unknown): void;
114
+ * test((arg) => {}) // oops, implicit any
115
+ * ```
116
+ *
117
+ * This type can be used to avoid this problem. This union represents the same value space as `unknown`.
118
+ */
119
+
120
+ // https://github.com/microsoft/TypeScript/issues/23182#issuecomment-379091887
121
+
122
+ /**
123
+ * The full definition of an event, with a string `type`.
124
+ */
125
+
126
+ /**
127
+ * The string or object representing the state value relative to the parent state node.
128
+ *
129
+ * @remarks
130
+ *
131
+ * - For a child atomic state node, this is a string, e.g., `"pending"`.
132
+ *
133
+ * - For complex state nodes, this is an object, e.g., `{ success: "someChildState" }`.
134
+ */
135
+
136
+ // TODO: remove once TS fixes this type-widening issue
137
+
138
+ /** @deprecated use `AnyMachineSnapshot` instead */
139
+
140
+ // TODO: possibly refactor this somehow, use even a simpler type, and maybe even make `machine.options` private or something
141
+
142
+ let SpecialTargets = /*#__PURE__*/function (SpecialTargets) {
143
+ SpecialTargets["Parent"] = "#_parent";
144
+ SpecialTargets["Internal"] = "#_internal";
145
+ return SpecialTargets;
146
+ }({});
147
+
148
+ /**
149
+ * @deprecated Use `AnyActor` instead.
150
+ */
151
+
152
+ // Based on RxJS types
153
+
154
+ /**
155
+ * @deprecated Use `Actor<T>` instead.
156
+ */
157
+
158
+ // only meant to be used internally for debugging purposes
159
+
160
+ /**
161
+ * Represents logic which can be used by an actor.
162
+ *
163
+ * @template TSnapshot - The type of the snapshot.
164
+ * @template TEvent - The type of the event object.
165
+ * @template TInput - The type of the input.
166
+ * @template TSystem - The type of the actor system.
167
+ */
168
+
169
+ function resolveSendTo(actorScope, snapshot, args, actionParams, {
170
+ to,
171
+ event: eventOrExpr,
172
+ id,
173
+ delay
174
+ }, extra) {
175
+ const delaysMap = snapshot.machine.implementations.delays;
176
+ if (typeof eventOrExpr === 'string') {
177
+ throw new Error(`Only event objects may be used with sendTo; use sendTo({ type: "${eventOrExpr}" }) instead`);
178
+ }
179
+ const resolvedEvent = typeof eventOrExpr === 'function' ? eventOrExpr(args, actionParams) : eventOrExpr;
180
+ let resolvedDelay;
181
+ if (typeof delay === 'string') {
182
+ const configDelay = delaysMap && delaysMap[delay];
183
+ resolvedDelay = typeof configDelay === 'function' ? configDelay(args, actionParams) : configDelay;
184
+ } else {
185
+ resolvedDelay = typeof delay === 'function' ? delay(args, actionParams) : delay;
186
+ }
187
+ const resolvedTarget = typeof to === 'function' ? to(args, actionParams) : to;
188
+ let targetActorRef;
189
+ if (typeof resolvedTarget === 'string') {
190
+ if (resolvedTarget === SpecialTargets.Parent) {
191
+ targetActorRef = actorScope?.self._parent;
192
+ } else if (resolvedTarget === SpecialTargets.Internal) {
193
+ targetActorRef = actorScope?.self;
194
+ } else if (resolvedTarget.startsWith('#_')) {
195
+ // SCXML compatibility: https://www.w3.org/TR/scxml/#SCXMLEventProcessor
196
+ // #_invokeid. If the target is the special term '#_invokeid', where invokeid is the invokeid of an SCXML session that the sending session has created by <invoke>, the Processor must add the event to the external queue of that session.
197
+ targetActorRef = snapshot.children[resolvedTarget.slice(2)];
198
+ } else {
199
+ targetActorRef = extra.deferredActorIds?.includes(resolvedTarget) ? resolvedTarget : snapshot.children[resolvedTarget];
200
+ }
201
+ if (!targetActorRef) {
202
+ throw new Error(`Unable to send event to actor '${resolvedTarget}' from machine '${snapshot.machine.id}'.`);
203
+ }
204
+ } else {
205
+ targetActorRef = resolvedTarget || actorScope?.self;
206
+ }
207
+ return [snapshot, {
208
+ to: targetActorRef,
209
+ event: resolvedEvent,
210
+ id,
211
+ delay: resolvedDelay
212
+ }];
213
+ }
214
+ function retryResolveSendTo(_, snapshot, params) {
215
+ if (typeof params.to === 'string') {
216
+ params.to = snapshot.children[params.to];
217
+ }
218
+ }
219
+ function executeSendTo(actorScope, params) {
220
+ if (typeof params.delay === 'number') {
221
+ actorScope.self.delaySend(params);
222
+ return;
223
+ }
224
+
225
+ // this forms an outgoing events queue
226
+ // thanks to that the recipient actors are able to read the *updated* snapshot value of the sender
227
+ actorScope.defer(() => {
228
+ const {
229
+ to,
230
+ event
231
+ } = params;
232
+ actorScope?.system._relay(actorScope.self, to, event.type === XSTATE_ERROR ? createErrorActorEvent(actorScope.self.id, event.data) : event);
233
+ });
234
+ }
235
+ /**
236
+ * Sends an event to an actor.
237
+ *
238
+ * @param actor The `ActorRef` to send the event to.
239
+ * @param event The event to send, or an expression that evaluates to the event to send
240
+ * @param options Send action options
241
+ * - `id` - The unique send event identifier (used with `cancel()`).
242
+ * - `delay` - The number of milliseconds to delay the sending of the event.
243
+ */
244
+ function sendTo(to, eventOrExpr, options) {
245
+ function sendTo(args, params) {
246
+ {
247
+ throw new Error(`This isn't supposed to be called`);
248
+ }
249
+ }
250
+ sendTo.type = 'xsnapshot.sendTo';
251
+ sendTo.to = to;
252
+ sendTo.event = eventOrExpr;
253
+ sendTo.id = options?.id;
254
+ sendTo.delay = options?.delay;
255
+ sendTo.resolve = resolveSendTo;
256
+ sendTo.retryResolve = retryResolveSendTo;
257
+ sendTo.execute = executeSendTo;
258
+ return sendTo;
259
+ }
260
+
261
+ /**
262
+ * Sends an event to this machine's parent.
263
+ *
264
+ * @param event The event to send to the parent machine.
265
+ * @param options Options to pass into the send event.
266
+ */
267
+ function sendParent(event, options) {
268
+ return sendTo(SpecialTargets.Parent, event, options);
269
+ }
270
+ /**
271
+ * Forwards (sends) an event to a specified service.
272
+ *
273
+ * @param target The target service to forward the event to.
274
+ * @param options Options to pass into the send action creator.
275
+ */
276
+ function forwardTo(target, options) {
277
+ if ((!target || typeof target === 'function')) {
278
+ const originalTarget = target;
279
+ target = (...args) => {
280
+ const resolvedTarget = typeof originalTarget === 'function' ? originalTarget(...args) : originalTarget;
281
+ if (!resolvedTarget) {
282
+ throw new Error(`Attempted to forward event to undefined actor. This risks an infinite loop in the sender.`);
283
+ }
284
+ return resolvedTarget;
285
+ };
286
+ }
287
+ return sendTo(target, ({
288
+ event
289
+ }) => event, options);
290
+ }
291
+
292
+ function resolveEnqueueActions(_, snapshot, args, _actionParams, {
293
+ collect
294
+ }) {
295
+ const actions = [];
296
+ const enqueue = function enqueue(action) {
297
+ actions.push(action);
298
+ };
299
+ enqueue.assign = (...args) => {
300
+ actions.push(assign(...args));
301
+ };
302
+ enqueue.cancel = (...args) => {
303
+ actions.push(cancel(...args));
304
+ };
305
+ enqueue.raise = (...args) => {
306
+ actions.push(raise(...args));
307
+ };
308
+ enqueue.sendTo = (...args) => {
309
+ actions.push(sendTo(...args));
310
+ };
311
+ enqueue.spawnChild = (...args) => {
312
+ actions.push(spawnChild(...args));
313
+ };
314
+ enqueue.stopChild = (...args) => {
315
+ actions.push(stopChild(...args));
316
+ };
317
+ collect({
318
+ context: args.context,
319
+ event: args.event,
320
+ enqueue,
321
+ check: guard => evaluateGuard(guard, snapshot.context, args.event, snapshot)
322
+ });
323
+ return [snapshot, undefined, actions];
324
+ }
325
+ function enqueueActions(collect) {
326
+ function enqueueActions(args, params) {
327
+ {
328
+ throw new Error(`This isn't supposed to be called`);
329
+ }
330
+ }
331
+ enqueueActions.type = 'xstate.enqueueActions';
332
+ enqueueActions.collect = collect;
333
+ enqueueActions.resolve = resolveEnqueueActions;
334
+ return enqueueActions;
335
+ }
336
+
337
+ function resolveLog(_, snapshot, actionArgs, actionParams, {
338
+ value,
339
+ label
340
+ }) {
341
+ return [snapshot, {
342
+ value: typeof value === 'function' ? value(actionArgs, actionParams) : value,
343
+ label
344
+ }];
345
+ }
346
+ function executeLog({
347
+ logger
348
+ }, {
349
+ value,
350
+ label
351
+ }) {
352
+ if (label) {
353
+ logger(label, value);
354
+ } else {
355
+ logger(value);
356
+ }
357
+ }
358
+ /**
359
+ *
360
+ * @param expr The expression function to evaluate which will be logged.
361
+ * Takes in 2 arguments:
362
+ * - `ctx` - the current state context
363
+ * - `event` - the event that caused this action to be executed.
364
+ * @param label The label to give to the logged expression.
365
+ */
366
+ function log(value = ({
367
+ context,
368
+ event
369
+ }) => ({
370
+ context,
371
+ event
372
+ }), label) {
373
+ function log(args, params) {
374
+ {
375
+ throw new Error(`This isn't supposed to be called`);
376
+ }
377
+ }
378
+ log.type = 'xstate.log';
379
+ log.value = value;
380
+ log.label = label;
381
+ log.resolve = resolveLog;
382
+ log.execute = executeLog;
383
+ return log;
384
+ }
385
+
386
+ export { SpecialTargets as S, assign as a, sendTo as b, enqueueActions as e, forwardTo as f, log as l, sendParent as s };