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.
package/web/loro_wasm.js CHANGED
@@ -229,11 +229,11 @@ function makeMutClosure(arg0, arg1, dtor, f) {
229
229
  CLOSURE_DTORS.register(real, state, state);
230
230
  return real;
231
231
  }
232
- function __wbg_adapter_58(arg0, arg1, arg2) {
232
+ function __wbg_adapter_60(arg0, arg1, arg2) {
233
233
  wasm.__wbindgen_export_3(arg0, arg1, addHeapObject(arg2));
234
234
  }
235
235
 
236
- function __wbg_adapter_61(arg0, arg1) {
236
+ function __wbg_adapter_63(arg0, arg1) {
237
237
  wasm.__wbindgen_export_4(arg0, arg1);
238
238
  }
239
239
 
@@ -963,9 +963,12 @@ export class LoroDoc {
963
963
  wasm.lorodoc_setRecordTimestamp(this.__wbg_ptr, auto_record);
964
964
  }
965
965
  /**
966
- * If two continuous local changes are within the interval, they will be merged into one change.
966
+ * If two continuous local changes are within (<=) the interval(**in seconds**), they will be merged into one change.
967
967
  *
968
- * The default value is 1_000_000, the default unit is seconds.
968
+ * The default value is 1_000 seconds.
969
+ *
970
+ * By default, we record timestamps in seconds for each change. So if the merge interval is 1, and changes A and B
971
+ * have timestamps of 3 and 4 respectively, then they will be merged into one change
969
972
  * @param {number} interval
970
973
  */
971
974
  setChangeMergeInterval(interval) {
@@ -1229,6 +1232,72 @@ export class LoroDoc {
1229
1232
  }
1230
1233
  }
1231
1234
  /**
1235
+ * Find the op id spans that between the `from` version and the `to` version.
1236
+ *
1237
+ * You can combine it with `exportJsonInIdSpan` to get the changes between two versions.
1238
+ *
1239
+ * You can use it to travel all the changes from `from` to `to`. `from` and `to` are frontiers,
1240
+ * and they can be concurrent to each other. You can use it to find all the changes related to an event:
1241
+ *
1242
+ * @example
1243
+ * ```ts
1244
+ * import { LoroDoc } from "loro-crdt";
1245
+ *
1246
+ * const docA = new LoroDoc();
1247
+ * docA.setPeerId("1");
1248
+ * const docB = new LoroDoc();
1249
+ *
1250
+ * docA.getText("text").update("Hello");
1251
+ * docA.commit();
1252
+ * const snapshot = docA.export({ mode: "snapshot" });
1253
+ * let done = false;
1254
+ * docB.subscribe(e => {
1255
+ * const spans = docB.findIdSpansBetween(e.from, e.to);
1256
+ * const changes = docB.exportJsonInIdSpan(spans.forward[0]);
1257
+ * console.log(changes);
1258
+ * // [{
1259
+ * // id: "0@1",
1260
+ * // timestamp: expect.any(Number),
1261
+ * // deps: [],
1262
+ * // lamport: 0,
1263
+ * // msg: undefined,
1264
+ * // ops: [{
1265
+ * // container: "cid:root-text:Text",
1266
+ * // counter: 0,
1267
+ * // content: {
1268
+ * // type: "insert",
1269
+ * // pos: 0,
1270
+ * // text: "Hello"
1271
+ * // }
1272
+ * // }]
1273
+ * // }]
1274
+ * });
1275
+ * docB.import(snapshot);
1276
+ * ```
1277
+ * @param {({ peer: PeerID, counter: number })[]} from
1278
+ * @param {({ peer: PeerID, counter: number })[]} to
1279
+ * @returns {VersionVectorDiff}
1280
+ */
1281
+ findIdSpansBetween(from, to) {
1282
+ try {
1283
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1284
+ const ptr0 = passArrayJsValueToWasm0(from, wasm.__wbindgen_export_0);
1285
+ const len0 = WASM_VECTOR_LEN;
1286
+ const ptr1 = passArrayJsValueToWasm0(to, wasm.__wbindgen_export_0);
1287
+ const len1 = WASM_VECTOR_LEN;
1288
+ wasm.lorodoc_findIdSpansBetween(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
1289
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
1290
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
1291
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
1292
+ if (r2) {
1293
+ throw takeObject(r1);
1294
+ }
1295
+ return takeObject(r0);
1296
+ } finally {
1297
+ wasm.__wbindgen_add_to_stack_pointer(16);
1298
+ }
1299
+ }
1300
+ /**
1232
1301
  * Checkout the `DocState` to a specific version.
1233
1302
  *
1234
1303
  * > The document becomes detached during a `checkout` operation.
@@ -1307,12 +1376,14 @@ export class LoroDoc {
1307
1376
  }
1308
1377
  }
1309
1378
  /**
1310
- * Commit the cumulative auto committed transaction.
1379
+ * Commit the cumulative auto-committed transaction.
1311
1380
  *
1312
1381
  * You can specify the `origin`, `timestamp`, and `message` of the commit.
1313
1382
  *
1314
1383
  * - The `origin` is used to mark the event
1315
1384
  * - The `message` works like a git commit message, which will be recorded and synced to peers
1385
+ * - The `timestamp` is the number of seconds that have elapsed since 00:00:00 UTC on January 1, 1970.
1386
+ * It defaults to `Date.now() / 1000` when timestamp recording is enabled
1316
1387
  *
1317
1388
  * The events will be emitted after a transaction is committed. A transaction is committed when:
1318
1389
  *
@@ -1889,12 +1960,32 @@ export class LoroDoc {
1889
1960
  * Export updates in the given range in JSON format.
1890
1961
  * @param {any} start_vv
1891
1962
  * @param {any} end_vv
1963
+ * @param {boolean | undefined} [with_peer_compression]
1892
1964
  * @returns {JsonSchema}
1893
1965
  */
1894
- exportJsonUpdates(start_vv, end_vv) {
1966
+ exportJsonUpdates(start_vv, end_vv, with_peer_compression) {
1967
+ try {
1968
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1969
+ wasm.lorodoc_exportJsonUpdates(retptr, this.__wbg_ptr, addHeapObject(start_vv), addHeapObject(end_vv), isLikeNone(with_peer_compression) ? 0xFFFFFF : with_peer_compression ? 1 : 0);
1970
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
1971
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
1972
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
1973
+ if (r2) {
1974
+ throw takeObject(r1);
1975
+ }
1976
+ return takeObject(r0);
1977
+ } finally {
1978
+ wasm.__wbindgen_add_to_stack_pointer(16);
1979
+ }
1980
+ }
1981
+ /**
1982
+ * @param {{ peer: PeerID, counter: number, length: number }} idSpan
1983
+ * @returns {any}
1984
+ */
1985
+ exportJsonInIdSpan(idSpan) {
1895
1986
  try {
1896
1987
  const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
1897
- wasm.lorodoc_exportJsonUpdates(retptr, this.__wbg_ptr, addHeapObject(start_vv), addHeapObject(end_vv));
1988
+ wasm.lorodoc_exportJsonInIdSpan(retptr, this.__wbg_ptr, addHeapObject(idSpan));
1898
1989
  var r0 = getInt32Memory0()[retptr / 4 + 0];
1899
1990
  var r1 = getInt32Memory0()[retptr / 4 + 1];
1900
1991
  var r2 = getInt32Memory0()[retptr / 4 + 2];
@@ -2093,7 +2184,7 @@ export class LoroDoc {
2093
2184
  *
2094
2185
  * @example
2095
2186
  * ```ts
2096
- * import { LoroDoc, LoroText } from "loro-crdt";
2187
+ * import { LoroDoc, LoroText, LoroMap } from "loro-crdt";
2097
2188
  *
2098
2189
  * const doc = new LoroDoc();
2099
2190
  * const list = doc.getList("list");
@@ -2457,6 +2548,84 @@ export class LoroDoc {
2457
2548
  wasm.__wbindgen_add_to_stack_pointer(16);
2458
2549
  }
2459
2550
  }
2551
+ /**
2552
+ * Revert the document to the given frontiers.
2553
+ *
2554
+ * The doc will not become detached when using this method. Instead, it will generate a series
2555
+ * of operations to revert the document to the given version.
2556
+ *
2557
+ * @example
2558
+ * ```ts
2559
+ * const doc = new LoroDoc();
2560
+ * doc.setPeerId("1");
2561
+ * const text = doc.getText("text");
2562
+ * text.insert(0, "Hello");
2563
+ * doc.commit();
2564
+ * doc.revertTo([{ peer: "1", counter: 1 }]);
2565
+ * expect(doc.getText("text").toString()).toBe("He");
2566
+ * ```
2567
+ * @param {({ peer: PeerID, counter: number })[]} frontiers
2568
+ */
2569
+ revertTo(frontiers) {
2570
+ try {
2571
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
2572
+ const ptr0 = passArrayJsValueToWasm0(frontiers, wasm.__wbindgen_export_0);
2573
+ const len0 = WASM_VECTOR_LEN;
2574
+ wasm.lorodoc_revertTo(retptr, this.__wbg_ptr, ptr0, len0);
2575
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
2576
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
2577
+ if (r1) {
2578
+ throw takeObject(r0);
2579
+ }
2580
+ } finally {
2581
+ wasm.__wbindgen_add_to_stack_pointer(16);
2582
+ }
2583
+ }
2584
+ /**
2585
+ * Apply a diff batch to the document
2586
+ * @param {Record<ContainerID, Diff>} diff
2587
+ */
2588
+ applyDiff(diff) {
2589
+ try {
2590
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
2591
+ wasm.lorodoc_applyDiff(retptr, this.__wbg_ptr, addHeapObject(diff));
2592
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
2593
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
2594
+ if (r1) {
2595
+ throw takeObject(r0);
2596
+ }
2597
+ } finally {
2598
+ wasm.__wbindgen_add_to_stack_pointer(16);
2599
+ }
2600
+ }
2601
+ /**
2602
+ * Calculate the differences between two frontiers
2603
+ *
2604
+ * The entries in the returned object are sorted by causal order: the creation of a child container will be
2605
+ * presented before its use.
2606
+ * @param {({ peer: PeerID, counter: number })[]} from
2607
+ * @param {({ peer: PeerID, counter: number })[]} to
2608
+ * @returns {Record<ContainerID, Diff>}
2609
+ */
2610
+ diff(from, to) {
2611
+ try {
2612
+ const retptr = wasm.__wbindgen_add_to_stack_pointer(-16);
2613
+ const ptr0 = passArrayJsValueToWasm0(from, wasm.__wbindgen_export_0);
2614
+ const len0 = WASM_VECTOR_LEN;
2615
+ const ptr1 = passArrayJsValueToWasm0(to, wasm.__wbindgen_export_0);
2616
+ const len1 = WASM_VECTOR_LEN;
2617
+ wasm.lorodoc_diff(retptr, this.__wbg_ptr, ptr0, len0, ptr1, len1);
2618
+ var r0 = getInt32Memory0()[retptr / 4 + 0];
2619
+ var r1 = getInt32Memory0()[retptr / 4 + 1];
2620
+ var r2 = getInt32Memory0()[retptr / 4 + 2];
2621
+ if (r2) {
2622
+ throw takeObject(r1);
2623
+ }
2624
+ return takeObject(r0);
2625
+ } finally {
2626
+ wasm.__wbindgen_add_to_stack_pointer(16);
2627
+ }
2628
+ }
2460
2629
  }
2461
2630
 
2462
2631
  const LoroListFinalization = (typeof FinalizationRegistry === 'undefined')
@@ -3354,7 +3523,7 @@ export class LoroMap {
3354
3523
  *
3355
3524
  * @example
3356
3525
  * ```ts
3357
- * import { LoroDoc } from "loro-crdt";
3526
+ * import { LoroDoc, LoroText } from "loro-crdt";
3358
3527
  *
3359
3528
  * const doc = new LoroDoc();
3360
3529
  * doc.setPeerId("1");
@@ -5168,26 +5337,6 @@ export class LoroTreeNode {
5168
5337
  }
5169
5338
  }
5170
5339
  /**
5171
- * Move this tree node to be a child of the parent.
5172
- * If the parent is undefined, this node will be a root node.
5173
- *
5174
- * If the index is not provided, the node will be appended to the end.
5175
- *
5176
- * It's not allowed that the target is an ancestor of the parent.
5177
- *
5178
- * @example
5179
- * ```ts
5180
- * const doc = new LoroDoc();
5181
- * const tree = doc.getTree("tree");
5182
- * const root = tree.createNode();
5183
- * const node = root.createNode();
5184
- * const node2 = node.createNode();
5185
- * node2.move(undefined, 0);
5186
- * // node2 root
5187
- * // |
5188
- * // node
5189
- *
5190
- * ```
5191
5340
  * @param {LoroTreeNode | undefined} parent
5192
5341
  * @param {number | undefined} [index]
5193
5342
  */
@@ -5350,11 +5499,6 @@ export class LoroTreeNode {
5350
5499
  }
5351
5500
  }
5352
5501
  /**
5353
- * Get the parent node of this node.
5354
- *
5355
- * - The parent of the root node is `undefined`.
5356
- * - The object returned is a new js object each time because it need to cross
5357
- * the WASM boundary.
5358
5502
  * @returns {LoroTreeNode | undefined}
5359
5503
  */
5360
5504
  parent() {
@@ -5552,6 +5696,7 @@ export class UndoManager {
5552
5696
  }
5553
5697
  /**
5554
5698
  * Set the merge interval (in ms).
5699
+ *
5555
5700
  * If the interval is set to 0, the undo steps will not be merged.
5556
5701
  * Otherwise, the undo steps will be merged if the interval between the two steps is less than the given interval.
5557
5702
  * @param {number} interval
@@ -5858,36 +6003,36 @@ function __wbg_get_imports() {
5858
6003
  imports.wbg.__wbindgen_object_drop_ref = function(arg0) {
5859
6004
  takeObject(arg0);
5860
6005
  };
5861
- imports.wbg.__wbg_loromap_new = function(arg0) {
5862
- const ret = LoroMap.__wrap(arg0);
6006
+ imports.wbg.__wbg_lorolist_new = function(arg0) {
6007
+ const ret = LoroList.__wrap(arg0);
5863
6008
  return addHeapObject(ret);
5864
6009
  };
5865
- imports.wbg.__wbg_lorotext_new = function(arg0) {
5866
- const ret = LoroText.__wrap(arg0);
6010
+ imports.wbg.__wbg_lorocounter_new = function(arg0) {
6011
+ const ret = LoroCounter.__wrap(arg0);
5867
6012
  return addHeapObject(ret);
5868
6013
  };
5869
- imports.wbg.__wbg_lorolist_new = function(arg0) {
5870
- const ret = LoroList.__wrap(arg0);
6014
+ imports.wbg.__wbg_loromap_new = function(arg0) {
6015
+ const ret = LoroMap.__wrap(arg0);
5871
6016
  return addHeapObject(ret);
5872
6017
  };
5873
- imports.wbg.__wbg_lorotree_new = function(arg0) {
5874
- const ret = LoroTree.__wrap(arg0);
6018
+ imports.wbg.__wbg_lorotreenode_new = function(arg0) {
6019
+ const ret = LoroTreeNode.__wrap(arg0);
5875
6020
  return addHeapObject(ret);
5876
6021
  };
5877
- imports.wbg.__wbg_cursor_new = function(arg0) {
5878
- const ret = Cursor.__wrap(arg0);
6022
+ imports.wbg.__wbg_lorotree_new = function(arg0) {
6023
+ const ret = LoroTree.__wrap(arg0);
5879
6024
  return addHeapObject(ret);
5880
6025
  };
5881
6026
  imports.wbg.__wbg_loromovablelist_new = function(arg0) {
5882
6027
  const ret = LoroMovableList.__wrap(arg0);
5883
6028
  return addHeapObject(ret);
5884
6029
  };
5885
- imports.wbg.__wbg_lorocounter_new = function(arg0) {
5886
- const ret = LoroCounter.__wrap(arg0);
6030
+ imports.wbg.__wbg_cursor_new = function(arg0) {
6031
+ const ret = Cursor.__wrap(arg0);
5887
6032
  return addHeapObject(ret);
5888
6033
  };
5889
- imports.wbg.__wbg_lorotreenode_new = function(arg0) {
5890
- const ret = LoroTreeNode.__wrap(arg0);
6034
+ imports.wbg.__wbg_lorotext_new = function(arg0) {
6035
+ const ret = LoroText.__wrap(arg0);
5891
6036
  return addHeapObject(ret);
5892
6037
  };
5893
6038
  imports.wbg.__wbg_versionvector_new = function(arg0) {
@@ -5983,6 +6128,10 @@ function __wbg_get_imports() {
5983
6128
  const ret = arg0;
5984
6129
  return addHeapObject(ret);
5985
6130
  };
6131
+ imports.wbg.__wbindgen_is_array = function(arg0) {
6132
+ const ret = Array.isArray(getObject(arg0));
6133
+ return ret;
6134
+ };
5986
6135
  imports.wbg.__wbindgen_typeof = function(arg0) {
5987
6136
  const ret = typeof getObject(arg0);
5988
6137
  return addHeapObject(ret);
@@ -6078,7 +6227,7 @@ function __wbg_get_imports() {
6078
6227
  wasm.__wbindgen_export_5(deferred0_0, deferred0_1, 1);
6079
6228
  }
6080
6229
  };
6081
- imports.wbg.__wbg_now_11683c634f92ae89 = typeof Date.now == 'function' ? Date.now : notDefined('Date.now');
6230
+ imports.wbg.__wbg_now_15d99db1ebc1f0b2 = typeof Date.now == 'function' ? Date.now : notDefined('Date.now');
6082
6231
  imports.wbg.__wbg_crypto_1d1f22824a6a080c = function(arg0) {
6083
6232
  const ret = getObject(arg0).crypto;
6084
6233
  return addHeapObject(ret);
@@ -6184,6 +6333,10 @@ function __wbg_get_imports() {
6184
6333
  imports.wbg.__wbg_set_d4638f722068f043 = function(arg0, arg1, arg2) {
6185
6334
  getObject(arg0)[arg1 >>> 0] = takeObject(arg2);
6186
6335
  };
6336
+ imports.wbg.__wbg_from_89e3fc3ba5e6fb48 = function(arg0) {
6337
+ const ret = Array.from(getObject(arg0));
6338
+ return addHeapObject(ret);
6339
+ };
6187
6340
  imports.wbg.__wbg_isArray_2ab64d95e09ea0ae = function(arg0) {
6188
6341
  const ret = Array.isArray(getObject(arg0));
6189
6342
  return ret;
@@ -6337,12 +6490,12 @@ function __wbg_get_imports() {
6337
6490
  const ret = wasm.memory;
6338
6491
  return addHeapObject(ret);
6339
6492
  };
6340
- imports.wbg.__wbindgen_closure_wrapper487 = function(arg0, arg1, arg2) {
6341
- const ret = makeMutClosure(arg0, arg1, 9, __wbg_adapter_58);
6493
+ imports.wbg.__wbindgen_closure_wrapper491 = function(arg0, arg1, arg2) {
6494
+ const ret = makeMutClosure(arg0, arg1, 9, __wbg_adapter_60);
6342
6495
  return addHeapObject(ret);
6343
6496
  };
6344
- imports.wbg.__wbindgen_closure_wrapper489 = function(arg0, arg1, arg2) {
6345
- const ret = makeMutClosure(arg0, arg1, 11, __wbg_adapter_61);
6497
+ imports.wbg.__wbindgen_closure_wrapper494 = function(arg0, arg1, arg2) {
6498
+ const ret = makeMutClosure(arg0, arg1, 11, __wbg_adapter_63);
6346
6499
  return addHeapObject(ret);
6347
6500
  };
6348
6501
 
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;