effect-machine 0.3.0 → 0.3.2

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 (61) hide show
  1. package/dist/_virtual/_rolldown/runtime.js +18 -0
  2. package/dist/actor.d.ts +251 -0
  3. package/dist/actor.js +385 -0
  4. package/dist/cluster/entity-machine.d.ts +90 -0
  5. package/dist/cluster/entity-machine.js +74 -0
  6. package/dist/cluster/index.d.ts +3 -0
  7. package/dist/cluster/index.js +4 -0
  8. package/dist/cluster/to-entity.d.ts +64 -0
  9. package/dist/cluster/to-entity.js +53 -0
  10. package/dist/errors.d.ts +61 -0
  11. package/dist/errors.js +38 -0
  12. package/dist/index.d.ts +13 -0
  13. package/dist/index.js +14 -0
  14. package/dist/inspection.d.ts +125 -0
  15. package/dist/inspection.js +50 -0
  16. package/dist/internal/brands.d.ts +40 -0
  17. package/dist/internal/brands.js +0 -0
  18. package/dist/internal/inspection.d.ts +11 -0
  19. package/dist/internal/inspection.js +15 -0
  20. package/dist/internal/transition.d.ts +159 -0
  21. package/dist/internal/transition.js +235 -0
  22. package/dist/internal/utils.d.ts +52 -0
  23. package/dist/internal/utils.js +31 -0
  24. package/dist/machine.d.ts +271 -0
  25. package/dist/machine.js +317 -0
  26. package/{src/persistence/adapter.ts → dist/persistence/adapter.d.ts} +40 -72
  27. package/dist/persistence/adapter.js +27 -0
  28. package/dist/persistence/adapters/in-memory.d.ts +32 -0
  29. package/dist/persistence/adapters/in-memory.js +176 -0
  30. package/dist/persistence/index.d.ts +5 -0
  31. package/dist/persistence/index.js +6 -0
  32. package/dist/persistence/persistent-actor.d.ts +50 -0
  33. package/dist/persistence/persistent-actor.js +348 -0
  34. package/{src/persistence/persistent-machine.ts → dist/persistence/persistent-machine.d.ts} +28 -54
  35. package/dist/persistence/persistent-machine.js +24 -0
  36. package/dist/schema.d.ts +141 -0
  37. package/dist/schema.js +165 -0
  38. package/dist/slot.d.ts +128 -0
  39. package/dist/slot.js +99 -0
  40. package/dist/testing.d.ts +142 -0
  41. package/dist/testing.js +131 -0
  42. package/package.json +18 -7
  43. package/src/actor.ts +0 -1050
  44. package/src/cluster/entity-machine.ts +0 -201
  45. package/src/cluster/index.ts +0 -43
  46. package/src/cluster/to-entity.ts +0 -99
  47. package/src/errors.ts +0 -64
  48. package/src/index.ts +0 -105
  49. package/src/inspection.ts +0 -178
  50. package/src/internal/brands.ts +0 -51
  51. package/src/internal/inspection.ts +0 -18
  52. package/src/internal/transition.ts +0 -489
  53. package/src/internal/utils.ts +0 -80
  54. package/src/machine.ts +0 -836
  55. package/src/persistence/adapters/in-memory.ts +0 -294
  56. package/src/persistence/index.ts +0 -24
  57. package/src/persistence/persistent-actor.ts +0 -791
  58. package/src/schema.ts +0 -362
  59. package/src/slot.ts +0 -281
  60. package/src/testing.ts +0 -284
  61. package/tsconfig.json +0 -65
