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