@liveblocks/client 0.12.2 → 0.13.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.
Files changed (44) hide show
  1. package/README.md +34 -6
  2. package/lib/cjs/AbstractCrdt.d.ts +61 -0
  3. package/lib/cjs/AbstractCrdt.js +98 -0
  4. package/lib/cjs/LiveList.d.ts +133 -0
  5. package/lib/cjs/LiveList.js +374 -0
  6. package/lib/cjs/LiveMap.d.ts +83 -0
  7. package/lib/cjs/LiveMap.js +272 -0
  8. package/lib/cjs/LiveObject.d.ts +66 -0
  9. package/lib/cjs/LiveObject.js +368 -0
  10. package/lib/cjs/LiveRegister.d.ts +21 -0
  11. package/lib/cjs/LiveRegister.js +69 -0
  12. package/lib/cjs/index.d.ts +3 -1
  13. package/lib/cjs/index.js +7 -5
  14. package/lib/cjs/room.d.ts +50 -9
  15. package/lib/cjs/room.js +476 -85
  16. package/lib/cjs/types.d.ts +220 -40
  17. package/lib/cjs/utils.d.ts +7 -0
  18. package/lib/cjs/utils.js +64 -1
  19. package/lib/esm/AbstractCrdt.d.ts +61 -0
  20. package/lib/esm/AbstractCrdt.js +94 -0
  21. package/lib/esm/LiveList.d.ts +133 -0
  22. package/lib/esm/LiveList.js +370 -0
  23. package/lib/esm/LiveMap.d.ts +83 -0
  24. package/lib/esm/LiveMap.js +268 -0
  25. package/lib/esm/LiveObject.d.ts +66 -0
  26. package/lib/esm/LiveObject.js +364 -0
  27. package/lib/esm/LiveRegister.d.ts +21 -0
  28. package/lib/esm/LiveRegister.js +65 -0
  29. package/lib/esm/index.d.ts +3 -1
  30. package/lib/esm/index.js +3 -1
  31. package/lib/esm/room.d.ts +50 -9
  32. package/lib/esm/room.js +478 -84
  33. package/lib/esm/types.d.ts +220 -40
  34. package/lib/esm/utils.d.ts +7 -0
  35. package/lib/esm/utils.js +58 -0
  36. package/package.json +3 -3
  37. package/lib/cjs/doc.d.ts +0 -347
  38. package/lib/cjs/doc.js +0 -1349
  39. package/lib/cjs/storage.d.ts +0 -21
  40. package/lib/cjs/storage.js +0 -68
  41. package/lib/esm/doc.d.ts +0 -347
  42. package/lib/esm/doc.js +0 -1342
  43. package/lib/esm/storage.d.ts +0 -21
  44. package/lib/esm/storage.js +0 -65