@@ -0,0 +1,348 @@
1
+ import { Inspector } from "../inspection.js";
2
+ import { INTERNAL_INIT_EVENT } from "../internal/utils.js";
3
+ import { processEventCore, resolveTransition, runSpawnEffects, runTransitionHandler } from "../internal/transition.js";
4
+ import { emitWithTimestamp } from "../internal/inspection.js";
5
+ import { PersistenceAdapterTag } from "./adapter.js";
6
+ import { buildActorRefCore, notifyListeners } from "../actor.js";
7
+ import { Cause, Clock, Effect, Exit, Fiber, Option, Queue, Ref, Schedule, Scope, SubscriptionRef } from "effect";
8
+
9
+ //#region src/persistence/persistent-actor.ts
10
+ /** Get current time in milliseconds using Effect Clock */
11
+ const now = Clock.currentTimeMillis;
12
+ /**
13
+ * Replay persisted events to compute state.
14
+ * Supports async handlers - used for initial restore.
15
+ * @internal
16
+ */
17
+ const replayEvents = Effect.fn("effect-machine.persistentActor.replayEvents")(function* (machine, startState, events, self, stopVersion) {
18
+ let state = startState;
19
+ let version = 0;
20
+ for (const persistedEvent of events) {
21
+ if (stopVersion !== void 0 && persistedEvent.version > stopVersion) break;
22
+ const transition = resolveTransition(machine, state, persistedEvent.event);
23
+ if (transition !== void 0) state = yield* runTransitionHandler(machine, transition, state, persistedEvent.event, self);
24
+ version = persistedEvent.version;
25
+ }
26
+ return {
27
+ state,
28
+ version
29
+ };
30
+ });
31
+ /**
32
+ * Build PersistentActorRef with all methods
33
+ */
34
+ const buildPersistentActorRef = (id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, listeners, stop, adapter) => {
35
+ const { machine, persistence } = persistentMachine;
36
+ const typedMachine = machine;
37
+ const persist = Effect.gen(function* () {
38
+ const snapshot = {
39
+ state: yield* SubscriptionRef.get(stateRef),
40
+ version: yield* Ref.get(versionRef),
41
+ timestamp: yield* now
42
+ };
43
+ yield* adapter.saveSnapshot(id, snapshot, persistence.stateSchema);
44
+ }).pipe(Effect.withSpan("effect-machine.persistentActor.persist"));
45
+ const version = Ref.get(versionRef).pipe(Effect.withSpan("effect-machine.persistentActor.version"));
46
+ const replayTo = Effect.fn("effect-machine.persistentActor.replayTo")(function* (targetVersion) {
47
+ if (targetVersion <= (yield* Ref.get(versionRef))) {
48
+ const maybeSnapshot = yield* adapter.loadSnapshot(id, persistence.stateSchema);
49
+ if (Option.isSome(maybeSnapshot)) {
50
+ const snapshot = maybeSnapshot.value;
51
+ if (snapshot.version <= targetVersion) {
52
+ const events = yield* adapter.loadEvents(id, persistence.eventSchema, snapshot.version);
53
+ const dummySelf = { send: Effect.fn("effect-machine.persistentActor.replay.send")((_event) => Effect.void) };
54
+ const result = yield* replayEvents(typedMachine, snapshot.state, events, dummySelf, targetVersion);
55
+ yield* SubscriptionRef.set(stateRef, result.state);
56
+ yield* Ref.set(versionRef, result.version);
57
+ notifyListeners(listeners, result.state);
58
+ }
59
+ } else {
60
+ const events = yield* adapter.loadEvents(id, persistence.eventSchema);
61
+ if (events.length > 0) {
62
+ const dummySelf = { send: Effect.fn("effect-machine.persistentActor.replay.send")((_event) => Effect.void) };
63
+ const result = yield* replayEvents(typedMachine, typedMachine.initial, events, dummySelf, targetVersion);
64
+ yield* SubscriptionRef.set(stateRef, result.state);
65
+ yield* Ref.set(versionRef, result.version);
66
+ notifyListeners(listeners, result.state);
67
+ }
68
+ }
69
+ }
70
+ });
71
+ return {
72
+ ...buildActorRefCore(id, typedMachine, stateRef, eventQueue, stoppedRef, listeners, stop),
73
+ persist,
74
+ version,
75
+ replayTo
76
+ };
77
+ };
78
+ /**
79
+ * Create a persistent actor from a PersistentMachine.
80
+ * Restores from existing snapshot if available, otherwise starts fresh.
81
+ */
82
+ const createPersistentActor = Effect.fn("effect-machine.persistentActor.spawn")(function* (id, persistentMachine, initialSnapshot, initialEvents) {
83
+ yield* Effect.annotateCurrentSpan("effect_machine.actor.id", id);
84
+ const adapter = yield* PersistenceAdapterTag;
85
+ const { machine, persistence } = persistentMachine;
86
+ const typedMachine = machine;
87
+ const inspector = Option.getOrUndefined(yield* Effect.serviceOption(Inspector));
88
+ const eventQueue = yield* Queue.unbounded();
89
+ const stoppedRef = yield* Ref.make(false);
90
+ const self = { send: Effect.fn("effect-machine.persistentActor.self.send")(function* (event) {
91
+ if (yield* Ref.get(stoppedRef)) return;
92
+ yield* Queue.offer(eventQueue, event);
93
+ }) };
94
+ let resolvedInitial;
95
+ let initialVersion;
96
+ if (Option.isSome(initialSnapshot)) {
97
+ const result = yield* replayEvents(typedMachine, initialSnapshot.value.state, initialEvents, self);
98
+ resolvedInitial = result.state;
99
+ initialVersion = initialEvents.length > 0 ? result.version : initialSnapshot.value.version;
100
+ } else if (initialEvents.length > 0) {
101
+ const result = yield* replayEvents(typedMachine, typedMachine.initial, initialEvents, self);
102
+ resolvedInitial = result.state;
103
+ initialVersion = result.version;
104
+ } else {
105
+ resolvedInitial = typedMachine.initial;
106
+ initialVersion = 0;
107
+ }
108
+ yield* Effect.annotateCurrentSpan("effect_machine.actor.initial_state", resolvedInitial._tag);
109
+ const stateRef = yield* SubscriptionRef.make(resolvedInitial);
110
+ const versionRef = yield* Ref.make(initialVersion);
111
+ const listeners = /* @__PURE__ */ new Set();
112
+ let createdAt;
113
+ if (Option.isSome(initialSnapshot)) {
114
+ const existingMeta = adapter.loadMetadata !== void 0 ? yield* adapter.loadMetadata(id) : Option.none();
115
+ createdAt = Option.isSome(existingMeta) ? existingMeta.value.createdAt : initialSnapshot.value.timestamp;
116
+ } else createdAt = yield* now;
117
+ yield* emitWithTimestamp(inspector, (timestamp) => ({
118
+ type: "@machine.spawn",
119
+ actorId: id,
120
+ initialState: resolvedInitial,
121
+ timestamp
122
+ }));
123
+ const snapshotEnabledRef = yield* Ref.make(true);
124
+ const persistenceQueue = yield* Queue.unbounded();
125
+ const persistenceFiber = yield* Effect.forkDaemon(persistenceWorker(persistenceQueue));
126
+ yield* Queue.offer(persistenceQueue, saveMetadata(id, resolvedInitial, initialVersion, createdAt, persistence, adapter));
127
+ const snapshotQueue = yield* Queue.unbounded();
128
+ const snapshotFiber = yield* Effect.forkDaemon(snapshotWorker(id, persistence, adapter, snapshotQueue, snapshotEnabledRef));
129
+ const backgroundFibers = [];
130
+ const initEvent = { _tag: INTERNAL_INIT_EVENT };
131
+ const initCtx = {
132
+ state: resolvedInitial,
133
+ event: initEvent,
134
+ self
135
+ };
136
+ const { effects: effectSlots } = typedMachine._slots;
137
+ for (const bg of typedMachine.backgroundEffects) {
138
+ const fiber = yield* Effect.forkDaemon(bg.handler({
139
+ state: resolvedInitial,
140
+ event: initEvent,
141
+ self,
142
+ effects: effectSlots
143
+ }).pipe(Effect.provideService(typedMachine.Context, initCtx)));
144
+ backgroundFibers.push(fiber);
145
+ }
146
+ const stateScopeRef = { current: yield* Scope.make() };
147
+ yield* runSpawnEffectsWithInspection(typedMachine, resolvedInitial, initEvent, self, stateScopeRef.current, id, inspector);
148
+ if (typedMachine.finalStates.has(resolvedInitial._tag)) {
149
+ yield* Scope.close(stateScopeRef.current, Exit.void);
150
+ yield* Effect.all(backgroundFibers.map(Fiber.interrupt), { concurrency: "unbounded" });
151
+ yield* Fiber.interrupt(snapshotFiber);
152
+ yield* Fiber.interrupt(persistenceFiber);
153
+ yield* Ref.set(stoppedRef, true);
154
+ yield* emitWithTimestamp(inspector, (timestamp) => ({
155
+ type: "@machine.stop",
156
+ actorId: id,
157
+ finalState: resolvedInitial,
158
+ timestamp
159
+ }));
160
+ return buildPersistentActorRef(id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, listeners, Ref.set(stoppedRef, true).pipe(Effect.withSpan("effect-machine.persistentActor.stop"), Effect.asVoid), adapter);
161
+ }
162
+ const loopFiber = yield* Effect.forkDaemon(persistentEventLoop(id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, self, listeners, adapter, createdAt, stateScopeRef, backgroundFibers, snapshotQueue, snapshotEnabledRef, persistenceQueue, snapshotFiber, persistenceFiber, inspector));
163
+ return buildPersistentActorRef(id, persistentMachine, stateRef, versionRef, eventQueue, stoppedRef, listeners, Effect.gen(function* () {
164
+ const finalState = yield* SubscriptionRef.get(stateRef);
165
+ yield* emitWithTimestamp(inspector, (timestamp) => ({
166
+ type: "@machine.stop",
167
+ actorId: id,
168
+ finalState,
169
+ timestamp
170
+ }));
171
+ yield* Ref.set(stoppedRef, true);
172
+ yield* Fiber.interrupt(loopFiber);
173
+ yield* Scope.close(stateScopeRef.current, Exit.void);
174
+ yield* Effect.all(backgroundFibers.map(Fiber.interrupt), { concurrency: "unbounded" });
175
+ yield* Fiber.interrupt(snapshotFiber);
176
+ yield* Fiber.interrupt(persistenceFiber);
177
+ }).pipe(Effect.withSpan("effect-machine.persistentActor.stop"), Effect.asVoid), adapter);
178
+ });
179
+ /**
180
+ * Main event loop for persistent actor
181
+ */
182
+ 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) {
183
+ const { machine, persistence } = persistentMachine;
184
+ const typedMachine = machine;
185
+ const hooks = inspector === void 0 ? void 0 : {
186
+ onSpawnEffect: (state) => emitWithTimestamp(inspector, (timestamp) => ({
187
+ type: "@machine.effect",
188
+ actorId: id,
189
+ effectType: "spawn",
190
+ state,
191
+ timestamp
192
+ })),
193
+ onTransition: (from, to, ev) => emitWithTimestamp(inspector, (timestamp) => ({
194
+ type: "@machine.transition",
195
+ actorId: id,
196
+ fromState: from,
197
+ toState: to,
198
+ event: ev,
199
+ timestamp
200
+ })),
201
+ onError: (info) => emitWithTimestamp(inspector, (timestamp) => ({
202
+ type: "@machine.error",
203
+ actorId: id,
204
+ phase: info.phase,
205
+ state: info.state,
206
+ event: info.event,
207
+ error: Cause.pretty(info.cause),
208
+ timestamp
209
+ }))
210
+ };
211
+ while (true) {
212
+ const event = yield* Queue.take(eventQueue);
213
+ const currentState = yield* SubscriptionRef.get(stateRef);
214
+ const currentVersion = yield* Ref.get(versionRef);
215
+ yield* emitWithTimestamp(inspector, (timestamp) => ({
216
+ type: "@machine.event",
217
+ actorId: id,
218
+ state: currentState,
219
+ event,
220
+ timestamp
221
+ }));
222
+ const result = yield* processEventCore(typedMachine, currentState, event, self, stateScopeRef, hooks);
223
+ if (!result.transitioned) continue;
224
+ const newVersion = currentVersion + 1;
225
+ yield* Ref.set(versionRef, newVersion);
226
+ yield* SubscriptionRef.set(stateRef, result.newState);
227
+ notifyListeners(listeners, result.newState);
228
+ if (persistence.journalEvents) {
229
+ const persistedEvent = {
230
+ event,
231
+ version: newVersion,
232
+ timestamp: yield* now
233
+ };
234
+ const journalTask = adapter.appendEvent(id, persistedEvent, persistence.eventSchema).pipe(Effect.catchAll((e) => Effect.logWarning(`Failed to journal event for actor ${id}`, e)), Effect.asVoid);
235
+ yield* Queue.offer(persistenceQueue, journalTask);
236
+ }
237
+ yield* Queue.offer(persistenceQueue, saveMetadata(id, result.newState, newVersion, createdAt, persistence, adapter));
238
+ if (yield* Ref.get(snapshotEnabledRef)) yield* Queue.offer(snapshotQueue, {
239
+ state: result.newState,
240
+ version: newVersion
241
+ });
242
+ if (result.lifecycleRan && result.isFinal) {
243
+ yield* emitWithTimestamp(inspector, (timestamp) => ({
244
+ type: "@machine.stop",
245
+ actorId: id,
246
+ finalState: result.newState,
247
+ timestamp
248
+ }));
249
+ yield* Ref.set(stoppedRef, true);
250
+ yield* Scope.close(stateScopeRef.current, Exit.void);
251
+ yield* Effect.all(backgroundFibers.map(Fiber.interrupt), { concurrency: "unbounded" });
252
+ yield* Fiber.interrupt(snapshotFiber);
253
+ yield* Fiber.interrupt(persistenceFiber);
254
+ return;
255
+ }
256
+ }
257
+ });
258
+ /**
259
+ * Run spawn effects with inspection and tracing.
260
+ * @internal
261
+ */
262
+ const runSpawnEffectsWithInspection = Effect.fn("effect-machine.persistentActor.spawnEffects")(function* (machine, state, event, self, stateScope, actorId, inspector) {
263
+ yield* emitWithTimestamp(inspector, (timestamp) => ({
264
+ type: "@machine.effect",
265
+ actorId,
266
+ effectType: "spawn",
267
+ state,
268
+ timestamp
269
+ }));
270
+ yield* runSpawnEffects(machine, state, event, self, stateScope, inspector === void 0 ? void 0 : (info) => emitWithTimestamp(inspector, (timestamp) => ({
271
+ type: "@machine.error",
272
+ actorId,
273
+ phase: info.phase,
274
+ state: info.state,
275
+ event: info.event,
276
+ error: Cause.pretty(info.cause),
277
+ timestamp
278
+ })));
279
+ });
280
+ /**
281
+ * Persistence worker (journaling + metadata).
282
+ */
283
+ const persistenceWorker = Effect.fn("effect-machine.persistentActor.persistenceWorker")(function* (queue) {
284
+ while (true) yield* yield* Queue.take(queue);
285
+ });
286
+ /**
287
+ * Snapshot scheduler worker (runs in background).
288
+ */
289
+ const snapshotWorker = Effect.fn("effect-machine.persistentActor.snapshotWorker")(function* (id, persistence, adapter, queue, enabledRef) {
290
+ const driver = yield* Schedule.driver(persistence.snapshotSchedule);
291
+ while (true) {
292
+ const { state, version } = yield* Queue.take(queue);
293
+ if (!(yield* Ref.get(enabledRef))) continue;
294
+ if (!(yield* driver.next(state).pipe(Effect.match({
295
+ onFailure: () => false,
296
+ onSuccess: () => true
297
+ })))) {
298
+ yield* Ref.set(enabledRef, false);
299
+ continue;
300
+ }
301
+ yield* saveSnapshot(id, state, version, persistence, adapter);
302
+ }
303
+ });
304
+ /**
305
+ * Save a snapshot after state transition.
306
+ * Called by snapshot scheduler.
307
+ */
308
+ const saveSnapshot = Effect.fn("effect-machine.persistentActor.saveSnapshot")(function* (id, state, version, persistence, adapter) {
309
+ const snapshot = {
310
+ state,
311
+ version,
312
+ timestamp: yield* now
313
+ };
314
+ yield* adapter.saveSnapshot(id, snapshot, persistence.stateSchema).pipe(Effect.catchAll((e) => Effect.logWarning(`Failed to save snapshot for actor ${id}`, e)));
315
+ });
316
+ /**
317
+ * Save or update actor metadata if adapter supports registry.
318
+ * Called on spawn and state transitions.
319
+ */
320
+ const saveMetadata = Effect.fn("effect-machine.persistentActor.saveMetadata")(function* (id, state, version, createdAt, persistence, adapter) {
321
+ const save = adapter.saveMetadata;
322
+ if (save === void 0) return;
323
+ const lastActivityAt = yield* now;
324
+ yield* save({
325
+ id,
326
+ machineType: persistence.machineType ?? "unknown",
327
+ createdAt,
328
+ lastActivityAt,
329
+ version,
330
+ stateTag: state._tag
331
+ }).pipe(Effect.catchAll((e) => Effect.logWarning(`Failed to save metadata for actor ${id}`, e)));
332
+ });
333
+ /**
334
+ * Restore an actor from persistence.
335
+ * Returns None if no persisted state exists.
336
+ */
337
+ const restorePersistentActor = Effect.fn("effect-machine.persistentActor.restore")(function* (id, persistentMachine) {
338
+ const adapter = yield* PersistenceAdapterTag;
339
+ const { persistence } = persistentMachine;
340
+ const maybeSnapshot = yield* adapter.loadSnapshot(id, persistence.stateSchema);
341
+ const events = yield* adapter.loadEvents(id, persistence.eventSchema, Option.isSome(maybeSnapshot) ? maybeSnapshot.value.version : void 0);
342
+ if (Option.isNone(maybeSnapshot) && events.length === 0) return Option.none();
343
+ const actor = yield* createPersistentActor(id, persistentMachine, maybeSnapshot, events);
344
+ return Option.some(actor);
345
+ });
346
+
347
+ //#endregion
348
+ export { createPersistentActor, restorePersistentActor };
@@ -1,13 +1,14 @@
1
- import type { Schema, Schedule } from "effect";
2
-
3
- import type { Machine } from "../machine.js";
4
- import type { StateBrand, EventBrand } from "../internal/brands.js";
5
- import { MissingSchemaError } from "../errors.js";
6
-
7
- // Branded type constraints
8
- type BrandedState = { readonly _tag: string } & StateBrand;
9
- type BrandedEvent = { readonly _tag: string } & EventBrand;
1
+ import { EventBrand, StateBrand } from "../internal/brands.js";
2
+ import { Machine } from "../machine.js";
3
+ import { Schedule, Schema } from "effect";
10
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;
11
12
  /**
12
13
  * Configuration for persistence behavior (after resolution).
13
14
  * Schemas are required at runtime - the persist function ensures this.
@@ -15,7 +16,7 @@ type BrandedEvent = { readonly _tag: string } & EventBrand;
15
16
  * Note: Schema types S and E should match the structural shape of the machine's
16
17
  * state and event types (without brands). The schemas don't know about brands.
17
18
  */
