loro-crdt 1.2.6 → 1.3.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.
@@ -232,11 +232,11 @@ function makeMutClosure(arg0, arg1, dtor, f) {
232
232
  CLOSURE_DTORS.register(real, state, state);
233
233
  return real;
234
234
  }
235
- function __wbg_adapter_58(arg0, arg1, arg2) {
235
+ function __wbg_adapter_60(arg0, arg1, arg2) {
236
236
  wasm.__wbindgen_export_3(arg0, arg1, addHeapObject(arg2));
237
237
  }
238
238
 
239
- function __wbg_adapter_61(arg0, arg1) {
239
+ function __wbg_adapter_63(arg0, arg1) {
240
240
  wasm.__wbindgen_export_4(arg0, arg1);
241
241
  }
242
242
 
@@ -969,9 +969,12 @@ class LoroDoc {
969
969
  wasm.lorodoc_setRecordTimestamp(this.__wbg_ptr, auto_record);
970
970
  }
971
971
  /**
972
- * If two continuous local changes are within the interval, they will be merged into one change.
972
+ * If two continuous local changes are within (<=) the interval(**in seconds**), they will be merged into one change.
973
973
  *
974
- * The default value is 1_000_000, the default unit is seconds.
974
+ * The default value is 1_000 seconds.
975
+ *
976
+ * By default, we record timestamps in seconds for each change. So if the merge interval is 1, and changes A and B
977
+ * have timestamps of 3 and 4 respectively, then they will be merged into one change
975
978
  * @param {number} interval
976
979
  */
977
980
  setChangeMergeInterval(interval) {
@@ -1235,6 +1238,72 @@ class LoroDoc {
1235
1238
  }
1236
1239
  }
1237
1240
  /**
1241
+ * Find the op id spans that between the `from` version and the `to` version.
1242
+ *
1243
+ * You can combine it with `exportJsonInIdSpan` to get the changes between two versions.
1244
+ *
1245
+ * You can use it to travel all the changes from `from` to `to`. `from` and `to` are frontiers,
1246
+ * and they can be concurrent to each other. You can use it to find all the changes related to an event:
1247
+ *
1248
+ * @example
1249
+ * ```ts
1250
+ * import { LoroDoc } from "loro-crdt";
1251
+ *
1252
+ * const docA = new LoroDoc();
1253
+ * docA.setPeerId("1");
1254
+ * const docB = new LoroDoc();
1255
+ *
1256
+ * docA.getText("text").update("Hello");
1257
+ * docA.commit();
1258
+ * const snapshot = docA.export({ mode: "snapshot" });
1259
+ * let done = false;
1260
+ * docB.subscribe(e => {
1261
+ * const spans = docB.findIdSpansBetween(e.from, e.to);
1262
+ * const changes = docB.exportJsonInIdSpan(spans.forward[0]);
1263
+ * console.log(changes);
1264
+ * // [{
1265
+ * // id: "0@1",
1266
+ * // timestamp: expect.any(Number),
1267
+ * // deps: [],
1268
+ * // lamport: 0,
1269
+ * // msg: undefined,
1270
+ * // ops: [{
1271
+ * // container: "cid:root-text:Text",
1272
+ * // counter: 0,
1273
+ * // content: {
1274
+ * // type: "insert",
1275
+ * // pos: 0,
1276
+ * // text: "Hello"
1277
+ * // }
1278
+ * // }]
1279
+ * // }]
1280
+ * });
1281
+ * docB.import(snapshot);
1282
+ * ```
1283
+ * @param {({ peer: PeerID, counter: number })[]} from
1284
+ * @param {({ peer: PeerID, counter: number })[]} to
1285
+ * @returns {VersionVectorDiff}
1286
+ */
1287
+ findIdSpansBetween(from, to) {
1288
+ try {
1289
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1290
+ const ptr0 = passArrayJsValueToWasm0(from, wasm.__wbindgen_export_0);
1291
+ const len0 = WASM_VECTOR_LEN;
1292
+ const ptr1 = passArrayJsValueToWasm0(to, wasm.__wbindgen_export_0);
1293
+ const len1 = WASM_VECTOR_LEN;
1294
+ wasm.lorodoc_findIdSpansBetween(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
1295
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
1296
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
1297
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
1298
+ if (r2) {
1299
+ throw takeObject(r1);
1300
+ }
1301
+ return takeObject(r0);
1302
+ } finally {
1303
+ wasm.__wbindgen_add_to_stack_pointer(16);
1304
+ }
1305
+ }
1306
+ /**
1238
1307
  * Checkout the `DocState` to a specific version.
1239
1308
  *
1240
1309
  * > The document becomes detached during a `checkout` operation.
@@ -1313,12 +1382,14 @@ class LoroDoc {
1313
1382
  }
1314
1383
  }
1315
1384
  /**
1316
- * Commit the cumulative auto committed transaction.
1385
+ * Commit the cumulative auto-committed transaction.
1317
1386
  *
1318
1387
  * You can specify the `origin`, `timestamp`, and `message` of the commit.
1319
1388
  *
1320
1389
  * - The `origin` is used to mark the event
1321
1390
  * - The `message` works like a git commit message, which will be recorded and synced to peers
1391
+ * - The `timestamp` is the number of seconds that have elapsed since 00:00:00 UTC on January 1, 1970.
1392
+ * It defaults to `Date.now() / 1000` when timestamp recording is enabled
1322
1393
  *
1323
1394
  * The events will be emitted after a transaction is committed. A transaction is committed when:
1324
1395
  *
@@ -1895,12 +1966,32 @@ class LoroDoc {
1895
1966
  * Export updates in the given range in JSON format.
1896
1967
  * @param {any} start_vv
1897
1968
  * @param {any} end_vv
1969
+ * @param {boolean | undefined} [with_peer_compression]
1898
1970
  * @returns {JsonSchema}
1899
1971
  */
1900
- exportJsonUpdates(start_vv, end_vv) {
1972
+ exportJsonUpdates(start_vv, end_vv, with_peer_compression) {
1901
1973
  try {
1902
1974
  const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1903
- wasm.lorodoc_exportJsonUpdates(retptr, this.__wbg_ptr, addHeapObject(start_vv), addHeapObject(end_vv));
1975
+ wasm.lorodoc_exportJsonUpdates(retptr, this.__wbg_ptr, addHeapObject(start_vv), addHeapObject(end_vv), isLikeNone(with_peer_compression) ? 0xFFFFFF : with_peer_compression ? 1 : 0);
1976
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
1977
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
1978
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
1979
+ if (r2) {
1980
+ throw takeObject(r1);
1981
+ }
1982
+ return takeObject(r0);
1983
+ } finally {
1984
+ wasm.__wbindgen_add_to_stack_pointer(16);
1985
+ }
1986
+ }
1987
+ /**
1988
+ * @param {{ peer: PeerID, counter: number, length: number }} idSpan
1989
+ * @returns {any}
1990
+ */
1991
+ exportJsonInIdSpan(idSpan) {
1992
+ try {
1993
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1994
+ wasm.lorodoc_exportJsonInIdSpan(retptr, this.__wbg_ptr, addHeapObject(idSpan));
1904
1995
  var r0 = getInt32Memory0()[retptr / 4 + 0];
1905
1996
  var r1 = getInt32Memory0()[retptr / 4 + 1];
1906
1997
  var r2 = getInt32Memory0()[retptr / 4 + 2];
@@ -2099,7 +2190,7 @@ class LoroDoc {
2099
2190
  *
2100
2191
  * @example
2101
2192
  * ```ts
2102
- * import { LoroDoc, LoroText } from "loro-crdt";
2193
+ * import { LoroDoc, LoroText, LoroMap } from "loro-crdt";
2103
2194
  *
2104
2195
  * const doc = new LoroDoc();
2105
2196
  * const list = doc.getList("list");
@@ -2463,6 +2554,84 @@ class LoroDoc {
2463
2554
  wasm.__wbindgen_add_to_stack_pointer(16);
2464
2555
  }
2465
2556
  }
2557
+ /**
2558
+ * Revert the document to the given frontiers.
2559
+ *
2560
+ * The doc will not become detached when using this method. Instead, it will generate a series
2561
+ * of operations to revert the document to the given version.
2562
+ *
2563
+ * @example
2564
+ * ```ts
2565
+ * const doc = new LoroDoc();
2566
+ * doc.setPeerId("1");
2567
+ * const text = doc.getText("text");
2568
+ * text.insert(0, "Hello");
2569
+ * doc.commit();
2570
+ * doc.revertTo([{ peer: "1", counter: 1 }]);
2571
+ * expect(doc.getText("text").toString()).toBe("He");
2572
+ * ```
2573
+ * @param {({ peer: PeerID, counter: number })[]} frontiers
2574
+ */
2575
+ revertTo(frontiers) {
2576
+ try {
2577
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
2578
+ const ptr0 = passArrayJsValueToWasm0(frontiers, wasm.__wbindgen_export_0);
2579
+ const len0 = WASM_VECTOR_LEN;
2580
+ wasm.lorodoc_revertTo(retptr, this.__wbg_ptr, ptr0, len0);
2581
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
2582
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
2583
+ if (r1) {
2584
+ throw takeObject(r0);
2585
+ }
2586
+ } finally {
2587
+ wasm.__wbindgen_add_to_stack_pointer(16);
2588
+ }
2589
+ }
2590
+ /**
2591
+ * Apply a diff batch to the document
2592
+ * @param {Record<ContainerID, Diff>} diff
2593
+ */
2594
+ applyDiff(diff) {
2595
+ try {
2596
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
2597
+ wasm.lorodoc_applyDiff(retptr, this.__wbg_ptr, addHeapObject(diff));
2598
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
2599
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
2600
+ if (r1) {
2601
+ throw takeObject(r0);
2602
+ }
2603
+ } finally {
2604
+ wasm.__wbindgen_add_to_stack_pointer(16);
2605
+ }
2606
+ }
2607
+ /**
2608
+ * Calculate the differences between two frontiers
2609
+ *
2610
+ * The entries in the returned object are sorted by causal order: the creation of a child container will be
2611
+ * presented before its use.
2612
+ * @param {({ peer: PeerID, counter: number })[]} from
2613
+ * @param {({ peer: PeerID, counter: number })[]} to
2614
+ * @returns {Record<ContainerID, Diff>}
2615
+ */
2616
+ diff(from, to) {
2617
+ try {
2618
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
2619
+ const ptr0 = passArrayJsValueToWasm0(from, wasm.__wbindgen_export_0);
2620
+ const len0 = WASM_VECTOR_LEN;
2621
+ const ptr1 = passArrayJsValueToWasm0(to, wasm.__wbindgen_export_0);
2622
+ const len1 = WASM_VECTOR_LEN;
2623
+ wasm.lorodoc_diff(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
2624
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
2625
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
2626
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
2627
+ if (r2) {
2628
+ throw takeObject(r1);
2629
+ }
2630
+ return takeObject(r0);
2631
+ } finally {
2632
+ wasm.__wbindgen_add_to_stack_pointer(16);
2633
+ }
2634
+ }
2466
2635
  }
2467
2636
  module.exports.LoroDoc = LoroDoc;
2468
2637
 
@@ -3362,7 +3531,7 @@ class LoroMap {
3362
3531
  *
3363
3532
  * @example
3364
3533
  * ```ts
3365
- * import { LoroDoc } from "loro-crdt";
3534
+ * import { LoroDoc, LoroText } from "loro-crdt";
3366
3535
  *
3367
3536
  * const doc = new LoroDoc();
3368
3537
  * doc.setPeerId("1");
@@ -5180,26 +5349,6 @@ class LoroTreeNode {
5180
5349
  }
5181
5350
  }
5182
5351
  /**
5183
- * Move this tree node to be a child of the parent.
5184
- * If the parent is undefined, this node will be a root node.
5185
- *
5186
- * If the index is not provided, the node will be appended to the end.
5187
- *
5188
- * It's not allowed that the target is an ancestor of the parent.
5189
- *
5190
- * @example
5191
- * ```ts
5192
- * const doc = new LoroDoc();
5193
- * const tree = doc.getTree("tree");
5194
- * const root = tree.createNode();
5195
- * const node = root.createNode();
5196
- * const node2 = node.createNode();
5197
- * node2.move(undefined, 0);
5198
- * // node2 root
5199
- * // |
5200
- * // node
5201
- *
5202
- * ```
5203
5352
  * @param {LoroTreeNode | undefined} parent
5204
5353
  * @param {number | undefined} [index]
5205
5354
  */
@@ -5362,11 +5511,6 @@ class LoroTreeNode {
5362
5511
  }
5363
5512
  }
5364
5513
  /**
5365
- * Get the parent node of this node.
5366
- *
5367
- * - The parent of the root node is `undefined`.
5368
- * - The object returned is a new js object each time because it need to cross
5369
- * the WASM boundary.
5370
5514
  * @returns {LoroTreeNode | undefined}
5371
5515
  */
5372
5516
  parent() {
@@ -5565,6 +5709,7 @@ class UndoManager {
5565
5709
  }
5566
5710
  /**
5567
5711
  * Set the merge interval (in ms).
5712
+ *
5568
5713
  * If the interval is set to 0, the undo steps will not be merged.
5569
5714
  * Otherwise, the undo steps will be merged if the interval between the two steps is less than the given interval.
5570
5715
  * @param {number} interval
@@ -5840,28 +5985,28 @@ module.exports.__wbindgen_object_drop_ref = function(arg0) {
5840
5985
  takeObject(arg0);
5841
5986
  };
5842
5987
 
5843
- module.exports.__wbg_loromap_new = function(arg0) {
5844
- const ret = LoroMap.__wrap(arg0);
5988
+ module.exports.__wbg_lorolist_new = function(arg0) {
5989
+ const ret = LoroList.__wrap(arg0);
5845
5990
  return addHeapObject(ret);
5846
5991
  };
5847
5992
 
5848
- module.exports.__wbg_lorotext_new = function(arg0) {
5849
- const ret = LoroText.__wrap(arg0);
5993
+ module.exports.__wbg_lorocounter_new = function(arg0) {
5994
+ const ret = LoroCounter.__wrap(arg0);
5850
5995
  return addHeapObject(ret);
5851
5996
  };
5852
5997
 
5853
- module.exports.__wbg_lorolist_new = function(arg0) {
5854
- const ret = LoroList.__wrap(arg0);
5998
+ module.exports.__wbg_loromap_new = function(arg0) {
5999
+ const ret = LoroMap.__wrap(arg0);
5855
6000
  return addHeapObject(ret);
5856
6001
  };
5857
6002
 
5858
- module.exports.__wbg_lorotree_new = function(arg0) {
5859
- const ret = LoroTree.__wrap(arg0);
6003
+ module.exports.__wbg_lorotreenode_new = function(arg0) {
6004
+ const ret = LoroTreeNode.__wrap(arg0);
5860
6005
  return addHeapObject(ret);
5861
6006
  };
5862
6007
 
5863
- module.exports.__wbg_cursor_new = function(arg0) {
5864
- const ret = Cursor.__wrap(arg0);
6008
+ module.exports.__wbg_lorotree_new = function(arg0) {
6009
+ const ret = LoroTree.__wrap(arg0);
5865
6010
  return addHeapObject(ret);
5866
6011
  };
5867
6012
 
@@ -5870,13 +6015,13 @@ module.exports.__wbg_loromovablelist_new = function(arg0) {
5870
6015
  return addHeapObject(ret);
5871
6016
  };
5872
6017
 
5873
- module.exports.__wbg_lorocounter_new = function(arg0) {
5874
- const ret = LoroCounter.__wrap(arg0);
6018
+ module.exports.__wbg_cursor_new = function(arg0) {
6019
+ const ret = Cursor.__wrap(arg0);
5875
6020
  return addHeapObject(ret);
5876
6021
  };
5877
6022
 
5878
- module.exports.__wbg_lorotreenode_new = function(arg0) {
5879
- const ret = LoroTreeNode.__wrap(arg0);
6023
+ module.exports.__wbg_lorotext_new = function(arg0) {
6024
+ const ret = LoroText.__wrap(arg0);
5880
6025
  return addHeapObject(ret);
5881
6026
  };
5882
6027
 
@@ -5993,6 +6138,11 @@ module.exports.__wbindgen_number_new = function(arg0) {
5993
6138
  return addHeapObject(ret);
5994
6139
  };
5995
6140
 
6141
+ module.exports.__wbindgen_is_array = function(arg0) {
6142
+ const ret = Array.isArray(getObject(arg0));
6143
+ return ret;
6144
+ };
6145
+
5996
6146
  module.exports.__wbindgen_typeof = function(arg0) {
5997
6147
  const ret = typeof getObject(arg0);
5998
6148
  return addHeapObject(ret);
@@ -6103,7 +6253,7 @@ module.exports.__wbg_error_f851667af71bcfc6 = function(arg0, arg1) {
6103
6253
  }
6104
6254
  };
6105
6255
 
6106
- module.exports.__wbg_now_11683c634f92ae89 = typeof Date.now == 'function' ? Date.now : notDefined('Date.now');
6256
+ module.exports.__wbg_now_15d99db1ebc1f0b2 = typeof Date.now == 'function' ? Date.now : notDefined('Date.now');
6107
6257
 
6108
6258
  module.exports.__wbg_crypto_1d1f22824a6a080c = function(arg0) {
6109
6259
  const ret = getObject(arg0).crypto;
@@ -6237,6 +6387,11 @@ module.exports.__wbg_set_d4638f722068f043 = function(arg0, arg1, arg2) {
6237
6387
  getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
6238
6388
  };
6239
6389
 
6390
+ module.exports.__wbg_from_89e3fc3ba5e6fb48 = function(arg0) {
6391
+ const ret = Array.from(getObject(arg0));
6392
+ return addHeapObject(ret);
6393
+ };
6394
+
6240
6395
  module.exports.__wbg_isArray_2ab64d95e09ea0ae = function(arg0) {
6241
6396
  const ret = Array.isArray(getObject(arg0));
6242
6397
  return ret;
@@ -6422,13 +6577,13 @@ module.exports.__wbindgen_memory = function() {
6422
6577
  return addHeapObject(ret);
6423
6578
  };
6424
6579
 
6425
- module.exports.__wbindgen_closure_wrapper487 = function(arg0, arg1, arg2) {
6426
- const ret = makeMutClosure(arg0, arg1, 9, __wbg_adapter_58);
6580
+ module.exports.__wbindgen_closure_wrapper491 = function(arg0, arg1, arg2) {
6581
+ const ret = makeMutClosure(arg0, arg1, 9, __wbg_adapter_60);
6427
6582
  return addHeapObject(ret);
6428
6583
  };
6429
6584
 
6430
- module.exports.__wbindgen_closure_wrapper489 = function(arg0, arg1, arg2) {
6431
- const ret = makeMutClosure(arg0, arg1, 11, __wbg_adapter_61);
6585
+ module.exports.__wbindgen_closure_wrapper494 = function(arg0, arg1, arg2) {
6586
+ const ret = makeMutClosure(arg0, arg1, 11, __wbg_adapter_63);
6432
6587
  return addHeapObject(ret);
6433
6588
  };
6434
6589
 
Binary file
@@ -44,6 +44,7 @@ export function lorodoc_fork(a: number): number;
44
44
  export function lorodoc_forkAt(a: number, b: number, c: number, d: number): void;
45
45
  export function lorodoc_checkoutToLatest(a: number, b: number): void;
46
46
  export function lorodoc_travelChangeAncestors(a: number, b: number, c: number, d: number, e: number): void;
47
+ export function lorodoc_findIdSpansBetween(a: number, b: number, c: number, d: number, e: number, f: number): void;
47
48
  export function lorodoc_checkout(a: number, b: number, c: number, d: number): void;
48
49
  export function lorodoc_peerId(a: number): number;
49
50
  export function lorodoc_peerIdStr(a: number): number;
@@ -73,7 +74,8 @@ export function lorodoc_cmpFrontiers(a: number, b: number, c: number, d: number,
73
74
  export function lorodoc_exportSnapshot(a: number, b: number): void;
74
75
  export function lorodoc_exportFrom(a: number, b: number, c: number): void;
75
76
  export function lorodoc_export(a: number, b: number, c: number): void;
76
- export function lorodoc_exportJsonUpdates(a: number, b: number, c: number, d: number): void;
77
+ export function lorodoc_exportJsonUpdates(a: number, b: number, c: number, d: number, e: number): void;
78
+ export function lorodoc_exportJsonInIdSpan(a: number, b: number, c: number): void;
77
79
  export function lorodoc_importJsonUpdates(a: number, b: number, c: number): void;
78
80
  export function lorodoc_import(a: number, b: number, c: number, d: number): void;
79
81
  export function lorodoc_importUpdateBatch(a: number, b: number, c: number): void;
@@ -94,6 +96,9 @@ export function lorodoc_vvToFrontiers(a: number, b: number, c: number): void;
94
96
  export function lorodoc_getByPath(a: number, b: number, c: number): number;
95
97
  export function lorodoc_getCursorPos(a: number, b: number, c: number): void;
96
98
  export function lorodoc_getChangedContainersIn(a: number, b: number, c: number, d: number): void;
99
+ export function lorodoc_revertTo(a: number, b: number, c: number, d: number): void;
100
+ export function lorodoc_applyDiff(a: number, b: number, c: number): void;
101
+ export function lorodoc_diff(a: number, b: number, c: number, d: number, e: number, f: number): void;
97
102
  export function __wbg_lorotext_free(a: number): void;
98
103
  export function lorotext_new(): number;
99
104
  export function lorotext_kind(a: number): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loro-crdt",
3
- "version": "1.2.6",
3
+ "version": "1.3.0",
4
4
  "description": "Loro CRDTs is a high-performance CRDT framework that makes your app state synchronized, collaborative and maintainable effortlessly.",
5
5
  "keywords": [
6
6
  "crdt",