effect-machine 0.12.0 → 0.14.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.
Files changed (53) hide show
  1. package/README.md +133 -324
  2. package/dist/actor.d.ts +46 -28
  3. package/dist/actor.js +276 -315
  4. package/dist/cluster/entity-machine.d.ts +1 -1
  5. package/dist/cluster/entity-machine.js +20 -9
  6. package/dist/cluster/to-entity.d.ts +3 -3
  7. package/dist/errors.d.ts +24 -20
  8. package/dist/errors.js +10 -6
  9. package/dist/index.d.ts +5 -4
  10. package/dist/index.js +3 -2
  11. package/dist/internal/runtime.d.ts +82 -7
  12. package/dist/internal/runtime.js +162 -58
  13. package/dist/internal/transition.d.ts +12 -11
  14. package/dist/internal/transition.js +12 -14
  15. package/dist/machine.d.ts +148 -140
  16. package/dist/machine.js +141 -155
  17. package/dist/schema.d.ts +14 -0
  18. package/dist/schema.js +10 -1
  19. package/dist/slot.d.ts +112 -86
  20. package/dist/slot.js +92 -59
  21. package/dist/supervision.d.ts +97 -0
  22. package/dist/supervision.js +42 -0
  23. package/dist/testing.d.ts +21 -12
  24. package/dist/testing.js +23 -26
  25. package/package.json +7 -7
  26. package/v3/dist/actor.d.ts +53 -30
  27. package/v3/dist/actor.js +286 -311
  28. package/v3/dist/cluster/entity-machine.d.ts +1 -1
  29. package/v3/dist/cluster/entity-machine.js +5 -5
  30. package/v3/dist/cluster/to-entity.d.ts +1 -1
  31. package/v3/dist/errors.d.ts +14 -10
  32. package/v3/dist/errors.js +11 -7
  33. package/v3/dist/index.d.ts +6 -5
  34. package/v3/dist/index.js +3 -2
  35. package/v3/dist/inspection.d.ts +3 -22
  36. package/v3/dist/inspection.js +1 -15
  37. package/v3/dist/internal/brands.d.ts +4 -8
  38. package/v3/dist/internal/inspection.js +1 -1
  39. package/v3/dist/internal/runtime.d.ts +87 -10
  40. package/v3/dist/internal/runtime.js +177 -61
  41. package/v3/dist/internal/transition.d.ts +13 -12
  42. package/v3/dist/internal/transition.js +14 -16
  43. package/v3/dist/internal/utils.js +5 -1
  44. package/v3/dist/machine.d.ts +158 -143
  45. package/v3/dist/machine.js +148 -155
  46. package/v3/dist/schema.d.ts +25 -11
  47. package/v3/dist/schema.js +18 -5
  48. package/v3/dist/slot.d.ts +112 -86
  49. package/v3/dist/slot.js +92 -59
  50. package/v3/dist/supervision.d.ts +97 -0
  51. package/v3/dist/supervision.js +42 -0
  52. package/v3/dist/testing.d.ts +21 -12
  53. package/v3/dist/testing.js +23 -24
package/dist/actor.d.ts CHANGED
@@ -1,30 +1,15 @@
1
- import { EffectsDef, GuardsDef, MachineContext } from "./slot.js";
2
1
  import { ExtractReply, ReplyTypeBrand } from "./internal/brands.js";
3
2
  import { ActorStoppedError, DuplicateActorError, NoReplyError } from "./errors.js";
3
+ import { ProvideSlots, SlotsDef } from "./slot.js";
4
+ import { ActorExit, Supervision } from "./supervision.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, PersistConfig } from "./machine.js";
7
+ import { RuntimeQueuedEvent } from "./internal/runtime.js";
6
8
  import { Deferred, Effect, Layer, Option, PubSub, Queue, Ref, Scope, ServiceMap, Stream, SubscriptionRef } from "effect";
7
- import * as effect_Tracer0 from "effect/Tracer";
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 (Step 3).
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.
@@ -140,15 +151,18 @@ interface ActorSystem {
140
151
  *
141
152
  * @example
142
153
  * ```ts
143
- * const built = machine.build({ fetchData: ... })
144
- * const actor = yield* system.spawn("my-actor", built);
154
+ * const actor = yield* system.spawn("my-actor", machine);
145
155
  * ```
146
156
  */
147
157
  readonly spawn: <S extends {
148
158
  readonly _tag: string;
149
159
  }, E extends {
150
160
  readonly _tag: string;
151
- }, R>(id: string, machine: BuiltMachine<S, E, R>) => Effect.Effect<ActorRef<S, E>, DuplicateActorError, R>;
161
+ }, R, SD extends SlotsDef = Record<string, never>>(id: string, machine: Machine<S, E, R, any, any, SD>, options?: {
162
+ readonly supervision?: Supervision.Policy;
163
+ readonly slots?: ProvideSlots<SD, any>;
164
+ readonly persist?: PersistConfig<S>;
165
+ }) => Effect.Effect<ActorRef<S, E>, DuplicateActorError, R>;
152
166
  /**
153
167
  * Get an existing actor by ID
154
168
  */
@@ -190,17 +204,21 @@ declare const buildActorRefCore: <S extends {
190
204
  readonly _tag: string;
191
205
  }, E extends {
192
206
  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>;
207
+ }, R, SD extends SlotsDef>(id: string, machine: Machine<S, E, R, any, any, SD>, 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
208
  /**
195
- * Create and start an actor for a machine
209
+ * Create and start an actor for a machine.
210
+ * Delegates to the shared runtime kernel with actor-specific lifecycle hooks.
196
211
  */
197
212
  declare const createActor: <S extends {
198
213
  readonly _tag: string;
199
214
  }, E extends {
200
215
  readonly _tag: string;
201
- }, R, GD extends GuardsDef, EFD extends EffectsDef>(id: string, machine: Machine<S, E, R, Record<string, never>, Record<string, never>, GD, EFD>, options?: {
216
+ }, R, SD extends SlotsDef>(id: string, machine: Machine<S, E, R, any, any, SD>, options?: {
202
217
  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_Tracer0.ParentSpan> | Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, Scope.Scope> | Exclude<Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, Scope.Scope>, effect_Tracer0.ParentSpan>>;
218
+ supervision?: Supervision.Policy;
219
+ persist?: PersistConfig<S>; /** @internal Called by system after each restart — emits ActorRestarted system event */
220
+ onRestart?: (generation: number, exit: ActorExit<unknown>) => Effect.Effect<void>;
221
+ } | undefined) => Effect.Effect<ActorRef<S, E>, never, never>;
204
222
  /** Fail all pending call/ask Deferreds with ActorStoppedError. Safe to call multiple times. */
205
223
  declare const settlePendingReplies: (pendingReplies: Set<Deferred.Deferred<unknown, unknown>>, actorId: string) => Effect.Effect<void, never, never>;
206
224
  /**