xstate 6.0.0-alpha.48 → 6.0.0-alpha.49

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 (62) hide show
  1. package/README.md +23 -0
  2. package/dist/{StateMachine-b09d2b00.cjs.js → StateMachine-0adb3a70.cjs.js} +1 -1
  3. package/dist/{StateMachine-b3a890bf.development.cjs.js → StateMachine-29450e6d.development.cjs.js} +1 -1
  4. package/dist/{StateMachine-8329769a.esm.js → StateMachine-2af31c11.esm.js} +1 -1
  5. package/dist/{StateMachine-88b3c26a.development.esm.js → StateMachine-faf50c5c.development.esm.js} +1 -1
  6. package/dist/{createActor-27287c65.esm.js → createActor-268165d4.esm.js} +1 -1
  7. package/dist/{createActor-309d77eb.development.esm.js → createActor-88709f74.development.esm.js} +1 -1
  8. package/dist/{createActor-cc3d6dde.development.cjs.js → createActor-a1e0dffb.development.cjs.js} +0 -3
  9. package/dist/{createActor-9b41ade9.cjs.js → createActor-b4dd05ae.cjs.js} +0 -3
  10. package/dist/declarations/src/fsm/createFSMActor.d.ts +67 -0
  11. package/dist/declarations/src/fsm/index.d.ts +2 -0
  12. package/dist/declarations/src/fsm.d.ts +5 -4
  13. package/dist/{effectDescriptor-b1f5deb6.esm.js → effectDescriptor-30c9a744.esm.js} +1 -1
  14. package/dist/{effectDescriptor-7707794b.development.esm.js → effectDescriptor-39d22100.development.esm.js} +1 -1
  15. package/dist/{effectDescriptor-a1678d06.development.cjs.js → effectDescriptor-7ff6802f.development.cjs.js} +1 -1
  16. package/dist/{effectDescriptor-bd36a33e.cjs.js → effectDescriptor-be348e50.cjs.js} +1 -1
  17. package/dist/fsm-7a2e9697.cjs.js +777 -0
  18. package/dist/fsm-b804b3e9.esm.js +775 -0
  19. package/dist/fsm-c1316948.development.cjs.js +777 -0
  20. package/dist/fsm-e18a7cb4.development.esm.js +775 -0
  21. package/dist/{types-81d03bef.esm.js → types-3c3455f7.esm.js} +1 -1
  22. package/dist/{types-1793b8b6.development.esm.js → types-adda3842.development.esm.js} +1 -1
  23. package/dist/{types-7e468202.development.cjs.js → types-b3fea5bd.development.cjs.js} +1 -1
  24. package/dist/{types-9c1bc85f.cjs.js → types-fbabaeee.cjs.js} +1 -1
  25. package/dist/xstate-actors.cjs.js +1 -1
  26. package/dist/xstate-actors.development.cjs.js +1 -1
  27. package/dist/xstate-actors.development.esm.js +2 -2
  28. package/dist/xstate-actors.esm.js +2 -2
  29. package/dist/xstate-durable.cjs.js +2 -2
  30. package/dist/xstate-durable.development.cjs.js +2 -2
  31. package/dist/xstate-durable.development.esm.js +2 -2
  32. package/dist/xstate-durable.esm.js +2 -2
  33. package/dist/xstate-fsm.cjs.d.mts +2 -0
  34. package/dist/xstate-fsm.cjs.d.ts +2 -0
  35. package/dist/xstate-fsm.cjs.js +409 -0
  36. package/dist/xstate-fsm.cjs.mjs +5 -0
  37. package/dist/xstate-fsm.development.cjs.js +409 -0
  38. package/dist/xstate-fsm.development.cjs.mjs +5 -0
  39. package/dist/xstate-fsm.development.esm.js +403 -0
  40. package/dist/xstate-fsm.esm.js +403 -0
  41. package/dist/xstate-fsm.umd.min.js +2 -0
  42. package/dist/xstate-fsm.umd.min.js.map +1 -0
  43. package/dist/xstate-graph.cjs.js +2 -2
  44. package/dist/xstate-graph.development.cjs.js +2 -2
  45. package/dist/xstate-graph.development.esm.js +2 -2
  46. package/dist/xstate-graph.esm.js +2 -2
  47. package/dist/xstate-scxml.cjs.js +3 -3
  48. package/dist/xstate-scxml.development.cjs.js +3 -3
  49. package/dist/xstate-scxml.development.esm.js +3 -3
  50. package/dist/xstate-scxml.esm.js +3 -3
  51. package/dist/xstate-validation.cjs.js +1 -1
  52. package/dist/xstate-validation.development.cjs.js +1 -1
  53. package/dist/xstate-validation.development.esm.js +1 -1
  54. package/dist/xstate-validation.esm.js +1 -1
  55. package/dist/xstate.cjs.js +6 -380
  56. package/dist/xstate.development.cjs.js +6 -380
  57. package/dist/xstate.development.esm.js +10 -384
  58. package/dist/xstate.esm.js +10 -384
  59. package/dist/xstate.umd.min.js +1 -1
  60. package/dist/xstate.umd.min.js.map +1 -1
  61. package/fsm/package.json +8 -0
  62. package/package.json +18 -2
