effect-machine 0.3.1 → 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.
- package/dist/_virtual/_rolldown/runtime.js +18 -0
- package/dist/actor.d.ts +251 -0
- package/dist/actor.js +385 -0
- package/dist/cluster/entity-machine.d.ts +90 -0
- package/dist/cluster/entity-machine.js +74 -0
- package/dist/cluster/index.d.ts +3 -0
- package/dist/cluster/index.js +4 -0
- package/dist/cluster/to-entity.d.ts +64 -0
- package/dist/cluster/to-entity.js +53 -0
- package/dist/errors.d.ts +61 -0
- package/dist/errors.js +38 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +14 -0
- package/dist/inspection.d.ts +125 -0
- package/dist/inspection.js +50 -0
- package/dist/internal/brands.d.ts +40 -0
- package/dist/internal/brands.js +0 -0
- package/dist/internal/inspection.d.ts +11 -0
- package/dist/internal/inspection.js +15 -0
- package/dist/internal/transition.d.ts +159 -0
- package/dist/internal/transition.js +235 -0
- package/dist/internal/utils.d.ts +52 -0
- package/dist/internal/utils.js +31 -0
- package/dist/machine.d.ts +271 -0
- package/dist/machine.js +317 -0
- package/{src/persistence/adapter.ts → dist/persistence/adapter.d.ts} +40 -72
- package/dist/persistence/adapter.js +27 -0
- package/dist/persistence/adapters/in-memory.d.ts +32 -0
- package/dist/persistence/adapters/in-memory.js +176 -0
- package/dist/persistence/index.d.ts +5 -0
- package/dist/persistence/index.js +6 -0
- package/dist/persistence/persistent-actor.d.ts +50 -0
- package/dist/persistence/persistent-actor.js +348 -0
- package/{src/persistence/persistent-machine.ts → dist/persistence/persistent-machine.d.ts} +28 -54
- package/dist/persistence/persistent-machine.js +24 -0
- package/dist/schema.d.ts +141 -0
- package/dist/schema.js +165 -0
- package/dist/slot.d.ts +128 -0
- package/dist/slot.js +99 -0
- package/dist/testing.d.ts +142 -0
- package/dist/testing.js +131 -0
- package/package.json +18 -7
- package/src/actor.ts +0 -1058
- package/src/cluster/entity-machine.ts +0 -201
- package/src/cluster/index.ts +0 -43
- package/src/cluster/to-entity.ts +0 -99
- package/src/errors.ts +0 -64
- package/src/index.ts +0 -105
- package/src/inspection.ts +0 -178
- package/src/internal/brands.ts +0 -51
- package/src/internal/inspection.ts +0 -18
- package/src/internal/transition.ts +0 -489
- package/src/internal/utils.ts +0 -80
- package/src/machine.ts +0 -836
- package/src/persistence/adapters/in-memory.ts +0 -294
- package/src/persistence/index.ts +0 -24
- package/src/persistence/persistent-actor.ts +0 -791
- package/src/schema.ts +0 -362
- package/src/slot.ts +0 -281
- package/src/testing.ts +0 -284
- package/tsconfig.json +0 -65
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
import type { PersistentActorRef } from "./persistent-actor.js";
|
|
5
|
-
import type { DuplicateActorError } from "../errors.js";
|
|
1
|
+
import { PersistentActorRef } from "./persistent-actor.js";
|
|
2
|
+
import { DuplicateActorError } from "../errors.js";
|
|
3
|
+
import { Context, Effect, Option, Schema } from "effect";
|
|
6
4
|
|
|
5
|
+
//#region src/persistence/adapter.d.ts
|
|
7
6
|
/**
|
|
8
7
|
* Metadata for a persisted actor.
|
|
9
8
|
* Used for discovery and filtering during bulk restore.
|
|
10
9
|
*/
|
|
11
|
-
|
|
10
|
+
interface ActorMetadata {
|
|
12
11
|
readonly id: string;
|
|
13
12
|
/** User-provided identifier for the machine type */
|
|
14
13
|
readonly machineType: string;
|
|
@@ -18,46 +17,41 @@ export interface ActorMetadata {
|
|
|
18
17
|
/** Current state _tag value */
|
|
19
18
|
readonly stateTag: string;
|
|
20
19
|
}
|
|
21
|
-
|
|
22
20
|
/**
|
|
23
21
|
* Result of a bulk restore operation.
|
|
24
22
|
* Contains both successfully restored actors and failures.
|
|
25
23
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
> {
|
|
24
|
+
interface RestoreResult<S extends {
|
|
25
|
+
readonly _tag: string;
|
|
26
|
+
}, E extends {
|
|
27
|
+
readonly _tag: string;
|
|
28
|
+
}, R = never> {
|
|
31
29
|
readonly restored: ReadonlyArray<PersistentActorRef<S, E, R>>;
|
|
32
30
|
readonly failed: ReadonlyArray<RestoreFailure>;
|
|
33
31
|
}
|
|
34
|
-
|
|
35
32
|
/**
|
|
36
33
|
* A single restore failure with actor ID and error details.
|
|
37
34
|
*/
|
|
38
|
-
|
|
35
|
+
interface RestoreFailure {
|
|
39
36
|
readonly id: string;
|
|
40
37
|
readonly error: PersistenceError | DuplicateActorError;
|
|
41
38
|
}
|
|
42
|
-
|
|
43
39
|
/**
|
|
44
40
|
* Snapshot of actor state at a point in time
|
|
45
41
|
*/
|
|
46
|
-
|
|
42
|
+
interface Snapshot<S> {
|
|
47
43
|
readonly state: S;
|
|
48
44
|
readonly version: number;
|
|
49
45
|
readonly timestamp: number;
|
|
50
46
|
}
|
|
51
|
-
|
|
52
47
|
/**
|
|
53
48
|
* Persisted event with metadata
|
|
54
49
|
*/
|
|
55
|
-
|
|
50
|
+
interface PersistedEvent<E> {
|
|
56
51
|
readonly event: E;
|
|
57
52
|
readonly version: number;
|
|
58
53
|
readonly timestamp: number;
|
|
59
54
|
}
|
|
60
|
-
|
|
61
55
|
/**
|
|
62
56
|
* Adapter for persisting actor state and events.
|
|
63
57
|
*
|
|
@@ -65,106 +59,80 @@ export interface PersistedEvent<E> {
|
|
|
65
59
|
* Schema parameters ensure type-safe serialization/deserialization.
|
|
66
60
|
* Schemas must have no context requirements (use Schema<S, SI, never>).
|
|
67
61
|
*/
|
|
68
|
-
|
|
62
|
+
interface PersistenceAdapter {
|
|
69
63
|
/**
|
|
70
64
|
* Save a snapshot of actor state.
|
|
71
65
|
* Implementations should use optimistic locking — fail if version mismatch.
|
|
72
66
|
*/
|
|
73
|
-
readonly saveSnapshot: <S, SI>(
|
|
74
|
-
id: string,
|
|
75
|
-
snapshot: Snapshot<S>,
|
|
76
|
-
schema: Schema.Schema<S, SI, never>,
|
|
77
|
-
) => Effect.Effect<void, PersistenceError | VersionConflictError>;
|
|
78
|
-
|
|
67
|
+
readonly saveSnapshot: <S, SI>(id: string, snapshot: Snapshot<S>, schema: Schema.Schema<S, SI, never>) => Effect.Effect<void, PersistenceError | VersionConflictError>;
|
|
79
68
|
/**
|
|
80
69
|
* Load the latest snapshot for an actor.
|
|
81
70
|
* Returns None if no snapshot exists.
|
|
82
71
|
*/
|
|
83
|
-
readonly loadSnapshot: <S, SI>(
|
|
84
|
-
id: string,
|
|
85
|
-
schema: Schema.Schema<S, SI, never>,
|
|
86
|
-
) => Effect.Effect<Option.Option<Snapshot<S>>, PersistenceError>;
|
|
87
|
-
|
|
72
|
+
readonly loadSnapshot: <S, SI>(id: string, schema: Schema.Schema<S, SI, never>) => Effect.Effect<Option.Option<Snapshot<S>>, PersistenceError>;
|
|
88
73
|
/**
|
|
89
74
|
* Append an event to the actor's event journal.
|
|
90
75
|
*/
|
|
91
|
-
readonly appendEvent: <E, EI>(
|
|
92
|
-
id: string,
|
|
93
|
-
event: PersistedEvent<E>,
|
|
94
|
-
schema: Schema.Schema<E, EI, never>,
|
|
95
|
-
) => Effect.Effect<void, PersistenceError>;
|
|
96
|
-
|
|
76
|
+
readonly appendEvent: <E, EI>(id: string, event: PersistedEvent<E>, schema: Schema.Schema<E, EI, never>) => Effect.Effect<void, PersistenceError>;
|
|
97
77
|
/**
|
|
98
78
|
* Load events from the journal, optionally after a specific version.
|
|
99
79
|
*/
|
|
100
|
-
readonly loadEvents: <E, EI>(
|
|
101
|
-
id: string,
|
|
102
|
-
schema: Schema.Schema<E, EI, never>,
|
|
103
|
-
afterVersion?: number,
|
|
104
|
-
) => Effect.Effect<ReadonlyArray<PersistedEvent<E>>, PersistenceError>;
|
|
105
|
-
|
|
80
|
+
readonly loadEvents: <E, EI>(id: string, schema: Schema.Schema<E, EI, never>, afterVersion?: number) => Effect.Effect<ReadonlyArray<PersistedEvent<E>>, PersistenceError>;
|
|
106
81
|
/**
|
|
107
82
|
* Delete all persisted data for an actor (snapshot + events).
|
|
108
83
|
*/
|
|
109
84
|
readonly deleteActor: (id: string) => Effect.Effect<void, PersistenceError>;
|
|
110
|
-
|
|
111
|
-
// --- Optional registry methods for actor discovery ---
|
|
112
|
-
|
|
113
85
|
/**
|
|
114
86
|
* List all persisted actor metadata.
|
|
115
87
|
* Optional — adapters without registry support can omit this.
|
|
116
88
|
*/
|
|
117
89
|
readonly listActors?: () => Effect.Effect<ReadonlyArray<ActorMetadata>, PersistenceError>;
|
|
118
|
-
|
|
119
90
|
/**
|
|
120
91
|
* Save or update actor metadata.
|
|
121
92
|
* Called on spawn and state transitions.
|
|
122
93
|
* Optional — adapters without registry support can omit this.
|
|
123
94
|
*/
|
|
124
95
|
readonly saveMetadata?: (metadata: ActorMetadata) => Effect.Effect<void, PersistenceError>;
|
|
125
|
-
|
|
126
96
|
/**
|
|
127
97
|
* Delete actor metadata.
|
|
128
98
|
* Called when actor is deleted.
|
|
129
99
|
* Optional — adapters without registry support can omit this.
|
|
130
100
|
*/
|
|
131
101
|
readonly deleteMetadata?: (id: string) => Effect.Effect<void, PersistenceError>;
|
|
132
|
-
|
|
133
102
|
/**
|
|
134
103
|
* Load metadata for a specific actor by ID.
|
|
135
104
|
* Returns None if no metadata exists.
|
|
136
105
|
* Optional — adapters without registry support can omit this.
|
|
137
106
|
*/
|
|
138
|
-
readonly loadMetadata?: (
|
|
139
|
-
id: string,
|
|
140
|
-
) => Effect.Effect<Option.Option<ActorMetadata>, PersistenceError>;
|
|
107
|
+
readonly loadMetadata?: (id: string) => Effect.Effect<Option.Option<ActorMetadata>, PersistenceError>;
|
|
141
108
|
}
|
|
142
|
-
|
|
109
|
+
declare const PersistenceError_base: Schema.TaggedErrorClass<PersistenceError, "PersistenceError", {
|
|
110
|
+
readonly _tag: Schema.tag<"PersistenceError">;
|
|
111
|
+
} & {
|
|
112
|
+
operation: typeof Schema.String;
|
|
113
|
+
actorId: typeof Schema.String;
|
|
114
|
+
cause: Schema.optional<typeof Schema.Unknown>;
|
|
115
|
+
message: Schema.optional<typeof Schema.String>;
|
|
116
|
+
}>;
|
|
143
117
|
/**
|
|
144
118
|
* Error type for persistence operations
|
|
145
119
|
*/
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
120
|
+
declare class PersistenceError extends PersistenceError_base {}
|
|
121
|
+
declare const VersionConflictError_base: Schema.TaggedErrorClass<VersionConflictError, "VersionConflictError", {
|
|
122
|
+
readonly _tag: Schema.tag<"VersionConflictError">;
|
|
123
|
+
} & {
|
|
124
|
+
actorId: typeof Schema.String;
|
|
125
|
+
expectedVersion: typeof Schema.Number;
|
|
126
|
+
actualVersion: typeof Schema.Number;
|
|
127
|
+
}>;
|
|
153
128
|
/**
|
|
154
129
|
* Version conflict error — snapshot version doesn't match expected
|
|
155
130
|
*/
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
{
|
|
159
|
-
actorId: Schema.String,
|
|
160
|
-
expectedVersion: Schema.Number,
|
|
161
|
-
actualVersion: Schema.Number,
|
|
162
|
-
},
|
|
163
|
-
) {}
|
|
164
|
-
|
|
131
|
+
declare class VersionConflictError extends VersionConflictError_base {}
|
|
132
|
+
declare const PersistenceAdapterTag_base: Context.TagClass<PersistenceAdapterTag, "effect-machine/src/persistence/adapter/PersistenceAdapterTag", PersistenceAdapter>;
|
|
165
133
|
/**
|
|
166
134
|
* PersistenceAdapter service tag
|
|
167
135
|
*/
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
136
|
+
declare class PersistenceAdapterTag extends PersistenceAdapterTag_base {}
|
|
137
|
+
//#endregion
|
|
138
|
+
export { ActorMetadata, PersistedEvent, PersistenceAdapter, PersistenceAdapterTag, PersistenceError, RestoreFailure, RestoreResult, Snapshot, VersionConflictError };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Context, Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
//#region src/persistence/adapter.ts
|
|
4
|
+
/**
|
|
5
|
+
* Error type for persistence operations
|
|
6
|
+
*/
|
|
7
|
+
var PersistenceError = class extends Schema.TaggedError()("PersistenceError", {
|
|
8
|
+
operation: Schema.String,
|
|
9
|
+
actorId: Schema.String,
|
|
10
|
+
cause: Schema.optional(Schema.Unknown),
|
|
11
|
+
message: Schema.optional(Schema.String)
|
|
12
|
+
}) {};
|
|
13
|
+
/**
|
|
14
|
+
* Version conflict error — snapshot version doesn't match expected
|
|
15
|
+
*/
|
|
16
|
+
var VersionConflictError = class extends Schema.TaggedError()("VersionConflictError", {
|
|
17
|
+
actorId: Schema.String,
|
|
18
|
+
expectedVersion: Schema.Number,
|
|
19
|
+
actualVersion: Schema.Number
|
|
20
|
+
}) {};
|
|
21
|
+
/**
|
|
22
|
+
* PersistenceAdapter service tag
|
|
23
|
+
*/
|
|
24
|
+
var PersistenceAdapterTag = class extends Context.Tag("effect-machine/src/persistence/adapter/PersistenceAdapterTag")() {};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { PersistenceAdapterTag, PersistenceError, VersionConflictError };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PersistenceAdapter, PersistenceAdapterTag } from "../adapter.js";
|
|
2
|
+
import { Effect, Layer } from "effect";
|
|
3
|
+
|
|
4
|
+
//#region src/persistence/adapters/in-memory.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create an in-memory persistence adapter effect.
|
|
7
|
+
* Returns the adapter directly for custom layer composition.
|
|
8
|
+
*/
|
|
9
|
+
declare const makeInMemoryPersistenceAdapter: Effect.Effect<PersistenceAdapter, never, never>;
|
|
10
|
+
/**
|
|
11
|
+
* In-memory persistence adapter layer.
|
|
12
|
+
* Data is not persisted across process restarts.
|
|
13
|
+
*
|
|
14
|
+
* NOTE: Each `Effect.provide(InMemoryPersistenceAdapter)` creates a NEW adapter
|
|
15
|
+
* with empty storage. For tests that need persistent storage across multiple
|
|
16
|
+
* runPromise calls, use `makeInMemoryPersistenceAdapter` with a shared scope.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const program = Effect.gen(function* () {
|
|
21
|
+
* const system = yield* ActorSystemService;
|
|
22
|
+
* const actor = yield* system.spawn("my-actor", persistentMachine);
|
|
23
|
+
* // ...
|
|
24
|
+
* }).pipe(
|
|
25
|
+
* Effect.provide(InMemoryPersistenceAdapter),
|
|
26
|
+
* Effect.provide(ActorSystemDefault),
|
|
27
|
+
* );
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
declare const InMemoryPersistenceAdapter: Layer.Layer<PersistenceAdapterTag>;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { InMemoryPersistenceAdapter, makeInMemoryPersistenceAdapter };
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { PersistenceAdapterTag, PersistenceError, VersionConflictError } from "../adapter.js";
|
|
2
|
+
import { Effect, Layer, Option, Ref, Schema } from "effect";
|
|
3
|
+
|
|
4
|
+
//#region src/persistence/adapters/in-memory.ts
|
|
5
|
+
/**
|
|
6
|
+
* Create an in-memory persistence adapter.
|
|
7
|
+
* Useful for testing and development.
|
|
8
|
+
*/
|
|
9
|
+
const make = Effect.gen(function* () {
|
|
10
|
+
const storage = yield* Ref.make(/* @__PURE__ */ new Map());
|
|
11
|
+
const registry = yield* Ref.make(/* @__PURE__ */ new Map());
|
|
12
|
+
const getOrCreateStorage = Effect.fn("effect-machine.persistence.inMemory.getOrCreateStorage")(function* (id) {
|
|
13
|
+
return yield* Ref.modify(storage, (map) => {
|
|
14
|
+
const existing = map.get(id);
|
|
15
|
+
if (existing !== void 0) return [existing, map];
|
|
16
|
+
const newStorage = {
|
|
17
|
+
snapshot: Option.none(),
|
|
18
|
+
events: []
|
|
19
|
+
};
|
|
20
|
+
const newMap = new Map(map);
|
|
21
|
+
newMap.set(id, newStorage);
|
|
22
|
+
return [newStorage, newMap];
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
const updateStorage = Effect.fn("effect-machine.persistence.inMemory.updateStorage")(function* (id, update) {
|
|
26
|
+
yield* Ref.update(storage, (map) => {
|
|
27
|
+
const existing = map.get(id);
|
|
28
|
+
if (existing === void 0) return map;
|
|
29
|
+
const newMap = new Map(map);
|
|
30
|
+
newMap.set(id, update(existing));
|
|
31
|
+
return newMap;
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
saveSnapshot: Effect.fn("effect-machine.persistence.inMemory.saveSnapshot")(function* (id, snapshot, schema) {
|
|
36
|
+
const actorStorage = yield* getOrCreateStorage(id);
|
|
37
|
+
if (Option.isSome(actorStorage.snapshot)) {
|
|
38
|
+
const existingVersion = actorStorage.snapshot.value.version;
|
|
39
|
+
if (snapshot.version < existingVersion) return yield* new VersionConflictError({
|
|
40
|
+
actorId: id,
|
|
41
|
+
expectedVersion: existingVersion,
|
|
42
|
+
actualVersion: snapshot.version
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const encoded = yield* Schema.encode(schema)(snapshot.state).pipe(Effect.mapError((cause) => new PersistenceError({
|
|
46
|
+
operation: "saveSnapshot",
|
|
47
|
+
actorId: id,
|
|
48
|
+
cause,
|
|
49
|
+
message: "Failed to encode state"
|
|
50
|
+
})));
|
|
51
|
+
yield* updateStorage(id, (s) => ({
|
|
52
|
+
...s,
|
|
53
|
+
snapshot: Option.some({
|
|
54
|
+
data: encoded,
|
|
55
|
+
version: snapshot.version,
|
|
56
|
+
timestamp: snapshot.timestamp
|
|
57
|
+
})
|
|
58
|
+
}));
|
|
59
|
+
}),
|
|
60
|
+
loadSnapshot: Effect.fn("effect-machine.persistence.inMemory.loadSnapshot")(function* (id, schema) {
|
|
61
|
+
const actorStorage = yield* getOrCreateStorage(id);
|
|
62
|
+
if (Option.isNone(actorStorage.snapshot)) return Option.none();
|
|
63
|
+
const stored = actorStorage.snapshot.value;
|
|
64
|
+
const decoded = yield* Schema.decode(schema)(stored.data).pipe(Effect.mapError((cause) => new PersistenceError({
|
|
65
|
+
operation: "loadSnapshot",
|
|
66
|
+
actorId: id,
|
|
67
|
+
cause,
|
|
68
|
+
message: "Failed to decode state"
|
|
69
|
+
})));
|
|
70
|
+
return Option.some({
|
|
71
|
+
state: decoded,
|
|
72
|
+
version: stored.version,
|
|
73
|
+
timestamp: stored.timestamp
|
|
74
|
+
});
|
|
75
|
+
}),
|
|
76
|
+
appendEvent: Effect.fn("effect-machine.persistence.inMemory.appendEvent")(function* (id, event, schema) {
|
|
77
|
+
yield* getOrCreateStorage(id);
|
|
78
|
+
const encoded = yield* Schema.encode(schema)(event.event).pipe(Effect.mapError((cause) => new PersistenceError({
|
|
79
|
+
operation: "appendEvent",
|
|
80
|
+
actorId: id,
|
|
81
|
+
cause,
|
|
82
|
+
message: "Failed to encode event"
|
|
83
|
+
})));
|
|
84
|
+
yield* updateStorage(id, (s) => ({
|
|
85
|
+
...s,
|
|
86
|
+
events: [...s.events, {
|
|
87
|
+
data: encoded,
|
|
88
|
+
version: event.version,
|
|
89
|
+
timestamp: event.timestamp
|
|
90
|
+
}]
|
|
91
|
+
}));
|
|
92
|
+
}),
|
|
93
|
+
loadEvents: Effect.fn("effect-machine.persistence.inMemory.loadEvents")(function* (id, schema, afterVersion) {
|
|
94
|
+
const actorStorage = yield* getOrCreateStorage(id);
|
|
95
|
+
const decoded = [];
|
|
96
|
+
for (const stored of actorStorage.events) {
|
|
97
|
+
if (afterVersion !== void 0 && stored.version <= afterVersion) continue;
|
|
98
|
+
const event = yield* Schema.decode(schema)(stored.data).pipe(Effect.mapError((cause) => new PersistenceError({
|
|
99
|
+
operation: "loadEvents",
|
|
100
|
+
actorId: id,
|
|
101
|
+
cause,
|
|
102
|
+
message: "Failed to decode event"
|
|
103
|
+
})));
|
|
104
|
+
decoded.push({
|
|
105
|
+
event,
|
|
106
|
+
version: stored.version,
|
|
107
|
+
timestamp: stored.timestamp
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
return decoded;
|
|
111
|
+
}),
|
|
112
|
+
deleteActor: Effect.fn("effect-machine.persistence.inMemory.deleteActor")(function* (id) {
|
|
113
|
+
yield* Ref.update(storage, (map) => {
|
|
114
|
+
const newMap = new Map(map);
|
|
115
|
+
newMap.delete(id);
|
|
116
|
+
return newMap;
|
|
117
|
+
});
|
|
118
|
+
yield* Ref.update(registry, (map) => {
|
|
119
|
+
const newMap = new Map(map);
|
|
120
|
+
newMap.delete(id);
|
|
121
|
+
return newMap;
|
|
122
|
+
});
|
|
123
|
+
}),
|
|
124
|
+
listActors: Effect.fn("effect-machine.persistence.inMemory.listActors")(function* () {
|
|
125
|
+
const map = yield* Ref.get(registry);
|
|
126
|
+
return Array.from(map.values());
|
|
127
|
+
}),
|
|
128
|
+
saveMetadata: Effect.fn("effect-machine.persistence.inMemory.saveMetadata")(function* (metadata) {
|
|
129
|
+
yield* Ref.update(registry, (map) => {
|
|
130
|
+
const newMap = new Map(map);
|
|
131
|
+
newMap.set(metadata.id, metadata);
|
|
132
|
+
return newMap;
|
|
133
|
+
});
|
|
134
|
+
}),
|
|
135
|
+
deleteMetadata: Effect.fn("effect-machine.persistence.inMemory.deleteMetadata")(function* (id) {
|
|
136
|
+
yield* Ref.update(registry, (map) => {
|
|
137
|
+
const newMap = new Map(map);
|
|
138
|
+
newMap.delete(id);
|
|
139
|
+
return newMap;
|
|
140
|
+
});
|
|
141
|
+
}),
|
|
142
|
+
loadMetadata: Effect.fn("effect-machine.persistence.inMemory.loadMetadata")(function* (id) {
|
|
143
|
+
const meta = (yield* Ref.get(registry)).get(id);
|
|
144
|
+
return meta !== void 0 ? Option.some(meta) : Option.none();
|
|
145
|
+
})
|
|
146
|
+
};
|
|
147
|
+
}).pipe(Effect.withSpan("effect-machine.persistence.inMemory.make"));
|
|
148
|
+
/**
|
|
149
|
+
* Create an in-memory persistence adapter effect.
|
|
150
|
+
* Returns the adapter directly for custom layer composition.
|
|
151
|
+
*/
|
|
152
|
+
const makeInMemoryPersistenceAdapter = make;
|
|
153
|
+
/**
|
|
154
|
+
* In-memory persistence adapter layer.
|
|
155
|
+
* Data is not persisted across process restarts.
|
|
156
|
+
*
|
|
157
|
+
* NOTE: Each `Effect.provide(InMemoryPersistenceAdapter)` creates a NEW adapter
|
|
158
|
+
* with empty storage. For tests that need persistent storage across multiple
|
|
159
|
+
* runPromise calls, use `makeInMemoryPersistenceAdapter` with a shared scope.
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```ts
|
|
163
|
+
* const program = Effect.gen(function* () {
|
|
164
|
+
* const system = yield* ActorSystemService;
|
|
165
|
+
* const actor = yield* system.spawn("my-actor", persistentMachine);
|
|
166
|
+
* // ...
|
|
167
|
+
* }).pipe(
|
|
168
|
+
* Effect.provide(InMemoryPersistenceAdapter),
|
|
169
|
+
* Effect.provide(ActorSystemDefault),
|
|
170
|
+
* );
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
const InMemoryPersistenceAdapter = Layer.effect(PersistenceAdapterTag, make);
|
|
174
|
+
|
|
175
|
+
//#endregion
|
|
176
|
+
export { InMemoryPersistenceAdapter, makeInMemoryPersistenceAdapter };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { PersistenceConfig, PersistentMachine, isPersistentMachine, persist } from "./persistent-machine.js";
|
|
2
|
+
import { PersistentActorRef, createPersistentActor, restorePersistentActor } from "./persistent-actor.js";
|
|
3
|
+
import { ActorMetadata, PersistedEvent, PersistenceAdapter, PersistenceAdapterTag, PersistenceError, RestoreFailure, RestoreResult, Snapshot, VersionConflictError } from "./adapter.js";
|
|
4
|
+
import { InMemoryPersistenceAdapter, makeInMemoryPersistenceAdapter } from "./adapters/in-memory.js";
|
|
5
|
+
export { type ActorMetadata, InMemoryPersistenceAdapter, type PersistedEvent, type PersistenceAdapter, PersistenceAdapterTag, type PersistenceConfig, PersistenceError, type PersistentActorRef, type PersistentMachine, type RestoreFailure, type RestoreResult, type Snapshot, VersionConflictError, createPersistentActor, isPersistentMachine, makeInMemoryPersistenceAdapter, persist, restorePersistentActor };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { isPersistentMachine, persist } from "./persistent-machine.js";
|
|
2
|
+
import { PersistenceAdapterTag, PersistenceError, VersionConflictError } from "./adapter.js";
|
|
3
|
+
import { createPersistentActor, restorePersistentActor } from "./persistent-actor.js";
|
|
4
|
+
import { InMemoryPersistenceAdapter, makeInMemoryPersistenceAdapter } from "./adapters/in-memory.js";
|
|
5
|
+
|
|
6
|
+
export { InMemoryPersistenceAdapter, PersistenceAdapterTag, PersistenceError, VersionConflictError, createPersistentActor, isPersistentMachine, makeInMemoryPersistenceAdapter, persist, restorePersistentActor };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { EffectsDef, GuardsDef, MachineContext } from "../slot.js";
|
|
2
|
+
import { PersistentMachine } from "./persistent-machine.js";
|
|
3
|
+
import { PersistedEvent, PersistenceAdapterTag, PersistenceError, Snapshot, VersionConflictError } from "./adapter.js";
|
|
4
|
+
import { MachineRef } from "../machine.js";
|
|
5
|
+
import { ActorRef } from "../actor.js";
|
|
6
|
+
import { Effect, Option, Scope } from "effect";
|
|
7
|
+
|
|
8
|
+
//#region src/persistence/persistent-actor.d.ts
|
|
9
|
+
/**
|
|
10
|
+
* Extended ActorRef with persistence capabilities
|
|
11
|
+
*/
|
|
12
|
+
interface PersistentActorRef<S extends {
|
|
13
|
+
readonly _tag: string;
|
|
14
|
+
}, E extends {
|
|
15
|
+
readonly _tag: string;
|
|
16
|
+
}, R = never> extends ActorRef<S, E> {
|
|
17
|
+
/**
|
|
18
|
+
* Force an immediate snapshot save
|
|
19
|
+
*/
|
|
20
|
+
readonly persist: Effect.Effect<void, PersistenceError | VersionConflictError>;
|
|
21
|
+
/**
|
|
22
|
+
* Get the current persistence version
|
|
23
|
+
*/
|
|
24
|
+
readonly version: Effect.Effect<number>;
|
|
25
|
+
/**
|
|
26
|
+
* Replay events to restore actor to a specific version.
|
|
27
|
+
* Note: This only computes state; does not re-run transition effects.
|
|
28
|
+
*/
|
|
29
|
+
readonly replayTo: (version: number) => Effect.Effect<void, PersistenceError, R>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Create a persistent actor from a PersistentMachine.
|
|
33
|
+
* Restores from existing snapshot if available, otherwise starts fresh.
|
|
34
|
+
*/
|
|
35
|
+
declare const createPersistentActor: <S extends {
|
|
36
|
+
readonly _tag: string;
|
|
37
|
+
}, E extends {
|
|
38
|
+
readonly _tag: string;
|
|
39
|
+
}, R, GD extends GuardsDef = Record<string, never>, EFD extends EffectsDef = Record<string, never>>(id: string, persistentMachine: PersistentMachine<S, E, R>, initialSnapshot: Option.Option<Snapshot<S>>, initialEvents: readonly PersistedEvent<E>[]) => Effect.Effect<PersistentActorRef<S, E, R>, PersistenceError, PersistenceAdapterTag | Exclude<R, MachineContext<S, E, MachineRef<E>>> | Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, Scope.Scope>>;
|
|
40
|
+
/**
|
|
41
|
+
* Restore an actor from persistence.
|
|
42
|
+
* Returns None if no persisted state exists.
|
|
43
|
+
*/
|
|
44
|
+
declare const restorePersistentActor: <S extends {
|
|
45
|
+
readonly _tag: string;
|
|
46
|
+
}, E extends {
|
|
47
|
+
readonly _tag: string;
|
|
48
|
+
}, R>(id: string, persistentMachine: PersistentMachine<S, E, R>) => Effect.Effect<Option.None<PersistentActorRef<S, E, R>> | Option.Some<PersistentActorRef<S, E, R>>, PersistenceError, PersistenceAdapterTag | Exclude<R, MachineContext<S, E, MachineRef<E>>> | Exclude<Exclude<R, MachineContext<S, E, MachineRef<E>>>, Scope.Scope>>;
|
|
49
|
+
//#endregion
|
|
50
|
+
export { PersistentActorRef, createPersistentActor, restorePersistentActor };
|