@typeonce/effect-machine 0.33.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.
Files changed (137) hide show
  1. package/README.md +272 -628
  2. package/dist/Machine.d.ts +1076 -896
  3. package/dist/Machine.d.ts.map +1 -1
  4. package/dist/Machine.js +141 -90
  5. package/dist/Machine.js.map +1 -1
  6. package/dist/internal/machine/childRegistry.d.ts +1 -1
  7. package/dist/internal/machine/childRegistry.d.ts.map +1 -1
  8. package/dist/internal/machine/cluster.d.ts +2 -50
  9. package/dist/internal/machine/cluster.d.ts.map +1 -1
  10. package/dist/internal/machine/cluster.js +4 -27
  11. package/dist/internal/machine/cluster.js.map +1 -1
  12. package/dist/internal/machine/clusterProtocol.d.ts +35 -0
  13. package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
  14. package/dist/internal/machine/clusterProtocol.js +28 -0
  15. package/dist/internal/machine/clusterProtocol.js.map +1 -0
  16. package/dist/internal/machine/commandRuntime.d.ts +1 -1
  17. package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
  18. package/dist/internal/machine/configuration.d.ts +2 -2
  19. package/dist/internal/machine/configuration.d.ts.map +1 -1
  20. package/dist/internal/machine/configuration.js +38 -64
  21. package/dist/internal/machine/configuration.js.map +1 -1
  22. package/dist/internal/machine/declaration.d.ts +17 -0
  23. package/dist/internal/machine/declaration.d.ts.map +1 -0
  24. package/dist/internal/machine/declaration.js +120 -0
  25. package/dist/internal/machine/declaration.js.map +1 -0
  26. package/dist/internal/machine/executionPlan.d.ts.map +1 -1
  27. package/dist/internal/machine/executionPlan.js +15 -22
  28. package/dist/internal/machine/executionPlan.js.map +1 -1
  29. package/dist/internal/machine/implementation.d.ts +34 -0
  30. package/dist/internal/machine/implementation.d.ts.map +1 -0
  31. package/dist/internal/machine/implementation.js +3 -0
  32. package/dist/internal/machine/implementation.js.map +1 -0
  33. package/dist/internal/machine/initialDeclaration.d.ts +14 -0
  34. package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
  35. package/dist/internal/machine/initialDeclaration.js +137 -0
  36. package/dist/internal/machine/initialDeclaration.js.map +1 -0
  37. package/dist/internal/machine/invocation.d.ts +1 -1
  38. package/dist/internal/machine/invocation.d.ts.map +1 -1
  39. package/dist/internal/machine/invocation.js +1 -1
  40. package/dist/internal/machine/invocation.js.map +1 -1
  41. package/dist/internal/machine/invocationDefinition.d.ts +1 -1
  42. package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
  43. package/dist/internal/machine/machine.d.ts +13 -4
  44. package/dist/internal/machine/machine.d.ts.map +1 -1
  45. package/dist/internal/machine/machine.js +169 -388
  46. package/dist/internal/machine/machine.js.map +1 -1
  47. package/dist/internal/machine/planner.d.ts +8 -0
  48. package/dist/internal/machine/planner.d.ts.map +1 -1
  49. package/dist/internal/machine/planner.js +37 -78
  50. package/dist/internal/machine/planner.js.map +1 -1
  51. package/dist/internal/machine/process.d.ts +8 -7
  52. package/dist/internal/machine/process.d.ts.map +1 -1
  53. package/dist/internal/machine/process.js +16 -12
  54. package/dist/internal/machine/process.js.map +1 -1
  55. package/dist/internal/machine/protocol.d.ts +1 -1
  56. package/dist/internal/machine/protocol.d.ts.map +1 -1
  57. package/dist/internal/machine/protocol.js.map +1 -1
  58. package/dist/internal/machine/runtime.d.ts +2 -230
  59. package/dist/internal/machine/runtime.d.ts.map +1 -1
  60. package/dist/internal/machine/runtime.js +8 -1857
  61. package/dist/internal/machine/runtime.js.map +1 -1
  62. package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
  63. package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
  64. package/dist/internal/machine/runtimeCompiled.js +867 -0
  65. package/dist/internal/machine/runtimeCompiled.js.map +1 -0
  66. package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
  67. package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
  68. package/dist/internal/machine/runtimeGeneric.js +471 -0
  69. package/dist/internal/machine/runtimeGeneric.js.map +1 -0
  70. package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
  71. package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
  72. package/dist/internal/machine/runtimeProtocol.js +561 -0
  73. package/dist/internal/machine/runtimeProtocol.js.map +1 -0
  74. package/dist/internal/machine/stateDefinition.d.ts +1 -1
  75. package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
  76. package/dist/internal/machine/stateDefinition.js +1 -13
  77. package/dist/internal/machine/stateDefinition.js.map +1 -1
  78. package/dist/internal/machine/targetReference.d.ts +15 -0
  79. package/dist/internal/machine/targetReference.d.ts.map +1 -0
  80. package/dist/internal/machine/targetReference.js +41 -0
  81. package/dist/internal/machine/targetReference.js.map +1 -0
  82. package/dist/internal/machine/topology.d.ts.map +1 -1
  83. package/dist/internal/machine/topology.js +5 -3
  84. package/dist/internal/machine/topology.js.map +1 -1
  85. package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
  86. package/dist/internal/testing/machine/finiteModel.js +53 -46
  87. package/dist/internal/testing/machine/finiteModel.js.map +1 -1
  88. package/dist/internal/testing/machine/probe.js +1 -1
  89. package/dist/internal/testing/machine/probe.js.map +1 -1
  90. package/dist/internal/testing/machine/verification.d.ts.map +1 -1
  91. package/dist/internal/testing/machine/verification.js +7 -1
  92. package/dist/internal/testing/machine/verification.js.map +1 -1
  93. package/dist/testing/MachineTest.d.ts +105 -73
  94. package/dist/testing/MachineTest.d.ts.map +1 -1
  95. package/dist/testing/MachineTest.js +105 -73
  96. package/dist/testing/MachineTest.js.map +1 -1
  97. package/dist/unstable/cluster/ClusterMachine.d.ts +18 -13
  98. package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
  99. package/dist/unstable/cluster/ClusterMachine.js +18 -25
  100. package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
  101. package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
  102. package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
  103. package/dist/unstable/reactivity/AtomMachine.js +41 -24
  104. package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
  105. package/docs/agent-guide.md +66 -91
  106. package/docs/machine-review.md +19 -49
  107. package/docs/root-api.md +62 -62
  108. package/package.json +1 -1
  109. package/src/Machine.ts +1836 -2907
  110. package/src/internal/machine/childRegistry.ts +1 -1
  111. package/src/internal/machine/cluster.ts +7 -73
  112. package/src/internal/machine/clusterProtocol.ts +56 -0
  113. package/src/internal/machine/commandRuntime.ts +1 -1
  114. package/src/internal/machine/configuration.ts +53 -65
  115. package/src/internal/machine/declaration.ts +140 -0
  116. package/src/internal/machine/executionPlan.ts +16 -22
  117. package/src/internal/machine/implementation.ts +41 -0
  118. package/src/internal/machine/initialDeclaration.ts +157 -0
  119. package/src/internal/machine/invocation.ts +3 -3
  120. package/src/internal/machine/invocationDefinition.ts +1 -1
  121. package/src/internal/machine/machine.ts +225 -576
  122. package/src/internal/machine/planner.ts +84 -118
  123. package/src/internal/machine/process.ts +40 -33
  124. package/src/internal/machine/protocol.ts +3 -3
  125. package/src/internal/machine/runtime.ts +20 -3037
  126. package/src/internal/machine/runtimeCompiled.ts +1166 -0
  127. package/src/internal/machine/runtimeGeneric.ts +799 -0
  128. package/src/internal/machine/runtimeProtocol.ts +1213 -0
  129. package/src/internal/machine/stateDefinition.ts +1 -15
  130. package/src/internal/machine/targetReference.ts +55 -0
  131. package/src/internal/machine/topology.ts +5 -3
  132. package/src/internal/testing/machine/finiteModel.ts +58 -53
  133. package/src/internal/testing/machine/probe.ts +1 -1
  134. package/src/internal/testing/machine/verification.ts +7 -1
  135. package/src/testing/MachineTest.ts +105 -73
  136. package/src/unstable/cluster/ClusterMachine.ts +22 -25
  137. package/src/unstable/reactivity/AtomMachine.ts +41 -24
