foldkit-sync 0.5.4 → 0.7.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 +129 -18
- package/dist/index.d.mts +148 -20
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +458 -160
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -7
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Clock, Context, Duration, Effect, Exit, Fiber, Layer, Metric, PubSub, Queue, Ref,
|
|
1
|
+
import { Clock, Context, Duration, Effect, Exit, Fiber, Layer, Metric, Option, Predicate, PubSub, Queue, Ref, Schema, Stream, SynchronizedRef } from "effect";
|
|
2
2
|
import { MessageSet, Projection, Surface } from "foldkit-surface";
|
|
3
|
+
import { absurd } from "effect/Function";
|
|
3
4
|
import * as Navigation from "foldkit/navigation";
|
|
4
5
|
import * as Port from "foldkit/port";
|
|
5
6
|
import * as Runtime from "foldkit/runtime";
|
|
@@ -84,11 +85,19 @@ const storageError = (message, cause) => new StorageError({
|
|
|
84
85
|
});
|
|
85
86
|
const openDatabase = (name, factory) => Effect.tryPromise({
|
|
86
87
|
try: () => new Promise((resolve, reject) => {
|
|
87
|
-
const request = factory.open(name,
|
|
88
|
-
request.onupgradeneeded = () =>
|
|
89
|
-
|
|
88
|
+
const request = factory.open(name, 2);
|
|
89
|
+
request.onupgradeneeded = (event) => {
|
|
90
|
+
const database = request.result;
|
|
91
|
+
if (event.oldVersion < 1) database.createObjectStore("replica");
|
|
92
|
+
if (event.oldVersion < 2) database.createObjectStore("outbox");
|
|
93
|
+
};
|
|
94
|
+
let blocked = false;
|
|
95
|
+
request.onsuccess = () => blocked ? request.result.close() : resolve(request.result);
|
|
90
96
|
request.onerror = () => reject(request.error);
|
|
91
|
-
request.onblocked = () =>
|
|
97
|
+
request.onblocked = () => {
|
|
98
|
+
blocked = true;
|
|
99
|
+
reject(/* @__PURE__ */ new Error("IndexedDB upgrade is blocked by another connection"));
|
|
100
|
+
};
|
|
92
101
|
}),
|
|
93
102
|
catch: (cause) => storageError("Could not open the storage", cause)
|
|
94
103
|
});
|
|
@@ -97,32 +106,66 @@ const indexedDb = Effect.fn("Storage.indexedDb")(function* (name, factory = glob
|
|
|
97
106
|
const databaseName = typeof name === "string" ? name : yield* name.pipe(Effect.catchTag("ConfigError", (cause) => Effect.fail(storageError("Could not read the storage name", cause))));
|
|
98
107
|
const database = yield* Effect.acquireRelease(openDatabase(databaseName, factory), (database) => Effect.sync(() => database.close()));
|
|
99
108
|
database.onversionchange = () => database.close();
|
|
109
|
+
/**
|
|
110
|
+
* Writes under compare-and-swap: `write` runs only if the stored revision is the one
|
|
111
|
+
* expected. The revision is its own key, so an append need not read the state; a database
|
|
112
|
+
* written before it existed has it inside the state.
|
|
113
|
+
*/
|
|
114
|
+
const write = (expectedRevision, writing) => new Promise((resolve, reject) => {
|
|
115
|
+
const transaction = database.transaction(["replica", "outbox"], "readwrite", { durability: "strict" });
|
|
116
|
+
const replica = transaction.objectStore("replica");
|
|
117
|
+
let conflict = false;
|
|
118
|
+
const check = (revision) => {
|
|
119
|
+
if (revision !== expectedRevision) {
|
|
120
|
+
conflict = true;
|
|
121
|
+
transaction.abort();
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
writing(replica, transaction.objectStore("outbox"));
|
|
125
|
+
};
|
|
126
|
+
const stored = replica.get("revision");
|
|
127
|
+
stored.onsuccess = () => {
|
|
128
|
+
if (stored.result !== void 0) return check(stored.result);
|
|
129
|
+
const state = replica.get("state");
|
|
130
|
+
state.onsuccess = () => check(state.result?.revision ?? null);
|
|
131
|
+
};
|
|
132
|
+
transaction.oncomplete = () => resolve();
|
|
133
|
+
transaction.onabort = () => reject(conflict ? /* @__PURE__ */ new Error("Replica was changed by another writer") : transaction.error ?? /* @__PURE__ */ new Error("IndexedDB write aborted"));
|
|
134
|
+
});
|
|
100
135
|
return {
|
|
101
136
|
load: () => Effect.tryPromise({
|
|
102
137
|
try: () => new Promise((resolve, reject) => {
|
|
103
|
-
const transaction = database.transaction("replica", "readonly");
|
|
104
|
-
const
|
|
105
|
-
|
|
138
|
+
const transaction = database.transaction(["replica", "outbox"], "readonly");
|
|
139
|
+
const state = transaction.objectStore("replica").get("state");
|
|
140
|
+
const appended = transaction.objectStore("outbox").getAll();
|
|
141
|
+
transaction.oncomplete = () => {
|
|
142
|
+
const saved = state.result;
|
|
143
|
+
const entries = appended.result;
|
|
144
|
+
const latest = entries[entries.length - 1];
|
|
145
|
+
const replaced = new Set(entries.map((entry) => entry.localSequence));
|
|
146
|
+
resolve(saved === void 0 || latest === void 0 ? saved : {
|
|
147
|
+
...saved,
|
|
148
|
+
revision: latest.revision,
|
|
149
|
+
nextLocalSequence: latest.nextLocalSequence,
|
|
150
|
+
pending: [...saved.pending.filter((operation) => !replaced.has(operation.localSequence)), ...entries.map((entry) => entry.operation)]
|
|
151
|
+
});
|
|
152
|
+
};
|
|
106
153
|
transaction.onabort = () => reject(transaction.error ?? /* @__PURE__ */ new Error("IndexedDB read aborted"));
|
|
107
154
|
}),
|
|
108
155
|
catch: (cause) => storageError("Could not read the replica", cause)
|
|
109
156
|
}),
|
|
110
157
|
save: (state, expectedRevision) => Effect.tryPromise({
|
|
111
|
-
try: () =>
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
store.put(state, "state");
|
|
123
|
-
};
|
|
124
|
-
transaction.oncomplete = () => resolve();
|
|
125
|
-
transaction.onabort = () => reject(conflict ? /* @__PURE__ */ new Error("Replica was changed by another writer") : transaction.error ?? /* @__PURE__ */ new Error("IndexedDB write aborted"));
|
|
158
|
+
try: () => write(expectedRevision, (replica, outbox) => {
|
|
159
|
+
replica.put(state, "state");
|
|
160
|
+
replica.put(state.revision, "revision");
|
|
161
|
+
outbox.clear();
|
|
162
|
+
}),
|
|
163
|
+
catch: (cause) => storageError("Could not save the replica", cause)
|
|
164
|
+
}),
|
|
165
|
+
append: (entry, expectedRevision) => Effect.tryPromise({
|
|
166
|
+
try: () => write(expectedRevision, (replica, outbox) => {
|
|
167
|
+
outbox.put(entry, entry.localSequence);
|
|
168
|
+
replica.put(entry.revision, "revision");
|
|
126
169
|
}),
|
|
127
170
|
catch: (cause) => storageError("Could not save the replica", cause)
|
|
128
171
|
}),
|
|
@@ -284,23 +327,25 @@ var TransportError = class extends Schema.TaggedError()("SyncTransportError", {
|
|
|
284
327
|
var Transport = class extends Context.Service()("foldkit-sync/Transport") {};
|
|
285
328
|
const failure = (error) => new TransportError({ message: error instanceof Error ? error.message : String(error) });
|
|
286
329
|
/** A transport backed by an in-process handler, for tests and single-process demos. */
|
|
287
|
-
const layerLoopback = (handler) => Layer.succeed(Transport, { exchange: (cursor, pending) => Effect.tryPromise({
|
|
288
|
-
try: () => Promise.resolve(handler(cursor, pending)),
|
|
330
|
+
const layerLoopback = (handler) => Layer.succeed(Transport, { exchange: (cursor, pending, epoch) => Effect.tryPromise({
|
|
331
|
+
try: () => Promise.resolve(handler(cursor, pending, epoch)),
|
|
289
332
|
catch: failure
|
|
290
333
|
}) });
|
|
291
334
|
/** Wraps the promise-based client the replica already speaks. */
|
|
292
|
-
const layerFromPromise = (transport) => Layer.succeed(Transport, { exchange: (cursor, pending) => Effect.tryPromise({
|
|
293
|
-
try: () => transport.exchange(sequence(cursor), pending),
|
|
335
|
+
const layerFromPromise = (transport) => Layer.succeed(Transport, { exchange: (cursor, pending, epoch) => Effect.tryPromise({
|
|
336
|
+
try: () => transport.exchange(sequence(cursor), pending, epoch),
|
|
294
337
|
catch: failure
|
|
295
338
|
}) });
|
|
296
339
|
/** Bridges the service back to the promise client the replica consumes. */
|
|
297
|
-
const toPromise = (transport) => ({ exchange: (cursor, pending) => Effect.runPromise(transport.exchange(cursor, pending)) });
|
|
340
|
+
const toPromise = (transport) => ({ exchange: (cursor, pending, epoch) => Effect.runPromise(transport.exchange(cursor, pending, epoch)) });
|
|
298
341
|
/**
|
|
299
342
|
* Serves one accepted socket, answering each exchange frame.
|
|
300
343
|
*
|
|
301
344
|
* The counterpart to `layerSocket`: the handler returns the exchange result, or
|
|
302
|
-
* throws and the failure is written back as an error frame.
|
|
303
|
-
*
|
|
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.
|
|
304
349
|
*/
|
|
305
350
|
const serveSocket = (socket, options) => {
|
|
306
351
|
const send = (reply) => {
|
|
@@ -316,9 +361,9 @@ const serveSocket = (socket, options) => {
|
|
|
316
361
|
return;
|
|
317
362
|
}
|
|
318
363
|
if (typeof parsed !== "object" || parsed === null) return;
|
|
319
|
-
const { id, cursor, pending } = parsed;
|
|
364
|
+
const { id, cursor, pending, epoch } = parsed;
|
|
320
365
|
if (typeof id !== "string") return;
|
|
321
|
-
if (typeof cursor !== "number" || !Number.isSafeInteger(cursor) || cursor < 0 || !Array.isArray(pending)) {
|
|
366
|
+
if (typeof cursor !== "number" || !Number.isSafeInteger(cursor) || cursor < 0 || !Array.isArray(pending) || epoch !== void 0 && typeof epoch !== "string") {
|
|
322
367
|
send({
|
|
323
368
|
id,
|
|
324
369
|
error: "Invalid exchange frame"
|
|
@@ -328,11 +373,12 @@ const serveSocket = (socket, options) => {
|
|
|
328
373
|
const frame = {
|
|
329
374
|
id,
|
|
330
375
|
cursor,
|
|
331
|
-
pending
|
|
376
|
+
pending,
|
|
377
|
+
epoch: typeof epoch === "string" ? epoch : void 0
|
|
332
378
|
};
|
|
333
379
|
(async () => {
|
|
334
380
|
try {
|
|
335
|
-
const result = await options.exchange(frame.cursor, frame.pending);
|
|
381
|
+
const result = await options.exchange(frame.cursor, frame.pending, frame.epoch);
|
|
336
382
|
send({
|
|
337
383
|
id: frame.id,
|
|
338
384
|
result
|
|
@@ -345,9 +391,14 @@ const serveSocket = (socket, options) => {
|
|
|
345
391
|
}
|
|
346
392
|
})();
|
|
347
393
|
});
|
|
348
|
-
const
|
|
349
|
-
|
|
394
|
+
const stopChanges = options.changes?.(() => send({ notify: true }));
|
|
395
|
+
const stop = () => {
|
|
350
396
|
stopMessage();
|
|
397
|
+
stopChanges?.();
|
|
398
|
+
};
|
|
399
|
+
const stopClose = socket.onClose(stop);
|
|
400
|
+
return () => {
|
|
401
|
+
stop();
|
|
351
402
|
stopClose();
|
|
352
403
|
};
|
|
353
404
|
};
|
|
@@ -384,27 +435,52 @@ const nativeSocket = (url) => {
|
|
|
384
435
|
* it closes, the transport reconnects on an exponential, jittered backoff and
|
|
385
436
|
* re-sends every queued and in-flight frame with its original id, so a lost
|
|
386
437
|
* reply is answered rather than dropped and a late reply cannot resolve a newer
|
|
387
|
-
* frame.
|
|
388
|
-
* with a `TransportError
|
|
389
|
-
*
|
|
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.
|
|
390
446
|
*/
|
|
391
447
|
const layerSocket = (options) => Layer.effect(Transport, Effect.fn("Transport.layerSocket")(function* () {
|
|
392
448
|
const makeSocket = options.makeSocket ?? nativeSocket;
|
|
393
449
|
const maxQueue = options.maxQueue ?? 64;
|
|
394
|
-
const
|
|
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
|
+
};
|
|
395
467
|
const queued = [];
|
|
396
468
|
const inFlight = /* @__PURE__ */ new Map();
|
|
397
469
|
let socket;
|
|
398
470
|
let ready = false;
|
|
399
471
|
let nextId = 0;
|
|
400
472
|
let disposed = false;
|
|
401
|
-
let
|
|
473
|
+
let failures = 0;
|
|
474
|
+
const healthy = () => {
|
|
475
|
+
failures = 0;
|
|
476
|
+
};
|
|
402
477
|
const send = (entry) => {
|
|
403
478
|
inFlight.set(entry.id, entry);
|
|
404
479
|
socket?.send(JSON.stringify({
|
|
405
480
|
id: entry.id,
|
|
406
481
|
cursor: entry.cursor,
|
|
407
|
-
pending: entry.pending
|
|
482
|
+
pending: entry.pending,
|
|
483
|
+
...entry.epoch === void 0 ? {} : { epoch: entry.epoch }
|
|
408
484
|
}));
|
|
409
485
|
};
|
|
410
486
|
const flush = () => {
|
|
@@ -425,19 +501,34 @@ const layerSocket = (options) => Layer.effect(Transport, Effect.fn("Transport.la
|
|
|
425
501
|
const next = makeSocket(options.url);
|
|
426
502
|
socket = next;
|
|
427
503
|
ready = next.onOpen === void 0;
|
|
504
|
+
let openedAt = ready ? clock.currentTimeMillisUnsafe() : void 0;
|
|
505
|
+
const opened = () => {
|
|
506
|
+
PubSub.publishUnsafe(notices, void 0);
|
|
507
|
+
};
|
|
428
508
|
const offOpen = next.onOpen?.(() => {
|
|
429
509
|
ready = true;
|
|
510
|
+
openedAt = clock.currentTimeMillisUnsafe();
|
|
511
|
+
opened();
|
|
430
512
|
flush();
|
|
431
513
|
});
|
|
432
514
|
const offMessage = next.onMessage((data) => {
|
|
515
|
+
for (const listener of [...listeners]) try {
|
|
516
|
+
listener(data);
|
|
517
|
+
} catch {}
|
|
433
518
|
let reply;
|
|
434
519
|
try {
|
|
435
520
|
reply = JSON.parse(data);
|
|
436
521
|
} catch {
|
|
437
522
|
return;
|
|
438
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
|
+
}
|
|
439
529
|
const entry = inFlight.get(reply.id);
|
|
440
530
|
if (entry === void 0) return;
|
|
531
|
+
healthy();
|
|
441
532
|
inFlight.delete(reply.id);
|
|
442
533
|
entry.resume(reply.error === void 0 ? Effect.succeed(reply.result) : Effect.fail(new TransportError({ message: reply.error })));
|
|
443
534
|
});
|
|
@@ -447,18 +538,27 @@ const layerSocket = (options) => Layer.effect(Transport, Effect.fn("Transport.la
|
|
|
447
538
|
offClose();
|
|
448
539
|
socket = void 0;
|
|
449
540
|
ready = false;
|
|
541
|
+
if (openedAt !== void 0 && clock.currentTimeMillisUnsafe() - openedAt >= maxRetryDelay) healthy();
|
|
450
542
|
requeue();
|
|
451
543
|
reject(/* @__PURE__ */ new Error("transport closed"));
|
|
452
544
|
});
|
|
453
|
-
if (ready)
|
|
545
|
+
if (ready) {
|
|
546
|
+
opened();
|
|
547
|
+
flush();
|
|
548
|
+
}
|
|
454
549
|
}),
|
|
455
550
|
catch: () => new TransportError({ message: "transport closed" })
|
|
456
551
|
});
|
|
457
|
-
yield* Effect.forkScoped(
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
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
|
+
}));
|
|
462
562
|
yield* Effect.addFinalizer(() => Effect.sync(() => {
|
|
463
563
|
disposed = true;
|
|
464
564
|
ready = false;
|
|
@@ -466,33 +566,38 @@ const layerSocket = (options) => Layer.effect(Transport, Effect.fn("Transport.la
|
|
|
466
566
|
socket = void 0;
|
|
467
567
|
failAll("transport closed");
|
|
468
568
|
}));
|
|
469
|
-
return Transport.of({
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
if (
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
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
|
+
});
|
|
496
601
|
})());
|
|
497
602
|
//#endregion
|
|
498
603
|
//#region src/sync.ts
|
|
@@ -518,6 +623,9 @@ const CommittedSchema = Schema.Struct({
|
|
|
518
623
|
});
|
|
519
624
|
const decodeOperation = Schema.decodeUnknownSync(OperationSchema, { onExcessProperty: "error" });
|
|
520
625
|
const decodeCommitted = Schema.decodeUnknownSync(CommittedSchema, { onExcessProperty: "error" });
|
|
626
|
+
/** How long `start` waits after a failed exchange: doubling from the base, up to the cap, in ms. */
|
|
627
|
+
const START_RETRY_BASE = 500;
|
|
628
|
+
const START_RETRY_MAX = 3e4;
|
|
521
629
|
/** Counters and a histogram an application can scrape. */
|
|
522
630
|
const syncMetrics = {
|
|
523
631
|
exchanges: Metric.counter("foldkit_sync_exchanges_total"),
|
|
@@ -555,7 +663,8 @@ const defineSync = (definition) => {
|
|
|
555
663
|
cursor: Sequence,
|
|
556
664
|
committed: definition.shared,
|
|
557
665
|
committedIds: Schema.Array(Schema.String),
|
|
558
|
-
pending: Schema.Array(OperationSchema)
|
|
666
|
+
pending: Schema.Array(OperationSchema),
|
|
667
|
+
epoch: Schema.optionalKey(Schema.NonEmptyString)
|
|
559
668
|
});
|
|
560
669
|
const CheckpointSchema = Schema.Struct({
|
|
561
670
|
cursor: Sequence,
|
|
@@ -565,10 +674,13 @@ const defineSync = (definition) => {
|
|
|
565
674
|
operations: Schema.Array(Schema.Unknown),
|
|
566
675
|
rejected: Schema.Array(OpId),
|
|
567
676
|
acknowledged: Schema.optional(Schema.Array(OpId)),
|
|
568
|
-
checkpoint: Schema.optional(CheckpointSchema)
|
|
677
|
+
checkpoint: Schema.optional(CheckpointSchema),
|
|
678
|
+
more: Schema.optional(Schema.Boolean),
|
|
679
|
+
epoch: Schema.optional(Schema.NonEmptyString)
|
|
569
680
|
});
|
|
570
681
|
const decodeState = Schema.decodeUnknownSync(ReplicaStateSchema, { onExcessProperty: "error" });
|
|
571
682
|
const encodeState = Schema.encodeSync(ReplicaStateSchema);
|
|
683
|
+
const encodeOperation = Schema.encodeSync(OperationSchema);
|
|
572
684
|
const VersionProbe = Schema.Struct({
|
|
573
685
|
protocolVersion: Schema.optional(Schema.Number),
|
|
574
686
|
schemaVersion: Schema.optional(Schema.Number)
|
|
@@ -630,6 +742,8 @@ const defineSync = (definition) => {
|
|
|
630
742
|
};
|
|
631
743
|
const committedFrom = (input, key) => decodeCommittedOperation(input, key).committed;
|
|
632
744
|
const journalContract = () => ({
|
|
745
|
+
replicaId: (operation) => operation.replicaId,
|
|
746
|
+
legacyReplicaId: (opId) => opId.slice(0, opId.lastIndexOf(":")),
|
|
633
747
|
operation: {
|
|
634
748
|
encode: (operation) => operation,
|
|
635
749
|
decode: normalizeOperation
|
|
@@ -698,6 +812,7 @@ const defineSync = (definition) => {
|
|
|
698
812
|
const closed = yield* Ref.make(false);
|
|
699
813
|
const lastError = yield* Ref.make(void 0);
|
|
700
814
|
const rejectedOps = yield* Ref.make([]);
|
|
815
|
+
const unsent = yield* Ref.make(/* @__PURE__ */ new Set());
|
|
701
816
|
const wake = yield* Queue.sliding(1);
|
|
702
817
|
const statusSignals = yield* PubSub.sliding(1);
|
|
703
818
|
const projection = yield* Ref.make(void 0);
|
|
@@ -735,8 +850,12 @@ const defineSync = (definition) => {
|
|
|
735
850
|
});
|
|
736
851
|
const result = yield* SynchronizedRef.modifyEffect(stateRef, (current) => Effect.gen(function* () {
|
|
737
852
|
if (yield* Ref.get(closed)) return yield* new ReplicaClosedError({ message: "Replica is closed" });
|
|
853
|
+
if (!definition.durable(message)) return yield* new InvalidOutboxError({ message: "Message is local-only" });
|
|
854
|
+
const last = current.pending[current.pending.length - 1];
|
|
855
|
+
const merged = last !== void 0 && definition.coalesce !== void 0 && (yield* Ref.get(unsent)).has(last.opId) ? definition.coalesce(decodeMessage(last.message), message) : void 0;
|
|
856
|
+
if (merged !== void 0 && !definition.durable(merged)) return yield* new InvalidOutboxError({ message: "coalesce returned a local-only Message" });
|
|
738
857
|
const operation = yield* Effect.try({
|
|
739
|
-
try: () => operationFrom({
|
|
858
|
+
try: () => operationFrom(merged === void 0 ? {
|
|
740
859
|
protocolVersion: PROTOCOL_VERSION,
|
|
741
860
|
schemaVersion: SCHEMA_VERSION,
|
|
742
861
|
documentId,
|
|
@@ -745,6 +864,9 @@ const defineSync = (definition) => {
|
|
|
745
864
|
opId: `${replicaId}:${current.nextLocalSequence}`,
|
|
746
865
|
baseCursor: current.cursor,
|
|
747
866
|
message: encodeMessage(message)
|
|
867
|
+
} : {
|
|
868
|
+
...last,
|
|
869
|
+
message: encodeMessage(merged)
|
|
748
870
|
}, documentId),
|
|
749
871
|
catch: () => new InvalidOutboxError({ message: "Invalid outbox" })
|
|
750
872
|
});
|
|
@@ -760,28 +882,49 @@ const defineSync = (definition) => {
|
|
|
760
882
|
...current,
|
|
761
883
|
revision: current.revision + 1,
|
|
762
884
|
nextLocalSequence: localSequence(current.nextLocalSequence + 1),
|
|
763
|
-
pending: [...current.pending, operation]
|
|
885
|
+
pending: [...merged === void 0 ? current.pending : current.pending.slice(0, -1), operation]
|
|
764
886
|
};
|
|
765
|
-
yield* persist(next, current);
|
|
887
|
+
if (storage.append === void 0) yield* persist(next, current);
|
|
888
|
+
else {
|
|
889
|
+
const encoded = yield* Effect.try({
|
|
890
|
+
try: () => encodeOperation(operation),
|
|
891
|
+
catch: (cause) => new InvalidReplicaHistoryError({
|
|
892
|
+
message: "Could not encode the operation",
|
|
893
|
+
cause
|
|
894
|
+
})
|
|
895
|
+
});
|
|
896
|
+
yield* storage.append({
|
|
897
|
+
revision: next.revision,
|
|
898
|
+
nextLocalSequence: next.nextLocalSequence,
|
|
899
|
+
localSequence: operation.localSequence,
|
|
900
|
+
operation: encoded
|
|
901
|
+
}, current.revision);
|
|
902
|
+
}
|
|
766
903
|
yield* Ref.set(projection, {
|
|
767
904
|
state: next,
|
|
768
905
|
shared: replayed
|
|
769
906
|
});
|
|
907
|
+
yield* Ref.update(unsent, (ids) => new Set(ids).add(operation.opId));
|
|
770
908
|
yield* Queue.offer(wake, void 0);
|
|
771
909
|
return [void 0, next];
|
|
772
910
|
}));
|
|
773
911
|
yield* PubSub.publish(statusSignals, void 0);
|
|
774
912
|
return result;
|
|
775
913
|
});
|
|
776
|
-
|
|
914
|
+
/** One exchange; whether the server has more to send and this one made progress. */
|
|
915
|
+
const exchangeOnce = Effect.fn("Sync.exchange")(function* () {
|
|
777
916
|
yield* Effect.annotateCurrentSpan({ documentId });
|
|
778
917
|
const transport = yield* Transport;
|
|
779
918
|
if (yield* Ref.get(closed)) return yield* new ReplicaClosedError({ message: "Replica is closed" });
|
|
780
|
-
const sent = yield* SynchronizedRef.
|
|
919
|
+
const sent = yield* SynchronizedRef.modifyEffect(stateRef, (current) => Ref.update(unsent, (ids) => {
|
|
920
|
+
const left = new Set(ids);
|
|
921
|
+
for (const operation of current.pending) left.delete(operation.opId);
|
|
922
|
+
return left;
|
|
923
|
+
}).pipe(Effect.as([current, current])));
|
|
781
924
|
yield* Metric.update(syncMetrics.exchanges, 1);
|
|
782
925
|
yield* Metric.update(syncMetrics.exchangePending, sent.pending.length);
|
|
783
926
|
const response = yield* Effect.gen(function* () {
|
|
784
|
-
const raw = yield* transport.exchange(sent.cursor, sent.pending);
|
|
927
|
+
const raw = yield* transport.exchange(sent.cursor, sent.pending, sent.epoch);
|
|
785
928
|
return yield* Effect.try({
|
|
786
929
|
try: () => decodeExchange(raw),
|
|
787
930
|
catch: (cause) => new InvalidExchangeError({
|
|
@@ -789,19 +932,17 @@ const defineSync = (definition) => {
|
|
|
789
932
|
cause
|
|
790
933
|
})
|
|
791
934
|
});
|
|
792
|
-
})
|
|
793
|
-
|
|
935
|
+
});
|
|
936
|
+
const restarted = yield* SynchronizedRef.modifyEffect(stateRef, (current) => Effect.gen(function* () {
|
|
937
|
+
if (yield* Ref.get(closed)) return yield* new ReplicaClosedError({ message: "Replica is closed" });
|
|
938
|
+
const restarted = response.epoch !== void 0 && current.epoch !== void 0 && response.epoch !== current.epoch;
|
|
939
|
+
if (restarted) yield* Effect.logWarning("sync server history changed; rebuilding", {
|
|
794
940
|
documentId,
|
|
795
|
-
replicaId
|
|
796
|
-
error: error.message
|
|
941
|
+
replicaId
|
|
797
942
|
});
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
if (yield* Ref.get(closed)) return yield* new ReplicaClosedError({ message: "Replica is closed" });
|
|
802
|
-
let cursor = current.cursor;
|
|
803
|
-
let committed = current.committed;
|
|
804
|
-
const ids = response.checkpoint === void 0 ? new Set(current.committedIds) : /* @__PURE__ */ new Set();
|
|
943
|
+
let cursor = restarted ? sequence(0) : current.cursor;
|
|
944
|
+
let committed = restarted ? definition.empty : current.committed;
|
|
945
|
+
const ids = response.checkpoint === void 0 && !restarted ? new Set(current.committedIds) : /* @__PURE__ */ new Set();
|
|
805
946
|
if (response.checkpoint !== void 0) {
|
|
806
947
|
if (response.checkpoint.cursor < cursor) return yield* new CheckpointRegressionError({
|
|
807
948
|
cursor,
|
|
@@ -849,33 +990,64 @@ const defineSync = (definition) => {
|
|
|
849
990
|
actual: operation.serverSequence,
|
|
850
991
|
message: "Invalid committed order"
|
|
851
992
|
});
|
|
852
|
-
|
|
993
|
+
const replaying = committed;
|
|
994
|
+
committed = yield* Effect.try({
|
|
995
|
+
try: () => definition.replay(replaying, message),
|
|
996
|
+
catch: (cause) => new ReplayError({
|
|
997
|
+
message: cause instanceof Error ? cause.message : "Replay failed",
|
|
998
|
+
cause
|
|
999
|
+
})
|
|
1000
|
+
});
|
|
853
1001
|
ids.add(operation.opId);
|
|
854
1002
|
cursor = operation.serverSequence;
|
|
855
1003
|
applied += 1;
|
|
856
1004
|
}
|
|
857
1005
|
if (applied > 0) yield* Metric.update(syncMetrics.applied, applied);
|
|
1006
|
+
const epoch = response.epoch ?? current.epoch;
|
|
858
1007
|
const next = {
|
|
859
1008
|
...current,
|
|
860
1009
|
revision: current.revision + 1,
|
|
861
1010
|
committed,
|
|
862
1011
|
cursor,
|
|
1012
|
+
...epoch === void 0 ? {} : { epoch },
|
|
863
1013
|
committedIds: [...ids].slice(-1024),
|
|
864
|
-
pending: current.pending.filter((operation) => !ids.has(operation.opId) && !acknowledged.has(operation.opId) && !rejected.has(operation.opId))
|
|
1014
|
+
pending: current.pending.filter((operation) => !ids.has(operation.opId) && !(acknowledged.has(operation.opId) && response.more !== true) && !rejected.has(operation.opId))
|
|
865
1015
|
};
|
|
866
1016
|
yield* persist(next, current);
|
|
867
|
-
return [
|
|
1017
|
+
return [restarted, next];
|
|
868
1018
|
}));
|
|
869
1019
|
yield* Ref.set(lastError, void 0);
|
|
870
1020
|
if (response.rejected.length > 0) yield* Ref.update(rejectedOps, (previous) => [...response.rejected, ...previous].slice(0, 32));
|
|
871
1021
|
yield* PubSub.publish(statusSignals, void 0);
|
|
872
|
-
|
|
1022
|
+
const reached = (yield* SynchronizedRef.get(stateRef)).cursor;
|
|
1023
|
+
return response.more === true && reached > (restarted ? 0 : sent.cursor);
|
|
1024
|
+
});
|
|
1025
|
+
const synchronize = Effect.gen(function* () {
|
|
1026
|
+
while (yield* exchangeOnce());
|
|
1027
|
+
}).pipe(Effect.withSpan("Sync.synchronize"), Effect.tapError((error) => Effect.gen(function* () {
|
|
1028
|
+
yield* Effect.logWarning("sync exchange failed", {
|
|
1029
|
+
documentId,
|
|
1030
|
+
replicaId,
|
|
1031
|
+
error: error.message
|
|
1032
|
+
});
|
|
1033
|
+
yield* Ref.set(lastError, error.message);
|
|
1034
|
+
yield* PubSub.publish(statusSignals, void 0);
|
|
1035
|
+
})));
|
|
873
1036
|
const start = Effect.gen(function* () {
|
|
874
|
-
yield*
|
|
1037
|
+
const transport = yield* Transport;
|
|
1038
|
+
if (transport.changes !== void 0) yield* Effect.forkChild(Stream.runForEach(transport.changes, () => Queue.offer(wake, void 0)), { startImmediately: true });
|
|
1039
|
+
let failures = 0;
|
|
875
1040
|
while (!(yield* Ref.get(closed))) {
|
|
876
|
-
yield*
|
|
1041
|
+
const exit = yield* Effect.exit(synchronize);
|
|
877
1042
|
if (yield* Ref.get(closed)) return;
|
|
878
|
-
|
|
1043
|
+
if (exit._tag === "Success") {
|
|
1044
|
+
failures = 0;
|
|
1045
|
+
yield* Queue.take(wake);
|
|
1046
|
+
} else {
|
|
1047
|
+
failures += 1;
|
|
1048
|
+
const backoff = Math.min(START_RETRY_MAX, START_RETRY_BASE * 2 ** Math.min(failures - 1, 16));
|
|
1049
|
+
yield* Effect.raceFirst(Queue.take(wake), Effect.sleep(backoff));
|
|
1050
|
+
}
|
|
879
1051
|
}
|
|
880
1052
|
});
|
|
881
1053
|
return {
|
|
@@ -899,6 +1071,7 @@ const defineSync = (definition) => {
|
|
|
899
1071
|
}, (effect, _replicaId, storage) => effect.pipe(Effect.onExit((exit) => Exit.isSuccess(exit) ? Effect.void : storage.close.pipe(Effect.ignore))));
|
|
900
1072
|
return {
|
|
901
1073
|
documentId,
|
|
1074
|
+
durable: definition.durable,
|
|
902
1075
|
codec: {
|
|
903
1076
|
normalizeOperation,
|
|
904
1077
|
operationFrom,
|
|
@@ -922,6 +1095,8 @@ const messageTag = (constructor) => {
|
|
|
922
1095
|
const literal = constructor.fields?._tag?.ast?.literal;
|
|
923
1096
|
return typeof literal === "string" ? literal : void 0;
|
|
924
1097
|
};
|
|
1098
|
+
/** A Message's tag. The application's union is generic here, so its `_tag` is read, not typed. */
|
|
1099
|
+
const tagOf = (message) => Predicate.hasProperty(message, "_tag") && typeof message._tag === "string" ? Option.some(message._tag) : Option.none();
|
|
925
1100
|
/**
|
|
926
1101
|
* Replay derived from the application's own `update`: install the shared slice
|
|
927
1102
|
* into the initial Model, apply the Message, and read the slice back.
|
|
@@ -938,13 +1113,12 @@ const derivedReplay = (app, shared) => {
|
|
|
938
1113
|
const fields = Object.entries(app.Model.fields).filter(([key]) => !owned.has(key)).map(([key, field]) => [key, Schema.toEquivalence(field)]);
|
|
939
1114
|
return (value, message) => {
|
|
940
1115
|
const result = update(shared.set(initial, value), message);
|
|
941
|
-
|
|
942
|
-
if (result.commands !== void 0 && result.commands.length > 0) throw new Error(`Sync.forApplication: durable "${tag}" returned ${result.commands.length} Command(s); a durable transition is state-only`);
|
|
1116
|
+
if (result.commands !== void 0 && result.commands.length > 0) throw new Error(`Sync.forApplication: durable "${Option.getOrElse(tagOf(message), () => "untagged")}" returned ${result.commands.length} Command(s); a durable transition is state-only`);
|
|
943
1117
|
const next = shared.get(result.model);
|
|
944
1118
|
const written = shared.set(initial, next);
|
|
945
1119
|
const actual = result.model;
|
|
946
1120
|
const changed = fields.filter(([key, equal]) => !equal(actual[key], written[key]));
|
|
947
|
-
if (changed.length > 0) throw new Error(`Sync.forApplication: durable "${
|
|
1121
|
+
if (changed.length > 0) throw new Error(`Sync.forApplication: durable "${Option.getOrElse(tagOf(message), () => "untagged")}" changed Model fields outside the shared projection: ${changed.map(([key]) => key).join(", ")}`);
|
|
948
1122
|
return next;
|
|
949
1123
|
};
|
|
950
1124
|
};
|
|
@@ -982,11 +1156,9 @@ const build = (app) => {
|
|
|
982
1156
|
message: app.Message,
|
|
983
1157
|
shared: sharedCodec,
|
|
984
1158
|
empty: shared.get(app.initial),
|
|
985
|
-
durable: (message) =>
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
},
|
|
989
|
-
replay
|
|
1159
|
+
durable: (message) => Option.exists(tagOf(message), (tag) => durableTags.has(tag)),
|
|
1160
|
+
replay,
|
|
1161
|
+
coalesce: options.coalesce
|
|
990
1162
|
});
|
|
991
1163
|
const rules = options.authorize;
|
|
992
1164
|
const journalContract = () => {
|
|
@@ -996,11 +1168,13 @@ const build = (app) => {
|
|
|
996
1168
|
...base,
|
|
997
1169
|
authorize: ({ principal, operation, snapshot }) => {
|
|
998
1170
|
const message = decodeMessage(operation.message);
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1171
|
+
return Option.match(Option.flatMapNullishOr(tagOf(message), (tag) => rules[tag]), {
|
|
1172
|
+
onNone: () => true,
|
|
1173
|
+
onSome: (rule) => rule({
|
|
1174
|
+
principal,
|
|
1175
|
+
message,
|
|
1176
|
+
shared: snapshot
|
|
1177
|
+
})
|
|
1004
1178
|
});
|
|
1005
1179
|
}
|
|
1006
1180
|
};
|
|
@@ -1063,6 +1237,34 @@ const PERSISTED = "foldkit-sync/Persisted";
|
|
|
1063
1237
|
const FAILED = "foldkit-sync/PersistenceFailed";
|
|
1064
1238
|
const NAVIGATE = "foldkit-sync/Navigate";
|
|
1065
1239
|
const NAVIGATED = "foldkit-sync/Navigated";
|
|
1240
|
+
const PRIVATE = {
|
|
1241
|
+
[REFRESH]: true,
|
|
1242
|
+
[PERSISTED]: true,
|
|
1243
|
+
[FAILED]: true,
|
|
1244
|
+
[NAVIGATE]: true,
|
|
1245
|
+
[NAVIGATED]: true
|
|
1246
|
+
};
|
|
1247
|
+
const isPrivate = (message) => Object.hasOwn(PRIVATE, message._tag);
|
|
1248
|
+
const FACT = Symbol.for("foldkit-sync/fact");
|
|
1249
|
+
/**
|
|
1250
|
+
* A Command that dispatches `message` as the next transition, which under `Sync.mount`
|
|
1251
|
+
* means within this one: the mount applies it straight after the `update` that returned
|
|
1252
|
+
* it, and persists it if it is durable, before any other Message is processed. This is
|
|
1253
|
+
* how a local intent turns into a durable fact that needs something only the intent's
|
|
1254
|
+
* transition knows, such as an id minted from local state, without a later Message
|
|
1255
|
+
* seeing the Model before the fact. Elsewhere, or once a parent has mapped the Command,
|
|
1256
|
+
* it is an ordinary Command that yields `message`, and so is one a durable Message's
|
|
1257
|
+
* update returns: replay would not apply it, and with derived replay the durable Message's
|
|
1258
|
+
* persist fails with `ReplayError` as for any Command.
|
|
1259
|
+
*/
|
|
1260
|
+
const fact = (message) => Object.defineProperty({
|
|
1261
|
+
name: "foldkit-sync/fact",
|
|
1262
|
+
effect: Effect.succeed(message)
|
|
1263
|
+
}, FACT, {
|
|
1264
|
+
value: message,
|
|
1265
|
+
enumerable: false
|
|
1266
|
+
});
|
|
1267
|
+
const factOf = (command) => typeof command === "object" && command !== null && FACT in command ? { message: command[FACT] } : void 0;
|
|
1066
1268
|
/**
|
|
1067
1269
|
* Commits, rejections and acknowledgments change what the replica holds; a
|
|
1068
1270
|
* submit only echoes a local edit. An acknowledgment that does not return the
|
|
@@ -1081,8 +1283,12 @@ const mount = (app, sync, options) => {
|
|
|
1081
1283
|
const durable = new Set(sync.contract.messages);
|
|
1082
1284
|
const edits = [];
|
|
1083
1285
|
let tail = Promise.resolve();
|
|
1286
|
+
let installedFrom;
|
|
1287
|
+
let editedSinceInstall = false;
|
|
1084
1288
|
const install = (model) => {
|
|
1085
1289
|
const snapshot = Effect.runSync(replica.snapshot);
|
|
1290
|
+
installedFrom = snapshot.shared;
|
|
1291
|
+
editedSinceInstall = false;
|
|
1086
1292
|
const shared = unconfirmedEdits(edits, snapshot.nextLocalSequence).reduce((value, message) => {
|
|
1087
1293
|
try {
|
|
1088
1294
|
return sync.replay(value, message);
|
|
@@ -1107,14 +1313,14 @@ const mount = (app, sync, options) => {
|
|
|
1107
1313
|
}
|
|
1108
1314
|
};
|
|
1109
1315
|
const notifyCommitted = () => notifyEach(committedListeners, void 0);
|
|
1110
|
-
const
|
|
1316
|
+
const updatePrivate = (model, message) => {
|
|
1111
1317
|
switch (message._tag) {
|
|
1112
|
-
case REFRESH: return { model: install(model)
|
|
1318
|
+
case REFRESH: return !editedSinceInstall && Effect.runSync(replica.snapshot).shared === installedFrom ? { model } : reinstalled(install(model), model);
|
|
1113
1319
|
case PERSISTED: return { model };
|
|
1114
1320
|
case FAILED: {
|
|
1115
1321
|
const { error } = message;
|
|
1116
1322
|
const reverted = install(model);
|
|
1117
|
-
return
|
|
1323
|
+
return reinstalled(options.onPersistenceFailure?.(reverted, error) ?? reverted, model);
|
|
1118
1324
|
}
|
|
1119
1325
|
case NAVIGATE: {
|
|
1120
1326
|
const { request } = message;
|
|
@@ -1127,46 +1333,81 @@ const mount = (app, sync, options) => {
|
|
|
1127
1333
|
};
|
|
1128
1334
|
}
|
|
1129
1335
|
case NAVIGATED: return { model };
|
|
1130
|
-
default:
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1336
|
+
default: return absurd(message);
|
|
1337
|
+
}
|
|
1338
|
+
};
|
|
1339
|
+
const update = (model, message) => {
|
|
1340
|
+
if (isPrivate(message)) return updatePrivate(model, message);
|
|
1341
|
+
return transition(model, message);
|
|
1342
|
+
};
|
|
1343
|
+
const reinstalled = (next, previous) => options.onReinstall === void 0 ? { model: next } : applyFacts(options.onReinstall(next, previous));
|
|
1344
|
+
/** The application's transition for one of its Messages, with the persist a durable one needs. */
|
|
1345
|
+
const step = (model, message) => {
|
|
1346
|
+
notifyEach(messageListeners, message);
|
|
1347
|
+
const result = app.update(model, message);
|
|
1348
|
+
if (!durable.has(message._tag)) return result;
|
|
1349
|
+
editedSinceInstall = true;
|
|
1350
|
+
const edit = {
|
|
1351
|
+
message,
|
|
1352
|
+
started: void 0
|
|
1353
|
+
};
|
|
1354
|
+
edits.push(edit);
|
|
1355
|
+
const previous = tail;
|
|
1356
|
+
let settle;
|
|
1357
|
+
const done = new Promise((resolve) => {
|
|
1358
|
+
settle = resolve;
|
|
1359
|
+
});
|
|
1360
|
+
tail = done;
|
|
1361
|
+
const release = () => {
|
|
1362
|
+
const index = edits.indexOf(edit);
|
|
1363
|
+
if (index !== -1) edits.splice(index, 1);
|
|
1364
|
+
inFlight.delete(done);
|
|
1365
|
+
settle();
|
|
1366
|
+
};
|
|
1367
|
+
const persist = {
|
|
1368
|
+
name: "foldkit-sync/persist",
|
|
1369
|
+
effect: Effect.gen(function* () {
|
|
1370
|
+
inFlight.add(done);
|
|
1371
|
+
yield* Effect.promise(() => previous);
|
|
1372
|
+
edit.started = (yield* replica.snapshot).nextLocalSequence;
|
|
1373
|
+
const outcome = yield* Effect.result(replica.submit(edit.message));
|
|
1374
|
+
return outcome._tag === "Success" ? { _tag: PERSISTED } : {
|
|
1375
|
+
_tag: FAILED,
|
|
1376
|
+
error: outcome.failure
|
|
1167
1377
|
};
|
|
1378
|
+
}).pipe(Effect.ensuring(Effect.sync(release)))
|
|
1379
|
+
};
|
|
1380
|
+
return {
|
|
1381
|
+
model: result.model,
|
|
1382
|
+
commands: [...result.commands ?? [], persist]
|
|
1383
|
+
};
|
|
1384
|
+
};
|
|
1385
|
+
/**
|
|
1386
|
+
* A Message's transition, with the facts it returned applied after it. A durable
|
|
1387
|
+
* Message's are not: replay would not apply them, so they stay ordinary Commands.
|
|
1388
|
+
*/
|
|
1389
|
+
const transition = (model, message) => {
|
|
1390
|
+
const result = step(model, message);
|
|
1391
|
+
return durable.has(message._tag) ? result : applyFacts(result);
|
|
1392
|
+
};
|
|
1393
|
+
/** Applies the facts a transition returned, in order, each as the transition after it. */
|
|
1394
|
+
const applyFacts = (result) => {
|
|
1395
|
+
let model = result.model;
|
|
1396
|
+
const kept = [];
|
|
1397
|
+
for (const command of result.commands ?? []) {
|
|
1398
|
+
const found = factOf(command);
|
|
1399
|
+
if (found === void 0) {
|
|
1400
|
+
kept.push(command);
|
|
1401
|
+
continue;
|
|
1168
1402
|
}
|
|
1403
|
+
const next = transition(model, found.message);
|
|
1404
|
+
model = next.model;
|
|
1405
|
+
kept.push(...next.commands ?? []);
|
|
1169
1406
|
}
|
|
1407
|
+
return {
|
|
1408
|
+
model,
|
|
1409
|
+
commands: kept
|
|
1410
|
+
};
|
|
1170
1411
|
};
|
|
1171
1412
|
const ports = { inbound: { message: Port.inbound(app.Message) } };
|
|
1172
1413
|
const own = Subscription.make()(() => ({
|
|
@@ -1247,6 +1488,13 @@ const mount = (app, sync, options) => {
|
|
|
1247
1488
|
*/
|
|
1248
1489
|
const createPresence = Effect.fn("Presence.create")(function* (options) {
|
|
1249
1490
|
const ttl = Duration.toMillis(options.ttl);
|
|
1491
|
+
const throttle = options.throttle === void 0 ? 0 : Duration.toMillis(options.throttle);
|
|
1492
|
+
const scope = yield* Effect.scope;
|
|
1493
|
+
const outbound = yield* SynchronizedRef.make({
|
|
1494
|
+
lastSent: void 0,
|
|
1495
|
+
held: void 0,
|
|
1496
|
+
flusher: void 0
|
|
1497
|
+
});
|
|
1250
1498
|
const peers = yield* Ref.make(/* @__PURE__ */ new Map());
|
|
1251
1499
|
const closed = yield* Ref.make(false);
|
|
1252
1500
|
const signals = yield* PubSub.sliding(1);
|
|
@@ -1291,17 +1539,63 @@ const createPresence = Effect.fn("Presence.create")(function* (options) {
|
|
|
1291
1539
|
});
|
|
1292
1540
|
const channel = options.channel;
|
|
1293
1541
|
const consuming = channel === void 0 ? void 0 : yield* Stream.runForEach(Stream.fromPubSub(channel.updates), receive).pipe(Effect.forkScoped);
|
|
1542
|
+
const publish = (value) => channel.publish({
|
|
1543
|
+
id: options.id,
|
|
1544
|
+
value
|
|
1545
|
+
});
|
|
1546
|
+
/** Drops a value waiting to be sent, so nothing older goes out after what comes next. */
|
|
1547
|
+
const cancelHeld = SynchronizedRef.updateEffect(outbound, (state) => (state.flusher === void 0 ? Effect.void : Fiber.interrupt(state.flusher)).pipe(Effect.as({
|
|
1548
|
+
...state,
|
|
1549
|
+
held: void 0,
|
|
1550
|
+
flusher: void 0
|
|
1551
|
+
})));
|
|
1552
|
+
/** Sends what is held once per interval, until an interval passes with nothing held. */
|
|
1553
|
+
const flush = (wait) => Effect.gen(function* () {
|
|
1554
|
+
for (let delay = wait;; delay = throttle) {
|
|
1555
|
+
yield* Effect.sleep(delay);
|
|
1556
|
+
const now = yield* Clock.currentTimeMillis;
|
|
1557
|
+
const latest = yield* SynchronizedRef.modify(outbound, (state) => state.held === void 0 ? [void 0, {
|
|
1558
|
+
...state,
|
|
1559
|
+
flusher: void 0
|
|
1560
|
+
}] : [state.held, {
|
|
1561
|
+
...state,
|
|
1562
|
+
held: void 0,
|
|
1563
|
+
lastSent: now
|
|
1564
|
+
}]);
|
|
1565
|
+
if (latest === void 0) return;
|
|
1566
|
+
yield* publish(latest.value);
|
|
1567
|
+
}
|
|
1568
|
+
});
|
|
1569
|
+
const broadcast = (value, at) => Effect.gen(function* () {
|
|
1570
|
+
if (yield* SynchronizedRef.modifyEffect(outbound, (state) => Effect.gen(function* () {
|
|
1571
|
+
if (yield* Ref.get(closed)) return [false, state];
|
|
1572
|
+
if (state.flusher !== void 0) return [false, {
|
|
1573
|
+
...state,
|
|
1574
|
+
held: { value }
|
|
1575
|
+
}];
|
|
1576
|
+
const wait = state.lastSent === void 0 ? 0 : Math.min(throttle, state.lastSent + throttle - at);
|
|
1577
|
+
if (wait <= 0) return [true, {
|
|
1578
|
+
...state,
|
|
1579
|
+
lastSent: at
|
|
1580
|
+
}];
|
|
1581
|
+
const flusher = yield* Effect.forkIn(flush(wait), scope);
|
|
1582
|
+
return [false, {
|
|
1583
|
+
...state,
|
|
1584
|
+
held: { value },
|
|
1585
|
+
flusher
|
|
1586
|
+
}];
|
|
1587
|
+
}).pipe(Effect.uninterruptible))) yield* publish(value);
|
|
1588
|
+
});
|
|
1294
1589
|
const set = Effect.fn("Presence.set")(function* (value) {
|
|
1295
1590
|
if (yield* Ref.get(closed)) return;
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
value
|
|
1300
|
-
});
|
|
1591
|
+
const at = yield* Clock.currentTimeMillis;
|
|
1592
|
+
yield* put(options.id, value, at);
|
|
1593
|
+
if (channel !== void 0) yield* broadcast(value, at);
|
|
1301
1594
|
yield* notify();
|
|
1302
1595
|
});
|
|
1303
1596
|
const leave = Effect.fn("Presence.leave")(function* () {
|
|
1304
1597
|
if (yield* Ref.get(closed)) return;
|
|
1598
|
+
yield* cancelHeld;
|
|
1305
1599
|
yield* remove(options.id);
|
|
1306
1600
|
if (channel !== void 0) yield* channel.publish({
|
|
1307
1601
|
id: options.id,
|
|
@@ -1329,6 +1623,7 @@ const createPresence = Effect.fn("Presence.create")(function* (options) {
|
|
|
1329
1623
|
const close = Effect.fn("Presence.close")(function* () {
|
|
1330
1624
|
if (yield* Ref.get(closed)) return;
|
|
1331
1625
|
yield* Ref.set(closed, true);
|
|
1626
|
+
yield* cancelHeld;
|
|
1332
1627
|
if (consuming !== void 0) yield* Fiber.interrupt(consuming);
|
|
1333
1628
|
yield* Effect.sync(() => listeners.clear());
|
|
1334
1629
|
yield* PubSub.shutdown(signals);
|
|
@@ -1377,6 +1672,7 @@ const decodePresence = (data) => {
|
|
|
1377
1672
|
} catch {
|
|
1378
1673
|
return;
|
|
1379
1674
|
}
|
|
1675
|
+
if (typeof frame !== "object" || frame === null) return void 0;
|
|
1380
1676
|
const payload = frame[frameType];
|
|
1381
1677
|
if (typeof payload !== "object" || payload === null) return void 0;
|
|
1382
1678
|
const { id, value } = payload;
|
|
@@ -1445,6 +1741,8 @@ const Sync = {
|
|
|
1445
1741
|
define: defineSync,
|
|
1446
1742
|
/** Runs a Foldkit application over an open replica with one reducer. */
|
|
1447
1743
|
mount,
|
|
1744
|
+
/** A Command whose Message the mount applies within the transition that returned it. */
|
|
1745
|
+
fact,
|
|
1448
1746
|
/** Counters and a histogram an application can scrape. */
|
|
1449
1747
|
metrics: syncMetrics,
|
|
1450
1748
|
/** The only storage adapter: one IndexedDB database per document/replica. */
|
|
@@ -1482,6 +1780,6 @@ const Sync = {
|
|
|
1482
1780
|
}
|
|
1483
1781
|
};
|
|
1484
1782
|
//#endregion
|
|
1485
|
-
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, forApplication, indexedDb, layerFromPromise, layerLoopback, layerSocket, localSequence, loopbackPresenceChannel, lwwRegister, mount, nativeSocket, opId, openLwwClock, replicaId, sequence, servePresence, serveSocket, socketPresenceChannel, syncMetrics, toPromise };
|
|
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 };
|
|
1486
1784
|
|
|
1487
1785
|
//# sourceMappingURL=index.mjs.map
|