@typeonce/effect-machine 0.34.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.
Files changed (98) hide show
  1. package/README.md +98 -33
  2. package/dist/Machine.d.ts +408 -483
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +126 -87
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/command.d.ts +0 -1
  7. package/dist/internal/machine/command.d.ts.map +1 -1
  8. package/dist/internal/machine/command.js +0 -14
  9. package/dist/internal/machine/command.js.map +1 -1
  10. package/dist/internal/machine/configuration.d.ts +0 -1
  11. package/dist/internal/machine/configuration.d.ts.map +1 -1
  12. package/dist/internal/machine/configuration.js +15 -21
  13. package/dist/internal/machine/configuration.js.map +1 -1
  14. package/dist/internal/machine/construction.d.ts +9 -0
  15. package/dist/internal/machine/construction.d.ts.map +1 -0
  16. package/dist/internal/machine/construction.js +132 -0
  17. package/dist/internal/machine/construction.js.map +1 -0
  18. package/dist/internal/machine/declaration.d.ts +1 -0
  19. package/dist/internal/machine/declaration.d.ts.map +1 -1
  20. package/dist/internal/machine/declaration.js +3 -8
  21. package/dist/internal/machine/declaration.js.map +1 -1
  22. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  23. package/dist/internal/machine/executionPlan.js +21 -27
  24. package/dist/internal/machine/executionPlan.js.map +1 -1
  25. package/dist/internal/machine/implementation.d.ts +2 -3
  26. package/dist/internal/machine/implementation.d.ts.map +1 -1
  27. package/dist/internal/machine/implementation.js.map +1 -1
  28. package/dist/internal/machine/initialDeclaration.d.ts +14 -0
  29. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
  30. package/dist/internal/machine/initialDeclaration.js +137 -0
  31. package/dist/internal/machine/initialDeclaration.js.map +1 -0
  32. package/dist/internal/machine/machine.d.ts +0 -2
  33. package/dist/internal/machine/machine.d.ts.map +1 -1
  34. package/dist/internal/machine/machine.js +101 -282
  35. package/dist/internal/machine/machine.js.map +1 -1
  36. package/dist/internal/machine/planner.d.ts +8 -0
  37. package/dist/internal/machine/planner.d.ts.map +1 -1
  38. package/dist/internal/machine/planner.js +86 -82
  39. package/dist/internal/machine/planner.js.map +1 -1
  40. package/dist/internal/machine/protocol.d.ts +1 -1
  41. package/dist/internal/machine/protocol.d.ts.map +1 -1
  42. package/dist/internal/machine/protocol.js.map +1 -1
  43. package/dist/internal/machine/stateDefinition.d.ts +1 -1
  44. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  45. package/dist/internal/machine/stateDefinition.js +1 -13
  46. package/dist/internal/machine/stateDefinition.js.map +1 -1
  47. package/dist/internal/machine/targetReference.d.ts.map +1 -1
  48. package/dist/internal/machine/targetReference.js +14 -4
  49. package/dist/internal/machine/targetReference.js.map +1 -1
  50. package/dist/internal/machine/topology.d.ts +6 -0
  51. package/dist/internal/machine/topology.d.ts.map +1 -1
  52. package/dist/internal/machine/topology.js +3 -2
  53. package/dist/internal/machine/topology.js.map +1 -1
  54. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  55. package/dist/internal/testing/machine/finiteModel.js +51 -82
  56. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  57. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  58. package/dist/internal/testing/machine/verification.js +7 -1
  59. package/dist/internal/testing/machine/verification.js.map +1 -1
  60. package/dist/testing/MachineTest.d.ts +105 -73
  61. package/dist/testing/MachineTest.d.ts.map +1 -1
  62. package/dist/testing/MachineTest.js +105 -73
  63. package/dist/testing/MachineTest.js.map +1 -1
  64. package/dist/unstable/cluster/ClusterMachine.d.ts +13 -8
  65. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  66. package/dist/unstable/cluster/ClusterMachine.js +13 -8
  67. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  68. package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
  69. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  70. package/dist/unstable/reactivity/AtomMachine.js +41 -24
  71. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  72. package/docs/agent-guide.md +37 -34
  73. package/docs/root-api.md +143 -190
  74. package/package.json +1 -1
  75. package/src/Machine.ts +631 -872
  76. package/src/internal/machine/command.ts +0 -16
  77. package/src/internal/machine/configuration.ts +13 -30
  78. package/src/internal/machine/construction.ts +147 -0
  79. package/src/internal/machine/declaration.ts +4 -7
  80. package/src/internal/machine/executionPlan.ts +20 -25
  81. package/src/internal/machine/implementation.ts +2 -3
  82. package/src/internal/machine/initialDeclaration.ts +157 -0
  83. package/src/internal/machine/machine.ts +118 -396
  84. package/src/internal/machine/planner.ts +97 -99
  85. package/src/internal/machine/protocol.ts +3 -3
  86. package/src/internal/machine/stateDefinition.ts +1 -15
  87. package/src/internal/machine/targetReference.ts +19 -4
  88. package/src/internal/machine/topology.ts +7 -2
  89. package/src/internal/testing/machine/finiteModel.ts +60 -132
  90. package/src/internal/testing/machine/verification.ts +7 -1
  91. package/src/testing/MachineTest.ts +105 -73
  92. package/src/unstable/cluster/ClusterMachine.ts +13 -8
  93. package/src/unstable/reactivity/AtomMachine.ts +41 -24
  94. package/dist/internal/machine/targetBuilder.d.ts +0 -16
  95. package/dist/internal/machine/targetBuilder.d.ts.map +0 -1
  96. package/dist/internal/machine/targetBuilder.js +0 -333
  97. package/dist/internal/machine/targetBuilder.js.map +0 -1
  98. package/src/internal/machine/targetBuilder.ts +0 -525
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
- from: ({ root, event }) => ({ count: root.count + event.by })
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
- const Search = Machine.make({ root: Root, events: Events }).handle({
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
- from: ({ event }) => ({ query: event.query })
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
- `from` accepts the schema's make input, including defaults and transformations.
120
- `decoded` accepts its decoded value, preserving class instances. The planner
121
- validates both boundaries and reports `MachineSchemaDecodeError`. Omit construction
122
- only when the selected builder supports empty default construction.
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: {
@@ -150,8 +209,8 @@ const machine = Machine.make({
150
209
  branches: "search",
151
210
  resolve: ({ event, select }) =>
152
211
  event.query.length > 0
153
- ? select.loading.from({ query: event.query })
154
- : select.idle.from()
212
+ ? select.loading({ data: { query: event.query } })
213
+ : select.idle()
155
214
  }
156
215
  }
157
216
  }
@@ -162,10 +221,10 @@ const machine = Machine.make({
162
221
  Each selector is bound to its declared destination. Its constructor rejects a
163
222
  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
- `select.checkout.from(parentValues, child => child.Review.from(childValues))`
224
+ `select.checkout({ data: parentValues, states: { Review: { data: childValues } } })`
166
225
  constructs the explicitly selected subtree. Parallel constructors require every
167
- entered region. Required initializers and final output implementations remain
168
- part of machine readiness checking.
226
+ entered region; source-local construction can retain active sibling regions. 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,12 @@ 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, from: ... }` | Enter a declared destination with its value. |
183
- | `{ initial: targets.root.Checkout, from: ... }` | Enter the declared initial configuration of a compound or parallel state. |
241
+ | `{ target: targets.root.Checkout.Review, data: ... }` | Enter a declared destination with its value. |
242
+ | `{ target: 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, from: ... }` | Replace a retained active owner's value. |
186
- | `{ update: targets.root, from: ... }` | Replace root data and retain active descendants. |
244
+ | `{ update: targets.root.Checkout, data: ... }` | Replace a retained active owner's value. |
245
+ | `{ target: targets.root, input: ... }` | Reconstruct root and its initial children using fresh machine input. |
246
+ | `{ update: targets.root, data: ... }` | Replace root data and retain active descendants. |
187
247
  | `{ none: true }` | Accept an event without changing the configuration. |
188
248
 
189
249
  An atomic transition can enter a destination and update one retained owner:
@@ -192,7 +252,7 @@ An atomic transition can enter a destination and update one retained owner:
192
252
  Save: {
193
253
  target: targets.root.Checkout.Saving,
194
254
  update: targets.root.Checkout,
195
- from: ({ event, ancestors }) => ({
255
+ data: ({ event, ancestors }) => ({
196
256
  target: { request: event.request },
197
257
  update: { ...ancestors.Checkout, revision: event.revision }
198
258
  })
@@ -201,13 +261,13 @@ Save: {
201
261
 
202
262
  The complete replacement values are validated before either change is applied.
203
263
  Advanced construction declares both references in a branch and uses
204
- `select.saved.from(destinationValues).update.from(ownerValues)`.
264
+ `select.saved({ data: destinationValues, update: { data: ownerValues } })`.
205
265
  A retained owner must be active for that source and remain active through the
206
266
  transition. A sibling region's value cannot be updated through this operation.
207
267
 
208
268
  The runtime transition API does not replace arbitrary complete root
209
- configurations. `initialConfiguration` and history defaults retain complete
210
- configuration construction for startup and restoration.
269
+ configurations. Root targets accept fresh input and follow the initial declarations in `.handle`; history
270
+ defaults retain complete subtree construction for restoration.
211
271
 
212
272
  ### Protocols and ownership
213
273
 
@@ -232,7 +292,7 @@ adapter.
232
292
 
233
293
  The same root supports compound and parallel states, eventless `always`
234
294
  transitions, ordered named choices, final states, `onDone` completion,
235
- initializers, shallow and deep history, and retained snapshots. The topology
295
+ initial declarations, shallow and deep history, and retained snapshots. The topology
236
296
  is captured directly from declarations; visualization and branch coverage do
237
297
  not execute user resolvers or depend on TypeScript source extraction.
238
298
 
@@ -257,15 +317,21 @@ const Search = Machine.make({
257
317
  logic: { worker: workerLogic },
258
318
  children: { validation: ValidationChild }
259
319
  }).handle({
320
+ initial: {
321
+ target: Machine.targets(Root).root.Idle
322
+ },
260
323
  states: {
324
+ Idle: {},
261
325
  Loading: {
262
326
  invoke: {
263
327
  src: "load",
264
328
  input: ({ state }) => state.query,
265
- onDone: { target: targets.root.Ready, from: ({ output }) => ({ items: output }) },
266
- onFailure: { target: targets.root.Failed, from: ({ error }) => ({ message: error.message }) }
329
+ onDone: { target: targets.root.Ready, data: ({ output }) => ({ items: output }) },
330
+ onFailure: { target: targets.root.Failed, data: ({ error }) => ({ message: error.message }) }
267
331
  }
268
- }
332
+ },
333
+ Ready: {},
334
+ Failed: {}
269
335
  }
270
336
  })
271
337
  ```
@@ -290,8 +356,7 @@ and cancellation boundaries; a typed `onFailure` handles the source's declared
290
356
  runtime error channel.
291
357
 
292
358
  ```ts
293
- invoke: ;
294
- ;[
359
+ const invocations = [
295
360
  {
296
361
  src: "updates",
297
362
  onElement: {