@liveblocks/client 0.15.0-alpha.3 → 0.15.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 (59) hide show
  1. package/README.md +5 -10
  2. package/lib/esm/index.js +2991 -5
  3. package/lib/esm/index.mjs +2991 -0
  4. package/lib/esm/internal.js +149 -0
  5. package/lib/esm/internal.mjs +149 -0
  6. package/lib/{esm/types.d.ts → index.d.ts} +253 -61
  7. package/lib/index.js +4237 -0
  8. package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
  9. package/lib/internal.js +193 -0
  10. package/package.json +32 -10
  11. package/lib/cjs/AbstractCrdt.d.ts +0 -68
  12. package/lib/cjs/AbstractCrdt.js +0 -95
  13. package/lib/cjs/LiveList.d.ts +0 -144
  14. package/lib/cjs/LiveList.js +0 -530
  15. package/lib/cjs/LiveMap.d.ts +0 -91
  16. package/lib/cjs/LiveMap.js +0 -325
  17. package/lib/cjs/LiveObject.d.ts +0 -80
  18. package/lib/cjs/LiveObject.js +0 -485
  19. package/lib/cjs/LiveRegister.d.ts +0 -29
  20. package/lib/cjs/LiveRegister.js +0 -88
  21. package/lib/cjs/client.d.ts +0 -27
  22. package/lib/cjs/client.js +0 -123
  23. package/lib/cjs/immutable.d.ts +0 -9
  24. package/lib/cjs/immutable.js +0 -299
  25. package/lib/cjs/index.d.ts +0 -6
  26. package/lib/cjs/index.js +0 -18
  27. package/lib/cjs/live.d.ts +0 -181
  28. package/lib/cjs/live.js +0 -49
  29. package/lib/cjs/position.d.ts +0 -6
  30. package/lib/cjs/position.js +0 -113
  31. package/lib/cjs/room.d.ts +0 -159
  32. package/lib/cjs/room.js +0 -1129
  33. package/lib/cjs/types.d.ts +0 -502
  34. package/lib/cjs/types.js +0 -2
  35. package/lib/cjs/utils.d.ts +0 -15
  36. package/lib/cjs/utils.js +0 -225
  37. package/lib/esm/AbstractCrdt.d.ts +0 -68
  38. package/lib/esm/AbstractCrdt.js +0 -91
  39. package/lib/esm/LiveList.d.ts +0 -144
  40. package/lib/esm/LiveList.js +0 -526
  41. package/lib/esm/LiveMap.d.ts +0 -91
  42. package/lib/esm/LiveMap.js +0 -321
  43. package/lib/esm/LiveObject.d.ts +0 -80
  44. package/lib/esm/LiveObject.js +0 -481
  45. package/lib/esm/LiveRegister.d.ts +0 -29
  46. package/lib/esm/LiveRegister.js +0 -84
  47. package/lib/esm/client.d.ts +0 -27
  48. package/lib/esm/client.js +0 -119
  49. package/lib/esm/immutable.d.ts +0 -9
  50. package/lib/esm/immutable.js +0 -290
  51. package/lib/esm/index.d.ts +0 -6
  52. package/lib/esm/live.js +0 -46
  53. package/lib/esm/position.d.ts +0 -6
  54. package/lib/esm/position.js +0 -106
  55. package/lib/esm/room.d.ts +0 -159
  56. package/lib/esm/room.js +0 -1123
  57. package/lib/esm/types.js +0 -1
  58. package/lib/esm/utils.d.ts +0 -15
  59. package/lib/esm/utils.js +0 -213
