loro-crdt 1.4.5 → 1.5.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.
@@ -27,6 +27,20 @@ export function awarenesswasm_removeOutdated(a: number, b: number): void;
27
27
  export function awarenesswasm_length(a: number): number;
28
28
  export function awarenesswasm_isEmpty(a: number): number;
29
29
  export function awarenesswasm_peers(a: number, b: number): void;
30
+ export function __wbg_ephemeralstorewasm_free(a: number): void;
31
+ export function ephemeralstorewasm_new(a: number): number;
32
+ export function ephemeralstorewasm_set(a: number, b: number, c: number, d: number): void;
33
+ export function ephemeralstorewasm_delete(a: number, b: number, c: number): void;
34
+ export function ephemeralstorewasm_get(a: number, b: number, c: number): number;
35
+ export function ephemeralstorewasm_getAllStates(a: number): number;
36
+ export function ephemeralstorewasm_subscribeLocalUpdates(a: number, b: number): number;
37
+ export function ephemeralstorewasm_subscribe(a: number, b: number): number;
38
+ export function ephemeralstorewasm_encode(a: number, b: number, c: number, d: number): void;
39
+ export function ephemeralstorewasm_encodeAll(a: number, b: number): void;
40
+ export function ephemeralstorewasm_apply(a: number, b: number, c: number): void;
41
+ export function ephemeralstorewasm_removeOutdated(a: number): void;
42
+ export function ephemeralstorewasm_isEmpty(a: number): number;
43
+ export function ephemeralstorewasm_keys(a: number, b: number): void;
30
44
  export function LORO_VERSION(a: number): void;
31
45
  export function run(): void;
32
46
  export function encodeFrontiers(a: number, b: number, c: number): void;
