xstate 6.0.0-alpha.9 → 6.0.0-beta.1
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 +223 -78
- package/bin/agent.js +347 -0
- package/bin/xstate.js +58 -0
- package/dist/createActor-25900f76.cjs.js +7015 -0
- package/dist/createActor-551369be.development.esm.js +7175 -0
- package/dist/createActor-a49ae0ce.development.cjs.js +7289 -0
- package/dist/createActor-cbe3abac.esm.js +6902 -0
- package/dist/declarations/src/SimulatedClock.d.ts +2 -1
- package/dist/declarations/src/State.d.ts +66 -5
- package/dist/declarations/src/StateMachine.d.ts +103 -35
- package/dist/declarations/src/StateNode.d.ts +21 -16
- package/dist/declarations/src/actions.d.ts +11 -6
- package/dist/declarations/src/actors/callback.d.ts +11 -2
- package/dist/declarations/src/actors/index.d.ts +6 -5
- package/dist/declarations/src/actors/listener.d.ts +8 -3
- package/dist/declarations/src/actors/logic.d.ts +42 -52
- package/dist/declarations/src/actors/observable.d.ts +15 -2
- package/dist/declarations/src/actors/promise.d.ts +90 -22
- package/dist/declarations/src/actors/subscription.d.ts +9 -3
- package/dist/declarations/src/assert.d.ts +1 -0
- package/dist/declarations/src/base.types.d.ts +102 -0
- package/dist/declarations/src/createActor.d.ts +101 -27
- package/dist/declarations/src/createMachine.d.ts +38 -17
- package/dist/declarations/src/delay.d.ts +8 -0
- package/dist/declarations/src/durable/index.d.ts +231 -0
- package/dist/declarations/src/effectDescriptor.d.ts +95 -0
- package/dist/declarations/src/eventTypes.d.ts +9 -0
- package/dist/declarations/src/fsm/index.d.ts +2 -0
- package/dist/declarations/src/fsm.d.ts +169 -0
- package/dist/declarations/src/graph/adjacency.d.ts +6 -3
- package/dist/declarations/src/graph/graph.d.ts +6 -3
- package/dist/declarations/src/graph/index.d.ts +2 -3
- package/dist/declarations/src/graph/pathFromEvents.d.ts +5 -2
- package/dist/declarations/src/graph/shortestPaths.d.ts +5 -3
- package/dist/declarations/src/graph/simplePaths.d.ts +5 -3
- package/dist/declarations/src/graph/types.d.ts +113 -59
- package/dist/declarations/src/hotSwap.d.ts +18 -0
- package/dist/declarations/src/index.d.ts +28 -20
- package/dist/declarations/src/inspection.d.ts +24 -1
- package/dist/declarations/src/machineFromJSON.d.ts +171 -0
- package/dist/declarations/src/machineSchemas.d.ts +27 -0
- package/dist/declarations/src/machineVersion.types.d.ts +53 -0
- package/dist/declarations/src/machineVersions.d.ts +103 -0
- package/dist/declarations/src/mapState.d.ts +2 -0
- package/dist/declarations/src/persist/index.d.ts +36 -0
- package/dist/declarations/src/persistedSnapshotValidation.d.ts +18 -0
- package/dist/declarations/src/runtimeHelpers.d.ts +43 -0
- package/dist/declarations/src/schema.types.d.ts +31 -4
- package/dist/declarations/src/serialize.d.ts +39 -28
- package/dist/declarations/src/setup.d.ts +1074 -157
- package/dist/declarations/src/spawn.d.ts +6 -4
- package/dist/declarations/src/stateUtils.d.ts +57 -14
- package/dist/declarations/src/system.d.ts +178 -17
- package/dist/declarations/src/systemTransition.d.ts +108 -0
- package/dist/declarations/src/toPromise.d.ts +1 -0
- package/dist/declarations/src/transition.d.ts +48 -10
- package/dist/declarations/src/transitionActions.d.ts +22 -4
- package/dist/declarations/src/typeUtils.d.ts +51 -0
- package/dist/declarations/src/types.d.ts +842 -319
- package/dist/declarations/src/types.v6.d.ts +540 -106
- package/dist/declarations/src/utils.d.ts +38 -8
- package/dist/declarations/src/validation/index.d.ts +49 -0
- package/dist/declarations/src/validation.types.d.ts +29 -0
- package/dist/declarations/src/waitFor.d.ts +1 -0
- package/dist/effectDescriptor-2fa27c54.cjs.js +351 -0
- package/dist/effectDescriptor-321a8ce9.development.esm.js +343 -0
- package/dist/effectDescriptor-d8b0f749.esm.js +343 -0
- package/dist/effectDescriptor-f73da4ec.development.cjs.js +351 -0
- package/dist/machineSchemas-45c21bc2.esm.js +1292 -0
- package/dist/machineSchemas-6d96bef4.development.cjs.js +1379 -0
- package/dist/machineSchemas-88d01821.cjs.js +1300 -0
- package/dist/machineSchemas-b5736804.development.esm.js +1371 -0
- package/dist/schema.types-0ecd17b8.esm.js +71 -0
- package/dist/schema.types-15a68ddc.cjs.js +74 -0
- package/dist/schema.types-1c16f035.development.cjs.js +74 -0
- package/dist/schema.types-1cd4652e.development.esm.js +71 -0
- package/dist/validation-05f1d517.esm.js +8 -0
- package/dist/validation-4561b43c.cjs.js +10 -0
- package/dist/validation-e69470ad.development.esm.js +8 -0
- package/dist/validation-f2714a35.development.cjs.js +10 -0
- package/dist/xstate-actors.cjs.js +1068 -12
- package/dist/xstate-actors.cjs.mjs +2 -4
- package/dist/xstate-actors.development.cjs.js +1068 -12
- package/dist/xstate-actors.development.cjs.mjs +2 -4
- package/dist/xstate-actors.development.esm.js +1064 -1
- package/dist/xstate-actors.esm.js +1064 -1
- package/dist/xstate-actors.umd.min.js +1 -1
- package/dist/xstate-actors.umd.min.js.map +1 -1
- package/dist/xstate-durable.cjs.d.mts +2 -0
- package/dist/xstate-durable.cjs.d.ts +2 -0
- package/dist/xstate-durable.cjs.js +513 -0
- package/dist/xstate-durable.cjs.mjs +5 -0
- package/dist/xstate-durable.development.cjs.js +513 -0
- package/dist/xstate-durable.development.cjs.mjs +5 -0
- package/dist/xstate-durable.development.esm.js +507 -0
- package/dist/xstate-durable.esm.js +507 -0
- package/dist/xstate-durable.umd.min.js +2 -0
- package/dist/xstate-durable.umd.min.js.map +1 -0
- package/dist/xstate-fsm.cjs.d.mts +2 -0
- package/dist/xstate-fsm.cjs.d.ts +2 -0
- package/dist/xstate-fsm.cjs.js +111 -0
- package/dist/xstate-fsm.cjs.mjs +5 -0
- package/dist/xstate-fsm.development.cjs.js +111 -0
- package/dist/xstate-fsm.development.cjs.mjs +5 -0
- package/dist/xstate-fsm.development.esm.js +105 -0
- package/dist/xstate-fsm.esm.js +105 -0
- package/dist/xstate-fsm.umd.min.js +2 -0
- package/dist/xstate-fsm.umd.min.js.map +1 -0
- package/dist/xstate-graph.cjs.js +173 -457
- package/dist/xstate-graph.cjs.mjs +2 -5
- package/dist/xstate-graph.development.cjs.js +173 -457
- package/dist/xstate-graph.development.cjs.mjs +2 -5
- package/dist/xstate-graph.development.esm.js +171 -451
- package/dist/xstate-graph.esm.js +171 -451
- package/dist/xstate-graph.umd.min.js +1 -1
- package/dist/xstate-graph.umd.min.js.map +1 -1
- package/dist/xstate-persist.cjs.d.mts +2 -0
- package/dist/xstate-persist.cjs.d.ts +2 -0
- package/dist/xstate-persist.cjs.js +314 -0
- package/dist/xstate-persist.cjs.mjs +6 -0
- package/dist/xstate-persist.development.cjs.js +325 -0
- package/dist/xstate-persist.development.cjs.mjs +6 -0
- package/dist/xstate-persist.development.esm.js +319 -0
- package/dist/xstate-persist.esm.js +308 -0
- package/dist/xstate-persist.umd.min.js +2 -0
- package/dist/xstate-persist.umd.min.js.map +1 -0
- package/dist/xstate-validation.cjs.d.mts +2 -0
- package/dist/xstate-validation.cjs.d.ts +2 -0
- package/dist/xstate-validation.cjs.js +330 -0
- package/dist/xstate-validation.cjs.mjs +5 -0
- package/dist/xstate-validation.development.cjs.js +330 -0
- package/dist/xstate-validation.development.cjs.mjs +5 -0
- package/dist/xstate-validation.development.esm.js +324 -0
- package/dist/xstate-validation.esm.js +324 -0
- package/dist/xstate-validation.umd.min.js +2 -0
- package/dist/xstate-validation.umd.min.js.map +1 -0
- package/dist/xstate.cjs.js +2486 -1431
- package/dist/xstate.cjs.mjs +23 -13
- package/dist/xstate.development.cjs.js +2486 -1431
- package/dist/xstate.development.cjs.mjs +23 -13
- package/dist/xstate.development.esm.js +2445 -1399
- package/dist/xstate.esm.js +2445 -1399
- package/dist/xstate.umd.min.js +1 -1
- package/dist/xstate.umd.min.js.map +1 -1
- package/docs/actions.md +75 -0
- package/docs/actor-logic.md +250 -0
- package/docs/actors.md +64 -0
- package/docs/agents.md +141 -0
- package/docs/api-stability.json +4293 -0
- package/docs/async-logic.md +75 -0
- package/docs/async-requests.md +157 -0
- package/docs/backend-workflows.md +124 -0
- package/docs/cancellation.md +42 -0
- package/docs/cheatsheet.md +563 -0
- package/docs/choice-states.md +109 -0
- package/docs/choose-xstate.md +47 -0
- package/docs/configuration.md +161 -0
- package/docs/context.md +67 -0
- package/docs/create-actor.md +159 -0
- package/docs/data-and-effects.md +48 -0
- package/docs/delays.md +81 -0
- package/docs/durable-execution.md +520 -0
- package/docs/emitted-events.md +146 -0
- package/docs/errors.md +812 -0
- package/docs/examples.md +75 -0
- package/docs/faq.md +60 -0
- package/docs/final-states.md +149 -0
- package/docs/forms-and-wizards.md +166 -0
- package/docs/frameworks.md +69 -0
- package/docs/fsm.md +164 -0
- package/docs/glossary.md +68 -0
- package/docs/guards.md +137 -0
- package/docs/history-states.md +161 -0
- package/docs/how-xstate-runs-machines.md +86 -0
- package/docs/index.md +74 -0
- package/docs/input-output.md +69 -0
- package/docs/inspect-actor-systems.md +118 -0
- package/docs/inspection.md +75 -0
- package/docs/internal-events.md +156 -0
- package/docs/invoke.md +371 -0
- package/docs/lifecycle-and-errors.md +48 -0
- package/docs/listen-and-subscribe.md +126 -0
- package/docs/model-based-testing.md +321 -0
- package/docs/modeling.md +40 -0
- package/docs/parallel-states.md +182 -0
- package/docs/persist-and-restore-actors.md +120 -0
- package/docs/persistence.md +312 -0
- package/docs/quick-start.md +55 -0
- package/docs/retries-and-timeouts.md +122 -0
- package/docs/route-states.md +104 -0
- package/docs/scxml.md +92 -0
- package/docs/selectors.md +99 -0
- package/docs/serialization.md +127 -0
- package/docs/setup-and-provide.md +273 -0
- package/docs/snapshots.md +134 -0
- package/docs/spawn.md +212 -0
- package/docs/state-input.md +162 -0
- package/docs/statecharts.md +54 -0
- package/docs/states-events-transitions.md +82 -0
- package/docs/states.md +168 -0
- package/docs/system-transitions.md +172 -0
- package/docs/systems.md +166 -0
- package/docs/testing.md +152 -0
- package/docs/timeouts.md +125 -0
- package/docs/transition-functions.md +89 -0
- package/docs/transitions.md +187 -0
- package/docs/troubleshooting.md +56 -0
- package/docs/tutorial.md +481 -0
- package/docs/typescript.md +213 -0
- package/docs/typestates.md +112 -0
- package/docs/utilities.md +39 -0
- package/docs/why-state-machines.md +59 -0
- package/docs/xstate-v5-to-v6.md +2080 -0
- package/docs/your-first-machine.md +79 -0
- package/durable/package.json +8 -0
- package/fsm/package.json +8 -0
- package/package.json +72 -10
- package/persist/package.json +8 -0
- package/validation/package.json +8 -0
- package/actions/package.json +0 -8
- package/dev/package.json +0 -8
- package/dist/StateMachine-0aa98f54.development.esm.js +0 -746
- package/dist/StateMachine-aef700df.cjs.js +0 -732
- package/dist/StateMachine-d4931336.esm.js +0 -729
- package/dist/StateMachine-da872dde.development.cjs.js +0 -749
- package/dist/declarations/src/createMachineFromConfig.d.ts +0 -177
- package/dist/declarations/src/getNextSnapshot.d.ts +0 -37
- package/dist/declarations/src/graph/TestModel.d.ts +0 -70
- package/dist/declarations/src/graph/pathGenerators.d.ts +0 -4
- package/dist/index-219cb621.development.cjs.js +0 -4476
- package/dist/index-2f2fbd9b.development.esm.js +0 -4421
- package/dist/index-559ceab3.cjs.js +0 -4437
- package/dist/index-8828376f.esm.js +0 -4382
- package/guards/package.json +0 -8
- package/invoke/package.json +0 -4
package/README.md
CHANGED
|
@@ -1,3 +1,78 @@
|
|
|
1
|
+
# XState v6
|
|
2
|
+
|
|
3
|
+
This package contains **XState v6 alpha**. Its APIs differ from v5.
|
|
4
|
+
|
|
5
|
+
<!-- bundled documentation generated by scripts/build-package-docs.mjs from docs/meta.json -->
|
|
6
|
+
|
|
7
|
+
Before writing code with an installed package, read `docs/index.md` beside this
|
|
8
|
+
README, usually `node_modules/xstate/docs/index.md`. These bundled docs match the
|
|
9
|
+
installed package version.
|
|
10
|
+
|
|
11
|
+
For coding agents, start with `docs/agents.md`. Run `npx xstate docs` to locate
|
|
12
|
+
the installed documentation and read its machine-readable stability manifest.
|
|
13
|
+
The data-only CLI also supports `inspect`, `validate`, `simulate`, `test`,
|
|
14
|
+
`replay` and `diff`; see the agent guide for their contracts and limits.
|
|
15
|
+
|
|
16
|
+
Online, start with the [quick start](https://github.com/statelyai/xstate/blob/next/docs/quick-start.md)
|
|
17
|
+
or the [v5 to v6 migration guide](https://github.com/statelyai/xstate/blob/next/docs/xstate-v5-to-v6.md).
|
|
18
|
+
|
|
19
|
+
<!-- v6 authoring replacements from src/index.ts, src/transitionActions.ts and src/types.ts -->
|
|
20
|
+
|
|
21
|
+
| XState v5 | XState v6 |
|
|
22
|
+
| --- | --- |
|
|
23
|
+
| `assign(...)` | Return a shallow `{ context: { ... } }` patch from a transition, entry or exit function. |
|
|
24
|
+
| `fromPromise(fn)` | `createAsyncLogic({ run: fn })` from `xstate`. |
|
|
25
|
+
| `types: {} as { ... }` | `schemas` with `types<T>()` for type-only schemas or Standard Schema for runtime schemas. |
|
|
26
|
+
| `{ target, guard, actions }` | A transition function that checks its condition and returns `{ target, context }`; queue effects with `enq`. |
|
|
27
|
+
|
|
28
|
+
<!-- quick start using public createMachine and createActor exports from src/index.ts -->
|
|
29
|
+
|
|
30
|
+
## Quick start
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install xstate@beta
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import { createMachine, createActor } from 'xstate';
|
|
38
|
+
|
|
39
|
+
// State machine
|
|
40
|
+
const toggleMachine = createMachine({
|
|
41
|
+
id: 'toggle',
|
|
42
|
+
initial: 'inactive',
|
|
43
|
+
context: {
|
|
44
|
+
count: 0
|
|
45
|
+
},
|
|
46
|
+
states: {
|
|
47
|
+
inactive: {
|
|
48
|
+
on: {
|
|
49
|
+
TOGGLE: { target: 'active' }
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
active: {
|
|
53
|
+
entry: ({ context }) => ({
|
|
54
|
+
context: { count: context.count + 1 }
|
|
55
|
+
}),
|
|
56
|
+
on: {
|
|
57
|
+
TOGGLE: { target: 'inactive' }
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// Actor (instance of the machine logic, like a store)
|
|
64
|
+
const toggleActor = createActor(toggleMachine);
|
|
65
|
+
toggleActor.subscribe((state) => console.log(state.value, state.context));
|
|
66
|
+
toggleActor.start();
|
|
67
|
+
// => logs 'inactive', { count: 0 }
|
|
68
|
+
|
|
69
|
+
toggleActor.send({ type: 'TOGGLE' });
|
|
70
|
+
// => logs 'active', { count: 1 }
|
|
71
|
+
|
|
72
|
+
toggleActor.send({ type: 'TOGGLE' });
|
|
73
|
+
// => logs 'inactive', { count: 1 }
|
|
74
|
+
```
|
|
75
|
+
|
|
1
76
|
<p align="center">
|
|
2
77
|
<br />
|
|
3
78
|
|
|
@@ -6,20 +81,120 @@
|
|
|
6
81
|
<img alt="XState logotype" src="https://raw.githubusercontent.com/statelyai/public-assets/main/logos/xstate-logo-black-nobg.svg" width="200">
|
|
7
82
|
</picture>
|
|
8
83
|
<br />
|
|
9
|
-
<strong>Actor-based state management & orchestration for complex app logic.</strong> <a href="https://
|
|
84
|
+
<strong>Actor-based state management & orchestration for complex app logic.</strong> <a href="https://github.com/statelyai/xstate/tree/next/docs">→ Documentation</a>
|
|
10
85
|
<br />
|
|
11
86
|
<br />
|
|
12
87
|
</p>
|
|
13
88
|
|
|
14
|
-
|
|
89
|
+
<!-- runtime dependencies and public entry points from package.json -->
|
|
90
|
+
|
|
91
|
+
XState is a state management and orchestration solution for JavaScript and TypeScript apps. The `xstate` package has _zero_ runtime dependencies. XState is useful for frontend and backend application logic.
|
|
15
92
|
|
|
16
93
|
It uses event-driven programming, state machines, statecharts, and the actor model to handle complex logic in predictable, robust, and visual ways. XState provides a powerful and flexible way to manage application and workflow state by allowing developers to model logic as actors and state machines.
|
|
17
94
|
|
|
95
|
+
<!-- system-wide emission observation from packages/core/src/setup.ts and packages/core/src/system.ts -->
|
|
96
|
+
|
|
97
|
+
Observe emitted events from root and child actors with `createSystem({ onEmitted })`:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { createMachine, createSystem } from 'xstate';
|
|
101
|
+
|
|
102
|
+
const machine = createMachine({
|
|
103
|
+
entry: (_, enq) => enq.emit({ type: 'started' })
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const system = createSystem({
|
|
107
|
+
onEmitted: ({ actorRef, event }) => {
|
|
108
|
+
console.log(actorRef.id, event);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const root = system.createActor(machine).start();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Use `system.onEmitted(listener)` for a removable subscription. `actor.on(...)`
|
|
116
|
+
observes only that actor. See [actor systems](https://github.com/statelyai/xstate/blob/next/docs/systems.md#observe-emitted-events-across-a-system).
|
|
117
|
+
|
|
118
|
+
<!-- experimental whole-system transitions from packages/core/src/systemTransition.ts -->
|
|
119
|
+
|
|
120
|
+
For pure actor-system simulation, `initialSystemTransition`, `systemTransition`
|
|
121
|
+
and `advanceSystemTime` return an immutable system snapshot and external effects
|
|
122
|
+
as data. Branch or replay actors, messages and virtual timers without starting live
|
|
123
|
+
actors. See [pure system transitions](https://github.com/statelyai/xstate/blob/next/docs/system-transitions.md).
|
|
124
|
+
|
|
125
|
+
<!-- snapshot restoration from packages/core/src/persist/index.ts -->
|
|
126
|
+
|
|
127
|
+
The `xstate/persist` entry point provides `restoreActor(logic, persistedSnapshot, options?)`
|
|
128
|
+
to resume any actor from a persisted snapshot, and `snapshotSchema(machine)` for the
|
|
129
|
+
persisted snapshot contract. See [persistence](https://github.com/statelyai/xstate/blob/next/docs/persistence.md).
|
|
130
|
+
|
|
131
|
+
<!-- durable execution and checkpoint restoration from packages/core/src/durable/index.ts -->
|
|
132
|
+
|
|
133
|
+
For durable hosts, the experimental `xstate/durable` entry point provides
|
|
134
|
+
`createDurable`. Resume a checkpoint with `execution.restore(persistedSnapshot)`
|
|
135
|
+
and execute the returned effects to restart active embedded children and pending
|
|
136
|
+
timers without replaying entry actions. Await `executeEffects()` before persisting
|
|
137
|
+
to retain accepted timer deadlines. The host handles root error snapshots;
|
|
138
|
+
`run()` rejects with the root error. See [durable execution](https://github.com/statelyai/xstate/blob/next/docs/durable-execution.md).
|
|
139
|
+
|
|
140
|
+
<!-- state context refinements from packages/core/src/setup.ts and packages/core/src/State.ts -->
|
|
141
|
+
|
|
142
|
+
For [typestates](https://github.com/statelyai/xstate/blob/next/docs/typestates.md), declare state-level `schemas.context`
|
|
143
|
+
in `setup({ states })`. State functions receive narrowed context, transitions
|
|
144
|
+
check the target context requirements, and `snapshot.matches(...)` narrows
|
|
145
|
+
context when reading snapshots.
|
|
146
|
+
|
|
147
|
+
<!-- typed inline invocation from packages/core/src/setup.ts -->
|
|
148
|
+
|
|
149
|
+
Use `s.createInvoke({ src, input, onDone })` inline in `s.createMachine(...)` for an actor used in one state. It infers the actor's input and output and the enclosing state's narrowed context without registering that actor in `setup.actors`. See [typed inline invokes](https://github.com/statelyai/xstate/blob/next/docs/invoke.md#typed-inline-invokes).
|
|
150
|
+
|
|
151
|
+
Pass an async function directly as `src`, with `schemas.input`, `schemas.output`, and `schemas.error` on the same invoke. Output also infers from the async return when its schema is omitted. Existing logic values use their own schemas.
|
|
152
|
+
|
|
153
|
+
The helper requires input for actors whose input excludes `undefined`, and checks child IDs and source compatibility against `schemas.children` declared in the setup or machine.
|
|
154
|
+
|
|
155
|
+
<!-- graph traversal events from packages/core/src/graph/index.ts -->
|
|
156
|
+
|
|
157
|
+
Graph traversal requires samples for known event types with required payloads. Pass an `events` map such as `{ ADD: [{ value: 1 }, { value: 0 }] }`; each entry also accepts a snapshot callback returning nonempty payload cases. Maps preserve payload-free defaults, invoke completions, errors, and delayed events. Event arrays and callbacks returning events replace all defaults. Coverage remains sample-based. See [model-based testing](https://github.com/statelyai/xstate/blob/next/docs/model-based-testing.md).
|
|
158
|
+
|
|
159
|
+
<!-- JSON loading and runtime validation from packages/core/src/machineFromJSON.ts -->
|
|
160
|
+
|
|
161
|
+
Revive JSON definitions with `machineFromJSON(json, { schemas, validator, ...sources })` to restore runtime validation. The loader validates config keys and references. See [serialization](https://github.com/statelyai/xstate/blob/next/docs/serialization.md).
|
|
162
|
+
|
|
163
|
+
For flat finite state machines, `xstate/fsm` provides a tiny pure transition
|
|
164
|
+
table with context and TypeScript support:
|
|
165
|
+
|
|
166
|
+
<!-- public exports from packages/core/src/fsm/index.ts; exact capabilities documented in docs/fsm.md -->
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
import { createFSM } from 'xstate/fsm';
|
|
170
|
+
|
|
171
|
+
const machine = createFSM({
|
|
172
|
+
initial: 'inactive',
|
|
173
|
+
states: {
|
|
174
|
+
inactive: { on: { toggle: 'active' } },
|
|
175
|
+
active: { on: { toggle: 'inactive' } }
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
let state = machine.initialState;
|
|
180
|
+
[state] = machine.transition(state, { type: 'toggle' });
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
The FSM is also actor logic, so `createActor(machine)` from `xstate` runs it.
|
|
184
|
+
|
|
185
|
+
For typed context, event payloads, or state-specific context snapshots, use
|
|
186
|
+
`setup` and `types` from `xstate/fsm`. These schemas are type-only in this
|
|
187
|
+
subpath; use root `xstate` for runtime schema validation.
|
|
188
|
+
|
|
189
|
+
See the [exact supported surface](https://stately.ai/docs/fsm). Use the root `xstate`
|
|
190
|
+
entry point for compound or parallel statecharts, invocation, named sources,
|
|
191
|
+
runtime schemas, serialization and inspection.
|
|
192
|
+
|
|
18
193
|
### ✨ Create state machines visually in Stately Studio → [state.new](https://state.new)
|
|
19
194
|
|
|
20
195
|
---
|
|
21
196
|
|
|
22
|
-
📖 [Read the documentation](https://
|
|
197
|
+
📖 [Read the documentation](https://github.com/statelyai/xstate/tree/next/docs)
|
|
23
198
|
|
|
24
199
|
➡️ [Create state machines with the Stately Editor](https://stately.ai/editor)
|
|
25
200
|
|
|
@@ -27,9 +202,13 @@ It uses event-driven programming, state machines, statecharts, and the actor mod
|
|
|
27
202
|
|
|
28
203
|
📑 Inspired by the [SCXML specification](https://www.w3.org/TR/scxml/)
|
|
29
204
|
|
|
205
|
+
<!-- public SCXML entry point from packages/xstate-scxml/src/index.ts -->
|
|
206
|
+
|
|
207
|
+
Create machines from SCXML documents with the separate [`@xstate/scxml`](https://www.npmjs.com/package/@xstate/scxml) package. See the [SCXML guide](https://github.com/statelyai/xstate/blob/next/docs/scxml.md).
|
|
208
|
+
|
|
30
209
|
💬 Chat on the [Stately Discord Community](https://discord.gg/xstate)
|
|
31
210
|
|
|
32
|
-
✍️ Browse through the many [XState examples](https://github.com/statelyai/xstate/tree/
|
|
211
|
+
✍️ Browse through the many [XState examples](https://github.com/statelyai/xstate/tree/next/examples)
|
|
33
212
|
|
|
34
213
|
## Templates
|
|
35
214
|
|
|
@@ -44,14 +223,14 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
44
223
|
<tr>
|
|
45
224
|
<td>
|
|
46
225
|
|
|
47
|
-
[🤖 XState Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/
|
|
226
|
+
[🤖 XState Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/next/templates/vanilla-ts)
|
|
48
227
|
|
|
49
|
-
[](https://stackblitz.com/github/statelyai/xstate/tree/
|
|
228
|
+
[](https://stackblitz.com/github/statelyai/xstate/tree/next/templates/vanilla-ts?file=%2Fsrc%2FfeedbackMachine.ts)
|
|
50
229
|
|
|
51
230
|
</td>
|
|
52
231
|
<td>
|
|
53
232
|
|
|
54
|
-
- XState
|
|
233
|
+
- XState v6 alpha
|
|
55
234
|
- TypeScript
|
|
56
235
|
- _No framework_
|
|
57
236
|
|
|
@@ -61,15 +240,15 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
61
240
|
<tr>
|
|
62
241
|
<td>
|
|
63
242
|
|
|
64
|
-
[⚛️ XState + React Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/
|
|
243
|
+
[⚛️ XState + React Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/next/templates/react-ts)
|
|
65
244
|
|
|
66
|
-
[](https://stackblitz.com/github/statelyai/xstate/tree/
|
|
245
|
+
[](https://stackblitz.com/github/statelyai/xstate/tree/next/templates/react-ts?file=%2Fsrc%2FfeedbackMachine.ts)
|
|
67
246
|
|
|
68
247
|
</td>
|
|
69
248
|
<td>
|
|
70
249
|
|
|
71
250
|
- [React](https://react.dev/)
|
|
72
|
-
- XState
|
|
251
|
+
- XState v6 alpha
|
|
73
252
|
- TypeScript
|
|
74
253
|
|
|
75
254
|
</td>
|
|
@@ -78,15 +257,15 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
78
257
|
<tr>
|
|
79
258
|
<td>
|
|
80
259
|
|
|
81
|
-
[💚 XState + Vue Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/
|
|
260
|
+
[💚 XState + Vue Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/next/templates/vue-ts)
|
|
82
261
|
|
|
83
|
-
[](https://stackblitz.com/github/statelyai/xstate/tree/
|
|
262
|
+
[](https://stackblitz.com/github/statelyai/xstate/tree/next/templates/vue-ts?file=%2Fsrc%2FfeedbackMachine.ts)
|
|
84
263
|
|
|
85
264
|
</td>
|
|
86
265
|
<td>
|
|
87
266
|
|
|
88
267
|
- [Vue](https://vuejs.org/)
|
|
89
|
-
- XState
|
|
268
|
+
- XState v6 alpha
|
|
90
269
|
- TypeScript
|
|
91
270
|
|
|
92
271
|
</td>
|
|
@@ -95,15 +274,15 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
95
274
|
<tr>
|
|
96
275
|
<td>
|
|
97
276
|
|
|
98
|
-
[🧡 XState + Svelte Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/
|
|
277
|
+
[🧡 XState + Svelte Template (CodeSandbox)](https://codesandbox.io/p/devbox/github/statelyai/xstate/tree/next/templates/svelte-ts)
|
|
99
278
|
|
|
100
|
-
[](https://stackblitz.com/github/statelyai/xstate/tree/
|
|
279
|
+
[](https://stackblitz.com/github/statelyai/xstate/tree/next/templates/svelte-ts?file=%2Fsrc%2FfeedbackMachine.ts)
|
|
101
280
|
|
|
102
281
|
</td>
|
|
103
282
|
<td>
|
|
104
283
|
|
|
105
284
|
- [Svelte](https://svelte.dev/)
|
|
106
|
-
- XState
|
|
285
|
+
- XState v6 alpha
|
|
107
286
|
- TypeScript
|
|
108
287
|
|
|
109
288
|
</td>
|
|
@@ -112,52 +291,6 @@ Get started by forking one of these templates on CodeSandbox:
|
|
|
112
291
|
</tbody>
|
|
113
292
|
</table>
|
|
114
293
|
|
|
115
|
-
## Super quick start
|
|
116
|
-
|
|
117
|
-
```bash
|
|
118
|
-
npm install xstate
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
```ts
|
|
122
|
-
import { createMachine, createActor } from 'xstate';
|
|
123
|
-
|
|
124
|
-
// State machine
|
|
125
|
-
const toggleMachine = createMachine({
|
|
126
|
-
id: 'toggle',
|
|
127
|
-
initial: 'inactive',
|
|
128
|
-
context: {
|
|
129
|
-
count: 0
|
|
130
|
-
},
|
|
131
|
-
states: {
|
|
132
|
-
inactive: {
|
|
133
|
-
on: {
|
|
134
|
-
TOGGLE: { target: 'active' }
|
|
135
|
-
}
|
|
136
|
-
},
|
|
137
|
-
active: {
|
|
138
|
-
entry: ({ context }) => ({
|
|
139
|
-
context: { count: context.count + 1 }
|
|
140
|
-
}),
|
|
141
|
-
on: {
|
|
142
|
-
TOGGLE: { target: 'inactive' }
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
// Actor (instance of the machine logic, like a store)
|
|
149
|
-
const toggleActor = createActor(toggleMachine);
|
|
150
|
-
toggleActor.subscribe((state) => console.log(state.value, state.context));
|
|
151
|
-
toggleActor.start();
|
|
152
|
-
// => logs 'inactive', { count: 0 }
|
|
153
|
-
|
|
154
|
-
toggleActor.send({ type: 'TOGGLE' });
|
|
155
|
-
// => logs 'active', { count: 1 }
|
|
156
|
-
|
|
157
|
-
toggleActor.send({ type: 'TOGGLE' });
|
|
158
|
-
// => logs 'inactive', { count: 1 }
|
|
159
|
-
```
|
|
160
|
-
|
|
161
294
|
## [Stately Studio](https://stately.ai)
|
|
162
295
|
|
|
163
296
|
- Visually create, edit, and collaborate on state machines
|
|
@@ -166,7 +299,7 @@ toggleActor.send({ type: 'TOGGLE' });
|
|
|
166
299
|
- Deploy to Stately Sky
|
|
167
300
|
- Generate & modify machines with Stately AI
|
|
168
301
|
|
|
169
|
-
<a href="stately.ai/registry/new?ref=github" title="Stately Studio">
|
|
302
|
+
<a href="https://stately.ai/registry/new?ref=github" title="Stately Studio">
|
|
170
303
|
<img src="https://github.com/statelyai/xstate/assets/1093738/74ed9cbc-b824-4ed7-a16d-f104947af8a7" alt="XState Viz" width="800" />
|
|
171
304
|
</a>
|
|
172
305
|
|
|
@@ -186,10 +319,10 @@ Read [📽 the slides](http://slides.com/davidkhourshid/finite-state-machines) (
|
|
|
186
319
|
| Package | Description |
|
|
187
320
|
| ------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
|
|
188
321
|
| 🤖 `xstate` | Core finite state machine and statecharts library + interpreter |
|
|
189
|
-
| [⚛️ `@xstate/react`](https://github.com/statelyai/xstate/tree/
|
|
190
|
-
| [💚 `@xstate/vue`](https://github.com/statelyai/xstate/tree/
|
|
191
|
-
| [🎷 `@xstate/svelte`](https://github.com/statelyai/xstate/tree/
|
|
192
|
-
| [🥏 `@xstate/solid`](https://github.com/statelyai/xstate/tree/
|
|
322
|
+
| [⚛️ `@xstate/react`](https://github.com/statelyai/xstate/tree/next/packages/xstate-react) | React hooks and utilities for using XState in React applications |
|
|
323
|
+
| [💚 `@xstate/vue`](https://github.com/statelyai/xstate/tree/next/packages/xstate-vue) | Vue composition functions and utilities for using XState in Vue applications |
|
|
324
|
+
| [🎷 `@xstate/svelte`](https://github.com/statelyai/xstate/tree/next/packages/xstate-svelte) | Svelte utilities for using XState in Svelte applications |
|
|
325
|
+
| [🥏 `@xstate/solid`](https://github.com/statelyai/xstate/tree/next/packages/xstate-solid) | Solid hooks and utilities for using XState in Solid applications |
|
|
193
326
|
|
|
194
327
|
## Finite State Machines
|
|
195
328
|
|
|
@@ -208,17 +341,17 @@ const lightMachine = createMachine({
|
|
|
208
341
|
states: {
|
|
209
342
|
green: {
|
|
210
343
|
on: {
|
|
211
|
-
TIMER: 'yellow'
|
|
344
|
+
TIMER: { target: 'yellow' }
|
|
212
345
|
}
|
|
213
346
|
},
|
|
214
347
|
yellow: {
|
|
215
348
|
on: {
|
|
216
|
-
TIMER: 'red'
|
|
349
|
+
TIMER: { target: 'red' }
|
|
217
350
|
}
|
|
218
351
|
},
|
|
219
352
|
red: {
|
|
220
353
|
on: {
|
|
221
|
-
TIMER: 'green'
|
|
354
|
+
TIMER: { target: 'green' }
|
|
222
355
|
}
|
|
223
356
|
}
|
|
224
357
|
}
|
|
@@ -281,12 +414,12 @@ const pedestrianStates = {
|
|
|
281
414
|
states: {
|
|
282
415
|
walk: {
|
|
283
416
|
on: {
|
|
284
|
-
PED_TIMER: 'wait'
|
|
417
|
+
PED_TIMER: { target: 'wait' }
|
|
285
418
|
}
|
|
286
419
|
},
|
|
287
420
|
wait: {
|
|
288
421
|
on: {
|
|
289
|
-
PED_TIMER: 'stop'
|
|
422
|
+
PED_TIMER: { target: 'stop' }
|
|
290
423
|
}
|
|
291
424
|
},
|
|
292
425
|
stop: {}
|
|
@@ -299,17 +432,17 @@ const lightMachine = createMachine({
|
|
|
299
432
|
states: {
|
|
300
433
|
green: {
|
|
301
434
|
on: {
|
|
302
|
-
TIMER: 'yellow'
|
|
435
|
+
TIMER: { target: 'yellow' }
|
|
303
436
|
}
|
|
304
437
|
},
|
|
305
438
|
yellow: {
|
|
306
439
|
on: {
|
|
307
|
-
TIMER: 'red'
|
|
440
|
+
TIMER: { target: 'red' }
|
|
308
441
|
}
|
|
309
442
|
},
|
|
310
443
|
red: {
|
|
311
444
|
on: {
|
|
312
|
-
TIMER: 'green'
|
|
445
|
+
TIMER: { target: 'green' }
|
|
313
446
|
},
|
|
314
447
|
...pedestrianStates
|
|
315
448
|
}
|
|
@@ -485,15 +618,15 @@ const paymentMachine = createMachine({
|
|
|
485
618
|
states: {
|
|
486
619
|
cash: {
|
|
487
620
|
on: {
|
|
488
|
-
SWITCH_CHECK: 'check'
|
|
621
|
+
SWITCH_CHECK: { target: 'check' }
|
|
489
622
|
}
|
|
490
623
|
},
|
|
491
624
|
check: {
|
|
492
625
|
on: {
|
|
493
|
-
SWITCH_CASH: 'cash'
|
|
626
|
+
SWITCH_CASH: { target: 'cash' }
|
|
494
627
|
}
|
|
495
628
|
},
|
|
496
|
-
hist: { type: 'history' }
|
|
629
|
+
hist: { type: 'history', target: 'cash' }
|
|
497
630
|
},
|
|
498
631
|
on: { NEXT: { target: 'review' } }
|
|
499
632
|
},
|
|
@@ -575,3 +708,15 @@ For these reasons, it is impractical for our team to be bound by decisions taken
|
|
|
575
708
|
### Packages
|
|
576
709
|
|
|
577
710
|
Most of the packages in the XState family declare a peer dependency on XState itself. We’ll be cautious about maintaining compatibility with already-released packages when releasing a new version of XState, **but** each release of packages depending on XState will always adjust the declared peer dependency range to include the latest version of XState. For example, you should always be able to update `xstate` without `@xstate/react`. But when you update `@xstate/react`, we highly recommend updating `xstate` too.
|
|
711
|
+
|
|
712
|
+
<!-- public input validation contract from src/StateMachine.ts -->
|
|
713
|
+
Validate external input with `eventSchema(machine)` before `actor.send()`.
|
|
714
|
+
It validates complete public events, excludes internal/runtime events, and allows
|
|
715
|
+
explicitly configured routes. Payload checks require runtime schemas;
|
|
716
|
+
`types<T>()` supplies types only. See [public input validation](https://github.com/statelyai/xstate/blob/next/docs/internal-events.md#validating-public-input).
|
|
717
|
+
|
|
718
|
+
<!-- entry effects and terminal transition behavior from packages/core/src/stateUtils.ts and transitionActions.ts -->
|
|
719
|
+
|
|
720
|
+
Use the `enq` argument in transition, entry and exit functions to queue effects. Return context or child patches from those functions; the return value of an enqueued effect is ignored, with a development warning. See [actions](https://github.com/statelyai/xstate/blob/next/docs/actions.md).
|
|
721
|
+
|
|
722
|
+
For actors that are done, stopped or failed, pure transitions return the same snapshot and no effects, and `snapshot.can(...)` returns `false`. See [snapshots](https://github.com/statelyai/xstate/blob/next/docs/snapshots.md) and [states](https://github.com/statelyai/xstate/blob/next/docs/states.md).
|