effect-machine 0.11.0 → 0.13.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 +128 -324
- package/dist/actor.d.ts +52 -31
- package/dist/actor.js +218 -283
- package/dist/cluster/adapters/in-memory.d.ts +28 -0
- package/dist/cluster/adapters/in-memory.js +79 -0
- package/dist/cluster/entity-actor-ref.d.ts +56 -0
- package/dist/cluster/entity-actor-ref.js +33 -0
- package/dist/cluster/entity-machine.d.ts +31 -49
- package/dist/cluster/entity-machine.js +178 -52
- package/dist/cluster/index.d.ts +5 -2
- package/dist/cluster/index.js +4 -1
- package/dist/cluster/persistence.d.ts +49 -0
- package/dist/cluster/persistence.js +18 -0
- package/dist/cluster/to-entity.d.ts +9 -3
- package/dist/cluster/to-entity.js +16 -4
- package/dist/errors.d.ts +25 -17
- package/dist/errors.js +10 -5
- package/dist/index.d.ts +6 -4
- package/dist/index.js +4 -3
- package/dist/internal/brands.d.ts +14 -1
- package/dist/internal/runtime.d.ts +142 -0
- package/dist/internal/runtime.js +357 -0
- package/dist/internal/transition.d.ts +10 -4
- package/dist/internal/transition.js +24 -12
- package/dist/internal/utils.d.ts +42 -6
- package/dist/internal/utils.js +27 -1
- package/dist/machine.d.ts +89 -55
- package/dist/machine.js +80 -68
- package/dist/schema.d.ts +35 -34
- package/dist/schema.js +33 -4
- package/dist/supervision.d.ts +97 -0
- package/dist/supervision.js +42 -0
- package/dist/testing.d.ts +17 -8
- package/dist/testing.js +22 -23
- package/package.json +7 -7
- package/v3/dist/actor.d.ts +54 -37
- package/v3/dist/actor.js +209 -277
- package/v3/dist/cluster/adapters/in-memory.d.ts +15 -0
- package/v3/dist/cluster/adapters/in-memory.js +62 -0
- package/v3/dist/cluster/entity-actor-ref.d.ts +49 -0
- package/v3/dist/cluster/entity-actor-ref.js +19 -0
- package/v3/dist/cluster/entity-machine.d.ts +34 -49
- package/v3/dist/cluster/entity-machine.js +134 -50
- package/v3/dist/cluster/index.d.ts +5 -2
- package/v3/dist/cluster/index.js +4 -1
- package/v3/dist/cluster/persistence.d.ts +48 -0
- package/v3/dist/cluster/persistence.js +14 -0
- package/v3/dist/cluster/to-entity.d.ts +5 -2
- package/v3/dist/cluster/to-entity.js +12 -4
- package/v3/dist/errors.d.ts +18 -8
- package/v3/dist/errors.js +9 -4
- package/v3/dist/index.d.ts +6 -4
- package/v3/dist/index.js +3 -2
- package/v3/dist/internal/brands.d.ts +15 -1
- package/v3/dist/internal/runtime.d.ts +142 -0
- package/v3/dist/internal/runtime.js +335 -0
- package/v3/dist/internal/transition.d.ts +10 -4
- package/v3/dist/internal/transition.js +23 -11
- package/v3/dist/internal/utils.d.ts +42 -6
- package/v3/dist/internal/utils.js +27 -1
- package/v3/dist/machine.d.ts +35 -47
- package/v3/dist/machine.js +62 -64
- package/v3/dist/schema.d.ts +35 -34
- package/v3/dist/schema.js +29 -3
- package/v3/dist/supervision.d.ts +97 -0
- package/v3/dist/supervision.js +42 -0
- package/v3/dist/testing.d.ts +18 -9
- package/v3/dist/testing.js +21 -22
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MissingSchemaError } from "../errors.js";
|
|
2
|
+
import { Schema } from "effect";
|
|
2
3
|
import { Entity } from "effect/unstable/cluster";
|
|
3
4
|
import { Rpc } from "effect/unstable/rpc";
|
|
4
5
|
//#region src/cluster/to-entity.ts
|
|
@@ -42,10 +43,21 @@ const toEntity = (machine, options) => {
|
|
|
42
43
|
const stateSchema = machine.stateSchema;
|
|
43
44
|
const eventSchema = machine.eventSchema;
|
|
44
45
|
if (stateSchema === void 0 || eventSchema === void 0) throw new MissingSchemaError({ operation: "toEntity" });
|
|
45
|
-
return Entity.make(options.type, [
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
46
|
+
return Entity.make(options.type, [
|
|
47
|
+
Rpc.make("Send", {
|
|
48
|
+
payload: { event: eventSchema },
|
|
49
|
+
success: stateSchema
|
|
50
|
+
}),
|
|
51
|
+
Rpc.make("Ask", {
|
|
52
|
+
payload: { event: eventSchema },
|
|
53
|
+
success: Schema.Unknown
|
|
54
|
+
}),
|
|
55
|
+
Rpc.make("GetState", { success: stateSchema }),
|
|
56
|
+
Rpc.make("WatchState", {
|
|
57
|
+
success: stateSchema,
|
|
58
|
+
stream: true
|
|
59
|
+
})
|
|
60
|
+
]);
|
|
49
61
|
};
|
|
50
62
|
//#endregion
|
|
51
63
|
export { toEntity };
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,57 +1,65 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
import * as effect_Cause0 from "effect/Cause";
|
|
2
|
+
import * as _$effect_Cause0 from "effect/Cause";
|
|
3
3
|
|
|
4
4
|
//#region src/errors.d.ts
|
|
5
5
|
declare const DuplicateActorError_base: Schema.ErrorClass<DuplicateActorError, Schema.TaggedStruct<"DuplicateActorError", {
|
|
6
6
|
readonly actorId: Schema.String;
|
|
7
|
-
}>, effect_Cause0.YieldableError>;
|
|
7
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
8
8
|
/** Attempted to spawn/restore actor with ID already in use */
|
|
9
9
|
declare class DuplicateActorError extends DuplicateActorError_base {}
|
|
10
|
-
declare const UnprovidedSlotsError_base: Schema.ErrorClass<UnprovidedSlotsError, Schema.TaggedStruct<"UnprovidedSlotsError", {
|
|
11
|
-
readonly slots: Schema.$Array<Schema.String>;
|
|
12
|
-
}>, effect_Cause0.YieldableError>;
|
|
13
|
-
/** Machine has unprovided effect slots */
|
|
14
|
-
declare class UnprovidedSlotsError extends UnprovidedSlotsError_base {}
|
|
15
10
|
declare const MissingSchemaError_base: Schema.ErrorClass<MissingSchemaError, Schema.TaggedStruct<"MissingSchemaError", {
|
|
16
11
|
readonly operation: Schema.String;
|
|
17
|
-
}>, effect_Cause0.YieldableError>;
|
|
12
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
18
13
|
/** Operation requires schemas attached to machine */
|
|
19
14
|
declare class MissingSchemaError extends MissingSchemaError_base {}
|
|
20
|
-
declare const InvalidSchemaError_base: Schema.ErrorClass<InvalidSchemaError, Schema.TaggedStruct<"InvalidSchemaError", {
|
|
15
|
+
declare const InvalidSchemaError_base: Schema.ErrorClass<InvalidSchemaError, Schema.TaggedStruct<"InvalidSchemaError", {
|
|
16
|
+
readonly message: Schema.String;
|
|
17
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
21
18
|
/** State/Event schema has no variants */
|
|
22
19
|
declare class InvalidSchemaError extends InvalidSchemaError_base {}
|
|
23
20
|
declare const MissingMatchHandlerError_base: Schema.ErrorClass<MissingMatchHandlerError, Schema.TaggedStruct<"MissingMatchHandlerError", {
|
|
24
21
|
readonly tag: Schema.String;
|
|
25
|
-
}>, effect_Cause0.YieldableError>;
|
|
22
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
26
23
|
/** $match called with missing handler for tag */
|
|
27
24
|
declare class MissingMatchHandlerError extends MissingMatchHandlerError_base {}
|
|
28
25
|
declare const SlotProvisionError_base: Schema.ErrorClass<SlotProvisionError, Schema.TaggedStruct<"SlotProvisionError", {
|
|
29
26
|
readonly slotName: Schema.String;
|
|
30
27
|
readonly slotType: Schema.Literals<readonly ["guard", "effect"]>;
|
|
31
|
-
}>, effect_Cause0.YieldableError>;
|
|
28
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
32
29
|
/** Slot handler not found at runtime (internal error) */
|
|
33
30
|
declare class SlotProvisionError extends SlotProvisionError_base {}
|
|
34
31
|
declare const ProvisionValidationError_base: Schema.ErrorClass<ProvisionValidationError, Schema.TaggedStruct<"ProvisionValidationError", {
|
|
35
32
|
readonly missing: Schema.$Array<Schema.String>;
|
|
36
33
|
readonly extra: Schema.$Array<Schema.String>;
|
|
37
|
-
}>, effect_Cause0.YieldableError>;
|
|
38
|
-
/**
|
|
34
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
35
|
+
/** Slot provision validation failed — missing or extra handlers */
|
|
39
36
|
declare class ProvisionValidationError extends ProvisionValidationError_base {}
|
|
40
37
|
declare const AssertionError_base: Schema.ErrorClass<AssertionError, Schema.TaggedStruct<"AssertionError", {
|
|
41
38
|
readonly message: Schema.String;
|
|
42
|
-
}>, effect_Cause0.YieldableError>;
|
|
39
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
43
40
|
/** Assertion failed in testing utilities */
|
|
44
41
|
declare class AssertionError extends AssertionError_base {}
|
|
45
42
|
declare const ActorStoppedError_base: Schema.ErrorClass<ActorStoppedError, Schema.TaggedStruct<"ActorStoppedError", {
|
|
46
43
|
readonly actorId: Schema.String;
|
|
47
|
-
}>, effect_Cause0.YieldableError>;
|
|
44
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
48
45
|
/** Actor was stopped while a call/ask was pending */
|
|
49
46
|
declare class ActorStoppedError extends ActorStoppedError_base {}
|
|
50
47
|
declare const NoReplyError_base: Schema.ErrorClass<NoReplyError, Schema.TaggedStruct<"NoReplyError", {
|
|
51
48
|
readonly actorId: Schema.String;
|
|
52
49
|
readonly eventTag: Schema.String;
|
|
53
|
-
}>, effect_Cause0.YieldableError>;
|
|
50
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
54
51
|
/** ask() was used but the transition handler did not call reply */
|
|
55
52
|
declare class NoReplyError extends NoReplyError_base {}
|
|
53
|
+
declare const PersistenceError_base: Schema.ErrorClass<PersistenceError, Schema.TaggedStruct<"PersistenceError", {
|
|
54
|
+
readonly message: Schema.String;
|
|
55
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
56
|
+
/** Persistence adapter operation failed */
|
|
57
|
+
declare class PersistenceError extends PersistenceError_base {}
|
|
58
|
+
declare const VersionConflictError_base: Schema.ErrorClass<VersionConflictError, Schema.TaggedStruct<"VersionConflictError", {
|
|
59
|
+
readonly expected: Schema.Number;
|
|
60
|
+
readonly actual: Schema.Number;
|
|
61
|
+
}>, _$effect_Cause0.YieldableError>;
|
|
62
|
+
/** Optimistic locking failure — stored version doesn't match expected */
|
|
63
|
+
declare class VersionConflictError extends VersionConflictError_base {}
|
|
56
64
|
//#endregion
|
|
57
|
-
export { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, ProvisionValidationError, SlotProvisionError,
|
|
65
|
+
export { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, PersistenceError, ProvisionValidationError, SlotProvisionError, VersionConflictError };
|
package/dist/errors.js
CHANGED
|
@@ -12,12 +12,10 @@ import { Schema } from "effect";
|
|
|
12
12
|
*/
|
|
13
13
|
/** Attempted to spawn/restore actor with ID already in use */
|
|
14
14
|
var DuplicateActorError = class extends Schema.TaggedErrorClass()("DuplicateActorError", { actorId: Schema.String }) {};
|
|
15
|
-
/** Machine has unprovided effect slots */
|
|
16
|
-
var UnprovidedSlotsError = class extends Schema.TaggedErrorClass()("UnprovidedSlotsError", { slots: Schema.Array(Schema.String) }) {};
|
|
17
15
|
/** Operation requires schemas attached to machine */
|
|
18
16
|
var MissingSchemaError = class extends Schema.TaggedErrorClass()("MissingSchemaError", { operation: Schema.String }) {};
|
|
19
17
|
/** State/Event schema has no variants */
|
|
20
|
-
var InvalidSchemaError = class extends Schema.TaggedErrorClass()("InvalidSchemaError", {}) {};
|
|
18
|
+
var InvalidSchemaError = class extends Schema.TaggedErrorClass()("InvalidSchemaError", { message: Schema.String }) {};
|
|
21
19
|
/** $match called with missing handler for tag */
|
|
22
20
|
var MissingMatchHandlerError = class extends Schema.TaggedErrorClass()("MissingMatchHandlerError", { tag: Schema.String }) {};
|
|
23
21
|
/** Slot handler not found at runtime (internal error) */
|
|
@@ -25,7 +23,7 @@ var SlotProvisionError = class extends Schema.TaggedErrorClass()("SlotProvisionE
|
|
|
25
23
|
slotName: Schema.String,
|
|
26
24
|
slotType: Schema.Literals(["guard", "effect"])
|
|
27
25
|
}) {};
|
|
28
|
-
/**
|
|
26
|
+
/** Slot provision validation failed — missing or extra handlers */
|
|
29
27
|
var ProvisionValidationError = class extends Schema.TaggedErrorClass()("ProvisionValidationError", {
|
|
30
28
|
missing: Schema.Array(Schema.String),
|
|
31
29
|
extra: Schema.Array(Schema.String)
|
|
@@ -39,5 +37,12 @@ var NoReplyError = class extends Schema.TaggedErrorClass()("NoReplyError", {
|
|
|
39
37
|
actorId: Schema.String,
|
|
40
38
|
eventTag: Schema.String
|
|
41
39
|
}) {};
|
|
40
|
+
/** Persistence adapter operation failed */
|
|
41
|
+
var PersistenceError = class extends Schema.TaggedErrorClass()("PersistenceError", { message: Schema.String }) {};
|
|
42
|
+
/** Optimistic locking failure — stored version doesn't match expected */
|
|
43
|
+
var VersionConflictError = class extends Schema.TaggedErrorClass()("VersionConflictError", {
|
|
44
|
+
expected: Schema.Number,
|
|
45
|
+
actual: Schema.Number
|
|
46
|
+
}) {};
|
|
42
47
|
//#endregion
|
|
43
|
-
export { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, ProvisionValidationError, SlotProvisionError,
|
|
48
|
+
export { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, PersistenceError, ProvisionValidationError, SlotProvisionError, VersionConflictError };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
+
import { DeferReplyResult, ReplyResult } from "./internal/utils.js";
|
|
2
|
+
import { Event, MachineEventSchema, MachineStateSchema, ReplyFields, State } from "./schema.js";
|
|
3
|
+
import { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, PersistenceError, ProvisionValidationError, SlotProvisionError, VersionConflictError } from "./errors.js";
|
|
1
4
|
import { EffectHandlers, EffectSlot, EffectSlots, EffectsDef, EffectsSchema, GuardHandlers, GuardSlot, GuardSlots, GuardsDef, GuardsSchema, MachineContext, Slot } from "./slot.js";
|
|
2
|
-
import {
|
|
3
|
-
import { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, ProvisionValidationError, SlotProvisionError, UnprovidedSlotsError } from "./errors.js";
|
|
5
|
+
import { ActorExit, CellPhase, DefectPhase, Supervision } from "./supervision.js";
|
|
4
6
|
import { ProcessEventResult } from "./internal/transition.js";
|
|
5
|
-
import { BackgroundEffect,
|
|
7
|
+
import { BackgroundEffect, HandlerContext, Machine, MachineRef, MakeConfig, ProvideHandlers, SlotContext, SpawnEffect, StateHandlerContext, TaskOptions, TimeoutConfig, Transition, machine_d_exports } from "./machine.js";
|
|
6
8
|
import { ActorRef, ActorRefSync, ActorSystem, Default, SystemEvent, SystemEventListener, TransitionInfo } from "./actor.js";
|
|
7
9
|
import { SimulationResult, TestHarness, TestHarnessOptions, assertNeverReaches, assertPath, assertReaches, createTestHarness, simulate } from "./testing.js";
|
|
8
10
|
import { AnyInspectionEvent, EffectEvent, ErrorEvent, EventReceivedEvent, InspectionEvent, Inspector, InspectorHandler, SpawnEvent, StopEvent, TaskEvent, TracingInspectorOptions, TransitionEvent, collectingInspector, combineInspectors, consoleInspector, makeInspector, makeInspectorEffect, tracingInspector } from "./inspection.js";
|
|
9
|
-
export { type ActorRef, type ActorRefSync, ActorStoppedError, type ActorSystem, Default as ActorSystemDefault, ActorSystem as ActorSystemService, type AnyInspectionEvent, AssertionError, type BackgroundEffect, type
|
|
11
|
+
export { ActorExit, type ActorRef, type ActorRefSync, ActorStoppedError, type ActorSystem, Default as ActorSystemDefault, ActorSystem as ActorSystemService, type AnyInspectionEvent, AssertionError, type BackgroundEffect, type CellPhase, type DefectPhase, type DeferReplyResult, DuplicateActorError, type EffectEvent, type EffectSlots, type EffectsDef, type EffectsSchema, type ErrorEvent, Event, type EventReceivedEvent, type GuardHandlers, type GuardSlot, type GuardSlots, type GuardsDef, type GuardsSchema, type HandlerContext, type InspectionEvent, type Inspector, type InspectorHandler, Inspector as InspectorService, InvalidSchemaError, machine_d_exports as Machine, type MachineContext, type MachineEventSchema, type MachineRef, type MachineStateSchema, type Machine as MachineType, type MakeConfig, MissingMatchHandlerError, MissingSchemaError, NoReplyError, PersistenceError, type ProcessEventResult, type ProvideHandlers, ProvisionValidationError, type ReplyFields, type ReplyResult, type SimulationResult, Slot, type SlotContext, type EffectHandlers as SlotEffectHandlers, type EffectSlot as SlotEffectSlot, SlotProvisionError, type SpawnEffect, type SpawnEvent, State, type StateHandlerContext, type StopEvent, Supervision, type SystemEvent, type SystemEventListener, type TaskEvent, type TaskOptions, type TestHarness, type TestHarnessOptions, type TimeoutConfig, type TracingInspectorOptions, type Transition, type TransitionEvent, type TransitionInfo, VersionConflictError, assertNeverReaches, assertPath, assertReaches, collectingInspector, combineInspectors, consoleInspector, createTestHarness, makeInspector, makeInspectorEffect, simulate, tracingInspector };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Inspector, collectingInspector, combineInspectors, consoleInspector, makeInspector, makeInspectorEffect, tracingInspector } from "./inspection.js";
|
|
2
|
-
import { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, ProvisionValidationError, SlotProvisionError,
|
|
2
|
+
import { ActorStoppedError, AssertionError, DuplicateActorError, InvalidSchemaError, MissingMatchHandlerError, MissingSchemaError, NoReplyError, PersistenceError, ProvisionValidationError, SlotProvisionError, VersionConflictError } from "./errors.js";
|
|
3
|
+
import { ActorExit, Supervision } from "./supervision.js";
|
|
4
|
+
import { ActorSystem, Default } from "./actor.js";
|
|
3
5
|
import { Slot } from "./slot.js";
|
|
4
6
|
import { machine_exports } from "./machine.js";
|
|
5
|
-
import { ActorSystem, Default } from "./actor.js";
|
|
6
7
|
import { Event, State } from "./schema.js";
|
|
7
8
|
import { assertNeverReaches, assertPath, assertReaches, createTestHarness, simulate } from "./testing.js";
|
|
8
|
-
export { ActorStoppedError, Default as ActorSystemDefault, ActorSystem as ActorSystemService, AssertionError, DuplicateActorError, Event, Inspector as InspectorService, InvalidSchemaError, machine_exports as Machine, MissingMatchHandlerError, MissingSchemaError, NoReplyError, ProvisionValidationError, Slot, SlotProvisionError, State,
|
|
9
|
+
export { ActorExit, ActorStoppedError, Default as ActorSystemDefault, ActorSystem as ActorSystemService, AssertionError, DuplicateActorError, Event, Inspector as InspectorService, InvalidSchemaError, machine_exports as Machine, MissingMatchHandlerError, MissingSchemaError, NoReplyError, PersistenceError, ProvisionValidationError, Slot, SlotProvisionError, State, Supervision, VersionConflictError, assertNeverReaches, assertPath, assertReaches, collectingInspector, combineInspectors, consoleInspector, createTestHarness, makeInspector, makeInspectorEffect, simulate, tracingInspector };
|
|
@@ -26,6 +26,19 @@ type FullStateBrand<D extends Record<string, unknown>> = StateBrand & SchemaIdBr
|
|
|
26
26
|
* Full event brand: combines base event brand with schema-specific brand
|
|
27
27
|
*/
|
|
28
28
|
type FullEventBrand<D extends Record<string, unknown>> = EventBrand & SchemaIdBrand<D>;
|
|
29
|
+
/**
|
|
30
|
+
* Brand that carries the reply type for an event variant.
|
|
31
|
+
* Present only on events defined with Event.reply().
|
|
32
|
+
*/
|
|
33
|
+
type ReplyTypeId = "effect-machine/ReplyTypeId";
|
|
34
|
+
interface ReplyTypeBrand<R> extends Brand.Brand<ReplyTypeId> {
|
|
35
|
+
readonly _ReplyType: R;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Extract the reply type from a branded event value.
|
|
39
|
+
* Returns `never` if the event has no reply schema.
|
|
40
|
+
*/
|
|
41
|
+
type ExtractReply<E> = E extends ReplyTypeBrand<infer R> ? R : never;
|
|
29
42
|
/**
|
|
30
43
|
* Value or constructor for a tagged type.
|
|
31
44
|
* Accepts both plain values (empty structs) and constructor functions (non-empty structs).
|
|
@@ -34,4 +47,4 @@ type TaggedOrConstructor<T extends {
|
|
|
34
47
|
readonly _tag: string;
|
|
35
48
|
}> = T | ((...args: never[]) => T);
|
|
36
49
|
//#endregion
|
|
37
|
-
export { BrandedEvent, BrandedState, EventBrand, EventTypeId, FullEventBrand, FullStateBrand, SchemaIdBrand, StateBrand, StateTypeId, TaggedOrConstructor };
|
|
50
|
+
export { BrandedEvent, BrandedState, EventBrand, EventTypeId, ExtractReply, FullEventBrand, FullStateBrand, ReplyTypeBrand, ReplyTypeId, SchemaIdBrand, StateBrand, StateTypeId, TaggedOrConstructor };
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { NoReplyError } from "../errors.js";
|
|
2
|
+
import { EffectsDef, GuardsDef, MachineContext } from "../slot.js";
|
|
3
|
+
import { ActorExit } from "../supervision.js";
|
|
4
|
+
import { ProcessEventHooks, ProcessEventResult } from "./transition.js";
|
|
5
|
+
import { Machine, MachineRef } from "../machine.js";
|
|
6
|
+
import { ActorSystem } from "../actor.js";
|
|
7
|
+
import { Deferred, Effect, Queue, Ref, Scope, SubscriptionRef } from "effect";
|
|
8
|
+
|
|
9
|
+
//#region src/internal/runtime.d.ts
|
|
10
|
+
/** @internal */
|
|
11
|
+
type RuntimeQueuedEvent<E> = {
|
|
12
|
+
readonly _tag: "send";
|
|
13
|
+
readonly event: E;
|
|
14
|
+
} | {
|
|
15
|
+
readonly _tag: "sendWait";
|
|
16
|
+
readonly event: E;
|
|
17
|
+
readonly done: Deferred.Deferred<void, unknown>;
|
|
18
|
+
} | {
|
|
19
|
+
readonly _tag: "call";
|
|
20
|
+
readonly event: E;
|
|
21
|
+
readonly reply: Deferred.Deferred<ProcessEventResult<{
|
|
22
|
+
readonly _tag: string;
|
|
23
|
+
}>, unknown>;
|
|
24
|
+
} | {
|
|
25
|
+
readonly _tag: "ask";
|
|
26
|
+
readonly event: E;
|
|
27
|
+
readonly reply: Deferred.Deferred<unknown, NoReplyError>;
|
|
28
|
+
} | {
|
|
29
|
+
readonly _tag: "drain";
|
|
30
|
+
readonly done: Deferred.Deferred<void, never>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Resources owned by the actor cell (stable across generations).
|
|
34
|
+
* When provided, createRuntime uses these instead of allocating its own.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
interface RuntimeCellResources<S, E> {
|
|
38
|
+
readonly stateRef: SubscriptionRef.SubscriptionRef<S>;
|
|
39
|
+
readonly eventQueue: Queue.Queue<RuntimeQueuedEvent<E>>;
|
|
40
|
+
readonly stoppedRef: Ref.Ref<boolean>;
|
|
41
|
+
}
|
|
42
|
+
/** @internal */
|
|
43
|
+
interface RuntimeHandle<S, E> {
|
|
44
|
+
/** Enqueue a fire-and-forget event */
|
|
45
|
+
readonly send: (event: E) => Effect.Effect<void>;
|
|
46
|
+
/** Enqueue event and wait for processing to complete (for RPC Send). Fails on defect. */
|
|
47
|
+
readonly sendWait: (event: E) => Effect.Effect<void, unknown>;
|
|
48
|
+
/** Enqueue an ask event, returns the reply value */
|
|
49
|
+
readonly ask: (event: E) => Effect.Effect<unknown, NoReplyError>;
|
|
50
|
+
/** Get current state */
|
|
51
|
+
readonly getState: Effect.Effect<S>;
|
|
52
|
+
/** SubscriptionRef for state observation (WatchState streaming) */
|
|
53
|
+
readonly stateRef: SubscriptionRef.SubscriptionRef<S>;
|
|
54
|
+
/** Whether the runtime has stopped (final state reached) */
|
|
55
|
+
readonly isStopped: Effect.Effect<boolean>;
|
|
56
|
+
/** Stop the runtime (interrupt event loop, clean up) */
|
|
57
|
+
readonly stop: Effect.Effect<void>;
|
|
58
|
+
/** @internal — raw event queue for direct enqueue (actor.ts uses this for pendingReplies tracking) */
|
|
59
|
+
readonly _queue: Queue.Queue<RuntimeQueuedEvent<E>>;
|
|
60
|
+
/** @internal — stopped ref for direct access */
|
|
61
|
+
readonly _stoppedRef: Ref.Ref<boolean>;
|
|
62
|
+
/**
|
|
63
|
+
* Exit deferred — set exactly once with the exit reason when the runtime stops.
|
|
64
|
+
* Final state → ActorExit.Final, explicit stop → ActorExit.Stopped, defect → ActorExit.Defect.
|
|
65
|
+
*/
|
|
66
|
+
readonly exitDeferred: Deferred.Deferred<ActorExit<S>, never>;
|
|
67
|
+
/**
|
|
68
|
+
* Actor scope — owns background fibers for this generation.
|
|
69
|
+
* Closing this scope interrupts all background fibers.
|
|
70
|
+
*/
|
|
71
|
+
readonly actorScope: Scope.Closeable;
|
|
72
|
+
}
|
|
73
|
+
/** @internal */
|
|
74
|
+
interface RuntimeLifecycleHooks<S, E> {
|
|
75
|
+
/** Before processEventCore — actor emits @machine.event inspection */
|
|
76
|
+
readonly onEvent?: (state: S, event: E) => Effect.Effect<void>;
|
|
77
|
+
/** After SubscriptionRef.set on transition — actor notifies listeners, annotates spans */
|
|
78
|
+
readonly onStateChange?: (result: ProcessEventResult<S>, event: E) => Effect.Effect<void>;
|
|
79
|
+
/** After reply settlement when transition occurred — actor publishes to transitionsPubSub */
|
|
80
|
+
readonly onProcessed?: (result: ProcessEventResult<S>, event: E) => Effect.Effect<void>;
|
|
81
|
+
/** When final state detected in event loop — actor emits @machine.stop */
|
|
82
|
+
readonly onFinal?: (state: S) => Effect.Effect<void>;
|
|
83
|
+
/** Before stop resource cleanup — actor emits @machine.stop, settles pending replies */
|
|
84
|
+
readonly onShutdown?: () => Effect.Effect<void>;
|
|
85
|
+
/** Before initial spawn effects — actor emits @machine.effect inspection */
|
|
86
|
+
readonly onInitialSpawnEffects?: (state: S) => Effect.Effect<void>;
|
|
87
|
+
}
|
|
88
|
+
/** @internal */
|
|
89
|
+
interface RuntimeConfig<S, E> {
|
|
90
|
+
readonly actorId: string;
|
|
91
|
+
readonly hooks?: ProcessEventHooks<S, E>;
|
|
92
|
+
/**
|
|
93
|
+
* Cell-owned resources. When provided, the runtime uses the cell's stateRef,
|
|
94
|
+
* eventQueue, and stoppedRef instead of creating its own.
|
|
95
|
+
* Used by actor.ts for supervision (cell owns stable resources across generations).
|
|
96
|
+
*/
|
|
97
|
+
readonly cellResources?: RuntimeCellResources<S, E>;
|
|
98
|
+
/**
|
|
99
|
+
* Custom queue factory. Default: `Queue.unbounded()`.
|
|
100
|
+
* Use `Queue.sliding(n)` or `Queue.dropping(n)` for bounded queues.
|
|
101
|
+
* Ignored when cellResources is provided.
|
|
102
|
+
*/
|
|
103
|
+
readonly queueFactory?: Effect.Effect<Queue.Queue<RuntimeQueuedEvent<E>>>;
|
|
104
|
+
/** Lifecycle callbacks for actor-specific concerns */
|
|
105
|
+
readonly lifecycle?: RuntimeLifecycleHooks<S, E>;
|
|
106
|
+
/** Wrap each processQueued invocation — actor uses for span annotations */
|
|
107
|
+
readonly wrapProcess?: (state: S, event: E, inner: Effect.Effect<ProcessQueuedResult<S>>) => Effect.Effect<ProcessQueuedResult<S>>;
|
|
108
|
+
/** Called after self.spawn succeeds — actor tracks children */
|
|
109
|
+
readonly onChildSpawned?: (childId: string, child: unknown) => Effect.Effect<void>;
|
|
110
|
+
/** Skip registering stop as scope finalizer — actor manages its own lifecycle */
|
|
111
|
+
readonly skipFinalizer?: boolean;
|
|
112
|
+
/** Prefix for child actor IDs in self.spawn. Entity-machine uses `${actorId}/`. Default: no prefix. */
|
|
113
|
+
readonly childIdPrefix?: string;
|
|
114
|
+
}
|
|
115
|
+
/** @internal */
|
|
116
|
+
interface ProcessQueuedResult<S> {
|
|
117
|
+
readonly shouldStop: boolean;
|
|
118
|
+
readonly stateChanged: boolean;
|
|
119
|
+
readonly result: ProcessEventResult<S>;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Create a runtime for a machine. Returns a handle for sending events
|
|
123
|
+
* and querying state. The runtime owns:
|
|
124
|
+
* - Event loop fiber
|
|
125
|
+
* - Postpone buffer
|
|
126
|
+
* - Background effects (under actorScope)
|
|
127
|
+
* - State scope (spawn effects)
|
|
128
|
+
* - Final state detection
|
|
129
|
+
* - Exit reason via exitDeferred
|
|
130
|
+
*
|
|
131
|
+
* Resources (stateRef, eventQueue, stoppedRef) are either cell-provided
|
|
132
|
+
* or allocated fresh by the runtime.
|
|
133
|
+
*
|
|
134
|
+
* @internal
|
|
135
|
+
*/
|
|
136
|
+
declare const createRuntime: <S extends {
|
|
137
|
+
readonly _tag: string;
|
|
138
|
+
}, E extends {
|
|
139
|
+
readonly _tag: string;
|
|
140
|
+
}, R, GD extends GuardsDef, EFD extends EffectsDef>(machine: Machine<S, E, R, any, any, GD, EFD>, system: ActorSystem, config: RuntimeConfig<S, E>) => Effect.Effect<RuntimeHandle<S, E>, never, Scope.Scope | Exclude<R, MachineContext<S, E, MachineRef<E>>> | Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, Scope.Scope>>;
|
|
141
|
+
//#endregion
|
|
142
|
+
export { ProcessQueuedResult, RuntimeCellResources, RuntimeConfig, RuntimeHandle, RuntimeLifecycleHooks, RuntimeQueuedEvent, createRuntime };
|