@typeonce/effect-machine 0.33.0 → 0.34.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 +216 -636
- package/dist/Machine.d.ts +815 -583
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +20 -8
- 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 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -45
- 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 +5 -4
- 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/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 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +140 -278
- 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 +12 -11
- 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/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/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +31 -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 +2 -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 +38 -40
- 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/unstable/cluster/ClusterMachine.d.ts +5 -5
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +5 -17
- package/dist/unstable/cluster/ClusterMachine.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 +1514 -2312
- 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 -45
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +6 -5
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +199 -409
- package/src/internal/machine/planner.ts +54 -35
- package/src/internal/machine/process.ts +40 -33
- 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/targetReference.ts +40 -0
- package/src/internal/machine/topology.ts +2 -1
- package/src/internal/testing/machine/finiteModel.ts +42 -43
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/unstable/cluster/ClusterMachine.ts +9 -17
|
@@ -28,14 +28,16 @@ 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 Declaration from "./declaration.js"
|
|
31
32
|
import type { ChildAlreadyExistsError, InfiniteTransitionError, StartupError } from "./errors.js"
|
|
33
|
+
import type { CapturedStateConfig } from "./implementation.js"
|
|
32
34
|
import * as InvocationDefinition from "./invocationDefinition.js"
|
|
33
35
|
import * as internalPlanner from "./planner.js"
|
|
34
36
|
import * as internalProcess from "./process.js"
|
|
35
37
|
import * as Protocol from "./protocol.js"
|
|
36
38
|
import type { EnsureExecutable } from "./readiness.js"
|
|
37
39
|
import type { ExcludeCompatibleRuntime } from "./requirements.js"
|
|
38
|
-
import * as internalRuntime from "./
|
|
40
|
+
import * as internalRuntime from "./runtimeProtocol.js"
|
|
39
41
|
import * as Serialization from "./serialization.js"
|
|
40
42
|
import * as StateDefinition from "./stateDefinition.js"
|
|
41
43
|
import { ChildMachineLogicTypeId } from "./symbols.js"
|
|
@@ -46,6 +48,7 @@ import {
|
|
|
46
48
|
makeTargetBuilder,
|
|
47
49
|
withFrom
|
|
48
50
|
} from "./targetBuilder.js"
|
|
51
|
+
import * as TargetReference from "./targetReference.js"
|
|
49
52
|
import * as Topology from "./topology.js"
|
|
50
53
|
|
|
51
54
|
export {
|
|
@@ -59,11 +62,14 @@ export {
|
|
|
59
62
|
} from "./errors.js"
|
|
60
63
|
export { ChildMachineLogicTypeId, InitialEventTypeId, SnapshotBuilderStateTypeId } from "./symbols.js"
|
|
61
64
|
|
|
65
|
+
/** Internal seam for the public target-reference factory and its opaque brand. */
|
|
66
|
+
export const TargetReferenceTypeId: typeof TargetReference.TypeId = TargetReference.TypeId
|
|
67
|
+
export const targets: (root: State<Machine.StateNodeConfig>) => { readonly root: TargetReference.Reference } =
|
|
68
|
+
TargetReference.make
|
|
69
|
+
|
|
62
70
|
const TypeId = "~effect/Machine"
|
|
63
71
|
const ParentTypeId = "~effect/Machine/Parent"
|
|
64
72
|
export const InvokeTypeId: unique symbol = Symbol.for("effect/Machine/Invoke")
|
|
65
|
-
export const TransitionTypeId: unique symbol = Symbol.for("effect/Machine/Transition")
|
|
66
|
-
const InvokeBuilderDescriptorTypeId: unique symbol = Symbol("effect/Machine/InvokeBuilderDescriptor")
|
|
67
73
|
const ChildMachineTypeId = "~effect/Machine/ChildMachine"
|
|
68
74
|
type MachineRuntimeRequirement = internalRuntime.MachineRuntime
|
|
69
75
|
|
|
@@ -92,7 +98,7 @@ const Proto = {
|
|
|
92
98
|
|
|
93
99
|
const makeWithHandlers = (
|
|
94
100
|
self: Definition.Any,
|
|
95
|
-
handlers:
|
|
101
|
+
handlers: Readonly<Record<string, CapturedStateConfig>>
|
|
96
102
|
): Machine.Any => {
|
|
97
103
|
const machine = Object.create(Proto)
|
|
98
104
|
machine.states = self.states
|
|
@@ -133,28 +139,6 @@ type CapturedNamedBranch = {
|
|
|
133
139
|
readonly selection: Topology.TargetSelection
|
|
134
140
|
}
|
|
135
141
|
|
|
136
|
-
const TransitionBuilderDescriptorTypeId: unique symbol = Symbol("effect/Machine/TransitionBuilderDescriptor")
|
|
137
|
-
|
|
138
|
-
type DirectTransitionDescriptor = {
|
|
139
|
-
readonly [TransitionBuilderDescriptorTypeId]: typeof TransitionBuilderDescriptorTypeId
|
|
140
|
-
readonly type: "direct"
|
|
141
|
-
readonly selection: Topology.TargetSelection
|
|
142
|
-
readonly resolver?: (context: any, enqueue: unknown) => unknown
|
|
143
|
-
readonly reenterSource: boolean
|
|
144
|
-
readonly declinable: boolean
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
type BranchesTransitionDescriptor = {
|
|
148
|
-
readonly [TransitionBuilderDescriptorTypeId]: typeof TransitionBuilderDescriptorTypeId
|
|
149
|
-
readonly type: "branches"
|
|
150
|
-
readonly declarations: unknown
|
|
151
|
-
readonly resolve: (context: any, enqueue: unknown) => unknown
|
|
152
|
-
readonly reenterSource: boolean
|
|
153
|
-
readonly declinable: boolean
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
type TransitionBuilderDescriptor = DirectTransitionDescriptor | BranchesTransitionDescriptor
|
|
157
|
-
|
|
158
142
|
const InitialBuilderDescriptorTypeId: unique symbol = Symbol("effect/Machine/InitialBuilderDescriptor")
|
|
159
143
|
|
|
160
144
|
type InitialBuilderDescriptor = {
|
|
@@ -168,34 +152,6 @@ const plainTargetSelection = (selection: Topology.TargetSelection): Topology.Tar
|
|
|
168
152
|
? Topology.noneTargetSelection
|
|
169
153
|
: Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, selection.updatePath)
|
|
170
154
|
|
|
171
|
-
const transitionOptions = (options: unknown): { readonly declinable: boolean } => {
|
|
172
|
-
const configuration = typeof options === "object" && options !== null
|
|
173
|
-
? options as { readonly declinable?: unknown }
|
|
174
|
-
: {}
|
|
175
|
-
if (hasProperty(configuration, "reenter")) {
|
|
176
|
-
throw new Error("Use .reenter() before construction instead of the resolver reenter option")
|
|
177
|
-
}
|
|
178
|
-
return { declinable: configuration.declinable === true }
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const makeDirectTransitionDescriptor = (
|
|
182
|
-
selection: Topology.TargetSelection,
|
|
183
|
-
resolve: ((context: any, enqueue: unknown) => unknown) | undefined,
|
|
184
|
-
options: unknown,
|
|
185
|
-
reenterSource = false
|
|
186
|
-
): DirectTransitionDescriptor => {
|
|
187
|
-
const shared: Omit<DirectTransitionDescriptor, "resolver"> = {
|
|
188
|
-
[TransitionBuilderDescriptorTypeId]: TransitionBuilderDescriptorTypeId,
|
|
189
|
-
type: "direct",
|
|
190
|
-
selection: plainTargetSelection(selection),
|
|
191
|
-
reenterSource,
|
|
192
|
-
...transitionOptions(options)
|
|
193
|
-
}
|
|
194
|
-
return resolve === undefined
|
|
195
|
-
? Object.freeze(shared)
|
|
196
|
-
: Object.freeze({ ...shared, resolver: resolve })
|
|
197
|
-
}
|
|
198
|
-
|
|
199
155
|
// The authored callback crosses an erased schema boundary here. Each builder
|
|
200
156
|
// retains its construction mode, and planning validates both resulting values.
|
|
201
157
|
const constructSelectionValue = (
|
|
@@ -210,73 +166,6 @@ const constructSelectionValue = (
|
|
|
210
166
|
return context.target[method](values.target).update(context.owner[method](values.update))
|
|
211
167
|
}
|
|
212
168
|
|
|
213
|
-
const guardedSelection = (
|
|
214
|
-
selection: Topology.TargetSelection,
|
|
215
|
-
predicate: (context: any) => boolean,
|
|
216
|
-
reenterSource: boolean
|
|
217
|
-
): object => {
|
|
218
|
-
const wrap = (resolve: (context: any, enqueue: unknown) => unknown, options?: unknown) =>
|
|
219
|
-
makeDirectTransitionDescriptor(
|
|
220
|
-
selection,
|
|
221
|
-
(context, enqueue) => predicate(context) ? resolve(context, enqueue) : Topology.makeDeclined(),
|
|
222
|
-
{ ...transitionOptions(options), declinable: true },
|
|
223
|
-
reenterSource
|
|
224
|
-
)
|
|
225
|
-
return Object.freeze({
|
|
226
|
-
...wrap((context) => selection.kind === "none" ? undefined : constructSelectedTarget(context.target)),
|
|
227
|
-
from: (value: (context: any) => unknown) =>
|
|
228
|
-
wrap((context) => constructSelectionValue(selection, context, "from", value(context))),
|
|
229
|
-
decoded: (value: (context: any) => unknown) =>
|
|
230
|
-
wrap((context) => constructSelectionValue(selection, context, "decoded", value(context))),
|
|
231
|
-
resolve: wrap
|
|
232
|
-
})
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
const decorateTransitionSelection = (
|
|
236
|
-
selection: Topology.TargetSelection,
|
|
237
|
-
reenterSource = false
|
|
238
|
-
): Topology.TargetSelection =>
|
|
239
|
-
Object.freeze({
|
|
240
|
-
...selection,
|
|
241
|
-
...(reenterSource ? makeDirectTransitionDescriptor(selection, undefined, undefined, true) : {}),
|
|
242
|
-
guard: (predicate: (context: any) => boolean) => guardedSelection(selection, predicate, reenterSource),
|
|
243
|
-
from: (value: (context: any) => unknown) =>
|
|
244
|
-
makeDirectTransitionDescriptor(
|
|
245
|
-
selection,
|
|
246
|
-
(context) => constructSelectionValue(selection, context, "from", value(context)),
|
|
247
|
-
undefined,
|
|
248
|
-
reenterSource
|
|
249
|
-
),
|
|
250
|
-
decoded: (value: (context: any) => unknown) =>
|
|
251
|
-
makeDirectTransitionDescriptor(
|
|
252
|
-
selection,
|
|
253
|
-
(context) => constructSelectionValue(selection, context, "decoded", value(context)),
|
|
254
|
-
undefined,
|
|
255
|
-
reenterSource
|
|
256
|
-
),
|
|
257
|
-
resolve: (resolve: (context: any, enqueue: unknown) => unknown, options?: unknown) =>
|
|
258
|
-
makeDirectTransitionDescriptor(selection, resolve, options, reenterSource),
|
|
259
|
-
reenter: () => decorateTransitionSelection(selection, true),
|
|
260
|
-
updating: (owner: unknown) => {
|
|
261
|
-
if (typeof owner !== "function") {
|
|
262
|
-
throw new Error("Machine updating owner must be a state selector")
|
|
263
|
-
}
|
|
264
|
-
const ownerSelection = owner()
|
|
265
|
-
if (
|
|
266
|
-
!Topology.isTargetSelection(ownerSelection) || ownerSelection.kind !== "state" ||
|
|
267
|
-
ownerSelection.scope !== "branch"
|
|
268
|
-
) {
|
|
269
|
-
throw new Error("Machine updating owner must be addressed by one branch state selector")
|
|
270
|
-
}
|
|
271
|
-
return decorateTransitionSelection(
|
|
272
|
-
Topology.makeTargetSelection(selection.kind, selection.path, selection.scope, ownerSelection.path),
|
|
273
|
-
reenterSource
|
|
274
|
-
)
|
|
275
|
-
}
|
|
276
|
-
})
|
|
277
|
-
|
|
278
|
-
const noneTransitionSelection = decorateTransitionSelection(Topology.noneTargetSelection)
|
|
279
|
-
|
|
280
169
|
const makeInitialBuilderDescriptor = (
|
|
281
170
|
selection: Topology.TargetSelection,
|
|
282
171
|
resolve: (context: any) => unknown
|
|
@@ -318,89 +207,6 @@ const decorateInitialSelectorNode = (node: unknown): unknown => {
|
|
|
318
207
|
return node
|
|
319
208
|
}
|
|
320
209
|
|
|
321
|
-
const decorateTransitionSelectorNode = (node: unknown): unknown => {
|
|
322
|
-
if (Topology.isTargetSelection(node)) {
|
|
323
|
-
if (node.kind === "state" && node.scope === "full" && hasProperty(node, "initial")) {
|
|
324
|
-
return Object.freeze({
|
|
325
|
-
...decorateTransitionSelection(node),
|
|
326
|
-
initial: decorateTransitionSelection(node.initial as Topology.TargetSelection)
|
|
327
|
-
})
|
|
328
|
-
}
|
|
329
|
-
return node === Topology.noneTargetSelection ? noneTransitionSelection : decorateTransitionSelection(node)
|
|
330
|
-
}
|
|
331
|
-
if (typeof node === "function") {
|
|
332
|
-
const wrapped = ((...args: ReadonlyArray<unknown>) => decorateTransitionSelection(node(...args))) as
|
|
333
|
-
& ((...args: ReadonlyArray<unknown>) => unknown)
|
|
334
|
-
& Record<string, unknown>
|
|
335
|
-
for (const key of Object.keys(node)) {
|
|
336
|
-
wrapped[key] = decorateTransitionSelectorNode((node as unknown as Record<string, unknown>)[key])
|
|
337
|
-
}
|
|
338
|
-
return Object.freeze(wrapped)
|
|
339
|
-
}
|
|
340
|
-
if (typeof node === "object" && node !== null) {
|
|
341
|
-
const wrapped: Record<string, unknown> = {}
|
|
342
|
-
for (const key of Object.keys(node)) {
|
|
343
|
-
wrapped[key] = decorateTransitionSelectorNode((node as Record<string, unknown>)[key])
|
|
344
|
-
}
|
|
345
|
-
return Object.freeze(wrapped)
|
|
346
|
-
}
|
|
347
|
-
return node
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
const decorateBranches = (declarations: unknown, reenterSource = false): object =>
|
|
351
|
-
Object.freeze({
|
|
352
|
-
reenter: () => decorateBranches(declarations, true),
|
|
353
|
-
resolve: (
|
|
354
|
-
resolve: (context: any, enqueue: unknown) => unknown,
|
|
355
|
-
options?: unknown
|
|
356
|
-
): BranchesTransitionDescriptor =>
|
|
357
|
-
Object.freeze({
|
|
358
|
-
[TransitionBuilderDescriptorTypeId]: TransitionBuilderDescriptorTypeId,
|
|
359
|
-
type: "branches",
|
|
360
|
-
declarations,
|
|
361
|
-
resolve,
|
|
362
|
-
reenterSource,
|
|
363
|
-
...transitionOptions(options)
|
|
364
|
-
})
|
|
365
|
-
})
|
|
366
|
-
|
|
367
|
-
const makeTransitionSelector = (
|
|
368
|
-
stateNodes: Machine.StateNodes,
|
|
369
|
-
source: string
|
|
370
|
-
): unknown =>
|
|
371
|
-
Object.freeze({
|
|
372
|
-
...decorateTransitionSelectorNode(makeTargetSelector(stateNodes, source)) as Record<string, unknown>,
|
|
373
|
-
branches: (declarations: unknown) => decorateBranches(declarations)
|
|
374
|
-
})
|
|
375
|
-
|
|
376
|
-
const normalizeTransitionBuilder = (
|
|
377
|
-
transition: (selector: unknown) => unknown,
|
|
378
|
-
stateNodes: Machine.StateNodes,
|
|
379
|
-
path: string
|
|
380
|
-
): unknown => {
|
|
381
|
-
const result = transition(makeTransitionSelector(stateNodes, path))
|
|
382
|
-
if (Topology.isTargetSelection(result) && !hasProperty(result, TransitionBuilderDescriptorTypeId)) {
|
|
383
|
-
const selection = plainTargetSelection(result)
|
|
384
|
-
return { target: () => selection }
|
|
385
|
-
}
|
|
386
|
-
if (!hasProperty(result, TransitionBuilderDescriptorTypeId)) return result
|
|
387
|
-
const descriptor = result as TransitionBuilderDescriptor
|
|
388
|
-
if (descriptor.type === "branches") {
|
|
389
|
-
return {
|
|
390
|
-
branches: () => descriptor.declarations,
|
|
391
|
-
resolve: descriptor.resolve,
|
|
392
|
-
reenter: descriptor.reenterSource,
|
|
393
|
-
declinable: descriptor.declinable
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
return {
|
|
397
|
-
target: () => descriptor.selection,
|
|
398
|
-
resolve: descriptor.resolver,
|
|
399
|
-
reenter: descriptor.reenterSource,
|
|
400
|
-
declinable: descriptor.declinable
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
|
|
404
210
|
const transitionTargetSelection = (
|
|
405
211
|
selection: Topology.TargetSelection
|
|
406
212
|
): Machine.TransitionTargetSelection =>
|
|
@@ -415,13 +221,6 @@ const selectionUpdates = (selection: Topology.TargetSelection): ReadonlyArray<st
|
|
|
415
221
|
selection.kind === "update" && selection.path !== undefined ? [selection.path] : []
|
|
416
222
|
: [selection.updatePath]
|
|
417
223
|
|
|
418
|
-
const makeSelectionMethod = (
|
|
419
|
-
kind: Topology.TargetSelectionKind,
|
|
420
|
-
path: string | undefined,
|
|
421
|
-
scope: Topology.TargetSelectionScope
|
|
422
|
-
): () => Topology.TargetSelection =>
|
|
423
|
-
() => Topology.makeTargetSelection(kind, path, scope)
|
|
424
|
-
|
|
425
224
|
const makeSelectionValue = (
|
|
426
225
|
kind: Topology.TargetSelectionKind,
|
|
427
226
|
path: string | undefined,
|
|
@@ -433,106 +232,6 @@ const makeStateUpdateSelection = (
|
|
|
433
232
|
scope: "local" | "branch"
|
|
434
233
|
): Topology.TargetSelection => Topology.makeTargetSelection("update", path, scope)
|
|
435
234
|
|
|
436
|
-
const addSelectionChildren = (
|
|
437
|
-
builder: Record<string, unknown>,
|
|
438
|
-
stateNodes: Machine.StateNodes,
|
|
439
|
-
parent: string,
|
|
440
|
-
scope: "local" | "branch",
|
|
441
|
-
source?: string
|
|
442
|
-
): void => {
|
|
443
|
-
for (const node of stateNodes.byPath.values()) {
|
|
444
|
-
if (node.parent !== parent || node.type === "history") continue
|
|
445
|
-
builder[node.key] = makeSelectionNode(stateNodes, node.path, scope, source)
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
|
|
449
|
-
const makeSelectionNode = (
|
|
450
|
-
stateNodes: Machine.StateNodes,
|
|
451
|
-
path: string,
|
|
452
|
-
scope: Topology.TargetSelectionScope,
|
|
453
|
-
source?: string
|
|
454
|
-
): unknown => {
|
|
455
|
-
const node = getTargetBuilderNode(stateNodes, path)
|
|
456
|
-
const kind: Topology.TargetSelectionKind = node.type === "choice" ? "choice" : "state"
|
|
457
|
-
const method = makeSelectionMethod(kind, path, scope) as unknown as Record<string, unknown>
|
|
458
|
-
if (node.type !== "atomic" && node.type !== "final" && node.type !== "choice" && node.type !== "history") {
|
|
459
|
-
Object.defineProperty(method, "initial", {
|
|
460
|
-
value: makeSelectionValue("initial", path, scope),
|
|
461
|
-
enumerable: true
|
|
462
|
-
})
|
|
463
|
-
if (scope === "local" || scope === "branch") {
|
|
464
|
-
addSelectionChildren(method, stateNodes, path, scope, source)
|
|
465
|
-
}
|
|
466
|
-
if (
|
|
467
|
-
scope === "branch" && source !== undefined && node.schema !== undefined &&
|
|
468
|
-
(source === path || Configuration.isDescendantOf(source, path)) &&
|
|
469
|
-
getTargetBuilderNode(stateNodes, source).type !== "choice"
|
|
470
|
-
) {
|
|
471
|
-
Object.defineProperty(method, "update", {
|
|
472
|
-
value: makeStateUpdateSelection(path, "branch"),
|
|
473
|
-
enumerable: true
|
|
474
|
-
})
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
if (node.type === "atomic" && scope === "branch" && source === path && node.schema !== undefined) {
|
|
478
|
-
Object.defineProperty(method, "update", { value: makeStateUpdateSelection(path, "branch"), enumerable: true })
|
|
479
|
-
}
|
|
480
|
-
return method
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
const makeHistorySelectionTree = (
|
|
484
|
-
stateNodes: Machine.StateNodes,
|
|
485
|
-
parent: string | undefined
|
|
486
|
-
): Record<string, unknown> => {
|
|
487
|
-
const builder: Record<string, unknown> = {}
|
|
488
|
-
for (const node of stateNodes.byPath.values()) {
|
|
489
|
-
if (node.parent !== parent) continue
|
|
490
|
-
if (node.type === "history") {
|
|
491
|
-
builder[node.key] = makeSelectionValue("history", node.path, "full")
|
|
492
|
-
} else if (node.type !== "choice") {
|
|
493
|
-
const children = makeHistorySelectionTree(stateNodes, node.path)
|
|
494
|
-
if (Object.keys(children).length > 0) builder[node.key] = children
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
return builder
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
const makeTargetSelector = (
|
|
501
|
-
stateNodes: Machine.StateNodes,
|
|
502
|
-
source: string
|
|
503
|
-
): unknown => {
|
|
504
|
-
const full = Object.assign({}, makeSelectionValue("state", "", "full"), {
|
|
505
|
-
initial: makeSelectionValue("initial", "", "full")
|
|
506
|
-
})
|
|
507
|
-
const branch: Record<string, unknown> = {}
|
|
508
|
-
const root = getTargetBuilderNode(stateNodes, stateNodes.roots[0]!)
|
|
509
|
-
addSelectionChildren(branch, stateNodes, root.path, "branch", source)
|
|
510
|
-
const local: Record<string, unknown> = {}
|
|
511
|
-
const localScope = getLocalTargetScope(stateNodes, source)
|
|
512
|
-
if (localScope !== undefined) {
|
|
513
|
-
const localScopeNode = getTargetBuilderNode(stateNodes, localScope)
|
|
514
|
-
if (localScopeNode.schema !== undefined) {
|
|
515
|
-
local.with = makeSelectionValue("state", localScope, "local")
|
|
516
|
-
if (getTargetBuilderNode(stateNodes, source).type !== "choice") {
|
|
517
|
-
local.update = makeStateUpdateSelection(localScope, "local")
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
addSelectionChildren(local, stateNodes, localScope, "local")
|
|
521
|
-
}
|
|
522
|
-
return {
|
|
523
|
-
none: Topology.noneTargetSelection,
|
|
524
|
-
self: getTargetBuilderNode(stateNodes, source).schema === undefined ||
|
|
525
|
-
getTargetBuilderNode(stateNodes, source).type === "final"
|
|
526
|
-
? {}
|
|
527
|
-
: { update: makeStateUpdateSelection(source, "branch") },
|
|
528
|
-
root: makeSelectionNode(stateNodes, root.path, "branch", source),
|
|
529
|
-
local,
|
|
530
|
-
branch,
|
|
531
|
-
full,
|
|
532
|
-
history: makeHistorySelectionTree(stateNodes, "")
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
|
|
536
235
|
const captureDefinitionBranch = (
|
|
537
236
|
branch: unknown,
|
|
538
237
|
selector: unknown,
|
|
@@ -704,7 +403,12 @@ const runCapturedBranch = (
|
|
|
704
403
|
): unknown => {
|
|
705
404
|
const selectedTarget = getSelectionBuilder(context.target, branch.selection, stateNodes, source)
|
|
706
405
|
if (branch.resolve === undefined) return constructSelectedTarget(selectedTarget)
|
|
707
|
-
|
|
406
|
+
// This fresh context is owned by this evaluation and can be retained by user callbacks.
|
|
407
|
+
const resolverContext: Record<string, any> = {
|
|
408
|
+
...context,
|
|
409
|
+
root: source === "" ? context.state : context.ancestors[""],
|
|
410
|
+
decline: Topology.makeDeclined
|
|
411
|
+
}
|
|
708
412
|
if (branch.selection.kind === "none") delete resolverContext.target
|
|
709
413
|
else resolverContext.target = selectedTarget
|
|
710
414
|
if (branch.selection.kind === "update") {
|
|
@@ -727,7 +431,6 @@ const runCapturedBranch = (
|
|
|
727
431
|
source
|
|
728
432
|
)
|
|
729
433
|
}
|
|
730
|
-
if (branch.declinable === true) resolverContext.decline = Topology.makeDeclined
|
|
731
434
|
const resolved = branch.resolve(resolverContext, enqueue)
|
|
732
435
|
if (Topology.isDeclined(resolved)) {
|
|
733
436
|
if (branch.declinable !== true) {
|
|
@@ -776,14 +479,6 @@ const captureNamedBranches = (
|
|
|
776
479
|
if (!Topology.isTargetSelection(target)) {
|
|
777
480
|
throw new Error(`Machine transition branch "${key}" must select exactly one target`)
|
|
778
481
|
}
|
|
779
|
-
if (target.updatePath !== undefined) {
|
|
780
|
-
throw new Error(`Machine transition branch "${key}" cannot declare an updating target`)
|
|
781
|
-
}
|
|
782
|
-
if (hasProperty(target, TransitionBuilderDescriptorTypeId)) {
|
|
783
|
-
throw new Error(
|
|
784
|
-
`Machine transition branch "${key}" requires a topology selection; apply .reenter() to .branches(...)`
|
|
785
|
-
)
|
|
786
|
-
}
|
|
787
482
|
if (title !== undefined && (typeof title !== "string" || title.length === 0)) {
|
|
788
483
|
throw new Error(`Machine transition branch "${key}" title must be a non-empty string`)
|
|
789
484
|
}
|
|
@@ -795,11 +490,32 @@ const wrapSelectedBranchBuilder = (
|
|
|
795
490
|
builder: unknown,
|
|
796
491
|
owner: object,
|
|
797
492
|
branchIndex: number,
|
|
798
|
-
branchKey: string
|
|
493
|
+
branchKey: string,
|
|
494
|
+
updateBuilder?: Record<string, (value: unknown) => unknown>
|
|
799
495
|
): unknown => {
|
|
800
496
|
if (typeof builder === "function") {
|
|
801
|
-
const wrapped = (...args: ReadonlyArray<unknown>) =>
|
|
802
|
-
|
|
497
|
+
const wrapped = (...args: ReadonlyArray<unknown>) => {
|
|
498
|
+
const result = builder(...args)
|
|
499
|
+
if (updateBuilder !== undefined) {
|
|
500
|
+
return Object.freeze({
|
|
501
|
+
update: Object.freeze(
|
|
502
|
+
Object.fromEntries(
|
|
503
|
+
Object.getOwnPropertyNames(updateBuilder).map((
|
|
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
|
+
}
|
|
803
519
|
for (const property of Reflect.ownKeys(builder)) {
|
|
804
520
|
if (
|
|
805
521
|
property === "length" || property === "name" || property === "prototype" || property === "caller" ||
|
|
@@ -808,7 +524,7 @@ const wrapSelectedBranchBuilder = (
|
|
|
808
524
|
const descriptor = Object.getOwnPropertyDescriptor(builder, property)
|
|
809
525
|
if (descriptor === undefined) continue
|
|
810
526
|
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
811
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey)
|
|
527
|
+
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder)
|
|
812
528
|
}
|
|
813
529
|
Object.defineProperty(wrapped, property, descriptor)
|
|
814
530
|
}
|
|
@@ -820,7 +536,7 @@ const wrapSelectedBranchBuilder = (
|
|
|
820
536
|
const descriptor = Object.getOwnPropertyDescriptor(builder, property)
|
|
821
537
|
if (descriptor === undefined) continue
|
|
822
538
|
if ("value" in descriptor && typeof descriptor.value === "function") {
|
|
823
|
-
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey)
|
|
539
|
+
descriptor.value = wrapSelectedBranchBuilder(descriptor.value, owner, branchIndex, branchKey, updateBuilder)
|
|
824
540
|
}
|
|
825
541
|
Object.defineProperty(wrapped, property, descriptor)
|
|
826
542
|
}
|
|
@@ -843,7 +559,15 @@ const makeBranchSelectors = (
|
|
|
843
559
|
getSelectionBuilder(context.target, branch.selection, stateNodes, source),
|
|
844
560
|
owner,
|
|
845
561
|
branchIndex,
|
|
846
|
-
branch.key
|
|
562
|
+
branch.key,
|
|
563
|
+
branch.selection.updatePath === undefined
|
|
564
|
+
? undefined
|
|
565
|
+
: getSelectionBuilder(
|
|
566
|
+
context.target,
|
|
567
|
+
makeStateUpdateSelection(branch.selection.updatePath, "branch"),
|
|
568
|
+
stateNodes,
|
|
569
|
+
source
|
|
570
|
+
) as Record<string, (value: unknown) => unknown>
|
|
847
571
|
)
|
|
848
572
|
}
|
|
849
573
|
return Object.freeze(select)
|
|
@@ -866,23 +590,118 @@ const validateSelectedBranchResult = (
|
|
|
866
590
|
validateResolvedSelection(result, selection, stateNodes)
|
|
867
591
|
}
|
|
868
592
|
|
|
593
|
+
// Invocation input contexts do not pass through captured transition evaluation.
|
|
594
|
+
const objectContext = (context: Record<string, any>, path: string): Record<string, any> => ({
|
|
595
|
+
...context,
|
|
596
|
+
root: path === "" ? context.state : context.ancestors[""]
|
|
597
|
+
})
|
|
598
|
+
|
|
599
|
+
const normalizeObjectTransition = (
|
|
600
|
+
raw: unknown,
|
|
601
|
+
declaration: Declaration.Declaration,
|
|
602
|
+
path: string,
|
|
603
|
+
stateNodes: Machine.StateNodes
|
|
604
|
+
): Record<string, unknown> => {
|
|
605
|
+
if (typeof raw !== "object" || raw === null) throw new Error(`Machine transition for "${path}" must be an object`)
|
|
606
|
+
const config = { ...raw } as Record<string, unknown>
|
|
607
|
+
const allowed = [
|
|
608
|
+
"target",
|
|
609
|
+
"update",
|
|
610
|
+
"initial",
|
|
611
|
+
"history",
|
|
612
|
+
"none",
|
|
613
|
+
"branches",
|
|
614
|
+
"from",
|
|
615
|
+
"decoded",
|
|
616
|
+
"resolve",
|
|
617
|
+
"guard",
|
|
618
|
+
"reenter",
|
|
619
|
+
"declinable"
|
|
620
|
+
]
|
|
621
|
+
for (const key of Reflect.ownKeys(config)) {
|
|
622
|
+
if (typeof key !== "string" || !allowed.includes(key)) {
|
|
623
|
+
throw new Error("Machine transition contains an unknown field")
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
for (const key of ["from", "decoded", "resolve", "guard"]) {
|
|
627
|
+
if (config[key] !== undefined && typeof config[key] !== "function") {
|
|
628
|
+
throw new Error(`Machine transition ${key} must be a function`)
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
const guard = typeof config.guard === "function" ? config.guard : undefined
|
|
632
|
+
const resolve = typeof config.resolve === "function" ? config.resolve : undefined
|
|
633
|
+
const construct = typeof config.from === "function"
|
|
634
|
+
? config.from
|
|
635
|
+
: typeof config.decoded === "function"
|
|
636
|
+
? config.decoded
|
|
637
|
+
: undefined
|
|
638
|
+
const methods = ["from", "decoded", "resolve"].filter((key) => config[key] !== undefined)
|
|
639
|
+
if (methods.length > 1) throw new Error("Machine transition construction methods are mutually exclusive")
|
|
640
|
+
const guarded = guard !== undefined
|
|
641
|
+
const declinable = guarded || config.declinable === true
|
|
642
|
+
if (typeof config.branches === "string") {
|
|
643
|
+
const group = declaration.branches.get(config.branches)
|
|
644
|
+
if (group === undefined || resolve === undefined) {
|
|
645
|
+
throw new Error("Machine branching transition requires a registered group and resolver")
|
|
646
|
+
}
|
|
647
|
+
if (
|
|
648
|
+
["target", "update", "initial", "history", "none", "from", "decoded"].some((key) => config[key] !== undefined)
|
|
649
|
+
) throw new Error("Machine branching transition cannot redeclare its destination")
|
|
650
|
+
const entries = Object.fromEntries(
|
|
651
|
+
Object.entries(group).map(([key, spec]) => [key, {
|
|
652
|
+
title: spec.title,
|
|
653
|
+
target: captureDefinitionBranch(
|
|
654
|
+
{ target: () => Declaration.selection(declaration, spec) },
|
|
655
|
+
undefined,
|
|
656
|
+
stateNodes,
|
|
657
|
+
path,
|
|
658
|
+
key
|
|
659
|
+
).selection
|
|
660
|
+
}])
|
|
661
|
+
)
|
|
662
|
+
return {
|
|
663
|
+
branches: () => entries,
|
|
664
|
+
reenter: config.reenter,
|
|
665
|
+
declinable,
|
|
666
|
+
resolve: (context: Record<string, any>, enqueue: unknown) => {
|
|
667
|
+
return guarded && !guard(context) ? Topology.makeDeclined() : resolve(context, enqueue)
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
const selection = Declaration.selection(declaration, config)
|
|
672
|
+
if (resolve !== undefined && selection.kind !== "none") {
|
|
673
|
+
throw new Error("Machine advanced construction requires a declared branch group")
|
|
674
|
+
}
|
|
675
|
+
const method = config.from !== undefined ? "from" : config.decoded !== undefined ? "decoded" : undefined
|
|
676
|
+
return {
|
|
677
|
+
target: () => selection,
|
|
678
|
+
reenter: config.reenter,
|
|
679
|
+
declinable,
|
|
680
|
+
...(!guarded && method === undefined && resolve === undefined ? {} : {
|
|
681
|
+
resolve: (context: Record<string, any>, enqueue: unknown) => {
|
|
682
|
+
if (guarded && !guard(context)) return Topology.makeDeclined()
|
|
683
|
+
if (method !== undefined && construct !== undefined) {
|
|
684
|
+
return constructSelectionValue(selection, context, method, construct(context))
|
|
685
|
+
}
|
|
686
|
+
if (resolve !== undefined) return resolve(context, enqueue)
|
|
687
|
+
return selection.kind === "none" ? undefined : constructSelectedTarget(context.target)
|
|
688
|
+
}
|
|
689
|
+
})
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
869
693
|
const captureTransition = (
|
|
870
694
|
rawTransition: unknown,
|
|
871
695
|
stateNodes: Machine.StateNodes,
|
|
872
696
|
path: string,
|
|
697
|
+
declaration: Declaration.Declaration,
|
|
873
698
|
trigger: PropertyKey
|
|
874
699
|
): unknown => {
|
|
875
|
-
|
|
876
|
-
throw new Error(
|
|
877
|
-
`Machine transition for state "${path}" on "${String(trigger)}" must be a target-first callback`
|
|
878
|
-
)
|
|
879
|
-
}
|
|
880
|
-
const transition = normalizeTransitionBuilder(rawTransition as (selector: unknown) => unknown, stateNodes, path)
|
|
700
|
+
const transition = normalizeObjectTransition(rawTransition, declaration, path, stateNodes)
|
|
881
701
|
if (typeof transition !== "object" || transition === null) {
|
|
882
702
|
throw new Error(`Machine transition for state "${path}" on "${String(trigger)}" must be an object`)
|
|
883
703
|
}
|
|
884
704
|
const definition = transition as Record<PropertyKey, unknown>
|
|
885
|
-
const selector = makeTargetSelector(stateNodes, path)
|
|
886
705
|
const reenter = definition.reenter === true
|
|
887
706
|
const declinable = definition.declinable === true
|
|
888
707
|
if (hasProperty(definition, "branches")) {
|
|
@@ -893,13 +712,16 @@ const captureTransition = (
|
|
|
893
712
|
)
|
|
894
713
|
}
|
|
895
714
|
const resolve = branching.resolve
|
|
896
|
-
const branches = captureNamedBranches(branching.branches(
|
|
715
|
+
const branches = captureNamedBranches(branching.branches(), path, trigger)
|
|
897
716
|
const owner = Object.freeze({})
|
|
898
717
|
const evaluate = (context: Record<string, any>, enqueue: unknown) => {
|
|
899
|
-
const resolverContext = {
|
|
718
|
+
const resolverContext: Record<string, any> = {
|
|
719
|
+
...context,
|
|
720
|
+
root: path === "" ? context.state : context.ancestors[""],
|
|
721
|
+
decline: Topology.makeDeclined
|
|
722
|
+
}
|
|
900
723
|
delete resolverContext.target
|
|
901
724
|
resolverContext.select = makeBranchSelectors(context, branches, owner, stateNodes, path)
|
|
902
|
-
if (declinable) resolverContext.decline = Topology.makeDeclined
|
|
903
725
|
const selected = resolve(resolverContext, enqueue)
|
|
904
726
|
if (Topology.isDeclined(selected)) {
|
|
905
727
|
if (!declinable) {
|
|
@@ -951,7 +773,7 @@ const captureTransition = (
|
|
|
951
773
|
transition: (context: Record<string, any>, enqueue: unknown) => evaluate(context, enqueue).result
|
|
952
774
|
}
|
|
953
775
|
}
|
|
954
|
-
const branch = captureDefinitionBranch(transition,
|
|
776
|
+
const branch = captureDefinitionBranch(transition, undefined, stateNodes, path, trigger)
|
|
955
777
|
const evaluate = (context: Record<string, any>, enqueue: unknown) => ({
|
|
956
778
|
result: runCapturedBranch(branch, context, enqueue, stateNodes, path),
|
|
957
779
|
branchIndex: 0,
|
|
@@ -975,94 +797,56 @@ const captureTransition = (
|
|
|
975
797
|
const captureEventHandlers = (
|
|
976
798
|
on: object,
|
|
977
799
|
stateNodes: Machine.StateNodes,
|
|
978
|
-
path: string
|
|
800
|
+
path: string,
|
|
801
|
+
declaration: Declaration.Declaration
|
|
979
802
|
): Record<PropertyKey, unknown> => {
|
|
980
803
|
// The machine owns its dispatch table. Compiled plans may snapshot these
|
|
981
804
|
// definitions, so retaining caller-owned containers would let strategies
|
|
982
805
|
// observe different handlers after an unsafe external mutation.
|
|
983
806
|
const captured: Record<PropertyKey, unknown> = Object.create(null)
|
|
984
807
|
for (const event of Reflect.ownKeys(on)) {
|
|
985
|
-
captured[event] = captureTransition(
|
|
808
|
+
captured[event] = captureTransition(
|
|
809
|
+
(on as Record<PropertyKey, unknown>)[event],
|
|
810
|
+
stateNodes,
|
|
811
|
+
path,
|
|
812
|
+
declaration,
|
|
813
|
+
event
|
|
814
|
+
)
|
|
986
815
|
}
|
|
987
816
|
return captured
|
|
988
817
|
}
|
|
989
818
|
|
|
990
|
-
interface InvokeBuilderDescriptor {
|
|
991
|
-
readonly [InvokeBuilderDescriptorTypeId]: typeof InvokeBuilderDescriptorTypeId
|
|
992
|
-
readonly config: Readonly<Record<PropertyKey, unknown>>
|
|
993
|
-
}
|
|
994
|
-
|
|
995
|
-
type InvokeBuilderChannel = "onDone" | "onFailure" | "onElement" | "onSnapshot"
|
|
996
|
-
|
|
997
|
-
const makeInvokeBuilder = (
|
|
998
|
-
config: Readonly<Record<PropertyKey, unknown>>,
|
|
999
|
-
channels: ReadonlyArray<InvokeBuilderChannel>
|
|
1000
|
-
): InvokeBuilderDescriptor => {
|
|
1001
|
-
const builder: Record<PropertyKey, unknown> = {
|
|
1002
|
-
[InvokeBuilderDescriptorTypeId]: InvokeBuilderDescriptorTypeId as typeof InvokeBuilderDescriptorTypeId,
|
|
1003
|
-
config
|
|
1004
|
-
}
|
|
1005
|
-
for (const channel of channels) {
|
|
1006
|
-
if (!hasProperty(config, channel)) {
|
|
1007
|
-
builder[channel] = (handler: unknown) => makeInvokeBuilder({ ...config, [channel]: handler }, channels)
|
|
1008
|
-
}
|
|
1009
|
-
}
|
|
1010
|
-
return Object.freeze(builder) as unknown as InvokeBuilderDescriptor
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
|
-
const invokeSelector = Object.freeze({
|
|
1014
|
-
effect: (id: string, effect: unknown) => makeInvokeBuilder({ id, effect }, ["onDone", "onFailure"]),
|
|
1015
|
-
stream: (id: string, stream: unknown) => makeInvokeBuilder({ id, stream }, ["onElement", "onDone", "onFailure"]),
|
|
1016
|
-
timer: (id: string, after: unknown) => makeInvokeBuilder({ id, after }, ["onDone"]),
|
|
1017
|
-
logic: (id: string, options: Readonly<Record<PropertyKey, unknown>>) =>
|
|
1018
|
-
makeInvokeBuilder({ id, ...options }, ["onSnapshot", "onDone", "onFailure"]),
|
|
1019
|
-
child: (child: unknown, options?: Readonly<Record<PropertyKey, unknown>>) =>
|
|
1020
|
-
makeInvokeBuilder(options === undefined ? { child } : { child, ...options }, [
|
|
1021
|
-
"onSnapshot",
|
|
1022
|
-
"onDone",
|
|
1023
|
-
"onFailure"
|
|
1024
|
-
])
|
|
1025
|
-
})
|
|
1026
|
-
|
|
1027
|
-
const invokeBuilderConfig = (value: unknown, path: string): Readonly<Record<PropertyKey, unknown>> => {
|
|
1028
|
-
if (
|
|
1029
|
-
typeof value !== "object" || value === null ||
|
|
1030
|
-
!hasProperty(value, InvokeBuilderDescriptorTypeId) ||
|
|
1031
|
-
value[InvokeBuilderDescriptorTypeId] !== InvokeBuilderDescriptorTypeId
|
|
1032
|
-
) {
|
|
1033
|
-
throw new Error(`Machine invocation for state "${path}" must be constructed from its source selector`)
|
|
1034
|
-
}
|
|
1035
|
-
return (value as unknown as InvokeBuilderDescriptor).config
|
|
1036
|
-
}
|
|
1037
|
-
|
|
1038
819
|
const captureInvokeDefinition = (
|
|
1039
820
|
invoke: unknown,
|
|
1040
821
|
stateNodes: Machine.StateNodes,
|
|
1041
|
-
path: string
|
|
822
|
+
path: string,
|
|
823
|
+
declaration: Declaration.Declaration
|
|
1042
824
|
): unknown => {
|
|
1043
|
-
|
|
1044
|
-
throw new Error(`Machine invocation for state "${path}" must be a source-first callback`)
|
|
1045
|
-
}
|
|
1046
|
-
const authored = invoke(invokeSelector)
|
|
1047
|
-
const definitions = Array.isArray(authored) ? authored : [authored]
|
|
825
|
+
const definitions = Array.isArray(invoke) ? invoke : [invoke]
|
|
1048
826
|
const capturedDefinitions = definitions.map((definition) => {
|
|
1049
|
-
const captured =
|
|
827
|
+
const captured = Declaration.invocation(
|
|
828
|
+
declaration,
|
|
829
|
+
definition,
|
|
830
|
+
path,
|
|
831
|
+
(context) => objectContext(context as Record<string, any>, path)
|
|
832
|
+
)
|
|
1050
833
|
for (const key of ["onElement", "onDone", "onFailure", "onSnapshot"] as const) {
|
|
1051
834
|
if (captured[key] !== undefined) {
|
|
1052
|
-
captured[key] = captureTransition(captured[key], stateNodes, path, key)
|
|
835
|
+
captured[key] = captureTransition(captured[key], stateNodes, path, declaration, key)
|
|
1053
836
|
}
|
|
1054
837
|
}
|
|
1055
838
|
return InvocationDefinition.capture(captured, path)
|
|
1056
839
|
})
|
|
1057
|
-
if (Array.isArray(
|
|
840
|
+
if (Array.isArray(invoke)) return Object.freeze(capturedDefinitions)
|
|
1058
841
|
return capturedDefinitions[0]
|
|
1059
842
|
}
|
|
1060
843
|
|
|
1061
844
|
const flattenHandlers = (
|
|
1062
|
-
handlers: Record<PropertyKey,
|
|
845
|
+
handlers: Record<PropertyKey, CapturedStateConfig>,
|
|
1063
846
|
stateNodes: Machine.StateNodes,
|
|
1064
847
|
states: Machine.StateTree,
|
|
1065
848
|
prefix: string,
|
|
849
|
+
declaration: Declaration.Declaration,
|
|
1066
850
|
config: Record<string, unknown>
|
|
1067
851
|
): void => {
|
|
1068
852
|
for (const key of Object.keys(config)) {
|
|
@@ -1085,20 +869,20 @@ const flattenHandlers = (
|
|
|
1085
869
|
}
|
|
1086
870
|
const on = stateConfig.on
|
|
1087
871
|
if (typeof on === "object" && on !== null) {
|
|
1088
|
-
const capturedOn = captureEventHandlers(on, stateNodes, path)
|
|
872
|
+
const capturedOn = captureEventHandlers(on, stateNodes, path, declaration)
|
|
1089
873
|
stateConfig.on = capturedOn
|
|
1090
874
|
}
|
|
1091
875
|
if (stateConfig.always !== undefined) {
|
|
1092
|
-
stateConfig.always = captureTransition(stateConfig.always, stateNodes, path, "always")
|
|
876
|
+
stateConfig.always = captureTransition(stateConfig.always, stateNodes, path, declaration, "always")
|
|
1093
877
|
}
|
|
1094
878
|
if (stateConfig.onDone !== undefined) {
|
|
1095
|
-
stateConfig.onDone = captureTransition(stateConfig.onDone, stateNodes, path, "done")
|
|
879
|
+
stateConfig.onDone = captureTransition(stateConfig.onDone, stateNodes, path, declaration, "done")
|
|
1096
880
|
}
|
|
1097
881
|
if (stateConfig.choice !== undefined) {
|
|
1098
|
-
stateConfig.choice = captureTransition(stateConfig.choice, stateNodes, path, "choice")
|
|
882
|
+
stateConfig.choice = captureTransition(stateConfig.choice, stateNodes, path, declaration, "choice")
|
|
1099
883
|
}
|
|
1100
884
|
if (stateConfig.invoke !== undefined) {
|
|
1101
|
-
stateConfig.invoke = captureInvokeDefinition(stateConfig.invoke, stateNodes, path)
|
|
885
|
+
stateConfig.invoke = captureInvokeDefinition(stateConfig.invoke, stateNodes, path, declaration)
|
|
1102
886
|
}
|
|
1103
887
|
const node = stateNodes.byPath.get(path)
|
|
1104
888
|
if (node?.type === "choice") {
|
|
@@ -1111,7 +895,7 @@ const flattenHandlers = (
|
|
|
1111
895
|
throw new Error(`Machine choice state "${path}" requires a transition`)
|
|
1112
896
|
}
|
|
1113
897
|
}
|
|
1114
|
-
handlers[path] = stateConfig as
|
|
898
|
+
handlers[path] = stateConfig as CapturedStateConfig
|
|
1115
899
|
if (childConfig !== undefined) {
|
|
1116
900
|
const node = Topology.getStateNodeDefinition(path, states[key]!)
|
|
1117
901
|
if (node.states === undefined) {
|
|
@@ -1120,15 +904,15 @@ const flattenHandlers = (
|
|
|
1120
904
|
if (typeof childConfig !== "object" || childConfig === null) {
|
|
1121
905
|
throw new Error(`Machine expected state "${path}" child handlers to be an object`)
|
|
1122
906
|
}
|
|
1123
|
-
flattenHandlers(handlers, stateNodes, node.states, path, childConfig as Record<string, unknown>)
|
|
907
|
+
flattenHandlers(handlers, stateNodes, node.states, path, declaration, childConfig as Record<string, unknown>)
|
|
1124
908
|
}
|
|
1125
909
|
}
|
|
1126
910
|
}
|
|
1127
911
|
|
|
1128
|
-
const makeHandle = (self: Definition.Any): Definition.Any["handle"] =>
|
|
912
|
+
const makeHandle = (self: Definition.Any, declaration: Declaration.Declaration): Definition.Any["handle"] =>
|
|
1129
913
|
((config: Record<string, unknown>) => {
|
|
1130
|
-
const handlers: Record<PropertyKey,
|
|
1131
|
-
flattenHandlers(handlers, self.stateNodes, self.states, "", { "": config })
|
|
914
|
+
const handlers: Record<PropertyKey, CapturedStateConfig> = Object.create(null)
|
|
915
|
+
flattenHandlers(handlers, self.stateNodes, self.states, "", declaration, { "": config })
|
|
1132
916
|
return makeWithHandlers(self, handlers)
|
|
1133
917
|
}) as Definition.Any["handle"]
|
|
1134
918
|
|
|
@@ -1325,6 +1109,12 @@ export const make = <
|
|
|
1325
1109
|
config: {
|
|
1326
1110
|
readonly id?: string
|
|
1327
1111
|
readonly root: State<Machine.StateNodeConfig>
|
|
1112
|
+
readonly effects?: unknown
|
|
1113
|
+
readonly streams?: unknown
|
|
1114
|
+
readonly timers?: unknown
|
|
1115
|
+
readonly logic?: unknown
|
|
1116
|
+
readonly children?: unknown
|
|
1117
|
+
readonly branches?: unknown
|
|
1328
1118
|
readonly initialConfiguration?: unknown
|
|
1329
1119
|
readonly events: Machine.EventProtocol<"public", InputEvents>
|
|
1330
1120
|
readonly internalEvents?: Machine.EventProtocol<"internal", InternalEvents>
|
|
@@ -1355,7 +1145,7 @@ export const make = <
|
|
|
1355
1145
|
self.initialDefinition = compiledInitial.definition
|
|
1356
1146
|
self.makeTargetBuilder = makeTargetBuilder(states, self.stateNodes)
|
|
1357
1147
|
self.handlers = Object.create(null)
|
|
1358
|
-
self.handle = makeHandle(self)
|
|
1148
|
+
self.handle = makeHandle(self, Declaration.capture(config.root, config))
|
|
1359
1149
|
Protocol.setProtocol(self)
|
|
1360
1150
|
return self
|
|
1361
1151
|
}
|