@typeonce/effect-machine 0.34.0 → 0.35.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.
- package/README.md +93 -29
- package/dist/Machine.d.ts +309 -361
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +126 -87
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +0 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +0 -19
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +12 -20
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/initialDeclaration.d.ts +14 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
- package/dist/internal/machine/initialDeclaration.js +137 -0
- package/dist/internal/machine/initialDeclaration.js.map +1 -0
- package/dist/internal/machine/machine.d.ts +0 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +39 -120
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +8 -0
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +25 -67
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +1 -13
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/targetReference.d.ts.map +1 -1
- package/dist/internal/machine/targetReference.js +14 -4
- package/dist/internal/machine/targetReference.js.map +1 -1
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +3 -2
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +15 -6
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +7 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +105 -73
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +105 -73
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +13 -8
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +13 -8
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +41 -24
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/package.json +1 -1
- package/src/Machine.ts +471 -744
- package/src/internal/machine/configuration.ts +0 -20
- package/src/internal/machine/executionPlan.ts +11 -18
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/machine.ts +38 -179
- package/src/internal/machine/planner.ts +30 -83
- package/src/internal/machine/protocol.ts +3 -3
- package/src/internal/machine/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +19 -4
- package/src/internal/machine/topology.ts +3 -2
- package/src/internal/testing/machine/finiteModel.ts +16 -10
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +13 -8
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
package/README.md
CHANGED
|
@@ -47,23 +47,21 @@ Declare the root once, derive its references, then add behavior with event maps:
|
|
|
47
47
|
```ts
|
|
48
48
|
import { Machine } from "@typeonce/effect-machine"
|
|
49
49
|
import { Effect, Schema } from "effect"
|
|
50
|
-
|
|
51
50
|
const Root = Machine.state({ fields: { count: Schema.Number } })
|
|
52
51
|
const targets = Machine.targets(Root)
|
|
53
52
|
const Events = Machine.events({ Increment: { by: Schema.Number } })
|
|
54
53
|
const Counter = Machine.make({
|
|
55
54
|
root: Root,
|
|
56
|
-
events: Events
|
|
57
|
-
initial: (root) => root.from(() => ({ count: 0 }))
|
|
55
|
+
events: Events
|
|
58
56
|
}).handle({
|
|
57
|
+
root: () => ({ count: 0 }),
|
|
59
58
|
on: {
|
|
60
59
|
Increment: {
|
|
61
60
|
update: targets.root,
|
|
62
|
-
|
|
61
|
+
data: ({ root, event }) => ({ count: root.count + event.by })
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
})
|
|
66
|
-
|
|
67
65
|
const program = Effect.scoped(Effect.gen(function*() {
|
|
68
66
|
const ref = yield* Machine.start(Counter)
|
|
69
67
|
yield* ref.send(Events.Increment({ by: 1 }))
|
|
@@ -86,11 +84,63 @@ input, sources, and named branch groups. `.handle` implements behavior in one
|
|
|
86
84
|
nested object and returns an executable machine. One definition can create
|
|
87
85
|
independent implementations by calling `.handle` more than once.
|
|
88
86
|
|
|
87
|
+
### Declare startup data and initial edges
|
|
88
|
+
|
|
89
|
+
The root descriptor contains schemas and hierarchy. `make` registers protocols
|
|
90
|
+
and programs. `.handle` selects initial children and constructs their values:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
const Root = Machine.state({
|
|
94
|
+
fields: { seed: Schema.Number },
|
|
95
|
+
states: {
|
|
96
|
+
Loading: { fields: { count: Schema.Number } },
|
|
97
|
+
Ready: {}
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
const targets = Machine.targets(Root)
|
|
101
|
+
const machine = Machine.make({ root: Root, input: Schema.Number, events: Machine.events({}) }).handle({
|
|
102
|
+
root: ({ input }) => ({ seed: input }),
|
|
103
|
+
initial: { target: targets.root.Loading, data: ({ root }) => ({ count: root.seed }) }
|
|
104
|
+
})
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Only the root constructor receives startup input. Child constructors receive
|
|
108
|
+
root data, their owning state's data, ancestors, and the lifecycle event.
|
|
109
|
+
Each compound declares an initial edge targeting one direct child; that edge
|
|
110
|
+
also supplies the child's required values. Nested compounds declare their own
|
|
111
|
+
edges in their handlers. There is no separate `initialConfiguration` override.
|
|
112
|
+
|
|
113
|
+
A parallel handler's `initial` is a map of region data. It enters all regions:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
const Root = Machine.state({
|
|
117
|
+
type: "parallel",
|
|
118
|
+
states: {
|
|
119
|
+
Editor: { fields: { draft: Schema.String }, states: { Editing: {}, Saved: {} } },
|
|
120
|
+
Network: { fields: { online: Schema.Boolean } }
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
const targets = Machine.targets(Root)
|
|
124
|
+
const machine = Machine.make({ root: Root, events: Machine.events({}) }).handle({
|
|
125
|
+
initial: { Editor: { draft: "" }, Network: { online: false } },
|
|
126
|
+
states: { Editor: { initial: { target: targets.root.Editor.Editing } } }
|
|
127
|
+
})
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
Root and region data accept the short literal or callback form. For decoded
|
|
131
|
+
values, use `{ decoded: true, data: valueOrCallback }`. This descriptor accepts
|
|
132
|
+
only those two keys. If your domain data itself has `decoded: true` and `data`
|
|
133
|
+
fields, return it from a callback: callback results are always ordinary schema
|
|
134
|
+
input. Required data is checked at compile time; schema-less regions accept no data.
|
|
135
|
+
|
|
136
|
+
Use root or state `entry`/`exit` for synchronous commands and `invoke` for
|
|
137
|
+
asynchronous work. Constructors only produce values. An unhandled definition
|
|
138
|
+
cannot be started, planned, or registered as a child machine.
|
|
139
|
+
|
|
89
140
|
### Construct state values
|
|
90
141
|
|
|
91
142
|
```ts
|
|
92
143
|
const Root = Machine.state({
|
|
93
|
-
initial: "Idle",
|
|
94
144
|
states: {
|
|
95
145
|
Idle: {},
|
|
96
146
|
Loading: { fields: { query: Schema.String } },
|
|
@@ -100,26 +150,34 @@ const Root = Machine.state({
|
|
|
100
150
|
})
|
|
101
151
|
const targets = Machine.targets(Root)
|
|
102
152
|
const Events = Machine.events({ Search: { query: Schema.String }, Reset: {} })
|
|
103
|
-
|
|
104
|
-
|
|
153
|
+
const Search = Machine.make({
|
|
154
|
+
root: Root,
|
|
155
|
+
events: Events
|
|
156
|
+
}).handle({
|
|
157
|
+
initial: {
|
|
158
|
+
target: targets.root.Idle
|
|
159
|
+
},
|
|
105
160
|
states: {
|
|
106
161
|
Idle: {
|
|
107
162
|
on: {
|
|
108
163
|
Search: {
|
|
109
164
|
target: targets.root.Loading,
|
|
110
|
-
|
|
165
|
+
data: ({ event }) => ({ query: event.query })
|
|
111
166
|
}
|
|
112
167
|
}
|
|
113
168
|
},
|
|
114
|
-
Loading: { on: { Reset: { target: targets.root.Idle } } }
|
|
169
|
+
Loading: { on: { Reset: { target: targets.root.Idle } } },
|
|
170
|
+
Ready: {},
|
|
171
|
+
Failed: {}
|
|
115
172
|
}
|
|
116
173
|
})
|
|
117
174
|
```
|
|
118
175
|
|
|
119
|
-
`
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
176
|
+
`data` accepts a value or a synchronous callback returning the schema's make
|
|
177
|
+
input, including constructor defaults and transformations. Add `decoded: true`
|
|
178
|
+
when supplying the already-decoded type, including class instances. Both paths
|
|
179
|
+
validate values and report `MachineSchemaDecodeError`. Omit data only when the
|
|
180
|
+
selected state supports empty construction. Schema-less states do not accept data.
|
|
123
181
|
|
|
124
182
|
A callback receives the specific event and source-state types, plus `root`,
|
|
125
183
|
`containingState`, `ancestors`, and the appropriate machine references. A
|
|
@@ -143,6 +201,7 @@ const machine = Machine.make({
|
|
|
143
201
|
}
|
|
144
202
|
}
|
|
145
203
|
}).handle({
|
|
204
|
+
initial: { target: targets.root.Idle },
|
|
146
205
|
states: {
|
|
147
206
|
Idle: {
|
|
148
207
|
on: {
|
|
@@ -164,8 +223,8 @@ payload belonging to another branch. A single-target group works the same way;
|
|
|
164
223
|
there is no second path declaration in the resolver. For a compound target,
|
|
165
224
|
`select.checkout.from(parentValues, child => child.Review.from(childValues))`
|
|
166
225
|
constructs the explicitly selected subtree. Parallel constructors require every
|
|
167
|
-
entered region.
|
|
168
|
-
part of machine readiness checking.
|
|
226
|
+
entered region. Every compound has an initial edge, including inactive branches. Final outputs,
|
|
227
|
+
history defaults, and choices remain part of machine readiness checking.
|
|
169
228
|
|
|
170
229
|
Use `guard: context => boolean` to decline before construction or commands.
|
|
171
230
|
For a resolver that can decline, explicitly add `declinable: true`; only then may
|
|
@@ -179,11 +238,11 @@ All references come from the same root descriptor supplied to `make`:
|
|
|
179
238
|
|
|
180
239
|
| Declaration | Meaning |
|
|
181
240
|
| ----------------------------------------------------- | ------------------------------------------------------------------------- |
|
|
182
|
-
| `{ target: targets.root.Checkout.Review,
|
|
183
|
-
| `{ initial: targets.root.Checkout,
|
|
241
|
+
| `{ target: targets.root.Checkout.Review, data: ... }` | Enter a declared destination with its value. |
|
|
242
|
+
| `{ initial: targets.root.Checkout, data: ... }` | Enter the declared initial configuration of a compound or parallel state. |
|
|
184
243
|
| `{ history: targets.root.Checkout.recent }` | Restore a declared history state. |
|
|
185
|
-
| `{ update: targets.root.Checkout,
|
|
186
|
-
| `{ update: targets.root,
|
|
244
|
+
| `{ update: targets.root.Checkout, data: ... }` | Replace a retained active owner's value. |
|
|
245
|
+
| `{ update: targets.root, data: ... }` | Replace root data and retain active descendants. |
|
|
187
246
|
| `{ none: true }` | Accept an event without changing the configuration. |
|
|
188
247
|
|
|
189
248
|
An atomic transition can enter a destination and update one retained owner:
|
|
@@ -192,7 +251,7 @@ An atomic transition can enter a destination and update one retained owner:
|
|
|
192
251
|
Save: {
|
|
193
252
|
target: targets.root.Checkout.Saving,
|
|
194
253
|
update: targets.root.Checkout,
|
|
195
|
-
|
|
254
|
+
data: ({ event, ancestors }) => ({
|
|
196
255
|
target: { request: event.request },
|
|
197
256
|
update: { ...ancestors.Checkout, revision: event.revision }
|
|
198
257
|
})
|
|
@@ -206,8 +265,8 @@ A retained owner must be active for that source and remain active through the
|
|
|
206
265
|
transition. A sibling region's value cannot be updated through this operation.
|
|
207
266
|
|
|
208
267
|
The runtime transition API does not replace arbitrary complete root
|
|
209
|
-
configurations.
|
|
210
|
-
|
|
268
|
+
configurations. Startup follows the initial declarations in `.handle`; history
|
|
269
|
+
defaults retain complete subtree construction for restoration.
|
|
211
270
|
|
|
212
271
|
### Protocols and ownership
|
|
213
272
|
|
|
@@ -232,7 +291,7 @@ adapter.
|
|
|
232
291
|
|
|
233
292
|
The same root supports compound and parallel states, eventless `always`
|
|
234
293
|
transitions, ordered named choices, final states, `onDone` completion,
|
|
235
|
-
|
|
294
|
+
initial declarations, shallow and deep history, and retained snapshots. The topology
|
|
236
295
|
is captured directly from declarations; visualization and branch coverage do
|
|
237
296
|
not execute user resolvers or depend on TypeScript source extraction.
|
|
238
297
|
|
|
@@ -257,15 +316,21 @@ const Search = Machine.make({
|
|
|
257
316
|
logic: { worker: workerLogic },
|
|
258
317
|
children: { validation: ValidationChild }
|
|
259
318
|
}).handle({
|
|
319
|
+
initial: {
|
|
320
|
+
target: Machine.targets(Root).root.Idle
|
|
321
|
+
},
|
|
260
322
|
states: {
|
|
323
|
+
Idle: {},
|
|
261
324
|
Loading: {
|
|
262
325
|
invoke: {
|
|
263
326
|
src: "load",
|
|
264
327
|
input: ({ state }) => state.query,
|
|
265
|
-
onDone: { target: targets.root.Ready,
|
|
266
|
-
onFailure: { target: targets.root.Failed,
|
|
328
|
+
onDone: { target: targets.root.Ready, data: ({ output }) => ({ items: output }) },
|
|
329
|
+
onFailure: { target: targets.root.Failed, data: ({ error }) => ({ message: error.message }) }
|
|
267
330
|
}
|
|
268
|
-
}
|
|
331
|
+
},
|
|
332
|
+
Ready: {},
|
|
333
|
+
Failed: {}
|
|
269
334
|
}
|
|
270
335
|
})
|
|
271
336
|
```
|
|
@@ -290,8 +355,7 @@ and cancellation boundaries; a typed `onFailure` handles the source's declared
|
|
|
290
355
|
runtime error channel.
|
|
291
356
|
|
|
292
357
|
```ts
|
|
293
|
-
|
|
294
|
-
;[
|
|
358
|
+
const invocations = [
|
|
295
359
|
{
|
|
296
360
|
src: "updates",
|
|
297
361
|
onElement: {
|