18
- export interface PersistenceConfig<S, E, SSI = unknown, ESI = unknown> {
19
+ interface PersistenceConfig<S, E, SSI = unknown, ESI = unknown> {
19
20
  /**
20
21
  * Schedule controlling when snapshots are taken.
21
22
  * Input is the new state after each transition.
@@ -26,25 +27,21 @@ export interface PersistenceConfig<S, E, SSI = unknown, ESI = unknown> {
26
27
  * - Schedule.recurs(100) — every N transitions
27
28
  */
28
29
  readonly snapshotSchedule: Schedule.Schedule<unknown, S>;
29
-
30
30
  /**
31
31
  * Whether to journal events for replay capability.
32
32
  * When true, all events are appended to the event log.
33
33
  */
34
34
  readonly journalEvents: boolean;
35
-
36
35
  /**
37
36
  * Schema for serializing/deserializing state.
38
37
  * Always present at runtime (resolved from config or machine).
39
38
  */
40
39
  readonly stateSchema: Schema.Schema<S, SSI, never>;
41
-
42
40
  /**
43
41
  * Schema for serializing/deserializing events.
44
42
  * Always present at runtime (resolved from config or machine).
45
43
  */
46
44
  readonly eventSchema: Schema.Schema<E, ESI, never>;
47
-
48
45
  /**
49
46
  * User-provided identifier for the machine type.
50
47
  * Used for filtering actors in restoreAll.
@@ -52,32 +49,27 @@ export interface PersistenceConfig<S, E, SSI = unknown, ESI = unknown> {
52
49
  */
53
50
  readonly machineType?: string;
54
51
  }
55
-
56
52
  /**
57
53
  * Machine with persistence configuration attached.
58
54
  * Spawn auto-detects this and returns PersistentActorRef.
59
55
  */
60
- export interface PersistentMachine<
61
- S extends { readonly _tag: string },
62
- E extends { readonly _tag: string },
63
- R = never,
64
- > {
56
+ interface PersistentMachine<S extends {
57
+ readonly _tag: string;
58
+ }, E extends {
59
+ readonly _tag: string;
60
+ }, R = never> {
65
61
  readonly _tag: "PersistentMachine";
66
62
  readonly machine: Machine<S, E, R>;
67
63
  readonly persistence: PersistenceConfig<S, E>;
68
64
  }
69
-
70
65
  /**
71
66
  * Type guard to check if a value is a PersistentMachine
72
67
  */
73
- export const isPersistentMachine = (
74
- value: unknown,
75
- ): value is PersistentMachine<{ readonly _tag: string }, { readonly _tag: string }, unknown> =>
76
- typeof value === "object" &&
77
- value !== null &&
78
- "_tag" in value &&
79
- (value as { _tag: unknown })._tag === "PersistentMachine";
80
-
68
+ declare const isPersistentMachine: (value: unknown) => value is PersistentMachine<{
69
+ readonly _tag: string;
70
+ }, {
71
+ readonly _tag: string;
72
+ }, unknown>;
81
73
  /**
82
74
  * Attach persistence configuration to a machine.
83
75
  *
@@ -101,31 +93,13 @@ export const isPersistentMachine = (
101
93
  * );
102
94
  * ```
103
95
  */
104
- export interface WithPersistenceConfig {
105
- readonly snapshotSchedule: Schedule.Schedule<unknown, { readonly _tag: string }>;
96
+ interface WithPersistenceConfig {
97
+ readonly snapshotSchedule: Schedule.Schedule<unknown, {
98
+ readonly _tag: string;
99
+ }>;
106
100
  readonly journalEvents: boolean;
107
101
  readonly machineType?: string;
108
102
  }
109
-
110
- export const persist =
111
- (config: WithPersistenceConfig) =>
112
- <S extends BrandedState, E extends BrandedEvent, R>(
113
- machine: Machine<S, E, R>,
114
- ): PersistentMachine<S, E, R> => {
115
- const stateSchema = machine.stateSchema;
116
- const eventSchema = machine.eventSchema;
117
-
118
- if (stateSchema === undefined || eventSchema === undefined) {
119
- throw new MissingSchemaError({ operation: "persist" });
120
- }
121
-
122
- return {
123
- _tag: "PersistentMachine",
124
- machine,
125
- persistence: {
126
- ...config,
127
- stateSchema,
128
- eventSchema,
129
- } as unknown as PersistenceConfig<S, E>,
130
- };
131
- };
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 };
@@ -0,0 +1,24 @@
1
+ import { MissingSchemaError } from "../errors.js";
2
+
3
+ //#region src/persistence/persistent-machine.ts
4
+ /**
5
+ * Type guard to check if a value is a PersistentMachine
6
+ */
7
+ const isPersistentMachine = (value) => typeof value === "object" && value !== null && "_tag" in value && value._tag === "PersistentMachine";
8
+ const persist = (config) => (machine) => {
9
+ const stateSchema = machine.stateSchema;
10
+ const eventSchema = machine.eventSchema;
11
+ if (stateSchema === void 0 || eventSchema === void 0) throw new MissingSchemaError({ operation: "persist" });
12
+ return {
13
+ _tag: "PersistentMachine",
14
+ machine,
15
+ persistence: {
16
+ ...config,
17
+ stateSchema,
18
+ eventSchema
19
+ }
20
+ };
21
+ };
22
+
23
+ //#endregion
24
+ export { isPersistentMachine, persist };
@@ -0,0 +1,141 @@
1
+ import { FullEventBrand, FullStateBrand } from "./internal/brands.js";
2
+ import { Schema } from "effect";
3
+
4
+ //#region src/schema.d.ts
5
+ /**
6
+ * Extract the TypeScript type from a TaggedStruct schema
7
+ */
8
+ type TaggedStructType<Tag extends string, Fields extends Schema.Struct.Fields> = Schema.Schema.Type<Schema.TaggedStruct<Tag, Fields>>;
9
+ /**
10
+ * Build variant schemas type from definition
11
+ */
12
+ type VariantSchemas<D extends Record<string, Schema.Struct.Fields>> = { readonly [K in keyof D & string]: Schema.TaggedStruct<K, D[K]> };
13
+ /**
14
+ * Build union type from variant schemas.
15
+ * Used for constraining fluent method type params.
16
+ */
17
+ type VariantsUnion<D extends Record<string, Schema.Struct.Fields>> = { [K in keyof D & string]: TaggedStructType<K, D[K]> }[keyof D & string];
18
+ /**
19
+ * Check if fields are empty (no required properties)
20
+ */
21
+ type IsEmptyFields<Fields extends Schema.Struct.Fields> = keyof Fields extends never ? true : false;
22
+ /**
23
+ * Constructor functions for each variant.
24
+ * Empty structs: plain values with `_tag`: `State.Idle`
25
+ * Non-empty structs require args: `State.Loading({ url })`
26
+ *
27
+ * Each variant also has a `derive` method for constructing from a source object.
28
+ */
29
+ /**
30
+ * Constructor functions for each variant.
31
+ * Empty structs: plain values with `_tag`: `State.Idle`
32
+ * Non-empty structs require args: `State.Loading({ url })`
33
+ *
34
+ * Each variant also has a `derive` method for constructing from a source object.
35
+ * The source type uses `object` to accept branded state types without index signature issues.
36
+ */
37
+ type VariantConstructors<D extends Record<string, Schema.Struct.Fields>, Brand> = { readonly [K in keyof D & string]: IsEmptyFields<D[K]> extends true ? TaggedStructType<K, D[K]> & Brand & {
38
+ readonly derive: (source: object) => TaggedStructType<K, D[K]> & Brand;
39
+ } : ((args: Schema.Struct.Constructor<D[K]>) => TaggedStructType<K, D[K]> & Brand) & {
40
+ readonly derive: (source: object, partial?: Partial<Schema.Struct.Constructor<D[K]>>) => TaggedStructType<K, D[K]> & Brand;
41
+ readonly _tag: K;
42
+ } };
43
+ /**
44
+ * Pattern matching cases type
45
+ */
46
+ type MatchCases<D extends Record<string, Schema.Struct.Fields>, R> = { readonly [K in keyof D & string]: (value: TaggedStructType<K, D[K]>) => R };
47
+ /**
48
+ * Base schema interface with pattern matching helpers
49
+ */
50
+ interface MachineSchemaBase<D extends Record<string, Schema.Struct.Fields>, Brand> {
51
+ /**
52
+ * Raw definition record for introspection
53
+ */
54
+ readonly _definition: D;
55
+ /**
56
+ * Per-variant schemas for fine-grained operations
57
+ */
58
+ readonly variants: VariantSchemas<D>;
59
+ /**
60
+ * Type guard: `OrderState.$is("Pending")(value)`
61
+ */
62
+ readonly $is: <Tag extends keyof D & string>(tag: Tag) => (u: unknown) => u is TaggedStructType<Tag, D[Tag]> & Brand;
63
+ /**
64
+ * Pattern matching (curried and uncurried)
65
+ */
66
+ readonly $match: {
67
+ <R>(cases: MatchCases<D, R>): (value: VariantsUnion<D> & Brand) => R;
68
+ <R>(value: VariantsUnion<D> & Brand, cases: MatchCases<D, R>): R;
69
+ };
70
+ }
71
+ /**
72
+ * Schema-first state definition that provides:
73
+ * - Schema for encode/decode/validate
74
+ * - Variant constructors: `OrderState.Pending({ orderId: "x" })`
75
+ * - Pattern matching: `$is`, `$match`
76
+ * - Type inference: `typeof OrderState.Type`
77
+ *
78
+ * The D type parameter captures the definition, creating a unique brand
79
+ * per distinct schema definition shape.
80
+ */
81
+ type MachineStateSchema<D extends Record<string, Schema.Struct.Fields>> = Schema.Schema<VariantsUnion<D> & FullStateBrand<D>, VariantsUnion<D>, never> & MachineSchemaBase<D, FullStateBrand<D>> & VariantConstructors<D, FullStateBrand<D>>;
82
+ /**
83
+ * Schema-first event definition (same structure as state, different brand)
84
+ *
85
+ * The D type parameter captures the definition, creating a unique brand
86
+ * per distinct schema definition shape.
87
+ */
88
+ type MachineEventSchema<D extends Record<string, Schema.Struct.Fields>> = Schema.Schema<VariantsUnion<D> & FullEventBrand<D>, VariantsUnion<D>, never> & MachineSchemaBase<D, FullEventBrand<D>> & VariantConstructors<D, FullEventBrand<D>>;
89
+ /**
90
+ * Create a schema-first State definition.
91
+ *
92
+ * The schema's definition type D creates a unique brand, preventing
93
+ * accidental use of constructors from different state schemas
94
+ * (unless they have identical definitions).
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * const OrderState = MachineSchema.State({
99
+ * Pending: { orderId: Schema.String },
100
+ * Shipped: { trackingId: Schema.String },
101
+ * })
102
+ *
103
+ * type OrderState = typeof OrderState.Type
104
+ *
105
+ * // Construct
106
+ * const s = OrderState.Pending({ orderId: "123" })
107
+ *
108
+ * // Pattern match
109
+ * OrderState.$match(s, {
110
+ * Pending: (v) => v.orderId,
111
+ * Shipped: (v) => v.trackingId,
112
+ * })
113
+ *
114
+ * // Validate
115
+ * Schema.decodeUnknownSync(OrderState)(rawJson)
116
+ * ```
117
+ */
118
+ declare const State: <const D extends Record<string, Schema.Struct.Fields>>(definition: D) => MachineStateSchema<D>;
119
+ /**
120
+ * Create a schema-first Event definition.
121
+ *
122
+ * The schema's definition type D creates a unique brand, preventing
123
+ * accidental use of constructors from different event schemas
124
+ * (unless they have identical definitions).
125
+ *
126
+ * @example
127
+ * ```ts
128
+ * const OrderEvent = MachineSchema.Event({
129
+ * Ship: { trackingId: Schema.String },
130
+ * Cancel: {},
131
+ * })
132
+ *
133
+ * type OrderEvent = typeof OrderEvent.Type
134
+ *
135
+ * // Construct
136
+ * const e = OrderEvent.Ship({ trackingId: "abc" })
137
+ * ```
138
+ */
139
+ declare const Event: <const D extends Record<string, Schema.Struct.Fields>>(definition: D) => MachineEventSchema<D>;
140
+ //#endregion
141
+ export { Event, MachineEventSchema, MachineStateSchema, State, VariantsUnion };