@@ -35,6 +35,8 @@ export const CounterEvents = Machine.eventsFromSchemas(
35
35
  })
36
36
  )
37
37
 
38
+ const targets = Machine.targets(CounterStates)
39
+
38
40
  export const CounterMachine = Machine.make({
39
41
  id: "Counter",
40
42
  root: CounterStates,
@@ -43,13 +45,13 @@ export const CounterMachine = Machine.make({
43
45
  }).handle({ states: {
44
46
  Idle: {
45
47
  on: {
46
- Start: (to) => to.branch.Running().resolve(({ target }) => target.from({ count: 0 }))
48
+ Start: { target: targets.root.Running, from: () => ({ count: 0 }) }
47
49
  }
48
50
  },
49
51
  Running: {
50
52
  on: {
51
- Increment: (to) => to.branch.Running().resolve(({ state, target }) => target.from({ count: state.count + 1 })),
52
- Stop: (to) => to.branch.Idle()
53
+ Increment: { update: targets.root.Running, from: ({ state }) => ({ count: state.count + 1 }) },
54
+ Stop: { target: targets.root.Idle }
53
55
  }
54
56
  }
55
57
  } })
@@ -171,16 +173,16 @@ const DocumentEvents = Machine.eventsFromSchemas(
171
173
  })
172
174
  )
