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.
Files changed (68) hide show
  1. package/README.md +128 -324
  2. package/dist/actor.d.ts +52 -31
  3. package/dist/actor.js +218 -283
  4. package/dist/cluster/adapters/in-memory.d.ts +28 -0
  5. package/dist/cluster/adapters/in-memory.js +79 -0
  6. package/dist/cluster/entity-actor-ref.d.ts +56 -0
  7. package/dist/cluster/entity-actor-ref.js +33 -0
  8. package/dist/cluster/entity-machine.d.ts +31 -49
  9. package/dist/cluster/entity-machine.js +178 -52
  10. package/dist/cluster/index.d.ts +5 -2
  11. package/dist/cluster/index.js +4 -1
  12. package/dist/cluster/persistence.d.ts +49 -0
  13. package/dist/cluster/persistence.js +18 -0
  14. package/dist/cluster/to-entity.d.ts +9 -3
  15. package/dist/cluster/to-entity.js +16 -4
  16. package/dist/errors.d.ts +25 -17
  17. package/dist/errors.js +10 -5
  18. package/dist/index.d.ts +6 -4
  19. package/dist/index.js +4 -3
  20. package/dist/internal/brands.d.ts +14 -1
  21. package/dist/internal/runtime.d.ts +142 -0
  22. package/dist/internal/runtime.js +357 -0
  23. package/dist/internal/transition.d.ts +10 -4
  24. package/dist/internal/transition.js +24 -12
  25. package/dist/internal/utils.d.ts +42 -6
  26. package/dist/internal/utils.js +27 -1
  27. package/dist/machine.d.ts +89 -55
  28. package/dist/machine.js +80 -68
  29. package/dist/schema.d.ts +35 -34
  30. package/dist/schema.js +33 -4
  31. package/dist/supervision.d.ts +97 -0
  32. package/dist/supervision.js +42 -0
  33. package/dist/testing.d.ts +17 -8
  34. package/dist/testing.js +22 -23
  35. package/package.json +7 -7
  36. package/v3/dist/actor.d.ts +54 -37
  37. package/v3/dist/actor.js +209 -277
  38. package/v3/dist/cluster/adapters/in-memory.d.ts +15 -0
  39. package/v3/dist/cluster/adapters/in-memory.js +62 -0
  40. package/v3/dist/cluster/entity-actor-ref.d.ts +49 -0
  41. package/v3/dist/cluster/entity-actor-ref.js +19 -0
  42. package/v3/dist/cluster/entity-machine.d.ts +34 -49
  43. package/v3/dist/cluster/entity-machine.js +134 -50
  44. package/v3/dist/cluster/index.d.ts +5 -2
  45. package/v3/dist/cluster/index.js +4 -1
  46. package/v3/dist/cluster/persistence.d.ts +48 -0
  47. package/v3/dist/cluster/persistence.js +14 -0
  48. package/v3/dist/cluster/to-entity.d.ts +5 -2
  49. package/v3/dist/cluster/to-entity.js +12 -4
  50. package/v3/dist/errors.d.ts +18 -8
  51. package/v3/dist/errors.js +9 -4
  52. package/v3/dist/index.d.ts +6 -4
  53. package/v3/dist/index.js +3 -2
  54. package/v3/dist/internal/brands.d.ts +15 -1
  55. package/v3/dist/internal/runtime.d.ts +142 -0
  56. package/v3/dist/internal/runtime.js +335 -0
  57. package/v3/dist/internal/transition.d.ts +10 -4
  58. package/v3/dist/internal/transition.js +23 -11
  59. package/v3/dist/internal/utils.d.ts +42 -6
  60. package/v3/dist/internal/utils.js +27 -1
  61. package/v3/dist/machine.d.ts +35 -47
  62. package/v3/dist/machine.js +62 -64
  63. package/v3/dist/schema.d.ts +35 -34
  64. package/v3/dist/schema.js +29 -3
  65. package/v3/dist/supervision.d.ts +97 -0
  66. package/v3/dist/supervision.js +42 -0
  67. package/v3/dist/testing.d.ts +18 -9
  68. package/v3/dist/testing.js +21 -22
