effect-machine 0.4.0 → 0.7.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 (70) hide show
  1. package/README.md +36 -0
  2. package/dist/actor.d.ts +40 -4
  3. package/dist/actor.js +89 -34
  4. package/dist/cluster/entity-machine.d.ts +2 -2
  5. package/dist/cluster/entity-machine.js +1 -1
  6. package/dist/cluster/to-entity.d.ts +5 -5
  7. package/dist/cluster/to-entity.js +2 -2
  8. package/dist/errors.d.ts +25 -40
  9. package/dist/errors.js +10 -10
  10. package/dist/index.d.ts +2 -2
  11. package/dist/inspection.d.ts +3 -3
  12. package/dist/inspection.js +2 -2
  13. package/dist/internal/brands.d.ts +3 -6
  14. package/dist/internal/inspection.js +5 -1
  15. package/dist/internal/transition.d.ts +2 -2
  16. package/dist/internal/transition.js +6 -6
  17. package/dist/internal/utils.js +11 -2
  18. package/dist/machine.d.ts +5 -5
  19. package/dist/machine.js +9 -5
  20. package/dist/persistence/adapter.d.ts +18 -21
  21. package/dist/persistence/adapter.js +4 -4
  22. package/dist/persistence/adapters/in-memory.js +4 -4
  23. package/dist/persistence/persistent-actor.js +23 -14
  24. package/dist/persistence/persistent-machine.d.ts +3 -3
  25. package/dist/schema.d.ts +4 -4
  26. package/dist/schema.js +2 -2
  27. package/dist/slot.d.ts +3 -3
  28. package/dist/slot.js +2 -2
  29. package/dist-v3/_virtual/_rolldown/runtime.js +18 -0
  30. package/dist-v3/actor.d.ts +291 -0
  31. package/dist-v3/actor.js +459 -0
  32. package/dist-v3/cluster/entity-machine.d.ts +90 -0
  33. package/dist-v3/cluster/entity-machine.js +80 -0
  34. package/dist-v3/cluster/index.d.ts +3 -0
  35. package/dist-v3/cluster/index.js +4 -0
  36. package/dist-v3/cluster/to-entity.d.ts +61 -0
  37. package/dist-v3/cluster/to-entity.js +53 -0
  38. package/dist-v3/errors.d.ts +27 -0
  39. package/dist-v3/errors.js +38 -0
  40. package/dist-v3/index.d.ts +13 -0
  41. package/dist-v3/index.js +14 -0
  42. package/dist-v3/inspection.d.ts +125 -0
  43. package/dist-v3/inspection.js +50 -0
  44. package/dist-v3/internal/brands.d.ts +40 -0
  45. package/dist-v3/internal/brands.js +0 -0
  46. package/dist-v3/internal/inspection.d.ts +11 -0
  47. package/dist-v3/internal/inspection.js +15 -0
  48. package/dist-v3/internal/transition.d.ts +160 -0
  49. package/dist-v3/internal/transition.js +238 -0
  50. package/dist-v3/internal/utils.d.ts +60 -0
  51. package/dist-v3/internal/utils.js +51 -0
  52. package/dist-v3/machine.d.ts +278 -0
  53. package/dist-v3/machine.js +317 -0
  54. package/dist-v3/persistence/adapter.d.ts +125 -0
  55. package/dist-v3/persistence/adapter.js +27 -0
  56. package/dist-v3/persistence/adapters/in-memory.d.ts +32 -0
  57. package/dist-v3/persistence/adapters/in-memory.js +176 -0
  58. package/dist-v3/persistence/index.d.ts +5 -0
  59. package/dist-v3/persistence/index.js +6 -0
  60. package/dist-v3/persistence/persistent-actor.d.ts +49 -0
  61. package/dist-v3/persistence/persistent-actor.js +367 -0
  62. package/dist-v3/persistence/persistent-machine.d.ts +105 -0
  63. package/dist-v3/persistence/persistent-machine.js +24 -0
  64. package/dist-v3/schema.d.ts +141 -0
  65. package/dist-v3/schema.js +165 -0
  66. package/dist-v3/slot.d.ts +130 -0
  67. package/dist-v3/slot.js +99 -0
  68. package/dist-v3/testing.d.ts +136 -0
  69. package/dist-v3/testing.js +138 -0
  70. package/package.json +29 -21
