@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.
- package/README.md +272 -628
- package/dist/Machine.d.ts +1076 -896
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +141 -90
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/childRegistry.d.ts +1 -1
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/cluster.d.ts +2 -50
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +4 -27
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts +35 -0
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
- package/dist/internal/machine/clusterProtocol.js +28 -0
- package/dist/internal/machine/clusterProtocol.js.map +1 -0
- package/dist/internal/machine/commandRuntime.d.ts +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/configuration.d.ts +2 -2
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -64
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +17 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -0
- package/dist/internal/machine/declaration.js +120 -0
- package/dist/internal/machine/declaration.js.map +1 -0
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +15 -22
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +34 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -0
- package/dist/internal/machine/implementation.js +3 -0
- package/dist/internal/machine/implementation.js.map +1 -0
- 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/invocation.d.ts +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +1 -1
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
- package/dist/internal/machine/machine.d.ts +13 -4
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +169 -388
- 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 +37 -78
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +8 -7
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +16 -12
- package/dist/internal/machine/process.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/runtime.d.ts +2 -230
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +8 -1857
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
- package/dist/internal/machine/runtimeCompiled.js +867 -0
- package/dist/internal/machine/runtimeCompiled.js.map +1 -0
- package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
- package/dist/internal/machine/runtimeGeneric.js +471 -0
- package/dist/internal/machine/runtimeGeneric.js.map +1 -0
- package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
- package/dist/internal/machine/runtimeProtocol.js +561 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -0
- 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 +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +41 -0
- package/dist/internal/machine/targetReference.js.map +1 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +5 -3
- 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 +53 -46
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.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 +18 -13
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +18 -25
- 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/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +62 -62
- package/package.json +1 -1
- package/src/Machine.ts +1836 -2907
- package/src/internal/machine/childRegistry.ts +1 -1
- package/src/internal/machine/cluster.ts +7 -73
- package/src/internal/machine/clusterProtocol.ts +56 -0
- package/src/internal/machine/commandRuntime.ts +1 -1
- package/src/internal/machine/configuration.ts +53 -65
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +16 -22
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +225 -576
- package/src/internal/machine/planner.ts +84 -118
- package/src/internal/machine/process.ts +40 -33
- package/src/internal/machine/protocol.ts +3 -3
- package/src/internal/machine/runtime.ts +20 -3037
- package/src/internal/machine/runtimeCompiled.ts +1166 -0
- package/src/internal/machine/runtimeGeneric.ts +799 -0
- package/src/internal/machine/runtimeProtocol.ts +1213 -0
- package/src/internal/machine/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +55 -0
- package/src/internal/machine/topology.ts +5 -3
- package/src/internal/testing/machine/finiteModel.ts +58 -53
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +22 -25
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
package/docs/agent-guide.md
CHANGED
|
@@ -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:
|
|
48
|
+
Start: { target: targets.root.Running, from: () => ({ count: 0 }) }
|
|
47
49
|
}
|
|
48
50
|
},
|
|
49
51
|
Running: {
|
|
50
52
|
on: {
|
|
51
|
-
Increment:
|
|
52
|
-
Stop:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
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:
|
|
328
|
-
|
|
329
|
-
|
|
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
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
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
|
-
|
|
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:
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
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
|
|
414
|
-
|
|
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:
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
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
|
|
package/docs/machine-review.md
CHANGED
|
@@ -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
|
-
##
|
|
33
|
+
## Keep ordinary transitions inline
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
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: (
|
|
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:
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
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:
|
|
20
|
-
Open: { on: { Close:
|
|
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:
|
|
49
|
+
Rename: { update: targets.root, from: ({ event }) => ({ title: event.title }) }
|
|
48
50
|
},
|
|
49
51
|
states: {
|
|
50
|
-
Editing: { on: { Save:
|
|
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.
|
|
76
|
-
addresses root data from a child.
|
|
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
|
|
82
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
and
|
|
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
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
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
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
183
|
-
|
|
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
|
-
|
|
187
|
+
An owner update replaces its entire value. A combined transition constructs
|
|
188
|
+
both values atomically:
|
|
190
189
|
|
|
191
190
|
```ts
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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
|
|