package/dist/actor.d.ts CHANGED
@@ -1,29 +1,15 @@
1
- import { EffectsDef, GuardsDef, MachineContext } from "./slot.js";
1
+ import { ExtractReply, ReplyTypeBrand } from "./internal/brands.js";
2
2
  import { ActorStoppedError, DuplicateActorError, NoReplyError } from "./errors.js";
3
+ import { EffectsDef, GuardsDef } from "./slot.js";
4
+ import { ActorExit, Supervision } from "./supervision.js";
3
5
  import { ProcessEventError, ProcessEventHooks, ProcessEventResult, processEventCore, resolveTransition, runSpawnEffects } from "./internal/transition.js";
4
- import { BuiltMachine, Machine, MachineRef } from "./machine.js";
6
+ import { Machine } from "./machine.js";
7
+ import { RuntimeQueuedEvent } from "./internal/runtime.js";
5
8
  import { Deferred, Effect, Layer, Option, PubSub, Queue, Ref, Scope, ServiceMap, Stream, SubscriptionRef } from "effect";
6
- import * as effect_Tracer0 from "effect/Tracer";
7
9
 
8
10
  //#region src/actor.d.ts
9
- /** Discriminated mailbox request */
10
- type QueuedEvent<E> = {
11
- readonly _tag: "send";
12
- readonly event: E;
13
- } | {
14
- readonly _tag: "call";
15
- readonly event: E;
16
- readonly reply: Deferred.Deferred<ProcessEventResult<{
17
- readonly _tag: string;
18
- }>, ActorStoppedError>;
19
- } | {
20
- readonly _tag: "ask";
21
- readonly event: E;
22
- readonly reply: Deferred.Deferred<unknown, NoReplyError | ActorStoppedError>;
23
- };
24
- /**
25
- * Reference to a running actor.
26
- */
11
+ /** Discriminated mailbox request — alias for RuntimeQueuedEvent */
12
+ type QueuedEvent<E> = RuntimeQueuedEvent<E>;
27
13
  /**
28
14
  * Sync projection of ActorRef for non-Effect boundaries (React hooks, framework callbacks).
29
15
  */
@@ -59,11 +45,11 @@ interface ActorRef<State extends {
59
45
  */
60
46
  readonly call: (event: Event) => Effect.Effect<ProcessEventResult<State>>;
61
47
  /**
62
- * Typed request-reply. Event is processed through the queue; caller gets
63
- * the domain value returned by the handler's `reply` field.
48
+ * Typed request-reply. Accepts only events with a reply schema
49
+ * (defined via `Event.reply()`). Return type is inferred from the schema.
64
50
  * Fails with NoReplyError if the handler doesn't provide a reply.
65
51
  */
66
- readonly ask: <R>(event: Event) => Effect.Effect<R, NoReplyError | ActorStoppedError>;
52
+ readonly ask: <E extends Event & ReplyTypeBrand<unknown>>(event: E) => Effect.Effect<ExtractReply<E>, NoReplyError | ActorStoppedError>;
67
53
  /** Observable state. */
68
54
  readonly state: SubscriptionRef.SubscriptionRef<State>;
69
55
  /** Stop the actor gracefully. */
@@ -103,6 +89,25 @@ interface ActorRef<State extends {
103
89
  };
104
90
  /** Subscribe to state changes (sync callback). Returns unsubscribe function. */
105
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>;
106
111
  /** Sync helpers for non-Effect boundaries. */
107
112
  readonly sync: ActorRefSync<State, Event>;
108
113
  /** The actor system this actor belongs to. */
@@ -121,10 +126,17 @@ type SystemEvent = {
121
126
  readonly _tag: "ActorSpawned";
122
127
  readonly id: string;
123
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>;
124
135
  } | {
125
136
  readonly _tag: "ActorStopped";
126
137
  readonly id: string;
127
138
  readonly actor: ActorRef<AnyState, unknown>;
139
+ readonly exit: ActorExit<unknown>;
128
140
  };
129
141
  /**
130
142
  * Listener callback for system events.
@@ -139,15 +151,16 @@ interface ActorSystem {
139
151
  *
140
152
  * @example
141
153
  * ```ts
142
- * const built = machine.build({ fetchData: ... })
143
- * const actor = yield* system.spawn("my-actor", built);
154
+ * const actor = yield* system.spawn("my-actor", machine);
144
155
  * ```
145
156
  */
146
157
  readonly spawn: <S extends {
147
158
  readonly _tag: string;
148
159
  }, E extends {
149
160
  readonly _tag: string;
150
- }, R>(id: string, machine: BuiltMachine<S, E, R>) => Effect.Effect<ActorRef<S, E>, DuplicateActorError, R>;
161
+ }, R>(id: string, machine: Machine<S, E, R, any, any, any, any>, options?: {
162
+ readonly supervision?: Supervision.Policy;
163
+ }) => Effect.Effect<ActorRef<S, E>, DuplicateActorError, R>;
151
164
  /**
152
165
  * Get an existing actor by ID
153
166
  */