@@ -3,10 +3,11 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var dist_xstateActors = require('./xstate-actors.development.cjs.js');
6
- var createActor = require('./createActor-cc3d6dde.development.cjs.js');
7
- var types$1 = require('./types-7e468202.development.cjs.js');
8
- var StateMachine = require('./StateMachine-b3a890bf.development.cjs.js');
9
- var effectDescriptor = require('./effectDescriptor-a1678d06.development.cjs.js');
6
+ var createActor = require('./createActor-a1e0dffb.development.cjs.js');
7
+ var types$1 = require('./types-b3fea5bd.development.cjs.js');
8
+ var fsm = require('./fsm-c1316948.development.cjs.js');
9
+ var StateMachine = require('./StateMachine-29450e6d.development.cjs.js');
10
+ var effectDescriptor = require('./effectDescriptor-7ff6802f.development.cjs.js');
10
11
  require('./validation-f2714a35.development.cjs.js');
11
12
 
12
13
  /**
@@ -41,381 +42,6 @@ function assertEvent(event, type) {
41
42
  }
42
43
  }
43
44
 
44
- const emptySources = {
45
- actions: {},
46
- actors: {},
47
- guards: {},
48
- delays: {}
49
- };
50
- const emptyExecutableActions = [];
51
- const emptyRawActions = [];
52
- const builtInActionSet = new Set(Object.values(createActor.builtInActions));
53
- function toArray(value) {
54
- return value === undefined ? [] : Array.isArray(value) ? value : [value];
55
- }
56
- function resolveContext(context, input) {
57
- return typeof context === 'function' ? context({
58
- input
59
- }) : context ?? {};
60
- }
61
- function resolveTransitionContext(context, args) {
62
- return typeof context === 'function' ? context(args) : context;
63
- }
64
- function resolveInput(input, context, event) {
65
- return typeof input === 'function' ? input({
66
- context,
67
- event
68
- }) : input;
69
- }
70
- function mergeContextPatch(context, patch) {
71
- for (const key of Object.keys(patch)) {
72
- if (!Object.prototype.hasOwnProperty.call(context, key) || !Object.is(context[key], patch[key])) {
73
- return {
74
- ...context,
75
- ...patch
76
- };
77
- }
78
- }
79
- return context;
80
- }
81
- function createSnapshot(value, context, input, machine, stateInput) {
82
- return {
83
- status: 'active',
84
- output: undefined,
85
- error: undefined,
86
- value,
87
- context,
88
- input,
89
- children: {},
90
- timers: {},
91
- _nextTimerId: 0,
92
- _stateInput: stateInput,
93
- machine
94
- };
95
- }
96
- function cloneSnapshot(snapshot, value, context, stateInput) {
97
- return {
98
- status: snapshot.status,
99
- output: snapshot.output,
100
- error: snapshot.error,
101
- value,
102
- context,
103
- input: snapshot.input,
104
- children: snapshot.children,
105
- timers: snapshot.timers,
106
- _nextTimerId: snapshot._nextTimerId,
107
- // Carried across state changes: dropping these would let a later spawn
108
- // reuse the id of a still-running child.
109
- _nextActorIds: snapshot._nextActorIds,
110
- _stateInput: stateInput,
111
- machine: snapshot.machine
112
- };
113
- }
114
- function stopSnapshot(snapshot) {
115
- return {
116
- status: 'stopped',
117
- output: undefined,
118
- error: undefined,
119
- value: snapshot.value,
120
- context: snapshot.context,
121
- input: undefined,
122
- children: snapshot.children,
123
- timers: snapshot.timers,
124
- _nextTimerId: snapshot._nextTimerId,
125
- _nextActorIds: snapshot._nextActorIds,
126
- _stateInput: snapshot._stateInput,
127
- machine: snapshot.machine
128
- };
129
- }
130
- function assertNoStringTransitions(config) {
131
- for (const [stateKey, stateConfig] of Object.entries(config.states)) {
132
- for (const [eventType, transitionConfig] of Object.entries(stateConfig.on ?? {})) {
133
- for (const transition of toArray(transitionConfig)) {
134
- if (typeof transition === 'string') {
135
- const target = transition;
136
- throw new Error(`Invalid transition for "${stateKey}.${eventType}": use { target: "${target}" } instead of a string target.`);
137
- }
138
- }
139
- }
140
- }
141
- }
142
- function resolveSimpleEnqueuedActions(rawActions) {
143
- const executableActions = [];
144
- for (const action of rawActions) {
145
- if (!action || typeof action !== 'object' || !('action' in action) || typeof action.action !== 'function' || builtInActionSet.has(action.action) || '_special' in action.action) {
146
- return undefined;
147
- }
148
- executableActions.push({
149
- kind: 'action',
150
- type: action.action.name || '(anonymous)',
151
- params: undefined,
152
- args: action.args,
153
- action: action.action,
154
- exec() {
155
- return this.action?.(...this.args);
156
- }
157
- });
158
- }
159
- return executableActions;
160
- }
161
- function createFSM(config) {
162
- const machine = {
163
- id: config.id ?? '(fsm)',
164
- sources: emptySources
165
- };
166
- assertNoStringTransitions(config);
167
- const runActions = (snapshot, event, actorScope, rawActions) => {
168
- if (!rawActions?.length) {
169
- return [snapshot, emptyExecutableActions];
170
- }
171
- const simpleExecutableActions = resolveSimpleEnqueuedActions(rawActions);
172
- if (simpleExecutableActions) {
173
- return [snapshot, simpleExecutableActions];
174
- }
175
- return createActor.resolveActionsWithContext(snapshot, event, actorScope, rawActions);
176
- };
177
- const runStateActions = (snapshot, event, actorScope, actionsConfig, stateInput, internalQueue) => {
178
- if (!actionsConfig) {
179
- return [snapshot, emptyExecutableActions];
180
- }
181
- const actions = [];
182
- const enq = createActor.createTransitionEnqueue(actorScope, actions, internalQueue, true);
183
- let context;
184
- const actionCount = Array.isArray(actionsConfig) ? actionsConfig.length : 1;
185
- for (let i = 0; i < actionCount; i++) {
186
- const action = Array.isArray(actionsConfig) ? actionsConfig[i] : actionsConfig;
187
- const result = action(createActor.withActorScope({
188
- context: context ?? snapshot.context,
189
- event: event,
190
- input: stateInput,
191
- value: snapshot.value,
192
- children: snapshot.children
193
- }, actorScope), enq);
194
- if (result?.context !== undefined) {
195
- const currentContext = context ?? snapshot.context;
196
- const nextContext = mergeContextPatch(currentContext, result.context);
197
- if (nextContext !== currentContext) {
198
- context = nextContext;
199
- }
200
- }
201
- }
202
- const nextSnapshot = context !== undefined ? cloneSnapshot(snapshot, snapshot.value, context, snapshot._stateInput) : snapshot;
203
- return runActions(nextSnapshot, event, actorScope, actions);
204
- };
205
- const selectTransition = (snapshot, event, actorScope, internalQueue) => {
206
- const state = config.states[snapshot.value];
207
- const transitionsConfig = state?.on?.[event.type];
208
- if (!transitionsConfig) {
209
- return undefined;
210
- }
211
- const transitionCount = Array.isArray(transitionsConfig) ? transitionsConfig.length : 1;
212
- for (let i = 0; i < transitionCount; i++) {
213
- const transition = Array.isArray(transitionsConfig) ? transitionsConfig[i] : transitionsConfig;
214
- const args = createActor.withActorScope({
215
- context: snapshot.context,
216
- event,
217
- input: snapshot.input,
218
- value: snapshot.value,
219
- children: snapshot.children
220
- }, actorScope);
221
- if (typeof transition === 'function') {
222
- const actions = [];
223
- const enq = createActor.createTransitionEnqueue(actorScope, actions, internalQueue, true);
224
- const result = transition(args, enq);
225
- if (!result) {
226
- if (actions.length) {
227
- return {
228
- actions
229
- };
230
- }
231
- continue;
232
- }
233
- return {
234
- target: result.target,
235
- context: result.context,
236
- input: result.input,
237
- actions
238
- };
239
- }
240
- if ('guard' in transition && transition.guard && !transition.guard(args)) {
241
- continue;
242
- }
243
- return {
244
- target: transition.target,
245
- context: resolveTransitionContext(transition.context, args),
246
- input: transition.input,
247
- actions: 'actions' in transition && transition.actions ? toArray(transition.actions) : emptyRawActions
248
- };
249
- }
250
- return undefined;
251
- };
252
- const transitionCore = (snapshot, event, actorScope) => {
253
- if (snapshot.status !== 'active') {
254
- return [snapshot, []];
255
- }
256
- if (event.type === createActor.XSTATE_STOP) {
257
- const actions = [];
258
- const enqueue = createActor.createTransitionEnqueue(actorScope, actions, []);
259
- for (const id of Object.keys(snapshot.timers)) {
260
- enqueue.cancel(id);
261
- }
262
- const [withoutTimers, cancelEffects] = createActor.resolveActionsWithContext(snapshot, event, actorScope, actions);
263
- return [stopSnapshot(withoutTimers), cancelEffects];
264
- }
265
- if (event.type === createActor.XSTATE_TIMER) {
266
- const timer = snapshot.timers[event.id];
267
- if (!timer) {
268
- return [snapshot, emptyExecutableActions];
269
- }
270
- const timers = {
271
- ...snapshot.timers
272
- };
273
- delete timers[timer.id];
274
- const nextSnapshot = {
275
- ...snapshot,
276
- timers
277
- };
278
- if (timer.type === '@xstate.raise') {
279
- return transitionCore(nextSnapshot, timer.event, actorScope);
280
- }
281
- const target = timer.target === 'self' ? actorScope.self : timer.target;
282
- return [nextSnapshot, [createActor.createSendToEffect(actorScope, target, timer.event)]];
283
- }
284
- const stateConfig = config.states[snapshot.value];
285
- const directTransition = stateConfig?.on?.[event.type];
286
- if (!directTransition) {
287
- return [snapshot, emptyExecutableActions];
288
- }
289
- if (!Array.isArray(directTransition) && typeof directTransition !== 'function' && !('guard' in directTransition) && !('actions' in directTransition && directTransition.actions) && typeof directTransition.input !== 'function') {
290
- const target = directTransition.target ?? snapshot.value;
291
- const stateChanged = target !== snapshot.value;
292
- if (stateChanged && (stateConfig.exit || config.states[target]?.entry)) ; else {
293
- const hasContext = directTransition.context !== undefined;
294
- const hasInput = directTransition.input !== undefined;
295
- const resolvedContext = !createActor.isLazyActorScope(actorScope) ? resolveTransitionContext(directTransition.context, {
296
- context: snapshot.context,
297
- event,
298
- input: snapshot.input,
299
- value: snapshot.value,
300
- self: actorScope.self,
301
- system: actorScope.system,
302
- parent: actorScope.self._parent,
303
- children: snapshot.children
304
- }) : typeof directTransition.context === 'function' ? directTransition.context(createActor.withActorScope({
305
- context: snapshot.context,
306
- event,
307
- input: snapshot.input,
308
- value: snapshot.value,
309
- children: snapshot.children
310
- }, actorScope)) : directTransition.context;
311
- const context = hasContext && resolvedContext ? mergeContextPatch(snapshot.context, resolvedContext) : snapshot.context;
312
- if (!stateChanged && context === snapshot.context && !hasInput && snapshot._stateInput === undefined) {
313
- return [snapshot, emptyExecutableActions];
314
- }
315
- return [cloneSnapshot(snapshot, target, context, hasInput ? resolveInput(directTransition.input, context, event) : undefined), emptyExecutableActions];
316
- }
317
- }
318
- if (typeof directTransition === 'function' && directTransition.length < 2 && !stateConfig?.exit) {
319
- const result = directTransition(createActor.withActorScope({
320
- context: snapshot.context,
321
- event,
322
- input: snapshot.input,
323
- value: snapshot.value,
324
- children: snapshot.children
325
- }, actorScope), undefined);
326
- if (result) {
327
- const target = result.target ?? snapshot.value;
328
- if (!config.states[target]?.entry) {
329
- const hasContext = result.context !== undefined;
330
- const hasInput = result.input !== undefined;
331
- const context = hasContext && result.context ? mergeContextPatch(snapshot.context, result.context) : snapshot.context;
332
- if (target === snapshot.value && context === snapshot.context && !hasInput && snapshot._stateInput === undefined) {
333
- return [snapshot, emptyExecutableActions];
334
- }
335
- return [cloneSnapshot(snapshot, target, context, hasInput ? resolveInput(result.input, context, event) : undefined), emptyExecutableActions];
336
- }
337
- }
338
- }
339
- let nextSnapshot = snapshot;
340
- const executableActions = [];
341
- const internalQueue = [event];
342
- let iterations = 0;
343
- while (internalQueue.length) {
344
- if (++iterations > 1000) {
345
- throw new Error('FSM microstep count exceeded 1000');
346
- }
347
- const nextEvent = internalQueue.shift();
348
- const selected = selectTransition(nextSnapshot, nextEvent, actorScope, internalQueue);
349
- if (!selected) {
350
- continue;
351
- }
352
- const nextValue = selected.target ?? nextSnapshot.value;
353
- const stateChanged = nextValue !== nextSnapshot.value;
354
- if (stateChanged) {
355
- const [exited, exitActions] = runStateActions(nextSnapshot, nextEvent, actorScope, config.states[nextSnapshot.value]?.exit, nextSnapshot._stateInput, internalQueue);
356
- nextSnapshot = exited;
357
- executableActions.push(...exitActions);
358
- }
359
- let context = nextSnapshot.context;
360
- if (selected.context !== undefined) {
361
- context = mergeContextPatch(context, selected.context);
362
- }
363
- const hasInput = selected.input !== undefined;
364
- const stateInput = hasInput ? resolveInput(selected.input, context, nextEvent) : undefined;
365
- if (stateChanged || context !== nextSnapshot.context || hasInput || nextSnapshot._stateInput !== undefined) {
366
- nextSnapshot = cloneSnapshot(nextSnapshot, nextValue, context, stateInput);
367
- }
368
- const [afterTransition, transitionActions] = runActions(nextSnapshot, nextEvent, actorScope, selected.actions);
369
- nextSnapshot = afterTransition;
370
- executableActions.push(...transitionActions);
371
- if (stateChanged) {
372
- const [entered, entryActions] = runStateActions(nextSnapshot, nextEvent, actorScope, config.states[nextValue]?.entry, stateInput, internalQueue);
373
- nextSnapshot = entered;
374
- executableActions.push(...entryActions);
375
- }
376
- }
377
- return [nextSnapshot, executableActions];
378
- };
379
- const transition = (...args) => {
380
- createActor.beginSpawnAllocation(args[2]);
381
- const [nextSnapshot, effects] = transitionCore(...args);
382
- return createActor.finalizeTransitionResult(args[2], args[0], [nextSnapshot, [...effects, ...createActor.deriveDeferredStarts(effects)]]);
383
- };
384
- const logic = {
385
- id: config.id,
386
- config,
387
- transition,
388
- initialTransition: (input, actorScope) => {
389
- createActor.beginSpawnAllocation(actorScope);
390
- const context = resolveContext(config.context, input);
391
- const snapshot = createSnapshot(config.initial, context, input, machine);
392
- const internalQueue = [];
393
- let [nextSnapshot, actions] = runStateActions(snapshot, {
394
- type: createActor.XSTATE_INIT
395
- }, actorScope, config.states[config.initial]?.entry, undefined, internalQueue);
396
- if (!actions.length) {
397
- actions = [];
398
- }
399
- while (internalQueue.length) {
400
- const [raisedSnapshot, raisedActions] = transitionCore(nextSnapshot, internalQueue.shift(), actorScope);
401
- nextSnapshot = raisedSnapshot;
402
- actions.push(...raisedActions);
403
- }
404
- return createActor.finalizeTransitionResult(actorScope, undefined, [nextSnapshot, [...actions, ...createActor.deriveDeferredStarts(actions)]]);
405
- },
406
- getInitialSnapshot: (actorScope, input) => logic.initialTransition(input, actorScope)[0],
407
- getPersistedSnapshot: ({
408
- machine: _,
409
- ...snapshot
410
- }) => snapshot,
411
- restoreSnapshot: snapshot => ({
412
- ...snapshot,
413
- machine: machine
414
- })
415
- };
416
- return logic;
417
- }
418
-
419
45
  function delayToMs(delay) {
420
46
  const parsedDelay = createActor.parseDelayToMilliseconds(delay);
421
47
  if (parsedDelay !== undefined) return parsedDelay;
@@ -1996,6 +1622,7 @@ exports.toObserver = createActor.toObserver;
1996
1622
  exports.SpecialTargets = types$1.SpecialTargets;
1997
1623
  exports.createMachine = types$1.createMachine;
1998
1624
  exports.createStateConfig = types$1.createStateConfig;
1625
+ exports.createFSM = fsm.createFSM;
1999
1626
  exports.StateMachine = StateMachine.StateMachine;
2000
1627
  exports.StateNode = StateMachine.StateNode;
2001
1628
  exports.getEffectDescriptor = effectDescriptor.getEffectDescriptor;
@@ -2006,7 +1633,6 @@ exports.initialTransition = effectDescriptor.initialTransition;
2006
1633
  exports.transition = effectDescriptor.transition;
2007
1634
  exports.SimulatedClock = SimulatedClock;
2008
1635
  exports.assertEvent = assertEvent;
2009
- exports.createFSM = createFSM;
2010
1636
  exports.createMachineFromConfig = createMachineFromConfig;
2011
1637
  exports.createSystem = createSystem;
2012
1638
  exports.isTypeSchema = isTypeSchema;