173
175
 
176
+ const documentTargets = Machine.targets(DocumentStates)
174
177
  const DocumentMachine = Machine.make({
175
178
  root: DocumentStates,
176
- events: DocumentEvents,
177
- initial: (to) => to.Closed()
179
+ events: DocumentEvents
178
180
  }).handle({ states: {
179
181
  Closed: {},
180
182
  Open: {
181
183
  on: {
182
184
  // All Open children close the document in the same way.
183
- Close: (to) => to.branch.Closed()
185
+ Close: { target: documentTargets.root.Closed }
184
186
  },
185
187
  states: {
186
188
  Editing: {},
@@ -209,20 +211,20 @@ export const CheckoutEvents = Machine.eventsFromSchemas(
209
211
  })
210
212
  )
211
213
 
214
+ const checkoutTargets = Machine.targets(CheckoutStates)
212
215
  const CheckoutMachine = Machine.make({
213
216
  root: CheckoutStates,
214
- events: CheckoutEvents,
215
- initial: (to) => to.Editing()
217
+ events: CheckoutEvents
216
218
  }).handle({ states: {
217
219
  Editing: {
218
220
  on: {
219
- Submit: (to) => to.branch.Submitting()
221
+ Submit: { target: checkoutTargets.root.Submitting }
220
222
  }
221
223
  },
222
224
  Submitting: {
223
225
  on: {
224
226
  // Cancel has meaning while work is in progress.
225
- Cancel: (to) => to.branch.Editing()
227
+ Cancel: { target: checkoutTargets.root.Editing }
226
228
  }
227
229
  },
228
230
  Complete: {}
@@ -293,18 +295,21 @@ const LoadStates = Machine.state({ initial: "Idle", states: {
293
295
  Failed: LoadState.cases.Failed
294
296
  } })
295
297
 
298
+ const loadTargets = Machine.targets(LoadStates)
296
299
  const LoadMachine = Machine.make({
297
300
  root: LoadStates,
301
+ effects: { loadDocument },
298
302
  events: Machine.eventsFromSchemas(),
299
303
  initialConfiguration: root => root.resolve(({ target }) => target.from(to => to.Idle.from()))
300
304
  }).handle({ states: {
301
305
  Idle: {},
302
306
  Loading: {
303
- invoke: (from) =>
304
- from
305
- .effect("load-document", ({ state }) => loadDocument(state.documentId))
306
- .onDone((to) => to.branch.Ready().resolve(({ output, target }) => target.from({ content: output })))
307
- .onFailure((to) => to.branch.Failed().resolve(({ error, target }) => target.from({ message: String(error) })))
307
+ invoke: {
308
+ src: "loadDocument",
309
+ input: ({ state }) => state.documentId,
310
+ onDone: { target: loadTargets.root.Ready, from: ({ output }) => ({ content: output }) },
311
+ onFailure: { target: loadTargets.root.Failed, from: ({ error }) => ({ message: String(error) }) }
312
+ }
308
313
  },
309
314
  Ready: {},
310
315
  Failed: {}
@@ -323,10 +328,16 @@ logic or child process that can be active at the same time a unique runtime
323
328
  address as well:
324
329
 
325
330
  ```ts
331
+ // Register timers: { loadTimeout: "10 seconds" } in make.
326
332
  Loading: {
327
- invoke: (from) => [
328
- from.effect("load-document", loadDocument),
329
- from.timer("load-timeout", "10 seconds")
333
+ invoke: [
334
+ {
335
+ src: "loadDocument",
336
+ input: ({ state }) => state.documentId,
337
+ onDone: { target: loadTargets.root.Ready, from: ({ output }) => ({ content: output }) },
338
+ onFailure: { target: loadTargets.root.Failed, from: ({ error }) => ({ message: String(error) }) }
339
+ },
340
+ { src: "loadTimeout", onDone: { target: loadTargets.root.Idle } }
330
341
  ]
331
342
  }
332
343
  ```
@@ -339,27 +350,13 @@ reuse its identity.
339
350
 
340
351
  ### Choose state-owned or process-owned children
341
352
 
342
- Use `from.child(...)` when the child belongs to one state and must stop when
343
- that state exits. Use a child family and `children.spawn(...)` when runtime
344
- events determine the ids or cardinality and the children must survive owner
345
- state changes:
346
-
347
- ```ts
348
- const Worker = Machine.childFamily(workerMachine)
349
-
350
- Commissioning: {
351
- invoke: (from) =>
352
- from.effect("start-workers", ({ children, state }) =>
353
- Effect.forEach(
354
- state.workers,
355
- (input) => children.spawn(Worker(input.id), { input }),
356
- { discard: true }
357
- )
358
- )
359
- .onDone((to) => to.branch.Running())
360
- .onFailure((to) => to.branch.Failed())
361
- }
362
- ```
353
+ Register a descriptor in `make({ children: { worker: Worker } })` and use
354
+ `invoke: { src: "worker", ...outcomes }` when a child belongs to one state and
355
+ must stop when that state exits. Use a child family and `children.spawn(...)`
356
+ inside a registered Effect when runtime events determine ids or cardinality
357
+ and the children must survive owner state changes. See the
358
+ [dynamic children example](../README.md#dynamic-children) for the complete
359
+ input mapping and service requirements.
363
360
 
364
361
  The Effect owns the startup attempt. The machine process owns every child that
365
362
  starts successfully. Leaving `Commissioning` does not stop those children.
@@ -380,24 +377,25 @@ const ReviewEvents = Machine.eventsFromSchemas(
380
377
  })
381
378
  )
382
379
 
380
+ const reviewTargets = Machine.targets(ReviewStates)
383
381
  const ReviewMachine = Machine.make({
384
382
  root: ReviewStates,
385
383
  events: ReviewEvents,
386
- initial: (to) => to.Pending()
384
+ branches: {
385
+ evaluate: {
386
+ accepted: { target: reviewTargets.root.Accepted },
387
+ rejected: { target: reviewTargets.root.Rejected }
388
+ }
389
+ }
387
390
  }).handle({ states: {
388
391
  Pending: {
389
392
  on: {
390
- Evaluate: (to) =>
391
- to
392
- .branches({
393
- accepted: { target: to.branch.Accepted() },
394
- rejected: { target: to.branch.Rejected() }
395
- })
396
- .resolve(({ event, select }) =>
397
- event.score >= 80
398
- ? select.accepted.from()
399
- : select.rejected.from()
400
- )
393
+ Evaluate: {
394
+ branches: "evaluate",
395
+ resolve: ({ event, select }) => event.score >= 80
396
+ ? select.accepted.from()
397
+ : select.rejected.from()
398
+ }
401
399
  }
402
400
  },
403
401
  Accepted: {},
@@ -410,50 +408,27 @@ Do not read the clock, generate randomness, call a service, or await work while
410
408
  choosing a transition. Receive such values in an event or produce them through
411
409
  state-owned work first.
412
410
 
413
- When only an active compound or parallel state's value changes, use its static
414
- update selection instead of reconstructing its active descendants:
411
+ When only an active state's value changes, use `update` to preserve its active
412
+ descendants and running work:
415
413
 
416
414
  ```ts
417
- Changed: (to) =>
418
- to.local.update(({ current, owner }) =>
419
- owner.from({ revision: current.revision + 1 })
420
- )
421
- ```
422
-
423
- `to.local.update` addresses the nearest valued compound scope.
424
- `to.branch.<path>.update` addresses a valued compound or parallel ancestor of
425
- the handler source. Both preserve the complete active descendant
426
- configuration. Neither runs lifecycle actions or restarts state-owned work by
427
- default. Use an event handled inside a parallel sibling when that sibling owns
428
- the value that must change.
429
-
430
- When topology changes and one valued ancestor remains active but needs a new
431
- value, declare the owner on the destination:
432
-
433
- ```ts
434
- CreatePlan: (to) =>
435
- to.local.SavingPlan()
436
- .updating(to.branch.Ready)
437
- .resolve(({ current, event, owner, target }) =>
438
- target.from({ request: event.input }).update(
439
- owner.decoded(new Ready({ ...current, notice: null }))
440
- )
441
- )
415
+ Changed: {
416
+ update: targets.root.Document,
417
+ from: ({ ancestors }) => ({
418
+ ...ancestors.Document,
419
+ revision: ancestors.Document.revision + 1
420
+ })
421
+ }
442
422
  ```
443
423
 
444
- `.updating(...)` accepts the retained state selector itself. It makes the
445
- owner replacement mandatory at compile time: the resolver must finish a
446
- destination construction with `.update(...)`. `current` is the owner's
447
- decoded pre-transition value. `target` constructs the destination and `owner`
448
- constructs the complete replacement value.
449
-
450
- Both instructions are validated and applied atomically. The retained owner is
451
- not reentered, destination entry sees its new value, and eventless
452
- stabilization runs afterward. Only local and branch targets that retain the
453
- owner expose `.updating`; full targets do not. Combined targets support one
454
- owner. Keep separate domain changes explicit by constructing the complete
455
- owner value instead of relying on a partial merge helper. Combined updates use
456
- a direct resolver; named branches support value-only updates.
424
+ Choose a valued source or retained ancestor explicitly. To change a parallel
425
+ sibling, send an event handled by that sibling. Combine a destination and one
426
+ retained owner update with `{ target, update, from }`; the callback returns
427
+ `{ target: destinationInput, update: completeOwnerInput }`. Both values are
428
+ validated atomically, and destination entry sees the new owner value. A named
429
+ branch can declare the same pair when a resolver needs commands or nested
430
+ construction. Its constructor requires `.update.from(...)` or
431
+ `.update.decoded(...)` before the selection can be returned.
457
432
 
458
433
  ## Test paths and invariants
459
434
 
@@ -30,40 +30,18 @@ Use this split when deciding where code belongs:
30
30
  If a component coordinates a workflow, an atom performs business work, or a
31
31
  provider has no ownership purpose, inspect that code more closely.
32
32
 
33
- ## Remove identity resolvers
33
+ ## Keep ordinary transitions inline
34
34
 
35
- A target that supports default construction does not need a resolver whose only
36
- job is returning `target.from()`.
35
+ Use `{ target: targets.root.Running }` for a state with default construction.
36
+ Use `{ target: targets.root.Running, from: ({ event }) => ({ count: event.count }) }`
37
+ when the destination needs data. The checker rejects missing required fields.
38
+ Use `decoded` for values already decoded by their schema.
37
39
 
38
- ```ts
39
- // Redundant
40
- const handlers = {
41
- Start: (to) =>
42
- to.branch.Running().resolve(({ target }) => target.from())
43
- }
44
-
45
- // Preferred
46
- const handlers = {
47
- Start: (to) => to.branch.Running()
48
- }
49
- ```
50
-
51
- This applies to schema-less states and schemas whose constructor fields are all
52
- optional or defaulted. The type checker rejects the shorter form when the
53
- target needs data.
54
-
55
- Keep `.resolve(...)` when it uses handler context, constructs state data,
56
- updates a retained owner, chooses a branch, declines a transition, or enqueues
57
- commands. For resolver-free reentry, use `.reenter()`:
58
-
59
- ```ts
60
- const handlers = {
61
- Refresh: (to) => to.local.Ready().reenter()
62
- }
63
- ```
64
-
65
- Review check: search for `.resolve(...)` callbacks that only return an empty
66
- `target.from()` and remove the callback.
40
+ Reserve named branch groups and `resolve` for conditional outcomes, commands,
41
+ or nested construction. The group lives in `make`; its resolver selects only
42
+ constructors derived from those declarations. Avoid an extra branch group for
43
+ an ordinary transition. Use `reenter: true` when the source must restart.
44
+ An empty targetless resolver simplifies to `{ none: true }`.
67
45
 
68
46
  ## Choose React ownership or keyed family lookup
69
47
 
@@ -176,29 +154,21 @@ Model `Submit` as the component-facing event. Let a machine state own the work
176
154
  and its lifetime:
177
155
 
178
156
  ```ts
157
+ // In make: effects: { submitOrder }
158
+ // targets is Machine.targets(OrderStates).
179
159
  machine.handle({ states: {
180
160
  Editing: {
181
161
  on: {
182
- Submit: (to) =>
183
- to.branch.Submitting().resolve(({ event, target }) =>
184
- target.from({ order: event.order })
185
- )
162
+ Submit: { target: targets.root.Submitting, from: ({ event }) => ({ order: event.order }) }
186
163
  }
187
164
  },
188
165
  Submitting: {
189
- invoke: (from) =>
190
- from
191
- .effect("submit-order", ({ state }) => submitOrder(state.order))
192
- .onDone((to) =>
193
- to.branch.Complete().resolve(({ output, target }) =>
194
- target.from({ order: output })
195
- )
196
- )
197
- .onFailure((to) =>
198
- to.branch.Failed().resolve(({ error, target }) =>
199
- target.from({ message: String(error) })
200
- )
201
- )
166
+ invoke: {
167
+ src: "submitOrder",
168
+ 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) }) }
171
+ }
202
172
  }
203
173
  } })
204
174
  ```
package/docs/root-api.md CHANGED
@@ -14,10 +14,11 @@ const Root = Machine.state({
14
14
  initial: "Closed",
15
15
  states: { Closed: {}, Open: {} }
16
16
  })
17
+ const targets = Machine.targets(Root)
17
18
  const Door = Machine.make({ root: Root, events: Events }).handle({
18
19
  states: {
19
- Closed: { on: { Open: (to) => to.local.Open() } },
20
- Open: { on: { Close: (to) => to.local.Closed() } }
20
+ Closed: { on: { Open: { target: targets.root.Open } } },
21
+ Open: { on: { Close: { target: targets.root.Closed } } }
21
22
  }
22
23
  })
23
24
  ```
@@ -37,6 +38,7 @@ const Root = Machine.state({
37
38
  }
38
39
  })
39
40
  const Events = Machine.events({ Rename: { title: Schema.String }, Save: {} })
41
+ const targets = Machine.targets(Root)
40
42
  const Editor = Machine.make({
41
43
  root: Root,
42
44
  events: Events,
@@ -44,10 +46,10 @@ const Editor = Machine.make({
44
46
  }).handle({
45
47
  initialize: ({ builder, state }) => builder.from({ draft: state.title }),
46
48
  on: {
47
- Rename: (to) => to.self.update.from(({ event }) => ({ title: event.title }))
49
+ Rename: { update: targets.root, from: ({ event }) => ({ title: event.title }) }
48
50
  },
49
51
  states: {
50
- Editing: { on: { Save: (to) => to.local.Saving() } }
52
+ Editing: { on: { Save: { target: targets.root.Saving } } }
51
53
  }
52
54
  })
53
55
  ```
@@ -72,14 +74,15 @@ The complete snapshot always includes the root:
72
74
  ```
73
75
 
74
76
  Root handlers remain active across child transitions. Child handlers receive
75
- their own state, typed ancestors, and the complete snapshot. `to.root.update`
76
- addresses root data from a child. `to.self.update` addresses the handler owner.
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.
77
80
  Both replace the entire value; omitted fields are not silently retained.
78
81
  Updates preserve the active topology and do not restart scoped work.
79
82
 
80
83
  Simultaneous transitions retain conflict checking. Two parallel handlers cannot
81
- silently overwrite the same owner. Use `.updating(owner)` to combine a selected
82
- destination and a retained owner's value in one atomic transition.
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.
83
86
 
84
87
  ## Schemas and reusable descriptors
85
88
 
@@ -139,75 +142,72 @@ a validated snapshot, including its completion and history metadata.
139
142
 
140
143
  ## Construction, guards, and branches
141
144
 
142
- A selected target supports a direct callback:
145
+ Ordinary transitions declare their destination inline. `from` constructs schema
146
+ make input; `decoded` returns an already decoded value. Omit construction only
147
+ when the selected state can be constructed without arguments.
143
148
 
144
149
  ```ts
145
- Save: (to) => to.local.Saving().from(({ event }) => ({ requestId: event.requestId }))
150
+ Save: {
151
+ target: targets.root.Saving,
152
+ guard: ({ state }) => state.draft.length > 0,
153
+ from: ({ state }) => ({ requestId: state.draft })
154
+ }
146
155
  ```
147
156
 
148
- Use `.decoded(({ state }) => state)` for already decoded schema values. Use
149
- `.resolve((context, enqueue) => ...)` when constructing child configurations or
150
- queuing commands; its `context.target.from(...)` and `.decoded(...)` take values
151
- and explicit child builders.
157
+ A false guard declines the handler and allows ancestor fallback. `{ none: true }`
158
+ accepts an event without changing topology. Initial entry and total choices
159
+ cannot decline. `reenter: true` explicitly restarts the handler source; a value
160
+ update alone retains its lifecycle and cannot request reentry.
152
161
 
153
- A guard runs before construction and commands:
162
+ Use a branch group in `make` for conditional selection, commands, or explicit
163
+ nested construction. The resolver receives constructors derived from that
164
+ exact declaration, so its result cannot introduce another destination.
154
165
 
155
166
  ```ts
156
- Save: (to) => to.local.Saving()
157
- .guard(({ state }) => state.draft.length > 0)
158
- .from(({ state }) => ({ requestId: state.draft }))
159
- ```
160
-
161
- A false guard declines this handler, allowing ancestor fallback. Guards are not
162
- available for mandatory initial resolution or a total choice. `.none` accepts
163
- an event without changing topology; declining and accepting have different
164
- semantics. Named branches keep `{ target, title? }`: `target` identifies the
165
- checked destination and `title` supplies optional presentation metadata.
166
-
167
- Guards also apply to standalone owner updates and combined transitions:
167
+ // In make:
168
+ branches: {
169
+ choose: {
170
+ saving: { target: targets.root.Saving, title: "Save changes" },
171
+ idle: { target: targets.root.Idle }
172
+ }
173
+ }
168
174
 
169
- ```ts
170
- Increment: (to) => to.self.update
171
- .guard(({ current }) => current.count < 10)
172
- .from(({ current }) => ({ ...current, count: current.count + 1 }))
173
-
174
- Save: (to) => to.local.Saving().updating(to.root)
175
- .guard(({ current }) => current.draft.length > 0)
176
- .from(({ current }) => ({
177
- target: { requestId: current.draft },
178
- update: { ...current, attempts: current.attempts + 1 }
179
- }))
175
+ // In handle:
176
+ Choose: {
177
+ branches: "choose",
178
+ resolve: ({ event, select }) => event.save
179
+ ? select.saving.from({ requestId: event.requestId })
180
+ : select.idle.from()
181
+ }
180
182
  ```
181
183
 
182
- Combined `.from` returns constructor inputs for both the destination and the
183
- complete owner replacement. `.decoded` returns their decoded values instead.
184
- For a destination with no construction arguments, use `target: undefined`.
185
- Both values are validated before applying either change; destination entry
186
- observes the updated owner. Use `.resolve` for mixed construction methods,
187
- explicit child configurations, or commands.
184
+ Declare `declinable: true` if a resolver may return `decline()`. Guards and
185
+ resolvers must remain synchronous and deterministic.
188
186
 
189
- Reentry is a modifier before construction:
187
+ An owner update replaces its entire value. A combined transition constructs
188
+ both values atomically:
190
189
 
191
190
  ```ts
192
- Retry: (to) => to.local.Saving().reenter()
193
- .from(({ event }) => ({ requestId: event.requestId }))
194
-
195
- Refresh: (to) => to.none.reenter()
196
-
197
- Choose: (to) => to.branches({
198
- saving: { target: to.local.Saving() },
199
- idle: { target: to.local.Idle() }
200
- }).reenter().resolve(({ state, select }) =>
201
- state.retry ? select.saving.from({ requestId: state.requestId }) : select.idle.from()
202
- )
191
+ Save: {
192
+ target: targets.root.Saving,
193
+ update: targets.root,
194
+ from: ({ root, event }) => ({
195
+ target: { requestId: event.requestId },
196
+ update: { ...root, attempts: root.attempts + 1 }
197
+ })
198
+ }
203
199
  ```
204
200
 
205
- `.reenter()` restarts the handler source. It composes with `.updating`, `.guard`,
206
- `.from`, `.decoded`, and `.resolve` wherever reentry is supported. Apply it to
207
- the whole named-branches builder, whose individual targets describe topology.
208
- Migrate `.resolve(callback, { reenter: true })` to `.reenter().resolve(callback)`;
209
- remove `{ reenter: false }`. `to.self.update` retains the source lifecycle and
210
- does not expose `.reenter()`.
201
+ Destination entry sees the updated owner. Use a named branch with the same
202
+ `{ target, update }` declaration for nested construction or mixed methods:
203
+ `select.saved.from(payload).update.decoded(ownerValue)`.
204
+
205
+ All destination references are paths derived by `Machine.targets(Root)`.
206
+ `target` selects a descendant; `initial` enters a compound or parallel subtree
207
+ using its declared initialization; `history` restores a history reference.
208
+ `update: targets.root` changes root data while preserving active children.
209
+ There is no transition operation that replaces an arbitrary full root
210
+ configuration. Use explicit branches to keep every possible destination visible.
211
211
 
212
212
  ## Completion and history
213
213
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typeonce/effect-machine",
3
- "version": "0.33.0",
3
+ "version": "0.35.0",
4
4
  "description": "Schema-first state machines and statecharts for Effect",
5
5
  "author": "Sandro Maglione",
6
6
  "repository": {