@typeonce/effect-machine 0.38.0 → 0.39.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 (61) hide show
  1. package/README.md +43 -36
  2. package/dist/Machine.d.ts +62 -88
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +11 -24
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/atom.d.ts.map +1 -1
  7. package/dist/internal/machine/atom.js.map +1 -1
  8. package/dist/internal/machine/declaration.d.ts +10 -3
  9. package/dist/internal/machine/declaration.d.ts.map +1 -1
  10. package/dist/internal/machine/declaration.js +47 -16
  11. package/dist/internal/machine/declaration.js.map +1 -1
  12. package/dist/internal/machine/errors.d.ts +15 -1
  13. package/dist/internal/machine/errors.d.ts.map +1 -1
  14. package/dist/internal/machine/errors.js +10 -0
  15. package/dist/internal/machine/errors.js.map +1 -1
  16. package/dist/internal/machine/initialDeclaration.d.ts +2 -2
  17. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -1
  18. package/dist/internal/machine/initialDeclaration.js +8 -9
  19. package/dist/internal/machine/initialDeclaration.js.map +1 -1
  20. package/dist/internal/machine/machine.d.ts +26 -76
  21. package/dist/internal/machine/machine.d.ts.map +1 -1
  22. package/dist/internal/machine/machine.js +21 -16
  23. package/dist/internal/machine/machine.js.map +1 -1
  24. package/dist/internal/machine/planner.d.ts +216 -31
  25. package/dist/internal/machine/planner.d.ts.map +1 -1
  26. package/dist/internal/machine/planner.js +6 -12
  27. package/dist/internal/machine/planner.js.map +1 -1
  28. package/dist/internal/machine/process.d.ts +5 -9
  29. package/dist/internal/machine/process.d.ts.map +1 -1
  30. package/dist/internal/machine/process.js +19 -32
  31. package/dist/internal/machine/process.js.map +1 -1
  32. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  33. package/dist/internal/testing/machine/finiteModel.js +9 -12
  34. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  35. package/dist/testing/MachineTest.d.ts +7 -7
  36. package/dist/testing/MachineTest.js +7 -7
  37. package/dist/unstable/cluster/ClusterMachine.d.ts +1 -1
  38. package/dist/unstable/cluster/ClusterMachine.js +1 -1
  39. package/dist/unstable/reactivity/AtomMachine.d.ts +3 -3
  40. package/dist/unstable/reactivity/AtomMachine.js +3 -3
  41. package/docs/agent-guide.md +23 -30
  42. package/docs/machine-review.md +5 -6
  43. package/docs/root-api.md +27 -19
  44. package/package.json +5 -5
  45. package/src/Machine.ts +125 -163
  46. package/src/internal/machine/atom.ts +4 -7
  47. package/src/internal/machine/declaration.ts +56 -17
  48. package/src/internal/machine/errors.ts +22 -1
  49. package/src/internal/machine/initialDeclaration.ts +10 -12
  50. package/src/internal/machine/machine.ts +85 -465
  51. package/src/internal/machine/planner.ts +84 -190
  52. package/src/internal/machine/process.ts +80 -279
  53. package/src/internal/testing/machine/finiteModel.ts +7 -11
  54. package/src/testing/MachineTest.ts +7 -7
  55. package/src/unstable/cluster/ClusterMachine.ts +1 -1
  56. package/src/unstable/reactivity/AtomMachine.ts +3 -3
  57. package/dist/internal/machine/targetReference.d.ts +0 -15
  58. package/dist/internal/machine/targetReference.d.ts.map +0 -1
  59. package/dist/internal/machine/targetReference.js +0 -41
  60. package/dist/internal/machine/targetReference.js.map +0 -1
  61. package/src/internal/machine/targetReference.ts +0 -55
@@ -35,22 +35,20 @@ export const CounterEvents = Machine.eventsFromSchemas(
35
35
  })
36
36
  )
37
37
 
