@typeonce/effect-machine 0.33.0 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +272 -628
- package/dist/Machine.d.ts +1076 -896
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +141 -90
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/childRegistry.d.ts +1 -1
- package/dist/internal/machine/childRegistry.d.ts.map +1 -1
- package/dist/internal/machine/cluster.d.ts +2 -50
- package/dist/internal/machine/cluster.d.ts.map +1 -1
- package/dist/internal/machine/cluster.js +4 -27
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/clusterProtocol.d.ts +35 -0
- package/dist/internal/machine/clusterProtocol.d.ts.map +1 -0
- package/dist/internal/machine/clusterProtocol.js +28 -0
- package/dist/internal/machine/clusterProtocol.js.map +1 -0
- package/dist/internal/machine/commandRuntime.d.ts +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/configuration.d.ts +2 -2
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +38 -64
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +17 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -0
- package/dist/internal/machine/declaration.js +120 -0
- package/dist/internal/machine/declaration.js.map +1 -0
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +15 -22
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +34 -0
- package/dist/internal/machine/implementation.d.ts.map +1 -0
- package/dist/internal/machine/implementation.js +3 -0
- package/dist/internal/machine/implementation.js.map +1 -0
- package/dist/internal/machine/initialDeclaration.d.ts +14 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
- package/dist/internal/machine/initialDeclaration.js +137 -0
- package/dist/internal/machine/initialDeclaration.js.map +1 -0
- package/dist/internal/machine/invocation.d.ts +1 -1
- package/dist/internal/machine/invocation.d.ts.map +1 -1
- package/dist/internal/machine/invocation.js +1 -1
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts +1 -1
- package/dist/internal/machine/invocationDefinition.d.ts.map +1 -1
- package/dist/internal/machine/machine.d.ts +13 -4
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +169 -388
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +8 -0
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +37 -78
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +8 -7
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +16 -12
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +2 -230
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js +8 -1857
- package/dist/internal/machine/runtime.js.map +1 -1
- package/dist/internal/machine/runtimeCompiled.d.ts +5 -0
- package/dist/internal/machine/runtimeCompiled.d.ts.map +1 -0
- package/dist/internal/machine/runtimeCompiled.js +867 -0
- package/dist/internal/machine/runtimeCompiled.js.map +1 -0
- package/dist/internal/machine/runtimeGeneric.d.ts +3 -0
- package/dist/internal/machine/runtimeGeneric.d.ts.map +1 -0
- package/dist/internal/machine/runtimeGeneric.js +471 -0
- package/dist/internal/machine/runtimeGeneric.js.map +1 -0
- package/dist/internal/machine/runtimeProtocol.d.ts +312 -0
- package/dist/internal/machine/runtimeProtocol.d.ts.map +1 -0
- package/dist/internal/machine/runtimeProtocol.js +561 -0
- package/dist/internal/machine/runtimeProtocol.js.map +1 -0
- package/dist/internal/machine/stateDefinition.d.ts +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +1 -13
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/targetReference.d.ts +15 -0
- package/dist/internal/machine/targetReference.d.ts.map +1 -0
- package/dist/internal/machine/targetReference.js +41 -0
- package/dist/internal/machine/targetReference.js.map +1 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +5 -3
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +53 -46
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/probe.js +1 -1
- package/dist/internal/testing/machine/probe.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +7 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +105 -73
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +105 -73
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +18 -13
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +18 -25
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +41 -24
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/docs/agent-guide.md +66 -91
- package/docs/machine-review.md +19 -49
- package/docs/root-api.md +62 -62
- package/package.json +1 -1
- package/src/Machine.ts +1836 -2907
- package/src/internal/machine/childRegistry.ts +1 -1
- package/src/internal/machine/cluster.ts +7 -73
- package/src/internal/machine/clusterProtocol.ts +56 -0
- package/src/internal/machine/commandRuntime.ts +1 -1
- package/src/internal/machine/configuration.ts +53 -65
- package/src/internal/machine/declaration.ts +140 -0
- package/src/internal/machine/executionPlan.ts +16 -22
- package/src/internal/machine/implementation.ts +41 -0
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/invocation.ts +3 -3
- package/src/internal/machine/invocationDefinition.ts +1 -1
- package/src/internal/machine/machine.ts +225 -576
- package/src/internal/machine/planner.ts +84 -118
- package/src/internal/machine/process.ts +40 -33
- package/src/internal/machine/protocol.ts +3 -3
- package/src/internal/machine/runtime.ts +20 -3037
- package/src/internal/machine/runtimeCompiled.ts +1166 -0
- package/src/internal/machine/runtimeGeneric.ts +799 -0
- package/src/internal/machine/runtimeProtocol.ts +1213 -0
- package/src/internal/machine/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +55 -0
- package/src/internal/machine/topology.ts +5 -3
- package/src/internal/testing/machine/finiteModel.ts +58 -53
- package/src/internal/testing/machine/probe.ts +1 -1
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +22 -25
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
|
@@ -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") {
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema"
|
|
2
|
+
import type { Machine, State } from "../../Machine.js"
|
|
3
|
+
|
|
4
|
+
/** Reference metadata is separate from child names, including names such as `path`. */
|
|
5
|
+
export const TypeId: unique symbol = Symbol.for("effect/Machine/TargetReference")
|
|
6
|
+
|
|
7
|
+
export interface Reference {
|
|
8
|
+
readonly [TypeId]: {
|
|
9
|
+
readonly root: State<Machine.StateNodeConfig>
|
|
10
|
+
readonly path: string
|
|
11
|
+
readonly kind: "state" | "choice" | "history"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Captures an immutable reference tree without allocating or executing a machine. */
|
|
16
|
+
export const make = (root: State<Machine.StateNodeConfig>): { readonly root: Reference } => {
|
|
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)
|
|
33
|
+
const references = new Map<string, Reference>()
|
|
34
|
+
for (const node of nodes) {
|
|
35
|
+
const reference = Object.create(null)
|
|
36
|
+
Object.defineProperty(reference, TypeId, {
|
|
37
|
+
value: Object.freeze({
|
|
38
|
+
root,
|
|
39
|
+
path: node.path,
|
|
40
|
+
kind: node.type === "history" || node.type === "choice" ? node.type : "state"
|
|
41
|
+
})
|
|
42
|
+
})
|
|
43
|
+
references.set(node.path, reference)
|
|
44
|
+
}
|
|
45
|
+
for (const node of nodes) {
|
|
46
|
+
if (node.parent !== undefined) {
|
|
47
|
+
Object.defineProperty(references.get(node.parent), node.key, {
|
|
48
|
+
value: references.get(node.path),
|
|
49
|
+
enumerable: true
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (const reference of references.values()) Object.freeze(reference)
|
|
54
|
+
return Object.freeze({ root: references.get("")! })
|
|
55
|
+
}
|
|
@@ -8,6 +8,7 @@ import * as Option from "effect/Option"
|
|
|
8
8
|
import { hasProperty } from "effect/Predicate"
|
|
9
9
|
import * as Schema from "effect/Schema"
|
|
10
10
|
import type { Machine } from "../../Machine.js"
|
|
11
|
+
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
11
12
|
|
|
12
13
|
export const TargetTypeId = "~effect/Machine/Target"
|
|
13
14
|
|
|
@@ -329,7 +330,8 @@ export const getStateNodeDefinition = (
|
|
|
329
330
|
states: definition.states
|
|
330
331
|
}
|
|
331
332
|
}
|
|
332
|
-
|
|
333
|
+
const initial = "initial" in definition ? definition.initial : undefined
|
|
334
|
+
if (typeof initial !== "string") {
|
|
333
335
|
throw new Error(`Machine.make expected compound state "${path}" to declare an initial child`)
|
|
334
336
|
}
|
|
335
337
|
return {
|
|
@@ -338,7 +340,7 @@ export const getStateNodeDefinition = (
|
|
|
338
340
|
annotations,
|
|
339
341
|
type: "compound",
|
|
340
342
|
history: undefined,
|
|
341
|
-
initial
|
|
343
|
+
initial,
|
|
342
344
|
states: definition.states
|
|
343
345
|
}
|
|
344
346
|
}
|
|
@@ -506,7 +508,7 @@ export const transitionDefinitions = (
|
|
|
506
508
|
): ReadonlyArray<Machine.TransitionDefinition> => {
|
|
507
509
|
const definitions: Array<Machine.TransitionDefinition> = []
|
|
508
510
|
for (const node of machine.stateNodes.byPath.values()) {
|
|
509
|
-
const config = machine.handlers[node.path] as
|
|
511
|
+
const config = toImpl(machine).handlers[node.path] as CapturedStateConfig | undefined
|
|
510
512
|
if (config === undefined) {
|
|
511
513
|
continue
|
|
512
514
|
}
|
|
@@ -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 {
|
|
@@ -1315,13 +1314,6 @@ const findSnapshot = (snapshot: unknown, path: string): unknown => {
|
|
|
1315
1314
|
return undefined
|
|
1316
1315
|
}
|
|
1317
1316
|
|
|
1318
|
-
const selectHistoryTarget = (builder: Record<string, any>, path: string): unknown => {
|
|
1319
|
-
const parts = path.split(".")
|
|
1320
|
-
let current: any = builder
|
|
1321
|
-
for (let index = 0; index < parts.length - 1; index++) current = current[parts[index]!]
|
|
1322
|
-
return current[parts[parts.length - 1]!]
|
|
1323
|
-
}
|
|
1324
|
-
|
|
1325
1317
|
const selectableDefinitionTarget = (
|
|
1326
1318
|
source: string,
|
|
1327
1319
|
target: string,
|
|
@@ -1340,26 +1332,20 @@ const selectableDefinitionTarget = (
|
|
|
1340
1332
|
}
|
|
1341
1333
|
|
|
1342
1334
|
const selectDefinitionTarget = (
|
|
1343
|
-
|
|
1335
|
+
references: Record<string, any>,
|
|
1344
1336
|
source: string,
|
|
1345
1337
|
target: string,
|
|
1346
1338
|
byPath: ReadonlyMap<string, FlatFiniteState>
|
|
1347
|
-
): unknown => {
|
|
1339
|
+
): Readonly<Record<string, unknown>> => {
|
|
1348
1340
|
const selected = byPath.get(target)!
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
}
|
|
1357
|
-
const selectable = selectableDefinitionTarget(source, target, byPath)
|
|
1358
|
-
let current = selector.branch
|
|
1359
|
-
const parts = selectable.split(".")
|
|
1360
|
-
for (const part of parts) current = current[part]
|
|
1361
|
-
if (typeof current === "function") return current()
|
|
1362
|
-
return current.initial
|
|
1341
|
+
const selectedPath = selected.node._tag === "History" ?
|
|
1342
|
+
target
|
|
1343
|
+
: selected.root !== byPath.get(source)!.root ?
|
|
1344
|
+
target.split(".")[0]!
|
|
1345
|
+
: selectableDefinitionTarget(source, target, byPath)
|
|
1346
|
+
let reference = references
|
|
1347
|
+
for (const part of selectedPath.split(".")) reference = reference[part]
|
|
1348
|
+
return selected.node._tag === "History" ? { history: reference } : { target: reference }
|
|
1363
1349
|
}
|
|
1364
1350
|
|
|
1365
1351
|
const resolveDefinitionTarget = (
|
|
@@ -1410,18 +1396,23 @@ const makeHandlers = (
|
|
|
1410
1396
|
states: ReadonlyArray<FiniteState>,
|
|
1411
1397
|
parent: string | undefined,
|
|
1412
1398
|
byPath: ReadonlyMap<string, FlatFiniteState>,
|
|
1413
|
-
transitions: ReadonlyMap<string, FiniteTransition
|
|
1399
|
+
transitions: ReadonlyMap<string, FiniteTransition>,
|
|
1400
|
+
references: Record<string, any>,
|
|
1401
|
+
branches: Record<string, Readonly<Record<string, unknown>>>
|
|
1414
1402
|
): Record<string, unknown> => {
|
|
1415
1403
|
const handlers: Record<string, unknown> = Object.create(null)
|
|
1416
1404
|
for (const node of states) {
|
|
1417
1405
|
const path = parent === undefined ? node.key : `${parent}.${node.key}`
|
|
1418
1406
|
if (node._tag === "History") continue
|
|
1419
1407
|
if (node._tag === "Choice") {
|
|
1408
|
+
const group = `choice:${path}`
|
|
1409
|
+
branches[group] = { destination: selectDefinitionTarget(references, path, node.selected, byPath) }
|
|
1420
1410
|
handlers[node.key] = {
|
|
1421
|
-
choice:
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1411
|
+
choice: {
|
|
1412
|
+
branches: group,
|
|
1413
|
+
resolve: ({ select }: { readonly select: any }) =>
|
|
1414
|
+
resolveDefinitionTarget(select.destination, path, node.selected, byPath)
|
|
1415
|
+
}
|
|
1425
1416
|
}
|
|
1426
1417
|
continue
|
|
1427
1418
|
}
|
|
@@ -1434,15 +1425,19 @@ const makeHandlers = (
|
|
|
1434
1425
|
let onDone: unknown
|
|
1435
1426
|
for (const transition of transitions.values()) {
|
|
1436
1427
|
if (transition.source !== path) continue
|
|
1437
|
-
const
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
: selectDefinitionTarget(
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1428
|
+
const group = `transition:${path}:${triggerKey(transition.trigger)}`
|
|
1429
|
+
if (transition.target !== undefined) {
|
|
1430
|
+
branches[group] = {
|
|
1431
|
+
destination: selectDefinitionTarget(references, path, transition.target, byPath)
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
const config = {
|
|
1435
|
+
...(transition.target === undefined ? { none: true } : {
|
|
1436
|
+
branches: group,
|
|
1437
|
+
resolve: ({ select }: { readonly select: any }) =>
|
|
1438
|
+
resolveDefinitionTarget(select.destination, path, transition.target!, byPath, transition.targetValue)
|
|
1439
|
+
}),
|
|
1440
|
+
...("reenter" in transition && transition.reenter ? { reenter: true } : {})
|
|
1446
1441
|
}
|
|
1447
1442
|
if (transition.trigger.type === "event") on[transition.trigger.event] = config
|
|
1448
1443
|
else if (transition.trigger.type === "always") always = config
|
|
@@ -1477,19 +1472,22 @@ const makeHandlers = (
|
|
|
1477
1472
|
...(Object.keys(history).length === 0 ? {} : { history }),
|
|
1478
1473
|
...(node._tag === "Compound"
|
|
1479
1474
|
? {
|
|
1480
|
-
|
|
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
|
+
}
|
|
1481
1482
|
}
|
|
1482
1483
|
: {
|
|
1483
|
-
|
|
1484
|
+
initial: Object.fromEntries(
|
|
1484
1485
|
node.states
|
|
1485
1486
|
.filter((child) => child._tag !== "History" && child._tag !== "Choice")
|
|
1486
|
-
.
|
|
1487
|
-
|
|
1488
|
-
current[child.key].decoded(stateValue(byPath.get(`${path}.${child.key}`)!)),
|
|
1489
|
-
builder
|
|
1490
|
-
)
|
|
1487
|
+
.map((child) => [child.key, { decoded: true, data: stateValue(byPath.get(`${path}.${child.key}`)!) }])
|
|
1488
|
+
)
|
|
1491
1489
|
}),
|
|
1492
|
-
states: makeHandlers(node.states, path, byPath, transitions)
|
|
1490
|
+
states: makeHandlers(node.states, path, byPath, transitions, references, branches)
|
|
1493
1491
|
}
|
|
1494
1492
|
: {})
|
|
1495
1493
|
}
|
|
@@ -1512,21 +1510,28 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
|
|
|
1512
1510
|
const flattened = validateModel(model)
|
|
1513
1511
|
const byPath = new Map(flattened.map((state) => [state.path, state]))
|
|
1514
1512
|
const stateTree = makeStateTree(model.roots, undefined)
|
|
1515
|
-
const defined = Machine.state({
|
|
1513
|
+
const defined = Machine.state({ states: stateTree } as any)
|
|
1516
1514
|
const eventSchemas = model.events.map((event) => Schema.TaggedStruct(event, {}))
|
|
1517
1515
|
const initial = byPath.get(model.initial)!
|
|
1518
|
-
const machine = Machine.make({
|
|
1519
|
-
root: defined,
|
|
1520
|
-
events: Machine.eventsFromSchemas(...eventSchemas) as any
|
|
1521
|
-
} as any)
|
|
1522
1516
|
const transitions = new Map(model.transitions.map((transition) => [
|
|
1523
1517
|
`${transition.source}\u0000${triggerKey(transition.trigger)}`,
|
|
1524
1518
|
transition
|
|
1525
1519
|
]))
|
|
1520
|
+
const branches: Record<string, Readonly<Record<string, unknown>>> = Object.create(null)
|
|
1521
|
+
const handlers = makeHandlers(model.roots, undefined, byPath, transitions, Machine.targets(defined).root, branches)
|
|
1522
|
+
const machine = Machine.make({
|
|
1523
|
+
root: defined,
|
|
1524
|
+
branches,
|
|
1525
|
+
events: Machine.eventsFromSchemas(...eventSchemas) as any
|
|
1526
|
+
} as any)
|
|
1526
1527
|
return machine.handle(
|
|
1527
1528
|
{
|
|
1528
|
-
|
|
1529
|
-
|
|
1529
|
+
initial: {
|
|
1530
|
+
target: (Machine.targets(defined).root as unknown as Record<string, unknown>)[model.initial],
|
|
1531
|
+
decoded: true,
|
|
1532
|
+
data: stateValue(initial)
|
|
1533
|
+
},
|
|
1534
|
+
states: handlers
|
|
1530
1535
|
} as any
|
|
1531
1536
|
) as Machine.Machine.Any
|
|
1532
1537
|
}
|
|
@@ -8,7 +8,7 @@ import * as Data from "effect/Data"
|
|
|
8
8
|
import * as Effect from "effect/Effect"
|
|
9
9
|
import type * as Machine from "../../../Machine.js"
|
|
10
10
|
import type { Probe, ProbePlan } from "../../../testing/MachineTest.js"
|
|
11
|
-
import * as Runtime from "../../machine/
|
|
11
|
+
import * as Runtime from "../../machine/runtimeProtocol.js"
|
|
12
12
|
|
|
13
13
|
type AnyMachine = Machine.Machine.Any
|
|
14
14
|
|
|
@@ -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)
|