@loro-dev/flock 2.0.1 → 2.1.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/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
 
@@ -635,12 +650,23 @@ export class Flock {
635
650
  return decodeVersionVector(version_ffi(this.inner));
636
651
  }
637
652
 
638
- private exportJsonInternal(from?: VersionVector): ExportBundle {
639
- return export_json_ffi(this.inner, encodeVersionVector(from)) as ExportBundle;
653
+ private exportJsonInternal(
654
+ from?: VersionVector,
655
+ pruneTombstonesBefore?: number,
656
+ ): ExportBundle {
657
+ const pruneBefore = normalizePruneBefore(pruneTombstonesBefore);
658
+ return export_json_ffi(
659
+ this.inner,
660
+ encodeVersionVector(from),
661
+ pruneBefore,
662
+ ) as ExportBundle;
640
663
  }
641
664
 
642
665
  private async exportJsonWithHooks(options: ExportOptions): Promise<ExportBundle> {
643
- const base = this.exportJsonInternal(options.from);
666
+ const base = this.exportJsonInternal(
667
+ options.from,
668
+ options.pruneTombstonesBefore,
669
+ );
644
670
  const transform = options.hooks?.transform;
645
671
  if (!transform) {
646
672
  return base;
@@ -662,17 +688,22 @@ export class Flock {
662
688
 
663
689
  exportJson(): ExportBundle;
664
690
  exportJson(from: VersionVector): ExportBundle;
691
+ exportJson(
692
+ from: VersionVector,
693
+ pruneTombstonesBefore: number,
694
+ ): ExportBundle;
665
695
  exportJson(options: ExportOptions): Promise<ExportBundle>;
666
696
  exportJson(
667
697
  arg?: VersionVector | ExportOptions,
698
+ pruneTombstonesBefore?: number,
668
699
  ): ExportBundle | Promise<ExportBundle> {
669
700
  if (arg === undefined) {
670
- return this.exportJsonInternal();
701
+ return this.exportJsonInternal(undefined, pruneTombstonesBefore);
671
702
  }
672
703
  if (isExportOptions(arg)) {
673
704
  return this.exportJsonWithHooks(arg);
674
705
  }
675
- return this.exportJsonInternal(arg);
706
+ return this.exportJsonInternal(arg, pruneTombstonesBefore);
676
707
  }
677
708
 
678
709
  private importJsonInternal(bundle: ExportBundle): ImportReport {