@liveblocks/client 0.15.10 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -3
- package/lib/esm/index.js +438 -453
- package/lib/esm/index.mjs +438 -453
- package/lib/index.d.ts +204 -160
- package/lib/index.js +563 -557
- package/lib/internal.d.ts +29 -15
- package/package.json +8 -4
package/lib/esm/index.js
CHANGED
|
@@ -46,48 +46,23 @@ var WebsocketCloseCodes = /* @__PURE__ */ ((WebsocketCloseCodes2) => {
|
|
|
46
46
|
return WebsocketCloseCodes2;
|
|
47
47
|
})(WebsocketCloseCodes || {});
|
|
48
48
|
|
|
49
|
-
var __accessCheck$4 = (obj, member, msg) => {
|
|
50
|
-
if (!member.has(obj))
|
|
51
|
-
throw TypeError("Cannot " + msg);
|
|
52
|
-
};
|
|
53
|
-
var __privateGet$4 = (obj, member, getter) => {
|
|
54
|
-
__accessCheck$4(obj, member, "read from private field");
|
|
55
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
56
|
-
};
|
|
57
|
-
var __privateAdd$4 = (obj, member, value) => {
|
|
58
|
-
if (member.has(obj))
|
|
59
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
60
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
61
|
-
};
|
|
62
|
-
var __privateSet$4 = (obj, member, value, setter) => {
|
|
63
|
-
__accessCheck$4(obj, member, "write to private field");
|
|
64
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
65
|
-
return value;
|
|
66
|
-
};
|
|
67
|
-
var _parent, _doc, _id, _parentKey;
|
|
68
49
|
class AbstractCrdt {
|
|
69
|
-
constructor() {
|
|
70
|
-
__privateAdd$4(this, _parent, void 0);
|
|
71
|
-
__privateAdd$4(this, _doc, void 0);
|
|
72
|
-
__privateAdd$4(this, _id, void 0);
|
|
73
|
-
__privateAdd$4(this, _parentKey, void 0);
|
|
74
|
-
}
|
|
75
50
|
get _doc() {
|
|
76
|
-
return
|
|
51
|
+
return this.__doc;
|
|
77
52
|
}
|
|
78
53
|
get roomId() {
|
|
79
|
-
return
|
|
54
|
+
return this.__doc ? this.__doc.roomId : null;
|
|
80
55
|
}
|
|
81
56
|
get _id() {
|
|
82
|
-
return
|
|
57
|
+
return this.__id;
|
|
83
58
|
}
|
|
84
59
|
get _parent() {
|
|
85
|
-
return
|
|
60
|
+
return this.__parent;
|
|
86
61
|
}
|
|
87
62
|
get _parentKey() {
|
|
88
|
-
return
|
|
63
|
+
return this.__parentKey;
|
|
89
64
|
}
|
|
90
|
-
_apply(op,
|
|
65
|
+
_apply(op, _isLocal) {
|
|
91
66
|
switch (op.type) {
|
|
92
67
|
case OpType.DeleteCrdt: {
|
|
93
68
|
if (this._parent != null && this._parentKey != null) {
|
|
@@ -99,32 +74,28 @@ class AbstractCrdt {
|
|
|
99
74
|
return { modified: false };
|
|
100
75
|
}
|
|
101
76
|
_setParentLink(parent, key) {
|
|
102
|
-
if (
|
|
77
|
+
if (this.__parent != null && this.__parent !== parent) {
|
|
103
78
|
throw new Error("Cannot attach parent if it already exist");
|
|
104
79
|
}
|
|
105
|
-
|
|
106
|
-
|
|
80
|
+
this.__parentKey = key;
|
|
81
|
+
this.__parent = parent;
|
|
107
82
|
}
|
|
108
83
|
_attach(id, doc) {
|
|
109
|
-
if (
|
|
84
|
+
if (this.__id || this.__doc) {
|
|
110
85
|
throw new Error("Cannot attach if CRDT is already attached");
|
|
111
86
|
}
|
|
112
87
|
doc.addItem(id, this);
|
|
113
|
-
|
|
114
|
-
|
|
88
|
+
this.__id = id;
|
|
89
|
+
this.__doc = doc;
|
|
115
90
|
}
|
|
116
91
|
_detach() {
|
|
117
|
-
if (
|
|
118
|
-
|
|
92
|
+
if (this.__doc && this.__id) {
|
|
93
|
+
this.__doc.deleteItem(this.__id);
|
|
119
94
|
}
|
|
120
|
-
|
|
121
|
-
|
|
95
|
+
this.__parent = void 0;
|
|
96
|
+
this.__doc = void 0;
|
|
122
97
|
}
|
|
123
98
|
}
|
|
124
|
-
_parent = new WeakMap();
|
|
125
|
-
_doc = new WeakMap();
|
|
126
|
-
_id = new WeakMap();
|
|
127
|
-
_parentKey = new WeakMap();
|
|
128
99
|
|
|
129
100
|
const min = 32;
|
|
130
101
|
const max = 126;
|
|
@@ -227,43 +198,23 @@ function compare(posA, posB) {
|
|
|
227
198
|
throw new Error(`Impossible to compare similar position "${posA}" and "${posB}"`);
|
|
228
199
|
}
|
|
229
200
|
|
|
230
|
-
|
|
231
|
-
if (!member.has(obj))
|
|
232
|
-
throw TypeError("Cannot " + msg);
|
|
233
|
-
};
|
|
234
|
-
var __privateGet$3 = (obj, member, getter) => {
|
|
235
|
-
__accessCheck$3(obj, member, "read from private field");
|
|
236
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
237
|
-
};
|
|
238
|
-
var __privateAdd$3 = (obj, member, value) => {
|
|
239
|
-
if (member.has(obj))
|
|
240
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
241
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
242
|
-
};
|
|
243
|
-
var __privateSet$3 = (obj, member, value, setter) => {
|
|
244
|
-
__accessCheck$3(obj, member, "write to private field");
|
|
245
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
246
|
-
return value;
|
|
247
|
-
};
|
|
248
|
-
var _data;
|
|
249
|
-
const _LiveRegister = class extends AbstractCrdt {
|
|
201
|
+
class LiveRegister extends AbstractCrdt {
|
|
250
202
|
constructor(data) {
|
|
251
203
|
super();
|
|
252
|
-
|
|
253
|
-
__privateSet$3(this, _data, data);
|
|
204
|
+
this._data = data;
|
|
254
205
|
}
|
|
255
206
|
get data() {
|
|
256
|
-
return
|
|
207
|
+
return this._data;
|
|
257
208
|
}
|
|
258
|
-
static _deserialize([id, item],
|
|
209
|
+
static _deserialize([id, item], _parentToChildren, doc) {
|
|
259
210
|
if (item.type !== CrdtType.Register) {
|
|
260
211
|
throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
|
|
261
212
|
}
|
|
262
|
-
const register = new
|
|
213
|
+
const register = new LiveRegister(item.data);
|
|
263
214
|
register._attach(id, doc);
|
|
264
215
|
return register;
|
|
265
216
|
}
|
|
266
|
-
_serialize(parentId, parentKey, doc) {
|
|
217
|
+
_serialize(parentId, parentKey, doc, intent) {
|
|
267
218
|
if (this._id == null || parentId == null || parentKey == null) {
|
|
268
219
|
throw new Error("Cannot serialize register if parentId or parentKey is undefined");
|
|
269
220
|
}
|
|
@@ -272,6 +223,7 @@ const _LiveRegister = class extends AbstractCrdt {
|
|
|
272
223
|
type: OpType.CreateRegister,
|
|
273
224
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
274
225
|
id: this._id,
|
|
226
|
+
intent,
|
|
275
227
|
parentId,
|
|
276
228
|
parentKey,
|
|
277
229
|
data: this.data
|
|
@@ -287,52 +239,31 @@ const _LiveRegister = class extends AbstractCrdt {
|
|
|
287
239
|
data: this.data
|
|
288
240
|
};
|
|
289
241
|
}
|
|
290
|
-
_attachChild(
|
|
242
|
+
_attachChild(_op, _isLocal) {
|
|
291
243
|
throw new Error("Method not implemented.");
|
|
292
244
|
}
|
|
293
|
-
_detachChild(
|
|
245
|
+
_detachChild(_crdt) {
|
|
294
246
|
throw new Error("Method not implemented.");
|
|
295
247
|
}
|
|
296
248
|
_apply(op, isLocal) {
|
|
297
249
|
return super._apply(op, isLocal);
|
|
298
250
|
}
|
|
299
|
-
}
|
|
300
|
-
let LiveRegister = _LiveRegister;
|
|
301
|
-
_data = new WeakMap();
|
|
251
|
+
}
|
|
302
252
|
|
|
303
|
-
|
|
304
|
-
if (!member.has(obj))
|
|
305
|
-
throw TypeError("Cannot " + msg);
|
|
306
|
-
};
|
|
307
|
-
var __privateGet$2 = (obj, member, getter) => {
|
|
308
|
-
__accessCheck$2(obj, member, "read from private field");
|
|
309
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
310
|
-
};
|
|
311
|
-
var __privateAdd$2 = (obj, member, value) => {
|
|
312
|
-
if (member.has(obj))
|
|
313
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
314
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
315
|
-
};
|
|
316
|
-
var __privateSet$2 = (obj, member, value, setter) => {
|
|
317
|
-
__accessCheck$2(obj, member, "write to private field");
|
|
318
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
319
|
-
return value;
|
|
320
|
-
};
|
|
321
|
-
var _items, _innerIterator;
|
|
322
|
-
const _LiveList = class extends AbstractCrdt {
|
|
253
|
+
class LiveList extends AbstractCrdt {
|
|
323
254
|
constructor(items = []) {
|
|
324
255
|
super();
|
|
325
|
-
|
|
256
|
+
this._items = [];
|
|
326
257
|
let position = void 0;
|
|
327
258
|
for (let i = 0; i < items.length; i++) {
|
|
328
259
|
const newPosition = makePosition(position);
|
|
329
260
|
const item = selfOrRegister(items[i]);
|
|
330
|
-
|
|
261
|
+
this._items.push([item, newPosition]);
|
|
331
262
|
position = newPosition;
|
|
332
263
|
}
|
|
333
264
|
}
|
|
334
|
-
static _deserialize([id
|
|
335
|
-
const list = new
|
|
265
|
+
static _deserialize([id], parentToChildren, doc) {
|
|
266
|
+
const list = new LiveList([]);
|
|
336
267
|
list._attach(id, doc);
|
|
337
268
|
const children = parentToChildren.get(id);
|
|
338
269
|
if (children == null) {
|
|
@@ -341,12 +272,12 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
341
272
|
for (const entry of children) {
|
|
342
273
|
const child = deserialize(entry, parentToChildren, doc);
|
|
343
274
|
child._setParentLink(list, entry[1].parentKey);
|
|
344
|
-
|
|
345
|
-
|
|
275
|
+
list._items.push([child, entry[1].parentKey]);
|
|
276
|
+
list._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
|
|
346
277
|
}
|
|
347
278
|
return list;
|
|
348
279
|
}
|
|
349
|
-
_serialize(parentId, parentKey, doc) {
|
|
280
|
+
_serialize(parentId, parentKey, doc, intent) {
|
|
350
281
|
if (this._id == null) {
|
|
351
282
|
throw new Error("Cannot serialize item is not attached");
|
|
352
283
|
}
|
|
@@ -357,56 +288,79 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
357
288
|
const op = {
|
|
358
289
|
id: this._id,
|
|
359
290
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
291
|
+
intent,
|
|
360
292
|
type: OpType.CreateList,
|
|
361
293
|
parentId,
|
|
362
294
|
parentKey
|
|
363
295
|
};
|
|
364
296
|
ops.push(op);
|
|
365
|
-
for (const [value, key] of
|
|
297
|
+
for (const [value, key] of this._items) {
|
|
366
298
|
ops.push(...value._serialize(this._id, key, doc));
|
|
367
299
|
}
|
|
368
300
|
return ops;
|
|
369
301
|
}
|
|
370
302
|
_indexOfPosition(position) {
|
|
371
|
-
return
|
|
303
|
+
return this._items.findIndex((item) => item[1] === position);
|
|
372
304
|
}
|
|
373
305
|
_attach(id, doc) {
|
|
374
306
|
super._attach(id, doc);
|
|
375
|
-
for (const [item
|
|
307
|
+
for (const [item] of this._items) {
|
|
376
308
|
item._attach(doc.generateId(), doc);
|
|
377
309
|
}
|
|
378
310
|
}
|
|
379
311
|
_detach() {
|
|
380
312
|
super._detach();
|
|
381
|
-
for (const [value] of
|
|
313
|
+
for (const [value] of this._items) {
|
|
382
314
|
value._detach();
|
|
383
315
|
}
|
|
384
316
|
}
|
|
385
|
-
_attachChild(
|
|
317
|
+
_attachChild(op, isLocal) {
|
|
386
318
|
var _a;
|
|
387
319
|
if (this._doc == null) {
|
|
388
320
|
throw new Error("Can't attach child if doc is not present");
|
|
389
321
|
}
|
|
322
|
+
const { id, parentKey, intent } = op;
|
|
323
|
+
const key = parentKey;
|
|
324
|
+
const child = creationOpToLiveStructure(op);
|
|
390
325
|
if (this._doc.getItem(id) !== void 0) {
|
|
391
326
|
return { modified: false };
|
|
392
327
|
}
|
|
393
328
|
child._attach(id, this._doc);
|
|
394
329
|
child._setParentLink(this, key);
|
|
395
|
-
const index =
|
|
330
|
+
const index = this._items.findIndex((entry) => entry[1] === key);
|
|
396
331
|
let newKey = key;
|
|
397
332
|
if (index !== -1) {
|
|
398
|
-
if (
|
|
399
|
-
|
|
400
|
-
|
|
333
|
+
if (intent === "set") {
|
|
334
|
+
const existingItem = this._items[index][0];
|
|
335
|
+
existingItem._detach();
|
|
336
|
+
const storageUpdate = {
|
|
337
|
+
node: this,
|
|
338
|
+
type: "LiveList",
|
|
339
|
+
updates: [
|
|
340
|
+
{
|
|
341
|
+
index,
|
|
342
|
+
type: "set",
|
|
343
|
+
item: child instanceof LiveRegister ? child.data : child
|
|
344
|
+
}
|
|
345
|
+
]
|
|
346
|
+
};
|
|
347
|
+
this._items[index][0] = child;
|
|
348
|
+
return {
|
|
349
|
+
modified: storageUpdate,
|
|
350
|
+
reverse: existingItem._serialize(this._id, key, this._doc, "set")
|
|
351
|
+
};
|
|
352
|
+
} else if (isLocal) {
|
|
353
|
+
const before = this._items[index] ? this._items[index][1] : void 0;
|
|
354
|
+
const after = this._items[index + 1] ? this._items[index + 1][1] : void 0;
|
|
401
355
|
newKey = makePosition(before, after);
|
|
402
356
|
child._setParentLink(this, newKey);
|
|
403
357
|
} else {
|
|
404
|
-
|
|
358
|
+
this._items[index][1] = makePosition(key, (_a = this._items[index + 1]) == null ? void 0 : _a[1]);
|
|
405
359
|
}
|
|
406
360
|
}
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const newIndex =
|
|
361
|
+
this._items.push([child, newKey]);
|
|
362
|
+
this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
|
|
363
|
+
const newIndex = this._items.findIndex((entry) => entry[1] === newKey);
|
|
410
364
|
return {
|
|
411
365
|
reverse: [{ type: OpType.DeleteCrdt, id }],
|
|
412
366
|
modified: {
|
|
@@ -425,8 +379,8 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
425
379
|
_detachChild(child) {
|
|
426
380
|
if (child) {
|
|
427
381
|
const reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
428
|
-
const indexToDelete =
|
|
429
|
-
|
|
382
|
+
const indexToDelete = this._items.findIndex((item) => item[0] === child);
|
|
383
|
+
this._items.splice(indexToDelete, 1);
|
|
430
384
|
child._detach();
|
|
431
385
|
const storageUpdate = {
|
|
432
386
|
node: this,
|
|
@@ -440,17 +394,17 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
440
394
|
_setChildKey(key, child, previousKey) {
|
|
441
395
|
var _a;
|
|
442
396
|
child._setParentLink(this, key);
|
|
443
|
-
const previousIndex =
|
|
444
|
-
const index =
|
|
397
|
+
const previousIndex = this._items.findIndex((entry) => entry[0]._id === child._id);
|
|
398
|
+
const index = this._items.findIndex((entry) => entry[1] === key);
|
|
445
399
|
if (index !== -1) {
|
|
446
|
-
|
|
400
|
+
this._items[index][1] = makePosition(key, (_a = this._items[index + 1]) == null ? void 0 : _a[1]);
|
|
447
401
|
}
|
|
448
|
-
const item =
|
|
402
|
+
const item = this._items.find((item2) => item2[0] === child);
|
|
449
403
|
if (item) {
|
|
450
404
|
item[1] = key;
|
|
451
405
|
}
|
|
452
|
-
|
|
453
|
-
const newIndex =
|
|
406
|
+
this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
|
|
407
|
+
const newIndex = this._items.findIndex((entry) => entry[0]._id === child._id);
|
|
454
408
|
const updatesDelta = newIndex === previousIndex ? [] : [
|
|
455
409
|
{
|
|
456
410
|
index: newIndex,
|
|
@@ -486,23 +440,23 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
486
440
|
};
|
|
487
441
|
}
|
|
488
442
|
get length() {
|
|
489
|
-
return
|
|
443
|
+
return this._items.length;
|
|
490
444
|
}
|
|
491
445
|
push(element) {
|
|
492
446
|
return this.insert(element, this.length);
|
|
493
447
|
}
|
|
494
448
|
insert(element, index) {
|
|
495
|
-
if (index < 0 || index >
|
|
496
|
-
throw new Error(`Cannot insert list item at index "${index}". index should be between 0 and ${
|
|
449
|
+
if (index < 0 || index > this._items.length) {
|
|
450
|
+
throw new Error(`Cannot insert list item at index "${index}". index should be between 0 and ${this._items.length}`);
|
|
497
451
|
}
|
|
498
|
-
|
|
499
|
-
|
|
452
|
+
const before = this._items[index - 1] ? this._items[index - 1][1] : void 0;
|
|
453
|
+
const after = this._items[index] ? this._items[index][1] : void 0;
|
|
500
454
|
const position = makePosition(before, after);
|
|
501
455
|
const value = selfOrRegister(element);
|
|
502
456
|
value._setParentLink(this, position);
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
const newIndex =
|
|
457
|
+
this._items.push([value, position]);
|
|
458
|
+
this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
|
|
459
|
+
const newIndex = this._items.findIndex((entry) => entry[1] === position);
|
|
506
460
|
if (this._doc && this._id) {
|
|
507
461
|
const id = this._doc.generateId();
|
|
508
462
|
value._attach(id, this._doc);
|
|
@@ -525,31 +479,31 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
525
479
|
if (targetIndex < 0) {
|
|
526
480
|
throw new Error("targetIndex cannot be less than 0");
|
|
527
481
|
}
|
|
528
|
-
if (targetIndex >=
|
|
482
|
+
if (targetIndex >= this._items.length) {
|
|
529
483
|
throw new Error("targetIndex cannot be greater or equal than the list length");
|
|
530
484
|
}
|
|
531
485
|
if (index < 0) {
|
|
532
486
|
throw new Error("index cannot be less than 0");
|
|
533
487
|
}
|
|
534
|
-
if (index >=
|
|
488
|
+
if (index >= this._items.length) {
|
|
535
489
|
throw new Error("index cannot be greater or equal than the list length");
|
|
536
490
|
}
|
|
537
491
|
let beforePosition = null;
|
|
538
492
|
let afterPosition = null;
|
|
539
493
|
if (index < targetIndex) {
|
|
540
|
-
afterPosition = targetIndex ===
|
|
541
|
-
beforePosition =
|
|
494
|
+
afterPosition = targetIndex === this._items.length - 1 ? void 0 : this._items[targetIndex + 1][1];
|
|
495
|
+
beforePosition = this._items[targetIndex][1];
|
|
542
496
|
} else {
|
|
543
|
-
afterPosition =
|
|
544
|
-
beforePosition = targetIndex === 0 ? void 0 :
|
|
497
|
+
afterPosition = this._items[targetIndex][1];
|
|
498
|
+
beforePosition = targetIndex === 0 ? void 0 : this._items[targetIndex - 1][1];
|
|
545
499
|
}
|
|
546
500
|
const position = makePosition(beforePosition, afterPosition);
|
|
547
|
-
const item =
|
|
501
|
+
const item = this._items[index];
|
|
548
502
|
const previousPosition = item[1];
|
|
549
503
|
item[1] = position;
|
|
550
504
|
item[0]._setParentLink(this, position);
|
|
551
|
-
|
|
552
|
-
const newIndex =
|
|
505
|
+
this._items.sort((itemA, itemB) => compare(itemA[1], itemB[1]));
|
|
506
|
+
const newIndex = this._items.findIndex((entry) => entry[1] === position);
|
|
553
507
|
if (this._doc && this._id) {
|
|
554
508
|
const storageUpdates = /* @__PURE__ */ new Map();
|
|
555
509
|
storageUpdates.set(this._id, {
|
|
@@ -581,12 +535,12 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
581
535
|
}
|
|
582
536
|
}
|
|
583
537
|
delete(index) {
|
|
584
|
-
if (index < 0 || index >=
|
|
585
|
-
throw new Error(`Cannot delete list item at index "${index}". index should be between 0 and ${
|
|
538
|
+
if (index < 0 || index >= this._items.length) {
|
|
539
|
+
throw new Error(`Cannot delete list item at index "${index}". index should be between 0 and ${this._items.length - 1}`);
|
|
586
540
|
}
|
|
587
|
-
const item =
|
|
541
|
+
const item = this._items[index];
|
|
588
542
|
item[0]._detach();
|
|
589
|
-
|
|
543
|
+
this._items.splice(index, 1);
|
|
590
544
|
if (this._doc) {
|
|
591
545
|
const childRecordId = item[0]._id;
|
|
592
546
|
if (childRecordId) {
|
|
@@ -608,11 +562,11 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
608
562
|
}
|
|
609
563
|
clear() {
|
|
610
564
|
if (this._doc) {
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
565
|
+
const ops = [];
|
|
566
|
+
const reverseOps = [];
|
|
567
|
+
const updateDelta = [];
|
|
614
568
|
let i = 0;
|
|
615
|
-
for (const item of
|
|
569
|
+
for (const item of this._items) {
|
|
616
570
|
item[0]._detach();
|
|
617
571
|
const childId = item[0]._id;
|
|
618
572
|
if (childId) {
|
|
@@ -622,7 +576,7 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
622
576
|
}
|
|
623
577
|
i++;
|
|
624
578
|
}
|
|
625
|
-
|
|
579
|
+
this._items = [];
|
|
626
580
|
const storageUpdates = /* @__PURE__ */ new Map();
|
|
627
581
|
storageUpdates.set(this._id, {
|
|
628
582
|
node: this,
|
|
@@ -631,14 +585,41 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
631
585
|
});
|
|
632
586
|
this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
633
587
|
} else {
|
|
634
|
-
for (const item of
|
|
588
|
+
for (const item of this._items) {
|
|
635
589
|
item[0]._detach();
|
|
636
590
|
}
|
|
637
|
-
|
|
591
|
+
this._items = [];
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
set(index, item) {
|
|
595
|
+
if (index < 0 || index >= this._items.length) {
|
|
596
|
+
throw new Error(`Cannot set list item at index "${index}". index should be between 0 and ${this._items.length - 1}`);
|
|
597
|
+
}
|
|
598
|
+
const [existingItem, position] = this._items[index];
|
|
599
|
+
existingItem._detach();
|
|
600
|
+
const value = selfOrRegister(item);
|
|
601
|
+
value._setParentLink(this, position);
|
|
602
|
+
this._items[index][0] = value;
|
|
603
|
+
if (this._doc && this._id) {
|
|
604
|
+
const id = this._doc.generateId();
|
|
605
|
+
value._attach(id, this._doc);
|
|
606
|
+
const storageUpdates = /* @__PURE__ */ new Map();
|
|
607
|
+
storageUpdates.set(this._id, {
|
|
608
|
+
node: this,
|
|
609
|
+
type: "LiveList",
|
|
610
|
+
updates: [
|
|
611
|
+
{
|
|
612
|
+
index,
|
|
613
|
+
item: value instanceof LiveRegister ? value.data : value,
|
|
614
|
+
type: "set"
|
|
615
|
+
}
|
|
616
|
+
]
|
|
617
|
+
});
|
|
618
|
+
this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, void 0, "set"), storageUpdates);
|
|
638
619
|
}
|
|
639
620
|
}
|
|
640
621
|
toArray() {
|
|
641
|
-
return
|
|
622
|
+
return this._items.map((entry) => selfOrRegisterValue(entry[0]));
|
|
642
623
|
}
|
|
643
624
|
every(predicate) {
|
|
644
625
|
return this.toArray().every(predicate);
|
|
@@ -656,10 +637,10 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
656
637
|
return this.toArray().forEach(callbackfn);
|
|
657
638
|
}
|
|
658
639
|
get(index) {
|
|
659
|
-
if (index < 0 || index >=
|
|
640
|
+
if (index < 0 || index >= this._items.length) {
|
|
660
641
|
return void 0;
|
|
661
642
|
}
|
|
662
|
-
return selfOrRegisterValue(
|
|
643
|
+
return selfOrRegisterValue(this._items[index][0]);
|
|
663
644
|
}
|
|
664
645
|
indexOf(searchElement, fromIndex) {
|
|
665
646
|
return this.toArray().indexOf(searchElement, fromIndex);
|
|
@@ -668,27 +649,24 @@ const _LiveList = class extends AbstractCrdt {
|
|
|
668
649
|
return this.toArray().lastIndexOf(searchElement, fromIndex);
|
|
669
650
|
}
|
|
670
651
|
map(callback) {
|
|
671
|
-
return
|
|
652
|
+
return this._items.map((entry, i) => callback(selfOrRegisterValue(entry[0]), i));
|
|
672
653
|
}
|
|
673
654
|
some(predicate) {
|
|
674
655
|
return this.toArray().some(predicate);
|
|
675
656
|
}
|
|
676
657
|
[Symbol.iterator]() {
|
|
677
|
-
return new LiveListIterator(
|
|
658
|
+
return new LiveListIterator(this._items);
|
|
678
659
|
}
|
|
679
|
-
}
|
|
680
|
-
let LiveList = _LiveList;
|
|
681
|
-
_items = new WeakMap();
|
|
660
|
+
}
|
|
682
661
|
class LiveListIterator {
|
|
683
662
|
constructor(items) {
|
|
684
|
-
|
|
685
|
-
__privateSet$2(this, _innerIterator, items[Symbol.iterator]());
|
|
663
|
+
this._innerIterator = items[Symbol.iterator]();
|
|
686
664
|
}
|
|
687
665
|
[Symbol.iterator]() {
|
|
688
666
|
return this;
|
|
689
667
|
}
|
|
690
668
|
next() {
|
|
691
|
-
const result =
|
|
669
|
+
const result = this._innerIterator.next();
|
|
692
670
|
if (result.done) {
|
|
693
671
|
return {
|
|
694
672
|
done: true,
|
|
@@ -700,31 +678,10 @@ class LiveListIterator {
|
|
|
700
678
|
};
|
|
701
679
|
}
|
|
702
680
|
}
|
|
703
|
-
_innerIterator = new WeakMap();
|
|
704
681
|
|
|
705
|
-
|
|
706
|
-
if (!member.has(obj))
|
|
707
|
-
throw TypeError("Cannot " + msg);
|
|
708
|
-
};
|
|
709
|
-
var __privateGet$1 = (obj, member, getter) => {
|
|
710
|
-
__accessCheck$1(obj, member, "read from private field");
|
|
711
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
712
|
-
};
|
|
713
|
-
var __privateAdd$1 = (obj, member, value) => {
|
|
714
|
-
if (member.has(obj))
|
|
715
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
716
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
717
|
-
};
|
|
718
|
-
var __privateSet$1 = (obj, member, value, setter) => {
|
|
719
|
-
__accessCheck$1(obj, member, "write to private field");
|
|
720
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
721
|
-
return value;
|
|
722
|
-
};
|
|
723
|
-
var _map$1;
|
|
724
|
-
const _LiveMap = class extends AbstractCrdt {
|
|
682
|
+
class LiveMap extends AbstractCrdt {
|
|
725
683
|
constructor(entries) {
|
|
726
684
|
super();
|
|
727
|
-
__privateAdd$1(this, _map$1, void 0);
|
|
728
685
|
if (entries) {
|
|
729
686
|
const mappedEntries = [];
|
|
730
687
|
for (const entry of entries) {
|
|
@@ -732,12 +689,12 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
732
689
|
value._setParentLink(this, entry[0]);
|
|
733
690
|
mappedEntries.push([entry[0], value]);
|
|
734
691
|
}
|
|
735
|
-
|
|
692
|
+
this._map = new Map(mappedEntries);
|
|
736
693
|
} else {
|
|
737
|
-
|
|
694
|
+
this._map = /* @__PURE__ */ new Map();
|
|
738
695
|
}
|
|
739
696
|
}
|
|
740
|
-
_serialize(parentId, parentKey, doc) {
|
|
697
|
+
_serialize(parentId, parentKey, doc, intent) {
|
|
741
698
|
if (this._id == null) {
|
|
742
699
|
throw new Error("Cannot serialize item is not attached");
|
|
743
700
|
}
|
|
@@ -749,11 +706,12 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
749
706
|
id: this._id,
|
|
750
707
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
751
708
|
type: OpType.CreateMap,
|
|
709
|
+
intent,
|
|
752
710
|
parentId,
|
|
753
711
|
parentKey
|
|
754
712
|
};
|
|
755
713
|
ops.push(op);
|
|
756
|
-
for (const [key, value] of
|
|
714
|
+
for (const [key, value] of this._map) {
|
|
757
715
|
ops.push(...value._serialize(this._id, key, doc));
|
|
758
716
|
}
|
|
759
717
|
return ops;
|
|
@@ -762,7 +720,7 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
762
720
|
if (item.type !== CrdtType.Map) {
|
|
763
721
|
throw new Error(`Tried to deserialize a map but item type is "${item.type}"`);
|
|
764
722
|
}
|
|
765
|
-
const map = new
|
|
723
|
+
const map = new LiveMap();
|
|
766
724
|
map._attach(id, doc);
|
|
767
725
|
const children = parentToChildren.get(id);
|
|
768
726
|
if (children == null) {
|
|
@@ -775,26 +733,29 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
775
733
|
}
|
|
776
734
|
const child = deserialize(entry, parentToChildren, doc);
|
|
777
735
|
child._setParentLink(map, crdt.parentKey);
|
|
778
|
-
|
|
736
|
+
map._map.set(crdt.parentKey, child);
|
|
779
737
|
}
|
|
780
738
|
return map;
|
|
781
739
|
}
|
|
782
740
|
_attach(id, doc) {
|
|
783
741
|
super._attach(id, doc);
|
|
784
|
-
for (const [
|
|
742
|
+
for (const [_key, value] of this._map) {
|
|
785
743
|
if (isCrdt(value)) {
|
|
786
744
|
value._attach(doc.generateId(), doc);
|
|
787
745
|
}
|
|
788
746
|
}
|
|
789
747
|
}
|
|
790
|
-
_attachChild(
|
|
748
|
+
_attachChild(op, _isLocal) {
|
|
791
749
|
if (this._doc == null) {
|
|
792
750
|
throw new Error("Can't attach child if doc is not present");
|
|
793
751
|
}
|
|
752
|
+
const { id, parentKey } = op;
|
|
753
|
+
const key = parentKey;
|
|
754
|
+
const child = creationOpToLiveStructure(op);
|
|
794
755
|
if (this._doc.getItem(id) !== void 0) {
|
|
795
756
|
return { modified: false };
|
|
796
757
|
}
|
|
797
|
-
const previousValue =
|
|
758
|
+
const previousValue = this._map.get(key);
|
|
798
759
|
let reverse;
|
|
799
760
|
if (previousValue) {
|
|
800
761
|
reverse = previousValue._serialize(this._id, key);
|
|
@@ -804,7 +765,7 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
804
765
|
}
|
|
805
766
|
child._setParentLink(this, key);
|
|
806
767
|
child._attach(id, this._doc);
|
|
807
|
-
|
|
768
|
+
this._map.set(key, child);
|
|
808
769
|
return {
|
|
809
770
|
modified: {
|
|
810
771
|
node: this,
|
|
@@ -816,15 +777,15 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
816
777
|
}
|
|
817
778
|
_detach() {
|
|
818
779
|
super._detach();
|
|
819
|
-
for (const item of
|
|
780
|
+
for (const item of this._map.values()) {
|
|
820
781
|
item._detach();
|
|
821
782
|
}
|
|
822
783
|
}
|
|
823
784
|
_detachChild(child) {
|
|
824
785
|
const reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
825
|
-
for (const [key, value] of
|
|
786
|
+
for (const [key, value] of this._map) {
|
|
826
787
|
if (value === child) {
|
|
827
|
-
|
|
788
|
+
this._map.delete(key);
|
|
828
789
|
}
|
|
829
790
|
}
|
|
830
791
|
child._detach();
|
|
@@ -844,20 +805,20 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
844
805
|
};
|
|
845
806
|
}
|
|
846
807
|
get(key) {
|
|
847
|
-
const value =
|
|
808
|
+
const value = this._map.get(key);
|
|
848
809
|
if (value == void 0) {
|
|
849
810
|
return void 0;
|
|
850
811
|
}
|
|
851
812
|
return selfOrRegisterValue(value);
|
|
852
813
|
}
|
|
853
814
|
set(key, value) {
|
|
854
|
-
const oldValue =
|
|
815
|
+
const oldValue = this._map.get(key);
|
|
855
816
|
if (oldValue) {
|
|
856
817
|
oldValue._detach();
|
|
857
818
|
}
|
|
858
819
|
const item = selfOrRegister(value);
|
|
859
820
|
item._setParentLink(this, key);
|
|
860
|
-
|
|
821
|
+
this._map.set(key, item);
|
|
861
822
|
if (this._doc && this._id) {
|
|
862
823
|
const id = this._doc.generateId();
|
|
863
824
|
item._attach(id, this._doc);
|
|
@@ -871,18 +832,18 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
871
832
|
}
|
|
872
833
|
}
|
|
873
834
|
get size() {
|
|
874
|
-
return
|
|
835
|
+
return this._map.size;
|
|
875
836
|
}
|
|
876
837
|
has(key) {
|
|
877
|
-
return
|
|
838
|
+
return this._map.has(key);
|
|
878
839
|
}
|
|
879
840
|
delete(key) {
|
|
880
|
-
const item =
|
|
841
|
+
const item = this._map.get(key);
|
|
881
842
|
if (item == null) {
|
|
882
843
|
return false;
|
|
883
844
|
}
|
|
884
845
|
item._detach();
|
|
885
|
-
|
|
846
|
+
this._map.delete(key);
|
|
886
847
|
if (this._doc && item._id) {
|
|
887
848
|
const storageUpdates = /* @__PURE__ */ new Map();
|
|
888
849
|
storageUpdates.set(this._id, {
|
|
@@ -901,7 +862,7 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
901
862
|
return true;
|
|
902
863
|
}
|
|
903
864
|
entries() {
|
|
904
|
-
const innerIterator =
|
|
865
|
+
const innerIterator = this._map.entries();
|
|
905
866
|
return {
|
|
906
867
|
[Symbol.iterator]: function() {
|
|
907
868
|
return this;
|
|
@@ -925,10 +886,10 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
925
886
|
return this.entries();
|
|
926
887
|
}
|
|
927
888
|
keys() {
|
|
928
|
-
return
|
|
889
|
+
return this._map.keys();
|
|
929
890
|
}
|
|
930
891
|
values() {
|
|
931
|
-
const innerIterator =
|
|
892
|
+
const innerIterator = this._map.values();
|
|
932
893
|
return {
|
|
933
894
|
[Symbol.iterator]: function() {
|
|
934
895
|
return this;
|
|
@@ -952,9 +913,7 @@ const _LiveMap = class extends AbstractCrdt {
|
|
|
952
913
|
callback(entry[1], entry[0], this);
|
|
953
914
|
}
|
|
954
915
|
}
|
|
955
|
-
}
|
|
956
|
-
let LiveMap = _LiveMap;
|
|
957
|
-
_map$1 = new WeakMap();
|
|
916
|
+
}
|
|
958
917
|
|
|
959
918
|
var __defProp$2 = Object.defineProperty;
|
|
960
919
|
var __defProps$2 = Object.defineProperties;
|
|
@@ -983,6 +942,18 @@ function remove(array, item) {
|
|
|
983
942
|
}
|
|
984
943
|
}
|
|
985
944
|
}
|
|
945
|
+
function creationOpToLiveStructure(op) {
|
|
946
|
+
switch (op.type) {
|
|
947
|
+
case OpType.CreateRegister:
|
|
948
|
+
return new LiveRegister(op.data);
|
|
949
|
+
case OpType.CreateObject:
|
|
950
|
+
return new LiveObject(op.data);
|
|
951
|
+
case OpType.CreateMap:
|
|
952
|
+
return new LiveMap();
|
|
953
|
+
case OpType.CreateList:
|
|
954
|
+
return new LiveList();
|
|
955
|
+
}
|
|
956
|
+
}
|
|
986
957
|
function isSameNodeOrChildOf(node, parent) {
|
|
987
958
|
if (node === parent) {
|
|
988
959
|
return true;
|
|
@@ -1099,32 +1070,41 @@ function getTreesDiffOperations(currentItems, newItems) {
|
|
|
1099
1070
|
});
|
|
1100
1071
|
return ops;
|
|
1101
1072
|
}
|
|
1073
|
+
function mergeObjectStorageUpdates(first, second) {
|
|
1074
|
+
const updates = first.updates;
|
|
1075
|
+
for (const [key, value] of entries(second.updates)) {
|
|
1076
|
+
updates[key] = value;
|
|
1077
|
+
}
|
|
1078
|
+
return __spreadProps$2(__spreadValues$2({}, second), {
|
|
1079
|
+
updates
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
function mergeMapStorageUpdates(first, second) {
|
|
1083
|
+
const updates = first.updates;
|
|
1084
|
+
for (const [key, value] of entries(second.updates)) {
|
|
1085
|
+
updates[key] = value;
|
|
1086
|
+
}
|
|
1087
|
+
return __spreadProps$2(__spreadValues$2({}, second), {
|
|
1088
|
+
updates
|
|
1089
|
+
});
|
|
1090
|
+
}
|
|
1091
|
+
function mergeListStorageUpdates(first, second) {
|
|
1092
|
+
const updates = first.updates;
|
|
1093
|
+
return __spreadProps$2(__spreadValues$2({}, second), {
|
|
1094
|
+
updates: updates.concat(second.updates)
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1102
1097
|
function mergeStorageUpdates(first, second) {
|
|
1103
1098
|
if (!first) {
|
|
1104
1099
|
return second;
|
|
1105
1100
|
}
|
|
1106
|
-
if (second.type === "LiveObject") {
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
return
|
|
1112
|
-
|
|
1113
|
-
});
|
|
1114
|
-
} else if (second.type === "LiveMap") {
|
|
1115
|
-
const updates = first.updates;
|
|
1116
|
-
for (const [key, value] of Object.entries(second.updates)) {
|
|
1117
|
-
updates[key] = value;
|
|
1118
|
-
}
|
|
1119
|
-
return __spreadProps$2(__spreadValues$2({}, second), {
|
|
1120
|
-
updates
|
|
1121
|
-
});
|
|
1122
|
-
} else if (second.type === "LiveList") {
|
|
1123
|
-
const updates = first.updates;
|
|
1124
|
-
return __spreadProps$2(__spreadValues$2({}, second), {
|
|
1125
|
-
updates: updates.concat(second.updates)
|
|
1126
|
-
});
|
|
1127
|
-
}
|
|
1101
|
+
if (first.type === "LiveObject" && second.type === "LiveObject") {
|
|
1102
|
+
return mergeObjectStorageUpdates(first, second);
|
|
1103
|
+
} else if (first.type === "LiveMap" && second.type === "LiveMap") {
|
|
1104
|
+
return mergeMapStorageUpdates(first, second);
|
|
1105
|
+
} else if (first.type === "LiveList" && second.type === "LiveList") {
|
|
1106
|
+
return mergeListStorageUpdates(first, second);
|
|
1107
|
+
} else ;
|
|
1128
1108
|
return second;
|
|
1129
1109
|
}
|
|
1130
1110
|
function isPlain(value) {
|
|
@@ -1134,7 +1114,7 @@ function isPlain(value) {
|
|
|
1134
1114
|
function isPlainObject$1(value) {
|
|
1135
1115
|
if (typeof value !== "object" || value === null)
|
|
1136
1116
|
return false;
|
|
1137
|
-
|
|
1117
|
+
const proto = Object.getPrototypeOf(value);
|
|
1138
1118
|
if (proto === null)
|
|
1139
1119
|
return true;
|
|
1140
1120
|
let baseProto = proto;
|
|
@@ -1170,46 +1150,41 @@ function findNonSerializableValue(value, path = "") {
|
|
|
1170
1150
|
}
|
|
1171
1151
|
return false;
|
|
1172
1152
|
}
|
|
1153
|
+
function isTokenValid(token) {
|
|
1154
|
+
if (token === null) {
|
|
1155
|
+
return false;
|
|
1156
|
+
}
|
|
1157
|
+
const tokenParts = token.split(".");
|
|
1158
|
+
if (tokenParts.length !== 3) {
|
|
1159
|
+
return false;
|
|
1160
|
+
}
|
|
1161
|
+
const data = JSON.parse(atob(tokenParts[1]));
|
|
1162
|
+
if (typeof data.exp !== "number") {
|
|
1163
|
+
return false;
|
|
1164
|
+
}
|
|
1165
|
+
const now = Date.now();
|
|
1166
|
+
if (now / 1e3 > data.exp - 300) {
|
|
1167
|
+
return false;
|
|
1168
|
+
}
|
|
1169
|
+
return true;
|
|
1170
|
+
}
|
|
1171
|
+
function entries(obj) {
|
|
1172
|
+
return Object.entries(obj);
|
|
1173
|
+
}
|
|
1173
1174
|
|
|
1174
|
-
|
|
1175
|
-
if (!member.has(obj))
|
|
1176
|
-
throw TypeError("Cannot " + msg);
|
|
1177
|
-
};
|
|
1178
|
-
var __privateGet = (obj, member, getter) => {
|
|
1179
|
-
__accessCheck(obj, member, "read from private field");
|
|
1180
|
-
return getter ? getter.call(obj) : member.get(obj);
|
|
1181
|
-
};
|
|
1182
|
-
var __privateAdd = (obj, member, value) => {
|
|
1183
|
-
if (member.has(obj))
|
|
1184
|
-
throw TypeError("Cannot add the same private member more than once");
|
|
1185
|
-
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
1186
|
-
};
|
|
1187
|
-
var __privateSet = (obj, member, value, setter) => {
|
|
1188
|
-
__accessCheck(obj, member, "write to private field");
|
|
1189
|
-
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
1190
|
-
return value;
|
|
1191
|
-
};
|
|
1192
|
-
var __privateMethod = (obj, member, method) => {
|
|
1193
|
-
__accessCheck(obj, member, "access private method");
|
|
1194
|
-
return method;
|
|
1195
|
-
};
|
|
1196
|
-
var _map, _propToLastUpdate, _applyUpdate, applyUpdate_fn, _applyDeleteObjectKey, applyDeleteObjectKey_fn;
|
|
1197
|
-
const _LiveObject = class extends AbstractCrdt {
|
|
1175
|
+
class LiveObject extends AbstractCrdt {
|
|
1198
1176
|
constructor(object = {}) {
|
|
1199
1177
|
super();
|
|
1200
|
-
|
|
1201
|
-
__privateAdd(this, _applyDeleteObjectKey);
|
|
1202
|
-
__privateAdd(this, _map, void 0);
|
|
1203
|
-
__privateAdd(this, _propToLastUpdate, /* @__PURE__ */ new Map());
|
|
1178
|
+
this._propToLastUpdate = /* @__PURE__ */ new Map();
|
|
1204
1179
|
for (const key in object) {
|
|
1205
1180
|
const value = object[key];
|
|
1206
1181
|
if (value instanceof AbstractCrdt) {
|
|
1207
1182
|
value._setParentLink(this, key);
|
|
1208
1183
|
}
|
|
1209
1184
|
}
|
|
1210
|
-
|
|
1185
|
+
this._map = new Map(Object.entries(object));
|
|
1211
1186
|
}
|
|
1212
|
-
_serialize(parentId, parentKey, doc) {
|
|
1187
|
+
_serialize(parentId, parentKey, doc, intent) {
|
|
1213
1188
|
if (this._id == null) {
|
|
1214
1189
|
throw new Error("Cannot serialize item is not attached");
|
|
1215
1190
|
}
|
|
@@ -1217,13 +1192,14 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1217
1192
|
const op = {
|
|
1218
1193
|
id: this._id,
|
|
1219
1194
|
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
1195
|
+
intent,
|
|
1220
1196
|
type: OpType.CreateObject,
|
|
1221
1197
|
parentId,
|
|
1222
1198
|
parentKey,
|
|
1223
1199
|
data: {}
|
|
1224
1200
|
};
|
|
1225
1201
|
ops.push(op);
|
|
1226
|
-
for (const [key, value] of
|
|
1202
|
+
for (const [key, value] of this._map) {
|
|
1227
1203
|
if (value instanceof AbstractCrdt) {
|
|
1228
1204
|
ops.push(...value._serialize(this._id, key, doc));
|
|
1229
1205
|
} else {
|
|
@@ -1236,7 +1212,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1236
1212
|
if (item.type !== CrdtType.Object) {
|
|
1237
1213
|
throw new Error(`Tried to deserialize a record but item type is "${item.type}"`);
|
|
1238
1214
|
}
|
|
1239
|
-
const object = new
|
|
1215
|
+
const object = new LiveObject(item.data);
|
|
1240
1216
|
object._attach(id, doc);
|
|
1241
1217
|
return this._deserializeChildren(object, parentToChildren, doc);
|
|
1242
1218
|
}
|
|
@@ -1252,37 +1228,40 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1252
1228
|
}
|
|
1253
1229
|
const child = deserialize(entry, parentToChildren, doc);
|
|
1254
1230
|
child._setParentLink(object, crdt.parentKey);
|
|
1255
|
-
|
|
1231
|
+
object._map.set(crdt.parentKey, child);
|
|
1256
1232
|
}
|
|
1257
1233
|
return object;
|
|
1258
1234
|
}
|
|
1259
1235
|
_attach(id, doc) {
|
|
1260
1236
|
super._attach(id, doc);
|
|
1261
|
-
for (const [
|
|
1237
|
+
for (const [_key, value] of this._map) {
|
|
1262
1238
|
if (value instanceof AbstractCrdt) {
|
|
1263
1239
|
value._attach(doc.generateId(), doc);
|
|
1264
1240
|
}
|
|
1265
1241
|
}
|
|
1266
1242
|
}
|
|
1267
|
-
_attachChild(
|
|
1243
|
+
_attachChild(op, isLocal) {
|
|
1268
1244
|
if (this._doc == null) {
|
|
1269
1245
|
throw new Error("Can't attach child if doc is not present");
|
|
1270
1246
|
}
|
|
1247
|
+
const { id, parentKey, opId } = op;
|
|
1248
|
+
const key = parentKey;
|
|
1249
|
+
const child = creationOpToLiveStructure(op);
|
|
1271
1250
|
if (this._doc.getItem(id) !== void 0) {
|
|
1272
|
-
if (
|
|
1273
|
-
|
|
1251
|
+
if (this._propToLastUpdate.get(key) === opId) {
|
|
1252
|
+
this._propToLastUpdate.delete(key);
|
|
1274
1253
|
}
|
|
1275
1254
|
return { modified: false };
|
|
1276
1255
|
}
|
|
1277
1256
|
if (isLocal) {
|
|
1278
|
-
|
|
1279
|
-
} else if (
|
|
1280
|
-
|
|
1257
|
+
this._propToLastUpdate.set(key, opId);
|
|
1258
|
+
} else if (this._propToLastUpdate.get(key) === void 0) ; else if (this._propToLastUpdate.get(key) === opId) {
|
|
1259
|
+
this._propToLastUpdate.delete(key);
|
|
1281
1260
|
return { modified: false };
|
|
1282
1261
|
} else {
|
|
1283
1262
|
return { modified: false };
|
|
1284
1263
|
}
|
|
1285
|
-
const previousValue =
|
|
1264
|
+
const previousValue = this._map.get(key);
|
|
1286
1265
|
let reverse;
|
|
1287
1266
|
if (isCrdt(previousValue)) {
|
|
1288
1267
|
reverse = previousValue._serialize(this._id, key);
|
|
@@ -1300,7 +1279,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1300
1279
|
}
|
|
1301
1280
|
];
|
|
1302
1281
|
}
|
|
1303
|
-
|
|
1282
|
+
this._map.set(key, child);
|
|
1304
1283
|
child._setParentLink(this, key);
|
|
1305
1284
|
child._attach(id, this._doc);
|
|
1306
1285
|
return {
|
|
@@ -1315,9 +1294,9 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1315
1294
|
_detachChild(child) {
|
|
1316
1295
|
if (child) {
|
|
1317
1296
|
const reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
1318
|
-
for (const [key, value] of
|
|
1297
|
+
for (const [key, value] of this._map) {
|
|
1319
1298
|
if (value === child) {
|
|
1320
|
-
|
|
1299
|
+
this._map.delete(key);
|
|
1321
1300
|
}
|
|
1322
1301
|
}
|
|
1323
1302
|
child._detach();
|
|
@@ -1333,14 +1312,14 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1333
1312
|
return { modified: false };
|
|
1334
1313
|
}
|
|
1335
1314
|
_detachChildren() {
|
|
1336
|
-
for (const [key, value] of
|
|
1337
|
-
|
|
1315
|
+
for (const [key, value] of this._map) {
|
|
1316
|
+
this._map.delete(key);
|
|
1338
1317
|
value._detach();
|
|
1339
1318
|
}
|
|
1340
1319
|
}
|
|
1341
1320
|
_detach() {
|
|
1342
1321
|
super._detach();
|
|
1343
|
-
for (const value of
|
|
1322
|
+
for (const value of this._map.values()) {
|
|
1344
1323
|
if (isCrdt(value)) {
|
|
1345
1324
|
value._detach();
|
|
1346
1325
|
}
|
|
@@ -1348,33 +1327,123 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1348
1327
|
}
|
|
1349
1328
|
_apply(op, isLocal) {
|
|
1350
1329
|
if (op.type === OpType.UpdateObject) {
|
|
1351
|
-
return
|
|
1330
|
+
return this._applyUpdate(op, isLocal);
|
|
1352
1331
|
} else if (op.type === OpType.DeleteObjectKey) {
|
|
1353
|
-
return
|
|
1332
|
+
return this._applyDeleteObjectKey(op);
|
|
1354
1333
|
}
|
|
1355
1334
|
return super._apply(op, isLocal);
|
|
1356
1335
|
}
|
|
1357
1336
|
_toSerializedCrdt() {
|
|
1358
1337
|
var _a;
|
|
1338
|
+
const data = {};
|
|
1339
|
+
for (const [key, value] of this._map) {
|
|
1340
|
+
if (value instanceof AbstractCrdt === false) {
|
|
1341
|
+
data[key] = value;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1359
1344
|
return {
|
|
1360
1345
|
type: CrdtType.Object,
|
|
1361
1346
|
parentId: (_a = this._parent) == null ? void 0 : _a._id,
|
|
1362
1347
|
parentKey: this._parentKey,
|
|
1363
|
-
data
|
|
1348
|
+
data
|
|
1349
|
+
};
|
|
1350
|
+
}
|
|
1351
|
+
_applyUpdate(op, isLocal) {
|
|
1352
|
+
let isModified = false;
|
|
1353
|
+
const reverse = [];
|
|
1354
|
+
const reverseUpdate = {
|
|
1355
|
+
type: OpType.UpdateObject,
|
|
1356
|
+
id: this._id,
|
|
1357
|
+
data: {}
|
|
1358
|
+
};
|
|
1359
|
+
reverse.push(reverseUpdate);
|
|
1360
|
+
for (const key in op.data) {
|
|
1361
|
+
const oldValue = this._map.get(key);
|
|
1362
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
1363
|
+
reverse.push(...oldValue._serialize(this._id, key));
|
|
1364
|
+
oldValue._detach();
|
|
1365
|
+
} else if (oldValue !== void 0) {
|
|
1366
|
+
reverseUpdate.data[key] = oldValue;
|
|
1367
|
+
} else if (oldValue === void 0) {
|
|
1368
|
+
reverse.push({ type: OpType.DeleteObjectKey, id: this._id, key });
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
const updateDelta = {};
|
|
1372
|
+
for (const key in op.data) {
|
|
1373
|
+
if (isLocal) {
|
|
1374
|
+
this._propToLastUpdate.set(key, op.opId);
|
|
1375
|
+
} else if (this._propToLastUpdate.get(key) == null) {
|
|
1376
|
+
isModified = true;
|
|
1377
|
+
} else if (this._propToLastUpdate.get(key) === op.opId) {
|
|
1378
|
+
this._propToLastUpdate.delete(key);
|
|
1379
|
+
continue;
|
|
1380
|
+
} else {
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1383
|
+
const oldValue = this._map.get(key);
|
|
1384
|
+
if (isCrdt(oldValue)) {
|
|
1385
|
+
oldValue._detach();
|
|
1386
|
+
}
|
|
1387
|
+
isModified = true;
|
|
1388
|
+
updateDelta[key] = { type: "update" };
|
|
1389
|
+
this._map.set(key, op.data[key]);
|
|
1390
|
+
}
|
|
1391
|
+
if (Object.keys(reverseUpdate.data).length !== 0) {
|
|
1392
|
+
reverse.unshift(reverseUpdate);
|
|
1393
|
+
}
|
|
1394
|
+
return isModified ? {
|
|
1395
|
+
modified: {
|
|
1396
|
+
node: this,
|
|
1397
|
+
type: "LiveObject",
|
|
1398
|
+
updates: updateDelta
|
|
1399
|
+
},
|
|
1400
|
+
reverse
|
|
1401
|
+
} : { modified: false };
|
|
1402
|
+
}
|
|
1403
|
+
_applyDeleteObjectKey(op) {
|
|
1404
|
+
const key = op.key;
|
|
1405
|
+
if (this._map.has(key) === false) {
|
|
1406
|
+
return { modified: false };
|
|
1407
|
+
}
|
|
1408
|
+
if (this._propToLastUpdate.get(key) !== void 0) {
|
|
1409
|
+
return { modified: false };
|
|
1410
|
+
}
|
|
1411
|
+
const oldValue = this._map.get(key);
|
|
1412
|
+
let reverse = [];
|
|
1413
|
+
if (isCrdt(oldValue)) {
|
|
1414
|
+
reverse = oldValue._serialize(this._id, op.key);
|
|
1415
|
+
oldValue._detach();
|
|
1416
|
+
} else if (oldValue !== void 0) {
|
|
1417
|
+
reverse = [
|
|
1418
|
+
{
|
|
1419
|
+
type: OpType.UpdateObject,
|
|
1420
|
+
id: this._id,
|
|
1421
|
+
data: { [key]: oldValue }
|
|
1422
|
+
}
|
|
1423
|
+
];
|
|
1424
|
+
}
|
|
1425
|
+
this._map.delete(key);
|
|
1426
|
+
return {
|
|
1427
|
+
modified: {
|
|
1428
|
+
node: this,
|
|
1429
|
+
type: "LiveObject",
|
|
1430
|
+
updates: { [op.key]: { type: "delete" } }
|
|
1431
|
+
},
|
|
1432
|
+
reverse
|
|
1364
1433
|
};
|
|
1365
1434
|
}
|
|
1366
1435
|
toObject() {
|
|
1367
|
-
return Object.fromEntries(
|
|
1436
|
+
return Object.fromEntries(this._map);
|
|
1368
1437
|
}
|
|
1369
1438
|
set(key, value) {
|
|
1370
1439
|
this.update({ [key]: value });
|
|
1371
1440
|
}
|
|
1372
1441
|
get(key) {
|
|
1373
|
-
return
|
|
1442
|
+
return this._map.get(key);
|
|
1374
1443
|
}
|
|
1375
1444
|
delete(key) {
|
|
1376
1445
|
const keyAsString = key;
|
|
1377
|
-
const oldValue =
|
|
1446
|
+
const oldValue = this._map.get(keyAsString);
|
|
1378
1447
|
if (oldValue === void 0) {
|
|
1379
1448
|
return;
|
|
1380
1449
|
}
|
|
@@ -1382,7 +1451,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1382
1451
|
if (oldValue instanceof AbstractCrdt) {
|
|
1383
1452
|
oldValue._detach();
|
|
1384
1453
|
}
|
|
1385
|
-
|
|
1454
|
+
this._map.delete(keyAsString);
|
|
1386
1455
|
return;
|
|
1387
1456
|
}
|
|
1388
1457
|
let reverse;
|
|
@@ -1398,7 +1467,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1398
1467
|
}
|
|
1399
1468
|
];
|
|
1400
1469
|
}
|
|
1401
|
-
|
|
1470
|
+
this._map.delete(keyAsString);
|
|
1402
1471
|
const storageUpdates = /* @__PURE__ */ new Map();
|
|
1403
1472
|
storageUpdates.set(this._id, {
|
|
1404
1473
|
node: this,
|
|
@@ -1417,7 +1486,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1417
1486
|
update(overrides) {
|
|
1418
1487
|
if (this._doc == null || this._id == null) {
|
|
1419
1488
|
for (const key in overrides) {
|
|
1420
|
-
const oldValue =
|
|
1489
|
+
const oldValue = this._map.get(key);
|
|
1421
1490
|
if (oldValue instanceof AbstractCrdt) {
|
|
1422
1491
|
oldValue._detach();
|
|
1423
1492
|
}
|
|
@@ -1425,7 +1494,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1425
1494
|
if (newValue instanceof AbstractCrdt) {
|
|
1426
1495
|
newValue._setParentLink(this, key);
|
|
1427
1496
|
}
|
|
1428
|
-
|
|
1497
|
+
this._map.set(key, newValue);
|
|
1429
1498
|
}
|
|
1430
1499
|
return;
|
|
1431
1500
|
}
|
|
@@ -1440,7 +1509,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1440
1509
|
};
|
|
1441
1510
|
const updateDelta = {};
|
|
1442
1511
|
for (const key in overrides) {
|
|
1443
|
-
const oldValue =
|
|
1512
|
+
const oldValue = this._map.get(key);
|
|
1444
1513
|
if (oldValue instanceof AbstractCrdt) {
|
|
1445
1514
|
reverseOps.push(...oldValue._serialize(this._id, key));
|
|
1446
1515
|
oldValue._detach();
|
|
@@ -1456,14 +1525,14 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1456
1525
|
const newAttachChildOps = newValue._serialize(this._id, key, this._doc);
|
|
1457
1526
|
const createCrdtOp = newAttachChildOps.find((op) => op.parentId === this._id);
|
|
1458
1527
|
if (createCrdtOp) {
|
|
1459
|
-
|
|
1528
|
+
this._propToLastUpdate.set(key, createCrdtOp.opId);
|
|
1460
1529
|
}
|
|
1461
1530
|
ops.push(...newAttachChildOps);
|
|
1462
1531
|
} else {
|
|
1463
1532
|
updatedProps[key] = newValue;
|
|
1464
|
-
|
|
1533
|
+
this._propToLastUpdate.set(key, opId);
|
|
1465
1534
|
}
|
|
1466
|
-
|
|
1535
|
+
this._map.set(key, newValue);
|
|
1467
1536
|
updateDelta[key] = { type: "update" };
|
|
1468
1537
|
}
|
|
1469
1538
|
if (Object.keys(reverseUpdateOp.data).length !== 0) {
|
|
@@ -1485,96 +1554,7 @@ const _LiveObject = class extends AbstractCrdt {
|
|
|
1485
1554
|
});
|
|
1486
1555
|
this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
1487
1556
|
}
|
|
1488
|
-
}
|
|
1489
|
-
let LiveObject = _LiveObject;
|
|
1490
|
-
_map = new WeakMap();
|
|
1491
|
-
_propToLastUpdate = new WeakMap();
|
|
1492
|
-
_applyUpdate = new WeakSet();
|
|
1493
|
-
applyUpdate_fn = function(op, isLocal) {
|
|
1494
|
-
let isModified = false;
|
|
1495
|
-
const reverse = [];
|
|
1496
|
-
const reverseUpdate = {
|
|
1497
|
-
type: OpType.UpdateObject,
|
|
1498
|
-
id: this._id,
|
|
1499
|
-
data: {}
|
|
1500
|
-
};
|
|
1501
|
-
reverse.push(reverseUpdate);
|
|
1502
|
-
for (const key in op.data) {
|
|
1503
|
-
const oldValue = __privateGet(this, _map).get(key);
|
|
1504
|
-
if (oldValue instanceof AbstractCrdt) {
|
|
1505
|
-
reverse.push(...oldValue._serialize(this._id, key));
|
|
1506
|
-
oldValue._detach();
|
|
1507
|
-
} else if (oldValue !== void 0) {
|
|
1508
|
-
reverseUpdate.data[key] = oldValue;
|
|
1509
|
-
} else if (oldValue === void 0) {
|
|
1510
|
-
reverse.push({ type: OpType.DeleteObjectKey, id: this._id, key });
|
|
1511
|
-
}
|
|
1512
|
-
}
|
|
1513
|
-
let updateDelta = {};
|
|
1514
|
-
for (const key in op.data) {
|
|
1515
|
-
if (isLocal) {
|
|
1516
|
-
__privateGet(this, _propToLastUpdate).set(key, op.opId);
|
|
1517
|
-
} else if (__privateGet(this, _propToLastUpdate).get(key) == null) {
|
|
1518
|
-
isModified = true;
|
|
1519
|
-
} else if (__privateGet(this, _propToLastUpdate).get(key) === op.opId) {
|
|
1520
|
-
__privateGet(this, _propToLastUpdate).delete(key);
|
|
1521
|
-
continue;
|
|
1522
|
-
} else {
|
|
1523
|
-
continue;
|
|
1524
|
-
}
|
|
1525
|
-
const oldValue = __privateGet(this, _map).get(key);
|
|
1526
|
-
if (isCrdt(oldValue)) {
|
|
1527
|
-
oldValue._detach();
|
|
1528
|
-
}
|
|
1529
|
-
isModified = true;
|
|
1530
|
-
updateDelta[key] = { type: "update" };
|
|
1531
|
-
__privateGet(this, _map).set(key, op.data[key]);
|
|
1532
|
-
}
|
|
1533
|
-
if (Object.keys(reverseUpdate.data).length !== 0) {
|
|
1534
|
-
reverse.unshift(reverseUpdate);
|
|
1535
|
-
}
|
|
1536
|
-
return isModified ? {
|
|
1537
|
-
modified: {
|
|
1538
|
-
node: this,
|
|
1539
|
-
type: "LiveObject",
|
|
1540
|
-
updates: updateDelta
|
|
1541
|
-
},
|
|
1542
|
-
reverse
|
|
1543
|
-
} : { modified: false };
|
|
1544
|
-
};
|
|
1545
|
-
_applyDeleteObjectKey = new WeakSet();
|
|
1546
|
-
applyDeleteObjectKey_fn = function(op) {
|
|
1547
|
-
const key = op.key;
|
|
1548
|
-
if (__privateGet(this, _map).has(key) === false) {
|
|
1549
|
-
return { modified: false };
|
|
1550
|
-
}
|
|
1551
|
-
if (__privateGet(this, _propToLastUpdate).get(key) !== void 0) {
|
|
1552
|
-
return { modified: false };
|
|
1553
|
-
}
|
|
1554
|
-
const oldValue = __privateGet(this, _map).get(key);
|
|
1555
|
-
let reverse = [];
|
|
1556
|
-
if (isCrdt(oldValue)) {
|
|
1557
|
-
reverse = oldValue._serialize(this._id, op.key);
|
|
1558
|
-
oldValue._detach();
|
|
1559
|
-
} else if (oldValue !== void 0) {
|
|
1560
|
-
reverse = [
|
|
1561
|
-
{
|
|
1562
|
-
type: OpType.UpdateObject,
|
|
1563
|
-
id: this._id,
|
|
1564
|
-
data: { [key]: oldValue }
|
|
1565
|
-
}
|
|
1566
|
-
];
|
|
1567
|
-
}
|
|
1568
|
-
__privateGet(this, _map).delete(key);
|
|
1569
|
-
return {
|
|
1570
|
-
modified: {
|
|
1571
|
-
node: this,
|
|
1572
|
-
type: "LiveObject",
|
|
1573
|
-
updates: { [op.key]: { type: "delete" } }
|
|
1574
|
-
},
|
|
1575
|
-
reverse
|
|
1576
|
-
};
|
|
1577
|
-
};
|
|
1557
|
+
}
|
|
1578
1558
|
|
|
1579
1559
|
var __defProp$1 = Object.defineProperty;
|
|
1580
1560
|
var __defProps$1 = Object.defineProperties;
|
|
@@ -1618,37 +1598,44 @@ function makeIdFactory(connectionId) {
|
|
|
1618
1598
|
let count = 0;
|
|
1619
1599
|
return () => `${connectionId}:${count++}`;
|
|
1620
1600
|
}
|
|
1621
|
-
function makeOthers(
|
|
1622
|
-
const
|
|
1623
|
-
const _a =
|
|
1601
|
+
function makeOthers(userMap) {
|
|
1602
|
+
const users = Object.values(userMap).map((user) => {
|
|
1603
|
+
const _a = user, publicKeys = __objRest(_a, ["_hasReceivedInitialPresence"]);
|
|
1624
1604
|
return publicKeys;
|
|
1625
1605
|
});
|
|
1626
1606
|
return {
|
|
1627
1607
|
get count() {
|
|
1628
|
-
return
|
|
1608
|
+
return users.length;
|
|
1629
1609
|
},
|
|
1630
1610
|
[Symbol.iterator]() {
|
|
1631
|
-
return
|
|
1611
|
+
return users[Symbol.iterator]();
|
|
1632
1612
|
},
|
|
1633
1613
|
map(callback) {
|
|
1634
|
-
return
|
|
1614
|
+
return users.map(callback);
|
|
1635
1615
|
},
|
|
1636
1616
|
toArray() {
|
|
1637
|
-
return
|
|
1617
|
+
return users;
|
|
1638
1618
|
}
|
|
1639
1619
|
};
|
|
1640
1620
|
}
|
|
1641
1621
|
function makeStateMachine(state, context, mockedEffects) {
|
|
1642
1622
|
const effects = mockedEffects || {
|
|
1643
1623
|
authenticate(auth, createWebSocket) {
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
}
|
|
1648
|
-
const parsedToken = parseToken(token);
|
|
1649
|
-
const socket = createWebSocket(token);
|
|
1624
|
+
if (isTokenValid(state.token)) {
|
|
1625
|
+
const parsedToken = parseToken(state.token);
|
|
1626
|
+
const socket = createWebSocket(state.token);
|
|
1650
1627
|
authenticationSuccess(parsedToken, socket);
|
|
1651
|
-
}
|
|
1628
|
+
} else {
|
|
1629
|
+
return auth(context.room).then(({ token }) => {
|
|
1630
|
+
if (state.connection.state !== "authenticating") {
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
const parsedToken = parseToken(token);
|
|
1634
|
+
const socket = createWebSocket(token);
|
|
1635
|
+
authenticationSuccess(parsedToken, socket);
|
|
1636
|
+
state.token = token;
|
|
1637
|
+
}).catch((er) => authenticationFailure(er));
|
|
1638
|
+
}
|
|
1652
1639
|
},
|
|
1653
1640
|
send(messageOrMessages) {
|
|
1654
1641
|
if (state.socket == null) {
|
|
@@ -1785,12 +1772,12 @@ function makeStateMachine(state, context, mockedEffects) {
|
|
|
1785
1772
|
function notify({
|
|
1786
1773
|
storageUpdates = /* @__PURE__ */ new Map(),
|
|
1787
1774
|
presence = false,
|
|
1788
|
-
others = []
|
|
1775
|
+
others: otherEvents = []
|
|
1789
1776
|
}) {
|
|
1790
|
-
if (
|
|
1777
|
+
if (otherEvents.length > 0) {
|
|
1791
1778
|
state.others = makeOthers(state.users);
|
|
1792
|
-
for (const event of
|
|
1793
|
-
for (const listener of state.listeners
|
|
1779
|
+
for (const event of otherEvents) {
|
|
1780
|
+
for (const listener of state.listeners.others) {
|
|
1794
1781
|
listener(state.others, event);
|
|
1795
1782
|
}
|
|
1796
1783
|
}
|
|
@@ -1889,33 +1876,15 @@ function makeStateMachine(state, context, mockedEffects) {
|
|
|
1889
1876
|
}
|
|
1890
1877
|
return { modified: false };
|
|
1891
1878
|
}
|
|
1892
|
-
case OpType.CreateObject:
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
return { modified: false };
|
|
1896
|
-
}
|
|
1897
|
-
return parent._attachChild(op.id, op.parentKey, new LiveObject(op.data), op.opId, isLocal);
|
|
1898
|
-
}
|
|
1899
|
-
case OpType.CreateList: {
|
|
1900
|
-
const parent = state.items.get(op.parentId);
|
|
1901
|
-
if (parent == null) {
|
|
1902
|
-
return { modified: false };
|
|
1903
|
-
}
|
|
1904
|
-
return parent._attachChild(op.id, op.parentKey, new LiveList(), op.opId, isLocal);
|
|
1905
|
-
}
|
|
1879
|
+
case OpType.CreateObject:
|
|
1880
|
+
case OpType.CreateList:
|
|
1881
|
+
case OpType.CreateMap:
|
|
1906
1882
|
case OpType.CreateRegister: {
|
|
1907
1883
|
const parent = state.items.get(op.parentId);
|
|
1908
1884
|
if (parent == null) {
|
|
1909
1885
|
return { modified: false };
|
|
1910
1886
|
}
|
|
1911
|
-
return parent._attachChild(op
|
|
1912
|
-
}
|
|
1913
|
-
case OpType.CreateMap: {
|
|
1914
|
-
const parent = state.items.get(op.parentId);
|
|
1915
|
-
if (parent == null) {
|
|
1916
|
-
return { modified: false };
|
|
1917
|
-
}
|
|
1918
|
-
return parent._attachChild(op.id, op.parentKey, new LiveMap(), op.opId, isLocal);
|
|
1887
|
+
return parent._attachChild(op, isLocal);
|
|
1919
1888
|
}
|
|
1920
1889
|
}
|
|
1921
1890
|
return { modified: false };
|
|
@@ -2006,6 +1975,7 @@ See v0.13 release notes for more information.
|
|
|
2006
1975
|
if (process.env.NODE_ENV !== "production") {
|
|
2007
1976
|
console.error("Call to authentication endpoint failed", error);
|
|
2008
1977
|
}
|
|
1978
|
+
state.token = null;
|
|
2009
1979
|
updateConnection({ state: "unavailable" });
|
|
2010
1980
|
state.numberOfRetry++;
|
|
2011
1981
|
state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
|
|
@@ -2136,8 +2106,9 @@ See v0.13 release notes for more information.
|
|
|
2136
2106
|
break;
|
|
2137
2107
|
}
|
|
2138
2108
|
case ServerMessageType.InitialStorageState: {
|
|
2109
|
+
const offlineOps = new Map(state.offlineOperations);
|
|
2139
2110
|
createOrUpdateRootFromMessage(subMessage);
|
|
2140
|
-
applyAndSendOfflineOps();
|
|
2111
|
+
applyAndSendOfflineOps(offlineOps);
|
|
2141
2112
|
_getInitialStateResolver == null ? void 0 : _getInitialStateResolver();
|
|
2142
2113
|
break;
|
|
2143
2114
|
}
|
|
@@ -2249,12 +2220,12 @@ See v0.13 release notes for more information.
|
|
|
2249
2220
|
clearInterval(state.intervalHandles.heartbeat);
|
|
2250
2221
|
connect();
|
|
2251
2222
|
}
|
|
2252
|
-
function applyAndSendOfflineOps() {
|
|
2253
|
-
if (
|
|
2223
|
+
function applyAndSendOfflineOps(offlineOps) {
|
|
2224
|
+
if (offlineOps.size === 0) {
|
|
2254
2225
|
return;
|
|
2255
2226
|
}
|
|
2256
2227
|
const messages = [];
|
|
2257
|
-
const ops = Array.from(
|
|
2228
|
+
const ops = Array.from(offlineOps.values());
|
|
2258
2229
|
const result = apply(ops, true);
|
|
2259
2230
|
messages.push({
|
|
2260
2231
|
type: ClientMessageType.UpdateStorage,
|
|
@@ -2504,6 +2475,7 @@ See v0.13 release notes for more information.
|
|
|
2504
2475
|
function defaultState(me, defaultStorageRoot) {
|
|
2505
2476
|
return {
|
|
2506
2477
|
connection: { state: "closed" },
|
|
2478
|
+
token: null,
|
|
2507
2479
|
lastConnectionId: null,
|
|
2508
2480
|
socket: null,
|
|
2509
2481
|
listeners: {
|
|
@@ -2696,7 +2668,7 @@ function createClient(options) {
|
|
|
2696
2668
|
return internalRoom.room;
|
|
2697
2669
|
}
|
|
2698
2670
|
function leave(roomId) {
|
|
2699
|
-
|
|
2671
|
+
const room = rooms.get(roomId);
|
|
2700
2672
|
if (room) {
|
|
2701
2673
|
room.disconnect();
|
|
2702
2674
|
rooms.delete(roomId);
|
|
@@ -2771,26 +2743,30 @@ var __spreadValues = (a, b) => {
|
|
|
2771
2743
|
return a;
|
|
2772
2744
|
};
|
|
2773
2745
|
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
2774
|
-
function
|
|
2746
|
+
function lsonObjectToJson(obj) {
|
|
2775
2747
|
const result = {};
|
|
2776
|
-
const obj = liveObject.toObject();
|
|
2777
2748
|
for (const key in obj) {
|
|
2778
|
-
result[key] =
|
|
2749
|
+
result[key] = lsonToJson(obj[key]);
|
|
2779
2750
|
}
|
|
2780
2751
|
return result;
|
|
2781
2752
|
}
|
|
2753
|
+
function liveObjectToJson(liveObject) {
|
|
2754
|
+
return lsonObjectToJson(liveObject.toObject());
|
|
2755
|
+
}
|
|
2782
2756
|
function liveMapToJson(map) {
|
|
2783
2757
|
const result = {};
|
|
2784
|
-
const
|
|
2785
|
-
|
|
2786
|
-
result[key] = liveNodeToJson(obj[key]);
|
|
2758
|
+
for (const [key, value] of map.entries()) {
|
|
2759
|
+
result[key] = lsonToJson(value);
|
|
2787
2760
|
}
|
|
2788
2761
|
return result;
|
|
2789
2762
|
}
|
|
2763
|
+
function lsonListToJson(value) {
|
|
2764
|
+
return value.map(lsonToJson);
|
|
2765
|
+
}
|
|
2790
2766
|
function liveListToJson(value) {
|
|
2791
|
-
return value.toArray()
|
|
2767
|
+
return lsonListToJson(value.toArray());
|
|
2792
2768
|
}
|
|
2793
|
-
function
|
|
2769
|
+
function lsonToJson(value) {
|
|
2794
2770
|
if (value instanceof LiveObject) {
|
|
2795
2771
|
return liveObjectToJson(value);
|
|
2796
2772
|
} else if (value instanceof LiveList) {
|
|
@@ -2799,11 +2775,18 @@ function liveNodeToJson(value) {
|
|
|
2799
2775
|
return liveMapToJson(value);
|
|
2800
2776
|
} else if (value instanceof LiveRegister) {
|
|
2801
2777
|
return value.data;
|
|
2778
|
+
} else if (value instanceof AbstractCrdt) {
|
|
2779
|
+
throw new Error("Unhandled subclass of AbstractCrdt encountered");
|
|
2780
|
+
}
|
|
2781
|
+
if (Array.isArray(value)) {
|
|
2782
|
+
return lsonListToJson(value);
|
|
2783
|
+
} else if (isPlainObject(value)) {
|
|
2784
|
+
return lsonObjectToJson(value);
|
|
2802
2785
|
}
|
|
2803
2786
|
return value;
|
|
2804
2787
|
}
|
|
2805
2788
|
function isPlainObject(obj) {
|
|
2806
|
-
return Object.prototype.toString.call(obj) === "[object Object]";
|
|
2789
|
+
return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
|
|
2807
2790
|
}
|
|
2808
2791
|
function anyToCrdt(obj) {
|
|
2809
2792
|
if (obj == null) {
|
|
@@ -2869,8 +2852,7 @@ function patchLiveList(liveList, prev, next) {
|
|
|
2869
2852
|
if (liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
|
|
2870
2853
|
patchLiveObject(liveListNode, prevNode, nextNode);
|
|
2871
2854
|
} else {
|
|
2872
|
-
liveList.
|
|
2873
|
-
liveList.insert(anyToCrdt(nextNode), i);
|
|
2855
|
+
liveList.set(i, anyToCrdt(nextNode));
|
|
2874
2856
|
}
|
|
2875
2857
|
i++;
|
|
2876
2858
|
}
|
|
@@ -2878,9 +2860,10 @@ function patchLiveList(liveList, prev, next) {
|
|
|
2878
2860
|
liveList.insert(anyToCrdt(next[i]), i);
|
|
2879
2861
|
i++;
|
|
2880
2862
|
}
|
|
2881
|
-
|
|
2863
|
+
let localI = i;
|
|
2864
|
+
while (localI <= prevEnd) {
|
|
2882
2865
|
liveList.delete(i);
|
|
2883
|
-
|
|
2866
|
+
localI++;
|
|
2884
2867
|
}
|
|
2885
2868
|
}
|
|
2886
2869
|
}
|
|
@@ -2950,10 +2933,10 @@ function patchImmutableNode(state, path, update) {
|
|
|
2950
2933
|
if (typeof state !== "object") {
|
|
2951
2934
|
throw new Error("Internal: received update on LiveObject but state was not an object");
|
|
2952
2935
|
}
|
|
2953
|
-
|
|
2936
|
+
const newState = Object.assign({}, state);
|
|
2954
2937
|
for (const key in update.updates) {
|
|
2955
2938
|
if (((_a = update.updates[key]) == null ? void 0 : _a.type) === "update") {
|
|
2956
|
-
newState[key] =
|
|
2939
|
+
newState[key] = lsonToJson(update.node.get(key));
|
|
2957
2940
|
} else if (((_b = update.updates[key]) == null ? void 0 : _b.type) === "delete") {
|
|
2958
2941
|
delete newState[key];
|
|
2959
2942
|
}
|
|
@@ -2966,13 +2949,15 @@ function patchImmutableNode(state, path, update) {
|
|
|
2966
2949
|
}
|
|
2967
2950
|
let newState = state.map((x) => x);
|
|
2968
2951
|
for (const listUpdate of update.updates) {
|
|
2969
|
-
if (listUpdate.type === "
|
|
2952
|
+
if (listUpdate.type === "set") {
|
|
2953
|
+
newState = newState.map((item, index) => index === listUpdate.index ? listUpdate.item : item);
|
|
2954
|
+
} else if (listUpdate.type === "insert") {
|
|
2970
2955
|
if (listUpdate.index === newState.length) {
|
|
2971
|
-
newState.push(
|
|
2956
|
+
newState.push(lsonToJson(listUpdate.item));
|
|
2972
2957
|
} else {
|
|
2973
2958
|
newState = [
|
|
2974
2959
|
...newState.slice(0, listUpdate.index),
|
|
2975
|
-
|
|
2960
|
+
lsonToJson(listUpdate.item),
|
|
2976
2961
|
...newState.slice(listUpdate.index)
|
|
2977
2962
|
];
|
|
2978
2963
|
}
|
|
@@ -2982,7 +2967,7 @@ function patchImmutableNode(state, path, update) {
|
|
|
2982
2967
|
if (listUpdate.previousIndex > listUpdate.index) {
|
|
2983
2968
|
newState = [
|
|
2984
2969
|
...newState.slice(0, listUpdate.index),
|
|
2985
|
-
|
|
2970
|
+
lsonToJson(listUpdate.item),
|
|
2986
2971
|
...newState.slice(listUpdate.index, listUpdate.previousIndex),
|
|
2987
2972
|
...newState.slice(listUpdate.previousIndex + 1)
|
|
2988
2973
|
];
|
|
@@ -2990,7 +2975,7 @@ function patchImmutableNode(state, path, update) {
|
|
|
2990
2975
|
newState = [
|
|
2991
2976
|
...newState.slice(0, listUpdate.previousIndex),
|
|
2992
2977
|
...newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1),
|
|
2993
|
-
|
|
2978
|
+
lsonToJson(listUpdate.item),
|
|
2994
2979
|
...newState.slice(listUpdate.index + 1)
|
|
2995
2980
|
];
|
|
2996
2981
|
}
|
|
@@ -3002,10 +2987,10 @@ function patchImmutableNode(state, path, update) {
|
|
|
3002
2987
|
if (typeof state !== "object") {
|
|
3003
2988
|
throw new Error("Internal: received update on LiveMap but state was not an object");
|
|
3004
2989
|
}
|
|
3005
|
-
|
|
2990
|
+
const newState = Object.assign({}, state);
|
|
3006
2991
|
for (const key in update.updates) {
|
|
3007
2992
|
if (((_c = update.updates[key]) == null ? void 0 : _c.type) === "update") {
|
|
3008
|
-
newState[key] =
|
|
2993
|
+
newState[key] = lsonToJson(update.node.get(key));
|
|
3009
2994
|
} else if (((_d = update.updates[key]) == null ? void 0 : _d.type) === "delete") {
|
|
3010
2995
|
delete newState[key];
|
|
3011
2996
|
}
|
|
@@ -3027,7 +3012,7 @@ function patchImmutableNode(state, path, update) {
|
|
|
3027
3012
|
|
|
3028
3013
|
const internals = {
|
|
3029
3014
|
liveObjectToJson,
|
|
3030
|
-
|
|
3015
|
+
lsonToJson,
|
|
3031
3016
|
patchLiveList,
|
|
3032
3017
|
patchImmutableObject,
|
|
3033
3018
|
patchLiveObject,
|