@typeonce/effect-machine 0.34.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -33
- package/dist/Machine.d.ts +408 -483
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +126 -87
- 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 +0 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +15 -21
- 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 +21 -27
- 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/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/machine.d.ts +0 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +101 -282
- 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 +86 -82
- package/dist/internal/machine/planner.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/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.map +1 -1
- package/dist/internal/machine/targetReference.js +14 -4
- package/dist/internal/machine/targetReference.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 +3 -2
- 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 +51 -82
- package/dist/internal/testing/machine/finiteModel.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 +13 -8
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +13 -8
- 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 +37 -34
- package/docs/root-api.md +143 -190
- package/package.json +1 -1
- package/src/Machine.ts +631 -872
- package/src/internal/machine/command.ts +0 -16
- package/src/internal/machine/configuration.ts +13 -30
- package/src/internal/machine/construction.ts +147 -0
- package/src/internal/machine/declaration.ts +4 -7
- package/src/internal/machine/executionPlan.ts +20 -25
- package/src/internal/machine/implementation.ts +2 -3
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/machine.ts +118 -396
- package/src/internal/machine/planner.ts +97 -99
- package/src/internal/machine/protocol.ts +3 -3
- package/src/internal/machine/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +19 -4
- package/src/internal/machine/topology.ts +7 -2
- package/src/internal/testing/machine/finiteModel.ts +60 -132
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +13 -8
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
- 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,9 +28,11 @@ 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"
|
|
35
|
+
import * as InitialDeclaration from "./initialDeclaration.js"
|
|
34
36
|
import * as InvocationDefinition from "./invocationDefinition.js"
|
|
35
37
|
import * as internalPlanner from "./planner.js"
|
|
36
38
|
import * as internalProcess from "./process.js"
|
|
@@ -41,13 +43,6 @@ import * as internalRuntime from "./runtimeProtocol.js"
|
|
|
41
43
|
import * as Serialization from "./serialization.js"
|
|
42
44
|
import * as StateDefinition from "./stateDefinition.js"
|
|
43
45
|
import { ChildMachineLogicTypeId } from "./symbols.js"
|
|
44
|
-
import {
|
|
45
|
-
getLocalTargetScope,
|
|
46
|
-
getTargetBuilderNode,
|
|
47
|
-
makeSnapshotBuilder,
|
|
48
|
-
makeTargetBuilder,
|
|
49
|
-
withFrom
|
|
50
|
-
} from "./targetBuilder.js"
|
|
51
46
|
import * as TargetReference from "./targetReference.js"
|
|
52
47
|
import * as Topology from "./topology.js"
|
|
53
48
|
|
|
@@ -97,7 +92,7 @@ const Proto = {
|
|
|
97
92
|
}
|
|
98
93
|
|
|
99
94
|
const makeWithHandlers = (
|
|
100
|
-
self:
|
|
95
|
+
self: Machine.Any,
|
|
101
96
|
handlers: Readonly<Record<string, CapturedStateConfig>>
|
|
102
97
|
): Machine.Any => {
|
|
103
98
|
const machine = Object.create(Proto)
|
|
@@ -112,7 +107,6 @@ const makeWithHandlers = (
|
|
|
112
107
|
machine.initial = self.initial
|
|
113
108
|
machine.initialDefinition = self.initialDefinition
|
|
114
109
|
machine.stateNodes = self.stateNodes
|
|
115
|
-
machine.makeTargetBuilder = self.makeTargetBuilder
|
|
116
110
|
machine.handlers = handlers
|
|
117
111
|
Protocol.copyProtocol(self, machine)
|
|
118
112
|
return machine
|
|
@@ -139,74 +133,11 @@ type CapturedNamedBranch = {
|
|
|
139
133
|
readonly selection: Topology.TargetSelection
|
|
140
134
|
}
|
|
141
135
|
|
|
142
|
-
const InitialBuilderDescriptorTypeId: unique symbol = Symbol("effect/Machine/InitialBuilderDescriptor")
|
|
143
|
-
|
|
144
|
-
type InitialBuilderDescriptor = {
|
|
145
|
-
readonly [InitialBuilderDescriptorTypeId]: typeof InitialBuilderDescriptorTypeId
|
|
146
|
-
readonly selection: Topology.TargetSelection
|
|
147
|
-
readonly resolve: (context: any) => unknown
|
|
148
|
-
}
|
|
149
|
-
|
|
150
136
|
const plainTargetSelection = (selection: Topology.TargetSelection): Topology.TargetSelection =>
|
|
151
137
|
selection.kind === "none"
|
|
152
138
|
? Topology.noneTargetSelection
|
|
153
139
|
: Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, selection.updatePath)
|
|
154
140
|
|
|
155
|
-
// The authored callback crosses an erased schema boundary here. Each builder
|
|
156
|
-
// retains its construction mode, and planning validates both resulting values.
|
|
157
|
-
const constructSelectionValue = (
|
|
158
|
-
selection: Topology.TargetSelection,
|
|
159
|
-
context: any,
|
|
160
|
-
method: "from" | "decoded",
|
|
161
|
-
value: unknown
|
|
162
|
-
): unknown => {
|
|
163
|
-
if (selection.kind === "update") return context.owner[method](value)
|
|
164
|
-
if (selection.updatePath === undefined) return context.target[method](value)
|
|
165
|
-
const values = value as { readonly target: unknown; readonly update: unknown }
|
|
166
|
-
return context.target[method](values.target).update(context.owner[method](values.update))
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
const makeInitialBuilderDescriptor = (
|
|
170
|
-
selection: Topology.TargetSelection,
|
|
171
|
-
resolve: (context: any) => unknown
|
|
172
|
-
): InitialBuilderDescriptor =>
|
|
173
|
-
Object.freeze({
|
|
174
|
-
[InitialBuilderDescriptorTypeId]: InitialBuilderDescriptorTypeId,
|
|
175
|
-
selection: plainTargetSelection(selection),
|
|
176
|
-
resolve
|
|
177
|
-
})
|
|
178
|
-
|
|
179
|
-
const decorateInitialSelection = (selection: Topology.TargetSelection): Topology.TargetSelection =>
|
|
180
|
-
Object.freeze({
|
|
181
|
-
...selection,
|
|
182
|
-
from: (value: (context: any) => unknown) =>
|
|
183
|
-
makeInitialBuilderDescriptor(selection, (context) => context.target.from(value(context))),
|
|
184
|
-
decoded: (value: (context: any) => unknown) =>
|
|
185
|
-
makeInitialBuilderDescriptor(selection, (context) => context.target.decoded(value(context))),
|
|
186
|
-
resolve: (resolve: (context: any) => unknown) => makeInitialBuilderDescriptor(selection, resolve)
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
const decorateInitialSelectorNode = (node: unknown): unknown => {
|
|
190
|
-
if (Topology.isTargetSelection(node)) return decorateInitialSelection(node)
|
|
191
|
-
if (typeof node === "function") {
|
|
192
|
-
const wrapped = ((...args: ReadonlyArray<unknown>) => decorateInitialSelection(node(...args))) as
|
|
193
|
-
& ((...args: ReadonlyArray<unknown>) => unknown)
|
|
194
|
-
& Record<string, unknown>
|
|
195
|
-
for (const key of Object.keys(node)) {
|
|
196
|
-
wrapped[key] = decorateInitialSelectorNode((node as unknown as Record<string, unknown>)[key])
|
|
197
|
-
}
|
|
198
|
-
return Object.freeze(wrapped)
|
|
199
|
-
}
|
|
200
|
-
if (typeof node === "object" && node !== null) {
|
|
201
|
-
const wrapped: Record<string, unknown> = {}
|
|
202
|
-
for (const key of Object.keys(node)) {
|
|
203
|
-
wrapped[key] = decorateInitialSelectorNode((node as Record<string, unknown>)[key])
|
|
204
|
-
}
|
|
205
|
-
return Object.freeze(wrapped)
|
|
206
|
-
}
|
|
207
|
-
return node
|
|
208
|
-
}
|
|
209
|
-
|
|
210
141
|
const transitionTargetSelection = (
|
|
211
142
|
selection: Topology.TargetSelection
|
|
212
143
|
): Machine.TransitionTargetSelection =>
|
|
@@ -221,17 +152,6 @@ const selectionUpdates = (selection: Topology.TargetSelection): ReadonlyArray<st
|
|
|
221
152
|
selection.kind === "update" && selection.path !== undefined ? [selection.path] : []
|
|
222
153
|
: [selection.updatePath]
|
|
223
154
|
|
|
224
|
-
const makeSelectionValue = (
|
|
225
|
-
kind: Topology.TargetSelectionKind,
|
|
226
|
-
path: string | undefined,
|
|
227
|
-
scope: Topology.TargetSelectionScope
|
|
228
|
-
): Topology.TargetSelection => Topology.makeTargetSelection(kind, path, scope)
|
|
229
|
-
|
|
230
|
-
const makeStateUpdateSelection = (
|
|
231
|
-
path: string,
|
|
232
|
-
scope: "local" | "branch"
|
|
233
|
-
): Topology.TargetSelection => Topology.makeTargetSelection("update", path, scope)
|
|
234
|
-
|
|
235
155
|
const captureDefinitionBranch = (
|
|
236
156
|
branch: unknown,
|
|
237
157
|
selector: unknown,
|
|
@@ -266,91 +186,6 @@ const captureDefinitionBranch = (
|
|
|
266
186
|
return { ...(branch as DefinitionBranch), selection }
|
|
267
187
|
}
|
|
268
188
|
|
|
269
|
-
const makeUpdatingConstruction = (
|
|
270
|
-
target: unknown,
|
|
271
|
-
ownerPath: string
|
|
272
|
-
): { readonly update: (update: unknown) => Topology.CombinedTarget } =>
|
|
273
|
-
Object.freeze({
|
|
274
|
-
update: (update: unknown) => {
|
|
275
|
-
if (!Topology.isStateUpdate(update) || update.path !== ownerPath) {
|
|
276
|
-
throw new Error(`Machine combined target must update its declared owner "${ownerPath}"`)
|
|
277
|
-
}
|
|
278
|
-
return Topology.makeCombinedTarget(target, update)
|
|
279
|
-
}
|
|
280
|
-
})
|
|
281
|
-
|
|
282
|
-
const makeUpdatingTargetBuilder = (
|
|
283
|
-
builder: unknown,
|
|
284
|
-
ownerPath: string
|
|
285
|
-
): unknown => {
|
|
286
|
-
if (typeof builder !== "object" || builder === null) {
|
|
287
|
-
throw new Error("Machine combined target requires a state construction builder")
|
|
288
|
-
}
|
|
289
|
-
const updating: Record<PropertyKey, unknown> = {}
|
|
290
|
-
for (const property of Reflect.ownKeys(builder)) {
|
|
291
|
-
const descriptor = Object.getOwnPropertyDescriptor(builder, property)
|
|
292
|
-
if (descriptor === undefined) continue
|
|
293
|
-
if (
|
|
294
|
-
(property === "from" || property === "decoded") && "value" in descriptor && typeof descriptor.value === "function"
|
|
295
|
-
) {
|
|
296
|
-
const construct = descriptor.value
|
|
297
|
-
descriptor.value = (...args: ReadonlyArray<unknown>) => makeUpdatingConstruction(construct(...args), ownerPath)
|
|
298
|
-
}
|
|
299
|
-
Object.defineProperty(updating, property, descriptor)
|
|
300
|
-
}
|
|
301
|
-
return Object.freeze(updating)
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
const getSelectionBuilder = (
|
|
305
|
-
target: Record<string, any>,
|
|
306
|
-
selection: Topology.TargetSelection,
|
|
307
|
-
stateNodes: Machine.StateNodes,
|
|
308
|
-
source: string
|
|
309
|
-
): unknown => {
|
|
310
|
-
if (selection.kind === "none") return target.none
|
|
311
|
-
if (selection.kind === "update") {
|
|
312
|
-
return withFrom(
|
|
313
|
-
(value: unknown) => Topology.makeStateUpdate(selection.path!, value),
|
|
314
|
-
"leaf",
|
|
315
|
-
true
|
|
316
|
-
)
|
|
317
|
-
}
|
|
318
|
-
let builder: any
|
|
319
|
-
let parts = selection.path!.split(".")
|
|
320
|
-
if (selection.kind === "history") {
|
|
321
|
-
builder = target.history
|
|
322
|
-
if (selection.path !== "") parts.unshift("")
|
|
323
|
-
} else if (selection.scope === "local") {
|
|
324
|
-
builder = target.local
|
|
325
|
-
const scope = getLocalTargetScope(stateNodes, source)
|
|
326
|
-
if (scope !== undefined) {
|
|
327
|
-
if (selection.path === scope) {
|
|
328
|
-
builder = builder.with
|
|
329
|
-
parts = []
|
|
330
|
-
} else {
|
|
331
|
-
parts = (scope === "" ? selection.path! : selection.path!.slice(scope.length + 1)).split(".")
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
} else if (selection.scope === "branch") {
|
|
335
|
-
builder = target.branch
|
|
336
|
-
if (selection.path !== "") parts.unshift("")
|
|
337
|
-
} else {
|
|
338
|
-
builder = target.full
|
|
339
|
-
}
|
|
340
|
-
for (const part of parts) builder = builder[part]
|
|
341
|
-
if (selection.kind === "initial") builder = builder.initial
|
|
342
|
-
if (
|
|
343
|
-
typeof builder !== "function" &&
|
|
344
|
-
(typeof builder !== "object" || builder === null || typeof builder.from !== "function")
|
|
345
|
-
) {
|
|
346
|
-
throw new Error(`Machine could not construct selected transition target "${selection.path}"`)
|
|
347
|
-
}
|
|
348
|
-
return selection.updatePath === undefined ? builder : makeUpdatingTargetBuilder(builder, selection.updatePath)
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
const constructSelectedTarget = (builder: any): unknown =>
|
|
352
|
-
typeof builder?.from === "function" ? builder.from() : builder()
|
|
353
|
-
|
|
354
189
|
const validateResolvedSelection = (
|
|
355
190
|
result: unknown,
|
|
356
191
|
selection: Topology.TargetSelection,
|
|
@@ -401,37 +236,13 @@ const runCapturedBranch = (
|
|
|
401
236
|
stateNodes: Machine.StateNodes,
|
|
402
237
|
source: string
|
|
403
238
|
): unknown => {
|
|
404
|
-
const selectedTarget = getSelectionBuilder(context.target, branch.selection, stateNodes, source)
|
|
405
|
-
if (branch.resolve === undefined) return constructSelectedTarget(selectedTarget)
|
|
406
|
-
// This fresh context is owned by this evaluation and can be retained by user callbacks.
|
|
407
239
|
const resolverContext: Record<string, any> = {
|
|
408
240
|
...context,
|
|
409
241
|
root: source === "" ? context.state : context.ancestors[""],
|
|
410
242
|
decline: Topology.makeDeclined
|
|
411
243
|
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
if (branch.selection.kind === "update") {
|
|
415
|
-
const ownerPath = branch.selection.path!
|
|
416
|
-
delete resolverContext.target
|
|
417
|
-
resolverContext.current = context.ancestors[ownerPath] ?? context.state
|
|
418
|
-
resolverContext.owner = getSelectionBuilder(
|
|
419
|
-
context.target,
|
|
420
|
-
makeStateUpdateSelection(ownerPath, branch.selection.scope === "local" ? "local" : "branch"),
|
|
421
|
-
stateNodes,
|
|
422
|
-
source
|
|
423
|
-
)
|
|
424
|
-
} else if (branch.selection.updatePath !== undefined) {
|
|
425
|
-
const ownerPath = branch.selection.updatePath
|
|
426
|
-
resolverContext.current = context.ancestors[ownerPath]
|
|
427
|
-
resolverContext.owner = getSelectionBuilder(
|
|
428
|
-
context.target,
|
|
429
|
-
makeStateUpdateSelection(ownerPath, "branch"),
|
|
430
|
-
stateNodes,
|
|
431
|
-
source
|
|
432
|
-
)
|
|
433
|
-
}
|
|
434
|
-
const resolved = branch.resolve(resolverContext, enqueue)
|
|
244
|
+
delete resolverContext.target
|
|
245
|
+
const resolved = branch.resolve!(resolverContext, enqueue)
|
|
435
246
|
if (Topology.isDeclined(resolved)) {
|
|
436
247
|
if (branch.declinable !== true) {
|
|
437
248
|
throw new Error(`Machine transition for state "${source}" returned decline without declaring declinable: true`)
|
|
@@ -439,7 +250,7 @@ const runCapturedBranch = (
|
|
|
439
250
|
return resolved
|
|
440
251
|
}
|
|
441
252
|
validateResolvedSelection(resolved, branch.selection, stateNodes)
|
|
442
|
-
return resolved
|
|
253
|
+
return resolved
|
|
443
254
|
}
|
|
444
255
|
|
|
445
256
|
const topologyTargetPath = (selection: Topology.TargetSelection): string | undefined =>
|
|
@@ -486,92 +297,59 @@ const captureNamedBranches = (
|
|
|
486
297
|
}))
|
|
487
298
|
}
|
|
488
299
|
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
300
|
+
const constructBranch = (
|
|
301
|
+
selection: Topology.TargetSelection,
|
|
302
|
+
stateNodes: Machine.StateNodes,
|
|
303
|
+
declaration: Declaration.Declaration,
|
|
304
|
+
raw: unknown = {},
|
|
305
|
+
source?: string
|
|
495
306
|
): unknown => {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
method
|
|
505
|
-
) => [method, (value: unknown) =>
|
|
506
|
-
Topology.makeSelectedBranch(
|
|
507
|
-
owner,
|
|
508
|
-
branchIndex,
|
|
509
|
-
branchKey,
|
|
510
|
-
result.update(updateBuilder[method]!(value))
|
|
511
|
-
)]
|
|
512
|
-
)
|
|
513
|
-
)
|
|
514
|
-
)
|
|
515
|
-
})
|
|
516
|
-
}
|
|
517
|
-
return Topology.makeSelectedBranch(owner, branchIndex, branchKey, result)
|
|
518
|
-
}
|
|
519
|
-
for (const property of Reflect.ownKeys(builder)) {
|
|
520
|
-
if (
|
|
521
|
-
property === "length" || property === "name" || property === "prototype" || property === "caller" ||
|
|
522
|
-
property === "arguments"
|
|
523
|
-
) continue
|
|
524
|
-
const descriptor = Object.getOwnPropertyDescriptor(builder, property)
|
|
525
|
-
if (descriptor === undefined) continue
|
|
526
|
-
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
527
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder)
|
|
528
|
-
}
|
|
529
|
-
Object.defineProperty(wrapped, property, descriptor)
|
|
530
|
-
}
|
|
531
|
-
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!)
|
|
532
315
|
}
|
|
533
|
-
if (
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
if (descriptor === undefined) continue
|
|
538
|
-
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
539
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder)
|
|
540
|
-
}
|
|
541
|
-
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")
|
|
542
320
|
}
|
|
543
|
-
|
|
321
|
+
if (declaration.initialize === undefined) throw new Error("Machine root initialization is unavailable")
|
|
322
|
+
return declaration.initialize(config.input)
|
|
323
|
+
}
|
|
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
|
+
)
|
|
544
330
|
}
|
|
545
|
-
|
|
331
|
+
return Construction.target(stateNodes, selection.path!, config, source)
|
|
546
332
|
}
|
|
547
333
|
|
|
548
334
|
const makeBranchSelectors = (
|
|
549
|
-
context: Record<string, any>,
|
|
550
335
|
branches: ReadonlyArray<CapturedNamedBranch>,
|
|
551
336
|
owner: object,
|
|
552
337
|
stateNodes: Machine.StateNodes,
|
|
338
|
+
declaration: Declaration.Declaration,
|
|
553
339
|
source: string
|
|
554
|
-
): Readonly<Record<string, unknown>> =>
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
: getSelectionBuilder(
|
|
566
|
-
context.target,
|
|
567
|
-
makeStateUpdateSelection(branch.selection.updatePath, "branch"),
|
|
568
|
-
stateNodes,
|
|
569
|
-
source
|
|
570
|
-
) 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
|
+
)]
|
|
571
351
|
)
|
|
572
|
-
|
|
573
|
-
return Object.freeze(select)
|
|
574
|
-
}
|
|
352
|
+
))
|
|
575
353
|
|
|
576
354
|
const validateSelectedBranchResult = (
|
|
577
355
|
result: unknown,
|
|
@@ -607,11 +385,11 @@ const normalizeObjectTransition = (
|
|
|
607
385
|
const allowed = [
|
|
608
386
|
"target",
|
|
609
387
|
"update",
|
|
610
|
-
"
|
|
388
|
+
"input",
|
|
611
389
|
"history",
|
|
612
390
|
"none",
|
|
613
391
|
"branches",
|
|
614
|
-
"
|
|
392
|
+
"data",
|
|
615
393
|
"decoded",
|
|
616
394
|
"resolve",
|
|
617
395
|
"guard",
|
|
@@ -623,20 +401,21 @@ const normalizeObjectTransition = (
|
|
|
623
401
|
throw new Error("Machine transition contains an unknown field")
|
|
624
402
|
}
|
|
625
403
|
}
|
|
626
|
-
for (const key of ["
|
|
404
|
+
for (const key of ["resolve", "guard"]) {
|
|
627
405
|
if (config[key] !== undefined && typeof config[key] !== "function") {
|
|
628
406
|
throw new Error(`Machine transition ${key} must be a function`)
|
|
629
407
|
}
|
|
630
408
|
}
|
|
631
409
|
const guard = typeof config.guard === "function" ? config.guard : undefined
|
|
632
410
|
const resolve = typeof config.resolve === "function" ? config.resolve : undefined
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
411
|
+
if (config.decoded !== undefined && typeof config.decoded !== "boolean") {
|
|
412
|
+
throw new Error("Machine decoded must be a boolean construction flag")
|
|
413
|
+
}
|
|
414
|
+
const hasData = Object.prototype.hasOwnProperty.call(config, "data")
|
|
415
|
+
if (config.decoded === true && !hasData) throw new Error("Machine decoded construction requires data")
|
|
416
|
+
if (hasData && resolve !== undefined) throw new Error("Machine data and resolve are mutually exclusive")
|
|
417
|
+
const data = config.data
|
|
418
|
+
const construct = typeof data === "function" ? data : () => data
|
|
640
419
|
const guarded = guard !== undefined
|
|
641
420
|
const declinable = guarded || config.declinable === true
|
|
642
421
|
if (typeof config.branches === "string") {
|
|
@@ -645,7 +424,7 @@ const normalizeObjectTransition = (
|
|
|
645
424
|
throw new Error("Machine branching transition requires a registered group and resolver")
|
|
646
425
|
}
|
|
647
426
|
if (
|
|
648
|
-
["target", "update", "
|
|
427
|
+
["target", "update", "input", "history", "none", "data", "decoded"].some((key) => config[key] !== undefined)
|
|
649
428
|
) throw new Error("Machine branching transition cannot redeclare its destination")
|
|
650
429
|
const entries = Object.fromEntries(
|
|
651
430
|
Object.entries(group).map(([key, spec]) => [key, {
|
|
@@ -672,21 +451,41 @@ const normalizeObjectTransition = (
|
|
|
672
451
|
if (resolve !== undefined && selection.kind !== "none") {
|
|
673
452
|
throw new Error("Machine advanced construction requires a declared branch group")
|
|
674
453
|
}
|
|
675
|
-
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
|
+
}
|
|
676
462
|
return {
|
|
677
463
|
target: () => selection,
|
|
678
464
|
reenter: config.reenter,
|
|
679
465
|
declinable,
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
}
|
|
686
|
-
if (resolve !== undefined) return resolve(context, enqueue)
|
|
687
|
-
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) })
|
|
688
471
|
}
|
|
689
|
-
|
|
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
|
+
}
|
|
690
489
|
}
|
|
691
490
|
}
|
|
692
491
|
|
|
@@ -721,7 +520,7 @@ const captureTransition = (
|
|
|
721
520
|
decline: Topology.makeDeclined
|
|
722
521
|
}
|
|
723
522
|
delete resolverContext.target
|
|
724
|
-
resolverContext.select = makeBranchSelectors(
|
|
523
|
+
resolverContext.select = makeBranchSelectors(branches, owner, stateNodes, declaration, path)
|
|
725
524
|
const selected = resolve(resolverContext, enqueue)
|
|
726
525
|
if (Topology.isDeclined(selected)) {
|
|
727
526
|
if (!declinable) {
|
|
@@ -911,9 +710,31 @@ const flattenHandlers = (
|
|
|
911
710
|
|
|
912
711
|
const makeHandle = (self: Definition.Any, declaration: Declaration.Declaration): Definition.Any["handle"] =>
|
|
913
712
|
((config: Record<string, unknown>) => {
|
|
713
|
+
const captured = InitialDeclaration.capture(declaration.root, config)
|
|
714
|
+
const compiled = Object.assign(Object.create(Proto), self)
|
|
715
|
+
Protocol.copyProtocol(self, compiled)
|
|
716
|
+
compiled.states = Object.freeze({ "": captured.node })
|
|
717
|
+
compiled.stateNodes = Topology.compileStateNodes(compiled.states)
|
|
718
|
+
if (compiled.stateNodes.byPath.get("").schema === undefined && config.root !== undefined) {
|
|
719
|
+
throw new Error("Machine schema-less root cannot construct data")
|
|
720
|
+
}
|
|
721
|
+
const constructRoot = compiled.stateNodes.byPath.get("").schema === undefined
|
|
722
|
+
? () => undefined
|
|
723
|
+
: InitialDeclaration.construction(config.root)
|
|
724
|
+
compiled.initial = (input: unknown) => ({
|
|
725
|
+
...Topology.makeInitialTarget("", constructRoot({ input })),
|
|
726
|
+
input: { value: input }
|
|
727
|
+
})
|
|
728
|
+
compiled.initialDefinition = Object.freeze({
|
|
729
|
+
target: "",
|
|
730
|
+
selection: Object.freeze({ path: "", kind: "initial", scope: "initial" })
|
|
731
|
+
})
|
|
914
732
|
const handlers: Record<PropertyKey, CapturedStateConfig> = Object.create(null)
|
|
915
|
-
flattenHandlers(handlers,
|
|
916
|
-
|
|
733
|
+
flattenHandlers(handlers, compiled.stateNodes, compiled.states, "", {
|
|
734
|
+
...declaration,
|
|
735
|
+
initialize: (input) => compiled.initial(Protocol.decodeInputSync(compiled, compiled.input, input))
|
|
736
|
+
}, { "": captured.handlers })
|
|
737
|
+
return makeWithHandlers(compiled, handlers)
|
|
917
738
|
}) as Definition.Any["handle"]
|
|
918
739
|
|
|
919
740
|
export const isMachine = (
|
|
@@ -953,96 +774,6 @@ export const isFinal = <
|
|
|
953
774
|
state: Machine.Snapshot<States>
|
|
954
775
|
): state is Machine.SnapshotContainingFinal<States, FinalStates> => internalPlanner.isFinal(machine as any, state)
|
|
955
776
|
|
|
956
|
-
const getInitialSelectionBuilder = (
|
|
957
|
-
initialBuilder: Record<string, any>,
|
|
958
|
-
selection: Topology.TargetSelection
|
|
959
|
-
): Record<string, any> => {
|
|
960
|
-
const path = selection.path
|
|
961
|
-
if (path === undefined || path.includes(".")) {
|
|
962
|
-
throw new Error("Machine initial target must select one top-level state")
|
|
963
|
-
}
|
|
964
|
-
const builder = initialBuilder[path]
|
|
965
|
-
if (typeof builder !== "object" || builder === null || typeof builder.from !== "function") {
|
|
966
|
-
throw new Error(`Machine could not construct selected initial state "${path}"`)
|
|
967
|
-
}
|
|
968
|
-
return builder
|
|
969
|
-
}
|
|
970
|
-
|
|
971
|
-
const captureInitialBranch = (
|
|
972
|
-
definition: unknown,
|
|
973
|
-
initialBuilder: Record<string, any>,
|
|
974
|
-
configuration: boolean
|
|
975
|
-
): {
|
|
976
|
-
readonly selection: Topology.TargetSelection
|
|
977
|
-
readonly resolve?: (context: any) => unknown
|
|
978
|
-
readonly builder: Record<string, any>
|
|
979
|
-
} => {
|
|
980
|
-
if (definition !== undefined && typeof definition !== "function") {
|
|
981
|
-
throw new Error("Machine initial definition must be a target-first callback")
|
|
982
|
-
}
|
|
983
|
-
const selector = decorateInitialSelection(makeSelectionValue(configuration ? "state" : "initial", "", "initial"))
|
|
984
|
-
const result = definition === undefined ? selector : definition(selector)
|
|
985
|
-
let selection: Topology.TargetSelection
|
|
986
|
-
let resolve: ((context: any) => unknown) | undefined
|
|
987
|
-
if (Topology.isTargetSelection(result)) {
|
|
988
|
-
selection = plainTargetSelection(result)
|
|
989
|
-
} else if (hasProperty(result, InitialBuilderDescriptorTypeId)) {
|
|
990
|
-
const descriptor = result as InitialBuilderDescriptor
|
|
991
|
-
selection = descriptor.selection
|
|
992
|
-
resolve = descriptor.resolve
|
|
993
|
-
} else {
|
|
994
|
-
throw new Error("Machine initial definition must select exactly one target")
|
|
995
|
-
}
|
|
996
|
-
if (selection.kind !== "state" && selection.kind !== "initial") {
|
|
997
|
-
throw new Error("Machine initial target must select a top-level state or its declared initial entry")
|
|
998
|
-
}
|
|
999
|
-
const captured = {
|
|
1000
|
-
selection,
|
|
1001
|
-
builder: getInitialSelectionBuilder(initialBuilder, selection)
|
|
1002
|
-
}
|
|
1003
|
-
return resolve === undefined ? Object.freeze(captured) : Object.freeze({ ...captured, resolve })
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
const validateInitialSelection = (result: unknown, selection: Topology.TargetSelection): void => {
|
|
1007
|
-
if (
|
|
1008
|
-
typeof result !== "object" || result === null || !hasProperty(result, "path") || result.path !== selection.path
|
|
1009
|
-
) {
|
|
1010
|
-
const resultPath = typeof result === "object" && result !== null && hasProperty(result, "path")
|
|
1011
|
-
? String(result.path)
|
|
1012
|
-
: "<invalid>"
|
|
1013
|
-
throw new Error(`Machine initial resolver selected "${selection.path}" but constructed "${resultPath}"`)
|
|
1014
|
-
}
|
|
1015
|
-
}
|
|
1016
|
-
|
|
1017
|
-
const compileInitial = (
|
|
1018
|
-
definition: unknown,
|
|
1019
|
-
states: Machine.StateTree,
|
|
1020
|
-
stateNodes: Machine.StateNodes,
|
|
1021
|
-
configuration = false
|
|
1022
|
-
): {
|
|
1023
|
-
readonly initial: (input?: unknown) => unknown
|
|
1024
|
-
readonly definition: Machine.InitialDefinition
|
|
1025
|
-
} => {
|
|
1026
|
-
const rootNode = getTargetBuilderNode(stateNodes, "")
|
|
1027
|
-
const initialBuilder = configuration ?
|
|
1028
|
-
makeSnapshotBuilder(states, { mode: "full", prefix: "" }) as Record<string, any>
|
|
1029
|
-
: { "": withFrom((value: unknown) => Topology.makeInitialTarget("", value), "leaf", rootNode.schema !== undefined) }
|
|
1030
|
-
const branch = captureInitialBranch(definition, initialBuilder, configuration)
|
|
1031
|
-
return {
|
|
1032
|
-
initial: (input?: unknown) => {
|
|
1033
|
-
const result = branch.resolve === undefined
|
|
1034
|
-
? constructSelectedTarget(branch.builder)
|
|
1035
|
-
: branch.resolve({ input, target: branch.builder })
|
|
1036
|
-
validateInitialSelection(result, branch.selection)
|
|
1037
|
-
return result
|
|
1038
|
-
},
|
|
1039
|
-
definition: Object.freeze({
|
|
1040
|
-
target: branch.selection.path!,
|
|
1041
|
-
selection: transitionTargetSelection(branch.selection) as Machine.InitialDefinition["selection"]
|
|
1042
|
-
})
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
|
|
1046
777
|
export const state = (node: unknown): State<Machine.StateNodeConfig> => {
|
|
1047
778
|
const captured = StateDefinition.captureRoot(node) as Machine.StateNodeConfig
|
|
1048
779
|
const access = makeStateHelpers()
|
|
@@ -1115,17 +846,18 @@ export const make = <
|
|
|
1115
846
|
readonly logic?: unknown
|
|
1116
847
|
readonly children?: unknown
|
|
1117
848
|
readonly branches?: unknown
|
|
1118
|
-
readonly initialConfiguration?: unknown
|
|
1119
849
|
readonly events: Machine.EventProtocol<"public", InputEvents>
|
|
1120
850
|
readonly internalEvents?: Machine.EventProtocol<"internal", InternalEvents>
|
|
1121
851
|
readonly emittedEvents?: Machine.EventProtocol<"emitted", Emits>
|
|
1122
852
|
readonly parent?: ParentDeclaration
|
|
1123
853
|
readonly input?: Input
|
|
1124
|
-
readonly initial: unknown
|
|
1125
854
|
}
|
|
1126
855
|
): MakeResult<States, InputEvents, Emits, Input, InitialE, InitialR, InternalEvents, ParentDeclaration> => {
|
|
856
|
+
if (Object.hasOwn(config, "initial") || Object.hasOwn(config, "initialConfiguration")) {
|
|
857
|
+
throw new Error("Machine initial edges and root data belong in handle")
|
|
858
|
+
}
|
|
1127
859
|
const states = Object.freeze({ "": config.root.node })
|
|
1128
|
-
const self = Object.create(
|
|
860
|
+
const self = Object.create(PipeablePrototype)
|
|
1129
861
|
self.states = states
|
|
1130
862
|
self.root = config.root
|
|
1131
863
|
self.events = config.events
|
|
@@ -1134,16 +866,6 @@ export const make = <
|
|
|
1134
866
|
self.parent = config.parent
|
|
1135
867
|
self.input = config.input
|
|
1136
868
|
self.id = config.id
|
|
1137
|
-
self.stateNodes = Topology.compileStateNodes(states)
|
|
1138
|
-
const compiledInitial = compileInitial(
|
|
1139
|
-
config.initialConfiguration ?? config.initial,
|
|
1140
|
-
states,
|
|
1141
|
-
self.stateNodes,
|
|
1142
|
-
config.initialConfiguration !== undefined
|
|
1143
|
-
)
|
|
1144
|
-
self.initial = compiledInitial.initial
|
|
1145
|
-
self.initialDefinition = compiledInitial.definition
|
|
1146
|
-
self.makeTargetBuilder = makeTargetBuilder(states, self.stateNodes)
|
|
1147
869
|
self.handlers = Object.create(null)
|
|
1148
870
|
self.handle = makeHandle(self, Declaration.capture(config.root, config))
|
|
1149
871
|
Protocol.setProtocol(self)
|