@typeonce/effect-machine 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/dist/Machine.d.ts +113 -136
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/command.d.ts +0 -1
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/command.js +0 -14
- package/dist/internal/machine/command.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +15 -2
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/construction.d.ts +9 -0
- package/dist/internal/machine/construction.d.ts.map +1 -0
- package/dist/internal/machine/construction.js +132 -0
- package/dist/internal/machine/construction.js.map +1 -0
- package/dist/internal/machine/declaration.d.ts +1 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +3 -8
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +9 -7
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +2 -3
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/implementation.js.map +1 -1
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +68 -168
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +63 -17
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/topology.d.ts +6 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +36 -76
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/docs/agent-guide.md +37 -34
- package/docs/root-api.md +143 -190
- package/package.json +1 -1
- package/src/Machine.ts +176 -144
- package/src/internal/machine/command.ts +0 -16
- package/src/internal/machine/configuration.ts +13 -10
- package/src/internal/machine/construction.ts +147 -0
- package/src/internal/machine/declaration.ts +4 -7
- package/src/internal/machine/executionPlan.ts +9 -7
- package/src/internal/machine/implementation.ts +2 -3
- package/src/internal/machine/machine.ts +86 -223
- package/src/internal/machine/planner.ts +69 -18
- package/src/internal/machine/topology.ts +4 -0
- package/src/internal/testing/machine/finiteModel.ts +44 -122
- package/dist/internal/machine/targetBuilder.d.ts +0 -16
- package/dist/internal/machine/targetBuilder.d.ts.map +0 -1
- package/dist/internal/machine/targetBuilder.js +0 -333
- package/dist/internal/machine/targetBuilder.js.map +0 -1
- package/src/internal/machine/targetBuilder.ts +0 -525
|
@@ -28,6 +28,7 @@ import type {
|
|
|
28
28
|
} from "../../Machine.js"
|
|
29
29
|
import * as Activities from "./activities.js"
|
|
30
30
|
import * as Configuration from "./configuration.js"
|
|
31
|
+
import * as Construction from "./construction.js"
|
|
31
32
|
import * as Declaration from "./declaration.js"
|
|
32
33
|
import type { ChildAlreadyExistsError, InfiniteTransitionError, StartupError } from "./errors.js"
|
|
33
34
|
import type { CapturedStateConfig } from "./implementation.js"
|
|
@@ -42,7 +43,6 @@ import * as internalRuntime from "./runtimeProtocol.js"
|
|
|
42
43
|
import * as Serialization from "./serialization.js"
|
|
43
44
|
import * as StateDefinition from "./stateDefinition.js"
|
|
44
45
|
import { ChildMachineLogicTypeId } from "./symbols.js"
|
|
45
|
-
import { getLocalTargetScope, makeTargetBuilder, withFrom } from "./targetBuilder.js"
|
|
46
46
|
import * as TargetReference from "./targetReference.js"
|
|
47
47
|
import * as Topology from "./topology.js"
|
|
48
48
|
|
|
@@ -107,7 +107,6 @@ const makeWithHandlers = (
|
|
|
107
107
|
machine.initial = self.initial
|
|
108
108
|
machine.initialDefinition = self.initialDefinition
|
|
109
109
|
machine.stateNodes = self.stateNodes
|
|
110
|
-
machine.makeTargetBuilder = self.makeTargetBuilder
|
|
111
110
|
machine.handlers = handlers
|
|
112
111
|
Protocol.copyProtocol(self, machine)
|
|
113
112
|
return machine
|
|
@@ -139,20 +138,6 @@ const plainTargetSelection = (selection: Topology.TargetSelection): Topology.Tar
|
|
|
139
138
|
? Topology.noneTargetSelection
|
|
140
139
|
: Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, selection.updatePath)
|
|
141
140
|
|
|
142
|
-
// The authored callback crosses an erased schema boundary here. Each builder
|
|
143
|
-
// retains its construction mode, and planning validates both resulting values.
|
|
144
|
-
const constructSelectionValue = (
|
|
145
|
-
selection: Topology.TargetSelection,
|
|
146
|
-
context: any,
|
|
147
|
-
method: "from" | "decoded",
|
|
148
|
-
value: unknown
|
|
149
|
-
): unknown => {
|
|
150
|
-
if (selection.kind === "update") return context.owner[method](value)
|
|
151
|
-
if (selection.updatePath === undefined) return context.target[method](value)
|
|
152
|
-
const values = value as { readonly target: unknown; readonly update: unknown }
|
|
153
|
-
return context.target[method](values.target).update(context.owner[method](values.update))
|
|
154
|
-
}
|
|
155
|
-
|
|
156
141
|
const transitionTargetSelection = (
|
|
157
142
|
selection: Topology.TargetSelection
|
|
158
143
|
): Machine.TransitionTargetSelection =>
|
|
@@ -167,11 +152,6 @@ const selectionUpdates = (selection: Topology.TargetSelection): ReadonlyArray<st
|
|
|
167
152
|
selection.kind === "update" && selection.path !== undefined ? [selection.path] : []
|
|
168
153
|
: [selection.updatePath]
|
|
169
154
|
|
|
170
|
-
const makeStateUpdateSelection = (
|
|
171
|
-
path: string,
|
|
172
|
-
scope: "local" | "branch"
|
|
173
|
-
): Topology.TargetSelection => Topology.makeTargetSelection("update", path, scope)
|
|
174
|
-
|
|
175
155
|
const captureDefinitionBranch = (
|
|
176
156
|
branch: unknown,
|
|
177
157
|
selector: unknown,
|
|
@@ -206,91 +186,6 @@ const captureDefinitionBranch = (
|
|
|
206
186
|
return { ...(branch as DefinitionBranch), selection }
|
|
207
187
|
}
|
|
208
188
|
|
|
209
|
-
const makeUpdatingConstruction = (
|
|
210
|
-
target: unknown,
|
|
211
|
-
ownerPath: string
|
|
212
|
-
): { readonly update: (update: unknown) => Topology.CombinedTarget } =>
|
|
213
|
-
Object.freeze({
|
|
214
|
-
update: (update: unknown) => {
|
|
215
|
-
if (!Topology.isStateUpdate(update) || update.path !== ownerPath) {
|
|
216
|
-
throw new Error(`Machine combined target must update its declared owner "${ownerPath}"`)
|
|
217
|
-
}
|
|
218
|
-
return Topology.makeCombinedTarget(target, update)
|
|
219
|
-
}
|
|
220
|
-
})
|
|
221
|
-
|
|
222
|
-
const makeUpdatingTargetBuilder = (
|
|
223
|
-
builder: unknown,
|
|
224
|
-
ownerPath: string
|
|
225
|
-
): unknown => {
|
|
226
|
-
if (typeof builder !== "object" || builder === null) {
|
|
227
|
-
throw new Error("Machine combined target requires a state construction builder")
|
|
228
|
-
}
|
|
229
|
-
const updating: Record<PropertyKey, unknown> = {}
|
|
230
|
-
for (const property of Reflect.ownKeys(builder)) {
|
|
231
|
-
const descriptor = Object.getOwnPropertyDescriptor(builder, property)
|
|
232
|
-
if (descriptor === undefined) continue
|
|
233
|
-
if (
|
|
234
|
-
(property === "from" || property === "decoded") && "value" in descriptor && typeof descriptor.value === "function"
|
|
235
|
-
) {
|
|
236
|
-
const construct = descriptor.value
|
|
237
|
-
descriptor.value = (...args: ReadonlyArray<unknown>) => makeUpdatingConstruction(construct(...args), ownerPath)
|
|
238
|
-
}
|
|
239
|
-
Object.defineProperty(updating, property, descriptor)
|
|
240
|
-
}
|
|
241
|
-
return Object.freeze(updating)
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
const getSelectionBuilder = (
|
|
245
|
-
target: Record<string, any>,
|
|
246
|
-
selection: Topology.TargetSelection,
|
|
247
|
-
stateNodes: Machine.StateNodes,
|
|
248
|
-
source: string
|
|
249
|
-
): unknown => {
|
|
250
|
-
if (selection.kind === "none") return target.none
|
|
251
|
-
if (selection.kind === "update") {
|
|
252
|
-
return withFrom(
|
|
253
|
-
(value: unknown) => Topology.makeStateUpdate(selection.path!, value),
|
|
254
|
-
"leaf",
|
|
255
|
-
true
|
|
256
|
-
)
|
|
257
|
-
}
|
|
258
|
-
let builder: any
|
|
259
|
-
let parts = selection.path!.split(".")
|
|
260
|
-
if (selection.kind === "history") {
|
|
261
|
-
builder = target.history
|
|
262
|
-
if (selection.path !== "") parts.unshift("")
|
|
263
|
-
} else if (selection.scope === "local") {
|
|
264
|
-
builder = target.local
|
|
265
|
-
const scope = getLocalTargetScope(stateNodes, source)
|
|
266
|
-
if (scope !== undefined) {
|
|
267
|
-
if (selection.path === scope) {
|
|
268
|
-
builder = builder.with
|
|
269
|
-
parts = []
|
|
270
|
-
} else {
|
|
271
|
-
parts = (scope === "" ? selection.path! : selection.path!.slice(scope.length + 1)).split(".")
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
} else if (selection.scope === "branch") {
|
|
275
|
-
builder = target.branch
|
|
276
|
-
if (selection.path !== "") parts.unshift("")
|
|
277
|
-
} else {
|
|
278
|
-
builder = target.full
|
|
279
|
-
}
|
|
280
|
-
for (const part of parts) builder = builder[part]
|
|
281
|
-
if (selection.kind === "initial") builder = builder.initial
|
|
282
|
-
if (
|
|
283
|
-
typeof builder !== "function" &&
|
|
284
|
-
(typeof builder !== "object" || builder === null || typeof builder.from !== "function")
|
|
285
|
-
) {
|
|
286
|
-
throw new Error(`Machine could not construct selected transition target "${selection.path}"`)
|
|
287
|
-
}
|
|
288
|
-
return selection.updatePath === undefined ? builder : makeUpdatingTargetBuilder(builder, selection.updatePath)
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
const constructSelectedTarget = (builder: any): unknown =>
|
|
292
|
-
typeof builder?.from === "function" ? builder.from() : builder()
|
|
293
|
-
|
|
294
189
|
const validateResolvedSelection = (
|
|
295
190
|
result: unknown,
|
|
296
191
|
selection: Topology.TargetSelection,
|
|
@@ -341,37 +236,13 @@ const runCapturedBranch = (
|
|
|
341
236
|
stateNodes: Machine.StateNodes,
|
|
342
237
|
source: string
|
|
343
238
|
): unknown => {
|
|
344
|
-
const selectedTarget = getSelectionBuilder(context.target, branch.selection, stateNodes, source)
|
|
345
|
-
if (branch.resolve === undefined) return constructSelectedTarget(selectedTarget)
|
|
346
|
-
// This fresh context is owned by this evaluation and can be retained by user callbacks.
|
|
347
239
|
const resolverContext: Record<string, any> = {
|
|
348
240
|
...context,
|
|
349
241
|
root: source === "" ? context.state : context.ancestors[""],
|
|
350
242
|
decline: Topology.makeDeclined
|
|
351
243
|
}
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
if (branch.selection.kind === "update") {
|
|
355
|
-
const ownerPath = branch.selection.path!
|
|
356
|
-
delete resolverContext.target
|
|
357
|
-
resolverContext.current = context.ancestors[ownerPath] ?? context.state
|
|
358
|
-
resolverContext.owner = getSelectionBuilder(
|
|
359
|
-
context.target,
|
|
360
|
-
makeStateUpdateSelection(ownerPath, branch.selection.scope === "local" ? "local" : "branch"),
|
|
361
|
-
stateNodes,
|
|
362
|
-
source
|
|
363
|
-
)
|
|
364
|
-
} else if (branch.selection.updatePath !== undefined) {
|
|
365
|
-
const ownerPath = branch.selection.updatePath
|
|
366
|
-
resolverContext.current = context.ancestors[ownerPath]
|
|
367
|
-
resolverContext.owner = getSelectionBuilder(
|
|
368
|
-
context.target,
|
|
369
|
-
makeStateUpdateSelection(ownerPath, "branch"),
|
|
370
|
-
stateNodes,
|
|
371
|
-
source
|
|
372
|
-
)
|
|
373
|
-
}
|
|
374
|
-
const resolved = branch.resolve(resolverContext, enqueue)
|
|
244
|
+
delete resolverContext.target
|
|
245
|
+
const resolved = branch.resolve!(resolverContext, enqueue)
|
|
375
246
|
if (Topology.isDeclined(resolved)) {
|
|
376
247
|
if (branch.declinable !== true) {
|
|
377
248
|
throw new Error(`Machine transition for state "${source}" returned decline without declaring declinable: true`)
|
|
@@ -379,7 +250,7 @@ const runCapturedBranch = (
|
|
|
379
250
|
return resolved
|
|
380
251
|
}
|
|
381
252
|
validateResolvedSelection(resolved, branch.selection, stateNodes)
|
|
382
|
-
return resolved
|
|
253
|
+
return resolved
|
|
383
254
|
}
|
|
384
255
|
|
|
385
256
|
const topologyTargetPath = (selection: Topology.TargetSelection): string | undefined =>
|
|
@@ -426,92 +297,59 @@ const captureNamedBranches = (
|
|
|
426
297
|
}))
|
|
427
298
|
}
|
|
428
299
|
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
300
|
+
const constructBranch = (
|
|
301
|
+
selection: Topology.TargetSelection,
|
|
302
|
+
stateNodes: Machine.StateNodes,
|
|
303
|
+
declaration: Declaration.Declaration,
|
|
304
|
+
raw: unknown = {},
|
|
305
|
+
source?: string
|
|
435
306
|
): unknown => {
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
method
|
|
445
|
-
) => [method, (value: unknown) =>
|
|
446
|
-
Topology.makeSelectedBranch(
|
|
447
|
-
owner,
|
|
448
|
-
branchIndex,
|
|
449
|
-
branchKey,
|
|
450
|
-
result.update(updateBuilder[method]!(value))
|
|
451
|
-
)]
|
|
452
|
-
)
|
|
453
|
-
)
|
|
454
|
-
)
|
|
455
|
-
})
|
|
456
|
-
}
|
|
457
|
-
return Topology.makeSelectedBranch(owner, branchIndex, branchKey, result)
|
|
458
|
-
}
|
|
459
|
-
for (const property of Reflect.ownKeys(builder)) {
|
|
460
|
-
if (
|
|
461
|
-
property === "length" || property === "name" || property === "prototype" || property === "caller" ||
|
|
462
|
-
property === "arguments"
|
|
463
|
-
) continue
|
|
464
|
-
const descriptor = Object.getOwnPropertyDescriptor(builder, property)
|
|
465
|
-
if (descriptor === undefined) continue
|
|
466
|
-
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
467
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder)
|
|
468
|
-
}
|
|
469
|
-
Object.defineProperty(wrapped, property, descriptor)
|
|
470
|
-
}
|
|
471
|
-
return wrapped
|
|
307
|
+
const config = Construction.record(raw)
|
|
308
|
+
if (selection.kind === "none" || selection.kind === "history" || selection.kind === "choice") {
|
|
309
|
+
if (Reflect.ownKeys(config).length > 0) throw new Error("Machine pseudo-state construction accepts no data")
|
|
310
|
+
if (selection.kind === "none") return undefined
|
|
311
|
+
const node = stateNodes.byPath.get(selection.path!)!
|
|
312
|
+
return selection.kind === "history" ?
|
|
313
|
+
Topology.makeHistoryTarget(node.path, node.parent!)
|
|
314
|
+
: Topology.makeChoiceTarget(node.path, node.parent!)
|
|
472
315
|
}
|
|
473
|
-
if (
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
if (descriptor === undefined) continue
|
|
478
|
-
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
479
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder)
|
|
480
|
-
}
|
|
481
|
-
Object.defineProperty(wrapped, property, descriptor)
|
|
316
|
+
if (selection.kind === "update") return Construction.update(stateNodes, selection.path!, config)
|
|
317
|
+
if (selection.path === "") {
|
|
318
|
+
if (Reflect.ownKeys(config).some((key) => key !== "input")) {
|
|
319
|
+
throw new Error("Machine root targets accept input only")
|
|
482
320
|
}
|
|
483
|
-
|
|
321
|
+
if (declaration.initialize === undefined) throw new Error("Machine root initialization is unavailable")
|
|
322
|
+
return declaration.initialize(config.input)
|
|
484
323
|
}
|
|
485
|
-
|
|
324
|
+
if (selection.updatePath !== undefined) {
|
|
325
|
+
const { update, ...state } = config
|
|
326
|
+
return Topology.makeCombinedTarget(
|
|
327
|
+
Construction.target(stateNodes, selection.path!, state, source),
|
|
328
|
+
Construction.update(stateNodes, selection.updatePath, update)
|
|
329
|
+
)
|
|
330
|
+
}
|
|
331
|
+
return Construction.target(stateNodes, selection.path!, config, source)
|
|
486
332
|
}
|
|
487
333
|
|
|
488
334
|
const makeBranchSelectors = (
|
|
489
|
-
context: Record<string, any>,
|
|
490
335
|
branches: ReadonlyArray<CapturedNamedBranch>,
|
|
491
336
|
owner: object,
|
|
492
337
|
stateNodes: Machine.StateNodes,
|
|
338
|
+
declaration: Declaration.Declaration,
|
|
493
339
|
source: string
|
|
494
|
-
): Readonly<Record<string, unknown>> =>
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
: getSelectionBuilder(
|
|
506
|
-
context.target,
|
|
507
|
-
makeStateUpdateSelection(branch.selection.updatePath, "branch"),
|
|
508
|
-
stateNodes,
|
|
509
|
-
source
|
|
510
|
-
) as Record<string, (value: unknown) => unknown>
|
|
340
|
+
): Readonly<Record<string, unknown>> =>
|
|
341
|
+
Object.freeze(Object.fromEntries(
|
|
342
|
+
branches.map((branch, index) => [branch.key, (config?: unknown) =>
|
|
343
|
+
Topology.makeSelectedBranch(
|
|
344
|
+
owner,
|
|
345
|
+
index,
|
|
346
|
+
branch.key,
|
|
347
|
+
branch.selection.kind === "none"
|
|
348
|
+
? Topology.makeNoTarget()
|
|
349
|
+
: constructBranch(branch.selection, stateNodes, declaration, config, source)
|
|
350
|
+
)]
|
|
511
351
|
)
|
|
512
|
-
|
|
513
|
-
return Object.freeze(select)
|
|
514
|
-
}
|
|
352
|
+
))
|
|
515
353
|
|
|
516
354
|
const validateSelectedBranchResult = (
|
|
517
355
|
result: unknown,
|
|
@@ -547,7 +385,7 @@ const normalizeObjectTransition = (
|
|
|
547
385
|
const allowed = [
|
|
548
386
|
"target",
|
|
549
387
|
"update",
|
|
550
|
-
"
|
|
388
|
+
"input",
|
|
551
389
|
"history",
|
|
552
390
|
"none",
|
|
553
391
|
"branches",
|
|
@@ -586,7 +424,7 @@ const normalizeObjectTransition = (
|
|
|
586
424
|
throw new Error("Machine branching transition requires a registered group and resolver")
|
|
587
425
|
}
|
|
588
426
|
if (
|
|
589
|
-
["target", "update", "
|
|
427
|
+
["target", "update", "input", "history", "none", "data", "decoded"].some((key) => config[key] !== undefined)
|
|
590
428
|
) throw new Error("Machine branching transition cannot redeclare its destination")
|
|
591
429
|
const entries = Object.fromEntries(
|
|
592
430
|
Object.entries(group).map(([key, spec]) => [key, {
|
|
@@ -613,21 +451,41 @@ const normalizeObjectTransition = (
|
|
|
613
451
|
if (resolve !== undefined && selection.kind !== "none") {
|
|
614
452
|
throw new Error("Machine advanced construction requires a declared branch group")
|
|
615
453
|
}
|
|
616
|
-
const
|
|
454
|
+
const input = config.input
|
|
455
|
+
const constructInput = typeof input === "function" ? input : () => input
|
|
456
|
+
if (selection.path !== "" && Object.hasOwn(config, "input")) {
|
|
457
|
+
throw new Error("Machine input belongs only to root targets")
|
|
458
|
+
}
|
|
459
|
+
if (selection.path === "" && selection.kind !== "update" && (hasData || config.decoded !== undefined)) {
|
|
460
|
+
throw new Error("Machine root targets accept input instead of data")
|
|
461
|
+
}
|
|
617
462
|
return {
|
|
618
463
|
target: () => selection,
|
|
619
464
|
reenter: config.reenter,
|
|
620
465
|
declinable,
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
}
|
|
627
|
-
if (resolve !== undefined) return resolve(context, enqueue)
|
|
628
|
-
return selection.kind === "none" ? undefined : constructSelectedTarget(context.target)
|
|
466
|
+
resolve: (context: Record<string, any>, enqueue: unknown) => {
|
|
467
|
+
if (guarded && !guard(context)) return Topology.makeDeclined()
|
|
468
|
+
if (resolve !== undefined) return resolve(context, enqueue)
|
|
469
|
+
if (selection.path === "" && selection.kind !== "update") {
|
|
470
|
+
return constructBranch(selection, stateNodes, declaration, { input: constructInput(context) })
|
|
629
471
|
}
|
|
630
|
-
|
|
472
|
+
const value = construct(context)
|
|
473
|
+
return constructBranch(
|
|
474
|
+
selection,
|
|
475
|
+
stateNodes,
|
|
476
|
+
declaration,
|
|
477
|
+
selection.updatePath === undefined
|
|
478
|
+
? {
|
|
479
|
+
...(hasData ? { data: value } : {}),
|
|
480
|
+
...(config.decoded === undefined ? {} : { decoded: config.decoded })
|
|
481
|
+
}
|
|
482
|
+
: {
|
|
483
|
+
...(value.target === undefined ? {} : { data: value.target, decoded: config.decoded }),
|
|
484
|
+
update: { data: value.update, decoded: config.decoded }
|
|
485
|
+
},
|
|
486
|
+
path
|
|
487
|
+
)
|
|
488
|
+
}
|
|
631
489
|
}
|
|
632
490
|
}
|
|
633
491
|
|
|
@@ -662,7 +520,7 @@ const captureTransition = (
|
|
|
662
520
|
decline: Topology.makeDeclined
|
|
663
521
|
}
|
|
664
522
|
delete resolverContext.target
|
|
665
|
-
resolverContext.select = makeBranchSelectors(
|
|
523
|
+
resolverContext.select = makeBranchSelectors(branches, owner, stateNodes, declaration, path)
|
|
666
524
|
const selected = resolve(resolverContext, enqueue)
|
|
667
525
|
if (Topology.isDeclined(selected)) {
|
|
668
526
|
if (!declinable) {
|
|
@@ -863,14 +721,19 @@ const makeHandle = (self: Definition.Any, declaration: Declaration.Declaration):
|
|
|
863
721
|
const constructRoot = compiled.stateNodes.byPath.get("").schema === undefined
|
|
864
722
|
? () => undefined
|
|
865
723
|
: InitialDeclaration.construction(config.root)
|
|
866
|
-
compiled.initial = (input: unknown) =>
|
|
724
|
+
compiled.initial = (input: unknown) => ({
|
|
725
|
+
...Topology.makeInitialTarget("", constructRoot({ input })),
|
|
726
|
+
input: { value: input }
|
|
727
|
+
})
|
|
867
728
|
compiled.initialDefinition = Object.freeze({
|
|
868
729
|
target: "",
|
|
869
730
|
selection: Object.freeze({ path: "", kind: "initial", scope: "initial" })
|
|
870
731
|
})
|
|
871
|
-
compiled.makeTargetBuilder = makeTargetBuilder(compiled.states, compiled.stateNodes)
|
|
872
732
|
const handlers: Record<PropertyKey, CapturedStateConfig> = Object.create(null)
|
|
873
|
-
flattenHandlers(handlers, compiled.stateNodes, compiled.states, "",
|
|
733
|
+
flattenHandlers(handlers, compiled.stateNodes, compiled.states, "", {
|
|
734
|
+
...declaration,
|
|
735
|
+
initialize: (input) => compiled.initial(Protocol.decodeInputSync(compiled, compiled.input, input))
|
|
736
|
+
}, { "": captured.handlers })
|
|
874
737
|
return makeWithHandlers(compiled, handlers)
|
|
875
738
|
}) as Definition.Any["handle"]
|
|
876
739
|
|
|
@@ -8,7 +8,7 @@ import * as Cause from "effect/Cause"
|
|
|
8
8
|
import * as Effect from "effect/Effect"
|
|
9
9
|
import type * as Schema from "effect/Schema"
|
|
10
10
|
import type { Enqueue, InitialEvent as MachineInitialEvent, Machine, MachineTarget } from "../../Machine.js"
|
|
11
|
-
import {
|
|
11
|
+
import { makeCollector, type RuntimeCommand } from "./command.js"
|
|
12
12
|
import {
|
|
13
13
|
type ActiveConfiguration,
|
|
14
14
|
captureHistory,
|
|
@@ -37,6 +37,7 @@ import {
|
|
|
37
37
|
snapshotFromConfiguration,
|
|
38
38
|
snapshotFromConfigurationAtPath
|
|
39
39
|
} from "./configuration.js"
|
|
40
|
+
import * as Construction from "./construction.js"
|
|
40
41
|
import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
|
|
41
42
|
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
42
43
|
import { isDataInitializer } from "./initialDeclaration.js"
|
|
@@ -265,7 +266,8 @@ const collectStateInitializer = (
|
|
|
265
266
|
const completeHistoryConfiguration = (
|
|
266
267
|
machine: Machine.Any,
|
|
267
268
|
configuration: ActiveConfiguration,
|
|
268
|
-
event: unknown
|
|
269
|
+
event: unknown,
|
|
270
|
+
input?: { readonly value: unknown }
|
|
269
271
|
) => {
|
|
270
272
|
const active = new Set(configuration.active)
|
|
271
273
|
const values = new Map(configuration.values)
|
|
@@ -345,6 +347,7 @@ const completeHistoryConfiguration = (
|
|
|
345
347
|
containingState: getParentValue(machine, current, path),
|
|
346
348
|
ancestors: getParentValues(machine, current, path),
|
|
347
349
|
event,
|
|
350
|
+
...(path === "" && input !== undefined ? { input: input.value } : {}),
|
|
348
351
|
...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
|
|
349
352
|
})
|
|
350
353
|
const initializedValues = getStateInitializeValues(path, initialized.value)
|
|
@@ -373,6 +376,7 @@ const completeHistoryConfiguration = (
|
|
|
373
376
|
containingState: getParentValue(machine, current, path),
|
|
374
377
|
ancestors: getParentValues(machine, current, path),
|
|
375
378
|
event,
|
|
379
|
+
...(path === "" && input !== undefined ? { input: input.value } : {}),
|
|
376
380
|
...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
|
|
377
381
|
})
|
|
378
382
|
const initializedValues = initialized === undefined
|
|
@@ -432,8 +436,53 @@ export function resolveInitialConfiguration(
|
|
|
432
436
|
target: InitialTargetInstruction,
|
|
433
437
|
event: unknown
|
|
434
438
|
) {
|
|
435
|
-
|
|
436
|
-
|
|
439
|
+
let partial = configurationFromInitialTargetSync(machine, configuration, target)
|
|
440
|
+
if (
|
|
441
|
+
target.children === undefined ||
|
|
442
|
+
![...target.children.keys()].some((path) => getNode(machine, path).type === "choice")
|
|
443
|
+
) {
|
|
444
|
+
return completeHistoryConfiguration(machine, partial, event, target.input)
|
|
445
|
+
}
|
|
446
|
+
const commands: Array<RuntimeCommand> = []
|
|
447
|
+
const raisedEvents: Array<unknown> = []
|
|
448
|
+
const emittedEvents: Array<unknown> = []
|
|
449
|
+
const transitions: Array<ResolvedChoiceTransition> = []
|
|
450
|
+
for (const path of target.children?.keys() ?? []) {
|
|
451
|
+
const node = getNode(machine, path)
|
|
452
|
+
if (node.type !== "choice") continue
|
|
453
|
+
const choice = resolveChoiceTarget(machine, partial, makeChoiceTarget(path, node.parent!), event)
|
|
454
|
+
for (const routed of [choice.target, ...choice.additionalTargets]) {
|
|
455
|
+
const resolved = isInitialTarget(routed) ?
|
|
456
|
+
resolveInitialTarget(machine, partial, routed, event)
|
|
457
|
+
: isHistoryTarget(routed)
|
|
458
|
+
? resolveHistoryTarget(machine, partial, routed, event)
|
|
459
|
+
: undefined
|
|
460
|
+
partial = normalizeTargetConfigurationSync(
|
|
461
|
+
machine,
|
|
462
|
+
partial,
|
|
463
|
+
(resolved?.target ?? routed) as Machine.Target<any, any>
|
|
464
|
+
)
|
|
465
|
+
if (resolved !== undefined) {
|
|
466
|
+
commands.push(...resolved.commands)
|
|
467
|
+
raisedEvents.push(...resolved.raisedEvents)
|
|
468
|
+
emittedEvents.push(...resolved.emittedEvents)
|
|
469
|
+
transitions.push(...resolved.transitions)
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
commands.push(...choice.commands)
|
|
473
|
+
raisedEvents.push(...choice.raisedEvents)
|
|
474
|
+
emittedEvents.push(...choice.emittedEvents)
|
|
475
|
+
transitions.push(...choice.transitions)
|
|
476
|
+
}
|
|
477
|
+
const completed = completeHistoryConfiguration(machine, partial, event, target.input)
|
|
478
|
+
if (transitions.length === 0) return completed
|
|
479
|
+
return {
|
|
480
|
+
...completed,
|
|
481
|
+
commands: [...commands, ...completed.commands],
|
|
482
|
+
raisedEvents: [...raisedEvents, ...completed.raisedEvents],
|
|
483
|
+
emittedEvents: [...emittedEvents, ...completed.emittedEvents],
|
|
484
|
+
transitions: [...transitions, ...completed.transitions]
|
|
485
|
+
}
|
|
437
486
|
}
|
|
438
487
|
|
|
439
488
|
export function resolveInitialTarget(
|
|
@@ -447,7 +496,7 @@ export function resolveInitialTarget(
|
|
|
447
496
|
return {
|
|
448
497
|
target: makeTarget(target.path as any, snapshot.value as any, {
|
|
449
498
|
snapshot: snapshot as any,
|
|
450
|
-
values:
|
|
499
|
+
values: target.values as any
|
|
451
500
|
}),
|
|
452
501
|
commands: completed.commands,
|
|
453
502
|
raisedEvents: completed.raisedEvents,
|
|
@@ -495,7 +544,7 @@ function resolveHistoryTarget(
|
|
|
495
544
|
}
|
|
496
545
|
const collected = collectTransition(machine, fallback, {
|
|
497
546
|
event,
|
|
498
|
-
target:
|
|
547
|
+
target: (config: unknown) => Construction.snapshot(machine.stateNodes, config),
|
|
499
548
|
owner: target.parent
|
|
500
549
|
})
|
|
501
550
|
if (collected.state === undefined || isHistoryTarget(collected.state) || !isSnapshot(collected.state)) {
|
|
@@ -732,8 +781,7 @@ const makeTransitionContext = <
|
|
|
732
781
|
containingState: getParentValue(machine, configuration, path) as Machine.ParentStateValue<States, StateId>,
|
|
733
782
|
ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
|
|
734
783
|
event,
|
|
735
|
-
snapshot
|
|
736
|
-
target: getTargetBuilder(machine, path)
|
|
784
|
+
snapshot
|
|
737
785
|
})
|
|
738
786
|
|
|
739
787
|
const makeDoneContext = <
|
|
@@ -755,8 +803,7 @@ const makeDoneContext = <
|
|
|
755
803
|
ancestors: getParentValues(machine, configuration, path) as Machine.ParentStateValues<States, StateId>,
|
|
756
804
|
event,
|
|
757
805
|
output: output as Machine.CompletionOutputByIdentifier<States, StateId>,
|
|
758
|
-
snapshot
|
|
759
|
-
target: getTargetBuilder(machine, path)
|
|
806
|
+
snapshot
|
|
760
807
|
})
|
|
761
808
|
|
|
762
809
|
const collectStateActions = <
|
|
@@ -859,8 +906,7 @@ const selectAlwaysTransitions = <
|
|
|
859
906
|
Machine.StateIdentifier<States>
|
|
860
907
|
>,
|
|
861
908
|
event,
|
|
862
|
-
snapshot: capturedSnapshot()
|
|
863
|
-
target: getTargetBuilder(machine, path)
|
|
909
|
+
snapshot: capturedSnapshot()
|
|
864
910
|
}
|
|
865
911
|
})
|
|
866
912
|
evaluatedSources.set(path, candidate)
|
|
@@ -1068,7 +1114,6 @@ const selectInvocationTransition = <
|
|
|
1068
1114
|
containingState: getParentValue(machine, configuration, event.path),
|
|
1069
1115
|
ancestors: getParentValues(machine, configuration, event.path),
|
|
1070
1116
|
snapshot,
|
|
1071
|
-
target: getTargetBuilder(machine, event.path),
|
|
1072
1117
|
id: event.id,
|
|
1073
1118
|
...(event.type === "element"
|
|
1074
1119
|
? { element: event.element }
|
|
@@ -1244,6 +1289,7 @@ const withChoiceValues = (target: unknown, values: Readonly<Record<string, unkno
|
|
|
1244
1289
|
if (isChoiceTarget(target)) {
|
|
1245
1290
|
return makeChoiceTarget(target.path, target.parent, { ...values, ...(target.values ?? {}) })
|
|
1246
1291
|
}
|
|
1292
|
+
if (isInitialTarget(target)) return { ...target, values: { ...values, ...target.values } }
|
|
1247
1293
|
if (!isTarget(target) || Object.keys(values).length === 0) return target
|
|
1248
1294
|
const snapshot = target[TargetSnapshotTypeId]
|
|
1249
1295
|
return makeTarget(target.path, target.value, {
|
|
@@ -1324,8 +1370,7 @@ function resolveChoiceTarget(
|
|
|
1324
1370
|
...resolveMachineReferences(machine, provisional),
|
|
1325
1371
|
containingState: getParentValue(machine, provisional, node.path),
|
|
1326
1372
|
ancestors: getParentValues(machine, provisional, node.path),
|
|
1327
|
-
event
|
|
1328
|
-
target: getTargetBuilder(machine, node.path)
|
|
1373
|
+
event
|
|
1329
1374
|
},
|
|
1330
1375
|
choice.evaluate
|
|
1331
1376
|
)
|
|
@@ -1419,17 +1464,23 @@ const collectEvaluatedTransition = <
|
|
|
1419
1464
|
}
|
|
1420
1465
|
})()
|
|
1421
1466
|
: undefined
|
|
1467
|
+
// Initial constructors and choices observe the owner value being committed
|
|
1468
|
+
// with this target, while the source configuration remains untouched.
|
|
1469
|
+
const constructionState =
|
|
1470
|
+
update !== undefined && (isInitialTarget(unresolvedTarget) || isChoiceTarget(unresolvedTarget))
|
|
1471
|
+
? { ...state, values: new Map(state.values).set(update.path, update.value) }
|
|
1472
|
+
: state
|
|
1422
1473
|
const choiceResolution = unresolvedTarget === undefined
|
|
1423
1474
|
? undefined
|
|
1424
1475
|
: resolveChoiceTarget(
|
|
1425
1476
|
machine,
|
|
1426
|
-
|
|
1477
|
+
constructionState,
|
|
1427
1478
|
unresolvedTarget,
|
|
1428
1479
|
(selection.context as any).event
|
|
1429
1480
|
)
|
|
1430
1481
|
const choiceResolvedTarget = choiceResolution?.target ?? unresolvedTarget
|
|
1431
1482
|
const initialResolution = choiceResolvedTarget !== undefined && isInitialTarget(choiceResolvedTarget)
|
|
1432
|
-
? resolveInitialTarget(machine,
|
|
1483
|
+
? resolveInitialTarget(machine, constructionState, choiceResolvedTarget, (selection.context as any).event)
|
|
1433
1484
|
: undefined
|
|
1434
1485
|
const routedTarget = initialResolution?.target ?? choiceResolvedTarget
|
|
1435
1486
|
let historyResolution: {
|
|
@@ -2321,4 +2372,4 @@ export const planInitial = (
|
|
|
2321
2372
|
|
|
2322
2373
|
// Captured constructors are private planner inputs. Public callbacks receive root data
|
|
2323
2374
|
// and the constructors bound to their declared branches during normalization.
|
|
2324
|
-
type ConstructionContext<C> = Omit<C, "root">
|
|
2375
|
+
type ConstructionContext<C> = Omit<C, "root">
|
|
@@ -55,6 +55,10 @@ export interface HistoryTarget {
|
|
|
55
55
|
export interface InitialTarget {
|
|
56
56
|
readonly [InitialTargetTypeId]: typeof InitialTargetTypeId
|
|
57
57
|
readonly _tag: "InitialTarget"
|
|
58
|
+
/** Transient startup input, never retained in a configuration. */
|
|
59
|
+
readonly input?: { readonly value: unknown }
|
|
60
|
+
/** Explicitly selected descendants and their construction values. */
|
|
61
|
+
readonly children?: ReadonlyMap<string, unknown>
|
|
58
62
|
readonly path: string
|
|
59
63
|
readonly value: unknown
|
|
60
64
|
readonly values?: Readonly<Record<string, unknown>>
|