effect-machine 0.9.0 → 0.11.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 (73) hide show
  1. package/README.md +118 -55
  2. package/dist/actor.d.ts +77 -179
  3. package/dist/actor.js +161 -113
  4. package/dist/cluster/entity-machine.js +5 -3
  5. package/dist/errors.d.ts +12 -1
  6. package/dist/errors.js +8 -1
  7. package/dist/index.d.ts +4 -8
  8. package/dist/index.js +2 -7
  9. package/dist/internal/transition.d.ts +27 -3
  10. package/dist/internal/transition.js +38 -9
  11. package/dist/internal/utils.d.ts +7 -2
  12. package/dist/internal/utils.js +1 -5
  13. package/dist/machine.d.ts +94 -35
  14. package/dist/machine.js +128 -13
  15. package/dist/testing.js +57 -3
  16. package/package.json +10 -9
  17. package/v3/dist/actor.d.ts +210 -0
  18. package/{dist-v3 → v3/dist}/actor.js +198 -117
  19. package/{dist-v3 → v3/dist}/cluster/entity-machine.d.ts +1 -1
  20. package/{dist-v3 → v3/dist}/cluster/entity-machine.js +8 -6
  21. package/{dist-v3 → v3/dist}/cluster/to-entity.d.ts +1 -1
  22. package/{dist-v3 → v3/dist}/cluster/to-entity.js +1 -1
  23. package/v3/dist/errors.d.ts +76 -0
  24. package/{dist-v3 → v3/dist}/errors.js +9 -2
  25. package/v3/dist/index.d.ts +9 -0
  26. package/v3/dist/index.js +8 -0
  27. package/{dist-v3 → v3/dist}/inspection.d.ts +53 -8
  28. package/v3/dist/inspection.js +156 -0
  29. package/{dist-v3 → v3/dist}/internal/brands.d.ts +1 -1
  30. package/{dist-v3 → v3/dist}/internal/inspection.d.ts +1 -1
  31. package/v3/dist/internal/inspection.js +20 -0
  32. package/{dist-v3 → v3/dist}/internal/transition.d.ts +35 -11
  33. package/{dist-v3 → v3/dist}/internal/transition.js +47 -15
  34. package/{dist-v3 → v3/dist}/internal/utils.d.ts +9 -4
  35. package/{dist-v3 → v3/dist}/internal/utils.js +2 -6
  36. package/{dist-v3 → v3/dist}/machine.d.ts +113 -40
  37. package/{dist-v3 → v3/dist}/machine.js +191 -15
  38. package/{dist-v3 → v3/dist}/schema.d.ts +1 -1
  39. package/{dist-v3 → v3/dist}/schema.js +5 -2
  40. package/{dist-v3 → v3/dist}/slot.d.ts +4 -3
  41. package/{dist-v3 → v3/dist}/slot.js +1 -1
  42. package/{dist-v3 → v3/dist}/testing.d.ts +14 -8
  43. package/{dist-v3 → v3/dist}/testing.js +60 -6
  44. package/dist/persistence/adapter.d.ts +0 -135
  45. package/dist/persistence/adapter.js +0 -25
  46. package/dist/persistence/adapters/in-memory.d.ts +0 -32
  47. package/dist/persistence/adapters/in-memory.js +0 -174
  48. package/dist/persistence/index.d.ts +0 -5
  49. package/dist/persistence/index.js +0 -5
  50. package/dist/persistence/persistent-actor.d.ts +0 -50
  51. package/dist/persistence/persistent-actor.js +0 -368
  52. package/dist/persistence/persistent-machine.d.ts +0 -105
  53. package/dist/persistence/persistent-machine.js +0 -22
  54. package/dist-v3/actor.d.ts +0 -291
  55. package/dist-v3/errors.d.ts +0 -27
  56. package/dist-v3/index.d.ts +0 -12
  57. package/dist-v3/index.js +0 -13
  58. package/dist-v3/inspection.js +0 -48
  59. package/dist-v3/internal/inspection.js +0 -13
  60. package/dist-v3/persistence/adapter.d.ts +0 -125
  61. package/dist-v3/persistence/adapter.js +0 -25
  62. package/dist-v3/persistence/adapters/in-memory.d.ts +0 -32
  63. package/dist-v3/persistence/adapters/in-memory.js +0 -174
  64. package/dist-v3/persistence/index.d.ts +0 -5
  65. package/dist-v3/persistence/index.js +0 -5
  66. package/dist-v3/persistence/persistent-actor.d.ts +0 -49
  67. package/dist-v3/persistence/persistent-actor.js +0 -365
  68. package/dist-v3/persistence/persistent-machine.d.ts +0 -105
  69. package/dist-v3/persistence/persistent-machine.js +0 -22
  70. /package/{dist-v3 → v3/dist}/_virtual/_rolldown/runtime.js +0 -0
  71. /package/{dist-v3 → v3/dist}/cluster/index.d.ts +0 -0
  72. /package/{dist-v3 → v3/dist}/cluster/index.js +0 -0
  73. /package/{dist-v3 → v3/dist}/internal/brands.js +0 -0