@@ -1,321 +0,0 @@
1
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
- if (kind === "m") throw new TypeError("Private method is not writable");
3
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
- 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");
5
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
- };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- 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");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
- var _LiveMap_map;
13
- import { AbstractCrdt } from "./AbstractCrdt";
14
- import { deserialize, isCrdt, selfOrRegister, selfOrRegisterValue, } from "./utils";
15
- import { OpType, CrdtType, } from "./live";
16
- /**
17
- * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
18
- * Keys should be a string, and values should be serializable to JSON.
19
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
20
- */
21
- export class LiveMap extends AbstractCrdt {
22
- constructor(entries) {
23
- super();
24
- _LiveMap_map.set(this, void 0);
25
- if (entries) {
26
- const mappedEntries = [];
27
- for (const entry of entries) {
28
- const value = selfOrRegister(entry[1]);
29
- value._setParentLink(this, entry[0]);
30
- mappedEntries.push([entry[0], value]);
31
- }
32
- __classPrivateFieldSet(this, _LiveMap_map, new Map(mappedEntries), "f");
33
- }
34
- else {
35
- __classPrivateFieldSet(this, _LiveMap_map, new Map(), "f");
36
- }
37
- }
38
- /**
39
- * INTERNAL
40
- */
41
- _serialize(parentId, parentKey, doc) {
42
- if (this._id == null) {
43
- throw new Error("Cannot serialize item is not attached");
44
- }
45
- if (parentId == null || parentKey == null) {
46
- throw new Error("Cannot serialize map if parentId or parentKey is undefined");
47
- }
48
- const ops = [];
49
- const op = {
50
- id: this._id,
51
- opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
52
- type: OpType.CreateMap,
53
- parentId,
54
- parentKey,
55
- };
56
- ops.push(op);
57
- for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
58
- ops.push(...value._serialize(this._id, key, doc));
59
- }
60
- return ops;
61
- }
62
- /**
63
- * INTERNAL
64
- */
65
- static _deserialize([id, item], parentToChildren, doc) {
66
- if (item.type !== CrdtType.Map) {
67
- throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
68
- }
69
- const map = new LiveMap();
70
- map._attach(id, doc);
71
- const children = parentToChildren.get(id);
72
- if (children == null) {
73
- return map;
74
- }
75
- for (const entry of children) {
76
- const crdt = entry[1];
77
- if (crdt.parentKey == null) {
78
- throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
79
- }
80
- const child = deserialize(entry, parentToChildren, doc);
81
- child._setParentLink(map, crdt.parentKey);
82
- __classPrivateFieldGet(map, _LiveMap_map, "f").set(crdt.parentKey, child);
83
- }
84
- return map;
85
- }
86
- /**
87
- * INTERNAL
88
- */
89
- _attach(id, doc) {
90
- super._attach(id, doc);
91
- for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
92
- if (isCrdt(value)) {
93
- value._attach(doc.generateId(), doc);
94
- }
95
- }
96
- }
97
- /**
98
- * INTERNAL
99
- */
100
- _attachChild(id, key, child, opId, isLocal) {
101
- if (this._doc == null) {
102
- throw new Error("Can't attach child if doc is not present");
103
- }
104
- if (this._doc.getItem(id) !== undefined) {
105
- return { modified: false };
106
- }
107
- const previousValue = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
108
- let reverse;
109
- if (previousValue) {
110
- reverse = previousValue._serialize(this._id, key);
111
- previousValue._detach();
112
- }
113
- else {
114
- reverse = [{ type: OpType.DeleteCrdt, id }];
115
- }
116
- child._setParentLink(this, key);
117
- child._attach(id, this._doc);
118
- __classPrivateFieldGet(this, _LiveMap_map, "f").set(key, child);
119
- return {
120
- modified: {
121
- node: this,
122
- type: "LiveMap",
123
- updates: { [key]: { type: "update" } },
124
- },
125
- reverse,
126
- };
127
- }
128
- /**
129
- * INTERNAL
130
- */
131
- _detach() {
132
- super._detach();
133
- for (const item of __classPrivateFieldGet(this, _LiveMap_map, "f").values()) {
134
- item._detach();
135
- }
136
- }
137
- /**
138
- * INTERNAL
139
- */
140
- _detachChild(child) {
141
- const reverse = child._serialize(this._id, child._parentKey, this._doc);
142
- for (const [key, value] of __classPrivateFieldGet(this, _LiveMap_map, "f")) {
143
- if (value === child) {
144
- __classPrivateFieldGet(this, _LiveMap_map, "f").delete(key);
145
- }
146
- }
147
- child._detach();
148
- const storageUpdate = {
149
- node: this,
150
- type: "LiveMap",
151
- updates: { [child._parentKey]: { type: "delete" } },
152
- };
153
- return { modified: storageUpdate, reverse };
154
- }
155
- /**
156
- * INTERNAL
157
- */
158
- _getType() {
159
- return "LiveMap";
160
- }
161
- /**
162
- * INTERNAL
163
- */
164
- _toSerializedCrdt() {
165
- var _a;
166
- return {
167
- type: CrdtType.Map,
168
- parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
169
- parentKey: this._parentKey,
170
- };
171
- }
172
- /**
173
- * Returns a specified element from the LiveMap.
174
- * @param key The key of the element to return.
175
- * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
176
- */
177
- get(key) {
178
- const value = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
179
- if (value == undefined) {
180
- return undefined;
181
- }
182
- return selfOrRegisterValue(value);
183
- }
184
- /**
185
- * Adds or updates an element with a specified key and a value.
186
- * @param key The key of the element to add. Should be a string.
187
- * @param value The value of the element to add. Should be serializable to JSON.
188
- */
189
- set(key, value) {
190
- const oldValue = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
191
- if (oldValue) {
192
- oldValue._detach();
193
- }
194
- const item = selfOrRegister(value);
195
- item._setParentLink(this, key);
196
- __classPrivateFieldGet(this, _LiveMap_map, "f").set(key, item);
197
- if (this._doc && this._id) {
198
- const id = this._doc.generateId();
199
- item._attach(id, this._doc);
200
- const storageUpdates = new Map();
201
- storageUpdates.set(this._id, {
202
- node: this,
203
- type: "LiveMap",
204
- updates: { [key]: { type: "update" } },
205
- });
206
- this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue
207
- ? oldValue._serialize(this._id, key)
208
- : [{ type: OpType.DeleteCrdt, id }], storageUpdates);
209
- }
210
- }
211
- /**
212
- * Returns the number of elements in the LiveMap.
213
- */
214
- get size() {
215
- return __classPrivateFieldGet(this, _LiveMap_map, "f").size;
216
- }
217
- /**
218
- * Returns a boolean indicating whether an element with the specified key exists or not.
219
- * @param key The key of the element to test for presence.
220
- */
221
- has(key) {
222
- return __classPrivateFieldGet(this, _LiveMap_map, "f").has(key);
223
- }
224
- /**
225
- * Removes the specified element by key.
226
- * @param key The key of the element to remove.
227
- * @returns true if an element existed and has been removed, or false if the element does not exist.
228
- */
229
- delete(key) {
230
- const item = __classPrivateFieldGet(this, _LiveMap_map, "f").get(key);
231
- if (item == null) {
232
- return false;
233
- }
234
- item._detach();
235
- if (this._doc && item._id) {
236
- const storageUpdates = new Map();
237
- storageUpdates.set(this._id, {
238
- node: this,
239
- type: "LiveMap",
240
- updates: { [key]: { type: "delete" } },
241
- });
242
- this._doc.dispatch([
243
- {
244
- type: OpType.DeleteCrdt,
245
- id: item._id,
246
- opId: this._doc.generateOpId(),
247
- },
248
- ], item._serialize(this._id, key), storageUpdates);
249
- }
250
- __classPrivateFieldGet(this, _LiveMap_map, "f").delete(key);
251
- return true;
252
- }
253
- /**
254
- * Returns a new Iterator object that contains the [key, value] pairs for each element.
255
- */
256
- entries() {
257
- const innerIterator = __classPrivateFieldGet(this, _LiveMap_map, "f").entries();
258
- return {
259
- [Symbol.iterator]: function () {
260
- return this;
261
- },
262
- next() {
263
- const iteratorValue = innerIterator.next();
264
- if (iteratorValue.done) {
265
- return {
266
- done: true,
267
- value: undefined,
268
- };
269
- }
270
- const entry = iteratorValue.value;
271
- return {
272
- value: [entry[0], selfOrRegisterValue(iteratorValue.value[1])],
273
- };
274
- },
275
- };
276
- }
277
- /**
278
- * Same function object as the initial value of the entries method.
279
- */
280
- [(_LiveMap_map = new WeakMap(), Symbol.iterator)]() {
281
- return this.entries();
282
- }
283
- /**
284
- * Returns a new Iterator object that contains the keys for each element.
285
- */
286
- keys() {
287
- return __classPrivateFieldGet(this, _LiveMap_map, "f").keys();
288
- }
289
- /**
290
- * Returns a new Iterator object that contains the values for each element.
291
- */
292
- values() {
293
- const innerIterator = __classPrivateFieldGet(this, _LiveMap_map, "f").values();
294
- return {
295
- [Symbol.iterator]: function () {
296
- return this;
297
- },
298
- next() {
299
- const iteratorValue = innerIterator.next();
300
- if (iteratorValue.done) {
301
- return {
302
- done: true,
303
- value: undefined,
304
- };
305
- }
306
- return {
307
- value: selfOrRegisterValue(iteratorValue.value),
308
- };
309
- },
310
- };
311
- }
312
- /**
313
- * Executes a provided function once per each key/value pair in the Map object, in insertion order.
314
- * @param callback Function to execute for each entry in the map.
315
- */
316
- forEach(callback) {
317
- for (const entry of this) {
318
- callback(entry[1], entry[0], this);
319
- }
320
- }
321
- }
@@ -1,80 +0,0 @@
1
- import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { Op, SerializedCrdt, SerializedCrdtWithId } from "./live";
3
- /**
4
- * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
5
- * Keys should be a string, and values should be serializable to JSON.
6
- * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
7
- */
8
- export declare class LiveObject<T extends Record<string, any> = Record<string, any>> extends AbstractCrdt {
9
- #private;
10
- constructor(object?: T);
11
- /**
12
- * INTERNAL
13
- */
14
- _serialize(parentId?: string, parentKey?: string, doc?: Doc): Op[];
15
- /**
16
- * INTERNAL
17
- */
18
- static _deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveObject<Record<string, any>>;
19
- /**
20
- * INTERNAL
21
- */
22
- static _deserializeChildren(object: LiveObject, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveObject<Record<string, any>>;
23
- /**
24
- * INTERNAL
25
- */
26
- _attach(id: string, doc: Doc): void;
27
- /**
28
- * INTERNAL
29
- */
30
- _attachChild(id: string, key: keyof T, child: AbstractCrdt, opId: string, isLocal: boolean): ApplyResult;
31
- /**
32
- * INTERNAL
33
- */
34
- _detachChild(child: AbstractCrdt): ApplyResult;
35
- /**
36
- * INTERNAL
37
- */
38
- _detachChildren(): void;
39
- /**
40
- * INTERNAL
41
- */
42
- _detach(): void;
43
- /**
44
- * INTERNAL
45
- */
46
- _apply(op: Op, isLocal: boolean): ApplyResult;
47
- /**
48
- * INTERNAL
49
- */
50
- _toSerializedCrdt(): SerializedCrdt;
51
- /**
52
- * INTERNAL
53
- */
54
- _getType(): string;
55
- /**
56
- * Transform the LiveObject into a javascript object
57
- */
58
- toObject(): T;
59
- /**
60
- * Adds or updates a property with a specified key and a value.
61
- * @param key The key of the property to add
62
- * @param value The value of the property to add
63
- */
64
- set<TKey extends keyof T>(key: TKey, value: T[TKey]): void;
65
- /**
66
- * Returns a specified property from the LiveObject.
67
- * @param key The key of the property to get
68
- */
69
- get<TKey extends keyof T>(key: TKey): T[TKey];
70
- /**
71
- * Deletes a key from the LiveObject
72
- * @param key The key of the property to delete
73
- */
74
- delete(key: keyof T): void;
75
- /**
76
- * Adds or updates multiple properties at once with an object.
77
- * @param overrides The object used to overrides properties
78
- */
79
- update(overrides: Partial<T>): void;
80
- }