38
- const targets = Machine.targets(CounterStates)
39
-
40
38
  export const CounterMachine = Machine.make({
41
39
  id: "Counter",
42
40
  root: CounterStates,
43
41
  events: CounterEvents,
44
- }).handle({ initial: { target: targets.root.Idle }, states: {
42
+ }).handle({ initial: { target: "Idle" }, states: {
45
43
  Idle: {
46
44
  on: {
47
- Start: { target: targets.root.Running, data: () => ({ count: 0 }) }
45
+ Start: { target: "Running", data: () => ({ count: 0 }) }
48
46
  }
49
47
  },
50
48
  Running: {
51
49
  on: {
52
- Increment: { update: targets.root.Running, data: ({ state }) => ({ count: state.count + 1 }) },
53
- Stop: { target: targets.root.Idle }
50
+ Increment: { update: "Running", data: ({ state }) => ({ count: state.count + 1 }) },
51
+ Stop: { target: "Idle" }
54
52
  }
55
53
  }
56
54
  } })
@@ -72,7 +70,7 @@ Use `data` for schema make input. Constructor defaults and validation run while
72
70
  planning. Use `{ decoded: true, data }` for an existing schema Type; validation
73
71
  still applies. Named selectors take the same object format, with nested children
74
72
  under `states`. Structural states omit data. See [Root API](./root-api.md) for
75
- startup input, root targets, subtree construction, and history fallbacks.
73
+ startup input, root initialization, subtree construction, and history fallbacks.
76
74
 
77
75
  The examples below show one modeling decision at a time. They omit unchanged
78
76
  state and event declarations already shown above.
@@ -170,17 +168,16 @@ const DocumentEvents = Machine.eventsFromSchemas(
170
168
  })
171
169
  )
172
170
 