@@ -1,368 +0,0 @@
1
- import { Inspector } from "../inspection.js";
2
- import { INTERNAL_INIT_EVENT, stubSystem } from "../internal/utils.js";
3
- import { emitWithTimestamp } from "../internal/inspection.js";
4
- import { processEventCore, resolveTransition, runSpawnEffects, runTransitionHandler } from "../internal/transition.js";
5
- import { PersistenceAdapterTag } from "./adapter.js";
6
- import { ActorSystem, buildActorRefCore, notifyListeners } from "../actor.js";
7
- import { Cause, Clock, Deferred, Effect, Exit, Fiber, Option, Queue, Ref, Schedule, Scope, SubscriptionRef } from "effect";
8
- //#region src/persistence/persistent-actor.ts
9
- /** Get current time in milliseconds using Effect Clock */
10
- const now = Clock.currentTimeMillis;
11
- /**
12
- * Replay persisted events to compute state.
13
- * Supports async handlers - used for initial restore.
14
- * @internal
15
- */
16
- const replayEvents = Effect.fn("effect-machine.persistentActor.replayEvents")(function* (machine, startState, events, self, stopVersion) {
17
- let state = startState;
18
- let version = 0;
19
- for (const persistedEvent of events) {
20
- if (stopVersion !== void 0 && persistedEvent.version > stopVersion) break;
21
- const transition = resolveTransition(machine, state, persistedEvent.event);
22
- if (transition !== void 0) state = yield* runTransitionHandler(machine, transition, state, persistedEvent.event, self, stubSystem, "restore");
23
- version = persistedEvent.version;
24
- }
25
- return {
26
- state,
27
- version
28
- };
29
- });
30
- /**
31
- * Build PersistentActorRef with all methods
32
- */
33
- const buildPersistentActorRef = (id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, listeners, stop, adapter, system, childrenMap) => {
34
- const { machine, persistence } = persistentMachine;
35
- const typedMachine = machine;
36
- const persist = Effect.gen(function* () {
37
- const snapshot = {
38
- state: yield* SubscriptionRef.get(stateRef),
39
- version: yield* Ref.get(versionRef),
40
- timestamp: yield* now
41
- };
42
- yield* adapter.saveSnapshot(id, snapshot, persistence.stateSchema);
43
- }).pipe(Effect.withSpan("effect-machine.persistentActor.persist"));
44
- const version = Ref.get(versionRef).pipe(Effect.withSpan("effect-machine.persistentActor.version"));
45
- const replayTo = Effect.fn("effect-machine.persistentActor.replayTo")(function* (targetVersion) {
46
- if (targetVersion <= (yield* Ref.get(versionRef))) {
47
- const dummySelf = {
48
- send: Effect.fn("effect-machine.persistentActor.replay.send")((_event) => Effect.void),
49
- spawn: () => Effect.die("spawn not supported in replay")
50
- };
51
- const maybeSnapshot = yield* adapter.loadSnapshot(id, persistence.stateSchema);
52
- if (Option.isSome(maybeSnapshot)) {
53
- const snapshot = maybeSnapshot.value;
54
- if (snapshot.version <= targetVersion) {
55
- const events = yield* adapter.loadEvents(id, persistence.eventSchema, snapshot.version);
56
- const result = yield* replayEvents(typedMachine, snapshot.state, events, dummySelf, targetVersion);
57
- yield* SubscriptionRef.set(stateRef, result.state);
58
- yield* Ref.set(versionRef, result.version);
59
- notifyListeners(listeners, result.state);
60
- }
61
- } else {
62
- const events = yield* adapter.loadEvents(id, persistence.eventSchema);
63
- if (events.length > 0) {
64
- const result = yield* replayEvents(typedMachine, typedMachine.initial, events, dummySelf, targetVersion);
65
- yield* SubscriptionRef.set(stateRef, result.state);
66
- yield* Ref.set(versionRef, result.version);
67
- notifyListeners(listeners, result.state);
68
- }
69
- }
70
- }
71
- });
72
- return {
73
- ...buildActorRefCore(id, typedMachine, stateRef, eventQueue, stoppedRef, listeners, stop, system, childrenMap),
74
- persist,
75
- version,
76
- replayTo
77
- };
78
- };
79
- /**
80
- * Create a persistent actor from a PersistentMachine.
81
- * Restores from existing snapshot if available, otherwise starts fresh.
82
- */
83
- const createPersistentActor = Effect.fn("effect-machine.persistentActor.spawn")(function* (id, persistentMachine, initialSnapshot, initialEvents) {
84
- yield* Effect.annotateCurrentSpan("effect_machine.actor.id", id);
85
- const adapter = yield* PersistenceAdapterTag;
86
- const { machine, persistence } = persistentMachine;
87
- const typedMachine = machine;
88
- const existingSystem = yield* Effect.serviceOption(ActorSystem);
89
- if (Option.isNone(existingSystem)) return yield* Effect.die("PersistentActor requires ActorSystem in context");
90
- const system = existingSystem.value;
91
- const inspector = Option.getOrUndefined(yield* Effect.serviceOption(Inspector));
92
- const eventQueue = yield* Queue.unbounded();
93
- const stoppedRef = yield* Ref.make(false);
94
- const childrenMap = /* @__PURE__ */ new Map();
95
- const self = {
96
- send: Effect.fn("effect-machine.persistentActor.self.send")(function* (event) {
97
- if (yield* Ref.get(stoppedRef)) return;
98
- yield* Queue.offer(eventQueue, { event });
99
- }),
100
- spawn: (childId, childMachine) => Effect.gen(function* () {
101
- const child = yield* system.spawn(childId, childMachine).pipe(Effect.provideService(ActorSystem, system));
102
- childrenMap.set(childId, child);
103
- const maybeScope = yield* Effect.serviceOption(Scope.Scope);
104
- if (Option.isSome(maybeScope)) yield* Scope.addFinalizer(maybeScope.value, Effect.sync(() => {
105
- childrenMap.delete(childId);
106
- }));
107
- return child;
108
- })
109
- };
110
- let resolvedInitial;
111
- let initialVersion;
112
- if (Option.isSome(initialSnapshot)) {
113
- const result = yield* replayEvents(typedMachine, initialSnapshot.value.state, initialEvents, self);
114
- resolvedInitial = result.state;
115
- initialVersion = initialEvents.length > 0 ? result.version : initialSnapshot.value.version;
116
- } else if (initialEvents.length > 0) {
117
- const result = yield* replayEvents(typedMachine, typedMachine.initial, initialEvents, self);
118
- resolvedInitial = result.state;
119
- initialVersion = result.version;
120
- } else {
121
- resolvedInitial = typedMachine.initial;
122
- initialVersion = 0;
123
- }
124
- yield* Effect.annotateCurrentSpan("effect_machine.actor.initial_state", resolvedInitial._tag);
125
- const stateRef = yield* SubscriptionRef.make(resolvedInitial);
126
- const versionRef = yield* Ref.make(initialVersion);
127
- const listeners = /* @__PURE__ */ new Set();
128
- let createdAt;
129
- if (Option.isSome(initialSnapshot)) {
130
- const existingMeta = adapter.loadMetadata !== void 0 ? yield* adapter.loadMetadata(id) : Option.none();
131
- createdAt = Option.isSome(existingMeta) ? existingMeta.value.createdAt : initialSnapshot.value.timestamp;
132
- } else createdAt = yield* now;
133
- yield* emitWithTimestamp(inspector, (timestamp) => ({
134
- type: "@machine.spawn",
135
- actorId: id,
136
- initialState: resolvedInitial,
137
- timestamp
138
- }));
139
- const snapshotEnabledRef = yield* Ref.make(true);
140
- const persistenceQueue = yield* Queue.unbounded();
141
- const persistenceFiber = yield* Effect.forkDetach(persistenceWorker(persistenceQueue));
142
- yield* Queue.offer(persistenceQueue, saveMetadata(id, resolvedInitial, initialVersion, createdAt, persistence, adapter));
143
- const snapshotQueue = yield* Queue.unbounded();
144
- const snapshotFiber = yield* Effect.forkDetach(snapshotWorker(id, persistence, adapter, snapshotQueue, snapshotEnabledRef));
145
- const backgroundFibers = [];
146
- const initEvent = { _tag: INTERNAL_INIT_EVENT };
147
- const initCtx = {
148
- actorId: id,
149
- state: resolvedInitial,
150
- event: initEvent,
151
- self,
152
- system
153
- };
154
- const { effects: effectSlots } = typedMachine._slots;
155
- for (const bg of typedMachine.backgroundEffects) {
156
- const fiber = yield* Effect.forkDetach(bg.handler({
157
- actorId: id,
158
- state: resolvedInitial,
159
- event: initEvent,
160
- self,
161
- effects: effectSlots,
162
- system
163
- }).pipe(Effect.provideService(typedMachine.Context, initCtx)));
164
- backgroundFibers.push(fiber);
165
- }
166
- const stateScopeRef = { current: yield* Scope.make() };
167
- yield* runSpawnEffectsWithInspection(typedMachine, resolvedInitial, initEvent, self, stateScopeRef.current, id, inspector, system);
168
- if (typedMachine.finalStates.has(resolvedInitial._tag)) {
169
- yield* Scope.close(stateScopeRef.current, Exit.void);
170
- yield* Effect.all(backgroundFibers.map(Fiber.interrupt), { concurrency: "unbounded" });
171
- yield* Fiber.interrupt(snapshotFiber);
172
- yield* Fiber.interrupt(persistenceFiber);
173
- yield* Ref.set(stoppedRef, true);
174
- yield* emitWithTimestamp(inspector, (timestamp) => ({
175
- type: "@machine.stop",
176
- actorId: id,
177
- finalState: resolvedInitial,
178
- timestamp
179
- }));
180
- return buildPersistentActorRef(id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, listeners, Ref.set(stoppedRef, true).pipe(Effect.withSpan("effect-machine.persistentActor.stop"), Effect.asVoid), adapter, system, childrenMap);
181
- }
182
- const loopFiber = yield* Effect.forkDetach(persistentEventLoop(id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, self, listeners, adapter, createdAt, stateScopeRef, backgroundFibers, snapshotQueue, snapshotEnabledRef, persistenceQueue, snapshotFiber, persistenceFiber, inspector, system));
183
- return buildPersistentActorRef(id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, listeners, Effect.gen(function* () {
184
- const finalState = yield* SubscriptionRef.get(stateRef);
185
- yield* emitWithTimestamp(inspector, (timestamp) => ({
186
- type: "@machine.stop",
187
- actorId: id,
188
- finalState,
189
- timestamp
190
- }));
191
- yield* Ref.set(stoppedRef, true);
192
- yield* Fiber.interrupt(loopFiber);
193
- yield* Scope.close(stateScopeRef.current, Exit.void);
194
- yield* Effect.all(backgroundFibers.map(Fiber.interrupt), { concurrency: "unbounded" });
195
- yield* Fiber.interrupt(snapshotFiber);
196
- yield* Fiber.interrupt(persistenceFiber);
197
- }).pipe(Effect.withSpan("effect-machine.persistentActor.stop"), Effect.asVoid), adapter, system, childrenMap);
198
- });
199
- /**
200
- * Main event loop for persistent actor
201
- */
202
- const persistentEventLoop = Effect.fn("effect-machine.persistentActor.eventLoop")(function* (id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, self, listeners, adapter, createdAt, stateScopeRef, backgroundFibers, snapshotQueue, snapshotEnabledRef, persistenceQueue, snapshotFiber, persistenceFiber, inspector, system) {
203
- const { machine, persistence } = persistentMachine;
204
- const typedMachine = machine;
205
- const hooks = inspector === void 0 ? void 0 : {
206
- onSpawnEffect: (state) => emitWithTimestamp(inspector, (timestamp) => ({
207
- type: "@machine.effect",
208
- actorId: id,
209
- effectType: "spawn",
210
- state,
211
- timestamp
212
- })),
213
- onTransition: (from, to, ev) => emitWithTimestamp(inspector, (timestamp) => ({
214
- type: "@machine.transition",
215
- actorId: id,
216
- fromState: from,
217
- toState: to,
218
- event: ev,
219
- timestamp
220
- })),
221
- onError: (info) => emitWithTimestamp(inspector, (timestamp) => ({
222
- type: "@machine.error",
223
- actorId: id,
224
- phase: info.phase,
225
- state: info.state,
226
- event: info.event,
227
- error: Cause.pretty(info.cause),
228
- timestamp
229
- }))
230
- };
231
- while (true) {
232
- const { event, reply } = yield* Queue.take(eventQueue);
233
- const currentState = yield* SubscriptionRef.get(stateRef);
234
- const currentVersion = yield* Ref.get(versionRef);
235
- yield* emitWithTimestamp(inspector, (timestamp) => ({
236
- type: "@machine.event",
237
- actorId: id,
238
- state: currentState,
239
- event,
240
- timestamp
241
- }));
242
- const result = yield* processEventCore(typedMachine, currentState, event, self, stateScopeRef, system, id, hooks);
243
- if (reply !== void 0) yield* Deferred.succeed(reply, result);
244
- if (!result.transitioned) continue;
245
- const newVersion = currentVersion + 1;
246
- yield* Ref.set(versionRef, newVersion);
247
- yield* SubscriptionRef.set(stateRef, result.newState);
248
- notifyListeners(listeners, result.newState);
249
- if (persistence.journalEvents) {
250
- const persistedEvent = {
251
- event,
252
- version: newVersion,
253
- timestamp: yield* now
254
- };
255
- const journalTask = adapter.appendEvent(id, persistedEvent, persistence.eventSchema).pipe(Effect.catchEager((e) => Effect.logWarning(`Failed to journal event for actor ${id}`, e)), Effect.asVoid);
256
- yield* Queue.offer(persistenceQueue, journalTask);
257
- }
258
- yield* Queue.offer(persistenceQueue, saveMetadata(id, result.newState, newVersion, createdAt, persistence, adapter));
259
- if (yield* Ref.get(snapshotEnabledRef)) yield* Queue.offer(snapshotQueue, {
260
- state: result.newState,
261
- version: newVersion
262
- });
263
- if (result.lifecycleRan && result.isFinal) {
264
- yield* emitWithTimestamp(inspector, (timestamp) => ({
265
- type: "@machine.stop",
266
- actorId: id,
267
- finalState: result.newState,
268
- timestamp
269
- }));
270
- yield* Ref.set(stoppedRef, true);
271
- yield* Scope.close(stateScopeRef.current, Exit.void);
272
- yield* Effect.all(backgroundFibers.map(Fiber.interrupt), { concurrency: "unbounded" });
273
- yield* Fiber.interrupt(snapshotFiber);
274
- yield* Fiber.interrupt(persistenceFiber);
275
- return;
276
- }
277
- }
278
- });
279
- /**
280
- * Run spawn effects with inspection and tracing.
281
- * @internal
282
- */
283
- const runSpawnEffectsWithInspection = Effect.fn("effect-machine.persistentActor.spawnEffects")(function* (machine, state, event, self, stateScope, actorId, inspector, system) {
284
- yield* emitWithTimestamp(inspector, (timestamp) => ({
285
- type: "@machine.effect",
286
- actorId,
287
- effectType: "spawn",
288
- state,
289
- timestamp
290
- }));
291
- yield* runSpawnEffects(machine, state, event, self, stateScope, system, actorId, inspector === void 0 ? void 0 : (info) => emitWithTimestamp(inspector, (timestamp) => ({
292
- type: "@machine.error",
293
- actorId,
294
- phase: info.phase,
295
- state: info.state,
296
- event: info.event,
297
- error: Cause.pretty(info.cause),
298
- timestamp
299
- })));
300
- });
301
- /**
302
- * Persistence worker (journaling + metadata).
303
- */
304
- const persistenceWorker = Effect.fn("effect-machine.persistentActor.persistenceWorker")(function* (queue) {
305
- while (true) yield* yield* Queue.take(queue);
306
- });
307
- /**
308
- * Snapshot scheduler worker (runs in background).
309
- */
310
- const snapshotWorker = Effect.fn("effect-machine.persistentActor.snapshotWorker")(function* (id, persistence, adapter, queue, enabledRef) {
311
- const step = yield* Schedule.toStep(persistence.snapshotSchedule);
312
- while (true) {
313
- const { state, version } = yield* Queue.take(queue);
314
- if (!(yield* Ref.get(enabledRef))) continue;
315
- if (!(yield* step(yield* Clock.currentTimeMillis, state).pipe(Effect.match({
316
- onFailure: () => false,
317
- onSuccess: () => true
318
- })))) {
319
- yield* Ref.set(enabledRef, false);
320
- continue;
321
- }
322
- yield* saveSnapshot(id, state, version, persistence, adapter);
323
- }
324
- });
325
- /**
326
- * Save a snapshot after state transition.
327
- * Called by snapshot scheduler.
328
- */
329
- const saveSnapshot = Effect.fn("effect-machine.persistentActor.saveSnapshot")(function* (id, state, version, persistence, adapter) {
330
- const snapshot = {
331
- state,
332
- version,
333
- timestamp: yield* now
334
- };
335
- yield* adapter.saveSnapshot(id, snapshot, persistence.stateSchema).pipe(Effect.catchEager((e) => Effect.logWarning(`Failed to save snapshot for actor ${id}`, e)));
336
- });
337
- /**
338
- * Save or update actor metadata if adapter supports registry.
339
- * Called on spawn and state transitions.
340
- */
341
- const saveMetadata = Effect.fn("effect-machine.persistentActor.saveMetadata")(function* (id, state, version, createdAt, persistence, adapter) {
342
- const save = adapter.saveMetadata;
343
- if (save === void 0) return;
344
- const lastActivityAt = yield* now;
345
- yield* save({
346
- id,
347
- machineType: persistence.machineType ?? "unknown",
348
- createdAt,
349
- lastActivityAt,
350
- version,
351
- stateTag: state._tag
352
- }).pipe(Effect.catchEager((e) => Effect.logWarning(`Failed to save metadata for actor ${id}`, e)));
353
- });
354
- /**
355
- * Restore an actor from persistence.
356
- * Returns None if no persisted state exists.
357
- */
358
- const restorePersistentActor = Effect.fn("effect-machine.persistentActor.restore")(function* (id, persistentMachine) {
359
- const adapter = yield* PersistenceAdapterTag;
360
- const { persistence } = persistentMachine;
361
- const maybeSnapshot = yield* adapter.loadSnapshot(id, persistence.stateSchema);
362
- const events = yield* adapter.loadEvents(id, persistence.eventSchema, Option.isSome(maybeSnapshot) ? maybeSnapshot.value.version : void 0);
363
- if (Option.isNone(maybeSnapshot) && events.length === 0) return Option.none();
364
- const actor = yield* createPersistentActor(id, persistentMachine, maybeSnapshot, events);
365
- return Option.some(actor);
366
- });
367
- //#endregion
368
- export { createPersistentActor, restorePersistentActor };
@@ -1,105 +0,0 @@
1
- import { EventBrand, StateBrand } from "../internal/brands.js";
2
- import { Machine } from "../machine.js";
3
- import { Schedule, Schema } from "effect";
4
-
5
- //#region src/persistence/persistent-machine.d.ts
6
- type BrandedState = {
7
- readonly _tag: string;
8
- } & StateBrand;
9
- type BrandedEvent = {
10
- readonly _tag: string;
11
- } & EventBrand;
12
- /**
13
- * Configuration for persistence behavior (after resolution).
14
- * Schemas are required at runtime - the persist function ensures this.
15
- *
16
- * Note: Schema types S and E should match the structural shape of the machine's
17
- * state and event types (without brands). The schemas don't know about brands.
18
- */
19
- interface PersistenceConfig<S, E> {
20
- /**
21
- * Schedule controlling when snapshots are taken.
22
- * Input is the new state after each transition.
23
- *
24
- * Examples:
25
- * - Schedule.forever — snapshot every transition
26
- * - Schedule.spaced("5 seconds") — debounced snapshots
27
- * - Schedule.recurs(100) — every N transitions
28
- */
29
- readonly snapshotSchedule: Schedule.Schedule<unknown, S>;
30
- /**
31
- * Whether to journal events for replay capability.
32
- * When true, all events are appended to the event log.
33
- */
34
- readonly journalEvents: boolean;
35
- /**
36
- * Schema for serializing/deserializing state.
37
- * Always present at runtime (resolved from config or machine).
38
- */
39
- readonly stateSchema: Schema.Codec<S, unknown, never, never>;
40
- /**
41
- * Schema for serializing/deserializing events.
42
- * Always present at runtime (resolved from config or machine).
43
- */
44
- readonly eventSchema: Schema.Codec<E, unknown, never, never>;
45
- /**
46
- * User-provided identifier for the machine type.
47
- * Used for filtering actors in restoreAll.
48
- * Optional — defaults to "unknown" if not provided.
49
- */
50
- readonly machineType?: string;
51
- }
52
- /**
53
- * Machine with persistence configuration attached.
54
- * Spawn auto-detects this and returns PersistentActorRef.
55
- */
56
- interface PersistentMachine<S extends {
57
- readonly _tag: string;
58
- }, E extends {
59
- readonly _tag: string;
60
- }, R = never> {
61
- readonly _tag: "PersistentMachine";
62
- readonly machine: Machine<S, E, R>;
63
- readonly persistence: PersistenceConfig<S, E>;
64
- }
65
- /**
66
- * Type guard to check if a value is a PersistentMachine
67
- */
68
- declare const isPersistentMachine: (value: unknown) => value is PersistentMachine<{
69
- readonly _tag: string;
70
- }, {
71
- readonly _tag: string;
72
- }, unknown>;
73
- /**
74
- * Attach persistence configuration to a machine.
75
- *
76
- * Schemas are read from the machine - must use `Machine.make({ state, event, initial })`.
77
- *
78
- * @example
79
- * ```ts
80
- * const orderMachine = Machine.make({
81
- * state: OrderState,
82
- * event: OrderEvent,
83
- * initial: OrderState.Idle(),
84
- * }).pipe(
85
- * Machine.on(OrderState.Idle, OrderEvent.Submit, ({ event }) =>
86
- * OrderState.Pending({ orderId: event.orderId })
87
- * ),
88
- * Machine.final(OrderState.Paid),
89
- * Machine.persist({
90
- * snapshotSchedule: Schedule.forever,
91
- * journalEvents: true,
92
- * }),
93
- * );
94
- * ```
95
- */
96
- interface WithPersistenceConfig {
97
- readonly snapshotSchedule: Schedule.Schedule<unknown, {
98
- readonly _tag: string;
99
- }>;
100
- readonly journalEvents: boolean;
101
- readonly machineType?: string;
102
- }
103
- declare const persist: (config: WithPersistenceConfig) => <S extends BrandedState, E extends BrandedEvent, R>(machine: Machine<S, E, R>) => PersistentMachine<S, E, R>;
104
- //#endregion
105
- export { PersistenceConfig, PersistentMachine, WithPersistenceConfig, isPersistentMachine, persist };
@@ -1,22 +0,0 @@
1
- import { MissingSchemaError } from "../errors.js";
2
- //#region src/persistence/persistent-machine.ts
3
- /**
4
- * Type guard to check if a value is a PersistentMachine
5
- */
6
- const isPersistentMachine = (value) => typeof value === "object" && value !== null && "_tag" in value && value._tag === "PersistentMachine";
7
- const persist = (config) => (machine) => {
8
- const stateSchema = machine.stateSchema;
9
- const eventSchema = machine.eventSchema;
10
- if (stateSchema === void 0 || eventSchema === void 0) throw new MissingSchemaError({ operation: "persist" });
11
- return {
12
- _tag: "PersistentMachine",
13
- machine,
14
- persistence: {
15
- ...config,
16
- stateSchema,
17
- eventSchema
18
- }
19
- };
20
- };
21
- //#endregion
22
- export { isPersistentMachine, persist };