loro-repo 0.15.0 → 0.16.1
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 +103 -283
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +103 -283
- 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.js +6 -5
- package/dist/storage/filesystem.js.map +1 -1
- package/dist/transport/broadcast-channel.cjs +0 -9
- package/dist/transport/broadcast-channel.cjs.map +1 -1
- package/dist/transport/broadcast-channel.js +0 -9
- package/dist/transport/broadcast-channel.js.map +1 -1
- package/dist/transport/streams.cjs +23 -21
- package/dist/transport/streams.cjs.map +1 -1
- package/dist/transport/streams.js +23 -21
- 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.js +1 -22
- package/dist/transport/websocket.js.map +1 -1
- package/dist/types.d.cts +0 -6
- package/dist/types.d.ts +0 -6
- package/package.json +5 -4
package/dist/index.cjs
CHANGED
|
@@ -349,12 +349,10 @@ function matchesQuery(docId, _metadata, query) {
|
|
|
349
349
|
var LazyMetadataCache = class {
|
|
350
350
|
cache = /* @__PURE__ */ new Map();
|
|
351
351
|
getMetaFlock;
|
|
352
|
-
eventBus;
|
|
353
352
|
docIdIndex;
|
|
354
353
|
docIdIndexDirty = true;
|
|
355
354
|
constructor(options) {
|
|
356
355
|
this.getMetaFlock = options.getMetaFlock;
|
|
357
|
-
this.eventBus = options.eventBus;
|
|
358
356
|
}
|
|
359
357
|
/**
|
|
360
358
|
* Get metadata for a document, reading from Flock if not cached.
|
|
@@ -367,19 +365,18 @@ var LazyMetadataCache = class {
|
|
|
367
365
|
return meta;
|
|
368
366
|
}
|
|
369
367
|
/**
|
|
370
|
-
* Check if a document has metadata in cache or Flock.
|
|
371
|
-
*/
|
|
372
|
-
has(docId) {
|
|
373
|
-
if (this.cache.has(docId)) return true;
|
|
374
|
-
return this.existsInFlock(docId);
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
368
|
* Check if a document exists in cache only (sync).
|
|
378
369
|
*/
|
|
379
370
|
hasInCache(docId) {
|
|
380
371
|
return this.cache.has(docId);
|
|
381
372
|
}
|
|
382
373
|
/**
|
|
374
|
+
* Peek a cached entry without reading from Flock.
|
|
375
|
+
*/
|
|
376
|
+
peek(docId) {
|
|
377
|
+
return this.cache.get(docId);
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
383
380
|
* Set metadata directly in cache (for local writes).
|
|
384
381
|
* The caller is responsible for writing to Flock.
|
|
385
382
|
*/
|
|
@@ -396,23 +393,6 @@ var LazyMetadataCache = class {
|
|
|
396
393
|
return existed;
|
|
397
394
|
}
|
|
398
395
|
/**
|
|
399
|
-
* Invalidate a specific document's cache entry.
|
|
400
|
-
* Next access will re-read from Flock.
|
|
401
|
-
*/
|
|
402
|
-
invalidate(docId) {
|
|
403
|
-
this.cache.delete(docId);
|
|
404
|
-
this.docIdIndexDirty = true;
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Invalidate all cache entries.
|
|
408
|
-
* Used when version vector indicates changes but we don't know which docs changed.
|
|
409
|
-
*/
|
|
410
|
-
invalidateAll() {
|
|
411
|
-
this.cache.clear();
|
|
412
|
-
this.docIdIndex = void 0;
|
|
413
|
-
this.docIdIndexDirty = true;
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
396
|
* Clear all cached data.
|
|
417
397
|
*/
|
|
418
398
|
clear() {
|
|
@@ -420,6 +400,9 @@ var LazyMetadataCache = class {
|
|
|
420
400
|
this.docIdIndex = void 0;
|
|
421
401
|
this.docIdIndexDirty = true;
|
|
422
402
|
}
|
|
403
|
+
markDocIdsDirty() {
|
|
404
|
+
this.docIdIndexDirty = true;
|
|
405
|
+
}
|
|
423
406
|
/**
|
|
424
407
|
* Get all document IDs from the `e/*` index.
|
|
425
408
|
* This scans once and caches the result until invalidated.
|
|
@@ -437,88 +420,6 @@ var LazyMetadataCache = class {
|
|
|
437
420
|
this.docIdIndexDirty = false;
|
|
438
421
|
return Array.from(ids);
|
|
439
422
|
}
|
|
440
|
-
/**
|
|
441
|
-
* Iterate over currently cached entries only.
|
|
442
|
-
* Warning: This does NOT return all docs in Flock - only those already loaded into cache.
|
|
443
|
-
* For full document listing, use MetadataManager.listDoc() instead.
|
|
444
|
-
*/
|
|
445
|
-
cachedEntries() {
|
|
446
|
-
return this.cache.entries();
|
|
447
|
-
}
|
|
448
|
-
/**
|
|
449
|
-
* Prefetch metadata for a list of document IDs.
|
|
450
|
-
* Useful before bulk operations like listDoc.
|
|
451
|
-
*/
|
|
452
|
-
prefetch(docIds) {
|
|
453
|
-
for (const docId of docIds) if (!this.cache.has(docId)) {
|
|
454
|
-
const meta = this.readFromFlock(docId);
|
|
455
|
-
if (meta) this.cache.set(docId, meta);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
/**
|
|
459
|
-
* Prefetch all metadata from Flock in a single scan.
|
|
460
|
-
* Use sparingly - this is the optimized version for bulk loading.
|
|
461
|
-
*/
|
|
462
|
-
prefetchAll() {
|
|
463
|
-
const result = /* @__PURE__ */ new Map();
|
|
464
|
-
const metaRows = this.metaFlock.scan({ prefix: ["m"] });
|
|
465
|
-
for (const row of metaRows) {
|
|
466
|
-
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
467
|
-
const docId = row.key[1];
|
|
468
|
-
if (typeof docId !== "string") continue;
|
|
469
|
-
if (row.value === void 0) continue;
|
|
470
|
-
let docMeta = result.get(docId);
|
|
471
|
-
if (!docMeta) {
|
|
472
|
-
docMeta = {};
|
|
473
|
-
result.set(docId, docMeta);
|
|
474
|
-
}
|
|
475
|
-
if (row.key.length === 2) {
|
|
476
|
-
const obj = asJsonObject(row.value);
|
|
477
|
-
if (!obj) continue;
|
|
478
|
-
for (const [field, value] of Object.entries(obj)) {
|
|
479
|
-
const cloned = cloneJsonValue(value);
|
|
480
|
-
if (cloned !== void 0) docMeta[field] = cloned;
|
|
481
|
-
}
|
|
482
|
-
} else {
|
|
483
|
-
const fieldKey = row.key[2];
|
|
484
|
-
if (typeof fieldKey !== "string") continue;
|
|
485
|
-
const jsonValue = cloneJsonValue(row.value);
|
|
486
|
-
if (jsonValue !== void 0) docMeta[fieldKey] = jsonValue;
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
for (const [docId, meta] of result) this.cache.set(docId, meta);
|
|
490
|
-
this.docIdIndexDirty = true;
|
|
491
|
-
return result;
|
|
492
|
-
}
|
|
493
|
-
/**
|
|
494
|
-
* Refresh metadata from Flock and emit event if changed.
|
|
495
|
-
*/
|
|
496
|
-
refreshAndEmit(docId, by) {
|
|
497
|
-
const previous = this.cache.get(docId);
|
|
498
|
-
const next = this.readFromFlock(docId);
|
|
499
|
-
if (!next) {
|
|
500
|
-
if (previous) {
|
|
501
|
-
this.cache.delete(docId);
|
|
502
|
-
this.docIdIndexDirty = true;
|
|
503
|
-
this.eventBus.emit({
|
|
504
|
-
kind: "doc-metadata",
|
|
505
|
-
docId,
|
|
506
|
-
patch: {},
|
|
507
|
-
by
|
|
508
|
-
});
|
|
509
|
-
}
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
this.cache.set(docId, next);
|
|
513
|
-
this.docIdIndexDirty = true;
|
|
514
|
-
const patch = diffJsonObjects(previous, next);
|
|
515
|
-
if (!previous || Object.keys(patch).length > 0) this.eventBus.emit({
|
|
516
|
-
kind: "doc-metadata",
|
|
517
|
-
docId,
|
|
518
|
-
patch,
|
|
519
|
-
by
|
|
520
|
-
});
|
|
521
|
-
}
|
|
522
423
|
readFromFlock(docId) {
|
|
523
424
|
const rows = this.metaFlock.scan({ prefix: ["m", docId] });
|
|
524
425
|
const docMeta = {};
|
|
@@ -550,11 +451,6 @@ var LazyMetadataCache = class {
|
|
|
550
451
|
if (populated) return docMeta;
|
|
551
452
|
if (sawRow) return {};
|
|
552
453
|
}
|
|
553
|
-
existsInFlock(docId) {
|
|
554
|
-
const rows = this.metaFlock.scan({ prefix: ["m", docId] });
|
|
555
|
-
for (const row of rows) if (row.value !== void 0) return true;
|
|
556
|
-
return false;
|
|
557
|
-
}
|
|
558
454
|
get metaFlock() {
|
|
559
455
|
return this.getMetaFlock();
|
|
560
456
|
}
|
|
@@ -570,18 +466,12 @@ var MetadataManager = class {
|
|
|
570
466
|
constructor(options) {
|
|
571
467
|
this.getMetaFlock = options.getMetaFlock;
|
|
572
468
|
this.eventBus = options.eventBus;
|
|
573
|
-
this.cache = new LazyMetadataCache({
|
|
574
|
-
getMetaFlock: options.getMetaFlock,
|
|
575
|
-
eventBus: options.eventBus
|
|
576
|
-
});
|
|
469
|
+
this.cache = new LazyMetadataCache({ getMetaFlock: options.getMetaFlock });
|
|
577
470
|
this.seedExistenceStateFromFlock();
|
|
578
471
|
}
|
|
579
472
|
async getDocIds() {
|
|
580
473
|
return this.cache.getDocIds();
|
|
581
474
|
}
|
|
582
|
-
cachedEntries() {
|
|
583
|
-
return this.cache.cachedEntries();
|
|
584
|
-
}
|
|
585
475
|
async get(docId) {
|
|
586
476
|
const active = this.readExistsFromFlock(docId);
|
|
587
477
|
if (active === void 0) return void 0;
|
|
@@ -595,23 +485,22 @@ var MetadataManager = class {
|
|
|
595
485
|
return active === void 0 ? void 0 : !active;
|
|
596
486
|
}
|
|
597
487
|
async ensureExists(docId) {
|
|
598
|
-
const previous = this.
|
|
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) {
|
|
605
|
-
const previous = this.
|
|
494
|
+
const previous = this.readTrackedExistenceState(docId);
|
|
606
495
|
if (this.readExistsFromFlock(docId) === false) return;
|
|
607
496
|
this.metaFlock.put(["e", docId], false);
|
|
608
|
-
this.
|
|
497
|
+
this.reconcileDoc(docId, "local", { previousExistence: previous });
|
|
609
498
|
}
|
|
610
499
|
async clearDeleted(docId) {
|
|
611
|
-
const previous = this.
|
|
500
|
+
const previous = this.readTrackedExistenceState(docId);
|
|
612
501
|
if (this.readExistsFromFlock(docId) !== false) return;
|
|
613
502
|
this.metaFlock.put(["e", docId], true);
|
|
614
|
-
this.
|
|
503
|
+
this.reconcileDoc(docId, "local", { previousExistence: previous });
|
|
615
504
|
}
|
|
616
505
|
async listDoc(query) {
|
|
617
506
|
if (query?.limit !== void 0 && query.limit <= 0) return [];
|
|
@@ -675,77 +564,49 @@ var MetadataManager = class {
|
|
|
675
564
|
by: "local"
|
|
676
565
|
});
|
|
677
566
|
}
|
|
678
|
-
|
|
679
|
-
this.
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
this.
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
this.cache.
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
this.cache.clear();
|
|
691
|
-
for (const [docId, meta] of nextMetadata) this.cache.set(docId, meta);
|
|
692
|
-
const docIds = new Set([...prevMetadata.keys(), ...nextMetadata.keys()]);
|
|
693
|
-
for (const docId of docIds) {
|
|
694
|
-
const previous = prevMetadata.get(docId);
|
|
695
|
-
const current = nextMetadata.get(docId);
|
|
696
|
-
if (!current) {
|
|
697
|
-
if (previous) {
|
|
698
|
-
const patch$1 = {};
|
|
699
|
-
for (const key of Object.keys(previous)) patch$1[key] = null;
|
|
700
|
-
this.eventBus.emit({
|
|
701
|
-
kind: "doc-metadata",
|
|
702
|
-
docId,
|
|
703
|
-
patch: patch$1,
|
|
704
|
-
by
|
|
705
|
-
});
|
|
706
|
-
}
|
|
707
|
-
continue;
|
|
708
|
-
}
|
|
709
|
-
const patch = diffJsonObjects(previous, current);
|
|
710
|
-
if (!previous || Object.keys(patch).length > 0) this.eventBus.emit({
|
|
711
|
-
kind: "doc-metadata",
|
|
712
|
-
docId,
|
|
713
|
-
patch,
|
|
714
|
-
by
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
clear() {
|
|
719
|
-
this.cache.clear();
|
|
720
|
-
this.existenceState.clear();
|
|
721
|
-
}
|
|
722
|
-
purgeMetadata(docId, by) {
|
|
723
|
-
const existed = this.cache.hasInCache(docId);
|
|
724
|
-
this.cache.delete(docId);
|
|
725
|
-
this.existenceState.delete(docId);
|
|
726
|
-
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({
|
|
727
579
|
kind: "doc-metadata",
|
|
728
580
|
docId,
|
|
729
|
-
patch
|
|
581
|
+
patch,
|
|
730
582
|
by
|
|
731
583
|
});
|
|
732
584
|
}
|
|
733
|
-
|
|
734
|
-
this.
|
|
735
|
-
|
|
736
|
-
replaceExistenceStates(nextStates, by) {
|
|
737
|
-
const allDocIds = new Set([...this.existenceState.keys(), ...nextStates.keys()]);
|
|
738
|
-
for (const docId of allDocIds) {
|
|
739
|
-
const next = nextStates.get(docId) ?? "missing";
|
|
740
|
-
this.applyExistenceState(docId, this.readTrackedExistenceState(docId), next, by);
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
getCache() {
|
|
744
|
-
return this.cache;
|
|
585
|
+
clear() {
|
|
586
|
+
this.cache.clear();
|
|
587
|
+
this.existenceState.clear();
|
|
745
588
|
}
|
|
746
589
|
getExistenceState(docId) {
|
|
747
590
|
return this.existenceState.get(docId) ?? this.readExistenceStateFromFlock(docId);
|
|
748
591
|
}
|
|
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;
|
|
609
|
+
}
|
|
749
610
|
computeDocRangeKeys(query) {
|
|
750
611
|
if (!query) return {};
|
|
751
612
|
const prefix = query.prefix && query.prefix.length > 0 ? query.prefix : void 0;
|
|
@@ -817,16 +678,10 @@ var FlockHydrator = class {
|
|
|
817
678
|
this.metadataManager = options.metadataManager;
|
|
818
679
|
this.docManager = options.docManager;
|
|
819
680
|
}
|
|
820
|
-
async hydrateAll(by) {
|
|
821
|
-
const { metadata, existence } = this.readAllState();
|
|
822
|
-
this.metadataManager.replaceExistenceStates(existence, by);
|
|
823
|
-
this.metadataManager.replaceAll(metadata, by);
|
|
824
|
-
}
|
|
825
681
|
async applyEvents(events, by) {
|
|
826
682
|
if (!events.length) return;
|
|
827
|
-
const
|
|
828
|
-
const
|
|
829
|
-
const docIdsToCheck = /* @__PURE__ */ new Set();
|
|
683
|
+
const metadataPatches = /* @__PURE__ */ new Map();
|
|
684
|
+
const docIdsToReconcile = /* @__PURE__ */ new Set();
|
|
830
685
|
for (const event of events) {
|
|
831
686
|
const key = event.key;
|
|
832
687
|
if (!Array.isArray(key) || key.length === 0) continue;
|
|
@@ -834,80 +689,43 @@ var FlockHydrator = class {
|
|
|
834
689
|
const docId = key[1];
|
|
835
690
|
if (typeof docId !== "string") continue;
|
|
836
691
|
if (root === "m") {
|
|
837
|
-
|
|
838
|
-
|
|
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
|
+
}
|
|
839
708
|
continue;
|
|
840
709
|
}
|
|
841
710
|
if (root === "e") {
|
|
842
|
-
|
|
843
|
-
if (typeof value === "boolean") existenceUpdates.set(docId, value ? "active" : "deleted");
|
|
844
|
-
else existenceUpdates.set(docId, "missing");
|
|
845
|
-
docIdsToCheck.add(docId);
|
|
711
|
+
docIdsToReconcile.add(docId);
|
|
846
712
|
continue;
|
|
847
713
|
}
|
|
848
714
|
}
|
|
849
|
-
for (const
|
|
850
|
-
this.
|
|
851
|
-
if (
|
|
852
|
-
}
|
|
853
|
-
for (const docId of metadataIds) await this.metadataManager.refreshFromFlock(docId, by);
|
|
854
|
-
for (const docId of docIdsToCheck) {
|
|
855
|
-
if (this.isDocKnown(docId)) continue;
|
|
856
|
-
try {
|
|
715
|
+
for (const docId of docIdsToReconcile) {
|
|
716
|
+
const isDocKnown = this.isDocKnown(docId);
|
|
717
|
+
if (!isDocKnown) try {
|
|
857
718
|
await this.docManager.dropDoc(docId);
|
|
858
719
|
await this.clearDocKeyspace(docId);
|
|
859
|
-
this.metadataManager.purgeMetadata(docId, by);
|
|
860
720
|
} catch (error) {
|
|
861
721
|
require_logging.logAsyncError(`drop purged doc ${docId} during hydration`)(error);
|
|
862
722
|
}
|
|
723
|
+
this.metadataManager.reconcileDoc(docId, by, {
|
|
724
|
+
metadataChanged: metadataPatches.has(docId) || !isDocKnown && this.metadataManager.hasCachedMetadata(docId),
|
|
725
|
+
metadataPatchHint: metadataPatches.get(docId)
|
|
726
|
+
});
|
|
863
727
|
}
|
|
864
728
|
}
|
|
865
|
-
readAllState() {
|
|
866
|
-
const metadata = /* @__PURE__ */ new Map();
|
|
867
|
-
const metadataRows = this.metaFlock.scan({ prefix: ["m"] });
|
|
868
|
-
for (const row of metadataRows) {
|
|
869
|
-
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
870
|
-
const docId = row.key[1];
|
|
871
|
-
if (typeof docId !== "string") continue;
|
|
872
|
-
if (row.key.length === 2) {
|
|
873
|
-
const obj = asJsonObject(row.value);
|
|
874
|
-
if (!obj) continue;
|
|
875
|
-
let docMeta$1 = metadata.get(docId);
|
|
876
|
-
if (!docMeta$1) {
|
|
877
|
-
docMeta$1 = {};
|
|
878
|
-
metadata.set(docId, docMeta$1);
|
|
879
|
-
}
|
|
880
|
-
for (const [field, value] of Object.entries(obj)) {
|
|
881
|
-
const cloned = cloneJsonValue(value);
|
|
882
|
-
if (cloned !== void 0) docMeta$1[field] = cloned;
|
|
883
|
-
}
|
|
884
|
-
continue;
|
|
885
|
-
}
|
|
886
|
-
const fieldKey = row.key[2];
|
|
887
|
-
if (typeof fieldKey !== "string") continue;
|
|
888
|
-
const jsonValue = cloneJsonValue(row.value);
|
|
889
|
-
if (jsonValue === void 0) continue;
|
|
890
|
-
let docMeta = metadata.get(docId);
|
|
891
|
-
if (!docMeta) {
|
|
892
|
-
docMeta = {};
|
|
893
|
-
metadata.set(docId, docMeta);
|
|
894
|
-
}
|
|
895
|
-
docMeta[fieldKey] = jsonValue;
|
|
896
|
-
}
|
|
897
|
-
const existence = /* @__PURE__ */ new Map();
|
|
898
|
-
const existenceRows = this.metaFlock.scan({ prefix: ["e"] });
|
|
899
|
-
for (const row of existenceRows) {
|
|
900
|
-
if (!Array.isArray(row.key) || row.key.length < 2) continue;
|
|
901
|
-
const docId = row.key[1];
|
|
902
|
-
if (typeof docId !== "string") continue;
|
|
903
|
-
if (typeof row.value !== "boolean") continue;
|
|
904
|
-
existence.set(docId, row.value ? "active" : "deleted");
|
|
905
|
-
}
|
|
906
|
-
return {
|
|
907
|
-
metadata,
|
|
908
|
-
existence
|
|
909
|
-
};
|
|
910
|
-
}
|
|
911
729
|
isDocKnown(docId) {
|
|
912
730
|
if (typeof this.metaFlock.get(["e", docId]) === "boolean") return true;
|
|
913
731
|
const rows = this.metaFlock.scan({ prefix: ["m", docId] });
|
|
@@ -934,6 +752,13 @@ var FlockHydrator = class {
|
|
|
934
752
|
|
|
935
753
|
//#endregion
|
|
936
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
|
+
}
|
|
937
762
|
/**
|
|
938
763
|
* Sync data between storage and transport layer
|
|
939
764
|
*/
|
|
@@ -1024,11 +849,16 @@ var SyncRunner = class {
|
|
|
1024
849
|
await this.ready();
|
|
1025
850
|
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1026
851
|
if (!this.transport.isConnected()) await this.transport.connect();
|
|
852
|
+
const requestedAuth = params?.auth;
|
|
1027
853
|
const existing = this.metaRoomSubscription;
|
|
1028
|
-
if (existing) {
|
|
854
|
+
if (existing && bytesEqual(existing.auth, requestedAuth)) {
|
|
1029
855
|
existing.refCount += 1;
|
|
1030
856
|
return this.createMetaLease(existing);
|
|
1031
857
|
}
|
|
858
|
+
if (existing) {
|
|
859
|
+
existing.base.unsubscribe();
|
|
860
|
+
this.metaRoomSubscription = void 0;
|
|
861
|
+
}
|
|
1032
862
|
await this.ensureMetaLiveMonitor();
|
|
1033
863
|
const base = this.transport.joinMetaRoom(this.metaFlock, params);
|
|
1034
864
|
const firstSyncedWithRemote = base.firstSyncedWithRemote.then(async () => {
|
|
@@ -1040,6 +870,7 @@ var SyncRunner = class {
|
|
|
1040
870
|
const record = {
|
|
1041
871
|
base,
|
|
1042
872
|
firstSyncedWithRemote,
|
|
873
|
+
auth: requestedAuth,
|
|
1043
874
|
refCount: 1
|
|
1044
875
|
};
|
|
1045
876
|
this.metaRoomSubscription = record;
|
|
@@ -1049,15 +880,21 @@ var SyncRunner = class {
|
|
|
1049
880
|
await this.ready();
|
|
1050
881
|
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1051
882
|
if (!this.transport.isConnected()) await this.transport.connect();
|
|
883
|
+
const requestedAuth = params?.auth;
|
|
1052
884
|
const existing = this.docSubscriptions.get(docId);
|
|
1053
|
-
if (existing) {
|
|
885
|
+
if (existing && bytesEqual(existing.auth, requestedAuth)) {
|
|
1054
886
|
existing.refCount += 1;
|
|
1055
887
|
return this.createDocLease(docId, existing);
|
|
1056
888
|
}
|
|
889
|
+
if (existing) {
|
|
890
|
+
existing.base.unsubscribe();
|
|
891
|
+
if (this.docSubscriptions.get(docId) === existing) this.docSubscriptions.delete(docId);
|
|
892
|
+
}
|
|
1057
893
|
const doc = await this.docManager.ensureDoc(docId);
|
|
1058
894
|
const base = this.transport.joinDocRoom(docId, doc, params);
|
|
1059
895
|
const record = {
|
|
1060
896
|
base,
|
|
897
|
+
auth: requestedAuth,
|
|
1061
898
|
refCount: 1
|
|
1062
899
|
};
|
|
1063
900
|
this.docSubscriptions.set(docId, record);
|
|
@@ -1083,9 +920,6 @@ var SyncRunner = class {
|
|
|
1083
920
|
}
|
|
1084
921
|
},
|
|
1085
922
|
firstSyncedWithRemote: record.firstSyncedWithRemote ?? record.base.firstSyncedWithRemote,
|
|
1086
|
-
get connected() {
|
|
1087
|
-
return record.base.connected;
|
|
1088
|
-
},
|
|
1089
923
|
get status() {
|
|
1090
924
|
return record.base.status;
|
|
1091
925
|
},
|
|
@@ -1107,9 +941,6 @@ var SyncRunner = class {
|
|
|
1107
941
|
}
|
|
1108
942
|
},
|
|
1109
943
|
firstSyncedWithRemote: record.base.firstSyncedWithRemote,
|
|
1110
|
-
get connected() {
|
|
1111
|
-
return record.base.connected;
|
|
1112
|
-
},
|
|
1113
944
|
get status() {
|
|
1114
945
|
return record.base.status;
|
|
1115
946
|
},
|
|
@@ -1363,9 +1194,6 @@ var LoroRepo = class LoroRepo {
|
|
|
1363
1194
|
const subscription = await this.syncRunner.joinMetaRoom(params);
|
|
1364
1195
|
return {
|
|
1365
1196
|
unsubscribe: subscription.unsubscribe,
|
|
1366
|
-
get connected() {
|
|
1367
|
-
return subscription.connected;
|
|
1368
|
-
},
|
|
1369
1197
|
get status() {
|
|
1370
1198
|
return subscription.status;
|
|
1371
1199
|
},
|
|
@@ -1376,23 +1204,13 @@ var LoroRepo = class LoroRepo {
|
|
|
1376
1204
|
};
|
|
1377
1205
|
}
|
|
1378
1206
|
async joinDocRoom(docId, params) {
|
|
1379
|
-
|
|
1380
|
-
return {
|
|
1381
|
-
...subscription,
|
|
1382
|
-
onStatusChange: subscription.onStatusChange,
|
|
1383
|
-
status: subscription.status
|
|
1384
|
-
};
|
|
1207
|
+
return this.syncRunner.joinDocRoom(docId, params);
|
|
1385
1208
|
}
|
|
1386
1209
|
async joinEphemeralRoom(roomId) {
|
|
1387
1210
|
if (!this.transport) throw new Error("Transport adapter not configured");
|
|
1388
1211
|
await this.syncRunner.ready();
|
|
1389
1212
|
if (!this.transport.isConnected()) await this.transport.connect();
|
|
1390
|
-
|
|
1391
|
-
return {
|
|
1392
|
-
...subscription,
|
|
1393
|
-
onStatusChange: subscription.onStatusChange,
|
|
1394
|
-
status: subscription.status
|
|
1395
|
-
};
|
|
1213
|
+
return this.transport.joinEphemeralRoom(roomId);
|
|
1396
1214
|
}
|
|
1397
1215
|
async openPersistedDoc(docId) {
|
|
1398
1216
|
return {
|
|
@@ -1424,8 +1242,10 @@ var LoroRepo = class LoroRepo {
|
|
|
1424
1242
|
const previous = this.metadataManager.getExistenceState(docId);
|
|
1425
1243
|
await this.docManager.dropDoc(docId);
|
|
1426
1244
|
await this.purgeDocKeyspace(docId);
|
|
1427
|
-
this.metadataManager.
|
|
1428
|
-
|
|
1245
|
+
this.metadataManager.reconcileDoc(docId, "local", {
|
|
1246
|
+
previousExistence: previous,
|
|
1247
|
+
metadataChanged: true
|
|
1248
|
+
});
|
|
1429
1249
|
}
|
|
1430
1250
|
getMeta() {
|
|
1431
1251
|
return this.metaFlock;
|