@@ -0,0 +1,368 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _LiveObject_instances, _LiveObject_map, _LiveObject_propToLastUpdate, _LiveObject_applyUpdate, _LiveObject_applyDeleteObjectKey;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.LiveObject = void 0;
16
+ const AbstractCrdt_1 = require("./AbstractCrdt");
17
+ const utils_1 = require("./utils");
18
+ const live_1 = require("./live");
19
+ /**
20
+ * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
21
+ * Keys should be a string, and values should be serializable to JSON.
22
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
23
+ */
24
+ class LiveObject extends AbstractCrdt_1.AbstractCrdt {
25
+ constructor(object = {}) {
26
+ super();
27
+ _LiveObject_instances.add(this);
28
+ _LiveObject_map.set(this, void 0);
29
+ _LiveObject_propToLastUpdate.set(this, new Map());
30
+ for (const key in object) {
31
+ const value = object[key];
32
+ if (value instanceof AbstractCrdt_1.AbstractCrdt) {
33
+ value._setParentLink(this, key);
34
+ }
35
+ }
36
+ __classPrivateFieldSet(this, _LiveObject_map, new Map(Object.entries(object)), "f");
37
+ }
38
+ /**
39
+ * INTERNAL
40
+ */
41
+ _serialize(parentId, parentKey) {
42
+ if (this._id == null) {
43
+ throw new Error("Cannot serialize item is not attached");
44
+ }
45
+ const ops = [];
46
+ const op = {
47
+ id: this._id,
48
+ type: live_1.OpType.CreateObject,
49
+ parentId,
50
+ parentKey,
51
+ data: {},
52
+ };
53
+ ops.push(op);
54
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
55
+ if (value instanceof AbstractCrdt_1.AbstractCrdt) {
56
+ ops.push(...value._serialize(this._id, key));
57
+ }
58
+ else {
59
+ op.data[key] = value;
60
+ }
61
+ }
62
+ return ops;
63
+ }
64
+ /**
65
+ * INTERNAL
66
+ */
67
+ static _deserialize([id, item], parentToChildren, doc) {
68
+ if (item.type !== live_1.CrdtType.Object) {
69
+ throw new Error(`Tried to deserialize a record but item type is "${item.type}"`);
70
+ }
71
+ const object = new LiveObject(item.data);
72
+ object._attach(id, doc);
73
+ const children = parentToChildren.get(id);
74
+ if (children == null) {
75
+ return object;
76
+ }
77
+ for (const entry of children) {
78
+ const crdt = entry[1];
79
+ if (crdt.parentKey == null) {
80
+ throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
81
+ }
82
+ const child = (0, utils_1.deserialize)(entry, parentToChildren, doc);
83
+ child._setParentLink(object, crdt.parentKey);
84
+ __classPrivateFieldGet(object, _LiveObject_map, "f").set(crdt.parentKey, child);
85
+ }
86
+ return object;
87
+ }
88
+ /**
89
+ * INTERNAL
90
+ */
91
+ _attach(id, doc) {
92
+ super._attach(id, doc);
93
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
94
+ if (value instanceof AbstractCrdt_1.AbstractCrdt) {
95
+ value._attach(doc.generateId(), doc);
96
+ }
97
+ }
98
+ }
99
+ /**
100
+ * INTERNAL
101
+ */
102
+ _attachChild(id, key, child) {
103
+ if (this._doc == null) {
104
+ throw new Error("Can't attach child if doc is not present");
105
+ }
106
+ const previousValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
107
+ let reverse;
108
+ if ((0, utils_1.isCrdt)(previousValue)) {
109
+ reverse = previousValue._serialize(this._id, key);
110
+ previousValue._detach();
111
+ }
112
+ else if (previousValue === undefined) {
113
+ reverse = [
114
+ { type: live_1.OpType.DeleteObjectKey, id: this._id, key: key },
115
+ ];
116
+ }
117
+ else {
118
+ reverse = [
119
+ {
120
+ type: live_1.OpType.UpdateObject,
121
+ id: this._id,
122
+ data: { [key]: previousValue },
123
+ },
124
+ ];
125
+ }
126
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, child);
127
+ child._setParentLink(this, key);
128
+ child._attach(id, this._doc);
129
+ return { reverse, modified: this };
130
+ }
131
+ /**
132
+ * INTERNAL
133
+ */
134
+ _detachChild(child) {
135
+ for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
136
+ if (value === child) {
137
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
138
+ }
139
+ }
140
+ if (child) {
141
+ child._detach();
142
+ }
143
+ }
144
+ /**
145
+ * INTERNAL
146
+ */
147
+ _detach() {
148
+ super._detach();
149
+ for (const value of __classPrivateFieldGet(this, _LiveObject_map, "f").values()) {
150
+ if ((0, utils_1.isCrdt)(value)) {
151
+ value._detach();
152
+ }
153
+ }
154
+ }
155
+ /**
156
+ * INTERNAL
157
+ */
158
+ _apply(op) {
159
+ if (op.type === live_1.OpType.UpdateObject) {
160
+ return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyUpdate).call(this, op);
161
+ }
162
+ else if (op.type === live_1.OpType.DeleteObjectKey) {
163
+ return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyDeleteObjectKey).call(this, op);
164
+ }
165
+ return super._apply(op);
166
+ }
167
+ /**
168
+ * Transform the LiveObject into a javascript object
169
+ */
170
+ toObject() {
171
+ return Object.fromEntries(__classPrivateFieldGet(this, _LiveObject_map, "f"));
172
+ }
173
+ /**
174
+ * Adds or updates a property with a specified key and a value.
175
+ * @param key The key of the property to add
176
+ * @param value The value of the property to add
177
+ */
178
+ set(key, value) {
179
+ // TODO: Find out why typescript complains
180
+ this.update({ [key]: value });
181
+ }
182
+ /**
183
+ * Returns a specified property from the LiveObject.
184
+ * @param key The key of the property to get
185
+ */
186
+ get(key) {
187
+ return __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
188
+ }
189
+ /**
190
+ * Deletes a key from the LiveObject
191
+ * @param key The key of the property to delete
192
+ */
193
+ delete(key) {
194
+ const keyAsString = key;
195
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(keyAsString);
196
+ if (oldValue === undefined) {
197
+ return;
198
+ }
199
+ if (this._doc == null || this._id == null) {
200
+ if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
201
+ oldValue._detach();
202
+ }
203
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
204
+ return;
205
+ }
206
+ let reverse;
207
+ if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
208
+ oldValue._detach();
209
+ reverse = oldValue._serialize(this._id, keyAsString);
210
+ }
211
+ else {
212
+ reverse = [
213
+ {
214
+ type: live_1.OpType.UpdateObject,
215
+ data: { [keyAsString]: oldValue },
216
+ id: this._id,
217
+ },
218
+ ];
219
+ }
220
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
221
+ this._doc.dispatch([{ type: live_1.OpType.DeleteObjectKey, key: keyAsString, id: this._id }], reverse, [this]);
222
+ }
223
+ /**
224
+ * Adds or updates multiple properties at once with an object.
225
+ * @param overrides The object used to overrides properties
226
+ */
227
+ update(overrides) {
228
+ if (this._doc == null || this._id == null) {
229
+ for (const key in overrides) {
230
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
231
+ if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
232
+ oldValue._detach();
233
+ }
234
+ const newValue = overrides[key];
235
+ if (newValue instanceof AbstractCrdt_1.AbstractCrdt) {
236
+ newValue._setParentLink(this, key);
237
+ }
238
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
239
+ }
240
+ return;
241
+ }
242
+ const ops = [];
243
+ const reverseOps = [];
244
+ const opId = this._doc.generateOpId();
245
+ const updatedProps = {};
246
+ const reverseUpdateOp = {
247
+ id: this._id,
248
+ type: live_1.OpType.UpdateObject,
249
+ data: {},
250
+ };
251
+ for (const key in overrides) {
252
+ __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, opId);
253
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
254
+ if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
255
+ reverseOps.push(...oldValue._serialize(this._id, key));
256
+ oldValue._detach();
257
+ }
258
+ else if (oldValue === undefined) {
259
+ reverseOps.push({ type: live_1.OpType.DeleteObjectKey, id: this._id, key });
260
+ }
261
+ else {
262
+ reverseUpdateOp.data[key] = oldValue;
263
+ }
264
+ const newValue = overrides[key];
265
+ if (newValue instanceof AbstractCrdt_1.AbstractCrdt) {
266
+ newValue._setParentLink(this, key);
267
+ newValue._attach(this._doc.generateId(), this._doc);
268
+ ops.push(...newValue._serialize(this._id, key));
269
+ }
270
+ else {
271
+ updatedProps[key] = newValue;
272
+ }
273
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
274
+ }
275
+ if (Object.keys(reverseUpdateOp.data).length !== 0) {
276
+ reverseOps.unshift(reverseUpdateOp);
277
+ }
278
+ if (Object.keys(updatedProps).length !== 0) {
279
+ ops.unshift({
280
+ opId,
281
+ id: this._id,
282
+ type: live_1.OpType.UpdateObject,
283
+ data: updatedProps,
284
+ });
285
+ }
286
+ this._doc.dispatch(ops, reverseOps, [this]);
287
+ }
288
+ }
289
+ exports.LiveObject = LiveObject;
290
+ _LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _LiveObject_instances = new WeakSet(), _LiveObject_applyUpdate = function _LiveObject_applyUpdate(op) {
291
+ let isModified = false;
292
+ const reverse = [];
293
+ const reverseUpdate = {
294
+ type: live_1.OpType.UpdateObject,
295
+ id: this._id,
296
+ data: {},
297
+ };
298
+ reverse.push(reverseUpdate);
299
+ for (const key in op.data) {
300
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
301
+ if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
302
+ reverse.push(...oldValue._serialize(this._id, key));
303
+ oldValue._detach();
304
+ }
305
+ else if (oldValue !== undefined) {
306
+ reverseUpdate.data[key] = oldValue;
307
+ }
308
+ else if (oldValue === undefined) {
309
+ reverse.push({ type: live_1.OpType.DeleteObjectKey, id: this._id, key });
310
+ }
311
+ }
312
+ let isLocal = false;
313
+ if (op.opId == null) {
314
+ isLocal = true;
315
+ op.opId = this._doc.generateOpId();
316
+ }
317
+ for (const key in op.data) {
318
+ if (isLocal) {
319
+ __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, op.opId);
320
+ }
321
+ else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) == null) {
322
+ // Not modified localy so we apply update
323
+ isModified = true;
324
+ }
325
+ else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) === op.opId) {
326
+ // Acknowlegment from local operation
327
+ __classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").delete(key);
328
+ continue;
329
+ }
330
+ else {
331
+ // Conflict, ignore remote operation
332
+ continue;
333
+ }
334
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
335
+ if ((0, utils_1.isCrdt)(oldValue)) {
336
+ oldValue._detach();
337
+ }
338
+ isModified = true;
339
+ __classPrivateFieldGet(this, _LiveObject_map, "f").set(key, op.data[key]);
340
+ }
341
+ if (Object.keys(reverseUpdate.data).length !== 0) {
342
+ reverse.unshift(reverseUpdate);
343
+ }
344
+ return isModified ? { modified: this, reverse } : { modified: false };
345
+ }, _LiveObject_applyDeleteObjectKey = function _LiveObject_applyDeleteObjectKey(op) {
346
+ const key = op.key;
347
+ // If property does not exist, exit without notifying
348
+ if (__classPrivateFieldGet(this, _LiveObject_map, "f").has(key) === false) {
349
+ return { modified: false };
350
+ }
351
+ const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
352
+ let reverse = [];
353
+ if ((0, utils_1.isCrdt)(oldValue)) {
354
+ reverse = oldValue._serialize(this._id, op.key);
355
+ oldValue._detach();
356
+ }
357
+ else if (oldValue !== undefined) {
358
+ reverse = [
359
+ {
360
+ type: live_1.OpType.UpdateObject,
361
+ id: this._id,
362
+ data: { [key]: oldValue },
363
+ },
364
+ ];
365
+ }
366
+ __classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
367
+ return { modified: this, reverse };
368
+ };
@@ -0,0 +1,21 @@
1
+ import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
+ import { SerializedCrdtWithId, Op } from "./live";
3
+ /**
4
+ * INTERNAL
5
+ */
6
+ export declare class LiveRegister<TValue = any> extends AbstractCrdt {
7
+ #private;
8
+ constructor(data: TValue);
9
+ get data(): TValue;
10
+ /**
11
+ * INTERNAL
12
+ */
13
+ static _deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveRegister<any>;
14
+ /**
15
+ * INTERNAL
16
+ */
17
+ _serialize(parentId: string, parentKey: string): Op[];
18
+ _attachChild(id: string, key: string, crdt: AbstractCrdt): ApplyResult;
19
+ _detachChild(crdt: AbstractCrdt): void;
20
+ _apply(op: Op): ApplyResult;
21
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _LiveRegister_data;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.LiveRegister = void 0;
16
+ const AbstractCrdt_1 = require("./AbstractCrdt");
17
+ const live_1 = require("./live");
18
+ /**
19
+ * INTERNAL
20
+ */
21
+ class LiveRegister extends AbstractCrdt_1.AbstractCrdt {
22
+ constructor(data) {
23
+ super();
24
+ _LiveRegister_data.set(this, void 0);
25
+ __classPrivateFieldSet(this, _LiveRegister_data, data, "f");
26
+ }
27
+ get data() {
28
+ return __classPrivateFieldGet(this, _LiveRegister_data, "f");
29
+ }
30
+ /**
31
+ * INTERNAL
32
+ */
33
+ static _deserialize([id, item], parentToChildren, doc) {
34
+ if (item.type !== live_1.CrdtType.Register) {
35
+ throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
36
+ }
37
+ const register = new LiveRegister(item.data);
38
+ register._attach(id, doc);
39
+ return register;
40
+ }
41
+ /**
42
+ * INTERNAL
43
+ */
44
+ _serialize(parentId, parentKey) {
45
+ if (this._id == null || parentId == null || parentKey == null) {
46
+ throw new Error("Cannot serialize register if parentId or parentKey is undefined");
47
+ }
48
+ return [
49
+ {
50
+ type: live_1.OpType.CreateRegister,
51
+ id: this._id,
52
+ parentId,
53
+ parentKey,
54
+ data: this.data,
55
+ },
56
+ ];
57
+ }
58
+ _attachChild(id, key, crdt) {
59
+ throw new Error("Method not implemented.");
60
+ }
61
+ _detachChild(crdt) {
62
+ throw new Error("Method not implemented.");
63
+ }
64
+ _apply(op) {
65
+ return super._apply(op);
66
+ }
67
+ }
68
+ exports.LiveRegister = LiveRegister;
69
+ _LiveRegister_data = new WeakMap();
@@ -1,3 +1,5 @@
1
- export { LiveObject, LiveList, LiveMap } from "./doc";
1
+ export { LiveObject } from "./LiveObject";
2
+ export { LiveMap } from "./LiveMap";
3
+ export { LiveList } from "./LiveList";
2
4
  export type { Others, Presence, Room, Client, User } from "./types";
3
5
  export { createClient } from "./client";
package/lib/cjs/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createClient = exports.LiveMap = exports.LiveList = exports.LiveObject = void 0;
4
- var doc_1 = require("./doc");
5
- Object.defineProperty(exports, "LiveObject", { enumerable: true, get: function () { return doc_1.LiveObject; } });
6
- Object.defineProperty(exports, "LiveList", { enumerable: true, get: function () { return doc_1.LiveList; } });
7
- Object.defineProperty(exports, "LiveMap", { enumerable: true, get: function () { return doc_1.LiveMap; } });
3
+ exports.createClient = exports.LiveList = exports.LiveMap = exports.LiveObject = void 0;
4
+ var LiveObject_1 = require("./LiveObject");
5
+ Object.defineProperty(exports, "LiveObject", { enumerable: true, get: function () { return LiveObject_1.LiveObject; } });
6
+ var LiveMap_1 = require("./LiveMap");
7
+ Object.defineProperty(exports, "LiveMap", { enumerable: true, get: function () { return LiveMap_1.LiveMap; } });
8
+ var LiveList_1 = require("./LiveList");
9
+ Object.defineProperty(exports, "LiveList", { enumerable: true, get: function () { return LiveList_1.LiveList; } });
8
10
  var client_1 = require("./client");
9
11
  Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return client_1.createClient; } });
