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