@loro-dev/flock 1.1.1 → 2.0.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
@@ -75,7 +75,7 @@ export type ExportRecord = {
75
75
  m?: MetadataMap;
76
76
  };
77
77
 
78
- export type ExportBundle = Record<string, ExportRecord>;
78
+ export type ExportBundle = { version: number, entries: Record<string, ExportRecord> };
79
79
 
80
80
  export type EntryClock = {
81
81
  physicalTime: number;
@@ -495,9 +495,9 @@ function decodeImportReport(raw: unknown): ImportReport {
495
495
  }
496
496
 
497
497
  function cloneBundle(bundle: ExportBundle): ExportBundle {
498
- const next: ExportBundle = {};
499
- for (const [key, record] of Object.entries(bundle)) {
500
- next[key] = cloneRecord(record);
498
+ const next: ExportBundle = { version: bundle.version, entries: {} };
499
+ for (const [key, record] of Object.entries(bundle.entries)) {
500
+ next.entries[key] = cloneRecord(record);
501
501
  }
502
502
  return next;
503
503
  }
@@ -592,6 +592,12 @@ export class Flock {
592
592
  );
593
593
  }
594
594
 
595
+ /**
596
+ * Put a value into the flock. If the given entry already exists, this insert will be skipped.
597
+ * @param key
598
+ * @param value
599
+ * @param now
600
+ */
595
601
  put(key: KeyPart[], value: Value, now?: number): void {
596
602
  put_json_ffi(this.inner, key, value, now);
597
603
  }
@@ -608,6 +614,11 @@ export class Flock {
608
614
  this.put(key, value, now);
609
615
  }
610
616
 
617
+ /**
618
+ * Delete a value from the flock. If the given entry does not exist, this delete will be skipped.
619
+ * @param key
620
+ * @param now
621
+ */
611
622
  delete(key: KeyPart[], now?: number): void {
612
623
  delete_ffi(this.inner, key, now);
613
624
  }
@@ -634,8 +645,8 @@ export class Flock {
634
645
  if (!transform) {
635
646
  return base;
636
647
  }
637
- const result: ExportBundle = {};
638
- for (const [key, record] of Object.entries(base)) {
648
+ const result: ExportBundle = { version: base.version, entries: {} };
649
+ for (const [key, record] of Object.entries(base.entries)) {
639
650
  const context = buildContext(key, record);
640
651
  const basePayload = createExportPayload(record);
641
652
  const workingPayload = clonePayload(basePayload);
@@ -644,7 +655,7 @@ export class Flock {
644
655
  basePayload,
645
656
  transformed ?? workingPayload,
646
657
  );
647
- result[key] = buildRecord(record.c, finalPayload);
658
+ result.entries[key] = buildRecord(record.c, finalPayload);
648
659
  }
649
660
  return result;
650
661
  }
@@ -674,8 +685,8 @@ export class Flock {
674
685
  const working = preprocess ? cloneBundle(options.bundle) : options.bundle;
675
686
  const skippedByHooks: Array<{ key: KeyPart[]; reason: string }> = [];
676
687
  if (preprocess) {
677
- for (const key of Object.keys(working)) {
678
- const record = working[key];
688
+ for (const key of Object.keys(working.entries)) {
689
+ const record = working.entries[key];
679
690
  if (!record) {
680
691
  continue;
681
692
  }
@@ -685,10 +696,10 @@ export class Flock {
685
696
  const normalized = normalizeImportDecision(decision);
686
697
  if (!normalized.accept) {
687
698
  skippedByHooks.push({ key: context.key, reason: normalized.reason });
688
- delete working[key];
699
+ delete working.entries[key];
689
700
  continue;
690
701
  }
691
- working[key] = buildRecord(record.c, basePayload);
702
+ working.entries[key] = buildRecord(record.c, basePayload);
692
703
  }
693
704
  }
694
705
  const coreReport = this.importJsonInternal(working);
@@ -0,0 +1,57 @@
1
+ /**
2
+ * A non-public unique symbol used to mark the brand of a type.
3
+ * And avoid user to construct values of the type.
4
+ */
5
+ export const __brand: unique symbol;
6
+
7
+ export type Unit = undefined;
8
+ export type Bool = boolean;
9
+
10
+ /**
11
+ * A signed integer in 32-bit two's complement format.
12
+ */
13
+ export type Int = number;
14
+
15
+ /**
16
+ * An unsigned integer in 32-bit two's complement format.
17
+ */
18
+ export type UInt = number;
19
+
20
+ /**
21
+ * A character in the range 0-0x10FFFF.
22
+ */
23
+ export type Char = number;
24
+
25
+ /**
26
+ * A byte in the range 0-255.
27
+ */
28
+ export type Byte = number;
29
+
30
+ /**
31
+ * Single-precision floating point number.
32
+ */
33
+ export type Float = number;
34
+
35
+ export type Double = number;
36
+
37
+ export type Int64 = { [__brand]: 568910272 };
38
+
39
+ export type UInt64 = { [__brand]: 689305396 };
40
+
41
+ export type String = string;
42
+
43
+ export type Bytes = Uint8Array;
44
+
45
+ export type FixedArray<T> = T[];
46
+
47
+ export type UnboxedOption<T> =
48
+ | /* Some */ T
49
+ | /* None */ undefined;
50
+
51
+ export type UnboxedOptionAsInt<T> =
52
+ | /* Some */ T
53
+ | /* None */ -1;
54
+
55
+ export type Result<T, E> =
56
+ | /* Ok */ { $tag: 1, _0: T }
57
+ | /* Err */ { $tag: 0, _0: E };