xstate 6.0.0-alpha.12 → 6.0.0-alpha.13

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 (33) hide show
  1. package/dist/{StateMachine-8a9e6cb4.development.esm.js → StateMachine-193c2d4d.development.esm.js} +42 -1
  2. package/dist/{StateMachine-7af634f9.cjs.js → StateMachine-1b26c5de.cjs.js} +42 -1
  3. package/dist/{StateMachine-c7e1996c.development.cjs.js → StateMachine-3120a7ff.development.cjs.js} +42 -1
  4. package/dist/{StateMachine-97ef0e5e.esm.js → StateMachine-614c0f36.esm.js} +42 -1
  5. package/dist/declarations/src/StateMachine.d.ts +1 -0
  6. package/dist/declarations/src/fsm.d.ts +74 -0
  7. package/dist/declarations/src/index.d.ts +1 -0
  8. package/dist/declarations/src/setup.d.ts +29 -10
  9. package/dist/declarations/src/types.d.ts +9 -3
  10. package/dist/declarations/src/types.v6.d.ts +5 -5
  11. package/dist/{index-3d018b1e.development.cjs.js → index-08d86676.development.cjs.js} +21 -15
  12. package/dist/{index-698a3320.cjs.js → index-32631949.cjs.js} +21 -15
  13. package/dist/{index-45856a94.esm.js → index-603c1cda.esm.js} +18 -16
  14. package/dist/{index-be3d4c2d.development.esm.js → index-7081e0c9.development.esm.js} +18 -16
  15. package/dist/xstate-actors.cjs.js +1 -1
  16. package/dist/xstate-actors.development.cjs.js +1 -1
  17. package/dist/xstate-actors.development.esm.js +1 -1
  18. package/dist/xstate-actors.esm.js +1 -1
  19. package/dist/xstate-graph.cjs.js +2 -2
  20. package/dist/xstate-graph.development.cjs.js +2 -2
  21. package/dist/xstate-graph.development.esm.js +2 -2
  22. package/dist/xstate-graph.esm.js +2 -2
  23. package/dist/xstate-graph.umd.min.js +1 -1
  24. package/dist/xstate-graph.umd.min.js.map +1 -1
  25. package/dist/xstate.cjs.js +336 -2
  26. package/dist/xstate.cjs.mjs +1 -0
  27. package/dist/xstate.development.cjs.js +336 -2
  28. package/dist/xstate.development.cjs.mjs +1 -0
  29. package/dist/xstate.development.esm.js +339 -6
  30. package/dist/xstate.esm.js +339 -6
  31. package/dist/xstate.umd.min.js +1 -1
  32. package/dist/xstate.umd.min.js.map +1 -1
  33. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
- import { m as matchesEventDescriptor, t as toArray, p as parseDelayToMilliseconds, i as isAtomicStateNode, c as createActor, a as macrostep, b as createInitEvent, d as initialMicrostep, g as getProperAncestors } from './index-45856a94.esm.js';
2
- export { A as Actor, T as TimeoutError, h as __unsafe_getAllOwnEventDescriptors, k as checkStateIn, c as createActor, w as createAsyncLogic, r as createCallbackLogic, q as createEmptyActor, u as createEventObservableLogic, x as createListenerLogic, v as createLogic, s as createObservableLogic, z as createSubscriptionLogic, f as getStateNodes, o as isBuiltInExecutableAction, e as isMachineSnapshot, y as listenerLogic, j as matchesState, l as pathToStateValue, B as subscriptionLogic, n as toObserver } from './index-45856a94.esm.js';
3
- import { S as StateMachine } from './StateMachine-97ef0e5e.esm.js';
4
- export { S as StateMachine, a as StateNode } from './StateMachine-97ef0e5e.esm.js';
1
+ import { m as matchesEventDescriptor, t as toArray$1, b as builtInActions, X as XSTATE_INIT, c as createTransitionEnqueue, a as XSTATE_STOP, r as resolveActionsWithContext, p as parseDelayToMilliseconds, i as isAtomicStateNode, d as createActor, e as macrostep, f as createInitEvent, g as initialMicrostep, h as getProperAncestors } from './index-603c1cda.esm.js';
2
+ export { A as Actor, T as TimeoutError, l as __unsafe_getAllOwnEventDescriptors, o as checkStateIn, d as createActor, B as createAsyncLogic, w as createCallbackLogic, v as createEmptyActor, y as createEventObservableLogic, C as createListenerLogic, z as createLogic, x as createObservableLogic, E as createSubscriptionLogic, k as getStateNodes, u as isBuiltInExecutableAction, j as isMachineSnapshot, D as listenerLogic, n as matchesState, q as pathToStateValue, F as subscriptionLogic, s as toObserver } from './index-603c1cda.esm.js';
3
+ import { S as StateMachine } from './StateMachine-614c0f36.esm.js';
4
+ export { S as StateMachine, a as StateNode } from './StateMachine-614c0f36.esm.js';
5
5
 
