@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.
- package/README.md +5 -10
- package/lib/esm/index.js +2991 -5
- package/lib/esm/index.mjs +2991 -0
- package/lib/esm/internal.js +149 -0
- package/lib/esm/internal.mjs +149 -0
- package/lib/{esm/types.d.ts → index.d.ts} +253 -61
- package/lib/index.js +4237 -0
- package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
- package/lib/internal.js +193 -0
- package/package.json +32 -10
- package/lib/cjs/AbstractCrdt.d.ts +0 -68
- package/lib/cjs/AbstractCrdt.js +0 -95
- package/lib/cjs/LiveList.d.ts +0 -144
- package/lib/cjs/LiveList.js +0 -530
- package/lib/cjs/LiveMap.d.ts +0 -91
- package/lib/cjs/LiveMap.js +0 -325
- package/lib/cjs/LiveObject.d.ts +0 -80
- package/lib/cjs/LiveObject.js +0 -485
- package/lib/cjs/LiveRegister.d.ts +0 -29
- package/lib/cjs/LiveRegister.js +0 -88
- package/lib/cjs/client.d.ts +0 -27
- package/lib/cjs/client.js +0 -123
- package/lib/cjs/immutable.d.ts +0 -9
- package/lib/cjs/immutable.js +0 -299
- package/lib/cjs/index.d.ts +0 -6
- package/lib/cjs/index.js +0 -18
- package/lib/cjs/live.d.ts +0 -181
- package/lib/cjs/live.js +0 -49
- package/lib/cjs/position.d.ts +0 -6
- package/lib/cjs/position.js +0 -113
- package/lib/cjs/room.d.ts +0 -159
- package/lib/cjs/room.js +0 -1129
- package/lib/cjs/types.d.ts +0 -502
- package/lib/cjs/types.js +0 -2
- package/lib/cjs/utils.d.ts +0 -15
- package/lib/cjs/utils.js +0 -225
- package/lib/esm/AbstractCrdt.d.ts +0 -68
- package/lib/esm/AbstractCrdt.js +0 -91
- package/lib/esm/LiveList.d.ts +0 -144
- package/lib/esm/LiveList.js +0 -526
- package/lib/esm/LiveMap.d.ts +0 -91
- package/lib/esm/LiveMap.js +0 -321
- package/lib/esm/LiveObject.d.ts +0 -80
- package/lib/esm/LiveObject.js +0 -481
- package/lib/esm/LiveRegister.d.ts +0 -29
- package/lib/esm/LiveRegister.js +0 -84
- package/lib/esm/client.d.ts +0 -27
- package/lib/esm/client.js +0 -119
- package/lib/esm/immutable.d.ts +0 -9
- package/lib/esm/immutable.js +0 -290
- package/lib/esm/index.d.ts +0 -6
- package/lib/esm/live.js +0 -46
- package/lib/esm/position.d.ts +0 -6
- package/lib/esm/position.js +0 -106
- package/lib/esm/room.d.ts +0 -159
- package/lib/esm/room.js +0 -1123
- package/lib/esm/types.js +0 -1
- package/lib/esm/utils.d.ts +0 -15
- package/lib/esm/utils.js +0 -213
package/lib/cjs/LiveObject.js
DELETED
|
@@ -1,485 +0,0 @@
|
|
|
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, doc) {
|
|
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
|
-
opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
|
|
49
|
-
type: live_1.OpType.CreateObject,
|
|
50
|
-
parentId,
|
|
51
|
-
parentKey,
|
|
52
|
-
data: {},
|
|
53
|
-
};
|
|
54
|
-
ops.push(op);
|
|
55
|
-
for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
|
|
56
|
-
if (value instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
57
|
-
ops.push(...value._serialize(this._id, key, doc));
|
|
58
|
-
}
|
|
59
|
-
else {
|
|
60
|
-
op.data[key] = value;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
return ops;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* INTERNAL
|
|
67
|
-
*/
|
|
68
|
-
static _deserialize([id, item], parentToChildren, doc) {
|
|
69
|
-
if (item.type !== live_1.CrdtType.Object) {
|
|
70
|
-
throw new Error(`Tried to deserialize a record but item type is "${item.type}"`);
|
|
71
|
-
}
|
|
72
|
-
const object = new LiveObject(item.data);
|
|
73
|
-
object._attach(id, doc);
|
|
74
|
-
return this._deserializeChildren(object, parentToChildren, doc);
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* INTERNAL
|
|
78
|
-
*/
|
|
79
|
-
static _deserializeChildren(object, parentToChildren, doc) {
|
|
80
|
-
const children = parentToChildren.get(object._id);
|
|
81
|
-
if (children == null) {
|
|
82
|
-
return object;
|
|
83
|
-
}
|
|
84
|
-
for (const entry of children) {
|
|
85
|
-
const crdt = entry[1];
|
|
86
|
-
if (crdt.parentKey == null) {
|
|
87
|
-
throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
|
|
88
|
-
}
|
|
89
|
-
const child = (0, utils_1.deserialize)(entry, parentToChildren, doc);
|
|
90
|
-
child._setParentLink(object, crdt.parentKey);
|
|
91
|
-
__classPrivateFieldGet(object, _LiveObject_map, "f").set(crdt.parentKey, child);
|
|
92
|
-
}
|
|
93
|
-
return object;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* INTERNAL
|
|
97
|
-
*/
|
|
98
|
-
_attach(id, doc) {
|
|
99
|
-
super._attach(id, doc);
|
|
100
|
-
for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
|
|
101
|
-
if (value instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
102
|
-
value._attach(doc.generateId(), doc);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* INTERNAL
|
|
108
|
-
*/
|
|
109
|
-
_attachChild(id, key, child, opId, isLocal) {
|
|
110
|
-
if (this._doc == null) {
|
|
111
|
-
throw new Error("Can't attach child if doc is not present");
|
|
112
|
-
}
|
|
113
|
-
if (this._doc.getItem(id) !== undefined) {
|
|
114
|
-
if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) === opId) {
|
|
115
|
-
// Acknowlegment from local operation
|
|
116
|
-
__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").delete(key);
|
|
117
|
-
}
|
|
118
|
-
return { modified: false };
|
|
119
|
-
}
|
|
120
|
-
if (isLocal) {
|
|
121
|
-
__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, opId);
|
|
122
|
-
}
|
|
123
|
-
else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) === undefined) {
|
|
124
|
-
// Remote operation with no local change => apply operation
|
|
125
|
-
}
|
|
126
|
-
else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) === opId) {
|
|
127
|
-
// Acknowlegment from local operation
|
|
128
|
-
__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").delete(key);
|
|
129
|
-
return { modified: false };
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
// Conflict, ignore remote operation
|
|
133
|
-
return { modified: false };
|
|
134
|
-
}
|
|
135
|
-
const previousValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
|
|
136
|
-
let reverse;
|
|
137
|
-
if ((0, utils_1.isCrdt)(previousValue)) {
|
|
138
|
-
reverse = previousValue._serialize(this._id, key);
|
|
139
|
-
previousValue._detach();
|
|
140
|
-
}
|
|
141
|
-
else if (previousValue === undefined) {
|
|
142
|
-
reverse = [
|
|
143
|
-
{ type: live_1.OpType.DeleteObjectKey, id: this._id, key: key },
|
|
144
|
-
];
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
reverse = [
|
|
148
|
-
{
|
|
149
|
-
type: live_1.OpType.UpdateObject,
|
|
150
|
-
id: this._id,
|
|
151
|
-
data: { [key]: previousValue },
|
|
152
|
-
},
|
|
153
|
-
];
|
|
154
|
-
}
|
|
155
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").set(key, child);
|
|
156
|
-
child._setParentLink(this, key);
|
|
157
|
-
child._attach(id, this._doc);
|
|
158
|
-
return {
|
|
159
|
-
reverse,
|
|
160
|
-
modified: {
|
|
161
|
-
node: this,
|
|
162
|
-
type: "LiveObject",
|
|
163
|
-
updates: { [key]: { type: "update" } },
|
|
164
|
-
},
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* INTERNAL
|
|
169
|
-
*/
|
|
170
|
-
_detachChild(child) {
|
|
171
|
-
if (child) {
|
|
172
|
-
const reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
173
|
-
for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
|
|
174
|
-
if (value === child) {
|
|
175
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
child._detach();
|
|
179
|
-
const storageUpdate = {
|
|
180
|
-
node: this,
|
|
181
|
-
type: "LiveObject",
|
|
182
|
-
updates: {
|
|
183
|
-
[child._parentKey]: { type: "delete" },
|
|
184
|
-
},
|
|
185
|
-
};
|
|
186
|
-
return { modified: storageUpdate, reverse };
|
|
187
|
-
}
|
|
188
|
-
return { modified: false };
|
|
189
|
-
}
|
|
190
|
-
/**
|
|
191
|
-
* INTERNAL
|
|
192
|
-
*/
|
|
193
|
-
_detachChildren() {
|
|
194
|
-
for (const [key, value] of __classPrivateFieldGet(this, _LiveObject_map, "f")) {
|
|
195
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
|
|
196
|
-
value._detach();
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/**
|
|
200
|
-
* INTERNAL
|
|
201
|
-
*/
|
|
202
|
-
_detach() {
|
|
203
|
-
super._detach();
|
|
204
|
-
for (const value of __classPrivateFieldGet(this, _LiveObject_map, "f").values()) {
|
|
205
|
-
if ((0, utils_1.isCrdt)(value)) {
|
|
206
|
-
value._detach();
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* INTERNAL
|
|
212
|
-
*/
|
|
213
|
-
_apply(op, isLocal) {
|
|
214
|
-
if (op.type === live_1.OpType.UpdateObject) {
|
|
215
|
-
return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyUpdate).call(this, op, isLocal);
|
|
216
|
-
}
|
|
217
|
-
else if (op.type === live_1.OpType.DeleteObjectKey) {
|
|
218
|
-
return __classPrivateFieldGet(this, _LiveObject_instances, "m", _LiveObject_applyDeleteObjectKey).call(this, op);
|
|
219
|
-
}
|
|
220
|
-
return super._apply(op, isLocal);
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* INTERNAL
|
|
224
|
-
*/
|
|
225
|
-
_toSerializedCrdt() {
|
|
226
|
-
var _a;
|
|
227
|
-
return {
|
|
228
|
-
type: live_1.CrdtType.Object,
|
|
229
|
-
parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
|
|
230
|
-
parentKey: this._parentKey,
|
|
231
|
-
data: this.toObject(),
|
|
232
|
-
};
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* INTERNAL
|
|
236
|
-
*/
|
|
237
|
-
_getType() {
|
|
238
|
-
return "LiveObject";
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Transform the LiveObject into a javascript object
|
|
242
|
-
*/
|
|
243
|
-
toObject() {
|
|
244
|
-
return Object.fromEntries(__classPrivateFieldGet(this, _LiveObject_map, "f"));
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Adds or updates a property with a specified key and a value.
|
|
248
|
-
* @param key The key of the property to add
|
|
249
|
-
* @param value The value of the property to add
|
|
250
|
-
*/
|
|
251
|
-
set(key, value) {
|
|
252
|
-
// TODO: Find out why typescript complains
|
|
253
|
-
this.update({ [key]: value });
|
|
254
|
-
}
|
|
255
|
-
/**
|
|
256
|
-
* Returns a specified property from the LiveObject.
|
|
257
|
-
* @param key The key of the property to get
|
|
258
|
-
*/
|
|
259
|
-
get(key) {
|
|
260
|
-
return __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Deletes a key from the LiveObject
|
|
264
|
-
* @param key The key of the property to delete
|
|
265
|
-
*/
|
|
266
|
-
delete(key) {
|
|
267
|
-
const keyAsString = key;
|
|
268
|
-
const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(keyAsString);
|
|
269
|
-
if (oldValue === undefined) {
|
|
270
|
-
return;
|
|
271
|
-
}
|
|
272
|
-
if (this._doc == null || this._id == null) {
|
|
273
|
-
if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
274
|
-
oldValue._detach();
|
|
275
|
-
}
|
|
276
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
let reverse;
|
|
280
|
-
if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
281
|
-
oldValue._detach();
|
|
282
|
-
reverse = oldValue._serialize(this._id, keyAsString);
|
|
283
|
-
}
|
|
284
|
-
else {
|
|
285
|
-
reverse = [
|
|
286
|
-
{
|
|
287
|
-
type: live_1.OpType.UpdateObject,
|
|
288
|
-
data: { [keyAsString]: oldValue },
|
|
289
|
-
id: this._id,
|
|
290
|
-
},
|
|
291
|
-
];
|
|
292
|
-
}
|
|
293
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").delete(keyAsString);
|
|
294
|
-
const storageUpdates = new Map();
|
|
295
|
-
storageUpdates.set(this._id, {
|
|
296
|
-
node: this,
|
|
297
|
-
type: "LiveObject",
|
|
298
|
-
updates: { [key]: { type: "delete" } },
|
|
299
|
-
});
|
|
300
|
-
this._doc.dispatch([
|
|
301
|
-
{
|
|
302
|
-
type: live_1.OpType.DeleteObjectKey,
|
|
303
|
-
key: keyAsString,
|
|
304
|
-
id: this._id,
|
|
305
|
-
opId: this._doc.generateOpId(),
|
|
306
|
-
},
|
|
307
|
-
], reverse, storageUpdates);
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Adds or updates multiple properties at once with an object.
|
|
311
|
-
* @param overrides The object used to overrides properties
|
|
312
|
-
*/
|
|
313
|
-
update(overrides) {
|
|
314
|
-
if (this._doc == null || this._id == null) {
|
|
315
|
-
for (const key in overrides) {
|
|
316
|
-
const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
|
|
317
|
-
if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
318
|
-
oldValue._detach();
|
|
319
|
-
}
|
|
320
|
-
const newValue = overrides[key];
|
|
321
|
-
if (newValue instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
322
|
-
newValue._setParentLink(this, key);
|
|
323
|
-
}
|
|
324
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
|
|
325
|
-
}
|
|
326
|
-
return;
|
|
327
|
-
}
|
|
328
|
-
const ops = [];
|
|
329
|
-
const reverseOps = [];
|
|
330
|
-
const opId = this._doc.generateOpId();
|
|
331
|
-
const updatedProps = {};
|
|
332
|
-
const reverseUpdateOp = {
|
|
333
|
-
id: this._id,
|
|
334
|
-
type: live_1.OpType.UpdateObject,
|
|
335
|
-
data: {},
|
|
336
|
-
};
|
|
337
|
-
const updateDelta = {};
|
|
338
|
-
for (const key in overrides) {
|
|
339
|
-
const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
|
|
340
|
-
if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
341
|
-
reverseOps.push(...oldValue._serialize(this._id, key));
|
|
342
|
-
oldValue._detach();
|
|
343
|
-
}
|
|
344
|
-
else if (oldValue === undefined) {
|
|
345
|
-
reverseOps.push({ type: live_1.OpType.DeleteObjectKey, id: this._id, key });
|
|
346
|
-
}
|
|
347
|
-
else {
|
|
348
|
-
reverseUpdateOp.data[key] = oldValue;
|
|
349
|
-
}
|
|
350
|
-
const newValue = overrides[key];
|
|
351
|
-
if (newValue instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
352
|
-
newValue._setParentLink(this, key);
|
|
353
|
-
newValue._attach(this._doc.generateId(), this._doc);
|
|
354
|
-
const newAttachChildOps = newValue._serialize(this._id, key, this._doc);
|
|
355
|
-
const createCrdtOp = newAttachChildOps.find((op) => op.parentId === this._id);
|
|
356
|
-
if (createCrdtOp) {
|
|
357
|
-
__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, createCrdtOp.opId);
|
|
358
|
-
}
|
|
359
|
-
ops.push(...newAttachChildOps);
|
|
360
|
-
}
|
|
361
|
-
else {
|
|
362
|
-
updatedProps[key] = newValue;
|
|
363
|
-
__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, opId);
|
|
364
|
-
}
|
|
365
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").set(key, newValue);
|
|
366
|
-
updateDelta[key] = { type: "update" };
|
|
367
|
-
}
|
|
368
|
-
if (Object.keys(reverseUpdateOp.data).length !== 0) {
|
|
369
|
-
reverseOps.unshift(reverseUpdateOp);
|
|
370
|
-
}
|
|
371
|
-
if (Object.keys(updatedProps).length !== 0) {
|
|
372
|
-
ops.unshift({
|
|
373
|
-
opId,
|
|
374
|
-
id: this._id,
|
|
375
|
-
type: live_1.OpType.UpdateObject,
|
|
376
|
-
data: updatedProps,
|
|
377
|
-
});
|
|
378
|
-
}
|
|
379
|
-
const storageUpdates = new Map();
|
|
380
|
-
storageUpdates.set(this._id, {
|
|
381
|
-
node: this,
|
|
382
|
-
type: "LiveObject",
|
|
383
|
-
updates: updateDelta,
|
|
384
|
-
});
|
|
385
|
-
this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
exports.LiveObject = LiveObject;
|
|
389
|
-
_LiveObject_map = new WeakMap(), _LiveObject_propToLastUpdate = new WeakMap(), _LiveObject_instances = new WeakSet(), _LiveObject_applyUpdate = function _LiveObject_applyUpdate(op, isLocal) {
|
|
390
|
-
let isModified = false;
|
|
391
|
-
const reverse = [];
|
|
392
|
-
const reverseUpdate = {
|
|
393
|
-
type: live_1.OpType.UpdateObject,
|
|
394
|
-
id: this._id,
|
|
395
|
-
data: {},
|
|
396
|
-
};
|
|
397
|
-
reverse.push(reverseUpdate);
|
|
398
|
-
for (const key in op.data) {
|
|
399
|
-
const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
|
|
400
|
-
if (oldValue instanceof AbstractCrdt_1.AbstractCrdt) {
|
|
401
|
-
reverse.push(...oldValue._serialize(this._id, key));
|
|
402
|
-
oldValue._detach();
|
|
403
|
-
}
|
|
404
|
-
else if (oldValue !== undefined) {
|
|
405
|
-
reverseUpdate.data[key] = oldValue;
|
|
406
|
-
}
|
|
407
|
-
else if (oldValue === undefined) {
|
|
408
|
-
reverse.push({ type: live_1.OpType.DeleteObjectKey, id: this._id, key });
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
let updateDelta = {};
|
|
412
|
-
for (const key in op.data) {
|
|
413
|
-
if (isLocal) {
|
|
414
|
-
__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").set(key, op.opId);
|
|
415
|
-
}
|
|
416
|
-
else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) == null) {
|
|
417
|
-
// Not modified localy so we apply update
|
|
418
|
-
isModified = true;
|
|
419
|
-
}
|
|
420
|
-
else if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) === op.opId) {
|
|
421
|
-
// Acknowlegment from local operation
|
|
422
|
-
__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").delete(key);
|
|
423
|
-
continue;
|
|
424
|
-
}
|
|
425
|
-
else {
|
|
426
|
-
// Conflict, ignore remote operation
|
|
427
|
-
continue;
|
|
428
|
-
}
|
|
429
|
-
const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
|
|
430
|
-
if ((0, utils_1.isCrdt)(oldValue)) {
|
|
431
|
-
oldValue._detach();
|
|
432
|
-
}
|
|
433
|
-
isModified = true;
|
|
434
|
-
updateDelta[key] = { type: "update" };
|
|
435
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").set(key, op.data[key]);
|
|
436
|
-
}
|
|
437
|
-
if (Object.keys(reverseUpdate.data).length !== 0) {
|
|
438
|
-
reverse.unshift(reverseUpdate);
|
|
439
|
-
}
|
|
440
|
-
return isModified
|
|
441
|
-
? {
|
|
442
|
-
modified: {
|
|
443
|
-
node: this,
|
|
444
|
-
type: "LiveObject",
|
|
445
|
-
updates: updateDelta,
|
|
446
|
-
},
|
|
447
|
-
reverse,
|
|
448
|
-
}
|
|
449
|
-
: { modified: false };
|
|
450
|
-
}, _LiveObject_applyDeleteObjectKey = function _LiveObject_applyDeleteObjectKey(op) {
|
|
451
|
-
const key = op.key;
|
|
452
|
-
// If property does not exist, exit without notifying
|
|
453
|
-
if (__classPrivateFieldGet(this, _LiveObject_map, "f").has(key) === false) {
|
|
454
|
-
return { modified: false };
|
|
455
|
-
}
|
|
456
|
-
// If a local operation exists on the same key
|
|
457
|
-
// prevent flickering by not applying delete op.
|
|
458
|
-
if (__classPrivateFieldGet(this, _LiveObject_propToLastUpdate, "f").get(key) !== undefined) {
|
|
459
|
-
return { modified: false };
|
|
460
|
-
}
|
|
461
|
-
const oldValue = __classPrivateFieldGet(this, _LiveObject_map, "f").get(key);
|
|
462
|
-
let reverse = [];
|
|
463
|
-
if ((0, utils_1.isCrdt)(oldValue)) {
|
|
464
|
-
reverse = oldValue._serialize(this._id, op.key);
|
|
465
|
-
oldValue._detach();
|
|
466
|
-
}
|
|
467
|
-
else if (oldValue !== undefined) {
|
|
468
|
-
reverse = [
|
|
469
|
-
{
|
|
470
|
-
type: live_1.OpType.UpdateObject,
|
|
471
|
-
id: this._id,
|
|
472
|
-
data: { [key]: oldValue },
|
|
473
|
-
},
|
|
474
|
-
];
|
|
475
|
-
}
|
|
476
|
-
__classPrivateFieldGet(this, _LiveObject_map, "f").delete(key);
|
|
477
|
-
return {
|
|
478
|
-
modified: {
|
|
479
|
-
node: this,
|
|
480
|
-
type: "LiveObject",
|
|
481
|
-
updates: { [op.key]: { type: "delete" } },
|
|
482
|
-
},
|
|
483
|
-
reverse,
|
|
484
|
-
};
|
|
485
|
-
};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
|
|
2
|
-
import { SerializedCrdtWithId, Op, SerializedCrdt } 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, doc?: Doc): Op[];
|
|
18
|
-
/**
|
|
19
|
-
* INTERNAL
|
|
20
|
-
*/
|
|
21
|
-
_toSerializedCrdt(): SerializedCrdt;
|
|
22
|
-
_attachChild(id: string, key: string, crdt: AbstractCrdt, opId: string, isLocal: boolean): ApplyResult;
|
|
23
|
-
_detachChild(crdt: AbstractCrdt): ApplyResult;
|
|
24
|
-
_apply(op: Op, isLocal: boolean): ApplyResult;
|
|
25
|
-
/**
|
|
26
|
-
* INTERNAL
|
|
27
|
-
*/
|
|
28
|
-
_getType(): string;
|
|
29
|
-
}
|
package/lib/cjs/LiveRegister.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
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, doc) {
|
|
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
|
-
opId: doc === null || doc === void 0 ? void 0 : doc.generateOpId(),
|
|
52
|
-
id: this._id,
|
|
53
|
-
parentId,
|
|
54
|
-
parentKey,
|
|
55
|
-
data: this.data,
|
|
56
|
-
},
|
|
57
|
-
];
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* INTERNAL
|
|
61
|
-
*/
|
|
62
|
-
_toSerializedCrdt() {
|
|
63
|
-
var _a;
|
|
64
|
-
return {
|
|
65
|
-
type: live_1.CrdtType.Register,
|
|
66
|
-
parentId: (_a = this._parent) === null || _a === void 0 ? void 0 : _a._id,
|
|
67
|
-
parentKey: this._parentKey,
|
|
68
|
-
data: this.data,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
_attachChild(id, key, crdt, opId, isLocal) {
|
|
72
|
-
throw new Error("Method not implemented.");
|
|
73
|
-
}
|
|
74
|
-
_detachChild(crdt) {
|
|
75
|
-
throw new Error("Method not implemented.");
|
|
76
|
-
}
|
|
77
|
-
_apply(op, isLocal) {
|
|
78
|
-
return super._apply(op, isLocal);
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* INTERNAL
|
|
82
|
-
*/
|
|
83
|
-
_getType() {
|
|
84
|
-
return "LiveRegister";
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
exports.LiveRegister = LiveRegister;
|
|
88
|
-
_LiveRegister_data = new WeakMap();
|
package/lib/cjs/client.d.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { ClientOptions, Client } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* Create a client that will be responsible to communicate with liveblocks servers.
|
|
4
|
-
*
|
|
5
|
-
* @example
|
|
6
|
-
* const client = createClient({
|
|
7
|
-
* authEndpoint: "/api/auth"
|
|
8
|
-
* });
|
|
9
|
-
*
|
|
10
|
-
* // It's also possible to use a function to call your authentication endpoint.
|
|
11
|
-
* // Useful to add additional headers or use an API wrapper (like Firebase functions)
|
|
12
|
-
* const client = createClient({
|
|
13
|
-
* authEndpoint: async (room) => {
|
|
14
|
-
* const response = await fetch("/api/auth", {
|
|
15
|
-
* method: "POST",
|
|
16
|
-
* headers: {
|
|
17
|
-
* Authentication: "token",
|
|
18
|
-
* "Content-Type": "application/json"
|
|
19
|
-
* },
|
|
20
|
-
* body: JSON.stringify({ room })
|
|
21
|
-
* });
|
|
22
|
-
*
|
|
23
|
-
* return await response.json();
|
|
24
|
-
* }
|
|
25
|
-
* });
|
|
26
|
-
*/
|
|
27
|
-
export declare function createClient(options: ClientOptions): Client;
|