@typeonce/effect-machine 0.34.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -33
- package/dist/Machine.d.ts +408 -483
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +126 -87
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/command.d.ts +0 -1
- package/dist/internal/machine/command.d.ts.map +1 -1
- package/dist/internal/machine/command.js +0 -14
- package/dist/internal/machine/command.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +0 -1
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +15 -21
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/construction.d.ts +9 -0
- package/dist/internal/machine/construction.d.ts.map +1 -0
- package/dist/internal/machine/construction.js +132 -0
- package/dist/internal/machine/construction.js.map +1 -0
- package/dist/internal/machine/declaration.d.ts +1 -0
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +3 -8
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +21 -27
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/implementation.d.ts +2 -3
- package/dist/internal/machine/implementation.d.ts.map +1 -1
- package/dist/internal/machine/implementation.js.map +1 -1
- package/dist/internal/machine/initialDeclaration.d.ts +14 -0
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -0
- package/dist/internal/machine/initialDeclaration.js +137 -0
- package/dist/internal/machine/initialDeclaration.js.map +1 -0
- package/dist/internal/machine/machine.d.ts +0 -2
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +101 -282
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +8 -0
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +86 -82
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/protocol.d.ts +1 -1
- package/dist/internal/machine/protocol.d.ts.map +1 -1
- package/dist/internal/machine/protocol.js.map +1 -1
- package/dist/internal/machine/stateDefinition.d.ts +1 -1
- package/dist/internal/machine/stateDefinition.d.ts.map +1 -1
- package/dist/internal/machine/stateDefinition.js +1 -13
- package/dist/internal/machine/stateDefinition.js.map +1 -1
- package/dist/internal/machine/targetReference.d.ts.map +1 -1
- package/dist/internal/machine/targetReference.js +14 -4
- package/dist/internal/machine/targetReference.js.map +1 -1
- package/dist/internal/machine/topology.d.ts +6 -0
- package/dist/internal/machine/topology.d.ts.map +1 -1
- package/dist/internal/machine/topology.js +3 -2
- package/dist/internal/machine/topology.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +51 -82
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/internal/testing/machine/verification.d.ts.map +1 -1
- package/dist/internal/testing/machine/verification.js +7 -1
- package/dist/internal/testing/machine/verification.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +105 -73
- package/dist/testing/MachineTest.d.ts.map +1 -1
- package/dist/testing/MachineTest.js +105 -73
- package/dist/testing/MachineTest.js.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.d.ts +13 -8
- package/dist/unstable/cluster/ClusterMachine.d.ts.map +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +13 -8
- package/dist/unstable/cluster/ClusterMachine.js.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +41 -24
- package/dist/unstable/reactivity/AtomMachine.d.ts.map +1 -1
- package/dist/unstable/reactivity/AtomMachine.js +41 -24
- package/dist/unstable/reactivity/AtomMachine.js.map +1 -1
- package/docs/agent-guide.md +37 -34
- package/docs/root-api.md +143 -190
- package/package.json +1 -1
- package/src/Machine.ts +631 -872
- package/src/internal/machine/command.ts +0 -16
- package/src/internal/machine/configuration.ts +13 -30
- package/src/internal/machine/construction.ts +147 -0
- package/src/internal/machine/declaration.ts +4 -7
- package/src/internal/machine/executionPlan.ts +20 -25
- package/src/internal/machine/implementation.ts +2 -3
- package/src/internal/machine/initialDeclaration.ts +157 -0
- package/src/internal/machine/machine.ts +118 -396
- package/src/internal/machine/planner.ts +97 -99
- package/src/internal/machine/protocol.ts +3 -3
- package/src/internal/machine/stateDefinition.ts +1 -15
- package/src/internal/machine/targetReference.ts +19 -4
- package/src/internal/machine/topology.ts +7 -2
- package/src/internal/testing/machine/finiteModel.ts +60 -132
- package/src/internal/testing/machine/verification.ts +7 -1
- package/src/testing/MachineTest.ts +105 -73
- package/src/unstable/cluster/ClusterMachine.ts +13 -8
- package/src/unstable/reactivity/AtomMachine.ts +41 -24
- package/dist/internal/machine/targetBuilder.d.ts +0 -16
- package/dist/internal/machine/targetBuilder.d.ts.map +0 -1
- package/dist/internal/machine/targetBuilder.js +0 -333
- package/dist/internal/machine/targetBuilder.js.map +0 -1
- package/src/internal/machine/targetBuilder.ts +0 -525
|
@@ -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 {
|
|
@@ -1218,101 +1217,52 @@ const makeStateTree = (
|
|
|
1218
1217
|
return tree
|
|
1219
1218
|
}
|
|
1220
1219
|
|
|
1221
|
-
const
|
|
1222
|
-
builder: Record<string, any>,
|
|
1220
|
+
const construction = (
|
|
1223
1221
|
path: string,
|
|
1224
1222
|
byPath: ReadonlyMap<string, FlatFiniteState>,
|
|
1225
1223
|
requestedParts: ReadonlyArray<string> | undefined,
|
|
1226
1224
|
index: number,
|
|
1227
1225
|
sourcePath?: string,
|
|
1228
1226
|
requestedValue?: number
|
|
1229
|
-
): unknown => {
|
|
1227
|
+
): Readonly<Record<string, unknown>> => {
|
|
1230
1228
|
const state = byPath.get(path)!
|
|
1231
|
-
if (state.node._tag === "History") {
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
if (state.node._tag === "
|
|
1235
|
-
return (builder[state.node.key] as () => unknown)()
|
|
1236
|
-
}
|
|
1237
|
-
const method = builder[state.node.key].decoded as (
|
|
1238
|
-
value: unknown,
|
|
1239
|
-
selector?: (builder: any) => unknown
|
|
1240
|
-
) => unknown
|
|
1241
|
-
const value = stateValue(state, path === requestedParts?.join(".") ? requestedValue : undefined)
|
|
1242
|
-
if (state.node._tag === "Atomic" || state.node._tag === "Final") return method(value)
|
|
1243
|
-
|
|
1229
|
+
if (state.node._tag === "History") throw new Error(`MachineTest cannot construct history state "${path}"`)
|
|
1230
|
+
if (state.node._tag === "Choice") return {}
|
|
1231
|
+
const data = stateValue(state, path === requestedParts?.join(".") ? requestedValue : undefined)
|
|
1232
|
+
if (state.node._tag === "Atomic" || state.node._tag === "Final") return { data, decoded: true }
|
|
1244
1233
|
const requestedChild = requestedParts?.[index + 1]
|
|
1234
|
+
let keys: ReadonlyArray<string>
|
|
1245
1235
|
if (state.node._tag === "Parallel") {
|
|
1246
|
-
const
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
selectSnapshot(
|
|
1256
|
-
children,
|
|
1257
|
-
`${path}.${childKey}`,
|
|
1258
|
-
byPath,
|
|
1259
|
-
requestedChild === undefined ? undefined : requestedParts,
|
|
1260
|
-
index + 1,
|
|
1261
|
-
sourcePath,
|
|
1262
|
-
requestedValue
|
|
1263
|
-
)
|
|
1236
|
+
const inside = sourcePath === path || sourcePath?.startsWith(`${path}.`) === true
|
|
1237
|
+
keys = inside ?
|
|
1238
|
+
[
|
|
1239
|
+
requestedChild ?? (sourcePath === path
|
|
1240
|
+
? state.node.states.find((child) => child._tag !== "History" && child._tag !== "Choice")!.key
|
|
1241
|
+
: sourcePath!.slice(path.length + 1).split(".")[0]!)
|
|
1242
|
+
]
|
|
1243
|
+
: state.node.states.filter((child) => child._tag !== "History" && child._tag !== "Choice").map((child) =>
|
|
1244
|
+
child.key
|
|
1264
1245
|
)
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1246
|
+
} else keys = [requestedChild ?? state.node.initial]
|
|
1247
|
+
return {
|
|
1248
|
+
data,
|
|
1249
|
+
decoded: true,
|
|
1250
|
+
states: Object.fromEntries(
|
|
1251
|
+
keys.map((
|
|
1252
|
+
key
|
|
1253
|
+
) => [
|
|
1254
|
+
key,
|
|
1255
|
+
construction(
|
|
1256
|
+
`${path}.${key}`,
|
|
1273
1257
|
byPath,
|
|
1274
|
-
|
|
1258
|
+
key === requestedChild ? requestedParts : undefined,
|
|
1275
1259
|
index + 1,
|
|
1276
1260
|
sourcePath,
|
|
1277
1261
|
requestedValue
|
|
1278
1262
|
)
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
})
|
|
1282
|
-
}
|
|
1283
|
-
|
|
1284
|
-
const childKey = requestedChild ?? state.node.initial
|
|
1285
|
-
const childPath = `${path}.${childKey}`
|
|
1286
|
-
return method(
|
|
1287
|
-
value,
|
|
1288
|
-
(children: Record<string, any>) =>
|
|
1289
|
-
selectSnapshot(
|
|
1290
|
-
children,
|
|
1291
|
-
childPath,
|
|
1292
|
-
byPath,
|
|
1293
|
-
requestedChild === undefined ? undefined : requestedParts,
|
|
1294
|
-
index + 1,
|
|
1295
|
-
sourcePath,
|
|
1296
|
-
requestedValue
|
|
1297
|
-
)
|
|
1298
|
-
)
|
|
1299
|
-
}
|
|
1300
|
-
|
|
1301
|
-
const findSnapshot = (snapshot: unknown, path: string): unknown => {
|
|
1302
|
-
if (typeof snapshot !== "object" || snapshot === null) return undefined
|
|
1303
|
-
const current = snapshot as Record<string, unknown>
|
|
1304
|
-
if (current.path === path) return snapshot
|
|
1305
|
-
if (current.state !== undefined) {
|
|
1306
|
-
const found = findSnapshot(current.state, path)
|
|
1307
|
-
if (found !== undefined) return found
|
|
1308
|
-
}
|
|
1309
|
-
if (typeof current.states === "object" && current.states !== null) {
|
|
1310
|
-
for (const child of Object.values(current.states)) {
|
|
1311
|
-
const found = findSnapshot(child, path)
|
|
1312
|
-
if (found !== undefined) return found
|
|
1313
|
-
}
|
|
1263
|
+
])
|
|
1264
|
+
)
|
|
1314
1265
|
}
|
|
1315
|
-
return undefined
|
|
1316
1266
|
}
|
|
1317
1267
|
|
|
1318
1268
|
const selectableDefinitionTarget = (
|
|
@@ -1350,47 +1300,19 @@ const selectDefinitionTarget = (
|
|
|
1350
1300
|
}
|
|
1351
1301
|
|
|
1352
1302
|
const resolveDefinitionTarget = (
|
|
1353
|
-
|
|
1303
|
+
build: (config?: unknown) => unknown,
|
|
1354
1304
|
source: string,
|
|
1355
1305
|
target: string,
|
|
1356
1306
|
byPath: ReadonlyMap<string, FlatFiniteState>,
|
|
1357
1307
|
value?: number
|
|
1358
1308
|
): unknown => {
|
|
1359
1309
|
const selected = byPath.get(target)!
|
|
1360
|
-
if (selected.node._tag === "History") return
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
if (selectable !== target) {
|
|
1367
|
-
const bound = byPath.get(selectable)!
|
|
1368
|
-
const parts = target.split(".")
|
|
1369
|
-
return selectSnapshot(
|
|
1370
|
-
{ [bound.node.key]: builder },
|
|
1371
|
-
selectable,
|
|
1372
|
-
byPath,
|
|
1373
|
-
parts,
|
|
1374
|
-
selectable.split(".").length - 1,
|
|
1375
|
-
source,
|
|
1376
|
-
value
|
|
1377
|
-
)
|
|
1378
|
-
}
|
|
1379
|
-
if (selected.node._tag === "Choice") return builder()
|
|
1380
|
-
const input = { value: value ?? selected.node.value }
|
|
1381
|
-
if (selected.node._tag === "Compound" || selected.node._tag === "Parallel") {
|
|
1382
|
-
const parts = target.split(".")
|
|
1383
|
-
return selectSnapshot(
|
|
1384
|
-
{ [selected.node.key]: builder },
|
|
1385
|
-
target,
|
|
1386
|
-
byPath,
|
|
1387
|
-
parts,
|
|
1388
|
-
parts.length - 1,
|
|
1389
|
-
source,
|
|
1390
|
-
value
|
|
1391
|
-
)
|
|
1392
|
-
}
|
|
1393
|
-
return builder.from(input)
|
|
1310
|
+
if (selected.node._tag === "History") return build()
|
|
1311
|
+
const parts = target.split(".")
|
|
1312
|
+
const path = selected.root !== byPath.get(source)!.root
|
|
1313
|
+
? parts[0]!
|
|
1314
|
+
: selectableDefinitionTarget(source, target, byPath)
|
|
1315
|
+
return build(construction(path, byPath, parts, path.split(".").length - 1, source, value))
|
|
1394
1316
|
}
|
|
1395
1317
|
|
|
1396
1318
|
const makeHandlers = (
|
|
@@ -1449,16 +1371,15 @@ const makeHandlers = (
|
|
|
1449
1371
|
if (child._tag !== "History") return []
|
|
1450
1372
|
return [[child.key, {
|
|
1451
1373
|
default: ({ target: root }: { readonly target: any }) =>
|
|
1452
|
-
root
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1374
|
+
root({
|
|
1375
|
+
states: {
|
|
1376
|
+
[byPath.get(child.fallback)!.root]: construction(
|
|
1377
|
+
byPath.get(child.fallback)!.root,
|
|
1378
|
+
byPath,
|
|
1379
|
+
child.fallback.split("."),
|
|
1380
|
+
0
|
|
1459
1381
|
)
|
|
1460
1382
|
}
|
|
1461
|
-
return completeRoot
|
|
1462
1383
|
})
|
|
1463
1384
|
}]]
|
|
1464
1385
|
}))
|
|
@@ -1473,17 +1394,20 @@ const makeHandlers = (
|
|
|
1473
1394
|
...(Object.keys(history).length === 0 ? {} : { history }),
|
|
1474
1395
|
...(node._tag === "Compound"
|
|
1475
1396
|
? {
|
|
1476
|
-
|
|
1397
|
+
initial: {
|
|
1398
|
+
target: `${path}.${node.initial}`.split(".").reduce((ref, key) => ref[key], references),
|
|
1399
|
+
...(byPath.get(`${path}.${node.initial}`)!.node._tag === "Choice" ? {} : {
|
|
1400
|
+
decoded: true,
|
|
1401
|
+
data: stateValue(byPath.get(`${path}.${node.initial}`)!)
|
|
1402
|
+
})
|
|
1403
|
+
}
|
|
1477
1404
|
}
|
|
1478
1405
|
: {
|
|
1479
|
-
|
|
1406
|
+
initial: Object.fromEntries(
|
|
1480
1407
|
node.states
|
|
1481
1408
|
.filter((child) => child._tag !== "History" && child._tag !== "Choice")
|
|
1482
|
-
.
|
|
1483
|
-
|
|
1484
|
-
current[child.key].decoded(stateValue(byPath.get(`${path}.${child.key}`)!)),
|
|
1485
|
-
builder
|
|
1486
|
-
)
|
|
1409
|
+
.map((child) => [child.key, { decoded: true, data: stateValue(byPath.get(`${path}.${child.key}`)!) }])
|
|
1410
|
+
)
|
|
1487
1411
|
}),
|
|
1488
1412
|
states: makeHandlers(node.states, path, byPath, transitions, references, branches)
|
|
1489
1413
|
}
|
|
@@ -1508,7 +1432,7 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
|
|
|
1508
1432
|
const flattened = validateModel(model)
|
|
1509
1433
|
const byPath = new Map(flattened.map((state) => [state.path, state]))
|
|
1510
1434
|
const stateTree = makeStateTree(model.roots, undefined)
|
|
1511
|
-
const defined = Machine.state({
|
|
1435
|
+
const defined = Machine.state({ states: stateTree } as any)
|
|
1512
1436
|
const eventSchemas = model.events.map((event) => Schema.TaggedStruct(event, {}))
|
|
1513
1437
|
const initial = byPath.get(model.initial)!
|
|
1514
1438
|
const transitions = new Map(model.transitions.map((transition) => [
|
|
@@ -1524,7 +1448,11 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
|
|
|
1524
1448
|
} as any)
|
|
1525
1449
|
return machine.handle(
|
|
1526
1450
|
{
|
|
1527
|
-
|
|
1451
|
+
initial: {
|
|
1452
|
+
target: (Machine.targets(defined).root as unknown as Record<string, unknown>)[model.initial],
|
|
1453
|
+
decoded: true,
|
|
1454
|
+
data: stateValue(initial)
|
|
1455
|
+
},
|
|
1528
1456
|
states: handlers
|
|
1529
1457
|
} as any
|
|
1530
1458
|
) 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)
|
|
@@ -341,18 +341,23 @@ export const layerMemory: Layer.Layer<Storage> = internal.layerMemory
|
|
|
341
341
|
* **Example**
|
|
342
342
|
*
|
|
343
343
|
* ```ts
|
|
344
|
-
* import { Schema } from "effect"
|
|
345
344
|
* import { Machine } from "@typeonce/effect-machine"
|
|
346
345
|
* import { ClusterMachine } from "@typeonce/effect-machine/cluster"
|
|
347
|
-
*
|
|
348
|
-
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
349
|
-
*
|
|
346
|
+
* import { Schema } from "effect"
|
|
347
|
+
* class Idle extends Schema.TaggedClass<Idle>("Idle")("Idle", {}) {
|
|
348
|
+
* }
|
|
349
|
+
* const States = Machine.state({ states: { Idle } })
|
|
350
350
|
* const machine = Machine.make({
|
|
351
351
|
* root: States,
|
|
352
|
-
* events: Machine.eventsFromSchemas()
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
*
|
|
352
|
+
* events: Machine.eventsFromSchemas()
|
|
353
|
+
* }).handle({
|
|
354
|
+
* initial: {
|
|
355
|
+
* target: Machine.targets(States).root.Idle
|
|
356
|
+
* },
|
|
357
|
+
* states: {
|
|
358
|
+
* Idle: {}
|
|
359
|
+
* }
|
|
360
|
+
* })
|
|
356
361
|
* const adapter = ClusterMachine.make("IdleMachine", machine, { version: "1" })
|
|
357
362
|
* ```
|
|
358
363
|
*
|