@typeonce/effect-machine 0.38.1 → 0.39.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 +42 -35
- package/dist/Machine.d.ts +62 -88
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +11 -24
- package/dist/Machine.js.map +1 -1
- package/dist/internal/machine/atom.d.ts.map +1 -1
- package/dist/internal/machine/atom.js.map +1 -1
- package/dist/internal/machine/declaration.d.ts +10 -3
- package/dist/internal/machine/declaration.d.ts.map +1 -1
- package/dist/internal/machine/declaration.js +47 -16
- package/dist/internal/machine/declaration.js.map +1 -1
- package/dist/internal/machine/errors.d.ts +15 -1
- package/dist/internal/machine/errors.d.ts.map +1 -1
- package/dist/internal/machine/errors.js +10 -0
- package/dist/internal/machine/errors.js.map +1 -1
- package/dist/internal/machine/initialDeclaration.d.ts +2 -2
- package/dist/internal/machine/initialDeclaration.d.ts.map +1 -1
- package/dist/internal/machine/initialDeclaration.js +8 -9
- package/dist/internal/machine/initialDeclaration.js.map +1 -1
- package/dist/internal/machine/machine.d.ts +26 -76
- package/dist/internal/machine/machine.d.ts.map +1 -1
- package/dist/internal/machine/machine.js +21 -16
- package/dist/internal/machine/machine.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +216 -31
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +6 -12
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.d.ts +5 -9
- package/dist/internal/machine/process.d.ts.map +1 -1
- package/dist/internal/machine/process.js +19 -32
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/testing/machine/finiteModel.d.ts.map +1 -1
- package/dist/internal/testing/machine/finiteModel.js +9 -12
- package/dist/internal/testing/machine/finiteModel.js.map +1 -1
- package/dist/testing/MachineTest.d.ts +7 -7
- package/dist/testing/MachineTest.js +7 -7
- package/dist/unstable/cluster/ClusterMachine.d.ts +1 -1
- package/dist/unstable/cluster/ClusterMachine.js +1 -1
- package/dist/unstable/reactivity/AtomMachine.d.ts +3 -3
- package/dist/unstable/reactivity/AtomMachine.js +3 -3
- package/docs/agent-guide.md +23 -30
- package/docs/machine-review.md +5 -6
- package/docs/root-api.md +27 -19
- package/package.json +1 -1
- package/src/Machine.ts +125 -163
- package/src/internal/machine/atom.ts +4 -7
- package/src/internal/machine/declaration.ts +56 -17
- package/src/internal/machine/errors.ts +22 -1
- package/src/internal/machine/initialDeclaration.ts +10 -12
- package/src/internal/machine/machine.ts +85 -465
- package/src/internal/machine/planner.ts +84 -190
- package/src/internal/machine/process.ts +80 -279
- package/src/internal/testing/machine/finiteModel.ts +7 -11
- package/src/testing/MachineTest.ts +7 -7
- package/src/unstable/cluster/ClusterMachine.ts +1 -1
- package/src/unstable/reactivity/AtomMachine.ts +3 -3
- package/dist/internal/machine/targetReference.d.ts +0 -15
- package/dist/internal/machine/targetReference.d.ts.map +0 -1
- package/dist/internal/machine/targetReference.js +0 -41
- package/dist/internal/machine/targetReference.js.map +0 -1
- package/src/internal/machine/targetReference.ts +0 -55
|
@@ -4,32 +4,28 @@
|
|
|
4
4
|
* @since 0.4.0
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import * as Cause from "effect/Cause"
|
|
8
7
|
import * as Effect from "effect/Effect"
|
|
9
8
|
import * as Option from "effect/Option"
|
|
10
|
-
import type
|
|
11
|
-
import type { ActionError, ExecutionServices, Machine, Runtime } from "../../Machine.js"
|
|
9
|
+
import type { Machine, Runtime } from "../../Machine.js"
|
|
12
10
|
import * as CommandRuntime from "./commandRuntime.js"
|
|
13
11
|
import * as Configuration from "./configuration.js"
|
|
14
|
-
import {
|
|
15
|
-
import type { StoppedError } from "./errors.js"
|
|
12
|
+
import { failPlanning, toStartupFailure } from "./errors.js"
|
|
16
13
|
import * as ExecutionPlan from "./executionPlan.js"
|
|
17
14
|
import { type CapturedStateConfig, toImpl } from "./implementation.js"
|
|
18
15
|
import * as Invocation from "./invocation.js"
|
|
19
16
|
import * as internalPlanner from "./planner.js"
|
|
20
|
-
import type { ExcludeCompatibleRuntime } from "./requirements.js"
|
|
21
17
|
import * as internalRuntime from "./runtime.js"
|
|
22
18
|
import * as internalRuntimeProtocol from "./runtimeProtocol.js"
|
|
23
19
|
import * as Serialization from "./serialization.js"
|
|
24
20
|
|
|
25
|
-
type ProcessEntry
|
|
21
|
+
type ProcessEntry =
|
|
26
22
|
| {
|
|
27
23
|
readonly _tag: "Initial"
|
|
28
|
-
readonly args:
|
|
24
|
+
readonly args: ReadonlyArray<unknown>
|
|
29
25
|
}
|
|
30
26
|
| {
|
|
31
27
|
readonly _tag: "Resume"
|
|
32
|
-
readonly snapshot: Machine.Snapshot<
|
|
28
|
+
readonly snapshot: Machine.Snapshot<any>
|
|
33
29
|
}
|
|
34
30
|
|
|
35
31
|
const runSequentialDiscard = <E, R>(
|
|
@@ -129,9 +125,7 @@ const makeChildlessCompiledDrain = (
|
|
|
129
125
|
context.scope
|
|
130
126
|
)
|
|
131
127
|
} catch (error) {
|
|
132
|
-
return error
|
|
133
|
-
? Effect.fail(error)
|
|
134
|
-
: Effect.die(error)
|
|
128
|
+
return failPlanning(error)
|
|
135
129
|
}
|
|
136
130
|
configuration = planned.next
|
|
137
131
|
context.executionState = configuration
|
|
@@ -254,9 +248,7 @@ const makeInvokingCompiledDrain = (
|
|
|
254
248
|
scope
|
|
255
249
|
)
|
|
256
250
|
} catch (error) {
|
|
257
|
-
return error
|
|
258
|
-
? Effect.fail(error)
|
|
259
|
-
: Effect.die(error)
|
|
251
|
+
return failPlanning(error)
|
|
260
252
|
}
|
|
261
253
|
configuration = planned.next
|
|
262
254
|
if (planned.microsteps.length === 0) {
|
|
@@ -370,69 +362,31 @@ const makeInvokingCompiledDrain = (
|
|
|
370
362
|
}
|
|
371
363
|
}
|
|
372
364
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
entry: ProcessEntry<States, Input>
|
|
388
|
-
) => internalRuntimeProtocol.ProcessLogic<
|
|
389
|
-
Machine.Snapshot<States>,
|
|
390
|
-
Machine.EventOf<Events>,
|
|
391
|
-
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
392
|
-
ExcludeCompatibleRuntime<
|
|
393
|
-
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
394
|
-
Machine.EventOf<Events>,
|
|
395
|
-
Machine.EmittedEventOf<Emits>
|
|
396
|
-
>,
|
|
397
|
-
Output,
|
|
398
|
-
| InitialE
|
|
399
|
-
| E
|
|
400
|
-
| ActionError<InitialR | R>
|
|
401
|
-
| InfiniteTransitionError
|
|
402
|
-
| MachineSchemaDecodeError
|
|
403
|
-
| StartupError
|
|
404
|
-
| StoppedError
|
|
405
|
-
> = <
|
|
406
|
-
const States extends Machine.StateSchemas,
|
|
407
|
-
const Events extends ReadonlyArray<Machine.TaggedSchema>,
|
|
408
|
-
const Emits extends ReadonlyArray<Machine.TaggedSchema> = readonly [],
|
|
409
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
410
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
411
|
-
E = never,
|
|
412
|
-
R = never,
|
|
413
|
-
InitialE = never,
|
|
414
|
-
InitialR = never,
|
|
415
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
416
|
-
Output = never
|
|
417
|
-
>(
|
|
418
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
419
|
-
entry: ProcessEntry<States, Input>
|
|
420
|
-
) => {
|
|
365
|
+
// Planned initial states only carry an output once the machine is done.
|
|
366
|
+
const initialResult = (
|
|
367
|
+
planned: { readonly state: Machine.Snapshot<any>; readonly done: boolean; readonly output: unknown }
|
|
368
|
+
): internalRuntimeProtocol.CompiledProcessInitial<Machine.Snapshot<any>, unknown> =>
|
|
369
|
+
planned.done
|
|
370
|
+
? { state: planned.state, done: true, output: planned.output }
|
|
371
|
+
: { state: planned.state, done: false, output: undefined }
|
|
372
|
+
|
|
373
|
+
// Process logic executes erased machine definitions. The public `Machine`
|
|
374
|
+
// module owns the typed contract that specializes this boundary.
|
|
375
|
+
const makeProcessLogic = (
|
|
376
|
+
machine: Machine.Any,
|
|
377
|
+
entry: ProcessEntry
|
|
378
|
+
): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> => {
|
|
421
379
|
const hasInvokes = hasInvokeCapability(machine)
|
|
422
380
|
const executionPlan = ExecutionPlan.compileExecutionPlan(machine)
|
|
423
381
|
const initialArgs = entry._tag === "Initial" ? entry.args : []
|
|
424
382
|
const compiledInitial = entry._tag === "Initial" ? executionPlan.initial : undefined
|
|
425
383
|
const makeCompiledInitial = compiledInitial === undefined ? undefined : (
|
|
426
|
-
scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<
|
|
384
|
+
scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<any>>
|
|
427
385
|
) => {
|
|
428
386
|
try {
|
|
429
387
|
const planned = compiledInitial(initialArgs, scope)
|
|
430
388
|
scope.inspectInitial(planned.initialEntryPaths)
|
|
431
|
-
const result =
|
|
432
|
-
state: planned.state as Machine.Snapshot<States>,
|
|
433
|
-
done: planned.done,
|
|
434
|
-
output: planned.output as Output | undefined
|
|
435
|
-
}
|
|
389
|
+
const result = initialResult(planned)
|
|
436
390
|
return hasInvokes
|
|
437
391
|
? {
|
|
438
392
|
...result,
|
|
@@ -444,13 +398,11 @@ const makeProcessLogic: <
|
|
|
444
398
|
}
|
|
445
399
|
: result
|
|
446
400
|
} catch (error) {
|
|
447
|
-
throw error
|
|
448
|
-
? error
|
|
449
|
-
: new StartupError({ cause: Cause.die(error) })
|
|
401
|
+
throw toStartupFailure(error)
|
|
450
402
|
}
|
|
451
403
|
}
|
|
452
404
|
const makeInitial = (
|
|
453
|
-
scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<
|
|
405
|
+
scope: internalRuntimeProtocol.ProcessScope<Machine.EventOf<any>>
|
|
454
406
|
) =>
|
|
455
407
|
compiledInitial === undefined
|
|
456
408
|
? internalRuntimeProtocol.provideMachineRuntime(
|
|
@@ -464,13 +416,9 @@ const makeProcessLogic: <
|
|
|
464
416
|
? undefined
|
|
465
417
|
: CommandRuntime.runEmittedEvents(
|
|
466
418
|
planned.emittedEvents,
|
|
467
|
-
CommandRuntime.makeLiveRuntime
|
|
419
|
+
CommandRuntime.makeLiveRuntime(machine, scope)
|
|
468
420
|
)
|
|
469
|
-
const result = Effect.succeed(
|
|
470
|
-
state: planned.state,
|
|
471
|
-
done: planned.done,
|
|
472
|
-
output: planned.output
|
|
473
|
-
})
|
|
421
|
+
const result = Effect.succeed(initialResult(planned))
|
|
474
422
|
return commands === undefined
|
|
475
423
|
? emitted === undefined ? result : emitted.pipe(Effect.andThen(result))
|
|
476
424
|
: emitted === undefined
|
|
@@ -480,14 +428,14 @@ const makeProcessLogic: <
|
|
|
480
428
|
),
|
|
481
429
|
scope
|
|
482
430
|
)
|
|
483
|
-
: Effect.try({ try: () => makeCompiledInitial!(scope), catch:
|
|
431
|
+
: Effect.try({ try: () => makeCompiledInitial!(scope), catch: toStartupFailure })
|
|
484
432
|
return ({
|
|
485
433
|
inspection: { kind: "Machine", definition: machine },
|
|
486
434
|
execution: {
|
|
487
435
|
_tag: "Compiled",
|
|
488
436
|
childless: !hasInvokes,
|
|
489
|
-
|
|
490
|
-
initialSync: makeCompiledInitial,
|
|
437
|
+
...(entry._tag === "Initial" ? { initial: makeInitial } : {}),
|
|
438
|
+
...(makeCompiledInitial === undefined ? {} : { initialSync: makeCompiledInitial }),
|
|
491
439
|
drain: {
|
|
492
440
|
_tag: "Owned",
|
|
493
441
|
run: hasInvokes
|
|
@@ -509,11 +457,11 @@ const makeProcessLogic: <
|
|
|
509
457
|
if (completeMessage === undefined || pollMessage === undefined || receiveMessage === undefined) {
|
|
510
458
|
return yield* Effect.die(new Error("Machine statechart started without acknowledged mailbox access"))
|
|
511
459
|
}
|
|
512
|
-
let terminal: { readonly output:
|
|
460
|
+
let terminal: { readonly output: any } | undefined
|
|
513
461
|
|
|
514
462
|
let current = yield* state
|
|
515
463
|
if (internalPlanner.isFinalState(machine, current)) {
|
|
516
|
-
return yield* internalPlanner.getFinalOutputEffect
|
|
464
|
+
return yield* internalPlanner.getFinalOutputEffect(
|
|
517
465
|
machine,
|
|
518
466
|
current,
|
|
519
467
|
internalPlanner.InitialEvent
|
|
@@ -528,9 +476,9 @@ const makeProcessLogic: <
|
|
|
528
476
|
// per iteration; every iteration still crosses Effect boundaries,
|
|
529
477
|
// so the Effect scheduler remains responsible for cooperative yield.
|
|
530
478
|
let configuration: Configuration.ActiveConfiguration | undefined
|
|
531
|
-
let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<
|
|
479
|
+
let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<any>>> = Option
|
|
532
480
|
.none()
|
|
533
|
-
let liveRuntime: Runtime<Machine.EventOf<
|
|
481
|
+
let liveRuntime: Runtime<Machine.EventOf<any>, Machine.EmittedEventOf<any>> | undefined
|
|
534
482
|
while (terminal === undefined) {
|
|
535
483
|
const message = Option.isSome(pendingMessage) ? pendingMessage.value : yield* receiveMessage
|
|
536
484
|
pendingMessage = Option.none()
|
|
@@ -548,21 +496,18 @@ const makeProcessLogic: <
|
|
|
548
496
|
event
|
|
549
497
|
)
|
|
550
498
|
} catch (error) {
|
|
551
|
-
|
|
552
|
-
return yield* error
|
|
553
|
-
}
|
|
554
|
-
throw error
|
|
499
|
+
return yield* failPlanning(error)
|
|
555
500
|
}
|
|
556
501
|
configuration = planned.next
|
|
557
502
|
|
|
558
503
|
if (planned.microsteps.length > 0) {
|
|
559
|
-
const next = Configuration.snapshotFromConfiguration
|
|
504
|
+
const next = Configuration.snapshotFromConfiguration(machine, planned.next)
|
|
560
505
|
yield* CommandRuntime.runCommands(planned.commands, context)
|
|
561
506
|
yield* setState(next)
|
|
562
507
|
current = next
|
|
563
508
|
if (planned.emittedEvents.length > 0) {
|
|
564
509
|
yield* CommandRuntime.runEmittedEvents(
|
|
565
|
-
planned.emittedEvents
|
|
510
|
+
planned.emittedEvents,
|
|
566
511
|
liveRuntime ??= CommandRuntime.makeLiveRuntime(machine, context)
|
|
567
512
|
)
|
|
568
513
|
}
|
|
@@ -610,16 +555,15 @@ const makeProcessLogic: <
|
|
|
610
555
|
const startInvokes: (
|
|
611
556
|
configuration: Configuration.ActiveConfiguration,
|
|
612
557
|
paths: ReadonlyArray<string>,
|
|
613
|
-
event: Machine.LifecycleEvent<
|
|
614
|
-
) => Effect.Effect<void,
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
) ?? Effect.void) as Effect.Effect<void, E | MachineSchemaDecodeError, R>
|
|
558
|
+
event: Machine.LifecycleEvent<any>
|
|
559
|
+
) => Effect.Effect<void, any, any> = (configuration, paths, event) => (Invocation.startAll(
|
|
560
|
+
machine,
|
|
561
|
+
context,
|
|
562
|
+
ownedChildren,
|
|
563
|
+
configuration,
|
|
564
|
+
paths,
|
|
565
|
+
event
|
|
566
|
+
) ?? Effect.void)
|
|
623
567
|
const stopInvokes = (paths: ReadonlyArray<string>): Effect.Effect<void> =>
|
|
624
568
|
ownedChildren.stopPaths(paths) ?? Effect.void
|
|
625
569
|
|
|
@@ -637,9 +581,9 @@ const makeProcessLogic: <
|
|
|
637
581
|
// As above, keep the normalized configuration only while this
|
|
638
582
|
// worker can continue draining an already queued batch.
|
|
639
583
|
configuration = undefined
|
|
640
|
-
let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<
|
|
584
|
+
let pendingMessage: Option.Option<internalRuntimeProtocol.ProcessMessage<Machine.EventOf<any>>> = Option
|
|
641
585
|
.none()
|
|
642
|
-
let liveRuntime: Runtime<Machine.EventOf<
|
|
586
|
+
let liveRuntime: Runtime<Machine.EventOf<any>, Machine.EmittedEventOf<any>> | undefined
|
|
643
587
|
|
|
644
588
|
// Match the compact non-invoke loop while retaining state-scoped
|
|
645
589
|
// child lifecycle work at the same ordered Effect boundaries.
|
|
@@ -660,25 +604,22 @@ const makeProcessLogic: <
|
|
|
660
604
|
event
|
|
661
605
|
)
|
|
662
606
|
} catch (error) {
|
|
663
|
-
|
|
664
|
-
return yield* error
|
|
665
|
-
}
|
|
666
|
-
throw error
|
|
607
|
+
return yield* failPlanning(error)
|
|
667
608
|
}
|
|
668
609
|
configuration = planned.next
|
|
669
610
|
if (planned.microsteps.length > 0) {
|
|
670
611
|
const changed = planned.microsteps.some((step) => step.changed)
|
|
671
612
|
const exitPaths = planned.microsteps.flatMap((step) => step.exitPaths)
|
|
672
|
-
const entryEvents = new Map<string, Machine.LifecycleEvent<
|
|
613
|
+
const entryEvents = new Map<string, Machine.LifecycleEvent<any>>()
|
|
673
614
|
for (const step of planned.microsteps) {
|
|
674
615
|
if (step.changed) {
|
|
675
616
|
for (const path of step.entryPaths) {
|
|
676
|
-
entryEvents.set(path, step.event
|
|
617
|
+
entryEvents.set(path, step.event)
|
|
677
618
|
}
|
|
678
619
|
}
|
|
679
620
|
}
|
|
680
621
|
|
|
681
|
-
const next = Configuration.snapshotFromConfiguration
|
|
622
|
+
const next = Configuration.snapshotFromConfiguration(machine, planned.next)
|
|
682
623
|
yield* CommandRuntime.runCommands(planned.commands, context)
|
|
683
624
|
if (changed) {
|
|
684
625
|
yield* stopInvokes(exitPaths)
|
|
@@ -687,7 +628,7 @@ const makeProcessLogic: <
|
|
|
687
628
|
current = next
|
|
688
629
|
if (planned.emittedEvents.length > 0) {
|
|
689
630
|
yield* CommandRuntime.runEmittedEvents(
|
|
690
|
-
planned.emittedEvents
|
|
631
|
+
planned.emittedEvents,
|
|
691
632
|
liveRuntime ??= CommandRuntime.makeLiveRuntime(machine, context)
|
|
692
633
|
)
|
|
693
634
|
}
|
|
@@ -734,24 +675,7 @@ const makeProcessLogic: <
|
|
|
734
675
|
}),
|
|
735
676
|
context
|
|
736
677
|
)
|
|
737
|
-
})
|
|
738
|
-
Machine.Snapshot<States>,
|
|
739
|
-
Machine.EventOf<Events>,
|
|
740
|
-
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
741
|
-
ExcludeCompatibleRuntime<
|
|
742
|
-
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
743
|
-
Machine.EventOf<Events>,
|
|
744
|
-
Machine.EmittedEventOf<Emits>
|
|
745
|
-
>,
|
|
746
|
-
Output,
|
|
747
|
-
| InitialE
|
|
748
|
-
| E
|
|
749
|
-
| ActionError<InitialR | R>
|
|
750
|
-
| InfiniteTransitionError
|
|
751
|
-
| MachineSchemaDecodeError
|
|
752
|
-
| StartupError
|
|
753
|
-
| StoppedError
|
|
754
|
-
>
|
|
678
|
+
})
|
|
755
679
|
}
|
|
756
680
|
|
|
757
681
|
const initialProcessLogicCache = new WeakMap<
|
|
@@ -759,39 +683,10 @@ const initialProcessLogicCache = new WeakMap<
|
|
|
759
683
|
internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any>
|
|
760
684
|
>()
|
|
761
685
|
|
|
762
|
-
export const toProcessLogic
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
767
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
768
|
-
E = never,
|
|
769
|
-
R = never,
|
|
770
|
-
InitialE = never,
|
|
771
|
-
InitialR = never,
|
|
772
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
773
|
-
Output = never
|
|
774
|
-
>(
|
|
775
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
776
|
-
...args: [...Machine.InputArgs<Input>]
|
|
777
|
-
) => internalRuntimeProtocol.ProcessLogic<
|
|
778
|
-
Machine.Snapshot<States>,
|
|
779
|
-
Machine.EventOf<Events>,
|
|
780
|
-
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
781
|
-
ExcludeCompatibleRuntime<
|
|
782
|
-
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
783
|
-
Machine.EventOf<Events>,
|
|
784
|
-
Machine.EmittedEventOf<Emits>
|
|
785
|
-
>,
|
|
786
|
-
Output,
|
|
787
|
-
| InitialE
|
|
788
|
-
| E
|
|
789
|
-
| ActionError<InitialR | R>
|
|
790
|
-
| InfiniteTransitionError
|
|
791
|
-
| MachineSchemaDecodeError
|
|
792
|
-
| StartupError
|
|
793
|
-
| StoppedError
|
|
794
|
-
> = (machine, ...args) => {
|
|
686
|
+
export const toProcessLogic = (
|
|
687
|
+
machine: Machine.Any,
|
|
688
|
+
...args: ReadonlyArray<unknown>
|
|
689
|
+
): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> => {
|
|
795
690
|
if (args.length > 0) {
|
|
796
691
|
return makeProcessLogic(machine, { _tag: "Initial", args })
|
|
797
692
|
}
|
|
@@ -813,7 +708,7 @@ const toResumedProcessLogic = (
|
|
|
813
708
|
machine: Machine.Any,
|
|
814
709
|
snapshot: Machine.Snapshot<any>
|
|
815
710
|
): internalRuntimeProtocol.ProcessLogic<any, any, any, any, any, any> =>
|
|
816
|
-
|
|
711
|
+
makeProcessLogic(machine, { _tag: "Resume", snapshot })
|
|
817
712
|
|
|
818
713
|
/** @internal Test-only runtime strategy selection for a fresh machine. */
|
|
819
714
|
export const startWithRuntimeStrategyForTesting = (
|
|
@@ -822,7 +717,7 @@ export const startWithRuntimeStrategyForTesting = (
|
|
|
822
717
|
...args: ReadonlyArray<unknown>
|
|
823
718
|
): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any>, any, any> =>
|
|
824
719
|
internalRuntime.startProcessWithStrategyForTesting(
|
|
825
|
-
|
|
720
|
+
toProcessLogic(machine, ...args),
|
|
826
721
|
strategy,
|
|
827
722
|
machine.id === undefined ? undefined : { id: machine.id }
|
|
828
723
|
)
|
|
@@ -834,7 +729,7 @@ export const prepareWithRuntimeStrategyForTesting = (
|
|
|
834
729
|
...args: ReadonlyArray<unknown>
|
|
835
730
|
): Effect.Effect<internalRuntimeProtocol.PreparedProcess<any, any, any, any, any, any, any>, any, any> =>
|
|
836
731
|
internalRuntime.prepareProcessWithStrategyForTesting(
|
|
837
|
-
|
|
732
|
+
toProcessLogic(machine, ...args),
|
|
838
733
|
strategy,
|
|
839
734
|
machine.id === undefined ? undefined : { id: machine.id }
|
|
840
735
|
)
|
|
@@ -851,125 +746,31 @@ export const resumeWithRuntimeStrategyForTesting = (
|
|
|
851
746
|
machine.id === undefined ? undefined : { id: machine.id }
|
|
852
747
|
)
|
|
853
748
|
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
E = never,
|
|
861
|
-
R = never,
|
|
862
|
-
InitialE = never,
|
|
863
|
-
InitialR = never,
|
|
864
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
865
|
-
Output = never
|
|
866
|
-
>(
|
|
867
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
868
|
-
...args: [...Machine.InputArgs<Input>]
|
|
869
|
-
) => Effect.Effect<
|
|
870
|
-
internalRuntimeProtocol.MachineRef<
|
|
871
|
-
Machine.Snapshot<States>,
|
|
872
|
-
Machine.EventOf<Events>,
|
|
873
|
-
| E
|
|
874
|
-
| ActionError<R>
|
|
875
|
-
| InfiniteTransitionError
|
|
876
|
-
| MachineSchemaDecodeError
|
|
877
|
-
| StoppedError,
|
|
878
|
-
Output
|
|
879
|
-
>,
|
|
880
|
-
| InitialE
|
|
881
|
-
| E
|
|
882
|
-
| ActionError<InitialR | R>
|
|
883
|
-
| InfiniteTransitionError
|
|
884
|
-
| MachineSchemaDecodeError
|
|
885
|
-
| StartupError
|
|
886
|
-
| StoppedError,
|
|
887
|
-
ExcludeCompatibleRuntime<
|
|
888
|
-
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
889
|
-
Machine.EventOf<Events>,
|
|
890
|
-
Machine.EmittedEventOf<Emits>
|
|
891
|
-
>
|
|
892
|
-
> = (machine, ...args) =>
|
|
749
|
+
// The public `Machine` module owns the typed start, prepare, and resume
|
|
750
|
+
// signatures. These implementations are the erased boundary they specialize.
|
|
751
|
+
export const start = (
|
|
752
|
+
machine: Machine.Any,
|
|
753
|
+
...args: ReadonlyArray<unknown>
|
|
754
|
+
): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any, any>, any, any> =>
|
|
893
755
|
internalRuntime.startProcess(
|
|
894
756
|
toProcessLogic(machine, ...args),
|
|
895
757
|
machine.id === undefined ? undefined : { id: machine.id }
|
|
896
|
-
)
|
|
897
|
-
|
|
898
|
-
export const prepare
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
903
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
904
|
-
E = never,
|
|
905
|
-
R = never,
|
|
906
|
-
InitialE = never,
|
|
907
|
-
InitialR = never,
|
|
908
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
909
|
-
Output = never
|
|
910
|
-
>(
|
|
911
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
912
|
-
...args: [...Machine.InputArgs<Input>]
|
|
913
|
-
) => Effect.Effect<
|
|
914
|
-
internalRuntimeProtocol.PreparedProcess<
|
|
915
|
-
Machine.Snapshot<States>,
|
|
916
|
-
Machine.EventOf<Events>,
|
|
917
|
-
| E
|
|
918
|
-
| ActionError<R>
|
|
919
|
-
| InfiniteTransitionError
|
|
920
|
-
| MachineSchemaDecodeError
|
|
921
|
-
| StoppedError,
|
|
922
|
-
Output,
|
|
923
|
-
Machine.EmittedEventOf<Emits>,
|
|
924
|
-
| InitialE
|
|
925
|
-
| E
|
|
926
|
-
| ActionError<InitialR | R>
|
|
927
|
-
| InfiniteTransitionError
|
|
928
|
-
| MachineSchemaDecodeError
|
|
929
|
-
| StartupError
|
|
930
|
-
| StoppedError,
|
|
931
|
-
ExcludeCompatibleRuntime<
|
|
932
|
-
Exclude<ExecutionServices<InitialR | R>, internalRuntimeProtocol.MachineRuntime>,
|
|
933
|
-
Machine.EventOf<Events>,
|
|
934
|
-
Machine.EmittedEventOf<Emits>
|
|
935
|
-
>
|
|
936
|
-
>
|
|
937
|
-
> = (machine, ...args) =>
|
|
758
|
+
)
|
|
759
|
+
|
|
760
|
+
export const prepare = (
|
|
761
|
+
machine: Machine.Any,
|
|
762
|
+
...args: ReadonlyArray<unknown>
|
|
763
|
+
): Effect.Effect<internalRuntimeProtocol.PreparedProcess<any, any, any, any, any, any, any>> =>
|
|
938
764
|
internalRuntime.prepareProcess(
|
|
939
765
|
toProcessLogic(machine, ...args),
|
|
940
766
|
machine.id === undefined ? undefined : { id: machine.id }
|
|
941
|
-
)
|
|
942
|
-
|
|
943
|
-
export const resume
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
const Input extends Schema.Top = typeof Schema.Void,
|
|
948
|
-
UnhandledStates extends Machine.StateIdentifier<States> = Machine.StateIdentifier<States>,
|
|
949
|
-
E = never,
|
|
950
|
-
R = never,
|
|
951
|
-
InitialE = never,
|
|
952
|
-
InitialR = never,
|
|
953
|
-
FinalStates extends Machine.StateIdentifier<States> = never,
|
|
954
|
-
Output = never
|
|
955
|
-
>(
|
|
956
|
-
machine: Machine<States, Events, Input, UnhandledStates, E, R, InitialE, InitialR, FinalStates, Output, Emits>,
|
|
957
|
-
snapshot: Machine.Snapshot<States>
|
|
958
|
-
) => Effect.Effect<
|
|
959
|
-
internalRuntimeProtocol.MachineRef<
|
|
960
|
-
Machine.Snapshot<States>,
|
|
961
|
-
Machine.EventOf<Events>,
|
|
962
|
-
E | ActionError<R> | InfiniteTransitionError | MachineSchemaDecodeError | StoppedError,
|
|
963
|
-
Output
|
|
964
|
-
>,
|
|
965
|
-
MachineSchemaDecodeError,
|
|
966
|
-
ExcludeCompatibleRuntime<
|
|
967
|
-
Exclude<ExecutionServices<R>, internalRuntimeProtocol.MachineRuntime>,
|
|
968
|
-
Machine.EventOf<Events>,
|
|
969
|
-
Machine.EmittedEventOf<Emits>
|
|
970
|
-
>
|
|
971
|
-
> = (machine, snapshot) =>
|
|
767
|
+
)
|
|
768
|
+
|
|
769
|
+
export const resume = (
|
|
770
|
+
machine: Machine.Any,
|
|
771
|
+
snapshot: Machine.Snapshot<any>
|
|
772
|
+
): Effect.Effect<internalRuntimeProtocol.MachineRef<any, any, any, any, any>, any, any> =>
|
|
972
773
|
internalRuntime.startProcess(
|
|
973
774
|
toResumedProcessLogic(machine, snapshot),
|
|
974
775
|
machine.id === undefined ? undefined : { id: machine.id }
|
|
975
|
-
)
|
|
776
|
+
)
|
|
@@ -1311,7 +1311,6 @@ const selectableDefinitionTarget = (
|
|
|
1311
1311
|
}
|
|
1312
1312
|
|
|
1313
1313
|
const selectDefinitionTarget = (
|
|
1314
|
-
references: Record<string, any>,
|
|
1315
1314
|
source: string,
|
|
1316
1315
|
target: string,
|
|
1317
1316
|
byPath: ReadonlyMap<string, FlatFiniteState>
|
|
@@ -1322,9 +1321,7 @@ const selectDefinitionTarget = (
|
|
|
1322
1321
|
: selected.root !== byPath.get(source)!.root ?
|
|
1323
1322
|
target.split(".")[0]!
|
|
1324
1323
|
: selectableDefinitionTarget(source, target, byPath)
|
|
1325
|
-
|
|
1326
|
-
for (const part of selectedPath.split(".")) reference = reference[part]
|
|
1327
|
-
return selected.node._tag === "History" ? { history: reference } : { target: reference }
|
|
1324
|
+
return selected.node._tag === "History" ? { history: selectedPath } : { target: selectedPath }
|
|
1328
1325
|
}
|
|
1329
1326
|
|
|
1330
1327
|
const resolveDefinitionTarget = (
|
|
@@ -1348,7 +1345,6 @@ const makeHandlers = (
|
|
|
1348
1345
|
parent: string | undefined,
|
|
1349
1346
|
byPath: ReadonlyMap<string, FlatFiniteState>,
|
|
1350
1347
|
transitions: ReadonlyMap<string, FiniteTransition>,
|
|
1351
|
-
references: Record<string, any>,
|
|
1352
1348
|
branches: Record<string, Readonly<Record<string, unknown>>>
|
|
1353
1349
|
): Record<string, unknown> => {
|
|
1354
1350
|
const handlers: Record<string, unknown> = Object.create(null)
|
|
@@ -1357,7 +1353,7 @@ const makeHandlers = (
|
|
|
1357
1353
|
if (node._tag === "History") continue
|
|
1358
1354
|
if (node._tag === "Choice") {
|
|
1359
1355
|
const group = `choice:${path}`
|
|
1360
|
-
branches[group] = { destination: selectDefinitionTarget(
|
|
1356
|
+
branches[group] = { destination: selectDefinitionTarget(path, node.selected, byPath) }
|
|
1361
1357
|
handlers[node.key] = {
|
|
1362
1358
|
choice: {
|
|
1363
1359
|
branches: group,
|
|
@@ -1379,7 +1375,7 @@ const makeHandlers = (
|
|
|
1379
1375
|
const group = `transition:${path}:${triggerKey(transition.trigger)}`
|
|
1380
1376
|
if (transition.target !== undefined) {
|
|
1381
1377
|
branches[group] = {
|
|
1382
|
-
destination: selectDefinitionTarget(
|
|
1378
|
+
destination: selectDefinitionTarget(path, transition.target, byPath)
|
|
1383
1379
|
}
|
|
1384
1380
|
}
|
|
1385
1381
|
const config = {
|
|
@@ -1423,7 +1419,7 @@ const makeHandlers = (
|
|
|
1423
1419
|
...(node._tag === "Compound"
|
|
1424
1420
|
? {
|
|
1425
1421
|
initial: {
|
|
1426
|
-
target: `${path}.${node.initial}
|
|
1422
|
+
target: `${path}.${node.initial}`,
|
|
1427
1423
|
...(byPath.get(`${path}.${node.initial}`)!.node._tag === "Choice" ? {} : {
|
|
1428
1424
|
decoded: true,
|
|
1429
1425
|
data: stateValue(byPath.get(`${path}.${node.initial}`)!)
|
|
@@ -1437,7 +1433,7 @@ const makeHandlers = (
|
|
|
1437
1433
|
.map((child) => [child.key, { decoded: true, data: stateValue(byPath.get(`${path}.${child.key}`)!) }])
|
|
1438
1434
|
)
|
|
1439
1435
|
}),
|
|
1440
|
-
states: makeHandlers(node.states, path, byPath, transitions,
|
|
1436
|
+
states: makeHandlers(node.states, path, byPath, transitions, branches)
|
|
1441
1437
|
}
|
|
1442
1438
|
: {})
|
|
1443
1439
|
}
|
|
@@ -1468,7 +1464,7 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
|
|
|
1468
1464
|
transition
|
|
1469
1465
|
]))
|
|
1470
1466
|
const branches: Record<string, Readonly<Record<string, unknown>>> = Object.create(null)
|
|
1471
|
-
const handlers = makeHandlers(model.roots, undefined, byPath, transitions,
|
|
1467
|
+
const handlers = makeHandlers(model.roots, undefined, byPath, transitions, branches)
|
|
1472
1468
|
const machine = Machine.make({
|
|
1473
1469
|
root: defined,
|
|
1474
1470
|
branches,
|
|
@@ -1477,7 +1473,7 @@ export const compileModel = (model: FiniteModel): Machine.Machine.Any => {
|
|
|
1477
1473
|
return machine.handle(
|
|
1478
1474
|
{
|
|
1479
1475
|
initial: {
|
|
1480
|
-
target:
|
|
1476
|
+
target: model.initial,
|
|
1481
1477
|
decoded: true,
|
|
1482
1478
|
data: stateValue(initial)
|
|
1483
1479
|
},
|