effect-machine 0.12.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.
@@ -1,30 +1,15 @@
1
- import { EffectsDef, GuardsDef, MachineContext } from "./slot.js";
1
+ import { ActorExit, Supervision } from "./supervision.js";
2
2
  import { ExtractReply, ReplyTypeBrand } from "./internal/brands.js";
3
3
  import { ActorStoppedError, DuplicateActorError, NoReplyError } from "./errors.js";
4
+ import { EffectsDef, GuardsDef } from "./slot.js";
4
5
  import { ProcessEventError, ProcessEventHooks, ProcessEventResult, processEventCore, resolveTransition, runSpawnEffects } from "./internal/transition.js";
5
- import { BuiltMachine, Machine, MachineRef } from "./machine.js";
6
+ import { Machine } from "./machine.js";
7
+ import { RuntimeQueuedEvent } from "./internal/runtime.js";
6
8
  import { Context, Deferred, Effect, Layer, Option, PubSub, Queue, Ref, Scope, Stream, SubscriptionRef } from "effect";
7
- import * as effect_dist_dts_Tracer_js0 from "effect/dist/dts/Tracer.js";
8
9
 
9
10
  //#region src/actor.d.ts
10
- /** Discriminated mailbox request */
11
- type QueuedEvent<E> = {
12
- readonly _tag: "send";
13
- readonly event: E;
14
- } | {
15
- readonly _tag: "call";
16
- readonly event: E;
17
- readonly reply: Deferred.Deferred<ProcessEventResult<{
18
- readonly _tag: string;
19
- }>, ActorStoppedError>;
20
- } | {
21
- readonly _tag: "ask";
22
- readonly event: E;
23
- readonly reply: Deferred.Deferred<unknown, NoReplyError | ActorStoppedError>;
24
- };
25
- /**
26
- * Reference to a running actor.
27
- */
11
+ /** Discriminated mailbox request — alias for RuntimeQueuedEvent */
12
+ type QueuedEvent<E> = RuntimeQueuedEvent<E>;
28
13
  /**
29
14
  * Sync projection of ActorRef for non-Effect boundaries (React hooks, framework callbacks).
30
15
  */
@@ -104,6 +89,25 @@ interface ActorRef<State extends {
104
89
  };
105
90
  /** Subscribe to state changes (sync callback). Returns unsubscribe function. */
106
91
  readonly subscribe: (fn: (state: State) => void) => () => void;
92
+ /**
93
+ * Wait for this actor's terminal exit. Resolves with the exit reason.
94
+ * Set exactly once when the actor terminates (final, stop, drain, or defect).
95
+ */
96
+ readonly awaitExit: Effect.Effect<ActorExit<State>>;
97
+ /**
98
+ * Watch another actor. Returns an Effect that resolves with the exit reason
99
+ * when the watched actor terminally stops. Ignores restarts.
100
+ * Built on the other actor's exitDeferred — authoritative, not system events.
101
+ */
102
+ readonly watch: (other: {
103
+ readonly id: string;
104
+ readonly awaitExit: Effect.Effect<ActorExit<unknown>>;
105
+ }) => Effect.Effect<ActorExit<unknown>>;
106
+ /**
107
+ * Drain: process all remaining events in the queue, then stop.
108
+ * Unlike `stop` (which interrupts immediately), `drain` lets the actor finish its work.
109
+ */
110
+ readonly drain: Effect.Effect<void>;
107
111
  /** Sync helpers for non-Effect boundaries. */
108
112
  readonly sync: ActorRefSync<State, Event>;
109
113
  /** The actor system this actor belongs to. */
@@ -122,10 +126,17 @@ type SystemEvent = {
122
126
  readonly _tag: "ActorSpawned";
123
127
  readonly id: string;
124
128
  readonly actor: ActorRef<AnyState, unknown>;
129
+ } | {
130
+ readonly _tag: "ActorRestarted";
131
+ readonly id: string;
132
+ readonly actor: ActorRef<AnyState, unknown>;
133
+ readonly generation: number;
134
+ readonly exit: ActorExit<unknown>;
125
135
  } | {
126
136
  readonly _tag: "ActorStopped";
127
137
  readonly id: string;
128
138
  readonly actor: ActorRef<AnyState, unknown>;
139
+ readonly exit: ActorExit<unknown>;
129
140
  };
130
141
  /**
131
142
  * Listener callback for system events.
@@ -137,18 +148,15 @@ type SystemEventListener = (event: SystemEvent) => void;
137
148
  interface ActorSystem {
138
149
  /**
139
150
  * Spawn a new actor with the given machine.
140
- *
141
- * @example
142
- * ```ts
143
- * const built = machine.build({ fetchData: ... })
144
- * const actor = yield* system.spawn("my-actor", built);
145
- * ```
146
151
  */
147
152
  readonly spawn: <S extends {
148
153
  readonly _tag: string;
149
154
  }, E extends {
150
155
  readonly _tag: string;
151
- }, R>(id: string, machine: BuiltMachine<S, E, R>) => Effect.Effect<ActorRef<S, E>, DuplicateActorError, R>;
156
+ }, R>(id: string, machine: Machine<S, E, R, any, any, any, any>, options?: {
157
+ slots?: Record<string, any>;
158
+ supervision?: Supervision.Policy;
159
+ }) => Effect.Effect<ActorRef<S, E>, DuplicateActorError, R>;
152
160
  /**
153
161
  * Get an existing actor by ID
154
162
  */