@@ -189,9 +202,10 @@ declare const buildActorRefCore: <S extends {
189
202
  readonly _tag: string;
190
203
  }, E extends {
191
204
  readonly _tag: string;
192
- }, 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>;
205
+ }, 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>;
193
206
  /**
194
- * Create and start an actor for a machine
207
+ * Create and start an actor for a machine.
208
+ * Delegates to the shared runtime kernel with actor-specific lifecycle hooks.
195
209
  */
196
210
  declare const createActor: <S extends {
197
211
  readonly _tag: string;
@@ -199,12 +213,19 @@ declare const createActor: <S extends {
199
213
  readonly _tag: string;
200
214
  }, R, GD extends GuardsDef, EFD extends EffectsDef>(id: string, machine: Machine<S, E, R, Record<string, never>, Record<string, never>, GD, EFD>, options?: {
201
215
  initialState?: S;
202
- } | 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>>;
216
+ supervision?: Supervision.Policy; /** @internal Called by system after each restart emits ActorRestarted system event */
217
+ onRestart?: (generation: number, exit: ActorExit<unknown>) => Effect.Effect<void>;
218
+ } | undefined) => Effect.Effect<ActorRef<S, E>, never, never>;
203
219
  /** Fail all pending call/ask Deferreds with ActorStoppedError. Safe to call multiple times. */
204
220
  declare const settlePendingReplies: (pendingReplies: Set<Deferred.Deferred<unknown, unknown>>, actorId: string) => Effect.Effect<void, never, never>;
221
+ /**
222
+ * Create an ActorSystem instance. Must be run in a Scope.
223
+ * @internal — use Default layer for normal usage
224
+ */
225
+ declare const makeSystem: () => Effect.Effect<ActorSystem, never, Scope.Scope>;
205
226
  /**
206
227
  * Default ActorSystem layer
207
228
  */
208
229
  declare const Default: Layer.Layer<ActorSystem, never, never>;
209
230
  //#endregion
210
- export { ActorRef, ActorRefSync, ActorSystem, Default, Listeners, type ProcessEventError, type ProcessEventHooks, type ProcessEventResult, QueuedEvent, SystemEvent, SystemEventListener, TransitionInfo, buildActorRefCore, createActor, notifyListeners, processEventCore, resolveTransition, runSpawnEffects, settlePendingReplies };
231
+ export { ActorRef, ActorRefSync, ActorSystem, Default, Listeners, type ProcessEventError, type ProcessEventHooks, type ProcessEventResult, QueuedEvent, SystemEvent, SystemEventListener, TransitionInfo, buildActorRefCore, createActor, makeSystem, notifyListeners, processEventCore, resolveTransition, runSpawnEffects, settlePendingReplies };