@loro-dev/flock 2.0.1 → 2.1.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/src/index.ts CHANGED
@@ -38,6 +38,7 @@ type MaybePromise<T> = T | Promise<T>;
38
38
  type ExportOptions = {
39
39
  from?: VersionVector;
40
40
  hooks?: ExportHooks;
41
+ pruneTombstonesBefore?: number;
41
42
  };
42
43
 
43
44
  type ImportOptions = {
@@ -246,6 +247,16 @@ function encodeVersionVector(vv?: VersionVector): RawVersionVector | undefined {
246
247
  return raw;
247
248
  }
248
249
 
250
+ function normalizePruneBefore(pruneTombstonesBefore?: number): number | undefined {
251
+ if (pruneTombstonesBefore === undefined) {
252
+ return undefined;
253
+ }
254
+ if (typeof pruneTombstonesBefore !== "number" || !Number.isFinite(pruneTombstonesBefore)) {
255
+ return undefined;
256
+ }
257
+ return pruneTombstonesBefore;
258
+ }
259
+
249
260
  function decodeVersionVector(raw: unknown): VersionVector {
250
261
  if (raw === null || typeof raw !== "object") {
251
262
  return {};
@@ -507,7 +518,11 @@ function isExportOptions(value: unknown): value is ExportOptions {
507
518
  typeof value === "object" &&
508
519
  value !== null &&
509
520
  (Object.prototype.hasOwnProperty.call(value, "hooks") ||
510
- Object.prototype.hasOwnProperty.call(value, "from"))
521
+ Object.prototype.hasOwnProperty.call(value, "from") ||
522
+ Object.prototype.hasOwnProperty.call(
523
+ value,
524
+ "pruneTombstonesBefore",
525
+ ))
511
526
  );
512
527
  }
513
528
 
@@ -555,9 +570,8 @@ export class Flock {
555
570
  metadata?: MetadataMap,
556
571
  now?: number,
557
572
  ): void {
558
- const clonedValue = cloneJson(value);
559
573
  const metadataClone = cloneMetadata(metadata);
560
- put_with_meta_ffi(this.inner, key, clonedValue, metadataClone, now);
574
+ put_with_meta_ffi(this.inner, key, JSON.stringify(value), metadataClone, now);
561
575
  }
562
576
 
563
577
  private async putWithMetaWithHooks(
@@ -599,7 +613,7 @@ export class Flock {
599
613
  * @param now
600
614
  */
601
615
  put(key: KeyPart[], value: Value, now?: number): void {
602
- put_json_ffi(this.inner, key, value, now);
616
+ put_json_ffi(this.inner, key, JSON.stringify(value), now);
603
617
  }
604
618
 
605
619
  putWithMeta(key: KeyPart[], value: Value, options?: PutWithMetaOptions): void | Promise<void> {
@@ -635,12 +649,23 @@ export class Flock {
635
649
  return decodeVersionVector(version_ffi(this.inner));
636
650
  }
637
651
 
638
- private exportJsonInternal(from?: VersionVector): ExportBundle {
639
- return export_json_ffi(this.inner, encodeVersionVector(from)) as ExportBundle;
652
+ private exportJsonInternal(
653
+ from?: VersionVector,
654
+ pruneTombstonesBefore?: number,
655
+ ): ExportBundle {
656
+ const pruneBefore = normalizePruneBefore(pruneTombstonesBefore);
657
+ return export_json_ffi(
658
+ this.inner,
659
+ encodeVersionVector(from),
660
+ pruneBefore,
661
+ ) as ExportBundle;
640
662
  }
641
663
 
642
664
  private async exportJsonWithHooks(options: ExportOptions): Promise<ExportBundle> {
643
- const base = this.exportJsonInternal(options.from);
665
+ const base = this.exportJsonInternal(
666
+ options.from,
667
+ options.pruneTombstonesBefore,
668
+ );
644
669
  const transform = options.hooks?.transform;
645
670
  if (!transform) {
646
671
  return base;
@@ -662,17 +687,22 @@ export class Flock {
662
687
 
663
688
  exportJson(): ExportBundle;
664
689
  exportJson(from: VersionVector): ExportBundle;
690
+ exportJson(
691
+ from: VersionVector,
692
+ pruneTombstonesBefore: number,
693
+ ): ExportBundle;
665
694
  exportJson(options: ExportOptions): Promise<ExportBundle>;
666
695
  exportJson(
667
696
  arg?: VersionVector | ExportOptions,
697
+ pruneTombstonesBefore?: number,
668
698
  ): ExportBundle | Promise<ExportBundle> {
669
699
  if (arg === undefined) {
670
- return this.exportJsonInternal();
700
+ return this.exportJsonInternal(undefined, pruneTombstonesBefore);
671
701
  }
672
702
  if (isExportOptions(arg)) {
673
703
  return this.exportJsonWithHooks(arg);
674
704
  }
675
- return this.exportJsonInternal(arg);
705
+ return this.exportJsonInternal(arg, pruneTombstonesBefore);
676
706
  }
677
707
 
678
708
  private importJsonInternal(bundle: ExportBundle): ImportReport {
@@ -745,7 +775,7 @@ export class Flock {
745
775
  }
746
776
 
747
777
  putMvr(key: KeyPart[], value: Value, now?: number): void {
748
- put_mvr_ffi(this.inner, key, value, now);
778
+ put_mvr_ffi(this.inner, key, JSON.stringify(value), now);
749
779
  }
750
780
 
751
781
  getMvr(key: KeyPart[]): Value[] {