@@ -184,15 +192,16 @@ type Listeners<S> = Set<(state: S) => void>;
184
192
  */
185
193
  declare const notifyListeners: <S>(listeners: Listeners<S>, state: S) => void;
186
194
  /**
187
- * Build core ActorRef methods shared between regular and persistent actors.
195
+ * Build core ActorRef methods.
188
196
  */
189
197
  declare const buildActorRefCore: <S extends {
190
198
  readonly _tag: string;
191
199
  }, E extends {
192
200
  readonly _tag: string;
193
- }, R, GD extends GuardsDef, EFD extends EffectsDef>(id: string, machine: Machine<S, E, R, any, any, GD, EFD>, stateRef: SubscriptionRef.SubscriptionRef<S>, eventQueue: Queue.Queue<QueuedEvent<E>>, stoppedRef: Ref.Ref<boolean>, listeners: Listeners<S>, stop: Effect.Effect<void>, system: ActorSystem, childrenMap: ReadonlyMap<string, ActorRef<AnyState, unknown>>, pendingReplies: Set<Deferred.Deferred<unknown, unknown>>, transitionsPubSub?: PubSub.PubSub<TransitionInfo<S, E>>) => ActorRef<S, E>;
201
+ }, R, GD extends GuardsDef, EFD extends EffectsDef>(id: string, machine: Machine<S, E, R, any, any, GD, EFD>, stateRef: SubscriptionRef.SubscriptionRef<S>, eventQueueRef: Ref.Ref<Queue.Queue<QueuedEvent<E>>>, stoppedRef: Ref.Ref<boolean>, listeners: Listeners<S>, stop: Effect.Effect<void>, system: ActorSystem, childrenMap: ReadonlyMap<string, ActorRef<AnyState, unknown>>, pendingReplies: Set<Deferred.Deferred<unknown, unknown>>, transitionsPubSub: PubSub.PubSub<TransitionInfo<S, E>> | undefined, exitDeferred: Deferred.Deferred<ActorExit<S>, never>) => ActorRef<S, E>;
194
202
  /**
195
- * Create and start an actor for a machine
203
+ * Create and start an actor for a machine.
204
+ * Uses the shared runtime kernel with lifecycle hooks for actor-specific concerns.
196
205
  */
197
206
  declare const createActor: <S extends {
198
207
  readonly _tag: string;
@@ -200,12 +209,19 @@ declare const createActor: <S extends {
200
209
  readonly _tag: string;
201
210
  }, R, GD extends GuardsDef, EFD extends EffectsDef>(id: string, machine: Machine<S, E, R, Record<string, never>, Record<string, never>, GD, EFD>, options?: {
202
211
  initialState?: S;
203
- } | undefined) => Effect.Effect<ActorRef<S, E>, never, Exclude<R, MachineContext<S, E, MachineRef<E>>> | Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, effect_dist_dts_Tracer_js0.ParentSpan> | Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, Scope.Scope> | Exclude<Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, Scope.Scope>, effect_dist_dts_Tracer_js0.ParentSpan>>;
212
+ supervision?: Supervision.Policy; /** @internal Called by system after each restart emits ActorRestarted system event */
213
+ onRestart?: (generation: number, exit: ActorExit<unknown>) => Effect.Effect<void>;
214
+ } | undefined) => Effect.Effect<ActorRef<S, E>, never, never>;
204
215
  /** Fail all pending call/ask Deferreds with ActorStoppedError. Safe to call multiple times. */
205
216
  declare const settlePendingReplies: (pendingReplies: Set<Deferred.Deferred<unknown, unknown>>, actorId: string) => Effect.Effect<void, never, never>;
217
+ /**
218
+ * Create an ActorSystem instance. Must be run in a Scope.
219
+ * @internal — use Default layer for normal usage
220
+ */
221
+ declare const makeSystem: () => Effect.Effect<ActorSystem, never, Scope.Scope>;
206
222
  /**
207
223
  * Default ActorSystem layer
208
224
  */
209
- declare const Default: Layer.Layer<ActorSystem, never, never>;
225
+ declare const Default: Layer.Layer<ActorSystem, never, Scope.Scope>;
210
226
  //#endregion
211
- export { ActorRef, ActorRefSync, ActorSystem, Default, Listeners, type ProcessEventError, type ProcessEventHooks, type ProcessEventResult, QueuedEvent, SystemEvent, SystemEventListener, TransitionInfo, buildActorRefCore, createActor, notifyListeners, processEventCore, resolveTransition, runSpawnEffects, settlePendingReplies };
227
+ export { ActorRef, ActorRefSync, ActorSystem, Default, Listeners, type ProcessEventError, type ProcessEventHooks, type ProcessEventResult, QueuedEvent, SystemEvent, SystemEventListener, TransitionInfo, buildActorRefCore, createActor, makeSystem, notifyListeners, processEventCore, resolveTransition, runSpawnEffects, settlePendingReplies };