foldkit-sync 0.7.0 → 0.9.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 +543 -893
- package/dist/entity.d.mts +231 -0
- package/dist/entity.d.mts.map +1 -0
- package/dist/entity.mjs +204 -0
- package/dist/entity.mjs.map +1 -0
- package/dist/index.d.mts +51 -485
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +132 -331
- package/dist/index.mjs.map +1 -1
- package/dist/journal.d.mts +132 -0
- package/dist/journal.d.mts.map +1 -0
- package/dist/journal.mjs +204 -0
- package/dist/journal.mjs.map +1 -0
- package/dist/sync-CP5UGs4r.d.mts +547 -0
- package/dist/sync-CP5UGs4r.d.mts.map +1 -0
- package/dist/transport-hlEQpHIv.mjs +359 -0
- package/dist/transport-hlEQpHIv.mjs.map +1 -0
- package/package.json +25 -8
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { _ as opId, a as layerSocket, c as serveSocket, d as LocalSequence, f as OpId, g as localSequence, h as documentId, i as layerLoopback, l as toPromise, m as Sequence, n as TransportError, o as nativeSocket, p as ReplicaId, r as layerFromPromise, s as portSocket, t as Transport, u as DocumentId, v as replicaId, y as sequence } from "./transport-hlEQpHIv.mjs";
|
|
2
|
+
import { Clock, Duration, Effect, Equal, Exit, Fiber, Metric, Option, Predicate, PubSub, Queue, Ref, Schema, Stream, SynchronizedRef } from "effect";
|
|
2
3
|
import { MessageSet, Projection, Surface } from "foldkit-surface";
|
|
3
4
|
import { absurd } from "effect/Function";
|
|
4
5
|
import * as Navigation from "foldkit/navigation";
|
|
@@ -173,31 +174,6 @@ const indexedDb = Effect.fn("Storage.indexedDb")(function* (name, factory = glob
|
|
|
173
174
|
};
|
|
174
175
|
});
|
|
175
176
|
//#endregion
|
|
176
|
-
//#region src/ids.ts
|
|
177
|
-
/** Identifies the replicated document a replica or operation belongs to. */
|
|
178
|
-
const DocumentId = Schema.NonEmptyString.pipe(Schema.brand("@foldkit-sync/DocumentId"));
|
|
179
|
-
/** Identifies one replica (writer identity) within a document. */
|
|
180
|
-
const ReplicaId = Schema.NonEmptyString.pipe(Schema.brand("@foldkit-sync/ReplicaId"));
|
|
181
|
-
/** The stable identity of one operation within a document. */
|
|
182
|
-
const OpId = Schema.NonEmptyString.pipe(Schema.brand("@foldkit-sync/OpId"));
|
|
183
|
-
/**
|
|
184
|
-
* A position in the committed document order. `0` is the start; a committed
|
|
185
|
-
* operation's `serverSequence` and a replica's `cursor` are the same family.
|
|
186
|
-
*/
|
|
187
|
-
const Sequence = Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(0), Schema.isLessThanOrEqualTo(Number.MAX_SAFE_INTEGER)).pipe(Schema.brand("@foldkit-sync/Sequence"));
|
|
188
|
-
/** A replica's own operation counter within a document; 1-based. */
|
|
189
|
-
const LocalSequence = Schema.Number.check(Schema.isInt(), Schema.isGreaterThanOrEqualTo(1), Schema.isLessThanOrEqualTo(Number.MAX_SAFE_INTEGER)).pipe(Schema.brand("@foldkit-sync/LocalSequence"));
|
|
190
|
-
/**
|
|
191
|
-
* Decode-and-brand helpers. A branded schema constructs the same value with
|
|
192
|
-
* `DocumentId.make('todos')`, which is the spelling to lead with; these stay
|
|
193
|
-
* for callers that already have them.
|
|
194
|
-
*/
|
|
195
|
-
const documentId = (value) => Schema.decodeUnknownSync(DocumentId)(value);
|
|
196
|
-
const replicaId = (value) => Schema.decodeUnknownSync(ReplicaId)(value);
|
|
197
|
-
const opId = (value) => Schema.decodeUnknownSync(OpId)(value);
|
|
198
|
-
const sequence = (value) => Schema.decodeUnknownSync(Sequence)(value);
|
|
199
|
-
const localSequence = (value) => Schema.decodeUnknownSync(LocalSequence)(value);
|
|
200
|
-
//#endregion
|
|
201
177
|
//#region src/lww.ts
|
|
202
178
|
/** A logical write time; replica ids break concurrent ties using UTF-16 order. */
|
|
203
179
|
const Stamp = Schema.Struct({
|
|
@@ -314,292 +290,6 @@ const lwwRegister = (value) => {
|
|
|
314
290
|
};
|
|
315
291
|
};
|
|
316
292
|
//#endregion
|
|
317
|
-
//#region src/transport.ts
|
|
318
|
-
/** The wire failure of a transport. A refusal is a result, not an error. */
|
|
319
|
-
var TransportError = class extends Schema.TaggedError()("SyncTransportError", { message: Schema.String }) {};
|
|
320
|
-
/**
|
|
321
|
-
* The seam the replica syncs through.
|
|
322
|
-
*
|
|
323
|
-
* A service, not a concrete client, so an application substitutes an in-process
|
|
324
|
-
* transport, a socket, or a test double without the replica knowing. A refusal
|
|
325
|
-
* arrives in the exchange result; only a wire failure is a `TransportError`.
|
|
326
|
-
*/
|
|
327
|
-
var Transport = class extends Context.Service()("foldkit-sync/Transport") {};
|
|
328
|
-
const failure = (error) => new TransportError({ message: error instanceof Error ? error.message : String(error) });
|
|
329
|
-
/** A transport backed by an in-process handler, for tests and single-process demos. */
|
|
330
|
-
const layerLoopback = (handler) => Layer.succeed(Transport, { exchange: (cursor, pending, epoch) => Effect.tryPromise({
|
|
331
|
-
try: () => Promise.resolve(handler(cursor, pending, epoch)),
|
|
332
|
-
catch: failure
|
|
333
|
-
}) });
|
|
334
|
-
/** Wraps the promise-based client the replica already speaks. */
|
|
335
|
-
const layerFromPromise = (transport) => Layer.succeed(Transport, { exchange: (cursor, pending, epoch) => Effect.tryPromise({
|
|
336
|
-
try: () => transport.exchange(sequence(cursor), pending, epoch),
|
|
337
|
-
catch: failure
|
|
338
|
-
}) });
|
|
339
|
-
/** Bridges the service back to the promise client the replica consumes. */
|
|
340
|
-
const toPromise = (transport) => ({ exchange: (cursor, pending, epoch) => Effect.runPromise(transport.exchange(cursor, pending, epoch)) });
|
|
341
|
-
/**
|
|
342
|
-
* Serves one accepted socket, answering each exchange frame.
|
|
343
|
-
*
|
|
344
|
-
* The counterpart to `layerSocket`: the handler returns the exchange result, or
|
|
345
|
-
* throws and the failure is written back as an error frame. With `changes`, a
|
|
346
|
-
* subscription to the document's commits, the socket is also sent a
|
|
347
|
-
* `NotifyFrame` after each, so a client hears of others' edits without polling.
|
|
348
|
-
* Returns a function that stops serving.
|
|
349
|
-
*/
|
|
350
|
-
const serveSocket = (socket, options) => {
|
|
351
|
-
const send = (reply) => {
|
|
352
|
-
try {
|
|
353
|
-
socket.send(JSON.stringify(reply));
|
|
354
|
-
} catch {}
|
|
355
|
-
};
|
|
356
|
-
const stopMessage = socket.onMessage((data) => {
|
|
357
|
-
let parsed;
|
|
358
|
-
try {
|
|
359
|
-
parsed = JSON.parse(data);
|
|
360
|
-
} catch {
|
|
361
|
-
return;
|
|
362
|
-
}
|
|
363
|
-
if (typeof parsed !== "object" || parsed === null) return;
|
|
364
|
-
const { id, cursor, pending, epoch } = parsed;
|
|
365
|
-
if (typeof id !== "string") return;
|
|
366
|
-
if (typeof cursor !== "number" || !Number.isSafeInteger(cursor) || cursor < 0 || !Array.isArray(pending) || epoch !== void 0 && typeof epoch !== "string") {
|
|
367
|
-
send({
|
|
368
|
-
id,
|
|
369
|
-
error: "Invalid exchange frame"
|
|
370
|
-
});
|
|
371
|
-
return;
|
|
372
|
-
}
|
|
373
|
-
const frame = {
|
|
374
|
-
id,
|
|
375
|
-
cursor,
|
|
376
|
-
pending,
|
|
377
|
-
epoch: typeof epoch === "string" ? epoch : void 0
|
|
378
|
-
};
|
|
379
|
-
(async () => {
|
|
380
|
-
try {
|
|
381
|
-
const result = await options.exchange(frame.cursor, frame.pending, frame.epoch);
|
|
382
|
-
send({
|
|
383
|
-
id: frame.id,
|
|
384
|
-
result
|
|
385
|
-
});
|
|
386
|
-
} catch (error) {
|
|
387
|
-
send({
|
|
388
|
-
id: frame.id,
|
|
389
|
-
error: error instanceof Error ? error.message : String(error)
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
})();
|
|
393
|
-
});
|
|
394
|
-
const stopChanges = options.changes?.(() => send({ notify: true }));
|
|
395
|
-
const stop = () => {
|
|
396
|
-
stopMessage();
|
|
397
|
-
stopChanges?.();
|
|
398
|
-
};
|
|
399
|
-
const stopClose = socket.onClose(stop);
|
|
400
|
-
return () => {
|
|
401
|
-
stop();
|
|
402
|
-
stopClose();
|
|
403
|
-
};
|
|
404
|
-
};
|
|
405
|
-
/** The default socket factory: the platform `WebSocket`, as a `SocketLike`. */
|
|
406
|
-
const nativeSocket = (url) => {
|
|
407
|
-
const socket = new WebSocket(url);
|
|
408
|
-
return {
|
|
409
|
-
send: (data) => socket.send(data),
|
|
410
|
-
close: () => socket.close(),
|
|
411
|
-
onOpen: (listener) => {
|
|
412
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
413
|
-
listener();
|
|
414
|
-
return () => {};
|
|
415
|
-
}
|
|
416
|
-
socket.addEventListener("open", listener);
|
|
417
|
-
return () => socket.removeEventListener("open", listener);
|
|
418
|
-
},
|
|
419
|
-
onMessage: (listener) => {
|
|
420
|
-
const handler = (event) => listener(String(event.data));
|
|
421
|
-
socket.addEventListener("message", handler);
|
|
422
|
-
return () => socket.removeEventListener("message", handler);
|
|
423
|
-
},
|
|
424
|
-
onClose: (listener) => {
|
|
425
|
-
const handler = () => listener();
|
|
426
|
-
socket.addEventListener("close", handler);
|
|
427
|
-
return () => socket.removeEventListener("close", handler);
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
};
|
|
431
|
-
/**
|
|
432
|
-
* A WebSocket client transport.
|
|
433
|
-
*
|
|
434
|
-
* One connection is live at a time. While it is connecting, exchanges queue; if
|
|
435
|
-
* it closes, the transport reconnects on an exponential, jittered backoff and
|
|
436
|
-
* re-sends every queued and in-flight frame with its original id, so a lost
|
|
437
|
-
* reply is answered rather than dropped and a late reply cannot resolve a newer
|
|
438
|
-
* frame. It never stops reconnecting: after `maxRetries` consecutive failures
|
|
439
|
-
* queued work fails with a `TransportError`, and exchanges fail fast while no
|
|
440
|
-
* socket is up, until a socket opens. The count resets only when a connection
|
|
441
|
-
* proves healthy, by answering a frame or by staying open for `maxRetryDelay`:
|
|
442
|
-
* a server that accepts a socket and drops it at once is backed off like one
|
|
443
|
-
* that refuses it. A queue over `maxQueue` fails new
|
|
444
|
-
* exchanges with backpressure. The socket is released when the layer's scope
|
|
445
|
-
* ends.
|
|
446
|
-
*/
|
|
447
|
-
const layerSocket = (options) => Layer.effect(Transport, Effect.fn("Transport.layerSocket")(function* () {
|
|
448
|
-
const makeSocket = options.makeSocket ?? nativeSocket;
|
|
449
|
-
const maxQueue = options.maxQueue ?? 64;
|
|
450
|
-
const maxRetries = options.maxRetries ?? 5;
|
|
451
|
-
const retryBase = Duration.toMillis(Duration.fromInputUnsafe(options.retryBase ?? "50 millis"));
|
|
452
|
-
const maxRetryDelay = Duration.toMillis(Duration.fromInputUnsafe(options.maxRetryDelay ?? "5 seconds"));
|
|
453
|
-
const clock = yield* Clock.Clock;
|
|
454
|
-
const notices = yield* PubSub.sliding(1);
|
|
455
|
-
const listeners = /* @__PURE__ */ new Set();
|
|
456
|
-
const shared = {
|
|
457
|
-
send: (data) => {
|
|
458
|
-
if (ready) socket?.send(data);
|
|
459
|
-
},
|
|
460
|
-
close: () => {},
|
|
461
|
-
onMessage: (listener) => {
|
|
462
|
-
listeners.add(listener);
|
|
463
|
-
return () => listeners.delete(listener);
|
|
464
|
-
},
|
|
465
|
-
onClose: () => () => {}
|
|
466
|
-
};
|
|
467
|
-
const queued = [];
|
|
468
|
-
const inFlight = /* @__PURE__ */ new Map();
|
|
469
|
-
let socket;
|
|
470
|
-
let ready = false;
|
|
471
|
-
let nextId = 0;
|
|
472
|
-
let disposed = false;
|
|
473
|
-
let failures = 0;
|
|
474
|
-
const healthy = () => {
|
|
475
|
-
failures = 0;
|
|
476
|
-
};
|
|
477
|
-
const send = (entry) => {
|
|
478
|
-
inFlight.set(entry.id, entry);
|
|
479
|
-
socket?.send(JSON.stringify({
|
|
480
|
-
id: entry.id,
|
|
481
|
-
cursor: entry.cursor,
|
|
482
|
-
pending: entry.pending,
|
|
483
|
-
...entry.epoch === void 0 ? {} : { epoch: entry.epoch }
|
|
484
|
-
}));
|
|
485
|
-
};
|
|
486
|
-
const flush = () => {
|
|
487
|
-
if (socket === void 0 || !ready) return;
|
|
488
|
-
for (const entry of queued.splice(0)) send(entry);
|
|
489
|
-
};
|
|
490
|
-
const requeue = () => {
|
|
491
|
-
queued.unshift(...inFlight.values());
|
|
492
|
-
inFlight.clear();
|
|
493
|
-
};
|
|
494
|
-
const failAll = (message) => {
|
|
495
|
-
const entries = [...queued.splice(0), ...inFlight.values()];
|
|
496
|
-
inFlight.clear();
|
|
497
|
-
for (const entry of entries) entry.resume(Effect.fail(new TransportError({ message })));
|
|
498
|
-
};
|
|
499
|
-
const connect = Effect.tryPromise({
|
|
500
|
-
try: () => new Promise((resolve, reject) => {
|
|
501
|
-
const next = makeSocket(options.url);
|
|
502
|
-
socket = next;
|
|
503
|
-
ready = next.onOpen === void 0;
|
|
504
|
-
let openedAt = ready ? clock.currentTimeMillisUnsafe() : void 0;
|
|
505
|
-
const opened = () => {
|
|
506
|
-
PubSub.publishUnsafe(notices, void 0);
|
|
507
|
-
};
|
|
508
|
-
const offOpen = next.onOpen?.(() => {
|
|
509
|
-
ready = true;
|
|
510
|
-
openedAt = clock.currentTimeMillisUnsafe();
|
|
511
|
-
opened();
|
|
512
|
-
flush();
|
|
513
|
-
});
|
|
514
|
-
const offMessage = next.onMessage((data) => {
|
|
515
|
-
for (const listener of [...listeners]) try {
|
|
516
|
-
listener(data);
|
|
517
|
-
} catch {}
|
|
518
|
-
let reply;
|
|
519
|
-
try {
|
|
520
|
-
reply = JSON.parse(data);
|
|
521
|
-
} catch {
|
|
522
|
-
return;
|
|
523
|
-
}
|
|
524
|
-
if (typeof reply !== "object" || reply === null) return;
|
|
525
|
-
if ("notify" in reply) {
|
|
526
|
-
if (reply.notify === true) PubSub.publishUnsafe(notices, void 0);
|
|
527
|
-
return;
|
|
528
|
-
}
|
|
529
|
-
const entry = inFlight.get(reply.id);
|
|
530
|
-
if (entry === void 0) return;
|
|
531
|
-
healthy();
|
|
532
|
-
inFlight.delete(reply.id);
|
|
533
|
-
entry.resume(reply.error === void 0 ? Effect.succeed(reply.result) : Effect.fail(new TransportError({ message: reply.error })));
|
|
534
|
-
});
|
|
535
|
-
const offClose = next.onClose(() => {
|
|
536
|
-
offOpen?.();
|
|
537
|
-
offMessage();
|
|
538
|
-
offClose();
|
|
539
|
-
socket = void 0;
|
|
540
|
-
ready = false;
|
|
541
|
-
if (openedAt !== void 0 && clock.currentTimeMillisUnsafe() - openedAt >= maxRetryDelay) healthy();
|
|
542
|
-
requeue();
|
|
543
|
-
reject(/* @__PURE__ */ new Error("transport closed"));
|
|
544
|
-
});
|
|
545
|
-
if (ready) {
|
|
546
|
-
opened();
|
|
547
|
-
flush();
|
|
548
|
-
}
|
|
549
|
-
}),
|
|
550
|
-
catch: () => new TransportError({ message: "transport closed" })
|
|
551
|
-
});
|
|
552
|
-
yield* Effect.forkScoped(Effect.gen(function* () {
|
|
553
|
-
while (!disposed) {
|
|
554
|
-
yield* Effect.exit(connect);
|
|
555
|
-
if (disposed) return;
|
|
556
|
-
failures += 1;
|
|
557
|
-
if (failures > maxRetries) failAll("transport closed");
|
|
558
|
-
const backoff = Math.min(maxRetryDelay, retryBase * 2 ** Math.min(failures - 1, 30));
|
|
559
|
-
yield* Effect.sleep(backoff * (.8 + Math.random() * .4));
|
|
560
|
-
}
|
|
561
|
-
}));
|
|
562
|
-
yield* Effect.addFinalizer(() => Effect.sync(() => {
|
|
563
|
-
disposed = true;
|
|
564
|
-
ready = false;
|
|
565
|
-
socket?.close();
|
|
566
|
-
socket = void 0;
|
|
567
|
-
failAll("transport closed");
|
|
568
|
-
}));
|
|
569
|
-
return Transport.of({
|
|
570
|
-
changes: Stream.fromPubSub(notices),
|
|
571
|
-
socket: shared,
|
|
572
|
-
exchange: (cursor, pending, epoch) => Effect.callback((resume) => {
|
|
573
|
-
if (disposed) {
|
|
574
|
-
resume(Effect.fail(new TransportError({ message: "transport closed" })));
|
|
575
|
-
return;
|
|
576
|
-
}
|
|
577
|
-
if (failures > maxRetries && !ready) {
|
|
578
|
-
resume(Effect.fail(new TransportError({ message: "transport closed" })));
|
|
579
|
-
return;
|
|
580
|
-
}
|
|
581
|
-
if (queued.length + inFlight.size >= maxQueue) {
|
|
582
|
-
resume(Effect.fail(new TransportError({ message: "transport queue full" })));
|
|
583
|
-
return;
|
|
584
|
-
}
|
|
585
|
-
const entry = {
|
|
586
|
-
id: String(nextId++),
|
|
587
|
-
cursor,
|
|
588
|
-
pending,
|
|
589
|
-
epoch,
|
|
590
|
-
resume
|
|
591
|
-
};
|
|
592
|
-
if (ready) send(entry);
|
|
593
|
-
else queued.push(entry);
|
|
594
|
-
return Effect.sync(() => {
|
|
595
|
-
const queuedIndex = queued.indexOf(entry);
|
|
596
|
-
if (queuedIndex >= 0) queued.splice(queuedIndex, 1);
|
|
597
|
-
if (inFlight.get(entry.id) === entry) inFlight.delete(entry.id);
|
|
598
|
-
});
|
|
599
|
-
})
|
|
600
|
-
});
|
|
601
|
-
})());
|
|
602
|
-
//#endregion
|
|
603
293
|
//#region src/sync.ts
|
|
604
294
|
/** Wire and persisted-format versions. A bump must handle the older value explicitly. */
|
|
605
295
|
const PROTOCOL_VERSION = 1;
|
|
@@ -673,6 +363,10 @@ const defineSync = (definition) => {
|
|
|
673
363
|
const ExchangeSchema = Schema.Struct({
|
|
674
364
|
operations: Schema.Array(Schema.Unknown),
|
|
675
365
|
rejected: Schema.Array(OpId),
|
|
366
|
+
reasons: Schema.optional(Schema.Array(Schema.Struct({
|
|
367
|
+
opId: OpId,
|
|
368
|
+
reason: Schema.String.check(Schema.isMaxLength(500))
|
|
369
|
+
}))),
|
|
676
370
|
acknowledged: Schema.optional(Schema.Array(OpId)),
|
|
677
371
|
checkpoint: Schema.optional(CheckpointSchema),
|
|
678
372
|
more: Schema.optional(Schema.Boolean),
|
|
@@ -753,7 +447,14 @@ const defineSync = (definition) => {
|
|
|
753
447
|
decode: decodeShared
|
|
754
448
|
},
|
|
755
449
|
empty: () => definition.empty,
|
|
756
|
-
reduce: (snapshot, operation) => definition.replay(snapshot, decodeMessage(operation.message))
|
|
450
|
+
reduce: (snapshot, operation) => definition.replay(snapshot, decodeMessage(operation.message)),
|
|
451
|
+
...definition.stamp === void 0 ? {} : { stamp: (operation, commit) => ({
|
|
452
|
+
...operation,
|
|
453
|
+
message: encodeMessage(definition.stamp(decodeMessage(operation.message), {
|
|
454
|
+
...commit,
|
|
455
|
+
replicaId: operation.replicaId
|
|
456
|
+
}))
|
|
457
|
+
}) }
|
|
757
458
|
});
|
|
758
459
|
const optimistic = (state) => state.pending.reduce((model, operation) => definition.replay(model, decodeMessage(operation.message)), state.committed);
|
|
759
460
|
const openReplica = Effect.fn("Sync.openReplica")(function* (replicaId, storage) {
|
|
@@ -815,6 +516,7 @@ const defineSync = (definition) => {
|
|
|
815
516
|
const unsent = yield* Ref.make(/* @__PURE__ */ new Set());
|
|
816
517
|
const wake = yield* Queue.sliding(1);
|
|
817
518
|
const statusSignals = yield* PubSub.sliding(1);
|
|
519
|
+
const following = (read) => Stream.unwrap(Effect.map(PubSub.subscribe(statusSignals), (signals) => Stream.concat(Stream.fromEffect(read), Stream.fromSubscription(signals).pipe(Stream.mapEffect(() => read)))));
|
|
818
520
|
const projection = yield* Ref.make(void 0);
|
|
819
521
|
const snapshot = Effect.fn("Sync.snapshot")(function* () {
|
|
820
522
|
const current = yield* SynchronizedRef.get(stateRef);
|
|
@@ -833,7 +535,8 @@ const defineSync = (definition) => {
|
|
|
833
535
|
pending: current.pending.length,
|
|
834
536
|
cursor: current.cursor,
|
|
835
537
|
lastError: yield* Ref.get(lastError),
|
|
836
|
-
rejected: yield* Ref.get(rejectedOps)
|
|
538
|
+
rejected: yield* Ref.get(rejectedOps),
|
|
539
|
+
epoch: Option.fromUndefinedOr(current.epoch)
|
|
837
540
|
},
|
|
838
541
|
shared: projected,
|
|
839
542
|
committed: current.committed,
|
|
@@ -975,6 +678,10 @@ const defineSync = (definition) => {
|
|
|
975
678
|
message: "Server both acknowledged and rejected an operation"
|
|
976
679
|
});
|
|
977
680
|
}
|
|
681
|
+
for (const { opId } of response.reasons ?? []) if (!rejected.has(opId)) return yield* new ForeignRejectionError({
|
|
682
|
+
opId,
|
|
683
|
+
message: "Server gave a reason for an operation it did not reject"
|
|
684
|
+
});
|
|
978
685
|
let applied = 0;
|
|
979
686
|
for (const raw of response.operations) {
|
|
980
687
|
const { committed: operation, message } = yield* Effect.try({
|
|
@@ -1017,7 +724,16 @@ const defineSync = (definition) => {
|
|
|
1017
724
|
return [restarted, next];
|
|
1018
725
|
}));
|
|
1019
726
|
yield* Ref.set(lastError, void 0);
|
|
1020
|
-
if (response.rejected.length > 0)
|
|
727
|
+
if (response.rejected.length > 0) {
|
|
728
|
+
const reasonOf = new Map((response.reasons ?? []).map(({ opId, reason }) => [opId, reason]));
|
|
729
|
+
const sentOf = new Map(sent.pending.map((operation) => [operation.opId, operation]));
|
|
730
|
+
const refused = response.rejected.map((opId) => ({
|
|
731
|
+
opId,
|
|
732
|
+
reason: Option.fromUndefinedOr(reasonOf.get(opId)),
|
|
733
|
+
operation: sentOf.get(opId)
|
|
734
|
+
}));
|
|
735
|
+
yield* Ref.update(rejectedOps, (previous) => [...refused, ...previous].slice(0, 32));
|
|
736
|
+
}
|
|
1021
737
|
yield* PubSub.publish(statusSignals, void 0);
|
|
1022
738
|
const reached = (yield* SynchronizedRef.get(stateRef)).cursor;
|
|
1023
739
|
return response.more === true && reached > (restarted ? 0 : sent.cursor);
|
|
@@ -1051,14 +767,15 @@ const defineSync = (definition) => {
|
|
|
1051
767
|
}
|
|
1052
768
|
});
|
|
1053
769
|
return {
|
|
770
|
+
replicaId,
|
|
1054
771
|
shared,
|
|
1055
772
|
snapshot,
|
|
1056
773
|
committed: Effect.map(SynchronizedRef.get(stateRef), (state) => state.committed),
|
|
1057
774
|
pending: Effect.map(SynchronizedRef.get(stateRef), (state) => state.pending),
|
|
1058
775
|
cursor: Effect.map(SynchronizedRef.get(stateRef), (state) => state.cursor),
|
|
1059
776
|
status,
|
|
1060
|
-
statusChanges:
|
|
1061
|
-
changes:
|
|
777
|
+
statusChanges: following(status),
|
|
778
|
+
changes: following(snapshot),
|
|
1062
779
|
submit,
|
|
1063
780
|
synchronize,
|
|
1064
781
|
start,
|
|
@@ -1151,6 +868,7 @@ const build = (app) => {
|
|
|
1151
868
|
const replay = options.replay === void 0 ? derivedReplay(app, shared) : (value, message) => options.replay(value, message);
|
|
1152
869
|
const durableTags = new Set(durable.constructors.map(messageTag).filter((tag) => tag !== void 0));
|
|
1153
870
|
const sharedCodec = shared.schema;
|
|
871
|
+
const stamps = options.stamp;
|
|
1154
872
|
const sync = defineSync({
|
|
1155
873
|
documentId: options.documentId,
|
|
1156
874
|
message: app.Message,
|
|
@@ -1158,7 +876,15 @@ const build = (app) => {
|
|
|
1158
876
|
empty: shared.get(app.initial),
|
|
1159
877
|
durable: (message) => Option.exists(tagOf(message), (tag) => durableTags.has(tag)),
|
|
1160
878
|
replay,
|
|
1161
|
-
coalesce: options.coalesce
|
|
879
|
+
coalesce: options.coalesce,
|
|
880
|
+
...stamps === void 0 ? {} : { stamp: (message, commit) => Option.match(Option.flatMapNullishOr(tagOf(message), (tag) => stamps[tag]), {
|
|
881
|
+
onNone: () => message,
|
|
882
|
+
onSome: (rule) => {
|
|
883
|
+
const stamped = rule(message, commit);
|
|
884
|
+
if (!Equal.equals(tagOf(stamped), tagOf(message))) throw new Error("A stamp must return the variant it was given");
|
|
885
|
+
return stamped;
|
|
886
|
+
}
|
|
887
|
+
}) }
|
|
1162
888
|
});
|
|
1163
889
|
const rules = options.authorize;
|
|
1164
890
|
const journalContract = () => {
|
|
@@ -1237,12 +963,14 @@ const PERSISTED = "foldkit-sync/Persisted";
|
|
|
1237
963
|
const FAILED = "foldkit-sync/PersistenceFailed";
|
|
1238
964
|
const NAVIGATE = "foldkit-sync/Navigate";
|
|
1239
965
|
const NAVIGATED = "foldkit-sync/Navigated";
|
|
966
|
+
const DISPATCHED = "foldkit-sync/Dispatched";
|
|
1240
967
|
const PRIVATE = {
|
|
1241
968
|
[REFRESH]: true,
|
|
1242
969
|
[PERSISTED]: true,
|
|
1243
970
|
[FAILED]: true,
|
|
1244
971
|
[NAVIGATE]: true,
|
|
1245
|
-
[NAVIGATED]: true
|
|
972
|
+
[NAVIGATED]: true,
|
|
973
|
+
[DISPATCHED]: true
|
|
1246
974
|
};
|
|
1247
975
|
const isPrivate = (message) => Object.hasOwn(PRIVATE, message._tag);
|
|
1248
976
|
const FACT = Symbol.for("foldkit-sync/fact");
|
|
@@ -1270,7 +998,7 @@ const factOf = (command) => typeof command === "object" && command !== null && F
|
|
|
1270
998
|
* submit only echoes a local edit. An acknowledgment that does not return the
|
|
1271
999
|
* operation moves no cursor, but it still drops the edit from the outbox.
|
|
1272
1000
|
*/
|
|
1273
|
-
const sharedChanged = (previous, next) => previous === void 0 || previous.cursor !== next.cursor || next.pending < previous.pending || previous.rejected.length !== next.rejected.length || previous.rejected.some((
|
|
1001
|
+
const sharedChanged = (previous, next) => previous === void 0 || previous.cursor !== next.cursor || next.pending < previous.pending || previous.rejected.length !== next.rejected.length || previous.rejected.some((rejection, index) => rejection.opId !== next.rejected[index]?.opId);
|
|
1274
1002
|
/**
|
|
1275
1003
|
* The edits a replica whose next local sequence is `next` does not hold yet.
|
|
1276
1004
|
* Submits run one at a time, so the one in flight is in the replica exactly
|
|
@@ -1299,6 +1027,27 @@ const mount = (app, sync, options) => {
|
|
|
1299
1027
|
return sync.projection.set(model, shared);
|
|
1300
1028
|
};
|
|
1301
1029
|
const inFlight = /* @__PURE__ */ new Set();
|
|
1030
|
+
let dispatchesOwed = 0;
|
|
1031
|
+
let persistsOwed = 0;
|
|
1032
|
+
let applied;
|
|
1033
|
+
let disposed = false;
|
|
1034
|
+
const waiters = /* @__PURE__ */ new Set();
|
|
1035
|
+
const quiet = () => dispatchesOwed === 0 && persistsOwed === 0 && !sharedChanged(applied, Effect.runSync(replica.status));
|
|
1036
|
+
const notifySettled = () => {
|
|
1037
|
+
if (!quiet()) return;
|
|
1038
|
+
for (const waiter of [...waiters]) {
|
|
1039
|
+
waiters.delete(waiter);
|
|
1040
|
+
waiter.resolve();
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
/**
|
|
1044
|
+
* A private transition that leaves the Model as it was: the runtime sends
|
|
1045
|
+
* no Subscription the change, so `settled` is told here, once it returns.
|
|
1046
|
+
*/
|
|
1047
|
+
const unchanged = (model) => {
|
|
1048
|
+
queueMicrotask(notifySettled);
|
|
1049
|
+
return { model };
|
|
1050
|
+
};
|
|
1302
1051
|
let latest = install(app.initial);
|
|
1303
1052
|
const modelListeners = /* @__PURE__ */ new Set();
|
|
1304
1053
|
const committedListeners = /* @__PURE__ */ new Set();
|
|
@@ -1315,12 +1064,20 @@ const mount = (app, sync, options) => {
|
|
|
1315
1064
|
const notifyCommitted = () => notifyEach(committedListeners, void 0);
|
|
1316
1065
|
const updatePrivate = (model, message) => {
|
|
1317
1066
|
switch (message._tag) {
|
|
1318
|
-
case REFRESH:
|
|
1319
|
-
|
|
1067
|
+
case REFRESH: {
|
|
1068
|
+
const reset = applied !== void 0 && Option.isSome(applied.epoch) && Option.isSome(message.status.epoch) && applied.epoch.value !== message.status.epoch.value;
|
|
1069
|
+
applied = message.status;
|
|
1070
|
+
if (!message.changed || !editedSinceInstall && Effect.runSync(replica.snapshot).shared === installedFrom) return unchanged(model);
|
|
1071
|
+
return reinstalled(install(model), model, { reset });
|
|
1072
|
+
}
|
|
1073
|
+
case PERSISTED:
|
|
1074
|
+
persistsOwed--;
|
|
1075
|
+
return unchanged(model);
|
|
1320
1076
|
case FAILED: {
|
|
1077
|
+
persistsOwed--;
|
|
1321
1078
|
const { error } = message;
|
|
1322
1079
|
const reverted = install(model);
|
|
1323
|
-
return reinstalled(options.onPersistenceFailure?.(reverted, error) ?? reverted, model);
|
|
1080
|
+
return reinstalled(options.onPersistenceFailure?.(reverted, error) ?? reverted, model, { reset: false });
|
|
1324
1081
|
}
|
|
1325
1082
|
case NAVIGATE: {
|
|
1326
1083
|
const { request } = message;
|
|
@@ -1333,6 +1090,9 @@ const mount = (app, sync, options) => {
|
|
|
1333
1090
|
};
|
|
1334
1091
|
}
|
|
1335
1092
|
case NAVIGATED: return { model };
|
|
1093
|
+
case DISPATCHED:
|
|
1094
|
+
dispatchesOwed--;
|
|
1095
|
+
return transition(model, message.message);
|
|
1336
1096
|
default: return absurd(message);
|
|
1337
1097
|
}
|
|
1338
1098
|
};
|
|
@@ -1340,13 +1100,14 @@ const mount = (app, sync, options) => {
|
|
|
1340
1100
|
if (isPrivate(message)) return updatePrivate(model, message);
|
|
1341
1101
|
return transition(model, message);
|
|
1342
1102
|
};
|
|
1343
|
-
const reinstalled = (next, previous) => options.onReinstall === void 0 ? { model: next } : applyFacts(options.onReinstall(next, previous));
|
|
1103
|
+
const reinstalled = (next, previous, change) => options.onReinstall === void 0 ? { model: next } : applyFacts(options.onReinstall(next, previous, change));
|
|
1344
1104
|
/** The application's transition for one of its Messages, with the persist a durable one needs. */
|
|
1345
1105
|
const step = (model, message) => {
|
|
1346
1106
|
notifyEach(messageListeners, message);
|
|
1347
1107
|
const result = app.update(model, message);
|
|
1348
1108
|
if (!durable.has(message._tag)) return result;
|
|
1349
1109
|
editedSinceInstall = true;
|
|
1110
|
+
persistsOwed++;
|
|
1350
1111
|
const edit = {
|
|
1351
1112
|
message,
|
|
1352
1113
|
started: void 0
|
|
@@ -1411,13 +1172,21 @@ const mount = (app, sync, options) => {
|
|
|
1411
1172
|
};
|
|
1412
1173
|
const ports = { inbound: { message: Port.inbound(app.Message) } };
|
|
1413
1174
|
const own = Subscription.make()(() => ({
|
|
1414
|
-
message: Port.subscription(ports.inbound.message, (message) =>
|
|
1415
|
-
|
|
1175
|
+
message: Port.subscription(ports.inbound.message, (message) => ({
|
|
1176
|
+
_tag: DISPATCHED,
|
|
1177
|
+
message
|
|
1178
|
+
})),
|
|
1179
|
+
refresh: Subscription.persistent(replica.statusChanges.pipe(Stream.tap(() => Effect.sync(notifyCommitted)), Stream.mapAccum(() => void 0, (previous, status) => [status, [{
|
|
1180
|
+
_tag: REFRESH,
|
|
1181
|
+
status,
|
|
1182
|
+
changed: sharedChanged(previous, status)
|
|
1183
|
+
}]]))),
|
|
1416
1184
|
latest: {
|
|
1417
1185
|
dependenciesSchema: Schema.Struct({}),
|
|
1418
1186
|
modelToDependencies: (model) => {
|
|
1419
1187
|
latest = model;
|
|
1420
1188
|
notifyEach(modelListeners, void 0);
|
|
1189
|
+
notifySettled();
|
|
1421
1190
|
return {};
|
|
1422
1191
|
},
|
|
1423
1192
|
dependenciesToStream: () => Stream.never
|
|
@@ -1453,8 +1222,17 @@ const mount = (app, sync, options) => {
|
|
|
1453
1222
|
}
|
|
1454
1223
|
});
|
|
1455
1224
|
const handle = Runtime.embed(program);
|
|
1225
|
+
const encodeMessage = Schema.encodeExit(app.Message);
|
|
1456
1226
|
return {
|
|
1457
|
-
dispatch: (message) =>
|
|
1227
|
+
dispatch: (message) => Exit.match(encodeMessage(message), {
|
|
1228
|
+
onSuccess: (encoded) => {
|
|
1229
|
+
dispatchesOwed++;
|
|
1230
|
+
const sent = handle.ports.message.send(encoded);
|
|
1231
|
+
if (Exit.isFailure(sent)) dispatchesOwed--;
|
|
1232
|
+
return sent;
|
|
1233
|
+
},
|
|
1234
|
+
onFailure: (cause) => Exit.failCause(cause)
|
|
1235
|
+
}),
|
|
1458
1236
|
model: () => latest,
|
|
1459
1237
|
subscribe: (listener) => {
|
|
1460
1238
|
modelListeners.add(listener);
|
|
@@ -1471,7 +1249,20 @@ const mount = (app, sync, options) => {
|
|
|
1471
1249
|
return () => committedListeners.delete(listener);
|
|
1472
1250
|
}
|
|
1473
1251
|
},
|
|
1252
|
+
settled: () => {
|
|
1253
|
+
if (disposed) return Promise.reject(/* @__PURE__ */ new Error("Sync.mount: settled() after dispose"));
|
|
1254
|
+
if (quiet()) return Promise.resolve();
|
|
1255
|
+
return new Promise((resolve, reject) => waiters.add({
|
|
1256
|
+
resolve,
|
|
1257
|
+
reject
|
|
1258
|
+
}));
|
|
1259
|
+
},
|
|
1474
1260
|
dispose: async () => {
|
|
1261
|
+
disposed = true;
|
|
1262
|
+
for (const waiter of [...waiters]) {
|
|
1263
|
+
waiters.delete(waiter);
|
|
1264
|
+
waiter.reject(/* @__PURE__ */ new Error("Sync.mount: disposed before it settled"));
|
|
1265
|
+
}
|
|
1475
1266
|
await Promise.all([...inFlight]);
|
|
1476
1267
|
handle.dispose();
|
|
1477
1268
|
}
|
|
@@ -1700,7 +1491,10 @@ const socketPresenceChannel = (socket) => Effect.gen(function* () {
|
|
|
1700
1491
|
publish: (update) => Effect.sync(() => socket.send(JSON.stringify({ [frameType]: { value: update.value } })))
|
|
1701
1492
|
};
|
|
1702
1493
|
});
|
|
1703
|
-
/**
|
|
1494
|
+
/**
|
|
1495
|
+
* Serves presence frames on an accepted socket, stamping the connection's
|
|
1496
|
+
* identity, until the socket closes or the returned function is called.
|
|
1497
|
+
*/
|
|
1704
1498
|
const servePresence = (socket, hub, options) => {
|
|
1705
1499
|
const send = (update) => socket.send(JSON.stringify({ [frameType]: {
|
|
1706
1500
|
id: update.id,
|
|
@@ -1714,10 +1508,15 @@ const servePresence = (socket, hub, options) => {
|
|
|
1714
1508
|
value: frame.value
|
|
1715
1509
|
});
|
|
1716
1510
|
});
|
|
1717
|
-
|
|
1511
|
+
const stop = () => {
|
|
1718
1512
|
off();
|
|
1719
1513
|
leave();
|
|
1720
1514
|
};
|
|
1515
|
+
const stopClose = socket.onClose(stop);
|
|
1516
|
+
return () => {
|
|
1517
|
+
stop();
|
|
1518
|
+
stopClose();
|
|
1519
|
+
};
|
|
1721
1520
|
};
|
|
1722
1521
|
//#endregion
|
|
1723
1522
|
//#region src/index.ts
|
|
@@ -1760,7 +1559,9 @@ const Sync = {
|
|
|
1760
1559
|
/** The server side of one accepted socket connection. */
|
|
1761
1560
|
serve: serveSocket,
|
|
1762
1561
|
/** The default socket factory: the platform `WebSocket`. */
|
|
1763
|
-
nativeSocket
|
|
1562
|
+
nativeSocket,
|
|
1563
|
+
/** A socket over a `MessagePort`, closed by `close()` or an abort signal. */
|
|
1564
|
+
portSocket
|
|
1764
1565
|
},
|
|
1765
1566
|
/** An ephemeral, TTL'd peer registry, deliberately outside the durable log. */
|
|
1766
1567
|
presence: {
|
|
@@ -1780,6 +1581,6 @@ const Sync = {
|
|
|
1780
1581
|
}
|
|
1781
1582
|
};
|
|
1782
1583
|
//#endregion
|
|
1783
|
-
export { CheckpointRegressionError, CommittedOrderError, DocumentId, ForeignAcknowledgementError, ForeignRejectionError, InvalidExchangeError, InvalidOutboxError, InvalidReplicaHistoryError, LocalSequence, OpId, ReplayError, ReplicaClosedError, ReplicaId, Sequence, StorageError, Sync, Transport, TransportError, UnsupportedClockVersionError, UnsupportedReplicaVersionError, WrongReplicaStorageError, createPresence, createPresenceHub, defineSync, documentId, fact, forApplication, indexedDb, layerFromPromise, layerLoopback, layerSocket, localSequence, loopbackPresenceChannel, lwwRegister, mount, nativeSocket, opId, openLwwClock, replicaId, sequence, servePresence, serveSocket, socketPresenceChannel, syncMetrics, toPromise };
|
|
1584
|
+
export { CheckpointRegressionError, CommittedOrderError, DocumentId, ForeignAcknowledgementError, ForeignRejectionError, InvalidExchangeError, InvalidOutboxError, InvalidReplicaHistoryError, LocalSequence, OpId, ReplayError, ReplicaClosedError, ReplicaId, Sequence, StorageError, Sync, Transport, TransportError, UnsupportedClockVersionError, UnsupportedReplicaVersionError, WrongReplicaStorageError, createPresence, createPresenceHub, defineSync, documentId, fact, forApplication, indexedDb, layerFromPromise, layerLoopback, layerSocket, localSequence, loopbackPresenceChannel, lwwRegister, mount, nativeSocket, opId, openLwwClock, portSocket, replicaId, sequence, servePresence, serveSocket, socketPresenceChannel, syncMetrics, toPromise };
|
|
1784
1585
|
|
|
1785
1586
|
//# sourceMappingURL=index.mjs.map
|