173
- const documentTargets = Machine.targets(DocumentStates)
174
171
  const DocumentMachine = Machine.make({
175
172
  root: DocumentStates,
176
173
  events: DocumentEvents
177
- }).handle({ initial: { target: documentTargets.root.Closed }, states: {
174
+ }).handle({ initial: { target: "Closed" }, states: {
178
175
  Closed: {},
179
176
  Open: {
180
- initial: { target: documentTargets.root.Open.Editing },
177
+ initial: { target: "Open.Editing" },
181
178
  on: {
182
179
  // All Open children close the document in the same way.
183
- Close: { target: documentTargets.root.Closed }
180
+ Close: { target: "Closed" }
184
181
  },
185
182
  states: {
186
183
  Editing: {},
@@ -209,20 +206,19 @@ export const CheckoutEvents = Machine.eventsFromSchemas(
209
206
  })
210
207
  )
211
208
 
212
- const checkoutTargets = Machine.targets(CheckoutStates)
213
209
  const CheckoutMachine = Machine.make({
214
210
  root: CheckoutStates,
215
211
  events: CheckoutEvents
216
212
  }).handle({ states: {
217
213
  Editing: {
218
214
  on: {
219
- Submit: { target: checkoutTargets.root.Submitting }
215
+ Submit: { target: "Submitting" }
220
216
  }
221
217
  },
222
218
  Submitting: {
223
219
  on: {
224
220
  // Cancel has meaning while work is in progress.
225
- Cancel: { target: checkoutTargets.root.Editing }
221
+ Cancel: { target: "Editing" }
226
222
  }
227
223
  },
228
224
  Complete: {}
@@ -262,12 +258,11 @@ const ScreenStates = Machine.state({ states: {
262
258
  }
263
259
  }
264
260
  } })
265
- const screenTargets = Machine.targets(ScreenStates)
266
261
  const ScreenMachine = Machine.make({ root: ScreenStates, events: Machine.events({}) }).handle({
267
- initial: { target: screenTargets.root.Screen },
262
+ initial: { target: "Screen" },
268
263
  states: { Screen: { states: {
269
- connection: { initial: { target: screenTargets.root.Screen.connection.Online } },
270
- panel: { initial: { target: screenTargets.root.Screen.panel.Closed } }
264
+ connection: { initial: { target: "Screen.connection.Online" } },
265
+ panel: { initial: { target: "Screen.panel.Closed" } }
271
266
  } } }
272
267
  })
273
268
 
@@ -300,19 +295,18 @@ const LoadStates = Machine.state({ states: {
300
295
  Failed: LoadState.cases.Failed
301
296
  } })
302
297
 
303
- const loadTargets = Machine.targets(LoadStates)
304
298
  const LoadMachine = Machine.make({
305
299
  root: LoadStates,
306
300
  effects: { loadDocument },
307
301
  events: Machine.eventsFromSchemas(),
308
- }).handle({ initial: { target: loadTargets.root.Idle }, states: {
302
+ }).handle({ initial: { target: "Idle" }, states: {
309
303
  Idle: {},
310
304
  Loading: {
311
305
  invoke: {
312
306
  src: "loadDocument",
313
307
  input: ({ state }) => state.documentId,
314
- onDone: { target: loadTargets.root.Ready, data: ({ output }) => ({ content: output }) },
315
- onFailure: { target: loadTargets.root.Failed, data: ({ error }) => ({ message: String(error) }) }
308
+ onDone: { target: "Ready", data: ({ output }) => ({ content: output }) },
309
+ onFailure: { target: "Failed", data: ({ error }) => ({ message: String(error) }) }
316
310
  }
317
311
  },
318
312
  Ready: {},
@@ -338,10 +332,10 @@ Loading: {
338
332
  {
339
333
  src: "loadDocument",
340
334
  input: ({ state }) => state.documentId,
341
- onDone: { target: loadTargets.root.Ready, data: ({ output }) => ({ content: output }) },
342
- onFailure: { target: loadTargets.root.Failed, data: ({ error }) => ({ message: String(error) }) }
335
+ onDone: { target: "Ready", data: ({ output }) => ({ content: output }) },
336
+ onFailure: { target: "Failed", data: ({ error }) => ({ message: String(error) }) }
343
337
  },
344
- { src: "loadTimeout", onDone: { target: loadTargets.root.Idle } }
338
+ { src: "loadTimeout", onDone: { target: "Idle" } }
345
339
  ]
346
340
  }
347
341
  ```
@@ -381,17 +375,16 @@ const ReviewEvents = Machine.eventsFromSchemas(
381
375
  })
382
376
  )
383
377
 
384
- const reviewTargets = Machine.targets(ReviewStates)
385
378
  const ReviewMachine = Machine.make({
386
379
  root: ReviewStates,
387
380
  events: ReviewEvents,
388
381
  branches: {
389
382
  evaluate: {
390
- accepted: { target: reviewTargets.root.Accepted },
391
- rejected: { target: reviewTargets.root.Rejected }
383
+ accepted: { target: "Accepted" },
384
+ rejected: { target: "Rejected" }
392
385
  }
393
386
  }
394
- }).handle({ initial: { target: reviewTargets.root.Pending }, states: {
387
+ }).handle({ initial: { target: "Pending" }, states: {
395
388
  Pending: {
396
389
  on: {
397
390
  Evaluate: {
@@ -417,7 +410,7 @@ descendants and running work:
417
410
 
418
411
  ```ts
419
412
  Changed: {
420
- update: targets.root.Document,
413
+ update: "Document",
421
414
  data: ({ ancestors }) => ({
422
415
  ...ancestors.Document,
423
416
  revision: ancestors.Document.revision + 1
@@ -32,8 +32,8 @@ provider has no ownership purpose, inspect that code more closely.
32
32
 
33
33
  ## Keep ordinary transitions inline
34
34
 
35
- Use `{ target: targets.root.Running }` for a state with default construction.
36
- Use `{ target: targets.root.Running, from: ({ event }) => ({ count: event.count }) }`
35
+ Use `{ target: "Running" }` for a state with default construction.
36
+ Use `{ target: "Running", data: ({ event }) => ({ count: event.count }) }`
37
37
  when the destination needs data. The checker rejects missing required fields.
38
38
  Use `decoded` for values already decoded by their schema.
39
39
 
@@ -155,19 +155,18 @@ and its lifetime:
155
155
 
156
156
  ```ts
157
157
  // In make: effects: { submitOrder }
158
- // targets is Machine.targets(OrderStates).
159
158
  machine.handle({ states: {
160
159
  Editing: {
161
160
  on: {
162
- Submit: { target: targets.root.Submitting, from: ({ event }) => ({ order: event.order }) }
161
+ Submit: { target: "Submitting", data: ({ event }) => ({ order: event.order }) }
163
162
  }
164
163
  },
165
164
  Submitting: {
166
165
  invoke: {
167
166
  src: "submitOrder",
168
167
  input: ({ state }) => state.order,
169
- onDone: { target: targets.root.Complete, from: ({ output }) => ({ order: output }) },
170
- onFailure: { target: targets.root.Failed, from: ({ error }) => ({ message: String(error) }) }
168
+ onDone: { target: "Complete", data: ({ output }) => ({ order: output }) },
169
+ onFailure: { target: "Failed", data: ({ error }) => ({ message: String(error) }) }
171
170
  }
172
171
  }
173
172
  } })
package/docs/root-api.md CHANGED
@@ -17,7 +17,6 @@ const Root = Machine.state({
17
17
  Ready: {}
18
18
  }
19
19
  })
20
- const targets = Machine.targets(Root)
21
20
  const Events = Machine.events({
22
21
  Loaded: {},
23
22
  Reload: { locale: Schema.String, documentId: Schema.String }
@@ -29,18 +28,17 @@ const Document = Machine.make({
29
28
  }).handle({
30
29
  root: ({ input }) => ({ locale: input.locale }),
31
30
  initial: {
32
- target: targets.root.Loading,
31
+ target: "Loading",
33
32
  data: ({ input }) => ({ documentId: input.documentId })
34
33
  },
35
34
  on: {
36
35
  Reload: {
37
- target: targets.root,
38
- input: ({ event }) => ({ locale: event.locale, documentId: event.documentId }),
36
+ initialize: ({ event }) => ({ locale: event.locale, documentId: event.documentId }),
39
37
  reenter: true
40
38
  }
41
39
  },
42
40
  states: {
43
- Loading: { on: { Loaded: { target: targets.root.Ready } } }
41
+ Loading: { on: { Loaded: { target: "Ready" } } }
44
42
  }
45
43
  })
46
44
  ```
@@ -68,21 +66,26 @@ optional region data. Compound handlers declare one direct initial child with
68
66
  `initial: { target, data }`; each compound has its own initial declaration.
69
67
  There is no `initialConfiguration` or initializer in `make`.
70
68
 
71
- ## Root targets and updates
69
+ ## Root initialization and updates
72
70
 
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.
71
+ The root is spelled `"root"` in declarations, so a top-level state cannot use
72
+ that name. It is not a transition destination.
77
73
 
78
- Targeting root retains its lifecycle unless it is reentered. Put a machine-wide
74
+ `{ initialize: ... }` reconstructs root data and follows its initial
75
+ declarations using fresh machine input: a value or a callback of the transition
76
+ context. A machine without input declares `{ initialize: true }`. `initialize`
77
+ rejects `target`, `update`, `history`, `none`, `data`, and `decoded`. A named
78
+ branch declares `{ initialize: true }`, and its resolver passes the input with
79
+ `select.branch({ input })`.
80
+
81
+ Initializing root retains its lifecycle unless it is reentered. Put a machine-wide
79
82
  reset handler at root and use `reenter: true` to exit and enter root as well as
80
83
  its descendants. Reentry restarts work owned by the exited states. Without
81
84
  reentry, retained root work continues; it does not automatically acquire new
82
85
  construction values. Ordinary transition conflict and lifecycle rules still apply.
83
86
  This does not create a new machine reference or discard history records.
84
87
 
85
- `{ update: targets.root, data: ... }` instead replaces root's complete value,
88
+ `{ update: "root", data: ... }` instead replaces root's complete value,
86
89
  retaining its active children and running work. It takes no input and does not
87
90
  run initial constructors. Other source/ancestor updates follow the same rules.
88
91
 
@@ -92,14 +95,14 @@ Ordinary transitions stay inline:
92
95
 
93
96
  ```ts
94
97
  Load: {
95
- target: targets.root.Loading,
98
+ target: "Loading",
96
99
  data: ({ event }) => ({ documentId: event.documentId })
97
100
  }
98
101
  ```
99
102
 
100
103
  Targeting a compound or parallel state follows its initial declarations. Event
101
104
  transitions use `target`, never `initial`. `initial` names only handler startup
102
- edges. `history` restores a declared history reference; `none: true` accepts an
105
+ edges. `history` restores a declared history state; `none: true` accepts an
103
106
  event without changing its target configuration.
104
107
 
105
108
  Declare branch topology in `make` when a resolver chooses an outcome, builds an
@@ -108,7 +111,7 @@ explicit subtree, or enqueues commands:
108
111
  ```ts
109
112
  branches: {
110
113
  open: {
111
- checkout: { target: targets.root.Checkout },
114
+ checkout: { target: "Checkout" },
112
115
  unchanged: { none: true }
113
116
  }
114
117
  }
@@ -145,12 +148,12 @@ add static analysis of resolver bodies.
145
148
 
146
149
  ## Retained-owner updates
147
150
 
148
- An inline combined transition keeps `target` and `update` references separate:
151
+ An inline combined transition keeps `target` and `update` paths separate:
149
152
 
150
153
  ```ts
151
154
  Save: {
152
- target: targets.root.Checkout.Saving,
153
- update: targets.root.Checkout,
155
+ target: "Checkout.Saving",
156
+ update: "Checkout",
154
157
  data: ({ event }) => ({
155
158
  target: { requestId: event.requestId },
156
159
  update: { cartId: event.cartId }
@@ -203,7 +206,12 @@ entry/exit, and required outcome channels retain their existing contracts.
203
206
 
204
207
  - Root initial constructors can read `input`; remove root fields used only to
205
208
  forward that input to the initial child.
206
- - Replace event `{ initial: reference, data }` with `{ target: reference, data }`.
209
+ - Remove `Machine.targets(Root)`. Replace `targets.root.A.B` with `"A.B"`,
210
+ `update: targets.root` with `update: "root"`, and
211
+ `{ target: targets.root, input }` with `{ initialize: input }` (or
212
+ `{ initialize: true }` without machine input). Rename a top-level state named
213
+ `root`.
214
+ - Replace event `{ initial: path, data }` with `{ target: path, data }`.
207
215
  - Replace `select.branch.from(value)` with `select.branch({ data: value })`.
208
216
  - Replace `.decoded(value)` with `({ decoded: true, data: value })`.
209
217
  - Replace child callbacks with `states: { Child: { data, states } }`.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typeonce/effect-machine",
3
- "version": "0.38.0",
3
+ "version": "0.39.0",
4
4
  "description": "Schema-first state machines and statecharts for Effect",
5
5
  "author": "Sandro Maglione",
6
6
  "repository": {
@@ -46,16 +46,16 @@
46
46
  "provenance": true
47
47
  },
48
48
  "peerDependencies": {
49
- "effect": "4.0.0-rc.116"
49
+ "effect": "4.0.0-rc.117"
50
50
  },
51
51
  "devDependencies": {
52
- "@effect/vitest": "4.0.0-rc.116",
52
+ "@effect/vitest": "4.0.0-rc.117",
53
53
  "@types/node": "25.7.0",
54
- "effect": "4.0.0-rc.116",
54
+ "effect": "4.0.0-rc.117",
55
55
  "tinybench": "2.9.0",
56
56
  "tstyche": "7.2.1",
57
57
  "typescript": "6.0.3",
58
- "vitest": "4.1.10"
58
+ "vitest": "5.0.1"
59
59
  },
60
60
  "engines": {
61
61
  "node": ">=20"