@loro-dev/flock 2.0.0 → 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
 
@@ -592,6 +607,12 @@ export class Flock {
592
607
  );
593
608
  }
594
609
 
610
+ /**
611
+ * Put a value into the flock. If the given entry already exists, this insert will be skipped.
612
+ * @param key
613
+ * @param value
614
+ * @param now
615
+ */
595
616
  put(key: KeyPart[], value: Value, now?: number): void {
596
617
  put_json_ffi(this.inner, key, value, now);
597
618
  }
@@ -608,6 +629,11 @@ export class Flock {
608
629
  this.put(key, value, now);
609
630
  }
610
631
 
632
+ /**
633
+ * Delete a value from the flock. If the given entry does not exist, this delete will be skipped.
634
+ * @param key
635
+ * @param now
636
+ */
611
637
  delete(key: KeyPart[], now?: number): void {
612
638
  delete_ffi(this.inner, key, now);
613
639
  }
@@ -624,12 +650,23 @@ export class Flock {
624
650
  return decodeVersionVector(version_ffi(this.inner));
625
651
  }
626
652
 
627
- private exportJsonInternal(from?: VersionVector): ExportBundle {
628
- 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;
629
663
  }
630
664
 
631
665
  private async exportJsonWithHooks(options: ExportOptions): Promise<ExportBundle> {
632
- const base = this.exportJsonInternal(options.from);
666
+ const base = this.exportJsonInternal(
667
+ options.from,
668
+ options.pruneTombstonesBefore,
669
+ );
633
670
  const transform = options.hooks?.transform;
634
671
  if (!transform) {
635
672
  return base;
@@ -651,17 +688,22 @@ export class Flock {
651
688
 
652
689
  exportJson(): ExportBundle;
653
690
  exportJson(from: VersionVector): ExportBundle;
691
+ exportJson(
692
+ from: VersionVector,
693
+ pruneTombstonesBefore: number,
694
+ ): ExportBundle;
654
695
  exportJson(options: ExportOptions): Promise<ExportBundle>;
655
696
  exportJson(
656
697
  arg?: VersionVector | ExportOptions,
698
+ pruneTombstonesBefore?: number,
657
699
  ): ExportBundle | Promise<ExportBundle> {
658
700
  if (arg === undefined) {
659
- return this.exportJsonInternal();
701
+ return this.exportJsonInternal(undefined, pruneTombstonesBefore);
660
702
  }
661
703
  if (isExportOptions(arg)) {
662
704
  return this.exportJsonWithHooks(arg);
663
705
  }
664
- return this.exportJsonInternal(arg);
706
+ return this.exportJsonInternal(arg, pruneTombstonesBefore);
665
707
  }
666
708
 
667
709
  private importJsonInternal(bundle: ExportBundle): ImportReport {