@typeonce/effect-machine 0.34.0 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +93 -29
- package/dist/Machine.d.ts +309 -361
- 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/configuration.d.ts +0 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +0 -19
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +12 -20
- package/dist/internal/machine/executionPlan.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 +39 -120
- 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 +25 -67
- 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.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 +15 -6
- 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/package.json +1 -1
- package/src/Machine.ts +471 -744
- package/src/internal/machine/configuration.ts +0 -20
- package/src/internal/machine/executionPlan.ts +11 -18
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/machine.ts +38 -179
- package/src/internal/machine/planner.ts +30 -83
- 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 +3 -2
- package/src/internal/testing/machine/finiteModel.ts +16 -10
- 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
|
@@ -35,11 +35,11 @@ import {
|
|
|
35
35
|
normalizeTargetConfigurationSync,
|
|
36
36
|
pathDepth,
|
|
37
37
|
snapshotFromConfiguration,
|
|
38
|
-
snapshotFromConfigurationAtPath
|
|
39
|
-
validateInitialConfiguration
|
|
38
|
+
snapshotFromConfigurationAtPath
|
|
40
39
|
} from "./configuration.js"
|
|
41
40
|
import { InfiniteTransitionError, MachineSchemaDecodeError, StartupError, StoppedError } from "./errors.js"
|
|
42
41
|
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
42
|
+
import { isDataInitializer } from "./initialDeclaration.js"
|
|
43
43
|
import { getStateInitializeValues, makeStateInitializeBuilder } from "./initialization.js"
|
|
44
44
|
import * as InvocationDefinition from "./invocationDefinition.js"
|
|
45
45
|
import * as InvocationEvent from "./invocationEvent.js"
|
|
@@ -300,11 +300,13 @@ const completeHistoryConfiguration = (
|
|
|
300
300
|
)
|
|
301
301
|
let next = current
|
|
302
302
|
for (const routed of [choice.target, ...choice.additionalTargets]) {
|
|
303
|
-
if (routed === undefined
|
|
303
|
+
if (routed === undefined) {
|
|
304
304
|
throw new Error(`Machine initial choice "${child.path}" must resolve to an active state target`)
|
|
305
305
|
}
|
|
306
306
|
const resolved = isInitialTarget(routed)
|
|
307
307
|
? resolveInitialTarget(machine, next, routed, event)
|
|
308
|
+
: isHistoryTarget(routed)
|
|
309
|
+
? resolveHistoryTarget(machine, next, routed, event)
|
|
308
310
|
: undefined
|
|
309
311
|
next = normalizeTargetConfigurationSync(
|
|
310
312
|
machine,
|
|
@@ -343,7 +345,7 @@ const completeHistoryConfiguration = (
|
|
|
343
345
|
containingState: getParentValue(machine, current, path),
|
|
344
346
|
ancestors: getParentValues(machine, current, path),
|
|
345
347
|
event,
|
|
346
|
-
builder: makeStateInitializeBuilder(machine, path)
|
|
348
|
+
...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
|
|
347
349
|
})
|
|
348
350
|
const initializedValues = getStateInitializeValues(path, initialized.value)
|
|
349
351
|
values.set(child.path, decodeStateValueSync(machine, child, initializedValues[child.key]))
|
|
@@ -371,7 +373,7 @@ const completeHistoryConfiguration = (
|
|
|
371
373
|
containingState: getParentValue(machine, current, path),
|
|
372
374
|
ancestors: getParentValues(machine, current, path),
|
|
373
375
|
event,
|
|
374
|
-
builder: makeStateInitializeBuilder(machine, path)
|
|
376
|
+
...(isDataInitializer(initializer) ? {} : { builder: makeStateInitializeBuilder(machine, path) })
|
|
375
377
|
})
|
|
376
378
|
const initializedValues = initialized === undefined
|
|
377
379
|
? undefined
|
|
@@ -387,6 +389,10 @@ const completeHistoryConfiguration = (
|
|
|
387
389
|
if (
|
|
388
390
|
initializedValues === undefined || !Object.prototype.hasOwnProperty.call(initializedValues, child.key)
|
|
389
391
|
) {
|
|
392
|
+
if (initializer !== undefined && isDataInitializer(initializer)) {
|
|
393
|
+
values.set(child.path, decodeStateValueSync(machine, child, makeStateInput({})))
|
|
394
|
+
continue
|
|
395
|
+
}
|
|
390
396
|
throw new Error(`Machine parallel state initializer for "${path}" must return region "${child.key}"`)
|
|
391
397
|
}
|
|
392
398
|
values.set(
|
|
@@ -419,14 +425,24 @@ const completeHistoryConfiguration = (
|
|
|
419
425
|
}
|
|
420
426
|
}
|
|
421
427
|
|
|
422
|
-
|
|
428
|
+
/** Resolves initial data into owned configuration without a snapshot round trip. */
|
|
429
|
+
export function resolveInitialConfiguration(
|
|
423
430
|
machine: Machine.Any,
|
|
424
431
|
configuration: ActiveConfiguration,
|
|
425
432
|
target: InitialTargetInstruction,
|
|
426
433
|
event: unknown
|
|
427
434
|
) {
|
|
428
435
|
const partial = configurationFromInitialTargetSync(machine, configuration, target)
|
|
429
|
-
|
|
436
|
+
return completeHistoryConfiguration(machine, partial, event)
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
export function resolveInitialTarget(
|
|
440
|
+
machine: Machine.Any,
|
|
441
|
+
configuration: ActiveConfiguration,
|
|
442
|
+
target: InitialTargetInstruction,
|
|
443
|
+
event: unknown
|
|
444
|
+
) {
|
|
445
|
+
const completed = resolveInitialConfiguration(machine, configuration, target, event)
|
|
430
446
|
const snapshot = snapshotFromConfigurationAtPath(machine, completed.configuration, target.path)
|
|
431
447
|
return {
|
|
432
448
|
target: makeTarget(target.path as any, snapshot.value as any, {
|
|
@@ -1720,77 +1736,13 @@ export const planInitialSync = <
|
|
|
1720
1736
|
outputs: new Map(),
|
|
1721
1737
|
history: new Map()
|
|
1722
1738
|
}
|
|
1723
|
-
const rootResolution =
|
|
1724
|
-
|
|
1725
|
-
: undefined
|
|
1726
|
-
const state = rootResolution?.target ?? initial
|
|
1727
|
-
const initialChoice = choiceFromTarget(state)
|
|
1728
|
-
const choiceResolution = initialChoice === undefined
|
|
1729
|
-
? undefined
|
|
1730
|
-
: resolveChoiceTarget(
|
|
1731
|
-
machine,
|
|
1732
|
-
emptyConfiguration,
|
|
1733
|
-
state,
|
|
1734
|
-
InitialEvent
|
|
1735
|
-
)
|
|
1736
|
-
const initialHistoryActions: Array<RuntimeCommand> = []
|
|
1737
|
-
const initialHistoryRaisedEvents: Array<unknown> = []
|
|
1738
|
-
const initialHistoryEmittedEvents: Array<unknown> = []
|
|
1739
|
-
const initialHistoryChoiceTransitions: Array<ResolvedChoiceTransition> = []
|
|
1740
|
-
const resolvedInitialTargets: Array<unknown> = []
|
|
1741
|
-
for (
|
|
1742
|
-
const target of choiceResolution !== undefined
|
|
1743
|
-
? [choiceResolution.target, ...choiceResolution.additionalTargets]
|
|
1744
|
-
: rootResolution === undefined
|
|
1745
|
-
? []
|
|
1746
|
-
: [rootResolution.target]
|
|
1747
|
-
) {
|
|
1748
|
-
if (!isHistoryTarget(target)) {
|
|
1749
|
-
resolvedInitialTargets.push(target)
|
|
1750
|
-
continue
|
|
1751
|
-
}
|
|
1752
|
-
const history = resolveHistoryTarget(machine, emptyConfiguration, target, InitialEvent)
|
|
1753
|
-
resolvedInitialTargets.push(history.target)
|
|
1754
|
-
initialHistoryActions.push(...history.commands)
|
|
1755
|
-
initialHistoryRaisedEvents.push(...history.raisedEvents)
|
|
1756
|
-
initialHistoryEmittedEvents.push(...history.emittedEvents)
|
|
1757
|
-
initialHistoryChoiceTransitions.push(...history.transitions)
|
|
1758
|
-
}
|
|
1759
|
-
let resolvedConfiguration = resolvedInitialTargets.length === 0
|
|
1760
|
-
? normalizeConfigurationSync<States>(machine, state as Machine.Snapshot<States>)
|
|
1761
|
-
: normalizeTargetConfigurationSync<States>(
|
|
1762
|
-
machine,
|
|
1763
|
-
emptyConfiguration,
|
|
1764
|
-
resolvedInitialTargets[0] as
|
|
1765
|
-
| Machine.Snapshot<States>
|
|
1766
|
-
| Machine.Target<States, Machine.StateIdentifier<States>>
|
|
1767
|
-
)
|
|
1768
|
-
for (const additionalTarget of resolvedInitialTargets.slice(1)) {
|
|
1769
|
-
resolvedConfiguration = normalizeTargetConfigurationSync<States>(
|
|
1770
|
-
machine,
|
|
1771
|
-
resolvedConfiguration,
|
|
1772
|
-
additionalTarget as Machine.Snapshot<States> | Machine.Target<States, Machine.StateIdentifier<States>>
|
|
1773
|
-
)
|
|
1774
|
-
}
|
|
1775
|
-
const configuration: ActiveConfiguration = resolvedConfiguration
|
|
1776
|
-
if (machine.initialDefinition.selection.kind === "initial") validateInitialConfiguration(machine, configuration)
|
|
1739
|
+
const rootResolution = resolveInitialTarget(machine, emptyConfiguration, initial, InitialEvent)
|
|
1740
|
+
const configuration = normalizeTargetConfigurationSync(machine, emptyConfiguration, rootResolution.target)
|
|
1777
1741
|
const startingState = snapshotFromConfiguration<States>(machine, configuration)
|
|
1778
1742
|
const initialEntryPaths = getInitialEntryPaths(machine, configuration)
|
|
1779
|
-
const commands =
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
...initialHistoryActions
|
|
1783
|
-
]
|
|
1784
|
-
const raisedEvents = [
|
|
1785
|
-
...(rootResolution?.raisedEvents ?? []),
|
|
1786
|
-
...(choiceResolution?.raisedEvents ?? []),
|
|
1787
|
-
...initialHistoryRaisedEvents
|
|
1788
|
-
]
|
|
1789
|
-
const emittedEvents = [
|
|
1790
|
-
...(rootResolution?.emittedEvents ?? []),
|
|
1791
|
-
...(choiceResolution?.emittedEvents ?? []),
|
|
1792
|
-
...initialHistoryEmittedEvents
|
|
1793
|
-
]
|
|
1743
|
+
const commands = rootResolution.commands
|
|
1744
|
+
const raisedEvents = rootResolution.raisedEvents
|
|
1745
|
+
const emittedEvents = rootResolution.emittedEvents
|
|
1794
1746
|
const entry = collectStateActions<States, Events, Emits, E, R>(
|
|
1795
1747
|
machine,
|
|
1796
1748
|
configuration,
|
|
@@ -1805,17 +1757,12 @@ export const planInitialSync = <
|
|
|
1805
1757
|
[...entry.commands],
|
|
1806
1758
|
[...raisedEvents, ...entry.raisedEvents] as Array<Machine.EventOf<Events>>,
|
|
1807
1759
|
[...emittedEvents, ...entry.emittedEvents],
|
|
1808
|
-
|
|
1809
|
-
initialHistoryChoiceTransitions.length === 0 ?
|
|
1760
|
+
rootResolution.transitions.length === 0 ?
|
|
1810
1761
|
[] :
|
|
1811
1762
|
[{
|
|
1812
1763
|
next: configuration,
|
|
1813
1764
|
event: InitialEvent,
|
|
1814
|
-
transitions:
|
|
1815
|
-
...(rootResolution?.transitions ?? []),
|
|
1816
|
-
...(choiceResolution?.transitions ?? []),
|
|
1817
|
-
...initialHistoryChoiceTransitions
|
|
1818
|
-
],
|
|
1765
|
+
transitions: rootResolution.transitions,
|
|
1819
1766
|
commands,
|
|
1820
1767
|
raisedEvents: [
|
|
1821
1768
|
...raisedEvents
|
|
@@ -82,7 +82,7 @@ const getBoundaryResultDecoder = (schema: Schema.Top): BoundaryResultDecoder =>
|
|
|
82
82
|
|
|
83
83
|
const MachineProtocolTypeId = Symbol.for("effect/Machine/protocol")
|
|
84
84
|
|
|
85
|
-
const getProtocolSchemas = (machine:
|
|
85
|
+
const getProtocolSchemas = (machine: object): MachineProtocolSchemas => {
|
|
86
86
|
const protocol = (machine as any)[MachineProtocolTypeId] as MachineProtocolSchemas | undefined
|
|
87
87
|
if (protocol === undefined) {
|
|
88
88
|
throw new Error("Machine protocol is unavailable")
|
|
@@ -90,7 +90,7 @@ const getProtocolSchemas = (machine: Machine.Any): MachineProtocolSchemas => {
|
|
|
90
90
|
return protocol
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
const setProtocolSchemas = (machine:
|
|
93
|
+
const setProtocolSchemas = (machine: object, protocol: MachineProtocolSchemas): void => {
|
|
94
94
|
Object.defineProperty(machine, MachineProtocolTypeId, {
|
|
95
95
|
value: protocol,
|
|
96
96
|
enumerable: false
|
|
@@ -173,7 +173,7 @@ export const setProtocol = (machine: Machine.Any): void => {
|
|
|
173
173
|
})
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
-
export const copyProtocol = (source:
|
|
176
|
+
export const copyProtocol = (source: object, target: object): void =>
|
|
177
177
|
setProtocolSchemas(target, getProtocolSchemas(source))
|
|
178
178
|
|
|
179
179
|
export const getEventName = (event: unknown): string | undefined =>
|
|
@@ -4,7 +4,7 @@ import * as SchemaAST from "effect/SchemaAST"
|
|
|
4
4
|
export const StateNodePropertyPolicy = {
|
|
5
5
|
atomic: ["schema", "type", "annotations"],
|
|
6
6
|
final: ["schema", "type", "output", "annotations"],
|
|
7
|
-
compound: ["schema", "type", "
|
|
7
|
+
compound: ["schema", "type", "states", "annotations"],
|
|
8
8
|
parallel: ["schema", "type", "output", "states", "annotations"],
|
|
9
9
|
history: ["type", "history", "annotations"],
|
|
10
10
|
choice: ["type", "annotations"]
|
|
@@ -266,9 +266,6 @@ const validateStateTree = (
|
|
|
266
266
|
fail(boundary, `${path}.output`, "state output must be an Effect Schema")
|
|
267
267
|
}
|
|
268
268
|
if (kind === "compound") {
|
|
269
|
-
if (typeof node.initial !== "string") {
|
|
270
|
-
fail(boundary, `${path}.initial`, "compound states must declare an initial child key")
|
|
271
|
-
}
|
|
272
269
|
validateTargetSelectorChildKey(
|
|
273
270
|
boundary,
|
|
274
271
|
path,
|
|
@@ -285,18 +282,7 @@ const validateStateTree = (
|
|
|
285
282
|
"schema-backed compound child states cannot use the reserved local target selector key \"with\""
|
|
286
283
|
)
|
|
287
284
|
}
|
|
288
|
-
const initialKey = node.initial as string
|
|
289
285
|
validateStateTree(boundary, node.states, path, true)
|
|
290
|
-
if (!hasOwn(node.states as object, initialKey)) {
|
|
291
|
-
fail(boundary, `${path}.initial`, `compound initial child "${initialKey}" does not exist`)
|
|
292
|
-
}
|
|
293
|
-
const initial = (node.states as Readonly<Record<PropertyKey, unknown>>)[initialKey]
|
|
294
|
-
if (
|
|
295
|
-
!Schema.isSchema(initial) && typeof initial === "object" && initial !== null &&
|
|
296
|
-
(initial as Readonly<Record<PropertyKey, unknown>>).type === "history"
|
|
297
|
-
) {
|
|
298
|
-
fail(boundary, `${path}.initial`, "compound initial children cannot be history states")
|
|
299
|
-
}
|
|
300
286
|
continue
|
|
301
287
|
}
|
|
302
288
|
if (kind === "parallel") {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema"
|
|
1
2
|
import type { Machine, State } from "../../Machine.js"
|
|
2
|
-
import * as Topology from "./topology.js"
|
|
3
3
|
|
|
4
4
|
/** Reference metadata is separate from child names, including names such as `path`. */
|
|
5
5
|
export const TypeId: unique symbol = Symbol.for("effect/Machine/TargetReference")
|
|
@@ -14,9 +14,24 @@ export interface Reference {
|
|
|
14
14
|
|
|
15
15
|
/** Captures an immutable reference tree without allocating or executing a machine. */
|
|
16
16
|
export const make = (root: State<Machine.StateNodeConfig>): { readonly root: Reference } => {
|
|
17
|
-
const nodes
|
|
17
|
+
const nodes: Array<{ path: string; key: string; parent: string | undefined; type: string }> = []
|
|
18
|
+
const visit = (
|
|
19
|
+
node: Machine.StateNodeConfig | Machine.TaggedSchema,
|
|
20
|
+
path: string,
|
|
21
|
+
key: string,
|
|
22
|
+
parent: string | undefined
|
|
23
|
+
): void => {
|
|
24
|
+
const type = !Schema.isSchema(node) && "type" in node ? node.type ?? "active" : "active"
|
|
25
|
+
nodes.push({ path, key, parent, type })
|
|
26
|
+
if (!Schema.isSchema(node) && "states" in node) {
|
|
27
|
+
for (const [childKey, child] of Object.entries(node.states)) {
|
|
28
|
+
visit(child, path === "" ? childKey : `${path}.${childKey}`, childKey, path)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
visit(root.node, "", "", undefined)
|
|
18
33
|
const references = new Map<string, Reference>()
|
|
19
|
-
for (const node of nodes
|
|
34
|
+
for (const node of nodes) {
|
|
20
35
|
const reference = Object.create(null)
|
|
21
36
|
Object.defineProperty(reference, TypeId, {
|
|
22
37
|
value: Object.freeze({
|
|
@@ -27,7 +42,7 @@ export const make = (root: State<Machine.StateNodeConfig>): { readonly root: Ref
|
|
|
27
42
|
})
|
|
28
43
|
references.set(node.path, reference)
|
|
29
44
|
}
|
|
30
|
-
for (const node of nodes
|
|
45
|
+
for (const node of nodes) {
|
|
31
46
|
if (node.parent !== undefined) {
|
|
32
47
|
Object.defineProperty(references.get(node.parent), node.key, {
|
|
33
48
|
value: references.get(node.path),
|
|
@@ -330,7 +330,8 @@ export const getStateNodeDefinition = (
|
|
|
330
330
|
states: definition.states
|
|
331
331
|
}
|
|
332
332
|
}
|
|
333
|
-
|
|
333
|
+
const initial = "initial" in definition ? definition.initial : undefined
|
|
334
|
+
if (typeof initial !== "string") {
|
|
334
335
|
throw new Error(`Machine.make expected compound state "${path}" to declare an initial child`)
|
|
335
336
|
}
|
|
336
337
|
return {
|
|
@@ -339,7 +340,7 @@ export const getStateNodeDefinition = (
|
|
|
339
340
|
annotations,
|
|
340
341
|
type: "compound",
|
|
341
342
|
history: undefined,
|
|
342
|
-
initial
|
|
343
|
+
initial,
|
|
343
344
|
states: definition.states
|
|
344
345
|
}
|
|
345
346
|
}
|
|
@@ -1203,7 +1203,6 @@ const makeStateTree = (
|
|
|
1203
1203
|
} else if (node._tag === "Compound") {
|
|
1204
1204
|
tree[node.key] = {
|
|
1205
1205
|
schema,
|
|
1206
|
-
initial: node.initial,
|
|
1207
1206
|
states: makeStateTree(node.states, path)
|
|
1208
1207
|
}
|
|
1209
1208
|
} else {
|
|
@@ -1473,17 +1472,20 @@ const makeHandlers = (
|
|
|
1473
1472
|
...(Object.keys(history).length === 0 ? {} : { history }),
|
|
1474
1473
|
...(node._tag === "Compound"
|
|
1475
1474
|
? {
|
|
1476
|
-
|
|
1475
|
+
initial: {
|
|
1476
|
+
target: `${path}.${node.initial}`.split(".").reduce((ref, key) => ref[key], references),
|
|
1477
|
+
...(byPath.get(`${path}.${node.initial}`)!.node._tag === "Choice" ? {} : {
|
|
1478
|
+
decoded: true,
|
|
1479
|
+
data: stateValue(byPath.get(`${path}.${node.initial}`)!)
|
|
1480
|
+
})
|
|
1481
|
+
}
|
|
1477
1482
|
}
|
|
1478
1483
|
: {
|
|
1479
|
-
|
|
1484
|
+
initial: Object.fromEntries(
|
|
1480
1485
|
node.states
|
|
1481
1486
|
.filter((child) => child._tag !== "History" && child._tag !== "Choice")
|
|
1482
|
-
.
|
|
1483
|
-
|
|
1484
|
-
current[child.key].decoded(stateValue(byPath.get(`${path}.${child.key}`)!)),
|
|
1485
|
-
builder
|
|
1486
|
-
)
|
|
1487
|
+
.map((child) => [child.key, { decoded: true, data: stateValue(byPath.get(`${path}.${child.key}`)!) }])
|
|
1488
|
+
)
|
|
1487
1489
|
}),
|
|
1488
1490
|
states: makeHandlers(node.states, path, byPath, transitions, references, branches)
|
|
1489
1491
|
}
|
|
@@ -1508,7 +1510,7 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
|
|
|
1508
1510
|
const flattened = validateModel(model)
|
|
1509
1511
|
const byPath = new Map(flattened.map((state) => [state.path, state]))
|
|
1510
1512
|
const stateTree = makeStateTree(model.roots, undefined)
|
|
1511
|
-
const defined = Machine.state({
|
|
1513
|
+
const defined = Machine.state({ states: stateTree } as any)
|
|
1512
1514
|
const eventSchemas = model.events.map((event) => Schema.TaggedStruct(event, {}))
|
|
1513
1515
|
const initial = byPath.get(model.initial)!
|
|
1514
1516
|
const transitions = new Map(model.transitions.map((transition) => [
|
|
@@ -1524,7 +1526,11 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
|
|
|
1524
1526
|
} as any)
|
|
1525
1527
|
return machine.handle(
|
|
1526
1528
|
{
|
|
1527
|
-
|
|
1529
|
+
initial: {
|
|
1530
|
+
target: (Machine.targets(defined).root as unknown as Record<string, unknown>)[model.initial],
|
|
1531
|
+
decoded: true,
|
|
1532
|
+
data: stateValue(initial)
|
|
1533
|
+
},
|
|
1528
1534
|
states: handlers
|
|
1529
1535
|
} as any
|
|
1530
1536
|
) as Machine.Machine.Any
|
|
@@ -1738,7 +1738,13 @@ export const verify = <M extends AnyMachine>(
|
|
|
1738
1738
|
const node = byPath.get(path)
|
|
1739
1739
|
if (node?.type !== "compound") continue
|
|
1740
1740
|
const child = node.children.find((child) => starting.active.has(child))
|
|
1741
|
-
|
|
1741
|
+
const routed = initialChoiceRouteRoot() !== undefined &&
|
|
1742
|
+
trace.initial.plan.microsteps[0]?.transitions.some((transition) => {
|
|
1743
|
+
const target = transition.resolvedTarget ?? transition.target
|
|
1744
|
+
return target !== undefined && child !== undefined &&
|
|
1745
|
+
isDescendantOrSelf(String(target), child)
|
|
1746
|
+
})
|
|
1747
|
+
if (byPath.get(node.initial ?? "")?.type !== "choice" && child !== node.initial && !routed) {
|
|
1742
1748
|
add(
|
|
1743
1749
|
"definitions.initial",
|
|
1744
1750
|
initialLocation,
|
|
@@ -232,26 +232,29 @@ export interface Scenarios<M extends AnyMachine> {
|
|
|
232
232
|
* **Example**
|
|
233
233
|
*
|
|
234
234
|
* ```ts
|
|
235
|
-
* import { Schema } from "effect"
|
|
236
235
|
* import { Machine } from "@typeonce/effect-machine"
|
|
237
236
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
238
|
-
*
|
|
239
|
-
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
240
|
-
*
|
|
241
|
-
*
|
|
237
|
+
* import { Schema } from "effect"
|
|
238
|
+
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
239
|
+
* }
|
|
240
|
+
* class Reset extends Schema.TaggedClass<Reset>("Reset")("Reset", {}) {
|
|
241
|
+
* }
|
|
242
|
+
* const States = Machine.state({ states: { Idle } })
|
|
242
243
|
* const machine = Machine.make({
|
|
243
244
|
* root: States,
|
|
244
|
-
* events: Machine.eventsFromSchemas(Reset)
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
248
|
-
*
|
|
249
|
-
*
|
|
250
|
-
*
|
|
245
|
+
* events: Machine.eventsFromSchemas(Reset)
|
|
246
|
+
* }).handle({
|
|
247
|
+
* initial: {
|
|
248
|
+
* target: Machine.targets(States).root.Idle
|
|
249
|
+
* },
|
|
250
|
+
* states: {
|
|
251
|
+
* Idle: {
|
|
252
|
+
* on: {
|
|
253
|
+
* Reset: (to) => to.branch.Idle().resolve(({ target }) => target.from())
|
|
254
|
+
* }
|
|
251
255
|
* }
|
|
252
256
|
* }
|
|
253
|
-
* }
|
|
254
|
-
*
|
|
257
|
+
* })
|
|
255
258
|
* const generated = MachineTest.scenarios(machine, { maxEvents: 5 })
|
|
256
259
|
* ```
|
|
257
260
|
*
|
|
@@ -491,18 +494,23 @@ export { ProbeUnavailableError } from "../internal/testing/machine/probe.js"
|
|
|
491
494
|
* **Example**
|
|
492
495
|
*
|
|
493
496
|
* ```ts
|
|
494
|
-
* import { Effect, Schema } from "effect"
|
|
495
497
|
* import { Machine } from "@typeonce/effect-machine"
|
|
496
498
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
497
|
-
*
|
|
498
|
-
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
499
|
-
*
|
|
499
|
+
* import { Effect, Schema } from "effect"
|
|
500
|
+
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
501
|
+
* }
|
|
502
|
+
* const States = Machine.state({ states: { Idle } })
|
|
500
503
|
* const machine = Machine.make({
|
|
501
504
|
* root: States,
|
|
502
|
-
* events: Machine.eventsFromSchemas()
|
|
503
|
-
*
|
|
504
|
-
*
|
|
505
|
-
*
|
|
505
|
+
* events: Machine.eventsFromSchemas()
|
|
506
|
+
* }).handle({
|
|
507
|
+
* initial: {
|
|
508
|
+
* target: Machine.targets(States).root.Idle
|
|
509
|
+
* },
|
|
510
|
+
* states: {
|
|
511
|
+
* Idle: {}
|
|
512
|
+
* }
|
|
513
|
+
* })
|
|
506
514
|
* const program = Effect.gen(function*() {
|
|
507
515
|
* const ref = yield* Machine.start(machine)
|
|
508
516
|
* return yield* MachineTest.probe(machine, ref)
|
|
@@ -1100,20 +1108,27 @@ export const Invariant: {
|
|
|
1100
1108
|
* **Example**
|
|
1101
1109
|
*
|
|
1102
1110
|
* ```ts
|
|
1103
|
-
* import { Schema } from "effect"
|
|
1104
1111
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1105
1112
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1106
|
-
*
|
|
1113
|
+
* import { Schema } from "effect"
|
|
1107
1114
|
* class Count extends Schema.TaggedClass<Count>("Count")("Count", {
|
|
1108
1115
|
* value: Schema.Number
|
|
1109
|
-
* }) {
|
|
1110
|
-
*
|
|
1116
|
+
* }) {
|
|
1117
|
+
* }
|
|
1118
|
+
* const States = Machine.state({ states: { Count } })
|
|
1111
1119
|
* const machine = Machine.make({
|
|
1112
1120
|
* root: States,
|
|
1113
|
-
* events: Machine.eventsFromSchemas()
|
|
1114
|
-
*
|
|
1115
|
-
*
|
|
1116
|
-
*
|
|
1121
|
+
* events: Machine.eventsFromSchemas()
|
|
1122
|
+
* }).handle({
|
|
1123
|
+
* initial: {
|
|
1124
|
+
* target: Machine.targets(States).root.Count,
|
|
1125
|
+
* decoded: true,
|
|
1126
|
+
* data: new Count({ value: 0 })
|
|
1127
|
+
* },
|
|
1128
|
+
* states: {
|
|
1129
|
+
* Count: {}
|
|
1130
|
+
* }
|
|
1131
|
+
* })
|
|
1117
1132
|
* const nonNegative = MachineTest.invariants(machine).state(
|
|
1118
1133
|
* "count is non-negative",
|
|
1119
1134
|
* ({ snapshot }) => snapshot.value.value >= 0
|
|
@@ -1431,29 +1446,34 @@ export type ExploreOptions<M extends AnyMachine, Key extends ExplorationKey = Ex
|
|
|
1431
1446
|
* **Example**
|
|
1432
1447
|
*
|
|
1433
1448
|
* ```ts
|
|
1434
|
-
* import { Schema } from "effect"
|
|
1435
1449
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1436
1450
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1437
|
-
*
|
|
1451
|
+
* import { Schema } from "effect"
|
|
1438
1452
|
* class Count extends Schema.TaggedClass<Count>("Count")("Count", {
|
|
1439
1453
|
* value: Schema.Number
|
|
1440
|
-
* }) {
|
|
1441
|
-
*
|
|
1442
|
-
*
|
|
1454
|
+
* }) {
|
|
1455
|
+
* }
|
|
1456
|
+
* class Increment extends Schema.TaggedClass<Increment>("Increment")("Increment", {}) {
|
|
1457
|
+
* }
|
|
1458
|
+
* const States = Machine.state({ states: { Count } })
|
|
1443
1459
|
* const machine = Machine.make({
|
|
1444
1460
|
* root: States,
|
|
1445
|
-
* events: Machine.eventsFromSchemas(Increment)
|
|
1446
|
-
*
|
|
1447
|
-
*
|
|
1448
|
-
*
|
|
1449
|
-
*
|
|
1450
|
-
*
|
|
1451
|
-
*
|
|
1452
|
-
*
|
|
1461
|
+
* events: Machine.eventsFromSchemas(Increment)
|
|
1462
|
+
* }).handle({
|
|
1463
|
+
* initial: {
|
|
1464
|
+
* target: Machine.targets(States).root.Count,
|
|
1465
|
+
* decoded: true,
|
|
1466
|
+
* data: new Count({ value: 0 })
|
|
1467
|
+
* },
|
|
1468
|
+
* states: {
|
|
1469
|
+
* Count: {
|
|
1470
|
+
* on: {
|
|
1471
|
+
* Increment: (to) =>
|
|
1472
|
+
* to.branch.Count().resolve(({ state, target }) => target.decoded(new Count({ value: state.value + 1 })))
|
|
1473
|
+
* }
|
|
1453
1474
|
* }
|
|
1454
1475
|
* }
|
|
1455
|
-
* }
|
|
1456
|
-
*
|
|
1476
|
+
* })
|
|
1457
1477
|
* const explored = MachineTest.explore(machine, {
|
|
1458
1478
|
* events: ({ snapshot }) => snapshot.value.value < 2 ? [new Increment({})] : [],
|
|
1459
1479
|
* stateKey: ({ snapshot }) => snapshot.value.value
|
|
@@ -1623,18 +1643,23 @@ export type RunServices<M extends AnyMachine> = IsAny<
|
|
|
1623
1643
|
* **Example**
|
|
1624
1644
|
*
|
|
1625
1645
|
* ```ts
|
|
1626
|
-
* import { Schema } from "effect"
|
|
1627
1646
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1628
1647
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1629
|
-
*
|
|
1630
|
-
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
1631
|
-
*
|
|
1648
|
+
* import { Schema } from "effect"
|
|
1649
|
+
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
1650
|
+
* }
|
|
1651
|
+
* const States = Machine.state({ states: { Idle } })
|
|
1632
1652
|
* const machine = Machine.make({
|
|
1633
1653
|
* root: States,
|
|
1634
|
-
* events: Machine.eventsFromSchemas()
|
|
1635
|
-
*
|
|
1636
|
-
*
|
|
1637
|
-
*
|
|
1654
|
+
* events: Machine.eventsFromSchemas()
|
|
1655
|
+
* }).handle({
|
|
1656
|
+
* initial: {
|
|
1657
|
+
* target: Machine.targets(States).root.Idle
|
|
1658
|
+
* },
|
|
1659
|
+
* states: {
|
|
1660
|
+
* Idle: {}
|
|
1661
|
+
* }
|
|
1662
|
+
* })
|
|
1638
1663
|
* const trace = MachineTest.run(machine, { events: [] })
|
|
1639
1664
|
* ```
|
|
1640
1665
|
*
|
|
@@ -1889,22 +1914,24 @@ export interface Coverage<M extends AnyMachine> {
|
|
|
1889
1914
|
* **Example**
|
|
1890
1915
|
*
|
|
1891
1916
|
* ```ts
|
|
1892
|
-
* import { Effect, Schema } from "effect"
|
|
1893
1917
|
* import { Machine } from "@typeonce/effect-machine"
|
|
1894
1918
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
1895
|
-
*
|
|
1896
|
-
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
1897
|
-
*
|
|
1919
|
+
* import { Effect, Schema } from "effect"
|
|
1920
|
+
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
1921
|
+
* }
|
|
1922
|
+
* const States = Machine.state({ states: { Idle } })
|
|
1898
1923
|
* const machine = Machine.make({
|
|
1899
1924
|
* root: States,
|
|
1900
|
-
* events: Machine.eventsFromSchemas()
|
|
1901
|
-
*
|
|
1902
|
-
*
|
|
1903
|
-
*
|
|
1904
|
-
*
|
|
1905
|
-
*
|
|
1906
|
-
*
|
|
1907
|
-
*
|
|
1925
|
+
* events: Machine.eventsFromSchemas()
|
|
1926
|
+
* }).handle({
|
|
1927
|
+
* initial: {
|
|
1928
|
+
* target: Machine.targets(States).root.Idle
|
|
1929
|
+
* },
|
|
1930
|
+
* states: {
|
|
1931
|
+
* Idle: {}
|
|
1932
|
+
* }
|
|
1933
|
+
* })
|
|
1934
|
+
* const report = Effect.map(MachineTest.run(machine, { events: [] }), (trace) => MachineTest.coverage(machine, trace))
|
|
1908
1935
|
* ```
|
|
1909
1936
|
*
|
|
1910
1937
|
* @category verification
|
|
@@ -2115,18 +2142,23 @@ export interface VerifyOptions {
|
|
|
2115
2142
|
* **Example**
|
|
2116
2143
|
*
|
|
2117
2144
|
* ```ts
|
|
2118
|
-
* import { Effect, Schema } from "effect"
|
|
2119
2145
|
* import { Machine } from "@typeonce/effect-machine"
|
|
2120
2146
|
* import { MachineTest } from "@typeonce/effect-machine/testing"
|
|
2121
|
-
*
|
|
2122
|
-
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
2123
|
-
*
|
|
2147
|
+
* import { Effect, Schema } from "effect"
|
|
2148
|
+
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
2149
|
+
* }
|
|
2150
|
+
* const States = Machine.state({ states: { Idle } })
|
|
2124
2151
|
* const machine = Machine.make({
|
|
2125
2152
|
* root: States,
|
|
2126
|
-
* events: Machine.eventsFromSchemas()
|
|
2127
|
-
*
|
|
2128
|
-
*
|
|
2129
|
-
*
|
|
2153
|
+
* events: Machine.eventsFromSchemas()
|
|
2154
|
+
* }).handle({
|
|
2155
|
+
* initial: {
|
|
2156
|
+
* target: Machine.targets(States).root.Idle
|
|
2157
|
+
* },
|
|
2158
|
+
* states: {
|
|
2159
|
+
* Idle: {}
|
|
2160
|
+
* }
|
|
2161
|
+
* })
|
|
2130
2162
|
* const checked = Effect.gen(function*() {
|
|
2131
2163
|
* const trace = yield* MachineTest.run(machine, { events: [] })
|
|
2132
2164
|
* yield* MachineTest.verify(machine, trace)
|