loro-crdt 1.2.0 → 1.2.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.2.2
4
+
5
+ ### Patch Changes
6
+
7
+ - 3b7a738: Add getShallowValue and toJsonWIthReplacer
8
+
9
+ - Add getShallowValue for each container (#581)
10
+ - Implement toJsonWithReplacer method for LoroDoc to customize JSON serialization (#582)
11
+ - Rename importUpdateBatch into importBatch & refine type (#580)
12
+
13
+ ## 1.2.1
14
+
15
+ ### Patch Changes
16
+
17
+ - adb6ab8: fix: panic when returned non-boolean value from text.iter(f) #578
18
+
3
19
  ## 1.2.0
4
20
 
5
21
  ### Minor Changes
package/base64/index.js CHANGED
@@ -861,6 +861,14 @@ class LoroCounter {
861
861
  const ret = wasm.lorocounter_getAttached(this.__wbg_ptr);
862
862
  return takeObject(ret);
863
863
  }
864
+ /**
865
+ * Get the value of the counter.
866
+ * @returns {number}
867
+ */
868
+ getShallowValue() {
869
+ const ret = wasm.lorocounter_getShallowValue(this.__wbg_ptr);
870
+ return ret;
871
+ }
864
872
  }
865
873
 
866
874
  const LoroDocFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -1987,10 +1995,12 @@ class LoroDoc {
1987
1995
  }
1988
1996
  }
1989
1997
  /**
1990
- * Import a batch of updates.
1998
+ * Import a batch of updates and snapshots.
1991
1999
  *
1992
2000
  * It's more efficient than importing updates one by one.
1993
2001
  *
2002
+ * @deprecated Use `importBatch` instead.
2003
+ *
1994
2004
  * @example
1995
2005
  * ```ts
1996
2006
  * import { LoroDoc } from "loro-crdt";
@@ -2001,9 +2011,9 @@ class LoroDoc {
2001
2011
  * const updates = doc.export({ mode: "update" });
2002
2012
  * const snapshot = doc.export({ mode: "snapshot" });
2003
2013
  * const doc2 = new LoroDoc();
2004
- * doc2.importUpdateBatch([snapshot, updates]);
2014
+ * doc2.importBatch([snapshot, updates]);
2005
2015
  * ```
2006
- * @param {Array<any>} data
2016
+ * @param {Uint8Array[]} data
2007
2017
  * @returns {ImportStatus}
2008
2018
  */
2009
2019
  importUpdateBatch(data) {
@@ -2022,8 +2032,46 @@ class LoroDoc {
2022
2032
  }
2023
2033
  }
2024
2034
  /**
2035
+ * Import a batch of updates or snapshots.
2036
+ *
2037
+ * It's more efficient than importing updates one by one.
2038
+ *
2039
+ * @example
2040
+ * ```ts
2041
+ * import { LoroDoc } from "loro-crdt";
2042
+ *
2043
+ * const doc = new LoroDoc();
2044
+ * const text = doc.getText("text");
2045
+ * text.insert(0, "Hello");
2046
+ * const updates = doc.export({ mode: "update" });
2047
+ * const snapshot = doc.export({ mode: "snapshot" });
2048
+ * const doc2 = new LoroDoc();
2049
+ * doc2.importBatch([snapshot, updates]);
2050
+ * ```
2051
+ * @param {Uint8Array[]} data
2052
+ * @returns {ImportStatus}
2053
+ */
2054
+ importBatch(data) {
2055
+ try {
2056
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
2057
+ wasm.lorodoc_importBatch(retptr, this.__wbg_ptr, addHeapObject(data));
2058
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
2059
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
2060
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
2061
+ if (r2) {
2062
+ throw takeObject(r1);
2063
+ }
2064
+ return takeObject(r0);
2065
+ } finally {
2066
+ wasm.__wbindgen_add_to_stack_pointer(16);
2067
+ }
2068
+ }
2069
+ /**
2025
2070
  * Get the shallow json format of the document state.
2026
2071
  *
2072
+ * Unlike `toJSON()` which recursively resolves all containers to their values,
2073
+ * `getShallowValue()` returns container IDs as strings for any nested containers.
2074
+ *
2027
2075
  * @example
2028
2076
  * ```ts
2029
2077
  * import { LoroDoc } from "loro-crdt";
@@ -2033,12 +2081,17 @@ class LoroDoc {
2033
2081
  * const tree = doc.getTree("tree");
2034
2082
  * const map = doc.getMap("map");
2035
2083
  * const shallowValue = doc.getShallowValue();
2036
- * /*
2037
- * {"list": ..., "tree": ..., "map": ...}
2038
- * *\/
2039
2084
  * console.log(shallowValue);
2085
+ * // {
2086
+ * // list: 'cid:root-list:List',
2087
+ * // tree: 'cid:root-tree:Tree',
2088
+ * // map: 'cid:root-map:Map'
2089
+ * // }
2090
+ *
2091
+ * // It points to the same container as `list`
2092
+ * const listB = doc.getContainerById(shallowValue.list);
2040
2093
  * ```
2041
- * @returns {any}
2094
+ * @returns {Record<string, ContainerID>}
2042
2095
  */
2043
2096
  getShallowValue() {
2044
2097
  try {
@@ -2058,9 +2111,12 @@ class LoroDoc {
2058
2111
  /**
2059
2112
  * Get the json format of the entire document state.
2060
2113
  *
2114
+ * Unlike `getShallowValue()` which returns container IDs as strings,
2115
+ * `toJSON()` recursively resolves all containers to their actual values.
2116
+ *
2061
2117
  * @example
2062
2118
  * ```ts
2063
- * import { LoroDoc, LoroText, LoroMap } from "loro-crdt";
2119
+ * import { LoroDoc, LoroText } from "loro-crdt";
2064
2120
  *
2065
2121
  * const doc = new LoroDoc();
2066
2122
  * const list = doc.getList("list");
@@ -2069,10 +2125,8 @@ class LoroDoc {
2069
2125
  * text.insert(0, "Hello");
2070
2126
  * const map = list.insertContainer(1, new LoroMap());
2071
2127
  * map.set("foo", "bar");
2072
- * /*
2073
- * {"list": ["Hello", {"foo": "bar"}]}
2074
- * *\/
2075
2128
  * console.log(doc.toJSON());
2129
+ * // {"list": ["Hello", {"foo": "bar"}]}
2076
2130
  * ```
2077
2131
  * @returns {any}
2078
2132
  */
@@ -2827,6 +2881,29 @@ class LoroList {
2827
2881
  const ret = wasm.lorolist_isDeleted(this.__wbg_ptr);
2828
2882
  return ret !== 0;
2829
2883
  }
2884
+ /**
2885
+ * Get the shallow value of the list.
2886
+ *
2887
+ * Unlike `toJSON()` which recursively resolves all containers to their values,
2888
+ * `getShallowValue()` returns container IDs as strings for any nested containers.
2889
+ *
2890
+ * ```js
2891
+ * const doc = new LoroDoc();
2892
+ * doc.setPeerId("1");
2893
+ * const list = doc.getList("list");
2894
+ * list.insert(0, 1);
2895
+ * list.insert(1, "two");
2896
+ * const subList = list.insertContainer(2, new LoroList());
2897
+ * subList.insert(0, "sub");
2898
+ * list.getShallowValue(); // [1, "two", "cid:2@1:List"]
2899
+ * list.toJSON(); // [1, "two", ["sub"]]
2900
+ * ```
2901
+ * @returns {Value[]}
2902
+ */
2903
+ getShallowValue() {
2904
+ const ret = wasm.lorolist_getShallowValue(this.__wbg_ptr);
2905
+ return takeObject(ret);
2906
+ }
2830
2907
  }
2831
2908
 
2832
2909
  const LoroMapFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -3277,6 +3354,37 @@ class LoroMap {
3277
3354
  const ret = wasm.loromap_isDeleted(this.__wbg_ptr);
3278
3355
  return ret !== 0;
3279
3356
  }
3357
+ /**
3358
+ * Get the shallow value of the map.
3359
+ *
3360
+ * Unlike `toJSON()` which recursively resolves all containers to their values,
3361
+ * `getShallowValue()` returns container IDs as strings for any nested containers.
3362
+ *
3363
+ * @example
3364
+ * ```ts
3365
+ * import { LoroDoc } from "loro-crdt";
3366
+ *
3367
+ * const doc = new LoroDoc();
3368
+ * doc.setPeerId("1");
3369
+ * const map = doc.getMap("map");
3370
+ * map.set("key", "value");
3371
+ * const subText = map.setContainer("text", new LoroText());
3372
+ * subText.insert(0, "Hello");
3373
+ *
3374
+ * // Get shallow value - nested containers are represented by their IDs
3375
+ * console.log(map.getShallowValue());
3376
+ * // Output: { key: "value", text: "cid:1@1:Text" }
3377
+ *
3378
+ * // Get full value with nested containers resolved by `toJSON()`
3379
+ * console.log(map.toJSON());
3380
+ * // Output: { key: "value", text: "Hello" }
3381
+ * ```
3382
+ * @returns {Record<string, Value>}
3383
+ */
3384
+ getShallowValue() {
3385
+ const ret = wasm.loromap_getShallowValue(this.__wbg_ptr);
3386
+ return takeObject(ret);
3387
+ }
3280
3388
  }
3281
3389
 
3282
3390
  const LoroMovableListFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -3784,6 +3892,29 @@ class LoroMovableList {
3784
3892
  const ret = wasm.loromovablelist_isDeleted(this.__wbg_ptr);
3785
3893
  return ret !== 0;
3786
3894
  }
3895
+ /**
3896
+ * Get the shallow value of the movable list.
3897
+ *
3898
+ * Unlike `toJSON()` which recursively resolves all containers to their values,
3899
+ * `getShallowValue()` returns container IDs as strings for any nested containers.
3900
+ *
3901
+ * ```js
3902
+ * const doc = new LoroDoc();
3903
+ * doc.setPeerId("1");
3904
+ * const list = doc.getMovableList("list");
3905
+ * list.insert(0, 1);
3906
+ * list.insert(1, "two");
3907
+ * const subList = list.insertContainer(2, new LoroList());
3908
+ * subList.insert(0, "sub");
3909
+ * list.getShallowValue(); // [1, "two", "cid:2@1:List"]
3910
+ * list.toJSON(); // [1, "two", ["sub"]]
3911
+ * ```
3912
+ * @returns {Value[]}
3913
+ */
3914
+ getShallowValue() {
3915
+ const ret = wasm.loromovablelist_getShallowValue(this.__wbg_ptr);
3916
+ return takeObject(ret);
3917
+ }
3787
3918
  }
3788
3919
 
3789
3920
  const LoroTextFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -3852,13 +3983,19 @@ class LoroText {
3852
3983
  * text.insert(0, "Hello");
3853
3984
  * text.iter((str) => (console.log(str), true));
3854
3985
  * ```
3855
- * @param {Function} callback
3986
+ * @param {(string) => boolean} callback
3856
3987
  */
3857
3988
  iter(callback) {
3858
3989
  try {
3859
- wasm.lorotext_iter(this.__wbg_ptr, addBorrowedObject(callback));
3990
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
3991
+ wasm.lorotext_iter(retptr, this.__wbg_ptr, addHeapObject(callback));
3992
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
3993
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
3994
+ if (r1) {
3995
+ throw takeObject(r0);
3996
+ }
3860
3997
  } finally {
3861
- heap[stack_pointer++] = undefined;
3998
+ wasm.__wbindgen_add_to_stack_pointer(16);
3862
3999
  }
3863
4000
  }
3864
4001
  /**
@@ -4433,6 +4570,26 @@ class LoroText {
4433
4570
  const ret = wasm.lorotext_isDeleted(this.__wbg_ptr);
4434
4571
  return ret !== 0;
4435
4572
  }
4573
+ /**
4574
+ * Get the shallow value of the text. This equals to `text.toString()`.
4575
+ * @returns {string}
4576
+ */
4577
+ getShallowValue() {
4578
+ let deferred1_0;
4579
+ let deferred1_1;
4580
+ try {
4581
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
4582
+ wasm.lorotext_getShallowValue(retptr, this.__wbg_ptr);
4583
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
4584
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
4585
+ deferred1_0 = r0;
4586
+ deferred1_1 = r1;
4587
+ return getStringFromWasm0(r0, r1);
4588
+ } finally {
4589
+ wasm.__wbindgen_add_to_stack_pointer(16);
4590
+ wasm.__wbindgen_export_5(deferred1_0, deferred1_1, 1);
4591
+ }
4592
+ }
4436
4593
  }
4437
4594
 
4438
4595
  const LoroTreeFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -4880,6 +5037,51 @@ class LoroTree {
4880
5037
  const ret = wasm.lorotree_isDeleted(this.__wbg_ptr);
4881
5038
  return ret !== 0;
4882
5039
  }
5040
+ /**
5041
+ * Get the shallow value of the tree.
5042
+ *
5043
+ * Unlike `toJSON()` which recursively resolves nested containers to their values,
5044
+ * `getShallowValue()` returns container IDs as strings for any nested containers.
5045
+ *
5046
+ * @example
5047
+ * ```ts
5048
+ * const doc = new LoroDoc();
5049
+ * doc.setPeerId("1");
5050
+ * const tree = doc.getTree("tree");
5051
+ * const root = tree.createNode();
5052
+ * root.data.set("name", "root");
5053
+ * const text = root.data.setContainer("content", new LoroText());
5054
+ * text.insert(0, "Hello");
5055
+ *
5056
+ * console.log(tree.getShallowValue());
5057
+ * // [{
5058
+ * // id: "0@1",
5059
+ * // parent: null,
5060
+ * // index: 0,
5061
+ * // fractional_index: "80",
5062
+ * // meta: "cid:0@1:Map",
5063
+ * // children: []
5064
+ * // }]
5065
+ *
5066
+ * console.log(tree.toJSON());
5067
+ * // [{
5068
+ * // id: "0@1",
5069
+ * // parent: null,
5070
+ * // index: 0,
5071
+ * // fractional_index: "80",
5072
+ * // meta: {
5073
+ * // name: "root",
5074
+ * // content: "Hello"
5075
+ * // },
5076
+ * // children: []
5077
+ * // }]
5078
+ * ```
5079
+ * @returns {TreeNodeShallowValue[]}
5080
+ */
5081
+ getShallowValue() {
5082
+ const ret = wasm.lorotree_getShallowValue(this.__wbg_ptr);
5083
+ return takeObject(ret);
5084
+ }
4883
5085
  }
4884
5086
 
4885
5087
  const LoroTreeNodeFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -6251,7 +6453,7 @@ var imports = /*#__PURE__*/Object.freeze({
6251
6453
  // Without this patch, Cloudflare Worker would raise issue like: "Uncaught TypeError: wasm2.__wbindgen_start is not a function"
6252
6454
 
6253
6455
 
6254
- import loro_wasm_bg_js from './loro_wasm_bg-543a4cca.js';
6456
+ import loro_wasm_bg_js from './loro_wasm_bg-8056e30c.js';
6255
6457
  const instance = new WebAssembly.Instance(loro_wasm_bg_js(), {
6256
6458
  "./loro_wasm_bg.js": imports,
6257
6459
  });
@@ -6408,5 +6610,52 @@ class Awareness {
6408
6610
  }, this.timeout / 2);
6409
6611
  }
6410
6612
  }
6613
+ LoroDoc.prototype.toJsonWithReplacer = function (replacer) {
6614
+ const doc = this;
6615
+ const m = (key, value) => {
6616
+ if (typeof value === "string") {
6617
+ if (isContainerId(value)) {
6618
+ const container = doc.getContainerById(value);
6619
+ if (container == null) {
6620
+ throw new Error(`ContainerID not found: ${value}`);
6621
+ }
6622
+ const ans = replacer(key, container);
6623
+ if (ans === container) {
6624
+ const ans = container.getShallowValue();
6625
+ if (typeof ans === "object") {
6626
+ return run(ans);
6627
+ }
6628
+ return ans;
6629
+ }
6630
+ if (isContainer(ans)) {
6631
+ throw new Error("Using new container is not allowed in toJsonWithReplacer");
6632
+ }
6633
+ return ans;
6634
+ }
6635
+ }
6636
+ const ans = replacer(key, value);
6637
+ if (isContainer(ans)) {
6638
+ throw new Error("Using new container is not allowed in toJsonWithReplacer");
6639
+ }
6640
+ return ans;
6641
+ };
6642
+ const run = (layer) => {
6643
+ if (Array.isArray(layer)) {
6644
+ return layer.map((item, index) => {
6645
+ return m(index, item);
6646
+ }).filter((item) => item !== undefined);
6647
+ }
6648
+ const result = {};
6649
+ for (const [key, value] of Object.entries(layer)) {
6650
+ const ans = m(key, value);
6651
+ if (ans !== undefined) {
6652
+ result[key] = ans;
6653
+ }
6654
+ }
6655
+ return result;
6656
+ };
6657
+ const layer = this.getShallowValue();
6658
+ return run(layer);
6659
+ };
6411
6660
 
6412
6661
  export { Awareness, AwarenessWasm, Cursor, Loro, LoroCounter, LoroDoc, LoroList, LoroMap, LoroMovableList, LoroText, LoroTree, LoroTreeNode, UndoManager, VersionVector, __wbg_String_b9412f8799faab3e, __wbg_apply_0a5aa603881e6d79, __wbg_buffer_12d079cc21e14bdb, __wbg_call_27c0f87801dedf93, __wbg_call_8e7cb608789c2528, __wbg_call_938992c832f74314, __wbg_call_b3ca7c6051f9bec1, __wbg_crypto_1d1f22824a6a080c, __wbg_cursor_new, __wbg_done_298b57d23c0fc80c, __wbg_entries_95cc2c823b285a09, __wbg_entries_ce844941d0c51880, __wbg_error_c8c2cca30a630316, __wbg_error_f851667af71bcfc6, __wbg_getRandomValues_3aa56aa6edec874c, __wbg_get_bd8e338fbd5f5cc8, __wbg_get_e3c254076557e348, __wbg_getindex_03d06b4e7ea3475e, __wbg_getwithrefkey_edc2c8960f0f1191, __wbg_globalThis_d1e6af4856ba331b, __wbg_global_207b558942527489, __wbg_instanceof_ArrayBuffer_836825be07d4c9d2, __wbg_instanceof_Map_87917e0a7aaf4012, __wbg_instanceof_Object_71ca3c0a59266746, __wbg_instanceof_Uint8Array_2b3bbecd033d19f6, __wbg_isArray_2ab64d95e09ea0ae, __wbg_isSafeInteger_f7b04ef02296c4d2, __wbg_iterator_2cee6dadfd956dfa, __wbg_length_c20a40f15020d68a, __wbg_length_cd7af8117672b8b8, __wbg_log_aba5996d9bde071f, __wbg_log_c9486ca5d8e2cbe8, __wbg_log_d8fdbde28117925d, __wbg_lorocounter_new, __wbg_lorolist_new, __wbg_loromap_new, __wbg_loromovablelist_new, __wbg_lorotext_new, __wbg_lorotree_new, __wbg_lorotreenode_new, __wbg_mark_40e050a77cc39fea, __wbg_measure_aa7a73f17813f708, __wbg_msCrypto_eb05e62b530a1508, __wbg_new_16b304a2cfa7ff4a, __wbg_new_63b92bc8671ed464, __wbg_new_72fb9a18b5ae2624, __wbg_new_abda76e883ba8a5f, __wbg_new_d9bc3a0147634640, __wbg_newnoargs_e258087cd0daa0ea, __wbg_newwithbyteoffsetandlength_aa4a17c33a06e5cb, __wbg_newwithlength_66ae46612e7f0234, __wbg_newwithlength_e9b4878cebadb3d3, __wbg_next_196c84450b364254, __wbg_next_40fc327bfc8770e6, __wbg_node_104a2ff8d6ea03a2, __wbg_now_11683c634f92ae89, __wbg_ownKeys_658942b7f28d1fe9, __wbg_process_4a72847cc503995b, __wbg_push_a5b05aedc7234f9f, __wbg_randomFillSync_5c9c955aa56b6049, __wbg_require_cca90b1a94a0255b, __wbg_resolve_b0083a7967828ec8, __wbg_self_ce0dbfc45cf2f5be, __wbg_set_1f9b04f170055d33, __wbg_set_8417257aaedc936b, __wbg_set_a47bac70306a19a7, __wbg_set_d4638f722068f043, __wbg_set_f975102236d3c502, __wbg_set_wasm, __wbg_setindex_0b7ede192dc5eca8, __wbg_stack_658279fe44541cf6, __wbg_subarray_a1f73cd4b5b42fe1, __wbg_then_0c86a60e8fcfe9f6, __wbg_value_d93c65011f51a456, __wbg_versions_f686565e586dd935, __wbg_versionvector_new, __wbg_window_c6fb939a7f436783, __wbindgen_as_number, __wbindgen_bigint_from_i64, __wbindgen_bigint_from_u64, __wbindgen_bigint_get_as_i64, __wbindgen_boolean_get, __wbindgen_cb_drop, __wbindgen_closure_wrapper487, __wbindgen_closure_wrapper489, __wbindgen_debug_string, __wbindgen_error_new, __wbindgen_in, __wbindgen_is_bigint, __wbindgen_is_falsy, __wbindgen_is_function, __wbindgen_is_null, __wbindgen_is_object, __wbindgen_is_string, __wbindgen_is_undefined, __wbindgen_jsval_eq, __wbindgen_jsval_loose_eq, __wbindgen_memory, __wbindgen_number_get, __wbindgen_number_new, __wbindgen_object_clone_ref, __wbindgen_object_drop_ref, __wbindgen_rethrow, __wbindgen_string_get, __wbindgen_string_new, __wbindgen_throw, __wbindgen_typeof, decodeFrontiers, decodeImportBlobMeta, encodeFrontiers, getType, isContainer, isContainerId, newContainerID, newRootContainerID, run, setDebug };