@@ -0,0 +1,291 @@
1
+ import { PersistentMachine } from "./persistence/persistent-machine.js";
2
+ import { DuplicateActorError } from "./errors.js";
3
+ import { EffectsDef, GuardsDef } from "./slot.js";
4
+ import { ProcessEventError, ProcessEventHooks, ProcessEventResult, processEventCore, resolveTransition, runSpawnEffects } from "./internal/transition.js";
5
+ import { PersistentActorRef } from "./persistence/persistent-actor.js";
6
+ import { ActorMetadata, PersistenceAdapterTag, PersistenceError, RestoreResult, VersionConflictError } from "./persistence/adapter.js";
7
+ import { BuiltMachine, Machine } from "./machine.js";
8
+ import { Effect, Option, Queue, Ref, Stream, SubscriptionRef } from "effect";
9
+
10
+ //#region src-v3/actor.d.ts
11
+ /**
12
+ * Reference to a running actor.
13
+ */
14
+ interface ActorRef<State extends {
15
+ readonly _tag: string;
16
+ }, Event> {
17
+ /**
18
+ * Unique identifier for this actor
19
+ */
20
+ readonly id: string;
21
+ /**
22
+ * Send an event to the actor
23
+ */
24
+ readonly send: (event: Event) => Effect.Effect<void>;
25
+ /**
26
+ * Observable state of the actor
27
+ */
28
+ readonly state: SubscriptionRef.SubscriptionRef<State>;
29
+ /**
30
+ * Stop the actor gracefully
31
+ */
32
+ readonly stop: Effect.Effect<void>;
33
+ /**
34
+ * Stop the actor (fire-and-forget).
35
+ * Signals graceful shutdown without waiting for completion.
36
+ * Use when stopping from sync contexts (e.g. framework cleanup hooks).
37
+ */
38
+ readonly stopSync: () => void;
39
+ /**
40
+ * Get current state snapshot (Effect)
41
+ */
42
+ readonly snapshot: Effect.Effect<State>;
43
+ /**
44
+ * Get current state snapshot (sync)
45
+ */
46
+ readonly snapshotSync: () => State;
47
+ /**
48
+ * Check if current state matches tag (Effect)
49
+ */
50
+ readonly matches: (tag: State["_tag"]) => Effect.Effect<boolean>;
51
+ /**
52
+ * Check if current state matches tag (sync)
53
+ */
54
+ readonly matchesSync: (tag: State["_tag"]) => boolean;
55
+ /**
56
+ * Check if event can be handled in current state (Effect)
57
+ */
58
+ readonly can: (event: Event) => Effect.Effect<boolean>;
59
+ /**
60
+ * Check if event can be handled in current state (sync)
61
+ */
62
+ readonly canSync: (event: Event) => boolean;
63
+ /**
64
+ * Stream of state changes
65
+ */
66
+ readonly changes: Stream.Stream<State>;
67
+ /**
68
+ * Wait for a state that matches predicate or state variant (includes current snapshot).
69
+ * Accepts a predicate function or a state constructor/value (e.g. `State.Active`).
70
+ */
71
+ readonly waitFor: {
72
+ (predicate: (state: State) => boolean): Effect.Effect<State>;
73
+ (state: {
74
+ readonly _tag: State["_tag"];
75
+ }): Effect.Effect<State>;
76
+ };
77
+ /**
78
+ * Wait for a final state (includes current snapshot)
79
+ */
80
+ readonly awaitFinal: Effect.Effect<State>;
81
+ /**
82
+ * Send event and wait for predicate, state variant, or final state.
83
+ * Accepts a predicate function or a state constructor/value (e.g. `State.Active`).
84
+ */
85
+ readonly sendAndWait: {
86
+ (event: Event, predicate: (state: State) => boolean): Effect.Effect<State>;
87
+ (event: Event, state: {
88
+ readonly _tag: State["_tag"];
89
+ }): Effect.Effect<State>;
90
+ (event: Event): Effect.Effect<State>;
91
+ };
92
+ /**
93
+ * Send event synchronously (fire-and-forget).
94
+ * No-op on stopped actors. Use when you need to send from sync contexts
95
+ * (e.g. framework hooks, event handlers).
96
+ */
97
+ readonly sendSync: (event: Event) => void;
98
+ /**
99
+ * Subscribe to state changes (sync callback)
100
+ * Returns unsubscribe function
101
+ */
102
+ readonly subscribe: (fn: (state: State) => void) => () => void;
103
+ /**
104
+ * The actor system this actor belongs to.
105
+ * Every actor always has a system — either inherited from context or implicitly created.
106
+ */
107
+ readonly system: ActorSystem;
108
+ /**
109
+ * Child actors spawned via `self.spawn` in this actor's handlers.
110
+ * State-scoped children are auto-removed on state exit.
111
+ */
112
+ readonly children: ReadonlyMap<string, ActorRef<AnyState, unknown>>;
113
+ }
114
+ /** Base type for stored actors (internal) */
115
+ type AnyState = {
116
+ readonly _tag: string;
117
+ };
118
+ /**
119
+ * Events emitted by the ActorSystem when actors are spawned or stopped.
120
+ */
121
+ type SystemEvent = {
122
+ readonly _tag: "ActorSpawned";
123
+ readonly id: string;
124
+ readonly actor: ActorRef<AnyState, unknown>;
125
+ } | {
126
+ readonly _tag: "ActorStopped";
127
+ readonly id: string;
128
+ readonly actor: ActorRef<AnyState, unknown>;
129
+ };
130
+ /**
131
+ * Listener callback for system events.
132
+ */
133
+ type SystemEventListener = (event: SystemEvent) => void;
134
+ /**
135
+ * Actor system for managing actor lifecycles
136
+ */
137
+ interface ActorSystem {
138
+ /**
139
+ * Spawn a new actor with the given machine.
140
+ *
141
+ * For regular machines, returns ActorRef.
142
+ * For persistent machines (created with Machine.persist), returns PersistentActorRef.
143
+ *
144
+ * All effect slots must be provided via `.build()` before spawning.
145
+ *
146
+ * @example
147
+ * ```ts
148
+ * // Regular machine (built)
149
+ * const built = machine.build({ fetchData: ... })
150
+ * const actor = yield* system.spawn("my-actor", built);
151
+ *
152
+ * // Persistent machine (auto-detected)
153
+ * const persistentActor = yield* system.spawn("my-actor", persistentMachine);
154
+ * persistentActor.persist; // available
155
+ * persistentActor.version; // available
156
+ * ```
157
+ */
158
+ readonly spawn: {
159
+ <S extends {
160
+ readonly _tag: string;
161
+ }, E extends {
162
+ readonly _tag: string;
163
+ }, R>(id: string, machine: BuiltMachine<S, E, R>): Effect.Effect<ActorRef<S, E>, DuplicateActorError, R>;
164
+ <S extends {
165
+ readonly _tag: string;
166
+ }, E extends {
167
+ readonly _tag: string;
168
+ }, R>(id: string, machine: PersistentMachine<S, E, R>): Effect.Effect<PersistentActorRef<S, E, R>, PersistenceError | VersionConflictError | DuplicateActorError, R | PersistenceAdapterTag>;
169
+ };
170
+ /**
171
+ * Restore an actor from persistence.
172
+ * Returns None if no persisted state exists for the given ID.
173
+ *
174
+ * @example
175
+ * ```ts
176
+ * const maybeActor = yield* system.restore("order-1", persistentMachine);
177
+ * if (Option.isSome(maybeActor)) {
178
+ * const actor = maybeActor.value;
179
+ * const state = yield* actor.snapshot;
180
+ * console.log(`Restored to state: ${state._tag}`);
181
+ * }
182
+ * ```
183
+ */
184
+ readonly restore: <S extends {
185
+ readonly _tag: string;
186
+ }, E extends {
187
+ readonly _tag: string;
188
+ }, R>(id: string, machine: PersistentMachine<S, E, R>) => Effect.Effect<Option.Option<PersistentActorRef<S, E, R>>, PersistenceError | DuplicateActorError, R | PersistenceAdapterTag>;
189
+ /**
190
+ * Get an existing actor by ID
191
+ */
192
+ readonly get: (id: string) => Effect.Effect<Option.Option<ActorRef<AnyState, unknown>>>;
193
+ /**
194
+ * Stop an actor by ID
195
+ */
196
+ readonly stop: (id: string) => Effect.Effect<boolean>;
197
+ /**
198
+ * Async stream of system events (actor spawned/stopped).
199
+ * Each subscriber gets their own queue — late subscribers miss prior events.
200
+ */
201
+ readonly events: Stream.Stream<SystemEvent>;
202
+ /**
203
+ * Sync snapshot of all currently registered actors.
204
+ * Returns a new Map on each access (not live).
205
+ */
206
+ readonly actors: ReadonlyMap<string, ActorRef<AnyState, unknown>>;
207
+ /**
208
+ * Subscribe to system events synchronously.
209
+ * Returns an unsubscribe function.
210
+ */
211
+ readonly subscribe: (fn: SystemEventListener) => () => void;
212
+ /**
213
+ * List all persisted actor metadata.
214
+ * Returns empty array if adapter doesn't support registry.
215
+ *
216
+ * @example
217
+ * ```ts
218
+ * const actors = yield* system.listPersisted();
219
+ * for (const meta of actors) {
220
+ * console.log(`${meta.id}: ${meta.stateTag} (v${meta.version})`);
221
+ * }
222
+ * ```
223
+ */
224
+ readonly listPersisted: () => Effect.Effect<ReadonlyArray<ActorMetadata>, PersistenceError, PersistenceAdapterTag>;
225
+ /**
226
+ * Restore multiple actors by ID.
227
+ * Returns both successfully restored actors and failures.
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * const result = yield* system.restoreMany(["order-1", "order-2"], orderMachine);
232
+ * console.log(`Restored: ${result.restored.length}, Failed: ${result.failed.length}`);
233
+ * ```
234
+ */
235
+ readonly restoreMany: <S extends {
236
+ readonly _tag: string;
237
+ }, E extends {
238
+ readonly _tag: string;
239
+ }, R>(ids: ReadonlyArray<string>, machine: PersistentMachine<S, E, R>) => Effect.Effect<RestoreResult<S, E, R>, never, R | PersistenceAdapterTag>;
240
+ /**
241
+ * Restore all persisted actors for a machine type.
242
+ * Uses adapter registry if available, otherwise returns empty result.
243
+ *
244
+ * @example
245
+ * ```ts
246
+ * const result = yield* system.restoreAll(orderMachine, {
247
+ * filter: (meta) => meta.stateTag !== "Done"
248
+ * });
249
+ * console.log(`Restored ${result.restored.length} active orders`);
250
+ * ```
251
+ */
252
+ readonly restoreAll: <S extends {
253
+ readonly _tag: string;
254
+ }, E extends {
255
+ readonly _tag: string;
256
+ }, R>(machine: PersistentMachine<S, E, R>, options?: {
257
+ filter?: (meta: ActorMetadata) => boolean;
258
+ }) => Effect.Effect<RestoreResult<S, E, R>, PersistenceError, R | PersistenceAdapterTag>;
259
+ }
260
+ /**
261
+ * ActorSystem service tag
262
+ */
263
+ declare const ActorSystem: any;
264
+ /** Listener set for sync subscriptions */
265
+ type Listeners<S> = Set<(state: S) => void>;
266
+ /**
267
+ * Notify all listeners of state change.
268
+ */
269
+ declare const notifyListeners: <S>(listeners: Listeners<S>, state: S) => void;
270
+ /**
271
+ * Build core ActorRef methods shared between regular and persistent actors.
272
+ */
273
+ declare const buildActorRefCore: <S extends {
274
+ readonly _tag: string;
275
+ }, E extends {
276
+ readonly _tag: string;
277
+ }, 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<E>, stoppedRef: Ref.Ref<boolean>, listeners: Listeners<S>, stop: Effect.Effect<void>, system: ActorSystem, childrenMap: ReadonlyMap<string, ActorRef<AnyState, unknown>>) => ActorRef<S, E>;
278
+ /**
279
+ * Create and start an actor for a machine
280
+ */
281
+ declare const createActor: <S extends {
282
+ readonly _tag: string;
283
+ }, E extends {
284
+ readonly _tag: string;
285
+ }, R, GD extends GuardsDef, EFD extends EffectsDef>(id: string, machine: Machine<S, E, R, Record<string, never>, Record<string, never>, GD, EFD>) => Effect.Effect<ActorRef<S, E>, unknown, unknown>;
286
+ /**
287
+ * Default ActorSystem layer
288
+ */
289
+ declare const Default: any;
290
+ //#endregion
291
+ export { ActorRef, ActorSystem, Default, Listeners, type ProcessEventError, type ProcessEventHooks, type ProcessEventResult, SystemEvent, SystemEventListener, buildActorRefCore, createActor, notifyListeners, processEventCore, resolveTransition, runSpawnEffects };