@typeonce/effect-machine 0.35.0 → 0.36.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 +8 -7
- package/dist/Machine.d.ts +113 -136
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/command.d.ts +0 -1
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/command.js +0 -14
- package/dist/internal/machine/command.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +15 -2
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/construction.d.ts +9 -0
- package/dist/internal/machine/construction.d.ts.map +1 -0
- package/dist/internal/machine/construction.js +132 -0
- package/dist/internal/machine/construction.js.map +1 -0
- package/dist/internal/machine/declaration.d.ts +1 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +3 -8
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +9 -7
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +2 -3
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/implementation.js.map +1 -1
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +68 -168
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +63 -17
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/topology.d.ts +6 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- 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 +36 -76
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/docs/agent-guide.md +37 -34
- package/docs/root-api.md +143 -190
- package/package.json +1 -1
- package/src/Machine.ts +176 -144
- package/src/internal/machine/command.ts +0 -16
- package/src/internal/machine/configuration.ts +13 -10
- package/src/internal/machine/construction.ts +147 -0
- package/src/internal/machine/declaration.ts +4 -7
- package/src/internal/machine/executionPlan.ts +9 -7
- package/src/internal/machine/implementation.ts +2 -3
- package/src/internal/machine/machine.ts +86 -223
- package/src/internal/machine/planner.ts +69 -18
- package/src/internal/machine/topology.ts +4 -0
- package/src/internal/testing/machine/finiteModel.ts +44 -122
- package/dist/internal/machine/targetBuilder.d.ts +0 -16
- package/dist/internal/machine/targetBuilder.d.ts.map +0 -1
- package/dist/internal/machine/targetBuilder.js +0 -333
- package/dist/internal/machine/targetBuilder.js.map +0 -1
- package/src/internal/machine/targetBuilder.ts +0 -525
package/docs/root-api.md
CHANGED
|
@@ -1,259 +1,212 @@
|
|
|
1
1
|
# Root machine API
|
|
2
2
|
|
|
3
|
-
A machine
|
|
4
|
-
|
|
3
|
+
A machine has one root state. It can own data, child topology, both, or neither.
|
|
4
|
+
`Machine.state` declares that structure; `Machine.make` declares protocols and
|
|
5
|
+
reusable sources; `.handle` supplies initialization and behavior.
|
|
5
6
|
|
|
6
|
-
##
|
|
7
|
+
## Startup input and state data
|
|
7
8
|
|
|
8
9
|
```ts
|
|
9
10
|
import { Machine } from "@typeonce/effect-machine"
|
|
10
11
|
import { Schema } from "effect"
|
|
11
12
|
|
|
12
|
-
const Events = Machine.events({ Open: {}, Close: {} })
|
|
13
13
|
const Root = Machine.state({
|
|
14
|
-
|
|
15
|
-
states: { Closed: {}, Open: {} }
|
|
16
|
-
})
|
|
17
|
-
const targets = Machine.targets(Root)
|
|
18
|
-
const Door = Machine.make({ root: Root, events: Events }).handle({
|
|
19
|
-
states: {
|
|
20
|
-
Closed: { on: { Open: { target: targets.root.Open } } },
|
|
21
|
-
Open: { on: { Close: { target: targets.root.Closed } } }
|
|
22
|
-
}
|
|
23
|
-
})
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
The root declares the default child once. No startup target, empty schema, or
|
|
27
|
-
resolver is needed. Structural nodes have `value: undefined` in snapshots.
|
|
28
|
-
|
|
29
|
-
## Root data and child data
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
const Root = Machine.state({
|
|
33
|
-
fields: { title: Schema.String },
|
|
34
|
-
initial: "Editing",
|
|
14
|
+
fields: { locale: Schema.String },
|
|
35
15
|
states: {
|
|
36
|
-
|
|
37
|
-
|
|
16
|
+
Loading: { fields: { documentId: Schema.String } },
|
|
17
|
+
Ready: {}
|
|
38
18
|
}
|
|
39
19
|
})
|
|
40
|
-
const Events = Machine.events({ Rename: { title: Schema.String }, Save: {} })
|
|
41
20
|
const targets = Machine.targets(Root)
|
|
42
|
-
const
|
|
21
|
+
const Events = Machine.events({
|
|
22
|
+
Loaded: {},
|
|
23
|
+
Reload: { locale: Schema.String, documentId: Schema.String }
|
|
24
|
+
})
|
|
25
|
+
const Document = Machine.make({
|
|
43
26
|
root: Root,
|
|
44
27
|
events: Events,
|
|
45
|
-
|
|
28
|
+
input: Schema.Struct({ locale: Schema.String, documentId: Schema.String })
|
|
46
29
|
}).handle({
|
|
47
|
-
|
|
30
|
+
root: ({ input }) => ({ locale: input.locale }),
|
|
31
|
+
initial: {
|
|
32
|
+
target: targets.root.Loading,
|
|
33
|
+
data: ({ input }) => ({ documentId: input.documentId })
|
|
34
|
+
},
|
|
48
35
|
on: {
|
|
49
|
-
|
|
36
|
+
Reload: {
|
|
37
|
+
target: targets.root,
|
|
38
|
+
input: ({ event }) => ({ locale: event.locale, documentId: event.documentId }),
|
|
39
|
+
reenter: true
|
|
40
|
+
}
|
|
50
41
|
},
|
|
51
42
|
states: {
|
|
52
|
-
|
|
43
|
+
Loading: { on: { Loaded: { target: targets.root.Ready } } }
|
|
53
44
|
}
|
|
54
45
|
})
|
|
55
46
|
```
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
input
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
The complete snapshot always includes the root:
|
|
63
|
-
|
|
64
|
-
```ts
|
|
65
|
-
// While Editing is active:
|
|
66
|
-
{
|
|
67
|
-
path: "",
|
|
68
|
-
value: { _tag: "", title: "Untitled" },
|
|
69
|
-
state: {
|
|
70
|
-
path: "Editing",
|
|
71
|
-
value: { _tag: "Editing", draft: "Untitled" }
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
Root handlers remain active across child transitions. Child handlers receive
|
|
77
|
-
their own state, typed ancestors, `root`, and the complete snapshot.
|
|
78
|
-
`update: targets.root` addresses root data from a child. An explicit descendant
|
|
79
|
-
reference addresses a valued handler owner or retained ancestor.
|
|
80
|
-
Both replace the entire value; omitted fields are not silently retained.
|
|
81
|
-
Updates preserve the active topology and do not restart scoped work.
|
|
82
|
-
|
|
83
|
-
Simultaneous transitions retain conflict checking. Two parallel handlers cannot
|
|
84
|
-
silently overwrite the same owner. Use `{ target, update }` to combine a selected destination and a retained
|
|
85
|
-
owner's value in one atomic transition.
|
|
48
|
+
Start with `Machine.start(Document, { locale: "en", documentId: "intro" })`.
|
|
49
|
+
Root construction runs before its initial child construction. Both receive the
|
|
50
|
+
same validated input. `documentId` belongs only to Loading; it need not be stored
|
|
51
|
+
in root data. Input is not retained as a reset value on the machine or snapshot.
|
|
86
52
|
|
|
87
|
-
|
|
53
|
+
Only the root's initial callbacks receive machine input. Nested initial callbacks
|
|
54
|
+
receive their owning state, ancestors, root, and lifecycle event. Pass any needed
|
|
55
|
+
values through the nested state's data. Input is not an entry/exit capability.
|
|
88
56
|
|
|
89
|
-
|
|
90
|
-
mutually exclusive. A schema preserves class identity, refinements, defaults,
|
|
91
|
-
and transformations; its tag is independent of its mount path.
|
|
57
|
+
For a parallel root, every required region constructor receives input:
|
|
92
58
|
|
|
93
59
|
```ts
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
}) {}
|
|
97
|
-
|
|
98
|
-
const Form = Machine.state({
|
|
99
|
-
initial: "Idle",
|
|
100
|
-
states: { Idle: {}, Ready: { schema: Ready } }
|
|
101
|
-
})
|
|
102
|
-
const Root = Machine.state({
|
|
103
|
-
type: "parallel",
|
|
104
|
-
states: { First: Form, Second: Form }
|
|
105
|
-
})
|
|
60
|
+
initial: {
|
|
61
|
+
Documents: ({ input }) => ({ folder: input.folder }),
|
|
62
|
+
Connection: ({ input }) => ({ endpoint: input.endpoint })
|
|
63
|
+
}
|
|
106
64
|
```
|
|
107
65
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
`Machine.events({ Save: { title: Schema.String } })` creates a deferred
|
|
113
|
-
`Events.Save({ title })` constructor. The machine validates it when processing
|
|
114
|
-
the event. To reuse tagged classes or a tagged union, use
|
|
115
|
-
`Machine.eventsFromSchemas(...)`. Internal and emitted protocols have distinct
|
|
116
|
-
constructors with the same fields/import distinction. Public sends cannot send
|
|
117
|
-
internal events, and emissions do not become machine input events implicitly.
|
|
66
|
+
Structural regions require no data. Schema constructor defaults can supply
|
|
67
|
+
optional region data. Compound handlers declare one direct initial child with
|
|
68
|
+
`initial: { target, data }`; each compound has its own initial declaration.
|
|
69
|
+
There is no `initialConfiguration` or initializer in `make`.
|
|
118
70
|
|
|
119
|
-
##
|
|
71
|
+
## Root targets and updates
|
|
120
72
|
|
|
121
|
-
`
|
|
122
|
-
|
|
123
|
-
|
|
73
|
+
`{ target: targets.root, input: ... }` reconstructs root data and follows its
|
|
74
|
+
initial declarations using fresh input. Input is required exactly when it is
|
|
75
|
+
required by `Machine.start`. Machines without input omit it. Root targets reject
|
|
76
|
+
`data`, `decoded`, and explicit child construction.
|
|
124
77
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
events: Events,
|
|
132
|
-
initialConfiguration: (root) => root.resolve(({ target }) =>
|
|
133
|
-
target.from((states) => states.Open.from())
|
|
134
|
-
)
|
|
135
|
-
}).handle({})
|
|
136
|
-
```
|
|
78
|
+
Targeting root retains its lifecycle unless it is reentered. Put a machine-wide
|
|
79
|
+
reset handler at root and use `reenter: true` to exit and enter root as well as
|
|
80
|
+
its descendants. Reentry restarts work owned by the exited states. Without
|
|
81
|
+
reentry, retained root work continues; it does not automatically acquire new
|
|
82
|
+
construction values. Ordinary transition conflict and lifecycle rules still apply.
|
|
83
|
+
This does not create a new machine reference or discard history records.
|
|
137
84
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
a validated snapshot, including its completion and history metadata.
|
|
85
|
+
`{ update: targets.root, data: ... }` instead replaces root's complete value,
|
|
86
|
+
retaining its active children and running work. It takes no input and does not
|
|
87
|
+
run initial constructors. Other source/ancestor updates follow the same rules.
|
|
142
88
|
|
|
143
|
-
##
|
|
89
|
+
## Inline and named construction
|
|
144
90
|
|
|
145
|
-
Ordinary transitions
|
|
146
|
-
make input; `decoded` returns an already decoded value. Omit construction only
|
|
147
|
-
when the selected state can be constructed without arguments.
|
|
91
|
+
Ordinary transitions stay inline:
|
|
148
92
|
|
|
149
93
|
```ts
|
|
150
|
-
|
|
151
|
-
target: targets.root.
|
|
152
|
-
|
|
153
|
-
from: ({ state }) => ({ requestId: state.draft })
|
|
94
|
+
Load: {
|
|
95
|
+
target: targets.root.Loading,
|
|
96
|
+
data: ({ event }) => ({ documentId: event.documentId })
|
|
154
97
|
}
|
|
155
98
|
```
|
|
156
99
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
100
|
+
Targeting a compound or parallel state follows its initial declarations. Event
|
|
101
|
+
transitions use `target`, never `initial`. `initial` names only handler startup
|
|
102
|
+
edges. `history` restores a declared history reference; `none: true` accepts an
|
|
103
|
+
event without changing its target configuration.
|
|
161
104
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
exact declaration, so its result cannot introduce another destination.
|
|
105
|
+
Declare branch topology in `make` when a resolver chooses an outcome, builds an
|
|
106
|
+
explicit subtree, or enqueues commands:
|
|
165
107
|
|
|
166
108
|
```ts
|
|
167
|
-
// In make:
|
|
168
109
|
branches: {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
110
|
+
open: {
|
|
111
|
+
checkout: { target: targets.root.Checkout },
|
|
112
|
+
unchanged: { none: true }
|
|
172
113
|
}
|
|
173
114
|
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
The resolver calls a constructor bound to that declaration:
|
|
174
118
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
branches: "
|
|
178
|
-
resolve: ({ event, select }) => event.
|
|
179
|
-
? select.
|
|
180
|
-
|
|
119
|
+
```ts
|
|
120
|
+
Open: {
|
|
121
|
+
branches: "open",
|
|
122
|
+
resolve: ({ event, select }) => event.open
|
|
123
|
+
? select.checkout({
|
|
124
|
+
data: { cartId: event.cartId },
|
|
125
|
+
states: { Review: { data: { total: event.total } } }
|
|
126
|
+
})
|
|
127
|
+
: select.unchanged()
|
|
181
128
|
}
|
|
182
129
|
```
|
|
183
130
|
|
|
184
|
-
|
|
185
|
-
|
|
131
|
+
Objects contain computed values, not nested constructor callbacks. `data` is
|
|
132
|
+
schema make input; `{ decoded: true, data: value }` supplies the decoded type.
|
|
133
|
+
Both paths validate and report typed schema failures before committing changes.
|
|
134
|
+
The decoded flag applies only to its own node. Structural nodes reject data.
|
|
186
135
|
|
|
187
|
-
|
|
188
|
-
|
|
136
|
+
Omitting `states` follows declared initial children. Explicit compound
|
|
137
|
+
construction selects exactly one child. Entering an inactive parallel subtree
|
|
138
|
+
requires every region. Source-local parallel construction can select a single
|
|
139
|
+
region while retaining its active siblings, preserving the existing scope rules.
|
|
140
|
+
Names under `states` cannot collide with construction metadata.
|
|
141
|
+
|
|
142
|
+
Declared branch targets remain inspectable without executing a resolver.
|
|
143
|
+
Dynamically selected descendants are visible after resolution; this API does not
|
|
144
|
+
add static analysis of resolver bodies.
|
|
145
|
+
|
|
146
|
+
## Retained-owner updates
|
|
147
|
+
|
|
148
|
+
An inline combined transition keeps `target` and `update` references separate:
|
|
189
149
|
|
|
190
150
|
```ts
|
|
191
151
|
Save: {
|
|
192
|
-
target: targets.root.Saving,
|
|
193
|
-
update: targets.root,
|
|
194
|
-
|
|
152
|
+
target: targets.root.Checkout.Saving,
|
|
153
|
+
update: targets.root.Checkout,
|
|
154
|
+
data: ({ event }) => ({
|
|
195
155
|
target: { requestId: event.requestId },
|
|
196
|
-
update: {
|
|
156
|
+
update: { cartId: event.cartId }
|
|
197
157
|
})
|
|
198
158
|
}
|
|
199
159
|
```
|
|
200
160
|
|
|
201
|
-
|
|
202
|
-
`{ target, update }` declaration for nested construction or mixed methods:
|
|
203
|
-
`select.saved.from(payload).update.decoded(ownerValue)`.
|
|
161
|
+
For the same pair declared as a named branch, construct both in one call:
|
|
204
162
|
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
## Completion and history
|
|
163
|
+
```ts
|
|
164
|
+
select.saved({
|
|
165
|
+
data: { requestId: event.requestId },
|
|
166
|
+
update: { data: { cartId: event.cartId } }
|
|
167
|
+
})
|
|
168
|
+
```
|
|
213
169
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
completed descendant does not make every ancestor final. Parallel roots
|
|
218
|
-
complete when all required regions complete.
|
|
170
|
+
`update` is required only for a branch declaring an owner update. It is not an
|
|
171
|
+
arbitrary nested-state operation. The owner must be active and retained across
|
|
172
|
+
the transition. Both complete values validate before either is committed.
|
|
219
173
|
|
|
220
|
-
|
|
221
|
-
root's own history can restore root-owned data. History fallback builders must
|
|
222
|
-
construct a complete configuration containing the history owner.
|
|
174
|
+
## History and lifecycle
|
|
223
175
|
|
|
224
|
-
|
|
176
|
+
History fallback uses a bound function with the same node object format:
|
|
225
177
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
178
|
+
```ts
|
|
179
|
+
history: {
|
|
180
|
+
recent: {
|
|
181
|
+
default: ({ target }) => target({
|
|
182
|
+
states: {
|
|
183
|
+
Checkout: {
|
|
184
|
+
data: { cartId: "new" },
|
|
185
|
+
states: { Review: { data: { total: 0 } } }
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
234
192
|
|
|
235
|
-
|
|
236
|
-
|
|
193
|
+
A fallback must explicitly construct a complete tree containing its owner,
|
|
194
|
+
including required ancestors and parallel siblings. Its root data is state data,
|
|
195
|
+
not startup input. `Machine.resume` restores a validated snapshot without rerunning
|
|
196
|
+
startup constructors.
|
|
237
197
|
|
|
238
|
-
`
|
|
239
|
-
|
|
240
|
-
|
|
198
|
+
`guard` declines before construction. A resolver may return `decline()` only with
|
|
199
|
+
`declinable: true`. Effect, Stream, timer, logic, child invocation, completion,
|
|
200
|
+
entry/exit, and required outcome channels retain their existing contracts.
|
|
241
201
|
|
|
242
202
|
## Migration
|
|
243
203
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
`
|
|
247
|
-
`
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
Replace
|
|
253
|
-
`emittedEvents` with field records, or use their explicit `FromSchemas`
|
|
254
|
-
constructors. Replace old Atom `.state` reads with `.result`. Use
|
|
255
|
-
`EmittedEvent`, `EmittedEvents`, `EmittedEventOf`, and `SchemaLessStateAnnotations` in place
|
|
256
|
-
of the removed deprecated type aliases.
|
|
257
|
-
|
|
258
|
-
Encoded snapshots now use version 2 and include the root at path `""`.
|
|
259
|
-
Version 1 payloads must be explicitly migrated; the decoder rejects them.
|
|
204
|
+
- Root initial constructors can read `input`; remove root fields used only to
|
|
205
|
+
forward that input to the initial child.
|
|
206
|
+
- Replace event `{ initial: reference, data }` with `{ target: reference, data }`.
|
|
207
|
+
- Replace `select.branch.from(value)` with `select.branch({ data: value })`.
|
|
208
|
+
- Replace `.decoded(value)` with `({ decoded: true, data: value })`.
|
|
209
|
+
- Replace child callbacks with `states: { Child: { data, states } }`.
|
|
210
|
+
- Replace `.update.from(value)` chaining with `update: { data: value }` inside
|
|
211
|
+
the same selector call.
|
|
212
|
+
- Replace history fallback chains with `target({ states: ... })`.
|