@typeonce/effect-machine 0.11.0 → 0.12.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 +33 -9
- package/dist/Machine.d.ts +43 -43
- package/dist/Machine.d.ts.map +1 -1
- package/dist/Machine.js +9 -9
- package/dist/internal/machine/cluster.js +2 -2
- package/dist/internal/machine/cluster.js.map +1 -1
- package/dist/internal/machine/commandRuntime.d.ts.map +1 -1
- package/dist/internal/machine/commandRuntime.js +2 -2
- package/dist/internal/machine/commandRuntime.js.map +1 -1
- package/dist/internal/machine/configuration.d.ts +7 -7
- package/dist/internal/machine/configuration.d.ts.map +1 -1
- package/dist/internal/machine/configuration.js +3 -3
- package/dist/internal/machine/configuration.js.map +1 -1
- package/dist/internal/machine/executionPlan.d.ts +3 -3
- package/dist/internal/machine/executionPlan.d.ts.map +1 -1
- package/dist/internal/machine/executionPlan.js +25 -25
- package/dist/internal/machine/executionPlan.js.map +1 -1
- package/dist/internal/machine/invocation.js +1 -1
- package/dist/internal/machine/invocation.js.map +1 -1
- package/dist/internal/machine/planner.d.ts +2 -2
- package/dist/internal/machine/planner.d.ts.map +1 -1
- package/dist/internal/machine/planner.js +20 -20
- package/dist/internal/machine/planner.js.map +1 -1
- package/dist/internal/machine/process.js +3 -3
- package/dist/internal/machine/process.js.map +1 -1
- package/dist/internal/machine/runtime.d.ts +3 -3
- package/dist/internal/machine/runtime.d.ts.map +1 -1
- package/dist/internal/machine/runtime.js.map +1 -1
- package/docs/agent-guide.md +28 -11
- package/package.json +1 -1
- package/src/Machine.ts +43 -43
- package/src/internal/machine/cluster.ts +2 -2
- package/src/internal/machine/commandRuntime.ts +4 -2
- package/src/internal/machine/configuration.ts +10 -10
- package/src/internal/machine/executionPlan.ts +35 -35
- package/src/internal/machine/invocation.ts +1 -1
- package/src/internal/machine/planner.ts +30 -24
- package/src/internal/machine/process.ts +3 -3
- package/src/internal/machine/runtime.ts +4 -4
package/src/Machine.ts
CHANGED
|
@@ -175,18 +175,18 @@ export interface Machine<
|
|
|
175
175
|
>
|
|
176
176
|
|
|
177
177
|
/**
|
|
178
|
-
* Ephemeral outward notifications published to this
|
|
179
|
-
* Emissions are never delivered implicitly to an owning
|
|
178
|
+
* Ephemeral outward notifications published to this machine's observers.
|
|
179
|
+
* Emissions are never delivered implicitly to an owning machine.
|
|
180
180
|
*
|
|
181
181
|
* @since 0.4.0
|
|
182
182
|
*/
|
|
183
183
|
readonly emittedEvents: Machine.EventProtocol<"emitted", Emits>
|
|
184
184
|
|
|
185
185
|
/**
|
|
186
|
-
* Public input events accepted by an owning
|
|
186
|
+
* Public input events accepted by an owning machine when this machine is
|
|
187
187
|
* running as a child.
|
|
188
188
|
*
|
|
189
|
-
* The protocol types the optional `parent`
|
|
189
|
+
* The protocol types the optional `parent` machine target exposed to
|
|
190
190
|
* handlers and is checked against a concrete parent's public events at
|
|
191
191
|
* composition boundaries.
|
|
192
192
|
*
|
|
@@ -387,7 +387,7 @@ export interface Runtime<in Events, in Emits> {
|
|
|
387
387
|
readonly raise: (event: Machine.EventInput<Events>) => Effect.Effect<void, MachineSchemaDecodeError | StoppedError>
|
|
388
388
|
|
|
389
389
|
/**
|
|
390
|
-
* Publishes an ephemeral notification to the running
|
|
390
|
+
* Publishes an ephemeral notification to the running machine's observers.
|
|
391
391
|
*
|
|
392
392
|
* @since 0.4.0
|
|
393
393
|
*/
|
|
@@ -397,37 +397,37 @@ export interface Runtime<in Events, in Emits> {
|
|
|
397
397
|
}
|
|
398
398
|
|
|
399
399
|
/**
|
|
400
|
-
* Minimal typed
|
|
400
|
+
* Minimal typed target accepted by inter-machine send commands.
|
|
401
401
|
*
|
|
402
402
|
* @category models
|
|
403
|
-
* @since 0.
|
|
403
|
+
* @since 0.12.0
|
|
404
404
|
*/
|
|
405
|
-
export interface
|
|
405
|
+
export interface MachineTarget<in Event> {
|
|
406
406
|
readonly id: string
|
|
407
407
|
readonly sessionId: string
|
|
408
408
|
readonly send: (event: Event) => Effect.Effect<void, StoppedError>
|
|
409
409
|
}
|
|
410
410
|
|
|
411
411
|
/**
|
|
412
|
-
*
|
|
412
|
+
* Machine targets available while evaluating machine behavior.
|
|
413
413
|
*
|
|
414
414
|
* @category models
|
|
415
|
-
* @since 0.
|
|
415
|
+
* @since 0.12.0
|
|
416
416
|
*/
|
|
417
|
-
export interface
|
|
417
|
+
export interface MachineReferences<
|
|
418
418
|
InputEvents extends ReadonlyArray<Machine.TaggedSchema>,
|
|
419
419
|
ParentEvents extends ReadonlyArray<Machine.TaggedSchema>
|
|
420
420
|
> {
|
|
421
|
-
/**
|
|
422
|
-
readonly self:
|
|
421
|
+
/** Target for the current machine. Sending queues a later mailbox event. */
|
|
422
|
+
readonly self: MachineTarget<Machine.EventInputOf<InputEvents>>
|
|
423
423
|
|
|
424
|
-
/**
|
|
425
|
-
readonly parent:
|
|
424
|
+
/** Target for the owning machine, or `undefined` when running as a root. */
|
|
425
|
+
readonly parent: MachineTarget<Machine.EventInputOf<ParentEvents>> | undefined
|
|
426
426
|
}
|
|
427
427
|
|
|
428
428
|
/**
|
|
429
429
|
* Synchronous commands available while a machine transition is being
|
|
430
|
-
* selected. Enqueuing only records statechart and
|
|
430
|
+
* selected. Enqueuing only records statechart and machine operations; it never
|
|
431
431
|
* executes an Effect.
|
|
432
432
|
*
|
|
433
433
|
* @category models
|
|
@@ -437,12 +437,12 @@ export interface Enqueue<in Events, in Emits> {
|
|
|
437
437
|
/** Raises an event inside the current macrostep. */
|
|
438
438
|
readonly raise: (event: Machine.EventInput<Events>) => void
|
|
439
439
|
|
|
440
|
-
/** Publishes an ephemeral notification to this
|
|
440
|
+
/** Publishes an ephemeral notification to this machine's observers. */
|
|
441
441
|
readonly emit: (event: Machine.EmittedEventInput<Emits>) => void
|
|
442
442
|
|
|
443
443
|
/** Sends an event to an invoked child after the transition is selected. */
|
|
444
444
|
readonly sendTo: {
|
|
445
|
-
<Event>(target:
|
|
445
|
+
<Event>(target: MachineTarget<Event>, event: Event): void
|
|
446
446
|
<Child extends ChildMachine.Any>(child: Child, event: ChildMachine.Event<Child>): void
|
|
447
447
|
<Address extends ChildAddress<never>>(child: Address, event: ChildAddress.Event<Address>): void
|
|
448
448
|
}
|
|
@@ -455,7 +455,7 @@ export interface Enqueue<in Events, in Emits> {
|
|
|
455
455
|
}
|
|
456
456
|
|
|
457
457
|
/**
|
|
458
|
-
* A closed
|
|
458
|
+
* A closed machine command recorded by a synchronous transition.
|
|
459
459
|
*
|
|
460
460
|
* @category models
|
|
461
461
|
* @since 0.4.0
|
|
@@ -463,7 +463,7 @@ export interface Enqueue<in Events, in Emits> {
|
|
|
463
463
|
export type Command =
|
|
464
464
|
| {
|
|
465
465
|
readonly _tag: "SendTo"
|
|
466
|
-
readonly target:
|
|
466
|
+
readonly target: MachineTarget<unknown> | ChildMachine.Any | ChildAddress<never>
|
|
467
467
|
readonly event: unknown
|
|
468
468
|
}
|
|
469
469
|
| {
|
|
@@ -1745,7 +1745,7 @@ export interface Prepared<out State, in Event, out Error, out Output, out Emitte
|
|
|
1745
1745
|
* @since 0.4.0
|
|
1746
1746
|
*/
|
|
1747
1747
|
export interface MachineRef<out State, in Event, out Error = never, out Output = never, out Emitted = never>
|
|
1748
|
-
extends
|
|
1748
|
+
extends MachineTarget<Event>
|
|
1749
1749
|
{
|
|
1750
1750
|
/** Stable machine definition id, or a generated fallback when none was declared. */
|
|
1751
1751
|
readonly id: string
|
|
@@ -1764,7 +1764,7 @@ export interface MachineRef<out State, in Event, out Error = never, out Output =
|
|
|
1764
1764
|
|
|
1765
1765
|
/**
|
|
1766
1766
|
* Streams ephemeral notifications published after subscription. Emissions
|
|
1767
|
-
* are not retained or replayed; the stream completes when the
|
|
1767
|
+
* are not retained or replayed; the stream completes when the machine stops.
|
|
1768
1768
|
*/
|
|
1769
1769
|
readonly emissions: Stream.Stream<Emitted>
|
|
1770
1770
|
|
|
@@ -1892,9 +1892,9 @@ export declare namespace Logic {
|
|
|
1892
1892
|
/** Starts a child process owned by this scope. */
|
|
1893
1893
|
readonly spawn: Spawn
|
|
1894
1894
|
|
|
1895
|
-
/** Sends an event to
|
|
1895
|
+
/** Sends an event to a machine target or typed parent-local child address. */
|
|
1896
1896
|
readonly sendTo: {
|
|
1897
|
-
<TargetEvent>(target:
|
|
1897
|
+
<TargetEvent>(target: MachineTarget<TargetEvent>, event: TargetEvent): Effect.Effect<void, StoppedError>
|
|
1898
1898
|
<Address extends ChildAddress<never>>(
|
|
1899
1899
|
id: Address,
|
|
1900
1900
|
event: ChildAddress.Event<Address>
|
|
@@ -2276,7 +2276,7 @@ export declare namespace Machine {
|
|
|
2276
2276
|
*/
|
|
2277
2277
|
export type InputEvents<M extends Any> = M[typeof MachineTypeId]["inputEvents"]
|
|
2278
2278
|
|
|
2279
|
-
/** Extracts the public input protocol required from an owning
|
|
2279
|
+
/** Extracts the public input protocol required from an owning machine. */
|
|
2280
2280
|
export type ParentEvents<M extends Any> = M[typeof MachineTypeId]["parentEvents"]
|
|
2281
2281
|
|
|
2282
2282
|
/** Extracts an internal event schema tuple from the complete and public protocols. */
|
|
@@ -4011,7 +4011,7 @@ export declare namespace Machine {
|
|
|
4011
4011
|
R,
|
|
4012
4012
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4013
4013
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4014
|
-
> extends
|
|
4014
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4015
4015
|
readonly state: StateByIdentifier<States, StateId>
|
|
4016
4016
|
readonly containingState: ParentStateValue<States, StateId>
|
|
4017
4017
|
readonly ancestors: ParentStateValues<States, StateId>
|
|
@@ -4041,7 +4041,7 @@ export declare namespace Machine {
|
|
|
4041
4041
|
StateId extends StateIdentifier<States>,
|
|
4042
4042
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4043
4043
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4044
|
-
> extends
|
|
4044
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4045
4045
|
readonly state: StateByIdentifier<States, StateId>
|
|
4046
4046
|
readonly containingState: ParentStateValue<States, StateId>
|
|
4047
4047
|
readonly ancestors: ParentStateValues<States, StateId>
|
|
@@ -4061,7 +4061,7 @@ export declare namespace Machine {
|
|
|
4061
4061
|
StateId extends StateIdentifier<States>,
|
|
4062
4062
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4063
4063
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4064
|
-
> extends
|
|
4064
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4065
4065
|
readonly state: StateByIdentifier<States, StateId>
|
|
4066
4066
|
readonly containingState: ParentStateValue<States, StateId>
|
|
4067
4067
|
readonly ancestors: ParentStateValues<States, StateId>
|
|
@@ -4084,7 +4084,7 @@ export declare namespace Machine {
|
|
|
4084
4084
|
Output,
|
|
4085
4085
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4086
4086
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4087
|
-
> extends
|
|
4087
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4088
4088
|
readonly id: string
|
|
4089
4089
|
readonly state: StateByIdentifier<States, StateId>
|
|
4090
4090
|
readonly containingState: ParentStateValue<States, StateId>
|
|
@@ -4107,7 +4107,7 @@ export declare namespace Machine {
|
|
|
4107
4107
|
Output,
|
|
4108
4108
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4109
4109
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4110
|
-
> extends
|
|
4110
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4111
4111
|
readonly id: string
|
|
4112
4112
|
readonly state: StateByIdentifier<States, StateId>
|
|
4113
4113
|
readonly containingState: ParentStateValue<States, StateId>
|
|
@@ -4126,7 +4126,7 @@ export declare namespace Machine {
|
|
|
4126
4126
|
Error,
|
|
4127
4127
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4128
4128
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4129
|
-
> extends
|
|
4129
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4130
4130
|
readonly id: string
|
|
4131
4131
|
readonly state: StateByIdentifier<States, StateId>
|
|
4132
4132
|
readonly containingState: ParentStateValue<States, StateId>
|
|
@@ -4149,7 +4149,7 @@ export declare namespace Machine {
|
|
|
4149
4149
|
StateId extends StateIdentifier<States>,
|
|
4150
4150
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4151
4151
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4152
|
-
> extends
|
|
4152
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4153
4153
|
readonly state: StateByIdentifier<States, StateId>
|
|
4154
4154
|
readonly containingState: ParentStateValue<States, StateId>
|
|
4155
4155
|
readonly ancestors: ParentStateValues<States, StateId>
|
|
@@ -4180,7 +4180,7 @@ export declare namespace Machine {
|
|
|
4180
4180
|
StateId extends StateIdentifier<States>,
|
|
4181
4181
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4182
4182
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4183
|
-
> extends
|
|
4183
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4184
4184
|
readonly state: StateByIdentifier<States, StateId>
|
|
4185
4185
|
readonly containingState: ParentStateValue<States, StateId>
|
|
4186
4186
|
readonly ancestors: ParentStateValues<States, StateId>
|
|
@@ -4207,7 +4207,7 @@ export declare namespace Machine {
|
|
|
4207
4207
|
ChoiceId extends ChoiceIdentifier<States>,
|
|
4208
4208
|
InputEvents extends ReadonlyArray<TaggedSchema> = Events,
|
|
4209
4209
|
ParentEvents extends ReadonlyArray<TaggedSchema> = readonly []
|
|
4210
|
-
> extends
|
|
4210
|
+
> extends MachineReferences<InputEvents, ParentEvents> {
|
|
4211
4211
|
readonly containingState: StateByIdentifier<
|
|
4212
4212
|
States,
|
|
4213
4213
|
Extract<ImmediateParentStateIdentifier<ChoiceId>, StateIdentifier<States>>
|
|
@@ -6431,7 +6431,7 @@ interface Make {
|
|
|
6431
6431
|
* adds raised events and other machine-local deliveries.
|
|
6432
6432
|
* `Machine.emittedEvents` defines outward ephemeral notifications, while
|
|
6433
6433
|
* `parentEvents` declares the inputs a child may explicitly send to its owning
|
|
6434
|
-
*
|
|
6434
|
+
* machine. All descriptors expose deferred constructors while retaining their
|
|
6435
6435
|
* schemas opaquely for runtime validation. Public and internal tags must be
|
|
6436
6436
|
* disjoint.
|
|
6437
6437
|
*
|
|
@@ -6543,8 +6543,8 @@ export const internalEvents: {
|
|
|
6543
6543
|
|
|
6544
6544
|
/**
|
|
6545
6545
|
* Defines the ephemeral notifications a machine may publish to external
|
|
6546
|
-
* observers. Emitted events are separate from
|
|
6547
|
-
* implicitly to a parent
|
|
6546
|
+
* observers. Emitted events are separate from machine input and are never sent
|
|
6547
|
+
* implicitly to a parent machine. Observe them through `MachineRef.emissions` or
|
|
6548
6548
|
* the AtomMachine emission stream adapters.
|
|
6549
6549
|
*
|
|
6550
6550
|
* @category constructors
|
|
@@ -7379,11 +7379,11 @@ export const invoke: {
|
|
|
7379
7379
|
>
|
|
7380
7380
|
} = ((config: unknown) => config) as any
|
|
7381
7381
|
/**
|
|
7382
|
-
* Plans the initial state for a machine without executing
|
|
7382
|
+
* Plans the initial state for a machine without executing machine commands.
|
|
7383
7383
|
*
|
|
7384
7384
|
* **Details**
|
|
7385
7385
|
*
|
|
7386
|
-
* The returned plan contains the settled initial snapshot,
|
|
7386
|
+
* The returned plan contains the settled initial snapshot, machine commands,
|
|
7387
7387
|
* emitted events, optional final output, and every startup microstep. Planning
|
|
7388
7388
|
* may evaluate transition logic and follow completion, eventless, and
|
|
7389
7389
|
* raised-event steps. Transition callbacks are evaluated synchronously.
|
|
@@ -7392,8 +7392,8 @@ export const invoke: {
|
|
|
7392
7392
|
*
|
|
7393
7393
|
* **Gotchas**
|
|
7394
7394
|
*
|
|
7395
|
-
* `start` executes the closed command list as part of the managed
|
|
7396
|
-
* protocol. Manual planners may inspect commands but need
|
|
7395
|
+
* `start` executes the closed command list as part of the managed machine commit
|
|
7396
|
+
* protocol. Manual planners may inspect commands but need running machine targets
|
|
7397
7397
|
* to execute child-addressed operations.
|
|
7398
7398
|
*
|
|
7399
7399
|
* **Example**
|
|
@@ -7628,7 +7628,7 @@ export const enabled: <
|
|
|
7628
7628
|
*
|
|
7629
7629
|
* **Gotchas**
|
|
7630
7630
|
*
|
|
7631
|
-
* `plan` returns data; it does not implement the
|
|
7631
|
+
* `plan` returns data; it does not implement the managed machine commit protocol.
|
|
7632
7632
|
* `start` executes child commands, publishes `next`, and then delivers
|
|
7633
7633
|
* `emittedEvents`. Events with no enabled transition are ignored and produce
|
|
7634
7634
|
* an unchanged plan.
|
|
@@ -8029,7 +8029,7 @@ export const prepare: <
|
|
|
8029
8029
|
* **Details**
|
|
8030
8030
|
*
|
|
8031
8031
|
* For each accepted event the runtime plans the complete synchronous
|
|
8032
|
-
* macrostep, executes closed
|
|
8032
|
+
* macrostep, executes closed machine commands, stops invokes for exited states,
|
|
8033
8033
|
* publishes the new state, delivers emitted events, and then starts invokes
|
|
8034
8034
|
* for entered states.
|
|
8035
8035
|
*
|
|
@@ -317,7 +317,7 @@ export const make = <
|
|
|
317
317
|
if (initial.commands.length > 0) {
|
|
318
318
|
return yield* fail(
|
|
319
319
|
"UnsupportedProcessLocal",
|
|
320
|
-
"Machine
|
|
320
|
+
"Machine commands require a managed local process"
|
|
321
321
|
)
|
|
322
322
|
}
|
|
323
323
|
current = initial.state
|
|
@@ -329,7 +329,7 @@ export const make = <
|
|
|
329
329
|
if (planned.commands.length > 0) {
|
|
330
330
|
return yield* fail(
|
|
331
331
|
"UnsupportedProcessLocal",
|
|
332
|
-
"Machine
|
|
332
|
+
"Machine commands require a managed local process"
|
|
333
333
|
)
|
|
334
334
|
}
|
|
335
335
|
current = planned.next
|
|
@@ -10,7 +10,9 @@ import type { RuntimeCommand } from "./command.js"
|
|
|
10
10
|
import { decodeEmit, decodeEvent } from "./protocol.js"
|
|
11
11
|
import type { ProcessScope } from "./runtime.js"
|
|
12
12
|
|
|
13
|
-
const
|
|
13
|
+
const isMachineTarget = (
|
|
14
|
+
target: unknown
|
|
15
|
+
): target is { readonly send: (event: unknown) => Effect.Effect<void, unknown> } =>
|
|
14
16
|
typeof target === "object" && target !== null && "send" in target && typeof target.send === "function"
|
|
15
17
|
|
|
16
18
|
export const makeLiveRuntime = <Events, Emits>(
|
|
@@ -33,7 +35,7 @@ export const runCommands = <Event>(
|
|
|
33
35
|
) =>
|
|
34
36
|
Effect.forEach(commands, (command) =>
|
|
35
37
|
command._tag === "SendTo"
|
|
36
|
-
?
|
|
38
|
+
? isMachineTarget(command.target)
|
|
37
39
|
? command.target.send(command.event)
|
|
38
40
|
: scope.sendTo(command.target as never, command.event)
|
|
39
41
|
: scope.stopChild(command.child as never), { discard: true })
|
|
@@ -8,7 +8,7 @@ import * as Cause from "effect/Cause"
|
|
|
8
8
|
import * as Effect from "effect/Effect"
|
|
9
9
|
import * as Option from "effect/Option"
|
|
10
10
|
import { hasProperty } from "effect/Predicate"
|
|
11
|
-
import type {
|
|
11
|
+
import type { Machine, MachineTarget } from "../../Machine.js"
|
|
12
12
|
import { MachineSchemaDecodeError } from "./errors.js"
|
|
13
13
|
import {
|
|
14
14
|
decodeBoundary,
|
|
@@ -252,25 +252,25 @@ export interface ActiveConfiguration {
|
|
|
252
252
|
readonly values: ReadonlyMap<string, unknown>
|
|
253
253
|
readonly outputs: ReadonlyMap<string, unknown>
|
|
254
254
|
readonly history: ReadonlyMap<string, HistoryRecord>
|
|
255
|
-
readonly
|
|
255
|
+
readonly machineReferences?: PlanningMachineReferences
|
|
256
256
|
}
|
|
257
257
|
|
|
258
|
-
export interface
|
|
259
|
-
readonly self:
|
|
260
|
-
readonly parent:
|
|
258
|
+
export interface PlanningMachineReferences {
|
|
259
|
+
readonly self: MachineTarget<any>
|
|
260
|
+
readonly parent: MachineTarget<any> | undefined
|
|
261
261
|
}
|
|
262
262
|
|
|
263
|
-
export const
|
|
263
|
+
export const withMachineReferences = (
|
|
264
264
|
configuration: ActiveConfiguration,
|
|
265
|
-
|
|
265
|
+
machineReferences: PlanningMachineReferences
|
|
266
266
|
): ActiveConfiguration => ({
|
|
267
267
|
...configuration,
|
|
268
|
-
|
|
268
|
+
machineReferences: { self: machineReferences.self, parent: machineReferences.parent }
|
|
269
269
|
})
|
|
270
270
|
|
|
271
|
-
export const
|
|
271
|
+
export const getMachineReferences = (
|
|
272
272
|
configuration: ActiveConfiguration
|
|
273
|
-
):
|
|
273
|
+
): PlanningMachineReferences | undefined => configuration.machineReferences
|
|
274
274
|
|
|
275
275
|
export interface FinalCompletion {
|
|
276
276
|
readonly path: string
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import * as Effect from "effect/Effect"
|
|
8
|
-
import type {
|
|
8
|
+
import type { Machine, MachineTarget } from "../../Machine.js"
|
|
9
9
|
import { getTargetBuilder, type RuntimeCommand } from "./command.js"
|
|
10
10
|
import {
|
|
11
11
|
type ActiveConfiguration,
|
|
@@ -18,10 +18,10 @@ import {
|
|
|
18
18
|
isDescendantOf,
|
|
19
19
|
normalizeConfigurationSync,
|
|
20
20
|
normalizeTargetConfigurationSync,
|
|
21
|
-
type
|
|
21
|
+
type PlanningMachineReferences,
|
|
22
22
|
snapshotFromConfiguration,
|
|
23
23
|
validateInitialConfiguration,
|
|
24
|
-
|
|
24
|
+
withMachineReferences
|
|
25
25
|
} from "./configuration.js"
|
|
26
26
|
import { InfiniteTransitionError, StoppedError } from "./errors.js"
|
|
27
27
|
import * as InvocationEvent from "./invocationEvent.js"
|
|
@@ -67,28 +67,28 @@ interface IndexedExecutionDescriptor {
|
|
|
67
67
|
>
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
const
|
|
70
|
+
const planningMachineReferences = new WeakMap<Machine.Any, PlanningMachineReferences>()
|
|
71
71
|
|
|
72
|
-
const
|
|
73
|
-
const cached =
|
|
72
|
+
const getPlanningMachineReferences = (machine: Machine.Any): PlanningMachineReferences => {
|
|
73
|
+
const cached = planningMachineReferences.get(machine)
|
|
74
74
|
if (cached !== undefined) return cached
|
|
75
|
-
const self:
|
|
75
|
+
const self: MachineTarget<unknown> = {
|
|
76
76
|
id: machine.id ?? "Machine",
|
|
77
77
|
sessionId: "Machine.plan",
|
|
78
78
|
send: () => Effect.fail(new StoppedError())
|
|
79
79
|
}
|
|
80
80
|
const scope = { self, parent: undefined }
|
|
81
|
-
|
|
81
|
+
planningMachineReferences.set(machine, scope)
|
|
82
82
|
return scope
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
const
|
|
85
|
+
const resolveMachineReferences = (
|
|
86
86
|
machine: Machine.Any,
|
|
87
|
-
|
|
88
|
-
):
|
|
89
|
-
|
|
90
|
-
?
|
|
91
|
-
: { self:
|
|
87
|
+
machineReferences: PlanningMachineReferences | undefined
|
|
88
|
+
): PlanningMachineReferences =>
|
|
89
|
+
machineReferences === undefined
|
|
90
|
+
? getPlanningMachineReferences(machine)
|
|
91
|
+
: { self: machineReferences.self, parent: machineReferences.parent }
|
|
92
92
|
|
|
93
93
|
const indexedStateConfigKeys: ReadonlySet<PropertyKey> = new Set([
|
|
94
94
|
"initial",
|
|
@@ -363,7 +363,7 @@ const makeIndexedTransitionContext = (
|
|
|
363
363
|
configuration: OwnedIndexedState,
|
|
364
364
|
sourceIndex: number,
|
|
365
365
|
event: any,
|
|
366
|
-
|
|
366
|
+
machineReferences?: PlanningMachineReferences
|
|
367
367
|
): any => {
|
|
368
368
|
const source = descriptor.nodes[sourceIndex]!
|
|
369
369
|
const parentIndex = descriptor.parentIndices[sourceIndex]!
|
|
@@ -372,7 +372,7 @@ const makeIndexedTransitionContext = (
|
|
|
372
372
|
ancestors[descriptor.nodes[ancestorIndex]!.path] = configuration.values[ancestorIndex]
|
|
373
373
|
}
|
|
374
374
|
return {
|
|
375
|
-
...
|
|
375
|
+
...resolveMachineReferences(machine, machineReferences),
|
|
376
376
|
state: configuration.values[sourceIndex],
|
|
377
377
|
containingState: parentIndex < 0 ? undefined : configuration.values[parentIndex],
|
|
378
378
|
ancestors,
|
|
@@ -454,7 +454,7 @@ const selectIndexedEventTransitions = (
|
|
|
454
454
|
descriptor: IndexedExecutionDescriptor,
|
|
455
455
|
configuration: OwnedIndexedState,
|
|
456
456
|
event: any,
|
|
457
|
-
|
|
457
|
+
machineReferences?: PlanningMachineReferences
|
|
458
458
|
): ReadonlyArray<IndexedSelectedTransition> => {
|
|
459
459
|
const selected: Array<IndexedSelectedTransition> = []
|
|
460
460
|
for (const leafIndex of configuration.activeLeaves) {
|
|
@@ -476,7 +476,7 @@ const selectIndexedEventTransitions = (
|
|
|
476
476
|
configuration,
|
|
477
477
|
sourceIndex,
|
|
478
478
|
event,
|
|
479
|
-
|
|
479
|
+
machineReferences
|
|
480
480
|
)
|
|
481
481
|
})
|
|
482
482
|
}
|
|
@@ -660,7 +660,7 @@ const planIndexedFlatState = (
|
|
|
660
660
|
configuration: OwnedIndexedState,
|
|
661
661
|
decoded: { readonly _tag: PropertyKey },
|
|
662
662
|
retainMicrosteps: boolean,
|
|
663
|
-
|
|
663
|
+
machineReferences?: PlanningMachineReferences
|
|
664
664
|
): ExecutionMacrostep<OwnedIndexedState> => {
|
|
665
665
|
let current = configuration
|
|
666
666
|
let event: any = decoded
|
|
@@ -712,7 +712,7 @@ const planIndexedFlatState = (
|
|
|
712
712
|
machine,
|
|
713
713
|
transition.transition,
|
|
714
714
|
{
|
|
715
|
-
...
|
|
715
|
+
...resolveMachineReferences(machine, machineReferences),
|
|
716
716
|
state: current.values[sourceIndex],
|
|
717
717
|
containingState: undefined,
|
|
718
718
|
ancestors: {},
|
|
@@ -807,7 +807,7 @@ const planIndexedState = (
|
|
|
807
807
|
configuration: OwnedIndexedState,
|
|
808
808
|
input: unknown,
|
|
809
809
|
retainMicrosteps: boolean,
|
|
810
|
-
|
|
810
|
+
machineReferences?: PlanningMachineReferences
|
|
811
811
|
): ExecutionMacrostep<OwnedIndexedState> => {
|
|
812
812
|
if (InvocationEvent.isInvocationEvent(input)) {
|
|
813
813
|
// Invocation lifecycle transitions retain the generic planner as their
|
|
@@ -816,9 +816,9 @@ const planIndexedState = (
|
|
|
816
816
|
// the representation boundary.
|
|
817
817
|
const planned = planConfiguration(
|
|
818
818
|
machine as any,
|
|
819
|
-
|
|
819
|
+
machineReferences === undefined
|
|
820
820
|
? activeConfigurationFromIndexedState(descriptor, configuration)
|
|
821
|
-
:
|
|
821
|
+
: withMachineReferences(activeConfigurationFromIndexedState(descriptor, configuration), machineReferences),
|
|
822
822
|
input
|
|
823
823
|
)
|
|
824
824
|
return {
|
|
@@ -841,7 +841,7 @@ const planIndexedState = (
|
|
|
841
841
|
}
|
|
842
842
|
const decoded = decodeEventSync(machine, input)
|
|
843
843
|
if (descriptor.flat) {
|
|
844
|
-
return planIndexedFlatState(machine, descriptor, configuration, decoded, retainMicrosteps,
|
|
844
|
+
return planIndexedFlatState(machine, descriptor, configuration, decoded, retainMicrosteps, machineReferences)
|
|
845
845
|
}
|
|
846
846
|
if (descriptor.finalIndices.some((index) => configuration.active[index] === 1)) {
|
|
847
847
|
const active = activeConfigurationFromIndexedState(descriptor, configuration)
|
|
@@ -862,7 +862,7 @@ const planIndexedState = (
|
|
|
862
862
|
}
|
|
863
863
|
}
|
|
864
864
|
|
|
865
|
-
const selections = selectIndexedEventTransitions(machine, descriptor, configuration, decoded,
|
|
865
|
+
const selections = selectIndexedEventTransitions(machine, descriptor, configuration, decoded, machineReferences)
|
|
866
866
|
if (selections.length === 0) {
|
|
867
867
|
return {
|
|
868
868
|
next: configuration,
|
|
@@ -930,7 +930,7 @@ const planIndexedState = (
|
|
|
930
930
|
}
|
|
931
931
|
raisedIndex += 1
|
|
932
932
|
currentEvent = raised
|
|
933
|
-
const raisedSelections = selectIndexedEventTransitions(machine, descriptor, current, raised,
|
|
933
|
+
const raisedSelections = selectIndexedEventTransitions(machine, descriptor, current, raised, machineReferences)
|
|
934
934
|
if (raisedSelections.length === 0) continue
|
|
935
935
|
const step = indexedMicrostep(machine, descriptor, current, raised, raisedSelections)
|
|
936
936
|
current = step.next
|
|
@@ -949,11 +949,11 @@ export interface CompiledExecutionPlan {
|
|
|
949
949
|
state: unknown,
|
|
950
950
|
event: unknown,
|
|
951
951
|
retainMicrosteps?: boolean,
|
|
952
|
-
|
|
952
|
+
machineReferences?: PlanningMachineReferences
|
|
953
953
|
) => ExecutionMacrostep
|
|
954
954
|
readonly initial?: (
|
|
955
955
|
args: ReadonlyArray<unknown>,
|
|
956
|
-
|
|
956
|
+
machineReferences?: PlanningMachineReferences
|
|
957
957
|
) => {
|
|
958
958
|
readonly state: Machine.Snapshot<any>
|
|
959
959
|
readonly configuration: unknown
|
|
@@ -970,12 +970,12 @@ const makeActiveExecutionPlan = (machine: Machine.Any): CompiledExecutionPlan =>
|
|
|
970
970
|
fromConfiguration: (configuration) => configuration,
|
|
971
971
|
toConfiguration: (state) => state as ActiveConfiguration,
|
|
972
972
|
snapshot: (state) => snapshotFromConfiguration(machine, state as ActiveConfiguration),
|
|
973
|
-
plan: (state, event, _retainMicrosteps,
|
|
973
|
+
plan: (state, event, _retainMicrosteps, machineReferences) =>
|
|
974
974
|
planConfiguration(
|
|
975
975
|
machine as any,
|
|
976
|
-
|
|
976
|
+
machineReferences === undefined
|
|
977
977
|
? state as ActiveConfiguration
|
|
978
|
-
:
|
|
978
|
+
: withMachineReferences(state as ActiveConfiguration, machineReferences),
|
|
979
979
|
event as any
|
|
980
980
|
)
|
|
981
981
|
})
|
|
@@ -987,9 +987,9 @@ const makeIndexedExecutionPlan = (
|
|
|
987
987
|
fromConfiguration: (configuration) => ownedIndexedStateFromActive(indexed, configuration),
|
|
988
988
|
toConfiguration: (state) => activeConfigurationFromIndexedState(indexed, state as OwnedIndexedState),
|
|
989
989
|
snapshot: (state) => snapshotFromIndexedState(indexed, state as OwnedIndexedState),
|
|
990
|
-
plan: (state, event, retainMicrosteps = false,
|
|
991
|
-
planIndexedState(machine, indexed, state as OwnedIndexedState, event, retainMicrosteps,
|
|
992
|
-
initial: (args,
|
|
990
|
+
plan: (state, event, retainMicrosteps = false, machineReferences) =>
|
|
991
|
+
planIndexedState(machine, indexed, state as OwnedIndexedState, event, retainMicrosteps, machineReferences),
|
|
992
|
+
initial: (args, machineReferences) => {
|
|
993
993
|
const inputArgs = machine.input === undefined
|
|
994
994
|
? args
|
|
995
995
|
: args.length === 0
|
|
@@ -997,7 +997,7 @@ const makeIndexedExecutionPlan = (
|
|
|
997
997
|
: [decodeInputSync(machine, machine.input, args[0])]
|
|
998
998
|
const initial = machine.initial(...inputArgs as any)
|
|
999
999
|
const normalized = normalizeConfigurationSync(machine, initial as Machine.Snapshot<any>)
|
|
1000
|
-
const active =
|
|
1000
|
+
const active = machineReferences === undefined ? normalized : withMachineReferences(normalized, machineReferences)
|
|
1001
1001
|
validateInitialConfiguration(machine, active)
|
|
1002
1002
|
const completed = completeConfigurationSync(machine, active, InitialEvent).configuration
|
|
1003
1003
|
const configuration = ownedIndexedStateFromActive(indexed, completed)
|
|
@@ -235,7 +235,7 @@ export const startAll = (
|
|
|
235
235
|
.filter((path) => configuration.active.has(path))
|
|
236
236
|
.flatMap((path) => {
|
|
237
237
|
const context = {
|
|
238
|
-
...(Configuration.
|
|
238
|
+
...(Configuration.getMachineReferences(configuration) ?? { self: scope.self, parent: scope.parent }),
|
|
239
239
|
state: configuration.values.get(path),
|
|
240
240
|
containingState: Configuration.getParentValue(machine, configuration, path),
|
|
241
241
|
ancestors: Configuration.getParentValues(machine, configuration, path),
|