loro-repo 0.17.0 → 0.18.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/dist/index.cjs +720 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +720 -27
- package/dist/index.js.map +1 -1
- package/dist/storage/filesystem.cjs +59 -0
- package/dist/storage/filesystem.cjs.map +1 -1
- package/dist/storage/filesystem.d.cts +13 -1
- package/dist/storage/filesystem.d.ts +13 -1
- package/dist/storage/filesystem.js +59 -0
- package/dist/storage/filesystem.js.map +1 -1
- package/dist/storage/indexeddb.cjs +102 -10
- package/dist/storage/indexeddb.cjs.map +1 -1
- package/dist/storage/indexeddb.d.cts +14 -1
- package/dist/storage/indexeddb.d.ts +14 -1
- package/dist/storage/indexeddb.js +102 -10
- package/dist/storage/indexeddb.js.map +1 -1
- package/dist/storage/sqlite.cjs +77 -0
- package/dist/storage/sqlite.cjs.map +1 -1
- package/dist/storage/sqlite.d.cts +7 -1
- package/dist/storage/sqlite.d.ts +7 -1
- package/dist/storage/sqlite.js +77 -0
- package/dist/storage/sqlite.js.map +1 -1
- package/dist/transport/broadcast-channel.cjs +131 -0
- package/dist/transport/broadcast-channel.cjs.map +1 -1
- package/dist/transport/broadcast-channel.d.cts +8 -1
- package/dist/transport/broadcast-channel.d.ts +8 -1
- package/dist/transport/broadcast-channel.js +131 -0
- package/dist/transport/broadcast-channel.js.map +1 -1
- package/dist/transport/streams.cjs +188 -0
- package/dist/transport/streams.cjs.map +1 -1
- package/dist/transport/streams.d.cts +27 -1
- package/dist/transport/streams.d.ts +27 -1
- package/dist/transport/streams.js +188 -0
- package/dist/transport/streams.js.map +1 -1
- package/dist/transport/websocket.cjs +196 -0
- package/dist/transport/websocket.cjs.map +1 -1
- package/dist/transport/websocket.d.cts +23 -2
- package/dist/transport/websocket.d.ts +23 -2
- package/dist/transport/websocket.js +196 -0
- package/dist/transport/websocket.js.map +1 -1
- package/dist/types.d.cts +122 -1
- package/dist/types.d.ts +122 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -60,12 +60,13 @@ var RepoEventBus = class {
|
|
|
60
60
|
return index >= 0 ? this.eventByStack[index] : defaultBy;
|
|
61
61
|
}
|
|
62
62
|
shouldNotify(filter, event) {
|
|
63
|
-
if (!filter.docIds && !filter.kinds && !filter.metadataFields && !filter.by) return true;
|
|
63
|
+
if (!filter.docIds && !filter.flockDocIds && !filter.kinds && !filter.metadataFields && !filter.by) return true;
|
|
64
64
|
if (filter.kinds && !filter.kinds.includes(event.kind)) return false;
|
|
65
65
|
if (filter.by && !filter.by.includes(event.by)) return false;
|
|
66
|
-
const docId = event.docId;
|
|
66
|
+
const docId = "docId" in event ? event.docId : void 0;
|
|
67
|
+
const flockDocId = "flockDocId" in event ? event.flockDocId : void 0;
|
|
67
68
|
if (filter.docIds && docId && !filter.docIds.includes(docId)) return false;
|
|
68
|
-
if (filter.
|
|
69
|
+
if (filter.flockDocIds && flockDocId && !filter.flockDocIds.includes(flockDocId)) return false;
|
|
69
70
|
if (filter.metadataFields && event.kind === "doc-metadata") {
|
|
70
71
|
if (!Object.keys(event.patch).some((key) => filter.metadataFields?.includes(key))) return false;
|
|
71
72
|
}
|
|
@@ -82,11 +83,11 @@ var RepoEventBus = class {
|
|
|
82
83
|
* before the event loop processes macro tasks, so they would not unblock the
|
|
83
84
|
* loop between CPU-heavy doc exports.
|
|
84
85
|
*/
|
|
85
|
-
function yieldToMacroTask() {
|
|
86
|
+
function yieldToMacroTask$1() {
|
|
86
87
|
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
87
88
|
}
|
|
88
|
-
const YIELD_BUDGET_MS = 5;
|
|
89
|
-
const nowMs = typeof globalThis.performance?.now === "function" ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
|
|
89
|
+
const YIELD_BUDGET_MS$1 = 5;
|
|
90
|
+
const nowMs$1 = typeof globalThis.performance?.now === "function" ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
|
|
90
91
|
var DocManager = class {
|
|
91
92
|
storage;
|
|
92
93
|
ensureDocExists;
|
|
@@ -338,22 +339,335 @@ var DocManager = class {
|
|
|
338
339
|
const docId = batch[i];
|
|
339
340
|
const doc = this.docs.get(docId);
|
|
340
341
|
if (!doc) continue;
|
|
341
|
-
const start = nowMs();
|
|
342
|
+
const start = nowMs$1();
|
|
342
343
|
try {
|
|
343
344
|
await this.persistDocUpdate(docId, doc);
|
|
344
345
|
} catch (error) {
|
|
345
346
|
requeue.push(docId);
|
|
346
347
|
if (firstError === void 0) firstError = error;
|
|
347
348
|
}
|
|
349
|
+
accumulatedMs += nowMs$1() - start;
|
|
350
|
+
const moreInBatch = i < batch.length - 1;
|
|
351
|
+
if (accumulatedMs > YIELD_BUDGET_MS$1 && (moreInBatch || this.dirtyDocs.size > 0)) {
|
|
352
|
+
await yieldToMacroTask$1();
|
|
353
|
+
accumulatedMs = 0;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
for (const docId of requeue) this.dirtyDocs.add(docId);
|
|
358
|
+
if (firstError !== void 0) throw firstError;
|
|
359
|
+
}
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
//#endregion
|
|
363
|
+
//#region src/internal/flock-doc-manager.ts
|
|
364
|
+
function yieldToMacroTask() {
|
|
365
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
366
|
+
}
|
|
367
|
+
const YIELD_BUDGET_MS = 5;
|
|
368
|
+
const nowMs = typeof globalThis.performance?.now === "function" ? globalThis.performance.now.bind(globalThis.performance) : Date.now;
|
|
369
|
+
function stableVersion(vector) {
|
|
370
|
+
const sorted = {};
|
|
371
|
+
for (const key of Object.keys(vector).sort()) sorted[key] = vector[key];
|
|
372
|
+
return JSON.stringify(sorted);
|
|
373
|
+
}
|
|
374
|
+
function versionEquals(a, b) {
|
|
375
|
+
return stableVersion(a) === stableVersion(b);
|
|
376
|
+
}
|
|
377
|
+
function hasEmptyVersion(vector) {
|
|
378
|
+
return Object.keys(vector).length === 0;
|
|
379
|
+
}
|
|
380
|
+
function hasVisibleRows(flock) {
|
|
381
|
+
const rows = flock.scan({});
|
|
382
|
+
for (const row of rows) if (row.value !== void 0) return true;
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
async function exportJsonUpdate(flock, from) {
|
|
386
|
+
if (hasEmptyVersion(from)) return flock.exportJson();
|
|
387
|
+
return flock.exportJson({ from });
|
|
388
|
+
}
|
|
389
|
+
function hasBundleEntries(bundle) {
|
|
390
|
+
return Object.keys(bundle.entries).length > 0;
|
|
391
|
+
}
|
|
392
|
+
var FlockDocManager = class {
|
|
393
|
+
storage;
|
|
394
|
+
ensureFlockDocExists;
|
|
395
|
+
retentionMs;
|
|
396
|
+
persistDebounceMs;
|
|
397
|
+
flocks = /* @__PURE__ */ new Map();
|
|
398
|
+
subscriptions = /* @__PURE__ */ new Map();
|
|
399
|
+
persistedVersions = /* @__PURE__ */ new Map();
|
|
400
|
+
pendingLoads = /* @__PURE__ */ new Map();
|
|
401
|
+
dirtyFlockDocs = /* @__PURE__ */ new Set();
|
|
402
|
+
existenceMaterialized = /* @__PURE__ */ new Set();
|
|
403
|
+
docsWithVisibleContent = /* @__PURE__ */ new Set();
|
|
404
|
+
debounceTimer;
|
|
405
|
+
flushScheduled = false;
|
|
406
|
+
operationPromise = Promise.resolve();
|
|
407
|
+
destroyed = false;
|
|
408
|
+
constructor(options) {
|
|
409
|
+
this.storage = options.storage;
|
|
410
|
+
this.ensureFlockDocExists = options.ensureFlockDocExists;
|
|
411
|
+
this.retentionMs = options.retentionMs;
|
|
412
|
+
const configured = options.persistDebounceMs;
|
|
413
|
+
this.persistDebounceMs = typeof configured === "number" && Number.isFinite(configured) && configured >= 0 ? configured : 0;
|
|
414
|
+
}
|
|
415
|
+
async openFlockDoc(flockDocId) {
|
|
416
|
+
return this.ensureFlockDoc(flockDocId);
|
|
417
|
+
}
|
|
418
|
+
async openDetachedFlockDoc(flockDocId) {
|
|
419
|
+
const cached = this.flocks.get(flockDocId);
|
|
420
|
+
if (cached) return __loro_dev_flock_wasm.Flock.fromFile(cached.exportFile());
|
|
421
|
+
const stored = await this.storage?.loadFlockDoc?.(flockDocId);
|
|
422
|
+
if (stored) return __loro_dev_flock_wasm.Flock.fromFile(stored.exportFile());
|
|
423
|
+
return new __loro_dev_flock_wasm.Flock();
|
|
424
|
+
}
|
|
425
|
+
async ensureFlockDoc(flockDocId) {
|
|
426
|
+
const cached = this.flocks.get(flockDocId);
|
|
427
|
+
if (cached) {
|
|
428
|
+
this.ensureSubscription(flockDocId, cached);
|
|
429
|
+
if (!this.persistedVersions.has(flockDocId)) this.persistedVersions.set(flockDocId, cached.version());
|
|
430
|
+
return cached;
|
|
431
|
+
}
|
|
432
|
+
const pending = this.pendingLoads.get(flockDocId);
|
|
433
|
+
if (pending) return pending;
|
|
434
|
+
const loadPromise = this.loadOrCreateFlockDoc(flockDocId);
|
|
435
|
+
this.pendingLoads.set(flockDocId, loadPromise);
|
|
436
|
+
try {
|
|
437
|
+
return await loadPromise;
|
|
438
|
+
} finally {
|
|
439
|
+
this.pendingLoads.delete(flockDocId);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
async persistFlockDoc(flockDocId, flock) {
|
|
443
|
+
const previousVersion = this.persistedVersions.get(flockDocId);
|
|
444
|
+
const snapshot = flock.exportFile();
|
|
445
|
+
const nextVersion = flock.version();
|
|
446
|
+
const versionAtCapture = flock.version();
|
|
447
|
+
if (!this.storage) {
|
|
448
|
+
this.persistedVersions.set(flockDocId, nextVersion);
|
|
449
|
+
await this.materializeExistence(flockDocId, flock);
|
|
450
|
+
this.clearDirtyIfStable(flockDocId, versionAtCapture, flock);
|
|
451
|
+
return;
|
|
452
|
+
}
|
|
453
|
+
this.persistedVersions.set(flockDocId, nextVersion);
|
|
454
|
+
try {
|
|
455
|
+
await this.storage.save({
|
|
456
|
+
type: "flock-doc-snapshot",
|
|
457
|
+
flockDocId,
|
|
458
|
+
snapshot
|
|
459
|
+
});
|
|
460
|
+
await this.materializeExistence(flockDocId, flock);
|
|
461
|
+
this.clearDirtyIfStable(flockDocId, versionAtCapture, flock);
|
|
462
|
+
} catch (error) {
|
|
463
|
+
if (previousVersion) this.persistedVersions.set(flockDocId, previousVersion);
|
|
464
|
+
else this.persistedVersions.delete(flockDocId);
|
|
465
|
+
throw error;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async unloadFlockDoc(flockDocId) {
|
|
469
|
+
const flock = this.flocks.get(flockDocId);
|
|
470
|
+
if (!flock) return;
|
|
471
|
+
await this.persistFlockDocUpdate(flockDocId, flock);
|
|
472
|
+
this.subscriptions.get(flockDocId)?.();
|
|
473
|
+
this.subscriptions.delete(flockDocId);
|
|
474
|
+
this.flocks.delete(flockDocId);
|
|
475
|
+
this.persistedVersions.delete(flockDocId);
|
|
476
|
+
this.existenceMaterialized.delete(flockDocId);
|
|
477
|
+
this.docsWithVisibleContent.delete(flockDocId);
|
|
478
|
+
}
|
|
479
|
+
async dropFlockDoc(flockDocId) {
|
|
480
|
+
await this.enqueueOperation(async () => {
|
|
481
|
+
this.subscriptions.get(flockDocId)?.();
|
|
482
|
+
this.subscriptions.delete(flockDocId);
|
|
483
|
+
this.flocks.delete(flockDocId);
|
|
484
|
+
this.persistedVersions.delete(flockDocId);
|
|
485
|
+
this.dirtyFlockDocs.delete(flockDocId);
|
|
486
|
+
this.existenceMaterialized.delete(flockDocId);
|
|
487
|
+
this.docsWithVisibleContent.delete(flockDocId);
|
|
488
|
+
await this.storage?.deleteFlockDoc?.(flockDocId);
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
async flush() {
|
|
492
|
+
this.cancelDebounceTimer();
|
|
493
|
+
this.flushScheduled = false;
|
|
494
|
+
while (true) {
|
|
495
|
+
await this.enqueueOperation(() => this.drainDirtyFlockDocs());
|
|
496
|
+
if (this.dirtyFlockDocs.size === 0 && !this.flushScheduled) break;
|
|
497
|
+
this.flushScheduled = false;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
async close() {
|
|
501
|
+
this.destroyed = true;
|
|
502
|
+
this.cancelDebounceTimer();
|
|
503
|
+
try {
|
|
504
|
+
await this.flush();
|
|
505
|
+
} finally {
|
|
506
|
+
for (const unsubscribe of this.subscriptions.values()) try {
|
|
507
|
+
unsubscribe();
|
|
508
|
+
} catch {}
|
|
509
|
+
this.subscriptions.clear();
|
|
510
|
+
this.flocks.clear();
|
|
511
|
+
this.persistedVersions.clear();
|
|
512
|
+
this.pendingLoads.clear();
|
|
513
|
+
this.dirtyFlockDocs.clear();
|
|
514
|
+
this.existenceMaterialized.clear();
|
|
515
|
+
this.docsWithVisibleContent.clear();
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
async loadOrCreateFlockDoc(flockDocId) {
|
|
519
|
+
const cached = this.flocks.get(flockDocId);
|
|
520
|
+
if (cached) {
|
|
521
|
+
this.ensureSubscription(flockDocId, cached);
|
|
522
|
+
if (!this.persistedVersions.has(flockDocId)) this.persistedVersions.set(flockDocId, cached.version());
|
|
523
|
+
return cached;
|
|
524
|
+
}
|
|
525
|
+
const flock = await this.storage?.loadFlockDoc?.(flockDocId) ?? new __loro_dev_flock_wasm.Flock();
|
|
526
|
+
this.registerFlockDoc(flockDocId, flock);
|
|
527
|
+
await this.materializeExistence(flockDocId, flock);
|
|
528
|
+
return flock;
|
|
529
|
+
}
|
|
530
|
+
registerFlockDoc(flockDocId, flock) {
|
|
531
|
+
this.flocks.set(flockDocId, flock);
|
|
532
|
+
this.persistedVersions.set(flockDocId, flock.version());
|
|
533
|
+
if (hasVisibleRows(flock)) this.docsWithVisibleContent.add(flockDocId);
|
|
534
|
+
this.ensureSubscription(flockDocId, flock);
|
|
535
|
+
}
|
|
536
|
+
ensureSubscription(flockDocId, flock) {
|
|
537
|
+
if (this.subscriptions.has(flockDocId)) return;
|
|
538
|
+
const unsubscribe = flock.subscribe(() => {
|
|
539
|
+
this.onFlockDocEvent(flockDocId);
|
|
540
|
+
});
|
|
541
|
+
if (typeof unsubscribe === "function") this.subscriptions.set(flockDocId, unsubscribe);
|
|
542
|
+
}
|
|
543
|
+
async persistFlockDocUpdate(flockDocId, flock) {
|
|
544
|
+
const previousVersion = this.persistedVersions.get(flockDocId);
|
|
545
|
+
const nextVersion = flock.version();
|
|
546
|
+
if (!this.storage) {
|
|
547
|
+
try {
|
|
548
|
+
await this.materializeExistence(flockDocId, flock);
|
|
549
|
+
this.persistedVersions.set(flockDocId, nextVersion);
|
|
550
|
+
this.clearDirtyIfStable(flockDocId, nextVersion, flock);
|
|
551
|
+
} catch (error) {
|
|
552
|
+
if (previousVersion) this.persistedVersions.set(flockDocId, previousVersion);
|
|
553
|
+
else this.persistedVersions.delete(flockDocId);
|
|
554
|
+
throw error;
|
|
555
|
+
}
|
|
556
|
+
return;
|
|
557
|
+
}
|
|
558
|
+
if (!previousVersion) {
|
|
559
|
+
await this.persistFlockDoc(flockDocId, flock);
|
|
560
|
+
this.persistedVersions.set(flockDocId, nextVersion);
|
|
561
|
+
this.clearDirtyIfStable(flockDocId, nextVersion, flock);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
if (versionEquals(previousVersion, nextVersion)) {
|
|
565
|
+
await this.materializeExistence(flockDocId, flock);
|
|
566
|
+
this.dirtyFlockDocs.delete(flockDocId);
|
|
567
|
+
return;
|
|
568
|
+
}
|
|
569
|
+
const bundle = await exportJsonUpdate(flock, previousVersion);
|
|
570
|
+
if (!hasBundleEntries(bundle)) {
|
|
571
|
+
await this.materializeExistence(flockDocId, flock);
|
|
572
|
+
this.persistedVersions.set(flockDocId, nextVersion);
|
|
573
|
+
this.dirtyFlockDocs.delete(flockDocId);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
this.persistedVersions.set(flockDocId, nextVersion);
|
|
577
|
+
try {
|
|
578
|
+
await this.storage.save({
|
|
579
|
+
type: "flock-doc-update",
|
|
580
|
+
flockDocId,
|
|
581
|
+
update: require_meta_snapshot.encodeMetaJsonUpdate(bundle)
|
|
582
|
+
});
|
|
583
|
+
await this.materializeExistence(flockDocId, flock);
|
|
584
|
+
this.clearDirtyIfStable(flockDocId, nextVersion, flock);
|
|
585
|
+
} catch (error) {
|
|
586
|
+
this.persistedVersions.set(flockDocId, previousVersion);
|
|
587
|
+
throw error;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
async materializeExistence(flockDocId, flock) {
|
|
591
|
+
if (this.existenceMaterialized.has(flockDocId)) return;
|
|
592
|
+
if (!this.docsWithVisibleContent.has(flockDocId)) {
|
|
593
|
+
if (!hasVisibleRows(flock)) return;
|
|
594
|
+
this.docsWithVisibleContent.add(flockDocId);
|
|
595
|
+
}
|
|
596
|
+
await this.ensureFlockDocExists(flockDocId, this.computeExpiresAtMs(flockDocId));
|
|
597
|
+
this.existenceMaterialized.add(flockDocId);
|
|
598
|
+
}
|
|
599
|
+
computeExpiresAtMs(flockDocId) {
|
|
600
|
+
const duration = this.retentionMs?.(flockDocId);
|
|
601
|
+
if (duration === void 0 || !Number.isFinite(duration) || duration < 0) return;
|
|
602
|
+
return Date.now() + duration;
|
|
603
|
+
}
|
|
604
|
+
clearDirtyIfStable(flockDocId, capturedVersion, flock) {
|
|
605
|
+
if (versionEquals(flock.version(), capturedVersion)) this.dirtyFlockDocs.delete(flockDocId);
|
|
606
|
+
}
|
|
607
|
+
onFlockDocEvent(flockDocId) {
|
|
608
|
+
const flock = this.flocks.get(flockDocId);
|
|
609
|
+
if (!flock) return;
|
|
610
|
+
if (!this.docsWithVisibleContent.has(flockDocId) && hasVisibleRows(flock)) this.docsWithVisibleContent.add(flockDocId);
|
|
611
|
+
this.dirtyFlockDocs.add(flockDocId);
|
|
612
|
+
this.scheduleDirtyFlush();
|
|
613
|
+
}
|
|
614
|
+
scheduleDirtyFlush() {
|
|
615
|
+
if (this.destroyed) return;
|
|
616
|
+
if (this.persistDebounceMs > 0) {
|
|
617
|
+
if (this.debounceTimer) clearTimeout(this.debounceTimer);
|
|
618
|
+
this.flushScheduled = true;
|
|
619
|
+
this.debounceTimer = setTimeout(() => {
|
|
620
|
+
this.debounceTimer = void 0;
|
|
621
|
+
this.flushScheduled = false;
|
|
622
|
+
this.enqueueOperation(() => this.drainDirtyFlockDocs()).catch(require_logging.logAsyncError("flock doc dirty flush"));
|
|
623
|
+
}, this.persistDebounceMs);
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
if (this.flushScheduled) return;
|
|
627
|
+
this.flushScheduled = true;
|
|
628
|
+
queueMicrotask(() => {
|
|
629
|
+
this.flushScheduled = false;
|
|
630
|
+
this.enqueueOperation(() => this.drainDirtyFlockDocs()).catch(require_logging.logAsyncError("flock doc dirty flush"));
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
cancelDebounceTimer() {
|
|
634
|
+
if (this.debounceTimer) {
|
|
635
|
+
clearTimeout(this.debounceTimer);
|
|
636
|
+
this.debounceTimer = void 0;
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
enqueueOperation(operation) {
|
|
640
|
+
const run = this.operationPromise.catch(() => {}).then(operation);
|
|
641
|
+
this.operationPromise = run.then(() => {}, () => {});
|
|
642
|
+
return run;
|
|
643
|
+
}
|
|
644
|
+
async drainDirtyFlockDocs() {
|
|
645
|
+
let accumulatedMs = 0;
|
|
646
|
+
let firstError = void 0;
|
|
647
|
+
const requeue = [];
|
|
648
|
+
while (this.dirtyFlockDocs.size > 0 && firstError === void 0) {
|
|
649
|
+
const batch = [...this.dirtyFlockDocs];
|
|
650
|
+
this.dirtyFlockDocs.clear();
|
|
651
|
+
for (let i = 0; i < batch.length; i += 1) {
|
|
652
|
+
const flockDocId = batch[i];
|
|
653
|
+
const flock = this.flocks.get(flockDocId);
|
|
654
|
+
if (!flock) continue;
|
|
655
|
+
const start = nowMs();
|
|
656
|
+
try {
|
|
657
|
+
await this.persistFlockDocUpdate(flockDocId, flock);
|
|
658
|
+
} catch (error) {
|
|
659
|
+
requeue.push(flockDocId);
|
|
660
|
+
if (firstError === void 0) firstError = error;
|
|
661
|
+
}
|
|
348
662
|
accumulatedMs += nowMs() - start;
|
|
349
663
|
const moreInBatch = i < batch.length - 1;
|
|
350
|
-
if (accumulatedMs > YIELD_BUDGET_MS && (moreInBatch || this.
|
|
664
|
+
if (accumulatedMs > YIELD_BUDGET_MS && (moreInBatch || this.dirtyFlockDocs.size > 0)) {
|
|
351
665
|
await yieldToMacroTask();
|
|
352
666
|
accumulatedMs = 0;
|
|
353
667
|
}
|
|
354
668
|
}
|
|
355
669
|
}
|
|
356
|
-
for (const
|
|
670
|
+
for (const flockDocId of requeue) this.dirtyFlockDocs.add(flockDocId);
|
|
357
671
|
if (firstError !== void 0) throw firstError;
|
|
358
672
|
}
|
|
359
673
|
};
|
|
@@ -790,21 +1104,215 @@ var MetadataManager = class {
|
|
|
790
1104
|
}
|
|
791
1105
|
};
|
|
792
1106
|
|
|
1107
|
+
//#endregion
|
|
1108
|
+
//#region src/internal/flock-doc-registry.ts
|
|
1109
|
+
var FlockDocRegistry = class {
|
|
1110
|
+
getMetaFlock;
|
|
1111
|
+
eventBus;
|
|
1112
|
+
existenceState = /* @__PURE__ */ new Map();
|
|
1113
|
+
constructor(options) {
|
|
1114
|
+
this.getMetaFlock = options.getMetaFlock;
|
|
1115
|
+
this.eventBus = options.eventBus;
|
|
1116
|
+
this.seedExistenceStateFromFlock();
|
|
1117
|
+
}
|
|
1118
|
+
async getIds(options = {}) {
|
|
1119
|
+
const result = [];
|
|
1120
|
+
const rows = this.metaFlock.scan({ prefix: ["ef"] });
|
|
1121
|
+
for (const row of rows) {
|
|
1122
|
+
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
1123
|
+
const flockDocId = row.key[1];
|
|
1124
|
+
if (typeof flockDocId !== "string") continue;
|
|
1125
|
+
if (typeof row.value !== "boolean") continue;
|
|
1126
|
+
if (options.activeOnly && row.value !== true) continue;
|
|
1127
|
+
if (options.nowMs !== void 0 && options.includeExpired !== true && this.isExpired(flockDocId, options.nowMs)) continue;
|
|
1128
|
+
result.push(flockDocId);
|
|
1129
|
+
}
|
|
1130
|
+
return result;
|
|
1131
|
+
}
|
|
1132
|
+
async get(flockDocId) {
|
|
1133
|
+
const active = this.readExistsFromFlock(flockDocId);
|
|
1134
|
+
if (active === void 0) return void 0;
|
|
1135
|
+
return {
|
|
1136
|
+
deleted: !active,
|
|
1137
|
+
expiresAtMs: this.readExpiresAtMs(flockDocId)
|
|
1138
|
+
};
|
|
1139
|
+
}
|
|
1140
|
+
async getDeleted(flockDocId) {
|
|
1141
|
+
const active = this.readExistsFromFlock(flockDocId);
|
|
1142
|
+
return active === void 0 ? void 0 : !active;
|
|
1143
|
+
}
|
|
1144
|
+
async ensureExists(flockDocId, expiresAtMs) {
|
|
1145
|
+
const previous = this.readTrackedExistenceState(flockDocId);
|
|
1146
|
+
const current = this.readExistsFromFlock(flockDocId);
|
|
1147
|
+
if (current === void 0) this.metaFlock.put(["ef", flockDocId], true);
|
|
1148
|
+
if (this.shouldWriteExpiresAt(flockDocId, expiresAtMs)) this.metaFlock.put(["efx", flockDocId], expiresAtMs);
|
|
1149
|
+
if (current !== void 0) return;
|
|
1150
|
+
this.reconcile(flockDocId, "local", { previousExistence: previous });
|
|
1151
|
+
}
|
|
1152
|
+
async markDeleted(flockDocId) {
|
|
1153
|
+
const previous = this.readTrackedExistenceState(flockDocId);
|
|
1154
|
+
if (this.readExistsFromFlock(flockDocId) === false) return;
|
|
1155
|
+
this.metaFlock.put(["ef", flockDocId], false);
|
|
1156
|
+
this.reconcile(flockDocId, "local", { previousExistence: previous });
|
|
1157
|
+
}
|
|
1158
|
+
async restore(flockDocId) {
|
|
1159
|
+
const previous = this.readTrackedExistenceState(flockDocId);
|
|
1160
|
+
if (this.readExistsFromFlock(flockDocId) !== false) return;
|
|
1161
|
+
this.metaFlock.put(["ef", flockDocId], true);
|
|
1162
|
+
this.reconcile(flockDocId, "local", { previousExistence: previous });
|
|
1163
|
+
}
|
|
1164
|
+
async list(query) {
|
|
1165
|
+
if (query?.limit !== void 0 && query.limit <= 0) return [];
|
|
1166
|
+
const { startKey, endKey } = this.computeRangeKeys(query);
|
|
1167
|
+
if (startKey && endKey && startKey >= endKey) return [];
|
|
1168
|
+
const scanOptions = { prefix: ["ef"] };
|
|
1169
|
+
if (startKey) scanOptions.start = {
|
|
1170
|
+
kind: "inclusive",
|
|
1171
|
+
key: ["ef", startKey]
|
|
1172
|
+
};
|
|
1173
|
+
if (endKey) scanOptions.end = {
|
|
1174
|
+
kind: "exclusive",
|
|
1175
|
+
key: ["ef", endKey]
|
|
1176
|
+
};
|
|
1177
|
+
const entries = [];
|
|
1178
|
+
const rows = this.metaFlock.scan(scanOptions);
|
|
1179
|
+
for (const row of rows) {
|
|
1180
|
+
if (query?.limit !== void 0 && entries.length >= query.limit) break;
|
|
1181
|
+
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
1182
|
+
const flockDocId = row.key[1];
|
|
1183
|
+
if (typeof flockDocId !== "string") continue;
|
|
1184
|
+
if (typeof row.value !== "boolean") continue;
|
|
1185
|
+
if (!matchesQuery(flockDocId, {}, query)) continue;
|
|
1186
|
+
entries.push({
|
|
1187
|
+
flockDocId,
|
|
1188
|
+
deleted: !row.value,
|
|
1189
|
+
expiresAtMs: this.readExpiresAtMs(flockDocId)
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
return entries;
|
|
1193
|
+
}
|
|
1194
|
+
getExpiredIds(nowMs$2) {
|
|
1195
|
+
const expired = [];
|
|
1196
|
+
const rows = this.metaFlock.scan({ prefix: ["efx"] });
|
|
1197
|
+
for (const row of rows) {
|
|
1198
|
+
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
1199
|
+
const flockDocId = row.key[1];
|
|
1200
|
+
if (typeof flockDocId !== "string") continue;
|
|
1201
|
+
const expiresAtMs = typeof row.value === "number" && Number.isFinite(row.value) ? row.value : void 0;
|
|
1202
|
+
if (expiresAtMs === void 0 || expiresAtMs > nowMs$2) continue;
|
|
1203
|
+
if (this.readExistsFromFlock(flockDocId) === void 0) continue;
|
|
1204
|
+
expired.push(flockDocId);
|
|
1205
|
+
}
|
|
1206
|
+
return expired;
|
|
1207
|
+
}
|
|
1208
|
+
purgeKeyspace(flockDocId) {
|
|
1209
|
+
this.metaFlock.delete(["ef", flockDocId]);
|
|
1210
|
+
this.metaFlock.delete(["efx", flockDocId]);
|
|
1211
|
+
}
|
|
1212
|
+
reconcile(flockDocId, by, options = {}) {
|
|
1213
|
+
const previousExistence = options.previousExistence ?? this.readTrackedExistenceState(flockDocId);
|
|
1214
|
+
const nextExistence = this.readExistenceStateFromFlock(flockDocId);
|
|
1215
|
+
this.applyExistenceState(flockDocId, previousExistence, nextExistence, by);
|
|
1216
|
+
}
|
|
1217
|
+
clear() {
|
|
1218
|
+
this.existenceState.clear();
|
|
1219
|
+
}
|
|
1220
|
+
getExistenceState(flockDocId) {
|
|
1221
|
+
return this.existenceState.get(flockDocId) ?? this.readExistenceStateFromFlock(flockDocId);
|
|
1222
|
+
}
|
|
1223
|
+
computeRangeKeys(query) {
|
|
1224
|
+
if (!query) return {};
|
|
1225
|
+
const prefix = query.prefix && query.prefix.length > 0 ? query.prefix : void 0;
|
|
1226
|
+
let startKey = query.start;
|
|
1227
|
+
if (prefix) startKey = !startKey || prefix > startKey ? prefix : startKey;
|
|
1228
|
+
let endKey = query.end;
|
|
1229
|
+
const prefixEnd = this.nextLexicographicString(prefix);
|
|
1230
|
+
if (prefixEnd) endKey = !endKey || prefixEnd < endKey ? prefixEnd : endKey;
|
|
1231
|
+
return {
|
|
1232
|
+
startKey,
|
|
1233
|
+
endKey
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
nextLexicographicString(value) {
|
|
1237
|
+
if (!value) return void 0;
|
|
1238
|
+
for (let i = value.length - 1; i >= 0; i -= 1) {
|
|
1239
|
+
const code = value.charCodeAt(i);
|
|
1240
|
+
if (code < 65535) return `${value.slice(0, i)}${String.fromCharCode(code + 1)}`;
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
readExistsFromFlock(flockDocId) {
|
|
1244
|
+
const raw = this.metaFlock.get(["ef", flockDocId]);
|
|
1245
|
+
return typeof raw === "boolean" ? raw : void 0;
|
|
1246
|
+
}
|
|
1247
|
+
readExpiresAtMs(flockDocId) {
|
|
1248
|
+
const raw = this.metaFlock.get(["efx", flockDocId]);
|
|
1249
|
+
return typeof raw === "number" && Number.isFinite(raw) ? raw : void 0;
|
|
1250
|
+
}
|
|
1251
|
+
shouldWriteExpiresAt(flockDocId, expiresAtMs) {
|
|
1252
|
+
if (expiresAtMs === void 0 || !Number.isFinite(expiresAtMs) || expiresAtMs < 0) return false;
|
|
1253
|
+
const current = this.readExpiresAtMs(flockDocId);
|
|
1254
|
+
return current === void 0 || expiresAtMs > current;
|
|
1255
|
+
}
|
|
1256
|
+
isExpired(flockDocId, nowMs$2) {
|
|
1257
|
+
const expiresAtMs = this.readExpiresAtMs(flockDocId);
|
|
1258
|
+
return expiresAtMs !== void 0 && expiresAtMs <= nowMs$2;
|
|
1259
|
+
}
|
|
1260
|
+
applyExistenceState(flockDocId, previous, next, by) {
|
|
1261
|
+
if (previous === next) return;
|
|
1262
|
+
if (next === "missing") this.existenceState.delete(flockDocId);
|
|
1263
|
+
else this.existenceState.set(flockDocId, next);
|
|
1264
|
+
this.eventBus.emit({
|
|
1265
|
+
kind: "flock-doc-existence-changed",
|
|
1266
|
+
flockDocId,
|
|
1267
|
+
from: previous,
|
|
1268
|
+
to: next,
|
|
1269
|
+
by
|
|
1270
|
+
});
|
|
1271
|
+
}
|
|
1272
|
+
seedExistenceStateFromFlock() {
|
|
1273
|
+
this.existenceState.clear();
|
|
1274
|
+
const rows = this.metaFlock.scan({ prefix: ["ef"] });
|
|
1275
|
+
for (const row of rows) {
|
|
1276
|
+
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
1277
|
+
const flockDocId = row.key[1];
|
|
1278
|
+
if (typeof flockDocId !== "string") continue;
|
|
1279
|
+
if (typeof row.value !== "boolean") continue;
|
|
1280
|
+
this.existenceState.set(flockDocId, row.value ? "active" : "deleted");
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
readTrackedExistenceState(flockDocId) {
|
|
1284
|
+
return this.existenceState.get(flockDocId) ?? "missing";
|
|
1285
|
+
}
|
|
1286
|
+
readExistenceStateFromFlock(flockDocId) {
|
|
1287
|
+
const current = this.readExistsFromFlock(flockDocId);
|
|
1288
|
+
if (current === void 0) return "missing";
|
|
1289
|
+
return current ? "active" : "deleted";
|
|
1290
|
+
}
|
|
1291
|
+
get metaFlock() {
|
|
1292
|
+
return this.getMetaFlock();
|
|
1293
|
+
}
|
|
1294
|
+
};
|
|
1295
|
+
|
|
793
1296
|
//#endregion
|
|
794
1297
|
//#region src/internal/flock-hydrator.ts
|
|
795
1298
|
var FlockHydrator = class {
|
|
796
1299
|
getMetaFlock;
|
|
797
1300
|
metadataManager;
|
|
1301
|
+
flockDocRegistry;
|
|
798
1302
|
docManager;
|
|
1303
|
+
flockDocManager;
|
|
799
1304
|
constructor(options) {
|
|
800
1305
|
this.getMetaFlock = options.getMetaFlock;
|
|
801
1306
|
this.metadataManager = options.metadataManager;
|
|
1307
|
+
this.flockDocRegistry = options.flockDocRegistry;
|
|
802
1308
|
this.docManager = options.docManager;
|
|
1309
|
+
this.flockDocManager = options.flockDocManager;
|
|
803
1310
|
}
|
|
804
1311
|
async applyEvents(events, by) {
|
|
805
1312
|
if (!events.length) return;
|
|
806
1313
|
const metadataPatches = /* @__PURE__ */ new Map();
|
|
807
1314
|
const docIdsToReconcile = /* @__PURE__ */ new Set();
|
|
1315
|
+
const flockDocIdsToReconcile = /* @__PURE__ */ new Set();
|
|
808
1316
|
for (const event of events) {
|
|
809
1317
|
const key = event.key;
|
|
810
1318
|
if (!Array.isArray(key) || key.length === 0) continue;
|
|
@@ -834,6 +1342,10 @@ var FlockHydrator = class {
|
|
|
834
1342
|
docIdsToReconcile.add(docId);
|
|
835
1343
|
continue;
|
|
836
1344
|
}
|
|
1345
|
+
if (root === "ef" || root === "efx") {
|
|
1346
|
+
flockDocIdsToReconcile.add(docId);
|
|
1347
|
+
continue;
|
|
1348
|
+
}
|
|
837
1349
|
}
|
|
838
1350
|
for (const docId of docIdsToReconcile) {
|
|
839
1351
|
const isDocKnown = this.isDocKnown(docId);
|
|
@@ -848,6 +1360,15 @@ var FlockHydrator = class {
|
|
|
848
1360
|
metadataPatchHint: metadataPatches.get(docId)
|
|
849
1361
|
});
|
|
850
1362
|
}
|
|
1363
|
+
for (const flockDocId of flockDocIdsToReconcile) {
|
|
1364
|
+
if (!this.isFlockDocKnown(flockDocId)) try {
|
|
1365
|
+
await this.flockDocManager.dropFlockDoc(flockDocId);
|
|
1366
|
+
this.flockDocRegistry.purgeKeyspace(flockDocId);
|
|
1367
|
+
} catch (error) {
|
|
1368
|
+
require_logging.logAsyncError(`drop purged flock doc ${flockDocId} during hydration`)(error);
|
|
1369
|
+
}
|
|
1370
|
+
this.flockDocRegistry.reconcile(flockDocId, by);
|
|
1371
|
+
}
|
|
851
1372
|
}
|
|
852
1373
|
isDocKnown(docId) {
|
|
853
1374
|
if (typeof this.metaFlock.get(["e", docId]) === "boolean") return true;
|
|
@@ -868,6 +1389,9 @@ var FlockHydrator = class {
|
|
|
868
1389
|
for (const row of rows) if (row.value !== void 0) this.metaFlock.delete(row.key);
|
|
869
1390
|
}
|
|
870
1391
|
}
|
|
1392
|
+
isFlockDocKnown(flockDocId) {
|
|
1393
|
+
return typeof this.metaFlock.get(["ef", flockDocId]) === "boolean";
|
|
1394
|
+
}
|
|
871
1395
|
get metaFlock() {
|
|
872
1396
|
return this.getMetaFlock();
|
|
873
1397
|
}
|
|
@@ -889,13 +1413,16 @@ var SyncRunner = class {
|
|
|
889
1413
|
transport;
|
|
890
1414
|
eventBus;
|
|
891
1415
|
docManager;
|
|
1416
|
+
flockDocManager;
|
|
892
1417
|
metadataManager;
|
|
1418
|
+
flockDocRegistry;
|
|
893
1419
|
flockHydrator;
|
|
894
1420
|
getMetaFlock;
|
|
895
1421
|
readyPromise;
|
|
896
1422
|
metaRoomSubscription;
|
|
897
1423
|
unsubscribeMetaFlock;
|
|
898
1424
|
docSubscriptions = /* @__PURE__ */ new Map();
|
|
1425
|
+
flockDocSubscriptions = /* @__PURE__ */ new Map();
|
|
899
1426
|
metaHydrationQueue = Promise.resolve();
|
|
900
1427
|
destroyed = false;
|
|
901
1428
|
suppressMetaLiveMonitor = false;
|
|
@@ -903,7 +1430,9 @@ var SyncRunner = class {
|
|
|
903
1430
|
this.transport = options.transport;
|
|
904
1431
|
this.eventBus = options.eventBus;
|
|
905
1432
|
this.docManager = options.docManager;
|
|
1433
|
+
this.flockDocManager = options.flockDocManager;
|
|
906
1434
|
this.metadataManager = options.metadataManager;
|
|
1435
|
+
this.flockDocRegistry = options.flockDocRegistry;
|
|
907
1436
|
this.flockHydrator = options.flockHydrator;
|
|
908
1437
|
this.getMetaFlock = options.getMetaFlock;
|
|
909
1438
|
}
|
|
@@ -918,12 +1447,12 @@ var SyncRunner = class {
|
|
|
918
1447
|
}
|
|
919
1448
|
async sync(options = {}) {
|
|
920
1449
|
await this.ready();
|
|
921
|
-
const {
|
|
1450
|
+
const { signal } = options;
|
|
922
1451
|
throwIfAborted(signal);
|
|
923
1452
|
if (!this.transport) return;
|
|
924
1453
|
if (!this.transport.isConnected()) await this.transport.connect();
|
|
925
1454
|
throwIfAborted(signal);
|
|
926
|
-
if (
|
|
1455
|
+
if ((await this.buildSyncPlan(options, { resolveAutoBodies: false })).syncMeta) {
|
|
927
1456
|
this.eventBus.pushEventBy("sync");
|
|
928
1457
|
this.suppressMetaLiveMonitor = true;
|
|
929
1458
|
let unsubscribe;
|
|
@@ -950,23 +1479,61 @@ var SyncRunner = class {
|
|
|
950
1479
|
}
|
|
951
1480
|
}
|
|
952
1481
|
throwIfAborted(signal);
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
1482
|
+
const plan = await this.buildSyncPlan(options);
|
|
1483
|
+
if (plan.flockDocIds.length > 0 && !this.transport.syncFlockDoc) throw new Error("Transport adapter does not support Flock docs");
|
|
1484
|
+
for (const docId of plan.docIds) {
|
|
1485
|
+
throwIfAborted(signal);
|
|
1486
|
+
const doc = await this.docManager.ensureDoc(docId);
|
|
1487
|
+
this.eventBus.pushEventBy("sync");
|
|
1488
|
+
try {
|
|
956
1489
|
throwIfAborted(signal);
|
|
957
|
-
const
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
throwIfAborted(signal);
|
|
963
|
-
if (!result.ok) throw new Error(`Document sync failed for ${docId}`);
|
|
964
|
-
} finally {
|
|
965
|
-
this.eventBus.popEventBy();
|
|
966
|
-
}
|
|
967
|
-
await this.docManager.persistDoc(docId, doc);
|
|
1490
|
+
const result = await this.transport.syncDoc(docId, doc);
|
|
1491
|
+
throwIfAborted(signal);
|
|
1492
|
+
if (!result.ok) throw new Error(`Document sync failed for ${docId}`);
|
|
1493
|
+
} finally {
|
|
1494
|
+
this.eventBus.popEventBy();
|
|
968
1495
|
}
|
|
1496
|
+
await this.docManager.persistDoc(docId, doc);
|
|
969
1497
|
}
|
|
1498
|
+
for (const flockDocId of plan.flockDocIds) {
|
|
1499
|
+
throwIfAborted(signal);
|
|
1500
|
+
const flock = await this.flockDocManager.ensureFlockDoc(flockDocId);
|
|
1501
|
+
this.eventBus.pushEventBy("sync");
|
|
1502
|
+
try {
|
|
1503
|
+
throwIfAborted(signal);
|
|
1504
|
+
const result = await this.transport.syncFlockDoc(flockDocId, flock);
|
|
1505
|
+
throwIfAborted(signal);
|
|
1506
|
+
if (!result.ok) throw new Error(`Flock document sync failed for ${flockDocId}`);
|
|
1507
|
+
} finally {
|
|
1508
|
+
this.eventBus.popEventBy();
|
|
1509
|
+
}
|
|
1510
|
+
await this.flockDocManager.persistFlockDoc(flockDocId, flock);
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
async buildSyncPlan(options, buildOptions = {}) {
|
|
1514
|
+
const { scope = "full", docIds, flockDocIds } = options;
|
|
1515
|
+
const syncMeta = scope === "meta" || scope === "full";
|
|
1516
|
+
if (!(scope === "doc" || scope === "full")) return {
|
|
1517
|
+
syncMeta,
|
|
1518
|
+
docIds: [],
|
|
1519
|
+
flockDocIds: []
|
|
1520
|
+
};
|
|
1521
|
+
const hasBodyAllowList = docIds !== void 0 || flockDocIds !== void 0;
|
|
1522
|
+
const resolveAutoBodies = buildOptions.resolveAutoBodies !== false;
|
|
1523
|
+
let plannedDocIds = [];
|
|
1524
|
+
if (docIds !== void 0) plannedDocIds = [...docIds];
|
|
1525
|
+
else if (!hasBodyAllowList && resolveAutoBodies) plannedDocIds = await this.metadataManager.getDocIds();
|
|
1526
|
+
let plannedFlockDocIds = [];
|
|
1527
|
+
if (flockDocIds !== void 0) plannedFlockDocIds = [...flockDocIds];
|
|
1528
|
+
else if (!hasBodyAllowList && resolveAutoBodies && this.transport?.syncFlockDoc) plannedFlockDocIds = await this.flockDocRegistry.getIds({
|
|
1529
|
+
activeOnly: true,
|
|
1530
|
+
nowMs: Date.now()
|
|
1531
|
+
});
|
|
1532
|
+
return {
|
|
1533
|
+
syncMeta,
|
|
1534
|
+
docIds: plannedDocIds,
|
|
1535
|
+
flockDocIds: plannedFlockDocIds
|
|
1536
|
+
};
|
|
970
1537
|
}
|
|
971
1538
|
async joinMetaRoom(params) {
|
|
972
1539
|
await this.ready();
|
|
@@ -1025,6 +1592,32 @@ var SyncRunner = class {
|
|
|
1025
1592
|
base.firstSyncedWithRemote.catch(require_logging.logAsyncError(`doc ${docId} first sync`));
|
|
1026
1593
|
return this.createDocLease(docId, record);
|
|
1027
1594
|
}
|
|
1595
|
+
async joinFlockDocRoom(flockDocId, params) {
|
|
1596
|
+
await this.ready();
|
|
1597
|
+
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1598
|
+
if (!this.transport.joinFlockDocRoom) throw new Error("Transport adapter does not support Flock docs");
|
|
1599
|
+
if (!this.transport.isConnected()) await this.transport.connect();
|
|
1600
|
+
const requestedAuth = params?.auth;
|
|
1601
|
+
const existing = this.flockDocSubscriptions.get(flockDocId);
|
|
1602
|
+
if (existing && bytesEqual(existing.auth, requestedAuth)) {
|
|
1603
|
+
existing.refCount += 1;
|
|
1604
|
+
return this.createFlockDocLease(flockDocId, existing);
|
|
1605
|
+
}
|
|
1606
|
+
if (existing) {
|
|
1607
|
+
existing.base.unsubscribe();
|
|
1608
|
+
if (this.flockDocSubscriptions.get(flockDocId) === existing) this.flockDocSubscriptions.delete(flockDocId);
|
|
1609
|
+
}
|
|
1610
|
+
const flock = await this.flockDocManager.ensureFlockDoc(flockDocId);
|
|
1611
|
+
const base = this.transport.joinFlockDocRoom(flockDocId, flock, params);
|
|
1612
|
+
const record = {
|
|
1613
|
+
base,
|
|
1614
|
+
auth: requestedAuth,
|
|
1615
|
+
refCount: 1
|
|
1616
|
+
};
|
|
1617
|
+
this.flockDocSubscriptions.set(flockDocId, record);
|
|
1618
|
+
base.firstSyncedWithRemote.catch(require_logging.logAsyncError(`flock doc ${flockDocId} first sync`));
|
|
1619
|
+
return this.createFlockDocLease(flockDocId, record);
|
|
1620
|
+
}
|
|
1028
1621
|
createMetaLease(record) {
|
|
1029
1622
|
let released = false;
|
|
1030
1623
|
return {
|
|
@@ -1088,6 +1681,33 @@ var SyncRunner = class {
|
|
|
1088
1681
|
onStatusChange: record.base.onStatusChange
|
|
1089
1682
|
};
|
|
1090
1683
|
}
|
|
1684
|
+
createFlockDocLease(flockDocId, record) {
|
|
1685
|
+
let released = false;
|
|
1686
|
+
return {
|
|
1687
|
+
unsubscribe: () => {
|
|
1688
|
+
if (released) return;
|
|
1689
|
+
released = true;
|
|
1690
|
+
const current = this.flockDocSubscriptions.get(flockDocId);
|
|
1691
|
+
if (!current || current !== record) return;
|
|
1692
|
+
current.refCount = Math.max(0, current.refCount - 1);
|
|
1693
|
+
if (current.refCount === 0) {
|
|
1694
|
+
current.base.unsubscribe();
|
|
1695
|
+
if (this.flockDocSubscriptions.get(flockDocId) === current) this.flockDocSubscriptions.delete(flockDocId);
|
|
1696
|
+
}
|
|
1697
|
+
},
|
|
1698
|
+
get firstSyncedWithRemote() {
|
|
1699
|
+
return record.base.firstSyncedWithRemote;
|
|
1700
|
+
},
|
|
1701
|
+
waitUntilSynced: () => record.base.waitUntilSynced(),
|
|
1702
|
+
rejoin: async () => {
|
|
1703
|
+
await record.base.rejoin?.();
|
|
1704
|
+
},
|
|
1705
|
+
get status() {
|
|
1706
|
+
return record.base.status;
|
|
1707
|
+
},
|
|
1708
|
+
onStatusChange: record.base.onStatusChange
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1091
1711
|
async destroy() {
|
|
1092
1712
|
if (this.destroyed) return;
|
|
1093
1713
|
this.destroyed = true;
|
|
@@ -1103,8 +1723,10 @@ var SyncRunner = class {
|
|
|
1103
1723
|
}
|
|
1104
1724
|
};
|
|
1105
1725
|
await safe(() => this.docManager.close());
|
|
1726
|
+
await safe(() => this.flockDocManager.close());
|
|
1106
1727
|
this.eventBus.clear();
|
|
1107
1728
|
this.metadataManager.clear();
|
|
1729
|
+
this.flockDocRegistry.clear();
|
|
1108
1730
|
this.readyPromise = void 0;
|
|
1109
1731
|
await safe(() => this.transport?.close() ?? Promise.resolve());
|
|
1110
1732
|
if (firstError !== void 0) throw firstError;
|
|
@@ -1137,6 +1759,8 @@ var SyncRunner = class {
|
|
|
1137
1759
|
}
|
|
1138
1760
|
for (const record of this.docSubscriptions.values()) record.base.unsubscribe();
|
|
1139
1761
|
this.docSubscriptions.clear();
|
|
1762
|
+
for (const record of this.flockDocSubscriptions.values()) record.base.unsubscribe();
|
|
1763
|
+
this.flockDocSubscriptions.clear();
|
|
1140
1764
|
this.unsubscribeMetaFlock?.();
|
|
1141
1765
|
this.unsubscribeMetaFlock = void 0;
|
|
1142
1766
|
}
|
|
@@ -1327,7 +1951,9 @@ var LoroRepo = class LoroRepo {
|
|
|
1327
1951
|
metaFlock;
|
|
1328
1952
|
eventBus;
|
|
1329
1953
|
docManager;
|
|
1954
|
+
flockDocManager;
|
|
1330
1955
|
metadataManager;
|
|
1956
|
+
flockDocRegistry;
|
|
1331
1957
|
flockHydrator;
|
|
1332
1958
|
syncRunner;
|
|
1333
1959
|
metaPersister;
|
|
@@ -1342,12 +1968,27 @@ var LoroRepo = class LoroRepo {
|
|
|
1342
1968
|
getMetaFlock,
|
|
1343
1969
|
eventBus: this.eventBus
|
|
1344
1970
|
});
|
|
1971
|
+
this.flockDocRegistry = new FlockDocRegistry({
|
|
1972
|
+
getMetaFlock,
|
|
1973
|
+
eventBus: this.eventBus
|
|
1974
|
+
});
|
|
1975
|
+
const getFlockDocRetentionMs = (flockDocId) => {
|
|
1976
|
+
const configured = options.flockDocRetentionMs;
|
|
1977
|
+
const value = typeof configured === "function" ? configured(flockDocId) : configured;
|
|
1978
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
|
|
1979
|
+
};
|
|
1345
1980
|
this.docManager = new DocManager({
|
|
1346
1981
|
storage: this.storage,
|
|
1347
1982
|
ensureDocExists: (docId) => this.metadataManager.ensureExists(docId),
|
|
1348
1983
|
eventBus: this.eventBus,
|
|
1349
1984
|
persistDebounceMs: options.docPersistDebounceMs
|
|
1350
1985
|
});
|
|
1986
|
+
this.flockDocManager = new FlockDocManager({
|
|
1987
|
+
storage: this.storage,
|
|
1988
|
+
ensureFlockDocExists: (flockDocId, expiresAtMs) => this.flockDocRegistry.ensureExists(flockDocId, expiresAtMs),
|
|
1989
|
+
retentionMs: getFlockDocRetentionMs,
|
|
1990
|
+
persistDebounceMs: options.docPersistDebounceMs
|
|
1991
|
+
});
|
|
1351
1992
|
if (this.storage) this.metaPersister = new MetaPersister({
|
|
1352
1993
|
getMetaFlock,
|
|
1353
1994
|
storage: this.storage,
|
|
@@ -1359,13 +2000,17 @@ var LoroRepo = class LoroRepo {
|
|
|
1359
2000
|
this.flockHydrator = new FlockHydrator({
|
|
1360
2001
|
getMetaFlock,
|
|
1361
2002
|
metadataManager: this.metadataManager,
|
|
1362
|
-
|
|
2003
|
+
flockDocRegistry: this.flockDocRegistry,
|
|
2004
|
+
docManager: this.docManager,
|
|
2005
|
+
flockDocManager: this.flockDocManager
|
|
1363
2006
|
});
|
|
1364
2007
|
this.syncRunner = new SyncRunner({
|
|
1365
2008
|
transport: this.transport,
|
|
1366
2009
|
eventBus: this.eventBus,
|
|
1367
2010
|
docManager: this.docManager,
|
|
2011
|
+
flockDocManager: this.flockDocManager,
|
|
1368
2012
|
metadataManager: this.metadataManager,
|
|
2013
|
+
flockDocRegistry: this.flockDocRegistry,
|
|
1369
2014
|
flockHydrator: this.flockHydrator,
|
|
1370
2015
|
getMetaFlock
|
|
1371
2016
|
});
|
|
@@ -1445,6 +2090,9 @@ var LoroRepo = class LoroRepo {
|
|
|
1445
2090
|
async joinDocRoom(docId, params) {
|
|
1446
2091
|
return this.syncRunner.joinDocRoom(docId, params);
|
|
1447
2092
|
}
|
|
2093
|
+
async joinFlockDocRoom(flockDocId, params) {
|
|
2094
|
+
return this.syncRunner.joinFlockDocRoom(flockDocId, params);
|
|
2095
|
+
}
|
|
1448
2096
|
async joinEphemeralRoom(roomId) {
|
|
1449
2097
|
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1450
2098
|
await this.syncRunner.ready();
|
|
@@ -1470,6 +2118,16 @@ var LoroRepo = class LoroRepo {
|
|
|
1470
2118
|
joinRoom: (auth) => this.syncRunner.joinDocRoom(docId, { auth })
|
|
1471
2119
|
};
|
|
1472
2120
|
}
|
|
2121
|
+
async openFlockDoc(flockDocId) {
|
|
2122
|
+
return {
|
|
2123
|
+
flock: await this.flockDocManager.openFlockDoc(flockDocId),
|
|
2124
|
+
syncOnce: () => this.sync({
|
|
2125
|
+
scope: "doc",
|
|
2126
|
+
flockDocIds: [flockDocId]
|
|
2127
|
+
}),
|
|
2128
|
+
joinRoom: (auth) => this.syncRunner.joinFlockDocRoom(flockDocId, { auth })
|
|
2129
|
+
};
|
|
2130
|
+
}
|
|
1473
2131
|
/**
|
|
1474
2132
|
* Merge `patch` into the document's metadata, creating the doc's
|
|
1475
2133
|
* existence entry if it does not exist.
|
|
@@ -1490,9 +2148,15 @@ var LoroRepo = class LoroRepo {
|
|
|
1490
2148
|
async getDocMeta(docId) {
|
|
1491
2149
|
return this.metadataManager.get(docId);
|
|
1492
2150
|
}
|
|
2151
|
+
async getFlockDoc(flockDocId) {
|
|
2152
|
+
return this.flockDocRegistry.get(flockDocId);
|
|
2153
|
+
}
|
|
1493
2154
|
async listDoc(query) {
|
|
1494
2155
|
return this.metadataManager.listDoc(query);
|
|
1495
2156
|
}
|
|
2157
|
+
async listFlockDoc(query) {
|
|
2158
|
+
return this.flockDocRegistry.list(query);
|
|
2159
|
+
}
|
|
1496
2160
|
async deleteDoc(docId, options = {}) {
|
|
1497
2161
|
if (await this.metadataManager.getDeleted(docId) === true && !options.force) return;
|
|
1498
2162
|
await this.metadataManager.markDeleted(docId);
|
|
@@ -1500,6 +2164,13 @@ var LoroRepo = class LoroRepo {
|
|
|
1500
2164
|
async restoreDoc(docId) {
|
|
1501
2165
|
await this.metadataManager.clearDeleted(docId);
|
|
1502
2166
|
}
|
|
2167
|
+
async deleteFlockDoc(flockDocId, options = {}) {
|
|
2168
|
+
if (await this.flockDocRegistry.getDeleted(flockDocId) === true && !options.force) return;
|
|
2169
|
+
await this.flockDocRegistry.markDeleted(flockDocId);
|
|
2170
|
+
}
|
|
2171
|
+
async restoreFlockDoc(flockDocId) {
|
|
2172
|
+
await this.flockDocRegistry.restore(flockDocId);
|
|
2173
|
+
}
|
|
1503
2174
|
async purgeDoc(docId) {
|
|
1504
2175
|
const previous = this.metadataManager.getExistenceState(docId);
|
|
1505
2176
|
await this.docManager.dropDoc(docId);
|
|
@@ -1509,6 +2180,18 @@ var LoroRepo = class LoroRepo {
|
|
|
1509
2180
|
metadataChanged: true
|
|
1510
2181
|
});
|
|
1511
2182
|
}
|
|
2183
|
+
async purgeFlockDoc(flockDocId) {
|
|
2184
|
+
const previous = this.flockDocRegistry.getExistenceState(flockDocId);
|
|
2185
|
+
await this.transport?.forgetFlockDoc?.(flockDocId);
|
|
2186
|
+
await this.flockDocManager.dropFlockDoc(flockDocId);
|
|
2187
|
+
this.flockDocRegistry.purgeKeyspace(flockDocId);
|
|
2188
|
+
this.flockDocRegistry.reconcile(flockDocId, "local", { previousExistence: previous });
|
|
2189
|
+
}
|
|
2190
|
+
async purgeExpiredFlockDocs(nowMs$2 = Date.now()) {
|
|
2191
|
+
const expired = this.flockDocRegistry.getExpiredIds(nowMs$2);
|
|
2192
|
+
for (const flockDocId of expired) await this.purgeFlockDoc(flockDocId);
|
|
2193
|
+
return expired;
|
|
2194
|
+
}
|
|
1512
2195
|
getMeta() {
|
|
1513
2196
|
return this.metaFlock;
|
|
1514
2197
|
}
|
|
@@ -1531,11 +2214,18 @@ var LoroRepo = class LoroRepo {
|
|
|
1531
2214
|
async openDetachedDoc(docId) {
|
|
1532
2215
|
return this.docManager.openDetachedDoc(docId);
|
|
1533
2216
|
}
|
|
2217
|
+
async openDetachedFlockDoc(flockDocId) {
|
|
2218
|
+
return this.flockDocManager.openDetachedFlockDoc(flockDocId);
|
|
2219
|
+
}
|
|
1534
2220
|
async unloadDoc(docId) {
|
|
1535
2221
|
await this.docManager.unloadDoc(docId);
|
|
1536
2222
|
}
|
|
2223
|
+
async unloadFlockDoc(flockDocId) {
|
|
2224
|
+
await this.flockDocManager.unloadFlockDoc(flockDocId);
|
|
2225
|
+
}
|
|
1537
2226
|
async flush() {
|
|
1538
2227
|
await this.docManager.flush();
|
|
2228
|
+
await this.flockDocManager.flush();
|
|
1539
2229
|
this.metaFlock.commit();
|
|
1540
2230
|
await this.metaPersister?.flushNow();
|
|
1541
2231
|
}
|
|
@@ -1554,8 +2244,11 @@ var LoroRepo = class LoroRepo {
|
|
|
1554
2244
|
if (firstError === void 0) firstError = error;
|
|
1555
2245
|
}
|
|
1556
2246
|
};
|
|
1557
|
-
await safe(() => this.metaPersister?.destroy());
|
|
1558
2247
|
await safe(() => this.syncRunner.destroy());
|
|
2248
|
+
await safe(() => {
|
|
2249
|
+
this.metaFlock.commit();
|
|
2250
|
+
return this.metaPersister?.destroy();
|
|
2251
|
+
});
|
|
1559
2252
|
await safe(() => this.storage?.close?.());
|
|
1560
2253
|
await safe(() => this.transport?.close());
|
|
1561
2254
|
if (firstError !== void 0) throw firstError;
|