package/lib/cjs/room.d.ts CHANGED
@@ -1,11 +1,19 @@
1
- import { Others, Presence, ClientOptions, Room, MyPresenceCallback, OthersEventCallback, AuthEndpoint, EventCallback, User, Connection, ErrorCallback, AuthenticationToken, ConnectionCallback } from "./types";
1
+ import { Others, Presence, ClientOptions, Room, MyPresenceCallback, OthersEventCallback, AuthEndpoint, EventCallback, User, Connection, ErrorCallback, AuthenticationToken, ConnectionCallback, StorageCallback, StorageUpdate } from "./types";
2
2
  import { ClientMessage, Op } from "./live";
3
+ import { LiveMap } from "./LiveMap";
4
+ import { LiveObject } from "./LiveObject";
5
+ import { LiveList } from "./LiveList";
6
+ import { AbstractCrdt } from "./AbstractCrdt";
7
+ declare type HistoryItem = Array<Op | {
8
+ type: "presence";
9
+ data: Presence;
10
+ }>;
3
11
  declare type IdFactory = () => string;
4
12
  export declare type State = {
5
13
  connection: Connection;
6
14
  socket: WebSocket | null;
7
15
  lastFlushTime: number;
8
- flushData: {
16
+ buffer: {
9
17
  presence: Presence | null;
10
18
  messages: ClientMessage[];
11
19
  storageOperations: Op[];
@@ -24,6 +32,7 @@ export declare type State = {
24
32
  "my-presence": MyPresenceCallback[];
25
33
  error: ErrorCallback[];
26
34
  connection: ConnectionCallback[];
35
+ storage: StorageCallback[];
27
36
  };
28
37
  me: Presence;
29
38
  others: Others;
@@ -35,6 +44,24 @@ export declare type State = {
35
44
  defaultStorageRoot?: {
36
45
  [key: string]: any;
37
46
  };
47
+ clock: number;
48
+ opClock: number;
49
+ items: Map<string, AbstractCrdt>;
50
+ root: LiveObject | undefined;
51
+ undoStack: HistoryItem[];
52
+ redoStack: HistoryItem[];
53
+ isHistoryPaused: boolean;
54
+ pausedHistory: HistoryItem;
55
+ isBatching: boolean;
56
+ batch: {
57
+ ops: Op[];
58
+ reverseOps: HistoryItem;
59
+ updates: {
60
+ others: [];
61
+ presence: boolean;
62
+ nodes: Set<AbstractCrdt>;
63
+ };
64
+ };
38
65
  };
39
66
  export declare type Effects = {
40
67
  authenticate(): void;
@@ -63,14 +90,23 @@ export declare function makeStateMachine(state: State, context: Context, mockedE
63
90
  heartbeat: () => void;
64
91
  onNavigatorOnline: () => void;
65
92
  onVisibilityChange: (visibilityState: VisibilityState) => void;
93
+ getUndoStack: () => HistoryItem[];
94
+ getItemsCount: () => number;
66
95
  connect: () => null | undefined;
67
96
  disconnect: () => void;
68
97
  subscribe: {
69
- <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): void;
70
- <T_1 extends Presence>(type: "others", listener: OthersEventCallback<T_1>): void;
71
- (type: "event", listener: EventCallback): void;
72
- (type: "error", listener: ErrorCallback): void;
73
- (type: "connection", listener: ConnectionCallback): void;
98
+ (callback: (updates: StorageUpdate) => void): () => void;
99
+ <TKey extends string, TValue>(liveMap: LiveMap<TKey, TValue>, callback: (liveMap: LiveMap<TKey, TValue>) => void): () => void;
100
+ <TData>(liveObject: LiveObject<TData>, callback: (liveObject: LiveObject<TData>) => void): () => void;
101
+ <TItem>(liveList: LiveList<TItem>, callback: (liveList: LiveList<TItem>) => void): () => void;
102
+ <TItem_1 extends AbstractCrdt>(node: TItem_1, callback: (updates: StorageUpdate[]) => void, options: {
103
+ isDeep: true;
104
+ }): () => void;
105
+ <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): () => void;
106
+ <T_1 extends Presence>(type: "others", listener: OthersEventCallback<T_1>): () => void;
107
+ (type: "event", listener: EventCallback): () => void;
108
+ (type: "error", listener: ErrorCallback): () => void;
109
+ (type: "connection", listener: ConnectionCallback): () => void;
74
110
  };
75
111
  unsubscribe: {
76
112
  <T_2 extends Presence>(type: "my-presence", listener: MyPresenceCallback<T_2>): void;
@@ -79,12 +115,17 @@ export declare function makeStateMachine(state: State, context: Context, mockedE
79
115
  (type: "error", listener: ErrorCallback): void;
80
116
  (type: "connection", listener: ConnectionCallback): void;
81
117
  };
82
- updatePresence: <T_4 extends Presence>(overrides: Partial<T_4>) => void;
118
+ updatePresence: <T_4 extends Presence>(overrides: Partial<T_4>, options?: {
119
+ addToHistory: boolean;
120
+ } | undefined) => void;
83
121
  broadcastEvent: (event: any) => void;
122
+ batch: (callback: () => void) => void;
84
123
  undo: () => void;
85
124
  redo: () => void;
125
+ pauseHistory: () => void;
126
+ resumeHistory: () => void;
86
127
  getStorage: <TRoot>() => Promise<{
87
- root: import("./doc").LiveObject<TRoot>;
128
+ root: LiveObject<TRoot>;
88
129
  }>;
89
130
  selectors: {
90
131
  getConnectionState: () => "failed" | "closed" | "connecting" | "open" | "authenticating" | "unavailable";