@@ -107,6 +121,9 @@ export function lorodoc_getChangedContainersIn(a: number, b: number, c: number,
107
121
  export function lorodoc_revertTo(a: number, b: number, c: number, d: number): void;
108
122
  export function lorodoc_applyDiff(a: number, b: number, c: number): void;
109
123
  export function lorodoc_diff(a: number, b: number, c: number, d: number, e: number, f: number, g: number): void;
124
+ export function lorodoc_getUncommittedOpsAsJson(a: number, b: number): void;
125
+ export function lorodoc_subscribeFirstCommitFromPeer(a: number, b: number): number;
126
+ export function lorodoc_subscribePreCommit(a: number, b: number): number;
110
127
  export function __wbg_lorotext_free(a: number): void;
111
128
  export function lorotext_new(): number;
112
129
  export function lorotext_kind(a: number): number;
@@ -278,6 +295,9 @@ export function versionvector_setLast(a: number, b: number, c: number): void;
278
295
  export function versionvector_remove(a: number, b: number, c: number): void;
279
296
  export function versionvector_length(a: number): number;
280
297
  export function decodeImportBlobMeta(a: number, b: number, c: number, d: number): void;
298
+ export function __wbg_changemodifier_free(a: number): void;
299
+ export function changemodifier_setMessage(a: number, b: number, c: number): number;
300
+ export function changemodifier_setTimestamp(a: number, b: number): number;
281
301
  export function __wbg_loromovablelist_free(a: number): void;
282
302
  export function lorodoc_importUpdateBatch(a: number, b: number, c: number): void;
283
303
  export function loromovablelist_parent(a: number): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "loro-crdt",
3
- "version": "1.4.5",
3
+ "version": "1.5.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",
package/web/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from "./loro_wasm";
2
2
  export type * from "./loro_wasm";
3
- import { AwarenessWasm, PeerID, Container, ContainerID, ContainerType, LoroCounter, LoroDoc, LoroList, LoroMap, LoroText, LoroTree, OpId, Value, AwarenessListener } from "./loro_wasm";
3
+ import { AwarenessWasm, EphemeralStoreWasm, PeerID, Container, ContainerID, ContainerType, LoroCounter, LoroDoc, LoroList, LoroMap, LoroText, LoroTree, OpId, Value, AwarenessListener, EphemeralListener, EphemeralLocalListener } from "./loro_wasm";
4
4
  /**
5
5
  * @deprecated Please use LoroDoc
6
6
  */
@@ -46,10 +46,12 @@ export declare function getType<T>(value: T): T extends LoroText ? "Text" : T ex
46
46
  export declare function newContainerID(id: OpId, type: ContainerType): ContainerID;
47
47
  export declare function newRootContainerID(name: string, type: ContainerType): ContainerID;
48
48
  /**
49
+ * @deprecated Please use `EphemeralStore` instead.
50
+ *
49
51
  * Awareness is a structure that allows to track the ephemeral state of the peers.
50
52
  *
51
53
  * If we don't receive a state update from a peer within the timeout, we will remove their state.
52
- * The timeout is in milliseconds. This can be used to handle the off-line state of a peer.
54
+ * The timeout is in milliseconds. This can be used to handle the offline state of a peer.
53
55
  */
54
56
  export declare class Awareness<T extends Value = Value> {
55
57
  inner: AwarenessWasm<T>;
@@ -70,3 +72,51 @@ export declare class Awareness<T extends Value = Value> {
70
72
  destroy(): void;
71
73
  private startTimerIfNotEmpty;
72
74
  }
75
+ /**
76
+ * EphemeralStore is a structure that allows to track the ephemeral state of the peers.
77
+ *
78
+ * It can be used to synchronize cursor positions, selections, and the names of the peers.
79
+ * Each entry uses timestamp-based LWW (Last-Write-Wins) for conflict resolution.
80
+ *
81
+ * If we don't receive a state update from a peer within the timeout, we will remove their state.
82
+ * The timeout is in milliseconds. This can be used to handle the offline state of a peer.
83
+ *
84
+ * @example
85
+ *
86
+ * ```ts
87
+ * const store = new EphemeralStore();
88
+ * const store2 = new EphemeralStore();
89
+ * // Subscribe to local updates
90
+ * store.subscribeLocalUpdates((data)=>{
91
+ * store2.apply(data);
92
+ * })
93
+ * // Subscribe to all updates
94
+ * store2.subscribe((event)=>{
95
+ * console.log("event: ", event);
96
+ * })
97
+ * // Set a value
98
+ * store.set("key", "value");
99
+ * // Encode the value
100
+ * const encoded = store.encode("key");
101
+ * // Apply the encoded value
102
+ * store2.apply(encoded);
103
+ * ```
104
+ */
105
+ export declare class EphemeralStore<T extends Value = Value> {
106
+ inner: EphemeralStoreWasm<T>;
107
+ private timer;
108
+ private timeout;
109
+ constructor(timeout?: number);
110
+ apply(bytes: Uint8Array): void;
111
+ set(key: string, value: T): void;
112
+ get(key: string): T | undefined;
113
+ getAllStates(): Record<string, T>;
114
+ encode(key: string): Uint8Array;
115
+ encodeAll(): Uint8Array;
116
+ keys(): string[];
117
+ destroy(): void;
118
+ subscribe(listener: EphemeralListener): () => void;
119
+ subscribeLocalUpdates(listener: EphemeralLocalListener): () => void;
120
+ private startTimerIfNotEmpty;
121
+ }
122
+ export declare function idStrToId(idStr: `${number}@${PeerID}`): OpId;
package/web/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { default } from "./loro_wasm.js";
2
- import { LoroDoc, AwarenessWasm } from "./loro_wasm.js";
2
+ import { LoroDoc, AwarenessWasm, EphemeralStoreWasm } from "./loro_wasm.js";
3
3
  export * from "./loro_wasm.js";
4
4
 
5
5
  /**
@@ -75,10 +75,12 @@ function newRootContainerID(name, type) {
75
75
  return `cid:root-${name}:${type}`;
76
76
  }
77
77
  /**
78
+ * @deprecated Please use `EphemeralStore` instead.
79
+ *
78
80
  * Awareness is a structure that allows to track the ephemeral state of the peers.
79
81
  *
80
82
  * If we don't receive a state update from a peer within the timeout, we will remove their state.
81
- * The timeout is in milliseconds. This can be used to handle the off-line state of a peer.
83
+ * The timeout is in milliseconds. This can be used to handle the offline state of a peer.
82
84
  */
83
85
  class Awareness {
84
86
  constructor(peer, timeout = 30000) {
@@ -152,6 +154,86 @@ class Awareness {
152
154
  }, this.timeout / 2);
153
155
  }
154
156
  }
157
+ /**
158
+ * EphemeralStore is a structure that allows to track the ephemeral state of the peers.
159
+ *
160
+ * It can be used to synchronize cursor positions, selections, and the names of the peers.
161
+ * Each entry uses timestamp-based LWW (Last-Write-Wins) for conflict resolution.
162
+ *
163
+ * If we don't receive a state update from a peer within the timeout, we will remove their state.
164
+ * The timeout is in milliseconds. This can be used to handle the offline state of a peer.
165
+ *
166
+ * @example
167
+ *
168
+ * ```ts
169
+ * const store = new EphemeralStore();
170
+ * const store2 = new EphemeralStore();
171
+ * // Subscribe to local updates
172
+ * store.subscribeLocalUpdates((data)=>{
173
+ * store2.apply(data);
174
+ * })
175
+ * // Subscribe to all updates
176
+ * store2.subscribe((event)=>{
177
+ * console.log("event: ", event);
178
+ * })
179
+ * // Set a value
180
+ * store.set("key", "value");
181
+ * // Encode the value
182
+ * const encoded = store.encode("key");
183
+ * // Apply the encoded value
184
+ * store2.apply(encoded);
185
+ * ```
186
+ */
187
+ class EphemeralStore {
188
+ constructor(timeout = 30000) {
189
+ this.inner = new EphemeralStoreWasm(timeout);
190
+ this.timeout = timeout;
191
+ }
192
+ apply(bytes) {
193
+ this.inner.apply(bytes);
194
+ this.startTimerIfNotEmpty();
195
+ }
196
+ set(key, value) {
197
+ this.inner.set(key, value);
198
+ this.startTimerIfNotEmpty();
199
+ }
200
+ get(key) {
201
+ return this.inner.get(key);
202
+ }
203
+ getAllStates() {
204
+ return this.inner.getAllStates();
205
+ }
206
+ encode(key) {
207
+ return this.inner.encode(key);
208
+ }
209
+ encodeAll() {
210
+ return this.inner.encodeAll();
211
+ }
212
+ keys() {
213
+ return this.inner.keys();
214
+ }
215
+ destroy() {
216
+ clearInterval(this.timer);
217
+ }
218
+ subscribe(listener) {
219
+ return this.inner.subscribe(listener);
220
+ }
221
+ subscribeLocalUpdates(listener) {
222
+ return this.inner.subscribeLocalUpdates(listener);
223
+ }
224
+ startTimerIfNotEmpty() {
225
+ if (this.inner.isEmpty() || this.timer != null) {
226
+ return;
227
+ }
228
+ this.timer = setInterval(() => {
229
+ this.inner.removeOutdated();
230
+ if (this.inner.isEmpty()) {
231
+ clearInterval(this.timer);
232
+ this.timer = undefined;
233
+ }
234
+ }, this.timeout / 2);
235
+ }
236
+ }
155
237
  LoroDoc.prototype.toJsonWithReplacer = function (replacer) {
156
238
  const processed = new Set();
157
239
  const doc = this;
@@ -207,6 +289,13 @@ LoroDoc.prototype.toJsonWithReplacer = function (replacer) {
207
289
  const layer = doc.getShallowValue();
208
290
  return run(layer);
209
291
  };
292
+ function idStrToId(idStr) {
293
+ const [counter, peer] = idStr.split("@");
294
+ return {
295
+ counter: parseInt(counter),
296
+ peer: peer,
297
+ };
298
+ }
210
299
 
211
- export { Awareness, Loro, getType, isContainer, isContainerId, newContainerID, newRootContainerID };
300
+ export { Awareness, EphemeralStore, Loro, getType, idStrToId, isContainer, isContainerId, newContainerID, newRootContainerID };
212
301
  //# sourceMappingURL=index.js.map
package/web/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../index.ts"],"sourcesContent":[null],"names":[],"mappings":";;;AAmBA;;AAEG;AACG,MAAO,IAAK,SAAQ,OAAO,CAAA;AAAI,CAAA;AAErC,MAAM,eAAe,GAAG;IACpB,KAAK;IACL,MAAM;IACN,MAAM;IACN,MAAM;IACN,aAAa;IACb,SAAS;CACZ,CAAC;AAEI,SAAU,aAAa,CAAC,CAAS,EAAA;AACnC,IAAA,OAAO,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC;AAChC,CAAC;AAED;;;;;;;;;;;;;;;;AAgBG;AACG,SAAU,WAAW,CAAC,KAAU,EAAA;IAClC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,IAAI,IAAI,EAAE;AAC5C,QAAA,OAAO,KAAK,CAAC;KAChB;IAED,MAAM,CAAC,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;AACvC,IAAA,IAAI,CAAC,IAAI,IAAI,IAAI,OAAO,CAAC,KAAK,QAAQ,IAAI,OAAO,CAAC,CAAC,MAAM,CAAC,KAAK,UAAU,EAAE;AACvE,QAAA,OAAO,KAAK,CAAC;KAChB;IAED,OAAO,eAAe,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC;AAClD,CAAC;AAGD;;;;;;;;;;;;;;;;AAgBG;AACG,SAAU,OAAO,CACnB,KAAQ,EAAA;AAOR,IAAA,IAAI,WAAW,CAAC,KAAK,CAAC,EAAE;AACpB,QAAA,OAAO,KAAK,CAAC,IAAI,EAAoB,CAAC;KACzC;AAED,IAAA,OAAO,MAAa,CAAC;AACzB,CAAC;AAGe,SAAA,cAAc,CAAC,EAAQ,EAAE,IAAmB,EAAA;IACxD,OAAO,CAAA,IAAA,EAAO,EAAE,CAAC,OAAO,CAAA,CAAA,EAAI,EAAE,CAAC,IAAI,CAAA,CAAA,EAAI,IAAI,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AA,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;YACtB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,KAAI;AAC7B,gBAAA,OAAO,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;AAC1B,aAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,KAAuC,IAAI,KAAK,SAAS,CAAC,CAAC;SAC7E;QAED,MAAM,MAAM,GAA0B,EAAE,CAAC;AACzC,QAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;YAC9C,MAAM,GAAG,GAAG,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AAC1B,YAAA,IAAI,GAAG,KAAK,SAAS,EAAE;AACnB,gBAAA,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC;aACrB;SACJ;AAED,QAAA,OAAO,MAAM,CAAC;AAClB,KAAC,CAAA;AAED,IAAA,MAAM,KAAK,GAAG,GAAG,CAAC,eAAe,EAAE,CAAC;AACpC,IAAA,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;AACtB,CAAC,CAAA;AAGK,SAAU,SAAS,CAAC,KAA4B,EAAA;AAClD,IAAA,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACzC,OAAO;AACH,QAAA,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC;AAC1B,QAAA,IAAI,EAAE,IAAc;KACvB,CAAC;AACN;;;;"}
@@ -161,6 +161,126 @@ interface LoroDoc {
161
161
  */
162
162
  subscribeLocalUpdates(f: (bytes: Uint8Array) => void): () => void
163
163
 
164
+ /**
165
+ * Subscribe to the first commit from a peer. Operations performed on the `LoroDoc` within this callback
166
+ * will be merged into the current commit.
167
+ *
168
+ * This is useful for managing the relationship between `PeerID` and user information.
169
+ * For example, you could store user names in a `LoroMap` using `PeerID` as the key and the `UserID` as the value.
170
+ *
171
+ * @param f - A callback function that receives a peer id.
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * const doc = new LoroDoc();
176
+ * doc.setPeerId(0);
177
+ * const p = [];
178
+ * doc.subscribeFirstCommitFromPeer((peer) => {
179
+ * p.push(peer);
180
+ * doc.getMap("map").set(e.peer, "user-" + e.peer);
181
+ * });
182
+ * doc.getList("list").insert(0, 100);
183
+ * doc.commit();
184
+ * doc.getList("list").insert(0, 200);
185
+ * doc.commit();
186
+ * doc.setPeerId(1);
187
+ * doc.getList("list").insert(0, 300);
188
+ * doc.commit();
189
+ * expect(p).toEqual(["0", "1"]);
190
+ * expect(doc.getMap("map").get("0")).toBe("user-0");
191
+ * ```
192
+ **/
193
+ subscribeFirstCommitFromPeer(f: (e: { peer: PeerID }) => void): () => void
194
+
195
+ /**
196
+ * Subscribe to the pre-commit event.
197
+ *
198
+ * The callback will be called when the changes are committed but not yet applied to the OpLog.
199
+ * You can modify the commit message and timestamp in the callback by `ChangeModifier`.
200
+ *
201
+ * @example
202
+ * ```ts
203
+ * const doc = new LoroDoc();
204
+ * doc.subscribePreCommit((e) => {
205
+ * e.modifier.setMessage("test").setTimestamp(Date.now());
206
+ * });
207
+ * doc.getList("list").insert(0, 100);
208
+ * doc.commit();
209
+ * expect(doc.getChangeAt({ peer: "0", counter: 0 }).message).toBe("test");
210
+ * ```
211
+ *
212
+ * ### Advanced Example: Creating a Merkle DAG
213
+ *
214
+ * By combining `doc.subscribePreCommit` with `doc.exportJsonInIdSpan`, you can implement advanced features like representing Loro's editing history as a Merkle DAG:
215
+ *
216
+ * ```ts
217
+ * const doc = new LoroDoc();
218
+ * doc.setPeerId(0);
219
+ * doc.subscribePreCommit((e) => {
220
+ * const changes = doc.exportJsonInIdSpan(e.changeMeta)
221
+ * expect(changes).toHaveLength(1);
222
+ * const hash = crypto.createHash('sha256');
223
+ * const change = {
224
+ * ...changes[0],
225
+ * deps: changes[0].deps.map(d => {
226
+ * const depChange = doc.getChangeAt(idStrToId(d))
227
+ * return depChange.message;
228
+ * })
229
+ * }
230
+ * console.log(change); // The output is shown below
231
+ * hash.update(JSON.stringify(change));
232
+ * const sha256Hash = hash.digest('hex');
233
+ * e.modifier.setMessage(sha256Hash);
234
+ * });
235
+ *
236
+ * doc.getList("list").insert(0, 100);
237
+ * doc.commit();
238
+ * // Change 0
239
+ * // {
240
+ * // id: '0@0',
241
+ * // timestamp: 0,
242
+ * // deps: [],
243
+ * // lamport: 0,
244
+ * // msg: undefined,
245
+ * // ops: [
246
+ * // {
247
+ * // container: 'cid:root-list:List',
248
+ * // content: { type: 'insert', pos: 0, value: [100] },
249
+ * // counter: 0
250
+ * // }
251
+ * // ]
252
+ * // }
253
+ *
254
+ *
255
+ * doc.getList("list").insert(0, 200);
256
+ * doc.commit();
257
+ * // Change 1
258
+ * // {
259
+ * // id: '1@0',
260
+ * // timestamp: 0,
261
+ * // deps: [
262
+ * // '2af99cf93869173984bcf6b1ce5412610b0413d027a5511a8f720a02a4432853'
263
+ * // ],
264
+ * // lamport: 1,
265
+ * // msg: undefined,
266
+ * // ops: [
267
+ * // {
268
+ * // container: 'cid:root-list:List',
269
+ * // content: { type: 'insert', pos: 0, value: [200] },
270
+ * // counter: 1
271
+ * // }
272
+ * // ]
273
+ * // }
274
+ *
275
+ * expect(doc.getChangeAt({ peer: "0", counter: 0 }).message).toBe("2af99cf93869173984bcf6b1ce5412610b0413d027a5511a8f720a02a4432853");
276
+ * expect(doc.getChangeAt({ peer: "0", counter: 1 }).message).toBe("aedbb442c554ecf59090e0e8339df1d8febf647f25cc37c67be0c6e27071d37f");
277
+ * ```
278
+ *
279
+ * @param f - A callback function that receives a pre commit event.
280
+ *
281
+ **/
282
+ subscribePreCommit(f: (e: { changeMeta: Change, origin: string, modifier: ChangeModifier }) => void): () => void
283
+
164
284
  /**
165
285
  * Convert the document to a JSON value with a custom replacer function.
166
286
  *
@@ -759,7 +879,6 @@ export type AwarenessListener = (
759
879
  origin: "local" | "timeout" | "remote" | string,
760
880
  ) => void;
761
881
 
762
-
763
882
  interface Listener {
764
883
  (event: LoroEventBatch): void;
765
884
  }
@@ -856,10 +975,15 @@ interface LoroDoc<T extends Record<string, Container> = Record<string, Container
856
975
  */
857
976
  exportJsonUpdates(start?: VersionVector, end?: VersionVector, withPeerCompression?: boolean): JsonSchema;
858
977
  /**
859
- * Export the readable [`Change`]s in the given [`IdSpan`].
978
+ * Exports changes within the specified ID span to JSON schema format.
860
979
  *
861
- * The peers are not compressed in the returned changes.
980
+ * The JSON schema format produced by this method is identical to the one generated by `export_json_updates`.
981
+ * It ensures deterministic output, making it ideal for hash calculations and integrity checks.
862
982
  *
983
+ * This method can also export pending changes from the uncommitted transaction that have not yet been applied to the OpLog.
984
+ *
985
+ * This method will NOT trigger a new commit implicitly.
986
+ *
863
987
  * @param idSpan - The id span to export.
864
988
  * @returns The changes in the given id span.
865
989
  */
@@ -1274,6 +1398,25 @@ interface AwarenessWasm<T extends Value = Value> {
1274
1398
  removeOutdated(): PeerID[];
1275
1399
  }
1276
1400
 
1401
+ type EphemeralListener = (event: EphemeralStoreEvent) => void;
1402
+ type EphemeralLocalListener = (bytes: Uint8Array) => void;
1403
+
1404
+ interface EphemeralStoreWasm<T extends Value = Value> {
1405
+ set(key: string, value: T): void;
1406
+ get(key: string): T | undefined;
1407
+ getAllStates(): Record<string, T>;
1408
+ removeOutdated();
1409
+ subscribeLocalUpdates(f: EphemeralLocalListener): () => void;
1410
+ subscribe(f: EphemeralListener): () => void;
1411
+ }
1412
+
1413
+ interface EphemeralStoreEvent {
1414
+ by: "local" | "import" | "timeout";
1415
+ added: string[];
1416
+ updated: string[];
1417
+ removed: string[];
1418
+ }
1419
+
1277
1420
 
1278
1421
 
1279
1422
  /**
@@ -1349,6 +1492,21 @@ export class AwarenessWasm {
1349
1492
  peers(): (PeerID)[];
1350
1493
  }
1351
1494
  /**
1495
+ */
1496
+ export class ChangeModifier {
1497
+ free(): void;
1498
+ /**
1499
+ * @param {string} message
1500
+ * @returns {ChangeModifier}
1501
+ */
1502
+ setMessage(message: string): ChangeModifier;
1503
+ /**
1504
+ * @param {number} timestamp
1505
+ * @returns {ChangeModifier}
1506
+ */
1507
+ setTimestamp(timestamp: number): ChangeModifier;
1508
+ }
1509
+ /**
1352
1510
  * Cursor is a stable position representation in the doc.
1353
1511
  * When expressing the position of a cursor, using "index" can be unstable
1354
1512
  * because the cursor's position may change due to other deletions and insertions,
@@ -1418,6 +1576,63 @@ export class Cursor {
1418
1576
  kind(): any;
1419
1577
  }
1420
1578
  /**
1579
+ */
1580
+ export class EphemeralStoreWasm {
1581
+ free(): void;
1582
+ /**
1583
+ * Creates a new `EphemeralStore` instance.
1584
+ *
1585
+ * The `timeout` parameter specifies the duration in milliseconds.
1586
+ * A state of a peer is considered outdated, if the last update of the state of the peer
1587
+ * is older than the `timeout`.
1588
+ * @param {number} timeout
1589
+ */
1590
+ constructor(timeout: number);
1591
+ /**
1592
+ * @param {string} key
1593
+ * @param {any} value
1594
+ */
1595
+ set(key: string, value: any): void;
1596
+ /**
1597
+ * @param {string} key
1598
+ */
1599
+ delete(key: string): void;
1600
+ /**
1601
+ * @param {string} key
1602
+ * @returns {any}
1603
+ */
1604
+ get(key: string): any;
1605
+ /**
1606
+ * @returns {any}
1607
+ */
1608
+ getAllStates(): any;
1609
+ /**
1610
+ * @param {string} key
1611
+ * @returns {Uint8Array}
1612
+ */
1613
+ encode(key: string): Uint8Array;
1614
+ /**
1615
+ * @returns {Uint8Array}
1616
+ */
1617
+ encodeAll(): Uint8Array;
1618
+ /**
1619
+ * @param {Uint8Array} data
1620
+ */
1621
+ apply(data: Uint8Array): void;
1622
+ /**
1623
+ */
1624
+ removeOutdated(): void;
1625
+ /**
1626
+ * If the state is empty.
1627
+ * @returns {boolean}
1628
+ */
1629
+ isEmpty(): boolean;
1630
+ /**
1631
+ * @returns {(string)[]}
1632
+ */
1633
+ keys(): (string)[];
1634
+ }
1635
+ /**
1421
1636
  * The handler of a counter container.
1422
1637
  */
1423
1638
  export class LoroCounter {
@@ -2443,6 +2658,27 @@ export class LoroDoc {
2443
2658
  */
2444
2659
  applyDiff(diff: [ContainerID, Diff|JsonDiff][]): void;
2445
2660
  /**
2661
+ * Get the pending operations from the current transaction in JSON format
2662
+ *
2663
+ * This method returns a JSON representation of operations that have been applied
2664
+ * but not yet committed in the current transaction.
2665
+ *
2666
+ * It will use the same data format as `doc.exportJsonUpdates()`
2667
+ *
2668
+ * @example
2669
+ * ```ts
2670
+ * const doc = new LoroDoc();
2671
+ * const text = doc.getText("text");
2672
+ * text.insert(0, "Hello");
2673
+ * // Get pending ops before commit
2674
+ * const pendingOps = doc.getPendingOpsFromCurrentTxnAsJson();
2675
+ * doc.commit();
2676
+ * const emptyOps = doc.getPendingOpsFromCurrentTxnAsJson(); // this is undefined
2677
+ * ```
2678
+ * @returns {JsonSchema | undefined}
2679
+ */
2680
+ getUncommittedOpsAsJson(): JsonSchema | undefined;
2681
+ /**
2446
2682
  * Peer ID of the current writer.
2447
2683
  */
2448
2684
  readonly peerId: bigint;
@@ -3104,8 +3340,6 @@ export class LoroText {
3104
3340
  *
3105
3341
  * You can use it to create a highlight, make a range of text bold, or add a link to a range of text.
3106
3342
  *
3107
- * Note: this is not suitable for unmergeable annotations like comments.
3108
- *
3109
3343
  * @example
3110
3344
  * ```ts
3111
3345
  * import { LoroDoc } from "loro-crdt";
@@ -3734,6 +3968,20 @@ export interface InitOutput {
3734
3968
  readonly awarenesswasm_length: (a: number) => number;
3735
3969
  readonly awarenesswasm_isEmpty: (a: number) => number;
3736
3970
  readonly awarenesswasm_peers: (a: number, b: number) => void;
3971
+ readonly __wbg_ephemeralstorewasm_free: (a: number) => void;
3972
+ readonly ephemeralstorewasm_new: (a: number) => number;
3973
+ readonly ephemeralstorewasm_set: (a: number, b: number, c: number, d: number) => void;
3974
+ readonly ephemeralstorewasm_delete: (a: number, b: number, c: number) => void;
3975
+ readonly ephemeralstorewasm_get: (a: number, b: number, c: number) => number;
3976
+ readonly ephemeralstorewasm_getAllStates: (a: number) => number;
3977
+ readonly ephemeralstorewasm_subscribeLocalUpdates: (a: number, b: number) => number;
3978
+ readonly ephemeralstorewasm_subscribe: (a: number, b: number) => number;
3979
+ readonly ephemeralstorewasm_encode: (a: number, b: number, c: number, d: number) => void;
3980
+ readonly ephemeralstorewasm_encodeAll: (a: number, b: number) => void;
3981
+ readonly ephemeralstorewasm_apply: (a: number, b: number, c: number) => void;
3982
+ readonly ephemeralstorewasm_removeOutdated: (a: number) => void;
3983
+ readonly ephemeralstorewasm_isEmpty: (a: number) => number;
3984
+ readonly ephemeralstorewasm_keys: (a: number, b: number) => void;
3737
3985
  readonly LORO_VERSION: (a: number) => void;
3738
3986
  readonly run: () => void;
3739
3987
  readonly encodeFrontiers: (a: number, b: number, c: number) => void;
@@ -3814,6 +4062,9 @@ export interface InitOutput {
3814
4062
  readonly lorodoc_revertTo: (a: number, b: number, c: number, d: number) => void;
3815
4063
  readonly lorodoc_applyDiff: (a: number, b: number, c: number) => void;
3816
4064
  readonly lorodoc_diff: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
4065
+ readonly lorodoc_getUncommittedOpsAsJson: (a: number, b: number) => void;
4066
+ readonly lorodoc_subscribeFirstCommitFromPeer: (a: number, b: number) => number;
4067
+ readonly lorodoc_subscribePreCommit: (a: number, b: number) => number;
3817
4068
  readonly __wbg_lorotext_free: (a: number) => void;
3818
4069
  readonly lorotext_new: () => number;
3819
4070
  readonly lorotext_kind: (a: number) => number;
@@ -3985,6 +4236,9 @@ export interface InitOutput {
3985
4236
  readonly versionvector_remove: (a: number, b: number, c: number) => void;
3986
4237
  readonly versionvector_length: (a: number) => number;
3987
4238
  readonly decodeImportBlobMeta: (a: number, b: number, c: number, d: number) => void;
4239
+ readonly __wbg_changemodifier_free: (a: number) => void;
4240
+ readonly changemodifier_setMessage: (a: number, b: number, c: number) => number;
4241
+ readonly changemodifier_setTimestamp: (a: number, b: number) => number;
3988
4242
  readonly __wbg_loromovablelist_free: (a: number) => void;
3989
4243
  readonly lorodoc_importUpdateBatch: (a: number, b: number, c: number) => void;
3990
4244
  readonly loromovablelist_parent: (a: number) => number;