foldkit-sync 0.3.1 → 0.5.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 +823 -266
- package/dist/index.d.mts +348 -184
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +369 -253
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
package/dist/index.d.mts
CHANGED
|
@@ -1,69 +1,99 @@
|
|
|
1
|
-
import { Config, Context, Duration, Effect, Exit, Layer, Metric, PubSub, Schema, Scope, Stream } from "effect";
|
|
1
|
+
import { Config, Context, Duration, Effect, Exit, Layer, Metric, PubSub, Schema as Schema$1, Scope, Stream } from "effect";
|
|
2
|
+
import { AppScope, Contract, MergeConstructors, MergeFields, MessageSet, RunnableApplication, Surface, WritableProjection } from "foldkit-surface";
|
|
2
3
|
import { UrlRequest } from "foldkit/navigation";
|
|
3
4
|
import { Subscriptions } from "foldkit/subscription";
|
|
4
5
|
import * as Url from "foldkit/url";
|
|
5
|
-
import { AppScope, Contract, MergeConstructors, MergeFields, MessageSet, RunnableApplication, Surface, WritableProjection } from "foldkit-surface";
|
|
6
6
|
import { Document, HtmlBuilder } from "foldkit/html";
|
|
7
|
+
//#region src/ids.d.ts
|
|
8
|
+
/** Identifies the replicated document a replica or operation belongs to. */
|
|
9
|
+
export declare const DocumentId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
10
|
+
export type DocumentId = typeof DocumentId.Type;
|
|
11
|
+
/** Identifies one replica (writer identity) within a document. */
|
|
12
|
+
export declare const ReplicaId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
13
|
+
export type ReplicaId = typeof ReplicaId.Type;
|
|
14
|
+
/** The stable identity of one operation within a document. */
|
|
15
|
+
export declare const OpId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/OpId">;
|
|
16
|
+
export type OpId = typeof OpId.Type;
|
|
17
|
+
/**
|
|
18
|
+
* A position in the committed document order. `0` is the start; a committed
|
|
19
|
+
* operation's `serverSequence` and a replica's `cursor` are the same family.
|
|
20
|
+
*/
|
|
21
|
+
export declare const Sequence: Schema$1.brand<Schema$1.Number, "@foldkit-sync/Sequence">;
|
|
22
|
+
export type Sequence = typeof Sequence.Type;
|
|
23
|
+
/** A replica's own operation counter within a document; 1-based. */
|
|
24
|
+
export declare const LocalSequence: Schema$1.brand<Schema$1.Number, "@foldkit-sync/LocalSequence">;
|
|
25
|
+
export type LocalSequence = typeof LocalSequence.Type;
|
|
26
|
+
/**
|
|
27
|
+
* Decode-and-brand helpers. A branded schema constructs the same value with
|
|
28
|
+
* `DocumentId.make('todos')`, which is the spelling to lead with; these stay
|
|
29
|
+
* for callers that already have them.
|
|
30
|
+
*/
|
|
31
|
+
export declare const documentId: (value: string) => DocumentId;
|
|
32
|
+
export declare const replicaId: (value: string) => ReplicaId;
|
|
33
|
+
export declare const opId: (value: string) => OpId;
|
|
34
|
+
export declare const sequence: (value: number) => Sequence;
|
|
35
|
+
export declare const localSequence: (value: number) => LocalSequence;
|
|
36
|
+
//#endregion
|
|
7
37
|
//#region src/errors.d.ts
|
|
8
|
-
declare const StorageError_base: Schema.Class<StorageError, Schema.TaggedStruct<"StorageError", {
|
|
9
|
-
readonly message: Schema.String;
|
|
10
|
-
readonly cause: Schema.optional<Schema.Unknown>;
|
|
38
|
+
declare const StorageError_base: Schema$1.Class<StorageError, Schema$1.TaggedStruct<"StorageError", {
|
|
39
|
+
readonly message: Schema$1.String;
|
|
40
|
+
readonly cause: Schema$1.optional<Schema$1.Unknown>;
|
|
11
41
|
}>, import("effect/Cause").YieldableError>;
|
|
12
42
|
/** The persisted storage could not be read or written. */
|
|
13
43
|
export declare class StorageError extends StorageError_base {}
|
|
14
|
-
declare const UnsupportedClockVersionError_base: Schema.Class<UnsupportedClockVersionError, Schema.TaggedStruct<"UnsupportedClockVersionError", {
|
|
15
|
-
readonly schemaVersion: Schema.NullOr<Schema.Number>;
|
|
16
|
-
readonly message: Schema.String;
|
|
44
|
+
declare const UnsupportedClockVersionError_base: Schema$1.Class<UnsupportedClockVersionError, Schema$1.TaggedStruct<"UnsupportedClockVersionError", {
|
|
45
|
+
readonly schemaVersion: Schema$1.NullOr<Schema$1.Number>;
|
|
46
|
+
readonly message: Schema$1.String;
|
|
17
47
|
}>, import("effect/Cause").YieldableError>;
|
|
18
48
|
/** The stored clock state targets a schema version this build does not support. */
|
|
19
49
|
export declare class UnsupportedClockVersionError extends UnsupportedClockVersionError_base {}
|
|
20
|
-
declare const WrongReplicaStorageError_base: Schema.Class<WrongReplicaStorageError, Schema.TaggedStruct<"WrongReplicaStorageError", {
|
|
21
|
-
readonly documentId: Schema.String;
|
|
22
|
-
readonly replicaId: Schema.String;
|
|
23
|
-
readonly message: Schema.String;
|
|
50
|
+
declare const WrongReplicaStorageError_base: Schema$1.Class<WrongReplicaStorageError, Schema$1.TaggedStruct<"WrongReplicaStorageError", {
|
|
51
|
+
readonly documentId: Schema$1.String;
|
|
52
|
+
readonly replicaId: Schema$1.String;
|
|
53
|
+
readonly message: Schema$1.String;
|
|
24
54
|
}>, import("effect/Cause").YieldableError>;
|
|
25
55
|
/** The storage holds a different replica's state. */
|
|
26
56
|
export declare class WrongReplicaStorageError extends WrongReplicaStorageError_base {}
|
|
27
|
-
declare const InvalidReplicaHistoryError_base: Schema.Class<InvalidReplicaHistoryError, Schema.TaggedStruct<"InvalidReplicaHistoryError", {
|
|
28
|
-
readonly message: Schema.String;
|
|
29
|
-
readonly cause: Schema.optional<Schema.Unknown>;
|
|
57
|
+
declare const InvalidReplicaHistoryError_base: Schema$1.Class<InvalidReplicaHistoryError, Schema$1.TaggedStruct<"InvalidReplicaHistoryError", {
|
|
58
|
+
readonly message: Schema$1.String;
|
|
59
|
+
readonly cause: Schema$1.optional<Schema$1.Unknown>;
|
|
30
60
|
}>, import("effect/Cause").YieldableError>;
|
|
31
61
|
/** The persisted replica history is internally inconsistent. */
|
|
32
62
|
export declare class InvalidReplicaHistoryError extends InvalidReplicaHistoryError_base {}
|
|
33
|
-
declare const InvalidExchangeError_base: Schema.Class<InvalidExchangeError, Schema.TaggedStruct<"InvalidExchangeError", {
|
|
34
|
-
readonly message: Schema.String;
|
|
35
|
-
readonly cause: Schema.optional<Schema.Unknown>;
|
|
63
|
+
declare const InvalidExchangeError_base: Schema$1.Class<InvalidExchangeError, Schema$1.TaggedStruct<"InvalidExchangeError", {
|
|
64
|
+
readonly message: Schema$1.String;
|
|
65
|
+
readonly cause: Schema$1.optional<Schema$1.Unknown>;
|
|
36
66
|
}>, import("effect/Cause").YieldableError>;
|
|
37
67
|
/** The server's exchange response did not match the protocol schema. */
|
|
38
68
|
export declare class InvalidExchangeError extends InvalidExchangeError_base {}
|
|
39
|
-
declare const InvalidOutboxError_base: Schema.Class<InvalidOutboxError, Schema.TaggedStruct<"InvalidOutboxError", {
|
|
40
|
-
readonly message: Schema.String;
|
|
69
|
+
declare const InvalidOutboxError_base: Schema$1.Class<InvalidOutboxError, Schema$1.TaggedStruct<"InvalidOutboxError", {
|
|
70
|
+
readonly message: Schema$1.String;
|
|
41
71
|
}>, import("effect/Cause").YieldableError>;
|
|
42
72
|
/** A persisted pending operation could not have been produced by this replica. */
|
|
43
73
|
export declare class InvalidOutboxError extends InvalidOutboxError_base {}
|
|
44
|
-
declare const CheckpointRegressionError_base: Schema.Class<CheckpointRegressionError, Schema.TaggedStruct<"CheckpointRegressionError", {
|
|
45
|
-
readonly cursor: Schema.Number;
|
|
46
|
-
readonly checkpointCursor: Schema.Number;
|
|
47
|
-
readonly message: Schema.String;
|
|
74
|
+
declare const CheckpointRegressionError_base: Schema$1.Class<CheckpointRegressionError, Schema$1.TaggedStruct<"CheckpointRegressionError", {
|
|
75
|
+
readonly cursor: Schema$1.Number;
|
|
76
|
+
readonly checkpointCursor: Schema$1.Number;
|
|
77
|
+
readonly message: Schema$1.String;
|
|
48
78
|
}>, import("effect/Cause").YieldableError>;
|
|
49
79
|
/** The server sent a checkpoint older than the replica's cursor. */
|
|
50
80
|
export declare class CheckpointRegressionError extends CheckpointRegressionError_base {}
|
|
51
|
-
declare const CommittedOrderError_base: Schema.Class<CommittedOrderError, Schema.TaggedStruct<"CommittedOrderError", {
|
|
52
|
-
readonly expected: Schema.Number;
|
|
53
|
-
readonly actual: Schema.Number;
|
|
54
|
-
readonly message: Schema.String;
|
|
81
|
+
declare const CommittedOrderError_base: Schema$1.Class<CommittedOrderError, Schema$1.TaggedStruct<"CommittedOrderError", {
|
|
82
|
+
readonly expected: Schema$1.Number;
|
|
83
|
+
readonly actual: Schema$1.Number;
|
|
84
|
+
readonly message: Schema$1.String;
|
|
55
85
|
}>, import("effect/Cause").YieldableError>;
|
|
56
86
|
/** Committed operations were not contiguous from the replica's cursor. */
|
|
57
87
|
export declare class CommittedOrderError extends CommittedOrderError_base {}
|
|
58
|
-
declare const ForeignRejectionError_base: Schema.Class<ForeignRejectionError, Schema.TaggedStruct<"ForeignRejectionError", {
|
|
59
|
-
readonly opId: Schema.String;
|
|
60
|
-
readonly message: Schema.String;
|
|
88
|
+
declare const ForeignRejectionError_base: Schema$1.Class<ForeignRejectionError, Schema$1.TaggedStruct<"ForeignRejectionError", {
|
|
89
|
+
readonly opId: Schema$1.String;
|
|
90
|
+
readonly message: Schema$1.String;
|
|
61
91
|
}>, import("effect/Cause").YieldableError>;
|
|
62
92
|
/** The server rejected an operation the replica never sent. */
|
|
63
93
|
export declare class ForeignRejectionError extends ForeignRejectionError_base {}
|
|
64
|
-
declare const ForeignAcknowledgementError_base: Schema.Class<ForeignAcknowledgementError, Schema.TaggedStruct<"ForeignAcknowledgementError", {
|
|
65
|
-
readonly opId: Schema.String;
|
|
66
|
-
readonly message: Schema.String;
|
|
94
|
+
declare const ForeignAcknowledgementError_base: Schema$1.Class<ForeignAcknowledgementError, Schema$1.TaggedStruct<"ForeignAcknowledgementError", {
|
|
95
|
+
readonly opId: Schema$1.String;
|
|
96
|
+
readonly message: Schema$1.String;
|
|
67
97
|
}>, import("effect/Cause").YieldableError>;
|
|
68
98
|
/**
|
|
69
99
|
* The server answered an operation this request never sent, or both
|
|
@@ -71,21 +101,21 @@ declare const ForeignAcknowledgementError_base: Schema.Class<ForeignAcknowledgem
|
|
|
71
101
|
* server delete local work.
|
|
72
102
|
*/
|
|
73
103
|
export declare class ForeignAcknowledgementError extends ForeignAcknowledgementError_base {}
|
|
74
|
-
declare const ReplicaClosedError_base: Schema.Class<ReplicaClosedError, Schema.TaggedStruct<"ReplicaClosedError", {
|
|
75
|
-
readonly message: Schema.String;
|
|
104
|
+
declare const ReplicaClosedError_base: Schema$1.Class<ReplicaClosedError, Schema$1.TaggedStruct<"ReplicaClosedError", {
|
|
105
|
+
readonly message: Schema$1.String;
|
|
76
106
|
}>, import("effect/Cause").YieldableError>;
|
|
77
107
|
/** The replica was closed while work was still being submitted. */
|
|
78
108
|
export declare class ReplicaClosedError extends ReplicaClosedError_base {}
|
|
79
|
-
declare const UnsupportedReplicaVersionError_base: Schema.Class<UnsupportedReplicaVersionError, Schema.TaggedStruct<"UnsupportedReplicaVersionError", {
|
|
80
|
-
readonly protocolVersion: Schema.NullOr<Schema.Number>;
|
|
81
|
-
readonly schemaVersion: Schema.NullOr<Schema.Number>;
|
|
82
|
-
readonly message: Schema.String;
|
|
109
|
+
declare const UnsupportedReplicaVersionError_base: Schema$1.Class<UnsupportedReplicaVersionError, Schema$1.TaggedStruct<"UnsupportedReplicaVersionError", {
|
|
110
|
+
readonly protocolVersion: Schema$1.NullOr<Schema$1.Number>;
|
|
111
|
+
readonly schemaVersion: Schema$1.NullOr<Schema$1.Number>;
|
|
112
|
+
readonly message: Schema$1.String;
|
|
83
113
|
}>, import("effect/Cause").YieldableError>;
|
|
84
114
|
/** The stored replica state targets a protocol or schema version this build does not support. */
|
|
85
115
|
export declare class UnsupportedReplicaVersionError extends UnsupportedReplicaVersionError_base {}
|
|
86
|
-
declare const ReplayError_base: Schema.Class<ReplayError, Schema.TaggedStruct<"ReplayError", {
|
|
87
|
-
readonly message: Schema.String;
|
|
88
|
-
readonly cause: Schema.optional<Schema.Unknown>;
|
|
116
|
+
declare const ReplayError_base: Schema$1.Class<ReplayError, Schema$1.TaggedStruct<"ReplayError", {
|
|
117
|
+
readonly message: Schema$1.String;
|
|
118
|
+
readonly cause: Schema$1.optional<Schema$1.Unknown>;
|
|
89
119
|
}>, import("effect/Cause").YieldableError>;
|
|
90
120
|
/**
|
|
91
121
|
* `replay` threw for a submitted Message, so it could never be applied here or
|
|
@@ -105,34 +135,9 @@ interface Storage {
|
|
|
105
135
|
/** One database per document/replica; CAS prevents two tabs from sharing a writer identity. */
|
|
106
136
|
export declare const indexedDb: (name: string | Config.Config<string>, factory?: IDBFactory | undefined) => Effect.Effect<Storage, StorageError, Scope.Scope>;
|
|
107
137
|
//#endregion
|
|
108
|
-
//#region src/ids.d.ts
|
|
109
|
-
/** Identifies the replicated document a replica or operation belongs to. */
|
|
110
|
-
export declare const DocumentId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
111
|
-
export type DocumentId = typeof DocumentId.Type;
|
|
112
|
-
/** Identifies one replica (writer identity) within a document. */
|
|
113
|
-
export declare const ReplicaId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
114
|
-
export type ReplicaId = typeof ReplicaId.Type;
|
|
115
|
-
/** The stable identity of one operation within a document. */
|
|
116
|
-
export declare const OpId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/OpId">;
|
|
117
|
-
export type OpId = typeof OpId.Type;
|
|
118
|
-
/**
|
|
119
|
-
* A position in the committed document order. `0` is the start; a committed
|
|
120
|
-
* operation's `serverSequence` and a replica's `cursor` are the same family.
|
|
121
|
-
*/
|
|
122
|
-
export declare const Sequence: Schema.brand<Schema.Number, "@foldkit-sync/Sequence">;
|
|
123
|
-
export type Sequence = typeof Sequence.Type;
|
|
124
|
-
/** A replica's own operation counter within a document; 1-based. */
|
|
125
|
-
export declare const LocalSequence: Schema.brand<Schema.Number, "@foldkit-sync/LocalSequence">;
|
|
126
|
-
export type LocalSequence = typeof LocalSequence.Type;
|
|
127
|
-
export declare const documentId: (value: string) => DocumentId;
|
|
128
|
-
export declare const replicaId: (value: string) => ReplicaId;
|
|
129
|
-
export declare const opId: (value: string) => OpId;
|
|
130
|
-
export declare const sequence: (value: number) => Sequence;
|
|
131
|
-
export declare const localSequence: (value: number) => LocalSequence;
|
|
132
|
-
//#endregion
|
|
133
138
|
//#region src/transport.d.ts
|
|
134
|
-
declare const TransportError_base: Schema.Class<TransportError, Schema.TaggedStruct<"SyncTransportError", {
|
|
135
|
-
readonly message: Schema.String;
|
|
139
|
+
declare const TransportError_base: Schema$1.Class<TransportError, Schema$1.TaggedStruct<"SyncTransportError", {
|
|
140
|
+
readonly message: Schema$1.String;
|
|
136
141
|
}>, import("effect/Cause").YieldableError>;
|
|
137
142
|
/** The wire failure of a transport. A refusal is a result, not an error. */
|
|
138
143
|
export declare class TransportError extends TransportError_base {}
|
|
@@ -218,29 +223,29 @@ export declare const layerSocket: (options: SocketOptions) => Layer.Layer<Transp
|
|
|
218
223
|
declare const PROTOCOL_VERSION = 1;
|
|
219
224
|
declare const SCHEMA_VERSION = 1;
|
|
220
225
|
/** A client-authored operation envelope. `message` is the application's encoded Message. */
|
|
221
|
-
declare const OperationSchema: Schema.Struct<{
|
|
222
|
-
readonly protocolVersion: Schema.Literal<1>;
|
|
223
|
-
readonly schemaVersion: Schema.Literal<1>;
|
|
224
|
-
readonly documentId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
225
|
-
readonly replicaId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
226
|
-
readonly localSequence: Schema.brand<Schema.Number, "@foldkit-sync/LocalSequence">;
|
|
227
|
-
readonly opId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/OpId">;
|
|
228
|
-
readonly baseCursor: Schema.brand<Schema.Number, "@foldkit-sync/Sequence">;
|
|
229
|
-
readonly message: Schema.Unknown;
|
|
226
|
+
declare const OperationSchema: Schema$1.Struct<{
|
|
227
|
+
readonly protocolVersion: Schema$1.Literal<1>;
|
|
228
|
+
readonly schemaVersion: Schema$1.Literal<1>;
|
|
229
|
+
readonly documentId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
230
|
+
readonly replicaId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
231
|
+
readonly localSequence: Schema$1.brand<Schema$1.Number, "@foldkit-sync/LocalSequence">;
|
|
232
|
+
readonly opId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/OpId">;
|
|
233
|
+
readonly baseCursor: Schema$1.brand<Schema$1.Number, "@foldkit-sync/Sequence">;
|
|
234
|
+
readonly message: Schema$1.Unknown;
|
|
230
235
|
}>;
|
|
231
236
|
type Operation = typeof OperationSchema.Type;
|
|
232
237
|
/** An operation the server committed, with its authoritative order and actor. */
|
|
233
|
-
declare const CommittedSchema: Schema.Struct<{
|
|
234
|
-
readonly serverSequence: Schema.brand<Schema.Number, "@foldkit-sync/Sequence">;
|
|
235
|
-
readonly actorId: Schema.NonEmptyString;
|
|
236
|
-
readonly protocolVersion: Schema.Literal<1>;
|
|
237
|
-
readonly schemaVersion: Schema.Literal<1>;
|
|
238
|
-
readonly documentId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
239
|
-
readonly replicaId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
240
|
-
readonly localSequence: Schema.brand<Schema.Number, "@foldkit-sync/LocalSequence">;
|
|
241
|
-
readonly opId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/OpId">;
|
|
242
|
-
readonly baseCursor: Schema.brand<Schema.Number, "@foldkit-sync/Sequence">;
|
|
243
|
-
readonly message: Schema.Unknown;
|
|
238
|
+
declare const CommittedSchema: Schema$1.Struct<{
|
|
239
|
+
readonly serverSequence: Schema$1.brand<Schema$1.Number, "@foldkit-sync/Sequence">;
|
|
240
|
+
readonly actorId: Schema$1.NonEmptyString;
|
|
241
|
+
readonly protocolVersion: Schema$1.Literal<1>;
|
|
242
|
+
readonly schemaVersion: Schema$1.Literal<1>;
|
|
243
|
+
readonly documentId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
244
|
+
readonly replicaId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
245
|
+
readonly localSequence: Schema$1.brand<Schema$1.Number, "@foldkit-sync/LocalSequence">;
|
|
246
|
+
readonly opId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/OpId">;
|
|
247
|
+
readonly baseCursor: Schema$1.brand<Schema$1.Number, "@foldkit-sync/Sequence">;
|
|
248
|
+
readonly message: Schema$1.Unknown;
|
|
244
249
|
}>;
|
|
245
250
|
/** Named to avoid colliding with `foldkit-durable`'s `Committed`. */
|
|
246
251
|
type CommittedOperation = typeof CommittedSchema.Type;
|
|
@@ -299,10 +304,24 @@ interface ReplicaStatus {
|
|
|
299
304
|
interface ReplicaSnapshot<Shared> {
|
|
300
305
|
readonly status: ReplicaStatus;
|
|
301
306
|
readonly shared: Shared;
|
|
307
|
+
/** The server-confirmed state the optimistic `shared` is built on. */
|
|
308
|
+
readonly committed: Shared;
|
|
309
|
+
/**
|
|
310
|
+
* The local sequence the next submitted operation takes. Only a successful
|
|
311
|
+
* submit moves it, so a caller that submits one Message at a time can tell
|
|
312
|
+
* from one snapshot whether `shared` already holds it.
|
|
313
|
+
*/
|
|
314
|
+
readonly nextLocalSequence: LocalSequence;
|
|
302
315
|
}
|
|
303
316
|
interface Replica<Message, Shared> {
|
|
304
317
|
/** The optimistic projection: committed state with pending operations replayed. */
|
|
305
318
|
readonly shared: Effect.Effect<Shared>;
|
|
319
|
+
/**
|
|
320
|
+
* The server-confirmed state through `cursor`, with no pending operation
|
|
321
|
+
* applied. A pending edit reaches it only once the server commits it, and a
|
|
322
|
+
* rejected one never does.
|
|
323
|
+
*/
|
|
324
|
+
readonly committed: Effect.Effect<Shared>;
|
|
306
325
|
readonly pending: Effect.Effect<ReadonlyArray<Operation>>;
|
|
307
326
|
readonly cursor: Effect.Effect<Sequence>;
|
|
308
327
|
/**
|
|
@@ -317,6 +336,8 @@ interface Replica<Message, Shared> {
|
|
|
317
336
|
* submit and exchange, so a UI can subscribe once instead of to both.
|
|
318
337
|
*/
|
|
319
338
|
readonly changes: Stream.Stream<ReplicaSnapshot<Shared>>;
|
|
339
|
+
/** The status, `shared`, `committed` and next local sequence, read from one replica state. */
|
|
340
|
+
readonly snapshot: Effect.Effect<ReplicaSnapshot<Shared>>;
|
|
320
341
|
readonly submit: (message: Message) => Effect.Effect<void, ReplicaError>;
|
|
321
342
|
/** Reconciles against the server. The `Transport` service must be provided. */
|
|
322
343
|
readonly synchronize: Effect.Effect<void, ReplicaError | TransportError, Transport>;
|
|
@@ -331,13 +352,13 @@ interface Replica<Message, Shared> {
|
|
|
331
352
|
}
|
|
332
353
|
interface SyncDefinition<Message, Shared, MessageEncoded, SharedEncoded> {
|
|
333
354
|
readonly documentId: DocumentId;
|
|
334
|
-
readonly message: Schema.Codec<Message, MessageEncoded, never, never>;
|
|
335
|
-
readonly shared: Schema.Codec<Shared, SharedEncoded, never, never>;
|
|
355
|
+
readonly message: Schema$1.Codec<Message, MessageEncoded, never, never>;
|
|
356
|
+
readonly shared: Schema$1.Codec<Shared, SharedEncoded, never, never>;
|
|
336
357
|
readonly empty: Shared;
|
|
337
358
|
readonly durable: (message: Message) => boolean;
|
|
338
359
|
readonly replay: (shared: Shared, message: Message) => Shared;
|
|
339
360
|
}
|
|
340
|
-
interface Sync<Message, Shared> {
|
|
361
|
+
interface Sync$1<Message, Shared> {
|
|
341
362
|
readonly documentId: DocumentId;
|
|
342
363
|
/**
|
|
343
364
|
* Low-level wire codecs for adapters and the transport. Most applications use
|
|
@@ -375,13 +396,13 @@ interface JournalContract<Operation, Shared> {
|
|
|
375
396
|
* projection means; the replica owns the local outbox, optimistic projection,
|
|
376
397
|
* and reconciliation as Effects, and never runs a second reducer.
|
|
377
398
|
*/
|
|
378
|
-
export declare const defineSync: <Message, Shared, MessageEncoded, SharedEncoded>(definition: SyncDefinition<Message, Shared, MessageEncoded, SharedEncoded>) => Sync<Message, Shared>;
|
|
399
|
+
export declare const defineSync: <Message, Shared, MessageEncoded, SharedEncoded>(definition: SyncDefinition<Message, Shared, MessageEncoded, SharedEncoded>) => Sync$1<Message, Shared>;
|
|
379
400
|
//#endregion
|
|
380
401
|
//#region src/make.d.ts
|
|
381
402
|
type MessageConstructor<Message> = (...args: never[]) => Message;
|
|
382
403
|
/** The Message union a tuple of constructors produces. */
|
|
383
404
|
type MsgOf<Ms extends readonly unknown[]> = Ms[number] extends ((...args: never[]) => infer M) ? M : never;
|
|
384
|
-
type AppMessage<AppModel, F extends Schema.Struct.Fields, Cases extends Record<string, Schema.Struct.Fields>> = Schema.Schema.Type<AppScope<AppModel, F, Cases>['Message']>;
|
|
405
|
+
type AppMessage<AppModel, F extends Schema$1.Struct.Fields, Cases extends Record<string, Schema$1.Struct.Fields>> = Schema$1.Schema.Type<AppScope<AppModel, F, Cases>['Message']>;
|
|
385
406
|
/** The `_tag` literal of the Message a constructor produces. */
|
|
386
407
|
type TagOf<C> = C extends ((...args: never[]) => infer M) ? M extends {
|
|
387
408
|
readonly _tag: infer T extends string;
|
|
@@ -408,10 +429,10 @@ interface AuthorizeRequest<Principal, Message, Shared> {
|
|
|
408
429
|
* `shared` the authoritative snapshot. A variant without a rule is allowed; the
|
|
409
430
|
* server may still refuse on its own grounds.
|
|
410
431
|
*/
|
|
411
|
-
type AuthorizePolicy<Principal, Fields extends Schema.Struct.Fields, Ms extends readonly unknown[]> = { readonly [K in TagOf<Ms[number]>]?: (request: AuthorizeRequest<Principal, Extract<MsgOf<Ms>, {
|
|
432
|
+
type AuthorizePolicy<Principal, Fields extends Schema$1.Struct.Fields, Ms extends readonly unknown[]> = { readonly [K in TagOf<Ms[number]>]?: (request: AuthorizeRequest<Principal, Extract<MsgOf<Ms>, {
|
|
412
433
|
readonly _tag: K;
|
|
413
|
-
}>, Schema.Struct.Type<Fields>>) => boolean; };
|
|
414
|
-
interface BaseOptions<Principal, Fields extends Schema.Struct.Fields, Ms extends readonly unknown[]> {
|
|
434
|
+
}>, Schema$1.Struct.Type<Fields>>) => boolean; };
|
|
435
|
+
interface BaseOptions<Principal, Fields extends Schema$1.Struct.Fields, Ms extends readonly unknown[]> {
|
|
415
436
|
readonly documentId: DocumentId;
|
|
416
437
|
/** Name for the generated `surface`; defaults to the document id. */
|
|
417
438
|
readonly name?: string;
|
|
@@ -421,11 +442,11 @@ interface BaseOptions<Principal, Fields extends Schema.Struct.Fields, Ms extends
|
|
|
421
442
|
* during admission, replay, and optimistic projection. It is not guarded: the
|
|
422
443
|
* author owns its agreement with `update`.
|
|
423
444
|
*/
|
|
424
|
-
readonly replay?: (shared: Schema.Struct.Type<NoInfer<Fields>>, message: MsgOf<NoInfer<Ms>>) => Schema.Struct.Type<NoInfer<Fields>>;
|
|
445
|
+
readonly replay?: (shared: Schema$1.Struct.Type<NoInfer<Fields>>, message: MsgOf<NoInfer<Ms>>) => Schema$1.Struct.Type<NoInfer<Fields>>;
|
|
425
446
|
/** Authorization the server journal applies before committing; see `AuthorizePolicy`. */
|
|
426
447
|
readonly authorize?: AuthorizePolicy<Principal, NoInfer<Fields>, NoInfer<Ms>>;
|
|
427
448
|
}
|
|
428
|
-
interface MakeOptions<AppModel, Fields extends Schema.Struct.Fields, Subset, Ms extends readonly MessageConstructor<any>[], Principal = unknown> extends BaseOptions<Principal, Fields, Ms> {
|
|
449
|
+
interface MakeOptions<AppModel, Fields extends Schema$1.Struct.Fields, Subset, Ms extends readonly MessageConstructor<any>[], Principal = unknown> extends BaseOptions<Principal, Fields, Ms> {
|
|
429
450
|
/** The writable projection of the shared fields. */
|
|
430
451
|
readonly shared: WritableProjection<AppModel, Fields>;
|
|
431
452
|
/** The Message subset the replica durably records and replays. */
|
|
@@ -437,7 +458,7 @@ interface MakeOptions<AppModel, Fields extends Schema.Struct.Fields, Subset, Ms
|
|
|
437
458
|
* `make`; a field declared twice with a different codec or a tag declared twice
|
|
438
459
|
* is rejected there.
|
|
439
460
|
*/
|
|
440
|
-
interface SyncFragment<AppModel, Fields extends Schema.Struct.Fields, Subset, Ms extends readonly MessageConstructor<any>[]> {
|
|
461
|
+
interface SyncFragment<AppModel, Fields extends Schema$1.Struct.Fields, Subset, Ms extends readonly MessageConstructor<any>[]> {
|
|
441
462
|
readonly shared: WritableProjection<AppModel, Fields>;
|
|
442
463
|
readonly durable: MessageSet<AppModel, any, Subset, Ms>;
|
|
443
464
|
}
|
|
@@ -454,17 +475,21 @@ type FragmentMessages<Fs extends readonly SyncFragment<any, any, any, any>[]> =
|
|
|
454
475
|
* projection, the declared Messages, and a read-only Surface over the
|
|
455
476
|
* projection. Exported so a consumer can name the type.
|
|
456
477
|
*/
|
|
457
|
-
interface DefinedSync<AppModel, Fields extends Schema.Struct.Fields, Message, Ms extends readonly unknown[], Principal = unknown
|
|
458
|
-
|
|
478
|
+
interface DefinedSync<AppModel, Fields extends Schema$1.Struct.Fields, Message, Ms extends readonly unknown[], Principal = unknown,
|
|
479
|
+
/** `true` when `make` was given `authorize`, so the contract's `authorize` is present. */
|
|
480
|
+
Authorized extends boolean = false> extends Sync$1<Message, Schema$1.Struct.Type<Fields>> {
|
|
481
|
+
readonly surface: Surface<AppModel, Schema$1.Struct.Type<Fields>, MsgOf<Ms>, void>;
|
|
459
482
|
readonly projection: WritableProjection<AppModel, Fields>;
|
|
483
|
+
/** The reducer the replica replays durable Messages with: derived from `update`, or `make`'s `replay`. */
|
|
484
|
+
readonly replay: (shared: Schema$1.Struct.Type<Fields>, message: Message) => Schema$1.Struct.Type<Fields>;
|
|
460
485
|
readonly messages: Ms;
|
|
461
486
|
/** For `Module`: this contract owns the shared projection's paths and records the durable tags. */
|
|
462
487
|
readonly contract: Contract;
|
|
463
488
|
/** The durable journal's codecs, reducer, and, when declared, authorization. */
|
|
464
|
-
readonly journalContract: () => PolicyJournalContract<Operation, Schema.Struct.Type<Fields>, Principal>;
|
|
489
|
+
readonly journalContract: () => Authorized extends true ? PolicyJournalContract<Operation, Schema$1.Struct.Type<Fields>, Principal> & Required<Pick<PolicyJournalContract<Operation, Schema$1.Struct.Type<Fields>, Principal>, 'authorize'>> : PolicyJournalContract<Operation, Schema$1.Struct.Type<Fields>, Principal>;
|
|
465
490
|
}
|
|
466
491
|
/** The sync constructors specialized to one application. */
|
|
467
|
-
interface ApplicationSync<AppModel, F extends Schema.Struct.Fields, Cases extends Record<string, Schema.Struct.Fields>, Principal = unknown> {
|
|
492
|
+
interface ApplicationSync<AppModel, F extends Schema$1.Struct.Fields, Cases extends Record<string, Schema$1.Struct.Fields>, Principal = unknown> {
|
|
468
493
|
/**
|
|
469
494
|
* Fixes the `Principal` that `authorize` rules see. A `Principal` cannot be a
|
|
470
495
|
* positional type argument beside an inferred Model, so it is supplied here:
|
|
@@ -472,7 +497,7 @@ interface ApplicationSync<AppModel, F extends Schema.Struct.Fields, Cases extend
|
|
|
472
497
|
*/
|
|
473
498
|
readonly withPrincipal: <P>() => ApplicationSync<AppModel, F, Cases, P>;
|
|
474
499
|
/** One feature's shared fields and durable Messages, for `compose`. */
|
|
475
|
-
readonly fragment: <Fields extends Schema.Struct.Fields, Subset, Ms extends readonly MessageConstructor<AppMessage<AppModel, F, Cases>>[]>(config: {
|
|
500
|
+
readonly fragment: <Fields extends Schema$1.Struct.Fields, Subset, Ms extends readonly MessageConstructor<AppMessage<AppModel, F, Cases>>[]>(config: {
|
|
476
501
|
readonly shared: WritableProjection<AppModel, Fields>;
|
|
477
502
|
readonly durable: MessageSet<AppModel, any, Subset, Ms>;
|
|
478
503
|
}) => SyncFragment<AppModel, Fields, Subset, Ms>;
|
|
@@ -482,7 +507,12 @@ interface ApplicationSync<AppModel, F extends Schema.Struct.Fields, Cases extend
|
|
|
482
507
|
* fragment from another application throws.
|
|
483
508
|
*/
|
|
484
509
|
readonly compose: <const Fs extends readonly SyncFragment<AppModel, any, any, any>[]>(...fragments: Fs) => SyncFragment<AppModel, FragmentFields<Fs>, FragmentMessages<Fs>, FragmentConstructors<Fs>>;
|
|
485
|
-
readonly make:
|
|
510
|
+
readonly make: {
|
|
511
|
+
<Fields extends Schema$1.Struct.Fields, Subset, Ms extends readonly MessageConstructor<AppMessage<AppModel, F, Cases>>[]>(options: MakeOptions<AppModel, Fields, Subset, Ms, Principal> & {
|
|
512
|
+
readonly authorize: AuthorizePolicy<Principal, NoInfer<Fields>, NoInfer<Ms>>;
|
|
513
|
+
}): DefinedSync<AppModel, Fields, AppMessage<AppModel, F, Cases>, Ms, Principal, true>;
|
|
514
|
+
<Fields extends Schema$1.Struct.Fields, Subset, Ms extends readonly MessageConstructor<AppMessage<AppModel, F, Cases>>[]>(options: MakeOptions<AppModel, Fields, Subset, Ms, Principal>): DefinedSync<AppModel, Fields, AppMessage<AppModel, F, Cases>, Ms, Principal>;
|
|
515
|
+
};
|
|
486
516
|
}
|
|
487
517
|
/**
|
|
488
518
|
* Specializes the sync constructors to a `Surface.application`, so
|
|
@@ -492,7 +522,7 @@ interface ApplicationSync<AppModel, F extends Schema.Struct.Fields, Cases extend
|
|
|
492
522
|
* `defineSync` remains the protocol primitive when there is no application to
|
|
493
523
|
* derive from.
|
|
494
524
|
*/
|
|
495
|
-
export declare const forApplication: <AppModel, F extends Schema.Struct.Fields, Cases extends Record<string, Schema.Struct.Fields>>(app: RunnableApplication<AppModel, F, Cases, any>) => ApplicationSync<AppModel, F, Cases>;
|
|
525
|
+
export declare const forApplication: <AppModel, F extends Schema$1.Struct.Fields, Cases extends Record<string, Schema$1.Struct.Fields>>(app: RunnableApplication<AppModel, F, Cases, any>) => ApplicationSync<AppModel, F, Cases>;
|
|
496
526
|
//#endregion
|
|
497
527
|
//#region src/mount.d.ts
|
|
498
528
|
/**
|
|
@@ -524,7 +554,17 @@ interface MountOptions<Model, Message, Shared, Resources> {
|
|
|
524
554
|
*/
|
|
525
555
|
readonly onPersistenceFailure?: ((model: Model, error: ReplicaError) => Model) | undefined;
|
|
526
556
|
}
|
|
527
|
-
|
|
557
|
+
/**
|
|
558
|
+
* The shared slice as the server has confirmed it, outside the Model. It is a
|
|
559
|
+
* source for waiting consumers, the shape `Agent.when({ source })` reads, and
|
|
560
|
+
* not a Projection: it does not render and is not derived from the Model.
|
|
561
|
+
*/
|
|
562
|
+
interface CommittedView<Shared> {
|
|
563
|
+
readonly get: () => Shared;
|
|
564
|
+
/** Told after every exchange, including one that moves no cursor (a checkpoint). */
|
|
565
|
+
readonly subscribe: (listener: () => void) => () => void;
|
|
566
|
+
}
|
|
567
|
+
interface Mounted<Model, Message, Shared = unknown> {
|
|
528
568
|
/** Sends an application Message through the runtime; the `Exit` reports a decode failure. */
|
|
529
569
|
readonly dispatch: (message: Message) => Exit.Exit<void, unknown>;
|
|
530
570
|
/** The Model after the last transition. */
|
|
@@ -537,84 +577,17 @@ interface Mounted<Model, Message> {
|
|
|
537
577
|
* finishes its work. The mount's private Messages are not reported.
|
|
538
578
|
*/
|
|
539
579
|
readonly observe: (listener: (message: Message) => void) => () => void;
|
|
580
|
+
/**
|
|
581
|
+
* The shared slice without pending local edits, for an agent that must not
|
|
582
|
+
* report an optimistic edit as done: `Agent.when({ source: mounted.committed, … })`.
|
|
583
|
+
*/
|
|
584
|
+
readonly committed: CommittedView<Shared>;
|
|
540
585
|
/** Waits for in-flight persists, then disposes the runtime. The replica stays open. */
|
|
541
586
|
readonly dispose: () => Promise<void>;
|
|
542
587
|
}
|
|
543
588
|
/** The application's Message union, as `Surface.application` types it. */
|
|
544
|
-
type MessageOf<App> = App extends RunnableApplication<any, any, any, any> ? Schema.Schema.Type<App['Message']> : never;
|
|
545
|
-
export declare const mount: <Model, F extends Schema.Struct.Fields, Cases extends Record<string, Schema.Struct.Fields>, Fields extends Schema.Struct.Fields, Ms extends readonly unknown[], Resources = never, Principal = unknown>(app: RunnableApplication<Model, F, Cases, Resources>, sync: DefinedSync<Model, Fields, MessageOf<RunnableApplication<Model, F, Cases, Resources>>, Ms, Principal>, options: MountOptions<Model, MessageOf<RunnableApplication<Model, F, Cases, Resources>>, Schema.Struct.Type<Fields>, Resources>) => Mounted<Model, MessageOf<RunnableApplication<Model, F, Cases, Resources
|
|
546
|
-
//#endregion
|
|
547
|
-
//#region src/lww.d.ts
|
|
548
|
-
/** A logical write time; replica ids break concurrent ties using UTF-16 order. */
|
|
549
|
-
declare const Stamp: Schema.Struct<{
|
|
550
|
-
readonly counter: Schema.Number;
|
|
551
|
-
readonly replicaId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
552
|
-
}>;
|
|
553
|
-
declare const ClockState: Schema.Struct<{
|
|
554
|
-
readonly schemaVersion: Schema.Literal<1>;
|
|
555
|
-
readonly documentId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
556
|
-
readonly replicaId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
557
|
-
readonly revision: Schema.Number;
|
|
558
|
-
}>;
|
|
559
|
-
type LwwClockState = typeof ClockState.Type;
|
|
560
|
-
interface LwwClock {
|
|
561
|
-
/** Persists a timestamp beyond the saved counter and supplied observation. */
|
|
562
|
-
next: (observedCounter?: number) => Effect.Effect<typeof Stamp.Type, StorageError>;
|
|
563
|
-
/** Drains accepted allocations, then closes storage. Repeated calls no-op. */
|
|
564
|
-
close: Effect.Effect<void>;
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Opens a durable clock over the caller's storage.
|
|
568
|
-
*
|
|
569
|
-
* Allocations are serialized through a `SynchronizedRef` and each persists before
|
|
570
|
-
* its stamp is returned. A crash after the save may skip a timestamp but never
|
|
571
|
-
* reuse one. Storage is owned by the caller; a failed open closes it so a partial
|
|
572
|
-
* initialization does not leak the connection.
|
|
573
|
-
*/
|
|
574
|
-
export declare const openLwwClock: (options: {
|
|
575
|
-
readonly documentId: DocumentId;
|
|
576
|
-
readonly replicaId: ReplicaId;
|
|
577
|
-
readonly storage: Storage;
|
|
578
|
-
}) => Effect.Effect<{
|
|
579
|
-
next: (observedCounter?: any) => Effect.Effect<{
|
|
580
|
-
readonly counter: number;
|
|
581
|
-
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
582
|
-
}, StorageError, never>;
|
|
583
|
-
close: Effect.Effect<void, never, never>;
|
|
584
|
-
}, StorageError | UnsupportedClockVersionError, never>;
|
|
585
|
-
/**
|
|
586
|
-
* A schema and reducer helper for a last-writer-wins register.
|
|
587
|
-
* Allocate a unique stamp per write before dispatch, and retain it in the Message.
|
|
588
|
-
* Merge is for decoded values; the application's schema validates wire input.
|
|
589
|
-
*/
|
|
590
|
-
export declare const lwwRegister: <Value, Encoded, RD, RE>(value: Schema.Codec<Value, Encoded, RD, RE>) => {
|
|
591
|
-
schema: Schema.Struct<{
|
|
592
|
-
readonly stamp: Schema.Struct<{
|
|
593
|
-
readonly counter: Schema.Number;
|
|
594
|
-
readonly replicaId: Schema.brand<Schema.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
595
|
-
}>;
|
|
596
|
-
readonly value: Schema.Codec<Value, Encoded, RD, RE>;
|
|
597
|
-
}>;
|
|
598
|
-
merge: (left: {
|
|
599
|
-
readonly stamp: {
|
|
600
|
-
readonly counter: number;
|
|
601
|
-
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
602
|
-
};
|
|
603
|
-
readonly value: Value;
|
|
604
|
-
}, right: {
|
|
605
|
-
readonly stamp: {
|
|
606
|
-
readonly counter: number;
|
|
607
|
-
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
608
|
-
};
|
|
609
|
-
readonly value: Value;
|
|
610
|
-
}) => {
|
|
611
|
-
readonly stamp: {
|
|
612
|
-
readonly counter: number;
|
|
613
|
-
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
614
|
-
};
|
|
615
|
-
readonly value: Value;
|
|
616
|
-
};
|
|
617
|
-
};
|
|
589
|
+
type MessageOf<App> = App extends RunnableApplication<any, any, any, any> ? Schema$1.Schema.Type<App['Message']> : never;
|
|
590
|
+
export declare const mount: <Model, F extends Schema$1.Struct.Fields, Cases extends Record<string, Schema$1.Struct.Fields>, Fields extends Schema$1.Struct.Fields, Ms extends readonly unknown[], Resources = never, Principal = unknown>(app: RunnableApplication<Model, F, Cases, Resources>, sync: DefinedSync<Model, Fields, MessageOf<RunnableApplication<Model, F, Cases, Resources>>, Ms, Principal>, options: MountOptions<Model, MessageOf<RunnableApplication<Model, F, Cases, Resources>>, Schema$1.Struct.Type<Fields>, Resources>) => Mounted<Model, MessageOf<RunnableApplication<Model, F, Cases, Resources>>, Schema$1.Struct.Type<Fields>>;
|
|
618
591
|
//#endregion
|
|
619
592
|
//#region src/presence.d.ts
|
|
620
593
|
/** One peer's presence. A `null` value means the peer left. */
|
|
@@ -718,5 +691,196 @@ export declare const servePresence: <Update>(socket: SocketLike, hub: PresenceHu
|
|
|
718
691
|
readonly peerId: string;
|
|
719
692
|
}) => (() => void);
|
|
720
693
|
//#endregion
|
|
721
|
-
|
|
694
|
+
//#region src/lww.d.ts
|
|
695
|
+
/** A logical write time; replica ids break concurrent ties using UTF-16 order. */
|
|
696
|
+
declare const Stamp: Schema$1.Struct<{
|
|
697
|
+
readonly counter: Schema$1.Number;
|
|
698
|
+
readonly replicaId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
699
|
+
}>;
|
|
700
|
+
declare const ClockState: Schema$1.Struct<{
|
|
701
|
+
readonly schemaVersion: Schema$1.Literal<1>;
|
|
702
|
+
readonly documentId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/DocumentId">;
|
|
703
|
+
readonly replicaId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
704
|
+
readonly revision: Schema$1.Number;
|
|
705
|
+
}>;
|
|
706
|
+
type LwwClockState = typeof ClockState.Type;
|
|
707
|
+
interface LwwClock {
|
|
708
|
+
/** Persists a timestamp beyond the saved counter and supplied observation. */
|
|
709
|
+
next: (observedCounter?: number) => Effect.Effect<typeof Stamp.Type, StorageError>;
|
|
710
|
+
/** Drains accepted allocations, then closes storage. Repeated calls no-op. */
|
|
711
|
+
close: Effect.Effect<void>;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Opens a durable clock over the caller's storage.
|
|
715
|
+
*
|
|
716
|
+
* Allocations are serialized through a `SynchronizedRef` and each persists before
|
|
717
|
+
* its stamp is returned. A crash after the save may skip a timestamp but never
|
|
718
|
+
* reuse one. Storage is owned by the caller; a failed open closes it so a partial
|
|
719
|
+
* initialization does not leak the connection.
|
|
720
|
+
*/
|
|
721
|
+
export declare const openLwwClock: (options: {
|
|
722
|
+
readonly documentId: DocumentId;
|
|
723
|
+
readonly replicaId: ReplicaId;
|
|
724
|
+
readonly storage: Storage;
|
|
725
|
+
}) => Effect.Effect<{
|
|
726
|
+
next: (observedCounter?: any) => Effect.Effect<{
|
|
727
|
+
readonly counter: number;
|
|
728
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
729
|
+
}, StorageError, never>;
|
|
730
|
+
close: Effect.Effect<void, never, never>;
|
|
731
|
+
}, StorageError | UnsupportedClockVersionError, never>;
|
|
732
|
+
/**
|
|
733
|
+
* A schema and reducer helper for a last-writer-wins register.
|
|
734
|
+
* Allocate a unique stamp per write before dispatch, and retain it in the Message.
|
|
735
|
+
* Merge is for decoded values; the application's schema validates wire input.
|
|
736
|
+
*/
|
|
737
|
+
export declare const lwwRegister: <Value, Encoded, RD, RE>(value: Schema$1.Codec<Value, Encoded, RD, RE>) => {
|
|
738
|
+
schema: Schema$1.Struct<{
|
|
739
|
+
readonly stamp: Schema$1.Struct<{
|
|
740
|
+
readonly counter: Schema$1.Number;
|
|
741
|
+
readonly replicaId: Schema$1.brand<Schema$1.NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
742
|
+
}>;
|
|
743
|
+
readonly value: Schema$1.Codec<Value, Encoded, RD, RE>;
|
|
744
|
+
}>;
|
|
745
|
+
merge: (left: {
|
|
746
|
+
readonly stamp: {
|
|
747
|
+
readonly counter: number;
|
|
748
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
749
|
+
};
|
|
750
|
+
readonly value: Value;
|
|
751
|
+
}, right: {
|
|
752
|
+
readonly stamp: {
|
|
753
|
+
readonly counter: number;
|
|
754
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
755
|
+
};
|
|
756
|
+
readonly value: Value;
|
|
757
|
+
}) => {
|
|
758
|
+
readonly stamp: {
|
|
759
|
+
readonly counter: number;
|
|
760
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
761
|
+
};
|
|
762
|
+
readonly value: Value;
|
|
763
|
+
};
|
|
764
|
+
};
|
|
765
|
+
//#endregion
|
|
766
|
+
//#region src/index.d.ts
|
|
767
|
+
/**
|
|
768
|
+
* The contract `Sync.define` and `Sync.forApplication(App).make` produce: the
|
|
769
|
+
* wire codecs, the server journal's contract, and `openReplica`.
|
|
770
|
+
*
|
|
771
|
+
* This is the type; `Sync` is also the value namespace below, the way
|
|
772
|
+
* `Effect.Effect` is both.
|
|
773
|
+
*/
|
|
774
|
+
export type Sync<Message, Shared> = Sync$1<Message, Shared>;
|
|
775
|
+
/** The package's public surface, grouped. Each entry is the exported function itself. */
|
|
776
|
+
export declare const Sync: {
|
|
777
|
+
/** Derives a sync contract from a Foldkit application: `Sync.forApplication(App).make({…})`. */
|
|
778
|
+
forApplication: <AppModel, F extends import("effect/Schema").Struct.Fields, Cases extends Record<string, import("effect/Schema").Struct.Fields>>(app: import("foldkit-surface").RunnableApplication<AppModel, F, Cases, any>) => ApplicationSync<AppModel, F, Cases>;
|
|
779
|
+
/** The protocol primitive, for a client with no Foldkit application to derive from. */
|
|
780
|
+
define: <Message, Shared, MessageEncoded, SharedEncoded>(definition: SyncDefinition<Message, Shared, MessageEncoded, SharedEncoded>) => Sync$1<Message, Shared>;
|
|
781
|
+
/** Runs a Foldkit application over an open replica with one reducer. */
|
|
782
|
+
mount: <Model, F extends import("effect/Schema").Struct.Fields, Cases extends Record<string, import("effect/Schema").Struct.Fields>, Fields extends import("effect/Schema").Struct.Fields, Ms extends readonly unknown[], Resources = never, Principal = unknown>(app: import("foldkit-surface").RunnableApplication<Model, F, Cases, Resources>, sync: DefinedSync<Model, Fields, import("effect/Schema").Schema.Type<import("foldkit/message").MessageUnion<Cases>>, Ms, Principal>, options: MountOptions<Model, import("effect/Schema").Schema.Type<import("foldkit/message").MessageUnion<Cases>>, import("effect/Schema").Struct.Type<Fields>, Resources>) => Mounted<Model, import("effect/Schema").Schema.Type<import("foldkit/message").MessageUnion<Cases>>, import("effect/Schema").Struct.Type<Fields>>;
|
|
783
|
+
/** Counters and a histogram an application can scrape. */
|
|
784
|
+
metrics: {
|
|
785
|
+
exchanges: import("effect/Metric").Counter<number>;
|
|
786
|
+
applied: import("effect/Metric").Counter<number>;
|
|
787
|
+
checkpoints: import("effect/Metric").Counter<number>;
|
|
788
|
+
exchangePending: import("effect/Metric").Histogram<number>;
|
|
789
|
+
};
|
|
790
|
+
/** The only storage adapter: one IndexedDB database per document/replica. */
|
|
791
|
+
indexedDb: (name: string | import("effect/Config").Config<string>, factory?: IDBFactory | undefined) => import("effect/Effect").Effect<Storage, StorageError, import("effect/Scope").Scope>;
|
|
792
|
+
/** The `Transport` service's layers, and the bridges to and from a promise client. */
|
|
793
|
+
transport: {
|
|
794
|
+
/** A reconnecting WebSocket client layer. */
|
|
795
|
+
socket: (options: SocketOptions) => import("effect/Layer").Layer<Transport, TransportError>;
|
|
796
|
+
/** An in-process layer, for tests and single-process demos. */
|
|
797
|
+
loopback: (handler: (cursor: number, pending: ReadonlyArray<Operation>) => unknown | Promise<unknown>) => import("effect/Layer").Layer<Transport>;
|
|
798
|
+
/** Wraps the promise-based client the replica already speaks. */
|
|
799
|
+
fromPromise: (transport: TransportClient) => import("effect/Layer").Layer<Transport>;
|
|
800
|
+
/** Bridges the service back to that promise client. */
|
|
801
|
+
toPromise: (transport: TransportShape) => TransportClient;
|
|
802
|
+
/** The server side of one accepted socket connection. */
|
|
803
|
+
serve: (socket: SocketLike, options: {
|
|
804
|
+
readonly exchange: (cursor: number, pending: ReadonlyArray<Operation>) => unknown | Promise<unknown>;
|
|
805
|
+
}) => (() => void);
|
|
806
|
+
/** The default socket factory: the platform `WebSocket`. */
|
|
807
|
+
nativeSocket: (url: string) => SocketLike;
|
|
808
|
+
};
|
|
809
|
+
/** An ephemeral, TTL'd peer registry, deliberately outside the durable log. */
|
|
810
|
+
presence: {
|
|
811
|
+
make: <Update>(options: PresenceOptions<Update>) => import("effect/Effect").Effect<{
|
|
812
|
+
set: (value: Update) => import("effect/Effect").Effect<void, never, never>;
|
|
813
|
+
leave: import("effect/Effect").Effect<void, never, never>;
|
|
814
|
+
peers: import("effect/Effect").Effect<{
|
|
815
|
+
id: string;
|
|
816
|
+
value: Update;
|
|
817
|
+
updatedAt: number;
|
|
818
|
+
}[], never, never>;
|
|
819
|
+
prune: import("effect/Effect").Effect<void, never, never>;
|
|
820
|
+
subscribe: (listener: () => void) => () => boolean;
|
|
821
|
+
changes: import("effect/Stream").Stream<{
|
|
822
|
+
id: string;
|
|
823
|
+
value: Update;
|
|
824
|
+
updatedAt: number;
|
|
825
|
+
}[], never, never>;
|
|
826
|
+
close: import("effect/Effect").Effect<void, never, never>;
|
|
827
|
+
}, never, import("effect/Scope").Scope>;
|
|
828
|
+
/** Broadcasts presence updates among connected peers, on the server. */
|
|
829
|
+
hub: <Update>() => PresenceHub<Update>;
|
|
830
|
+
/** Serves presence frames on an accepted socket, stamping the connection's identity. */
|
|
831
|
+
serve: <Update>(socket: SocketLike, hub: PresenceHub<Update>, options: {
|
|
832
|
+
readonly peerId: string;
|
|
833
|
+
}) => (() => void);
|
|
834
|
+
socketChannel: <Update>(socket: SocketLike) => import("effect/Effect").Effect<PresenceChannel<Update>, never, import("effect/Scope").Scope>;
|
|
835
|
+
loopbackChannel: <Update>() => import("effect/Effect").Effect<{
|
|
836
|
+
updates: import("effect/PubSub").PubSub<PresenceUpdate<Update>>;
|
|
837
|
+
publish: (update: PresenceUpdate<Update>) => import("effect/Effect").Effect<boolean, never, never>;
|
|
838
|
+
}, never, never>;
|
|
839
|
+
};
|
|
840
|
+
/** Last-writer-wins registers (experimental): a per-field merge on logical time. */
|
|
841
|
+
lww: {
|
|
842
|
+
register: <Value, Encoded, RD, RE>(value: import("effect/Schema").Codec<Value, Encoded, RD, RE>) => {
|
|
843
|
+
schema: import("effect/Schema").Struct<{
|
|
844
|
+
readonly stamp: import("effect/Schema").Struct<{
|
|
845
|
+
readonly counter: import("effect/Schema").Number;
|
|
846
|
+
readonly replicaId: import("effect/Schema").brand<import("effect/Schema").NonEmptyString, "@foldkit-sync/ReplicaId">;
|
|
847
|
+
}>;
|
|
848
|
+
readonly value: import("effect/Schema").Codec<Value, Encoded, RD, RE>;
|
|
849
|
+
}>;
|
|
850
|
+
merge: (left: {
|
|
851
|
+
readonly stamp: {
|
|
852
|
+
readonly counter: number;
|
|
853
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
854
|
+
};
|
|
855
|
+
readonly value: Value;
|
|
856
|
+
}, right: {
|
|
857
|
+
readonly stamp: {
|
|
858
|
+
readonly counter: number;
|
|
859
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
860
|
+
};
|
|
861
|
+
readonly value: Value;
|
|
862
|
+
}) => {
|
|
863
|
+
readonly stamp: {
|
|
864
|
+
readonly counter: number;
|
|
865
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
866
|
+
};
|
|
867
|
+
readonly value: Value;
|
|
868
|
+
};
|
|
869
|
+
};
|
|
870
|
+
/** The durable counter a stamp is allocated from, before dispatch. */
|
|
871
|
+
openClock: (options: {
|
|
872
|
+
readonly documentId: DocumentId;
|
|
873
|
+
readonly replicaId: ReplicaId;
|
|
874
|
+
readonly storage: Storage;
|
|
875
|
+
}) => import("effect/Effect").Effect<{
|
|
876
|
+
next: (observedCounter?: any) => import("effect/Effect").Effect<{
|
|
877
|
+
readonly counter: number;
|
|
878
|
+
readonly replicaId: string & import("effect/Brand").Brand<"@foldkit-sync/ReplicaId">;
|
|
879
|
+
}, StorageError, never>;
|
|
880
|
+
close: import("effect/Effect").Effect<void, never, never>;
|
|
881
|
+
}, StorageError | UnsupportedClockVersionError, never>;
|
|
882
|
+
};
|
|
883
|
+
};
|
|
884
|
+
//#endregion
|
|
885
|
+
export type { ApplicationSync, AuthorizePolicy, AuthorizeRequest, Checkpoint, CommittedOperation, CommittedView, DefinedSync, Exchange, ExchangeFrame, ExchangeReply, FragmentMessages, JournalContract, LwwClock, LwwClockState, MakeOptions, MountOptions, MountUrl, Mounted, MsgOf, Operation, PolicyJournalContract, Presence, PresenceChannel, PresenceHub, PresenceOptions, PresencePeer, PresenceUpdate, Replica, ReplicaError, ReplicaSnapshot, ReplicaState, ReplicaStatus, SocketLike, SocketOptions, Storage, SyncDefinition, SyncFragment, TransportClient, TransportShape };
|
|
722
886
|
//# sourceMappingURL=index.d.mts.map
|