6
6
  /**
7
7
  * Asserts that the given event object is of the specified type or types. Throws
@@ -27,7 +27,7 @@ export { S as StateMachine, a as StateNode } from './StateMachine-97ef0e5e.esm.j
27
27
  * ```
28
28
  */
29
29
  function assertEvent(event, type) {
30
- const types = toArray(type);
30
+ const types = toArray$1(type);
31
31
  const matches = types.some(descriptor => matchesEventDescriptor(event.type, descriptor));
32
32
  if (!matches) {
33
33
  const typesText = types.length === 1 ? `type matching "${types[0]}"` : `one of types matching "${types.join('", "')}"`;
@@ -89,6 +89,339 @@ function createStateConfig(config) {
89
89
  return config;
90
90
  }
91
91
 
92
+ const emptyImplementations = {
93
+ actions: {},
94
+ actorSources: {},
95
+ guards: {},
96
+ delays: {}
97
+ };
98
+ const emptyExecutableActions = [];
99
+ const emptyRawActions = [];
100
+ const builtInActionSet = new Set(Object.values(builtInActions));
101
+ function toArray(value) {
102
+ return value === undefined ? [] : Array.isArray(value) ? value : [value];
103
+ }
104
+ function resolveContext(context, input) {
105
+ return typeof context === 'function' ? context({
106
+ input
107
+ }) : context ?? {};
108
+ }
109
+ function resolveTransitionContext(context, args) {
110
+ return typeof context === 'function' ? context(args) : context;
111
+ }
112
+ function resolveInput(input, context, event) {
113
+ return typeof input === 'function' ? input({
114
+ context,
115
+ event
116
+ }) : input;
117
+ }
118
+ function mergeContextPatch(context, patch) {
119
+ for (const key of Object.keys(patch)) {
120
+ if (!Object.prototype.hasOwnProperty.call(context, key) || !Object.is(context[key], patch[key])) {
121
+ return {
122
+ ...context,
123
+ ...patch
124
+ };
125
+ }
126
+ }
127
+ return context;
128
+ }
129
+ function createSnapshot(value, context, input, machine, stateInput) {
130
+ return {
131
+ status: 'active',
132
+ output: undefined,
133
+ error: undefined,
134
+ value,
135
+ context,
136
+ input,
137
+ children: {},
138
+ _stateInput: stateInput,
139
+ machine
140
+ };
141
+ }
142
+ function cloneSnapshot(snapshot, value, context, stateInput) {
143
+ return {
144
+ status: snapshot.status,
145
+ output: snapshot.output,
146
+ error: snapshot.error,
147
+ value,
148
+ context,
149
+ input: snapshot.input,
150
+ children: snapshot.children,
151
+ _stateInput: stateInput,
152
+ machine: snapshot.machine
153
+ };
154
+ }
155
+ function stopSnapshot(snapshot) {
156
+ return {
157
+ status: 'stopped',
158
+ output: undefined,
159
+ error: undefined,
160
+ value: snapshot.value,
161
+ context: snapshot.context,
162
+ input: undefined,
163
+ children: snapshot.children,
164
+ _stateInput: snapshot._stateInput,
165
+ machine: snapshot.machine
166
+ };
167
+ }
168
+ function assertNoStringTransitions(config) {
169
+ for (const [stateKey, stateConfig] of Object.entries(config.states)) {
170
+ for (const [eventType, transitionConfig] of Object.entries(stateConfig.on ?? {})) {
171
+ for (const transition of toArray(transitionConfig)) {
172
+ if (typeof transition === 'string') {
173
+ const target = transition;
174
+ throw new Error(`Invalid transition for "${stateKey}.${eventType}": use { target: "${target}" } instead of a string target.`);
175
+ }
176
+ }
177
+ }
178
+ }
179
+ }
180
+ function resolveSimpleEnqueuedActions(rawActions) {
181
+ const executableActions = [];
182
+ for (const action of rawActions) {
183
+ if (!action || typeof action !== 'object' || !('action' in action) || typeof action.action !== 'function' || builtInActionSet.has(action.action) || '_special' in action.action) {
184
+ return undefined;
185
+ }
186
+ executableActions.push({
187
+ type: action.action.name || '(anonymous)',
188
+ params: undefined,
189
+ args: action.args,
190
+ exec: action.args.length ? action.action.bind(null, ...action.args) : action.action
191
+ });
192
+ }
193
+ return executableActions;
194
+ }
195
+ function createFSM(config) {
196
+ const machine = {
197
+ id: config.id ?? '(fsm)',
198
+ implementations: emptyImplementations
199
+ };
200
+ assertNoStringTransitions(config);
201
+ const runActions = (snapshot, event, actorScope, rawActions) => {
202
+ if (!rawActions?.length) {
203
+ return [snapshot, emptyExecutableActions];
204
+ }
205
+ const simpleExecutableActions = resolveSimpleEnqueuedActions(rawActions);
206
+ if (simpleExecutableActions) {
207
+ return [snapshot, simpleExecutableActions];
208
+ }
209
+ return resolveActionsWithContext(snapshot, event, actorScope, rawActions);
210
+ };
211
+ const runStateActions = (snapshot, event, actorScope, actionsConfig, stateInput, internalQueue) => {
212
+ if (!actionsConfig) {
213
+ return [snapshot, emptyExecutableActions];
214
+ }
215
+ const actions = [];
216
+ const enq = createTransitionEnqueue(actorScope, actions, internalQueue, true);
217
+ let context;
218
+ const actionCount = Array.isArray(actionsConfig) ? actionsConfig.length : 1;
219
+ for (let i = 0; i < actionCount; i++) {
220
+ const action = Array.isArray(actionsConfig) ? actionsConfig[i] : actionsConfig;
221
+ const result = action({
222
+ context: context ?? snapshot.context,
223
+ event: event,
224
+ input: stateInput,
225
+ value: snapshot.value,
226
+ self: actorScope.self,
227
+ system: actorScope.system,
228
+ parent: actorScope.self._parent,
229
+ children: snapshot.children
230
+ }, enq);
231
+ if (result?.context !== undefined) {
232
+ const currentContext = context ?? snapshot.context;
233
+ const nextContext = mergeContextPatch(currentContext, result.context);
234
+ if (nextContext !== currentContext) {
235
+ context = nextContext;
236
+ }
237
+ }
238
+ }
239
+ const nextSnapshot = context !== undefined ? cloneSnapshot(snapshot, snapshot.value, context, snapshot._stateInput) : snapshot;
240
+ return runActions(nextSnapshot, event, actorScope, actions);
241
+ };
242
+ const selectTransition = (snapshot, event, actorScope, internalQueue) => {
243
+ const state = config.states[snapshot.value];
244
+ const transitionsConfig = state?.on?.[event.type];
245
+ if (!transitionsConfig) {
246
+ return undefined;
247
+ }
248
+ const transitionCount = Array.isArray(transitionsConfig) ? transitionsConfig.length : 1;
249
+ for (let i = 0; i < transitionCount; i++) {
250
+ const transition = Array.isArray(transitionsConfig) ? transitionsConfig[i] : transitionsConfig;
251
+ const args = {
252
+ context: snapshot.context,
253
+ event,
254
+ input: snapshot.input,
255
+ value: snapshot.value,
256
+ self: actorScope.self,
257
+ system: actorScope.system,
258
+ parent: actorScope.self._parent,
259
+ children: snapshot.children
260
+ };
261
+ if (typeof transition === 'function') {
262
+ const actions = [];
263
+ const enq = createTransitionEnqueue(actorScope, actions, internalQueue, true);
264
+ const result = transition(args, enq);
265
+ if (!result) {
266
+ if (actions.length) {
267
+ return {
268
+ actions
269
+ };
270
+ }
271
+ continue;
272
+ }
273
+ return {
274
+ target: result.target,
275
+ context: result.context,
276
+ input: result.input,
277
+ actions
278
+ };
279
+ }
280
+ if ('guard' in transition && transition.guard && !transition.guard(args)) {
281
+ continue;
282
+ }
283
+ return {
284
+ target: transition.target,
285
+ context: resolveTransitionContext(transition.context, args),
286
+ input: transition.input,
287
+ actions: 'actions' in transition && transition.actions ? toArray(transition.actions) : emptyRawActions
288
+ };
289
+ }
290
+ return undefined;
291
+ };
292
+ const transition = (snapshot, event, actorScope) => {
293
+ if (snapshot.status !== 'active') {
294
+ return [snapshot, []];
295
+ }
296
+ if (event.type === XSTATE_STOP) {
297
+ return [stopSnapshot(snapshot), emptyExecutableActions];
298
+ }
299
+ const stateConfig = config.states[snapshot.value];
300
+ const directTransition = stateConfig?.on?.[event.type];
301
+ if (!directTransition) {
302
+ return [snapshot, emptyExecutableActions];
303
+ }
304
+ if (!Array.isArray(directTransition) && typeof directTransition !== 'function' && !('guard' in directTransition) && !('actions' in directTransition && directTransition.actions) && typeof directTransition.input !== 'function') {
305
+ const target = directTransition.target ?? snapshot.value;
306
+ const stateChanged = target !== snapshot.value;
307
+ if (stateChanged && (stateConfig.exit || config.states[target]?.entry)) ; else {
308
+ const hasContext = directTransition.context !== undefined;
309
+ const hasInput = directTransition.input !== undefined;
310
+ const resolvedContext = resolveTransitionContext(directTransition.context, {
311
+ context: snapshot.context,
312
+ event,
313
+ input: snapshot.input,
314
+ value: snapshot.value,
315
+ self: actorScope.self,
316
+ system: actorScope.system,
317
+ parent: actorScope.self._parent,
318
+ children: snapshot.children
319
+ });
320
+ const context = hasContext && resolvedContext ? mergeContextPatch(snapshot.context, resolvedContext) : snapshot.context;
321
+ if (!stateChanged && context === snapshot.context && !hasInput && snapshot._stateInput === undefined) {
322
+ return [snapshot, emptyExecutableActions];
323
+ }
324
+ return [cloneSnapshot(snapshot, target, context, hasInput ? resolveInput(directTransition.input, context, event) : undefined), emptyExecutableActions];
325
+ }
326
+ }
327
+ if (typeof directTransition === 'function' && directTransition.length < 2 && !stateConfig?.exit) {
328
+ const result = directTransition({
329
+ context: snapshot.context,
330
+ event,
331
+ input: snapshot.input,
332
+ value: snapshot.value,
333
+ self: actorScope.self,
334
+ system: actorScope.system,
335
+ parent: actorScope.self._parent,
336
+ children: snapshot.children
337
+ }, undefined);
338
+ if (result) {
339
+ const target = result.target ?? snapshot.value;
340
+ if (!config.states[target]?.entry) {
341
+ const hasContext = result.context !== undefined;
342
+ const hasInput = result.input !== undefined;
343
+ const context = hasContext && result.context ? mergeContextPatch(snapshot.context, result.context) : snapshot.context;
344
+ if (target === snapshot.value && context === snapshot.context && !hasInput && snapshot._stateInput === undefined) {
345
+ return [snapshot, emptyExecutableActions];
346
+ }
347
+ return [cloneSnapshot(snapshot, target, context, hasInput ? resolveInput(result.input, context, event) : undefined), emptyExecutableActions];
348
+ }
349
+ }
350
+ }
351
+ let nextSnapshot = snapshot;
352
+ const executableActions = [];
353
+ const internalQueue = [event];
354
+ let iterations = 0;
355
+ while (internalQueue.length) {
356
+ if (++iterations > 1000) {
357
+ throw new Error('FSM microstep count exceeded 1000');
358
+ }
359
+ const nextEvent = internalQueue.shift();
360
+ const selected = selectTransition(nextSnapshot, nextEvent, actorScope, internalQueue);
361
+ if (!selected) {
362
+ continue;
363
+ }
364
+ const nextValue = selected.target ?? nextSnapshot.value;
365
+ const stateChanged = nextValue !== nextSnapshot.value;
366
+ if (stateChanged) {
367
+ const [exited, exitActions] = runStateActions(nextSnapshot, nextEvent, actorScope, config.states[nextSnapshot.value]?.exit, nextSnapshot._stateInput, internalQueue);
368
+ nextSnapshot = exited;
369
+ executableActions.push(...exitActions);
370
+ }
371
+ let context = nextSnapshot.context;
372
+ if (selected.context !== undefined) {
373
+ context = mergeContextPatch(context, selected.context);
374
+ }
375
+ const hasInput = selected.input !== undefined;
376
+ const stateInput = hasInput ? resolveInput(selected.input, context, nextEvent) : undefined;
377
+ if (stateChanged || context !== nextSnapshot.context || hasInput || nextSnapshot._stateInput !== undefined) {
378
+ nextSnapshot = cloneSnapshot(nextSnapshot, nextValue, context, stateInput);
379
+ }
380
+ const [afterTransition, transitionActions] = runActions(nextSnapshot, nextEvent, actorScope, selected.actions);
381
+ nextSnapshot = afterTransition;
382
+ executableActions.push(...transitionActions);
383
+ if (stateChanged) {
384
+ const [entered, entryActions] = runStateActions(nextSnapshot, nextEvent, actorScope, config.states[nextValue]?.entry, stateInput, internalQueue);
385
+ nextSnapshot = entered;
386
+ executableActions.push(...entryActions);
387
+ }
388
+ }
389
+ return [nextSnapshot, executableActions];
390
+ };
391
+ const logic = {
392
+ id: config.id,
393
+ config,
394
+ transition,
395
+ initialTransition: (input, actorScope) => {
396
+ const context = resolveContext(config.context, input);
397
+ const snapshot = createSnapshot(config.initial, context, input, machine);
398
+ const internalQueue = [];
399
+ let [nextSnapshot, actions] = runStateActions(snapshot, {
400
+ type: XSTATE_INIT
401
+ }, actorScope, config.states[config.initial]?.entry, undefined, internalQueue);
402
+ if (!actions.length) {
403
+ actions = [];
404
+ }
405
+ while (internalQueue.length) {
406
+ const [raisedSnapshot, raisedActions] = transition(nextSnapshot, internalQueue.shift(), actorScope);
407
+ nextSnapshot = raisedSnapshot;
408
+ actions.push(...raisedActions);
409
+ }
410
+ return [nextSnapshot, actions];
411
+ },
412
+ getInitialSnapshot: (actorScope, input) => logic.initialTransition(input, actorScope)[0],
413
+ getPersistedSnapshot: ({
414
+ machine: _,
415
+ ...snapshot
416
+ }) => snapshot,
417
+ restoreSnapshot: snapshot => ({
418
+ ...snapshot,
419
+ machine: machine
420
+ })
421
+ };
422
+ return logic;
423
+ }
424
+
92
425
  function delayToMs(delay) {
93
426
  const parsedDelay = parseDelayToMilliseconds(delay);
94
427
  if (parsedDelay !== undefined) return parsedDelay;
@@ -2291,4 +2624,4 @@ function waitFor(actorRef, predicate, options) {
2291
2624
  });
2292
2625
  }
2293
2626
 
2294
- export { SimulatedClock, SpecialTargets, assertEvent, createMachine, createMachineFromConfig, createStateConfig, createSystem, getInitialMicrosteps, getInitialSnapshot, getMicrosteps, getNextSnapshot, getNextTransitions, initialTransition, isTypeSchema, machineConfigToJSON, mapState, serializeMachine, setup, toPromise, transition, types, waitFor };
2627
+ export { SimulatedClock, SpecialTargets, assertEvent, createFSM, createMachine, createMachineFromConfig, createStateConfig, createSystem, getInitialMicrosteps, getInitialSnapshot, getMicrosteps, getNextSnapshot, getNextTransitions, initialTransition, isTypeSchema, machineConfigToJSON, mapState, serializeMachine, setup, toPromise, transition, types, waitFor };