loro-repo 0.14.2 → 0.16.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 +2 -0
- package/dist/index.cjs +134 -322
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +134 -322
- package/dist/index.js.map +1 -1
- package/dist/storage/filesystem.cjs +6 -5
- package/dist/storage/filesystem.cjs.map +1 -1
- package/dist/storage/filesystem.d.cts +1 -1
- package/dist/storage/filesystem.d.ts +1 -1
- package/dist/storage/filesystem.js +6 -5
- package/dist/storage/filesystem.js.map +1 -1
- package/dist/storage/indexeddb.d.cts +1 -1
- package/dist/storage/indexeddb.d.ts +1 -1
- package/dist/transport/broadcast-channel.cjs +0 -9
- package/dist/transport/broadcast-channel.cjs.map +1 -1
- package/dist/transport/broadcast-channel.d.cts +1 -1
- package/dist/transport/broadcast-channel.d.ts +1 -1
- package/dist/transport/broadcast-channel.js +0 -9
- package/dist/transport/broadcast-channel.js.map +1 -1
- package/dist/transport/streams.cjs +21 -19
- package/dist/transport/streams.cjs.map +1 -1
- package/dist/transport/streams.d.cts +1 -1
- package/dist/transport/streams.d.ts +1 -1
- package/dist/transport/streams.js +21 -19
- package/dist/transport/streams.js.map +1 -1
- package/dist/transport/websocket.cjs +1 -22
- package/dist/transport/websocket.cjs.map +1 -1
- package/dist/transport/websocket.d.cts +1 -1
- package/dist/transport/websocket.d.ts +1 -1
- package/dist/transport/websocket.js +1 -22
- package/dist/transport/websocket.js.map +1 -1
- package/dist/types.d.cts +9 -15
- package/dist/types.d.ts +9 -15
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -85,6 +85,8 @@ await repo.unloadDoc("note:welcome");
|
|
|
85
85
|
**Events**
|
|
86
86
|
- `const handle = repo.watch(listener, filter?)`
|
|
87
87
|
- `handle.unsubscribe()`
|
|
88
|
+
- `doc-existence-changed` carries `{ from, to }` over `"missing" | "active" | "deleted"`
|
|
89
|
+
- `doc-metadata` carries field patches
|
|
88
90
|
|
|
89
91
|
## Doc deletion lifecycle
|
|
90
92
|
|
package/dist/index.cjs
CHANGED
|
@@ -62,9 +62,7 @@ var RepoEventBus = class {
|
|
|
62
62
|
if (!filter.docIds && !filter.kinds && !filter.metadataFields && !filter.by) return true;
|
|
63
63
|
if (filter.kinds && !filter.kinds.includes(event.kind)) return false;
|
|
64
64
|
if (filter.by && !filter.by.includes(event.by)) return false;
|
|
65
|
-
const docId =
|
|
66
|
-
if (event.kind === "doc-metadata" || event.kind === "doc-soft-deleted" || event.kind === "doc-purging") return event.docId;
|
|
67
|
-
})();
|
|
65
|
+
const docId = event.docId;
|
|
68
66
|
if (filter.docIds && docId && !filter.docIds.includes(docId)) return false;
|
|
69
67
|
if (filter.docIds && !docId) return false;
|
|
70
68
|
if (filter.metadataFields && event.kind === "doc-metadata") {
|
|
@@ -351,12 +349,10 @@ function matchesQuery(docId, _metadata, query) {
|
|
|
351
349
|
var LazyMetadataCache = class {
|
|
352
350
|
cache = /* @__PURE__ */ new Map();
|
|
353
351
|
getMetaFlock;
|
|
354
|
-
eventBus;
|
|
355
352
|
docIdIndex;
|
|
356
353
|
docIdIndexDirty = true;
|
|
357
354
|
constructor(options) {
|
|
358
355
|
this.getMetaFlock = options.getMetaFlock;
|
|
359
|
-
this.eventBus = options.eventBus;
|
|
360
356
|
}
|
|
361
357
|
/**
|
|
362
358
|
* Get metadata for a document, reading from Flock if not cached.
|
|
@@ -369,19 +365,18 @@ var LazyMetadataCache = class {
|
|
|
369
365
|
return meta;
|
|
370
366
|
}
|
|
371
367
|
/**
|
|
372
|
-
* Check if a document has metadata in cache or Flock.
|
|
373
|
-
*/
|
|
374
|
-
has(docId) {
|
|
375
|
-
if (this.cache.has(docId)) return true;
|
|
376
|
-
return this.existsInFlock(docId);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
368
|
* Check if a document exists in cache only (sync).
|
|
380
369
|
*/
|
|
381
370
|
hasInCache(docId) {
|
|
382
371
|
return this.cache.has(docId);
|
|
383
372
|
}
|
|
384
373
|
/**
|
|
374
|
+
* Peek a cached entry without reading from Flock.
|
|
375
|
+
*/
|
|
376
|
+
peek(docId) {
|
|
377
|
+
return this.cache.get(docId);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
385
380
|
* Set metadata directly in cache (for local writes).
|
|
386
381
|
* The caller is responsible for writing to Flock.
|
|
387
382
|
*/
|
|
@@ -398,23 +393,6 @@ var LazyMetadataCache = class {
|
|
|
398
393
|
return existed;
|
|
399
394
|
}
|
|
400
395
|
/**
|
|
401
|
-
* Invalidate a specific document's cache entry.
|
|
402
|
-
* Next access will re-read from Flock.
|
|
403
|
-
*/
|
|
404
|
-
invalidate(docId) {
|
|
405
|
-
this.cache.delete(docId);
|
|
406
|
-
this.docIdIndexDirty = true;
|
|
407
|
-
}
|
|
408
|
-
/**
|
|
409
|
-
* Invalidate all cache entries.
|
|
410
|
-
* Used when version vector indicates changes but we don't know which docs changed.
|
|
411
|
-
*/
|
|
412
|
-
invalidateAll() {
|
|
413
|
-
this.cache.clear();
|
|
414
|
-
this.docIdIndex = void 0;
|
|
415
|
-
this.docIdIndexDirty = true;
|
|
416
|
-
}
|
|
417
|
-
/**
|
|
418
396
|
* Clear all cached data.
|
|
419
397
|
*/
|
|
420
398
|
clear() {
|
|
@@ -422,6 +400,9 @@ var LazyMetadataCache = class {
|
|
|
422
400
|
this.docIdIndex = void 0;
|
|
423
401
|
this.docIdIndexDirty = true;
|
|
424
402
|
}
|
|
403
|
+
markDocIdsDirty() {
|
|
404
|
+
this.docIdIndexDirty = true;
|
|
405
|
+
}
|
|
425
406
|
/**
|
|
426
407
|
* Get all document IDs from the `e/*` index.
|
|
427
408
|
* This scans once and caches the result until invalidated.
|
|
@@ -439,88 +420,6 @@ var LazyMetadataCache = class {
|
|
|
439
420
|
this.docIdIndexDirty = false;
|
|
440
421
|
return Array.from(ids);
|
|
441
422
|
}
|
|
442
|
-
/**
|
|
443
|
-
* Iterate over currently cached entries only.
|
|
444
|
-
* Warning: This does NOT return all docs in Flock - only those already loaded into cache.
|
|
445
|
-
* For full document listing, use MetadataManager.listDoc() instead.
|
|
446
|
-
*/
|
|
447
|
-
cachedEntries() {
|
|
448
|
-
return this.cache.entries();
|
|
449
|
-
}
|
|
450
|
-
/**
|
|
451
|
-
* Prefetch metadata for a list of document IDs.
|
|
452
|
-
* Useful before bulk operations like listDoc.
|
|
453
|
-
*/
|
|
454
|
-
prefetch(docIds) {
|
|
455
|
-
for (const docId of docIds) if (!this.cache.has(docId)) {
|
|
456
|
-
const meta = this.readFromFlock(docId);
|
|
457
|
-
if (meta) this.cache.set(docId, meta);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
/**
|
|
461
|
-
* Prefetch all metadata from Flock in a single scan.
|
|
462
|
-
* Use sparingly - this is the optimized version for bulk loading.
|
|
463
|
-
*/
|
|
464
|
-
prefetchAll() {
|
|
465
|
-
const result = /* @__PURE__ */ new Map();
|
|
466
|
-
const metaRows = this.metaFlock.scan({ prefix: ["m"] });
|
|
467
|
-
for (const row of metaRows) {
|
|
468
|
-
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
469
|
-
const docId = row.key[1];
|
|
470
|
-
if (typeof docId !== "string") continue;
|
|
471
|
-
if (row.value === void 0) continue;
|
|
472
|
-
let docMeta = result.get(docId);
|
|
473
|
-
if (!docMeta) {
|
|
474
|
-
docMeta = {};
|
|
475
|
-
result.set(docId, docMeta);
|
|
476
|
-
}
|
|
477
|
-
if (row.key.length === 2) {
|
|
478
|
-
const obj = asJsonObject(row.value);
|
|
479
|
-
if (!obj) continue;
|
|
480
|
-
for (const [field, value] of Object.entries(obj)) {
|
|
481
|
-
const cloned = cloneJsonValue(value);
|
|
482
|
-
if (cloned !== void 0) docMeta[field] = cloned;
|
|
483
|
-
}
|
|
484
|
-
} else {
|
|
485
|
-
const fieldKey = row.key[2];
|
|
486
|
-
if (typeof fieldKey !== "string") continue;
|
|
487
|
-
const jsonValue = cloneJsonValue(row.value);
|
|
488
|
-
if (jsonValue !== void 0) docMeta[fieldKey] = jsonValue;
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
for (const [docId, meta] of result) this.cache.set(docId, meta);
|
|
492
|
-
this.docIdIndexDirty = true;
|
|
493
|
-
return result;
|
|
494
|
-
}
|
|
495
|
-
/**
|
|
496
|
-
* Refresh metadata from Flock and emit event if changed.
|
|
497
|
-
*/
|
|
498
|
-
refreshAndEmit(docId, by) {
|
|
499
|
-
const previous = this.cache.get(docId);
|
|
500
|
-
const next = this.readFromFlock(docId);
|
|
501
|
-
if (!next) {
|
|
502
|
-
if (previous) {
|
|
503
|
-
this.cache.delete(docId);
|
|
504
|
-
this.docIdIndexDirty = true;
|
|
505
|
-
this.eventBus.emit({
|
|
506
|
-
kind: "doc-metadata",
|
|
507
|
-
docId,
|
|
508
|
-
patch: {},
|
|
509
|
-
by
|
|
510
|
-
});
|
|
511
|
-
}
|
|
512
|
-
return;
|
|
513
|
-
}
|
|
514
|
-
this.cache.set(docId, next);
|
|
515
|
-
this.docIdIndexDirty = true;
|
|
516
|
-
const patch = diffJsonObjects(previous, next);
|
|
517
|
-
if (!previous || Object.keys(patch).length > 0) this.eventBus.emit({
|
|
518
|
-
kind: "doc-metadata",
|
|
519
|
-
docId,
|
|
520
|
-
patch,
|
|
521
|
-
by
|
|
522
|
-
});
|
|
523
|
-
}
|
|
524
423
|
readFromFlock(docId) {
|
|
525
424
|
const rows = this.metaFlock.scan({ prefix: ["m", docId] });
|
|
526
425
|
const docMeta = {};
|
|
@@ -552,11 +451,6 @@ var LazyMetadataCache = class {
|
|
|
552
451
|
if (populated) return docMeta;
|
|
553
452
|
if (sawRow) return {};
|
|
554
453
|
}
|
|
555
|
-
existsInFlock(docId) {
|
|
556
|
-
const rows = this.metaFlock.scan({ prefix: ["m", docId] });
|
|
557
|
-
for (const row of rows) if (row.value !== void 0) return true;
|
|
558
|
-
return false;
|
|
559
|
-
}
|
|
560
454
|
get metaFlock() {
|
|
561
455
|
return this.getMetaFlock();
|
|
562
456
|
}
|
|
@@ -568,21 +462,16 @@ var MetadataManager = class {
|
|
|
568
462
|
getMetaFlock;
|
|
569
463
|
eventBus;
|
|
570
464
|
cache;
|
|
571
|
-
|
|
465
|
+
existenceState = /* @__PURE__ */ new Map();
|
|
572
466
|
constructor(options) {
|
|
573
467
|
this.getMetaFlock = options.getMetaFlock;
|
|
574
468
|
this.eventBus = options.eventBus;
|
|
575
|
-
this.cache = new LazyMetadataCache({
|
|
576
|
-
|
|
577
|
-
eventBus: options.eventBus
|
|
578
|
-
});
|
|
469
|
+
this.cache = new LazyMetadataCache({ getMetaFlock: options.getMetaFlock });
|
|
470
|
+
this.seedExistenceStateFromFlock();
|
|
579
471
|
}
|
|
580
472
|
async getDocIds() {
|
|
581
473
|
return this.cache.getDocIds();
|
|
582
474
|
}
|
|
583
|
-
cachedEntries() {
|
|
584
|
-
return this.cache.cachedEntries();
|
|
585
|
-
}
|
|
586
475
|
async get(docId) {
|
|
587
476
|
const active = this.readExistsFromFlock(docId);
|
|
588
477
|
if (active === void 0) return void 0;
|
|
@@ -596,20 +485,22 @@ var MetadataManager = class {
|
|
|
596
485
|
return active === void 0 ? void 0 : !active;
|
|
597
486
|
}
|
|
598
487
|
async ensureExists(docId) {
|
|
488
|
+
const previous = this.readTrackedExistenceState(docId);
|
|
599
489
|
if (this.readExistsFromFlock(docId) !== void 0) return;
|
|
600
490
|
this.metaFlock.put(["e", docId], true);
|
|
601
|
-
this.
|
|
602
|
-
this.cache.invalidate(docId);
|
|
491
|
+
this.reconcileDoc(docId, "local", { previousExistence: previous });
|
|
603
492
|
}
|
|
604
493
|
async markDeleted(docId) {
|
|
494
|
+
const previous = this.readTrackedExistenceState(docId);
|
|
605
495
|
if (this.readExistsFromFlock(docId) === false) return;
|
|
606
496
|
this.metaFlock.put(["e", docId], false);
|
|
607
|
-
this.
|
|
497
|
+
this.reconcileDoc(docId, "local", { previousExistence: previous });
|
|
608
498
|
}
|
|
609
499
|
async clearDeleted(docId) {
|
|
500
|
+
const previous = this.readTrackedExistenceState(docId);
|
|
610
501
|
if (this.readExistsFromFlock(docId) !== false) return;
|
|
611
502
|
this.metaFlock.put(["e", docId], true);
|
|
612
|
-
this.
|
|
503
|
+
this.reconcileDoc(docId, "local", { previousExistence: previous });
|
|
613
504
|
}
|
|
614
505
|
async listDoc(query) {
|
|
615
506
|
if (query?.limit !== void 0 && query.limit <= 0) return [];
|
|
@@ -673,73 +564,48 @@ var MetadataManager = class {
|
|
|
673
564
|
by: "local"
|
|
674
565
|
});
|
|
675
566
|
}
|
|
676
|
-
|
|
677
|
-
this.
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
this.
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
this.cache.
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
this.cache.clear();
|
|
689
|
-
for (const [docId, meta] of nextMetadata) this.cache.set(docId, meta);
|
|
690
|
-
const docIds = new Set([...prevMetadata.keys(), ...nextMetadata.keys()]);
|
|
691
|
-
for (const docId of docIds) {
|
|
692
|
-
const previous = prevMetadata.get(docId);
|
|
693
|
-
const current = nextMetadata.get(docId);
|
|
694
|
-
if (!current) {
|
|
695
|
-
if (previous) {
|
|
696
|
-
const patch$1 = {};
|
|
697
|
-
for (const key of Object.keys(previous)) patch$1[key] = null;
|
|
698
|
-
this.eventBus.emit({
|
|
699
|
-
kind: "doc-metadata",
|
|
700
|
-
docId,
|
|
701
|
-
patch: patch$1,
|
|
702
|
-
by
|
|
703
|
-
});
|
|
704
|
-
}
|
|
705
|
-
continue;
|
|
706
|
-
}
|
|
707
|
-
const patch = diffJsonObjects(previous, current);
|
|
708
|
-
if (!previous || Object.keys(patch).length > 0) this.eventBus.emit({
|
|
709
|
-
kind: "doc-metadata",
|
|
710
|
-
docId,
|
|
711
|
-
patch,
|
|
712
|
-
by
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
clear() {
|
|
717
|
-
this.cache.clear();
|
|
718
|
-
this.deletionState.clear();
|
|
719
|
-
}
|
|
720
|
-
purgeMetadata(docId, by) {
|
|
721
|
-
const existed = this.cache.hasInCache(docId);
|
|
722
|
-
this.cache.delete(docId);
|
|
723
|
-
this.deletionState.delete(docId);
|
|
724
|
-
if (existed) this.eventBus.emit({
|
|
567
|
+
reconcileDoc(docId, by, options = {}) {
|
|
568
|
+
const previousExistence = options.previousExistence ?? this.readTrackedExistenceState(docId);
|
|
569
|
+
const nextExistence = this.readExistenceStateFromFlock(docId);
|
|
570
|
+
if (previousExistence !== nextExistence) this.cache.markDocIdsDirty();
|
|
571
|
+
this.applyExistenceState(docId, previousExistence, nextExistence, by);
|
|
572
|
+
if (!(options.metadataChanged ?? Boolean(options.metadataPatchHint))) return;
|
|
573
|
+
const previousMetadata = this.cache.peek(docId);
|
|
574
|
+
const nextMetadata = nextExistence === "missing" ? void 0 : this.cache.readFromFlock(docId);
|
|
575
|
+
const patch = this.computeMetadataPatch(previousMetadata, nextMetadata, options.metadataPatchHint);
|
|
576
|
+
if (nextMetadata === void 0) this.cache.delete(docId);
|
|
577
|
+
else this.cache.set(docId, nextMetadata);
|
|
578
|
+
if (patch) this.eventBus.emit({
|
|
725
579
|
kind: "doc-metadata",
|
|
726
580
|
docId,
|
|
727
|
-
patch
|
|
581
|
+
patch,
|
|
728
582
|
by
|
|
729
583
|
});
|
|
730
584
|
}
|
|
731
|
-
|
|
732
|
-
this.
|
|
585
|
+
clear() {
|
|
586
|
+
this.cache.clear();
|
|
587
|
+
this.existenceState.clear();
|
|
733
588
|
}
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
for (const docId of allDocIds) {
|
|
737
|
-
const next = nextStates.get(docId);
|
|
738
|
-
this.applyExistenceState(docId, next, by);
|
|
739
|
-
}
|
|
589
|
+
getExistenceState(docId) {
|
|
590
|
+
return this.existenceState.get(docId) ?? this.readExistenceStateFromFlock(docId);
|
|
740
591
|
}
|
|
741
|
-
|
|
742
|
-
return this.cache;
|
|
592
|
+
hasCachedMetadata(docId) {
|
|
593
|
+
return this.cache.hasInCache(docId);
|
|
594
|
+
}
|
|
595
|
+
computeMetadataPatch(previous, next, patchHint) {
|
|
596
|
+
const hasPatchHint = Boolean(patchHint && Object.keys(patchHint).length > 0);
|
|
597
|
+
if (next === void 0) {
|
|
598
|
+
if (hasPatchHint) return cloneJsonObject(patchHint);
|
|
599
|
+
if (!previous) return void 0;
|
|
600
|
+
const patch$1 = diffJsonObjects(previous, {});
|
|
601
|
+
return Object.keys(patch$1).length > 0 ? patch$1 : void 0;
|
|
602
|
+
}
|
|
603
|
+
if (!previous) {
|
|
604
|
+
if (hasPatchHint) return cloneJsonObject(patchHint);
|
|
605
|
+
return Object.keys(next).length > 0 ? cloneJsonObject(next) : void 0;
|
|
606
|
+
}
|
|
607
|
+
const patch = diffJsonObjects(previous, next);
|
|
608
|
+
return Object.keys(patch).length > 0 ? patch : void 0;
|
|
743
609
|
}
|
|
744
610
|
computeDocRangeKeys(query) {
|
|
745
611
|
if (!query) return {};
|
|
@@ -765,22 +631,37 @@ var MetadataManager = class {
|
|
|
765
631
|
const raw = this.metaFlock.get(["e", docId]);
|
|
766
632
|
return typeof raw === "boolean" ? raw : void 0;
|
|
767
633
|
}
|
|
768
|
-
applyExistenceState(docId,
|
|
769
|
-
|
|
770
|
-
if (
|
|
771
|
-
|
|
772
|
-
return;
|
|
773
|
-
}
|
|
774
|
-
if (prev === deleted) return;
|
|
775
|
-
this.deletionState.set(docId, deleted);
|
|
776
|
-
if (prev === void 0 && deleted === false) return;
|
|
634
|
+
applyExistenceState(docId, previous, next, by) {
|
|
635
|
+
if (previous === next) return;
|
|
636
|
+
if (next === "missing") this.existenceState.delete(docId);
|
|
637
|
+
else this.existenceState.set(docId, next);
|
|
777
638
|
this.eventBus.emit({
|
|
778
|
-
kind: "doc-
|
|
639
|
+
kind: "doc-existence-changed",
|
|
779
640
|
docId,
|
|
780
|
-
|
|
641
|
+
from: previous,
|
|
642
|
+
to: next,
|
|
781
643
|
by
|
|
782
644
|
});
|
|
783
645
|
}
|
|
646
|
+
seedExistenceStateFromFlock() {
|
|
647
|
+
this.existenceState.clear();
|
|
648
|
+
const rows = this.metaFlock.scan({ prefix: ["e"] });
|
|
649
|
+
for (const row of rows) {
|
|
650
|
+
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
651
|
+
const docId = row.key[1];
|
|
652
|
+
if (typeof docId !== "string") continue;
|
|
653
|
+
if (typeof row.value !== "boolean") continue;
|
|
654
|
+
this.existenceState.set(docId, row.value ? "active" : "deleted");
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
readTrackedExistenceState(docId) {
|
|
658
|
+
return this.existenceState.get(docId) ?? "missing";
|
|
659
|
+
}
|
|
660
|
+
readExistenceStateFromFlock(docId) {
|
|
661
|
+
const current = this.readExistsFromFlock(docId);
|
|
662
|
+
if (current === void 0) return "missing";
|
|
663
|
+
return current ? "active" : "deleted";
|
|
664
|
+
}
|
|
784
665
|
get metaFlock() {
|
|
785
666
|
return this.getMetaFlock();
|
|
786
667
|
}
|
|
@@ -797,29 +678,10 @@ var FlockHydrator = class {
|
|
|
797
678
|
this.metadataManager = options.metadataManager;
|
|
798
679
|
this.docManager = options.docManager;
|
|
799
680
|
}
|
|
800
|
-
async hydrateAll(by) {
|
|
801
|
-
const { metadata, existence } = this.readAllState();
|
|
802
|
-
this.metadataManager.replaceAll(metadata, by);
|
|
803
|
-
this.metadataManager.replaceExistenceStates(existence, by);
|
|
804
|
-
const docIds = new Set([
|
|
805
|
-
...metadata.keys(),
|
|
806
|
-
...existence.keys(),
|
|
807
|
-
...await this.collectDocIds(["ts"]),
|
|
808
|
-
...await this.collectDocIds(["f"])
|
|
809
|
-
]);
|
|
810
|
-
for (const docId of docIds) if (!this.isDocKnown(docId)) try {
|
|
811
|
-
await this.docManager.dropDoc(docId);
|
|
812
|
-
await this.clearDocKeyspace(docId);
|
|
813
|
-
this.metadataManager.purgeMetadata(docId, by);
|
|
814
|
-
} catch (error) {
|
|
815
|
-
require_logging.logAsyncError(`drop purged doc ${docId} during hydrateAll`)(error);
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
681
|
async applyEvents(events, by) {
|
|
819
682
|
if (!events.length) return;
|
|
820
|
-
const
|
|
821
|
-
const
|
|
822
|
-
const docIdsToCheck = /* @__PURE__ */ new Set();
|
|
683
|
+
const metadataPatches = /* @__PURE__ */ new Map();
|
|
684
|
+
const docIdsToReconcile = /* @__PURE__ */ new Set();
|
|
823
685
|
for (const event of events) {
|
|
824
686
|
const key = event.key;
|
|
825
687
|
if (!Array.isArray(key) || key.length === 0) continue;
|
|
@@ -827,91 +689,43 @@ var FlockHydrator = class {
|
|
|
827
689
|
const docId = key[1];
|
|
828
690
|
if (typeof docId !== "string") continue;
|
|
829
691
|
if (root === "m") {
|
|
830
|
-
|
|
831
|
-
|
|
692
|
+
docIdsToReconcile.add(docId);
|
|
693
|
+
const patch = metadataPatches.get(docId) ?? {};
|
|
694
|
+
metadataPatches.set(docId, patch);
|
|
695
|
+
if (key.length === 2) {
|
|
696
|
+
const value = asJsonObject(event.value);
|
|
697
|
+
if (value) for (const [field, entry] of Object.entries(value)) {
|
|
698
|
+
const cloned = cloneJsonValue(entry);
|
|
699
|
+
if (cloned !== void 0) patch[field] = cloned;
|
|
700
|
+
}
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
const fieldKey = key[2];
|
|
704
|
+
if (typeof fieldKey === "string") {
|
|
705
|
+
const cloned = cloneJsonValue(event.value);
|
|
706
|
+
patch[fieldKey] = cloned === void 0 ? null : cloned;
|
|
707
|
+
}
|
|
832
708
|
continue;
|
|
833
709
|
}
|
|
834
710
|
if (root === "e") {
|
|
835
|
-
|
|
836
|
-
if (typeof value === "boolean") existenceUpdates.set(docId, !value);
|
|
837
|
-
else existenceUpdates.set(docId, void 0);
|
|
838
|
-
docIdsToCheck.add(docId);
|
|
711
|
+
docIdsToReconcile.add(docId);
|
|
839
712
|
continue;
|
|
840
713
|
}
|
|
841
|
-
if (root === "ts" || root === "f" || root === "ld") docIdsToCheck.add(docId);
|
|
842
714
|
}
|
|
843
|
-
for (const docId of
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
if (deleted === void 0) this.metadataManager.invalidate(docId);
|
|
847
|
-
}
|
|
848
|
-
for (const docId of docIdsToCheck) {
|
|
849
|
-
if (this.isDocKnown(docId)) continue;
|
|
850
|
-
try {
|
|
715
|
+
for (const docId of docIdsToReconcile) {
|
|
716
|
+
const isDocKnown = this.isDocKnown(docId);
|
|
717
|
+
if (!isDocKnown) try {
|
|
851
718
|
await this.docManager.dropDoc(docId);
|
|
852
719
|
await this.clearDocKeyspace(docId);
|
|
853
|
-
this.metadataManager.purgeMetadata(docId, by);
|
|
854
720
|
} catch (error) {
|
|
855
721
|
require_logging.logAsyncError(`drop purged doc ${docId} during hydration`)(error);
|
|
856
722
|
}
|
|
723
|
+
this.metadataManager.reconcileDoc(docId, by, {
|
|
724
|
+
metadataChanged: metadataPatches.has(docId) || !isDocKnown && this.metadataManager.hasCachedMetadata(docId),
|
|
725
|
+
metadataPatchHint: metadataPatches.get(docId)
|
|
726
|
+
});
|
|
857
727
|
}
|
|
858
728
|
}
|
|
859
|
-
readAllState() {
|
|
860
|
-
const metadata = /* @__PURE__ */ new Map();
|
|
861
|
-
const metadataRows = this.metaFlock.scan({ prefix: ["m"] });
|
|
862
|
-
for (const row of metadataRows) {
|
|
863
|
-
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
864
|
-
const docId = row.key[1];
|
|
865
|
-
if (typeof docId !== "string") continue;
|
|
866
|
-
if (row.key.length === 2) {
|
|
867
|
-
const obj = asJsonObject(row.value);
|
|
868
|
-
if (!obj) continue;
|
|
869
|
-
let docMeta$1 = metadata.get(docId);
|
|
870
|
-
if (!docMeta$1) {
|
|
871
|
-
docMeta$1 = {};
|
|
872
|
-
metadata.set(docId, docMeta$1);
|
|
873
|
-
}
|
|
874
|
-
for (const [field, value] of Object.entries(obj)) {
|
|
875
|
-
const cloned = cloneJsonValue(value);
|
|
876
|
-
if (cloned !== void 0) docMeta$1[field] = cloned;
|
|
877
|
-
}
|
|
878
|
-
continue;
|
|
879
|
-
}
|
|
880
|
-
const fieldKey = row.key[2];
|
|
881
|
-
if (typeof fieldKey !== "string") continue;
|
|
882
|
-
const jsonValue = cloneJsonValue(row.value);
|
|
883
|
-
if (jsonValue === void 0) continue;
|
|
884
|
-
let docMeta = metadata.get(docId);
|
|
885
|
-
if (!docMeta) {
|
|
886
|
-
docMeta = {};
|
|
887
|
-
metadata.set(docId, docMeta);
|
|
888
|
-
}
|
|
889
|
-
docMeta[fieldKey] = jsonValue;
|
|
890
|
-
}
|
|
891
|
-
const existence = /* @__PURE__ */ new Map();
|
|
892
|
-
const existenceRows = this.metaFlock.scan({ prefix: ["e"] });
|
|
893
|
-
for (const row of existenceRows) {
|
|
894
|
-
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
895
|
-
const docId = row.key[1];
|
|
896
|
-
if (typeof docId !== "string") continue;
|
|
897
|
-
if (typeof row.value !== "boolean") continue;
|
|
898
|
-
existence.set(docId, !row.value);
|
|
899
|
-
}
|
|
900
|
-
return {
|
|
901
|
-
metadata,
|
|
902
|
-
existence
|
|
903
|
-
};
|
|
904
|
-
}
|
|
905
|
-
async collectDocIds(prefix) {
|
|
906
|
-
const ids = /* @__PURE__ */ new Set();
|
|
907
|
-
const rows = this.metaFlock.scan({ prefix });
|
|
908
|
-
for (const row of rows) {
|
|
909
|
-
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
910
|
-
const docId = row.key[1];
|
|
911
|
-
if (typeof docId === "string") ids.add(docId);
|
|
912
|
-
}
|
|
913
|
-
return ids;
|
|
914
|
-
}
|
|
915
729
|
isDocKnown(docId) {
|
|
916
730
|
if (typeof this.metaFlock.get(["e", docId]) === "boolean") return true;
|
|
917
731
|
const rows = this.metaFlock.scan({ prefix: ["m", docId] });
|
|
@@ -938,6 +752,13 @@ var FlockHydrator = class {
|
|
|
938
752
|
|
|
939
753
|
//#endregion
|
|
940
754
|
//#region src/internal/sync-runner.ts
|
|
755
|
+
function bytesEqual(a, b) {
|
|
756
|
+
if (a === b) return true;
|
|
757
|
+
if (!a || !b) return false;
|
|
758
|
+
if (a.length !== b.length) return false;
|
|
759
|
+
for (let i = 0; i < a.length; i += 1) if (a[i] !== b[i]) return false;
|
|
760
|
+
return true;
|
|
761
|
+
}
|
|
941
762
|
/**
|
|
942
763
|
* Sync data between storage and transport layer
|
|
943
764
|
*/
|
|
@@ -1028,11 +849,16 @@ var SyncRunner = class {
|
|
|
1028
849
|
await this.ready();
|
|
1029
850
|
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1030
851
|
if (!this.transport.isConnected()) await this.transport.connect();
|
|
852
|
+
const requestedAuth = params?.auth;
|
|
1031
853
|
const existing = this.metaRoomSubscription;
|
|
1032
|
-
if (existing) {
|
|
854
|
+
if (existing && bytesEqual(existing.auth, requestedAuth)) {
|
|
1033
855
|
existing.refCount += 1;
|
|
1034
856
|
return this.createMetaLease(existing);
|
|
1035
857
|
}
|
|
858
|
+
if (existing) {
|
|
859
|
+
existing.base.unsubscribe();
|
|
860
|
+
this.metaRoomSubscription = void 0;
|
|
861
|
+
}
|
|
1036
862
|
await this.ensureMetaLiveMonitor();
|
|
1037
863
|
const base = this.transport.joinMetaRoom(this.metaFlock, params);
|
|
1038
864
|
const firstSyncedWithRemote = base.firstSyncedWithRemote.then(async () => {
|
|
@@ -1044,6 +870,7 @@ var SyncRunner = class {
|
|
|
1044
870
|
const record = {
|
|
1045
871
|
base,
|
|
1046
872
|
firstSyncedWithRemote,
|
|
873
|
+
auth: requestedAuth,
|
|
1047
874
|
refCount: 1
|
|
1048
875
|
};
|
|
1049
876
|
this.metaRoomSubscription = record;
|
|
@@ -1053,15 +880,21 @@ var SyncRunner = class {
|
|
|
1053
880
|
await this.ready();
|
|
1054
881
|
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1055
882
|
if (!this.transport.isConnected()) await this.transport.connect();
|
|
883
|
+
const requestedAuth = params?.auth;
|
|
1056
884
|
const existing = this.docSubscriptions.get(docId);
|
|
1057
|
-
if (existing) {
|
|
885
|
+
if (existing && bytesEqual(existing.auth, requestedAuth)) {
|
|
1058
886
|
existing.refCount += 1;
|
|
1059
887
|
return this.createDocLease(docId, existing);
|
|
1060
888
|
}
|
|
889
|
+
if (existing) {
|
|
890
|
+
existing.base.unsubscribe();
|
|
891
|
+
if (this.docSubscriptions.get(docId) === existing) this.docSubscriptions.delete(docId);
|
|
892
|
+
}
|
|
1061
893
|
const doc = await this.docManager.ensureDoc(docId);
|
|
1062
894
|
const base = this.transport.joinDocRoom(docId, doc, params);
|
|
1063
895
|
const record = {
|
|
1064
896
|
base,
|
|
897
|
+
auth: requestedAuth,
|
|
1065
898
|
refCount: 1
|
|
1066
899
|
};
|
|
1067
900
|
this.docSubscriptions.set(docId, record);
|
|
@@ -1087,9 +920,6 @@ var SyncRunner = class {
|
|
|
1087
920
|
}
|
|
1088
921
|
},
|
|
1089
922
|
firstSyncedWithRemote: record.firstSyncedWithRemote ?? record.base.firstSyncedWithRemote,
|
|
1090
|
-
get connected() {
|
|
1091
|
-
return record.base.connected;
|
|
1092
|
-
},
|
|
1093
923
|
get status() {
|
|
1094
924
|
return record.base.status;
|
|
1095
925
|
},
|
|
@@ -1111,9 +941,6 @@ var SyncRunner = class {
|
|
|
1111
941
|
}
|
|
1112
942
|
},
|
|
1113
943
|
firstSyncedWithRemote: record.base.firstSyncedWithRemote,
|
|
1114
|
-
get connected() {
|
|
1115
|
-
return record.base.connected;
|
|
1116
|
-
},
|
|
1117
944
|
get status() {
|
|
1118
945
|
return record.base.status;
|
|
1119
946
|
},
|
|
@@ -1367,9 +1194,6 @@ var LoroRepo = class LoroRepo {
|
|
|
1367
1194
|
const subscription = await this.syncRunner.joinMetaRoom(params);
|
|
1368
1195
|
return {
|
|
1369
1196
|
unsubscribe: subscription.unsubscribe,
|
|
1370
|
-
get connected() {
|
|
1371
|
-
return subscription.connected;
|
|
1372
|
-
},
|
|
1373
1197
|
get status() {
|
|
1374
1198
|
return subscription.status;
|
|
1375
1199
|
},
|
|
@@ -1380,23 +1204,13 @@ var LoroRepo = class LoroRepo {
|
|
|
1380
1204
|
};
|
|
1381
1205
|
}
|
|
1382
1206
|
async joinDocRoom(docId, params) {
|
|
1383
|
-
|
|
1384
|
-
return {
|
|
1385
|
-
...subscription,
|
|
1386
|
-
onStatusChange: subscription.onStatusChange,
|
|
1387
|
-
status: subscription.status
|
|
1388
|
-
};
|
|
1207
|
+
return this.syncRunner.joinDocRoom(docId, params);
|
|
1389
1208
|
}
|
|
1390
1209
|
async joinEphemeralRoom(roomId) {
|
|
1391
1210
|
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1392
1211
|
await this.syncRunner.ready();
|
|
1393
1212
|
if (!this.transport.isConnected()) await this.transport.connect();
|
|
1394
|
-
|
|
1395
|
-
return {
|
|
1396
|
-
...subscription,
|
|
1397
|
-
onStatusChange: subscription.onStatusChange,
|
|
1398
|
-
status: subscription.status
|
|
1399
|
-
};
|
|
1213
|
+
return this.transport.joinEphemeralRoom(roomId);
|
|
1400
1214
|
}
|
|
1401
1215
|
async openPersistedDoc(docId) {
|
|
1402
1216
|
return {
|
|
@@ -1425,15 +1239,13 @@ var LoroRepo = class LoroRepo {
|
|
|
1425
1239
|
await this.metadataManager.clearDeleted(docId);
|
|
1426
1240
|
}
|
|
1427
1241
|
async purgeDoc(docId) {
|
|
1428
|
-
this.
|
|
1429
|
-
kind: "doc-purging",
|
|
1430
|
-
docId,
|
|
1431
|
-
by: "local"
|
|
1432
|
-
});
|
|
1242
|
+
const previous = this.metadataManager.getExistenceState(docId);
|
|
1433
1243
|
await this.docManager.dropDoc(docId);
|
|
1434
1244
|
await this.purgeDocKeyspace(docId);
|
|
1435
|
-
this.metadataManager.
|
|
1436
|
-
|
|
1245
|
+
this.metadataManager.reconcileDoc(docId, "local", {
|
|
1246
|
+
previousExistence: previous,
|
|
1247
|
+
metadataChanged: true
|
|
1248
|
+
});
|
|
1437
1249
|
}
|
|
1438
1250
|
getMeta() {
|
|
1439
1251
|
return this.metaFlock;
|