@liveblocks/client 0.16.2 → 0.16.4-beta2
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/index.d.ts +3 -727
- package/index.js +100 -2523
- package/index.mjs +1641 -0
- package/internal.d.ts +15 -19
- package/internal.js +32 -190
- package/internal.mjs +1 -0
- package/package.json +7 -18
- package/shared.d.ts +753 -0
- package/shared.js +2488 -0
- package/shared.mjs +1947 -0
- package/esm/index.js +0 -3060
- package/esm/index.mjs +0 -3060
- package/esm/internal.js +0 -150
- package/esm/internal.mjs +0 -150
package/shared.js
ADDED
|
@@ -0,0 +1,2488 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function _defineProperties(target, props) {
|
|
4
|
+
for (var i = 0; i < props.length; i++) {
|
|
5
|
+
var descriptor = props[i];
|
|
6
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
7
|
+
descriptor.configurable = true;
|
|
8
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
9
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
14
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
15
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
16
|
+
return Constructor;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function _extends() {
|
|
20
|
+
_extends = Object.assign || function (target) {
|
|
21
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
22
|
+
var source = arguments[i];
|
|
23
|
+
|
|
24
|
+
for (var key in source) {
|
|
25
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
26
|
+
target[key] = source[key];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return target;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return _extends.apply(this, arguments);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function _inheritsLoose(subClass, superClass) {
|
|
38
|
+
subClass.prototype = Object.create(superClass.prototype);
|
|
39
|
+
subClass.prototype.constructor = subClass;
|
|
40
|
+
|
|
41
|
+
_setPrototypeOf(subClass, superClass);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function _getPrototypeOf(o) {
|
|
45
|
+
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
46
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
47
|
+
};
|
|
48
|
+
return _getPrototypeOf(o);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function _setPrototypeOf(o, p) {
|
|
52
|
+
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
|
53
|
+
o.__proto__ = p;
|
|
54
|
+
return o;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
return _setPrototypeOf(o, p);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function _isNativeReflectConstruct() {
|
|
61
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
62
|
+
if (Reflect.construct.sham) return false;
|
|
63
|
+
if (typeof Proxy === "function") return true;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
|
67
|
+
return true;
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function _construct(Parent, args, Class) {
|
|
74
|
+
if (_isNativeReflectConstruct()) {
|
|
75
|
+
_construct = Reflect.construct;
|
|
76
|
+
} else {
|
|
77
|
+
_construct = function _construct(Parent, args, Class) {
|
|
78
|
+
var a = [null];
|
|
79
|
+
a.push.apply(a, args);
|
|
80
|
+
var Constructor = Function.bind.apply(Parent, a);
|
|
81
|
+
var instance = new Constructor();
|
|
82
|
+
if (Class) _setPrototypeOf(instance, Class.prototype);
|
|
83
|
+
return instance;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return _construct.apply(null, arguments);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function _isNativeFunction(fn) {
|
|
91
|
+
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function _wrapNativeSuper(Class) {
|
|
95
|
+
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
96
|
+
|
|
97
|
+
_wrapNativeSuper = function _wrapNativeSuper(Class) {
|
|
98
|
+
if (Class === null || !_isNativeFunction(Class)) return Class;
|
|
99
|
+
|
|
100
|
+
if (typeof Class !== "function") {
|
|
101
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (typeof _cache !== "undefined") {
|
|
105
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
106
|
+
|
|
107
|
+
_cache.set(Class, Wrapper);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function Wrapper() {
|
|
111
|
+
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
Wrapper.prototype = Object.create(Class.prototype, {
|
|
115
|
+
constructor: {
|
|
116
|
+
value: Wrapper,
|
|
117
|
+
enumerable: false,
|
|
118
|
+
writable: true,
|
|
119
|
+
configurable: true
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return _setPrototypeOf(Wrapper, Class);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
return _wrapNativeSuper(Class);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function _objectWithoutPropertiesLoose(source, excluded) {
|
|
129
|
+
if (source == null) return {};
|
|
130
|
+
var target = {};
|
|
131
|
+
var sourceKeys = Object.keys(source);
|
|
132
|
+
var key, i;
|
|
133
|
+
|
|
134
|
+
for (i = 0; i < sourceKeys.length; i++) {
|
|
135
|
+
key = sourceKeys[i];
|
|
136
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
137
|
+
target[key] = source[key];
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return target;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function _assertThisInitialized(self) {
|
|
144
|
+
if (self === void 0) {
|
|
145
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return self;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
152
|
+
if (!o) return;
|
|
153
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
154
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
155
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
156
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
157
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function _arrayLikeToArray(arr, len) {
|
|
161
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
162
|
+
|
|
163
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
164
|
+
|
|
165
|
+
return arr2;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
169
|
+
var it;
|
|
170
|
+
|
|
171
|
+
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
|
172
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
173
|
+
if (it) o = it;
|
|
174
|
+
var i = 0;
|
|
175
|
+
return function () {
|
|
176
|
+
if (i >= o.length) return {
|
|
177
|
+
done: true
|
|
178
|
+
};
|
|
179
|
+
return {
|
|
180
|
+
done: false,
|
|
181
|
+
value: o[i++]
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
it = o[Symbol.iterator]();
|
|
190
|
+
return it.next.bind(it);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
exports.ServerMessageType = void 0;
|
|
194
|
+
|
|
195
|
+
(function (ServerMessageType) {
|
|
196
|
+
ServerMessageType[ServerMessageType["UpdatePresence"] = 100] = "UpdatePresence";
|
|
197
|
+
ServerMessageType[ServerMessageType["UserJoined"] = 101] = "UserJoined";
|
|
198
|
+
ServerMessageType[ServerMessageType["UserLeft"] = 102] = "UserLeft";
|
|
199
|
+
ServerMessageType[ServerMessageType["Event"] = 103] = "Event";
|
|
200
|
+
ServerMessageType[ServerMessageType["RoomState"] = 104] = "RoomState";
|
|
201
|
+
ServerMessageType[ServerMessageType["InitialStorageState"] = 200] = "InitialStorageState";
|
|
202
|
+
ServerMessageType[ServerMessageType["UpdateStorage"] = 201] = "UpdateStorage";
|
|
203
|
+
})(exports.ServerMessageType || (exports.ServerMessageType = {}));
|
|
204
|
+
|
|
205
|
+
exports.ClientMessageType = void 0;
|
|
206
|
+
|
|
207
|
+
(function (ClientMessageType) {
|
|
208
|
+
ClientMessageType[ClientMessageType["UpdatePresence"] = 100] = "UpdatePresence";
|
|
209
|
+
ClientMessageType[ClientMessageType["ClientEvent"] = 103] = "ClientEvent";
|
|
210
|
+
ClientMessageType[ClientMessageType["FetchStorage"] = 200] = "FetchStorage";
|
|
211
|
+
ClientMessageType[ClientMessageType["UpdateStorage"] = 201] = "UpdateStorage";
|
|
212
|
+
})(exports.ClientMessageType || (exports.ClientMessageType = {}));
|
|
213
|
+
|
|
214
|
+
exports.CrdtType = void 0;
|
|
215
|
+
|
|
216
|
+
(function (CrdtType) {
|
|
217
|
+
CrdtType[CrdtType["Object"] = 0] = "Object";
|
|
218
|
+
CrdtType[CrdtType["List"] = 1] = "List";
|
|
219
|
+
CrdtType[CrdtType["Map"] = 2] = "Map";
|
|
220
|
+
CrdtType[CrdtType["Register"] = 3] = "Register";
|
|
221
|
+
})(exports.CrdtType || (exports.CrdtType = {}));
|
|
222
|
+
|
|
223
|
+
exports.OpType = void 0;
|
|
224
|
+
|
|
225
|
+
(function (OpType) {
|
|
226
|
+
OpType[OpType["Init"] = 0] = "Init";
|
|
227
|
+
OpType[OpType["SetParentKey"] = 1] = "SetParentKey";
|
|
228
|
+
OpType[OpType["CreateList"] = 2] = "CreateList";
|
|
229
|
+
OpType[OpType["UpdateObject"] = 3] = "UpdateObject";
|
|
230
|
+
OpType[OpType["CreateObject"] = 4] = "CreateObject";
|
|
231
|
+
OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt";
|
|
232
|
+
OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey";
|
|
233
|
+
OpType[OpType["CreateMap"] = 7] = "CreateMap";
|
|
234
|
+
OpType[OpType["CreateRegister"] = 8] = "CreateRegister";
|
|
235
|
+
})(exports.OpType || (exports.OpType = {}));
|
|
236
|
+
|
|
237
|
+
exports.WebsocketCloseCodes = void 0;
|
|
238
|
+
|
|
239
|
+
(function (WebsocketCloseCodes) {
|
|
240
|
+
WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
|
|
241
|
+
WebsocketCloseCodes[WebsocketCloseCodes["INVALID_MESSAGE_FORMAT"] = 4000] = "INVALID_MESSAGE_FORMAT";
|
|
242
|
+
WebsocketCloseCodes[WebsocketCloseCodes["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
|
|
243
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
|
|
244
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
|
|
245
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
|
|
246
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
|
|
247
|
+
WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_WITHOUT_RETRY"] = 4999] = "CLOSE_WITHOUT_RETRY";
|
|
248
|
+
})(exports.WebsocketCloseCodes || (exports.WebsocketCloseCodes = {}));
|
|
249
|
+
|
|
250
|
+
var AbstractCrdt = function () {
|
|
251
|
+
function AbstractCrdt() {}
|
|
252
|
+
|
|
253
|
+
var _proto = AbstractCrdt.prototype;
|
|
254
|
+
|
|
255
|
+
_proto._apply = function _apply(op, _isLocal) {
|
|
256
|
+
switch (op.type) {
|
|
257
|
+
case exports.OpType.DeleteCrdt:
|
|
258
|
+
{
|
|
259
|
+
if (this._parent != null && this._parentKey != null) {
|
|
260
|
+
return this._parent._detachChild(this);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
return {
|
|
264
|
+
modified: false
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return {
|
|
270
|
+
modified: false
|
|
271
|
+
};
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
_proto._setParentLink = function _setParentLink(parent, key) {
|
|
275
|
+
if (this.__parent != null && this.__parent !== parent) {
|
|
276
|
+
throw new Error("Cannot attach parent if it already exist");
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this.__parentKey = key;
|
|
280
|
+
this.__parent = parent;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
_proto._attach = function _attach(id, doc) {
|
|
284
|
+
if (this.__id || this.__doc) {
|
|
285
|
+
throw new Error("Cannot attach if CRDT is already attached");
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
doc.addItem(id, this);
|
|
289
|
+
this.__id = id;
|
|
290
|
+
this.__doc = doc;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
_proto._detach = function _detach() {
|
|
294
|
+
if (this.__doc && this.__id) {
|
|
295
|
+
this.__doc.deleteItem(this.__id);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
this.__parent = undefined;
|
|
299
|
+
this.__doc = undefined;
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
_createClass(AbstractCrdt, [{
|
|
303
|
+
key: "_doc",
|
|
304
|
+
get: function get() {
|
|
305
|
+
return this.__doc;
|
|
306
|
+
}
|
|
307
|
+
}, {
|
|
308
|
+
key: "roomId",
|
|
309
|
+
get: function get() {
|
|
310
|
+
return this.__doc ? this.__doc.roomId : null;
|
|
311
|
+
}
|
|
312
|
+
}, {
|
|
313
|
+
key: "_id",
|
|
314
|
+
get: function get() {
|
|
315
|
+
return this.__id;
|
|
316
|
+
}
|
|
317
|
+
}, {
|
|
318
|
+
key: "_parent",
|
|
319
|
+
get: function get() {
|
|
320
|
+
return this.__parent;
|
|
321
|
+
}
|
|
322
|
+
}, {
|
|
323
|
+
key: "_parentKey",
|
|
324
|
+
get: function get() {
|
|
325
|
+
return this.__parentKey;
|
|
326
|
+
}
|
|
327
|
+
}]);
|
|
328
|
+
|
|
329
|
+
return AbstractCrdt;
|
|
330
|
+
}();
|
|
331
|
+
|
|
332
|
+
var min = 32;
|
|
333
|
+
var max = 126;
|
|
334
|
+
function makePosition(before, after) {
|
|
335
|
+
if (before == null && after == null) {
|
|
336
|
+
return pos([min + 1]);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (before != null && after == null) {
|
|
340
|
+
return getNextPosition(before);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (before == null && after != null) {
|
|
344
|
+
return getPreviousPosition(after);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function getPreviousPosition(after) {
|
|
351
|
+
var result = [];
|
|
352
|
+
var afterCodes = posCodes(after);
|
|
353
|
+
|
|
354
|
+
for (var i = 0; i < afterCodes.length; i++) {
|
|
355
|
+
var code = afterCodes[i];
|
|
356
|
+
|
|
357
|
+
if (code <= min + 1) {
|
|
358
|
+
result.push(min);
|
|
359
|
+
|
|
360
|
+
if (afterCodes.length - 1 === i) {
|
|
361
|
+
result.push(max);
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
} else {
|
|
365
|
+
result.push(code - 1);
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return pos(result);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function getNextPosition(before) {
|
|
374
|
+
var result = [];
|
|
375
|
+
var beforeCodes = posCodes(before);
|
|
376
|
+
|
|
377
|
+
for (var i = 0; i < beforeCodes.length; i++) {
|
|
378
|
+
var code = beforeCodes[i];
|
|
379
|
+
|
|
380
|
+
if (code === max) {
|
|
381
|
+
result.push(code);
|
|
382
|
+
|
|
383
|
+
if (beforeCodes.length - 1 === i) {
|
|
384
|
+
result.push(min + 1);
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
result.push(code + 1);
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
return pos(result);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
function makePositionFromCodes(before, after) {
|
|
397
|
+
var index = 0;
|
|
398
|
+
var result = [];
|
|
399
|
+
|
|
400
|
+
while (true) {
|
|
401
|
+
var beforeDigit = before[index] || min;
|
|
402
|
+
var afterDigit = after[index] || max;
|
|
403
|
+
|
|
404
|
+
if (beforeDigit > afterDigit) {
|
|
405
|
+
throw new Error("Impossible to generate position between " + before + " and " + after);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (beforeDigit === afterDigit) {
|
|
409
|
+
result.push(beforeDigit);
|
|
410
|
+
index++;
|
|
411
|
+
continue;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (afterDigit - beforeDigit === 1) {
|
|
415
|
+
result.push(beforeDigit);
|
|
416
|
+
result.push.apply(result, makePositionFromCodes(before.slice(index + 1), []));
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
var mid = afterDigit + beforeDigit >> 1;
|
|
421
|
+
result.push(mid);
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return result;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function posCodes(str) {
|
|
429
|
+
var codes = [];
|
|
430
|
+
|
|
431
|
+
for (var i = 0; i < str.length; i++) {
|
|
432
|
+
codes.push(str.charCodeAt(i));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
return codes;
|
|
436
|
+
}
|
|
437
|
+
function pos(codes) {
|
|
438
|
+
return String.fromCharCode.apply(String, codes);
|
|
439
|
+
}
|
|
440
|
+
function compare(posA, posB) {
|
|
441
|
+
var aCodes = posCodes(posA);
|
|
442
|
+
var bCodes = posCodes(posB);
|
|
443
|
+
var maxLength = Math.max(aCodes.length, bCodes.length);
|
|
444
|
+
|
|
445
|
+
for (var i = 0; i < maxLength; i++) {
|
|
446
|
+
var a = aCodes[i] == null ? min : aCodes[i];
|
|
447
|
+
var b = bCodes[i] == null ? min : bCodes[i];
|
|
448
|
+
|
|
449
|
+
if (a === b) {
|
|
450
|
+
continue;
|
|
451
|
+
} else {
|
|
452
|
+
return a - b;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
throw new Error("Impossible to compare similar position \"" + posA + "\" and \"" + posB + "\"");
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
var LiveRegister = function (_AbstractCrdt) {
|
|
460
|
+
_inheritsLoose(LiveRegister, _AbstractCrdt);
|
|
461
|
+
|
|
462
|
+
function LiveRegister(data) {
|
|
463
|
+
var _this;
|
|
464
|
+
|
|
465
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
466
|
+
_this._data = data;
|
|
467
|
+
return _this;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
LiveRegister._deserialize = function _deserialize(_ref, _parentToChildren, doc) {
|
|
471
|
+
var id = _ref[0],
|
|
472
|
+
item = _ref[1];
|
|
473
|
+
|
|
474
|
+
if (item.type !== exports.CrdtType.Register) {
|
|
475
|
+
throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
var register = new LiveRegister(item.data);
|
|
479
|
+
|
|
480
|
+
register._attach(id, doc);
|
|
481
|
+
|
|
482
|
+
return register;
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
var _proto = LiveRegister.prototype;
|
|
486
|
+
|
|
487
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
|
|
488
|
+
if (this._id == null || parentId == null || parentKey == null) {
|
|
489
|
+
throw new Error("Cannot serialize register if parentId or parentKey is undefined");
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
return [{
|
|
493
|
+
type: exports.OpType.CreateRegister,
|
|
494
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
495
|
+
id: this._id,
|
|
496
|
+
intent: intent,
|
|
497
|
+
parentId: parentId,
|
|
498
|
+
parentKey: parentKey,
|
|
499
|
+
data: this.data
|
|
500
|
+
}];
|
|
501
|
+
};
|
|
502
|
+
|
|
503
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
504
|
+
var _this$_parent;
|
|
505
|
+
|
|
506
|
+
return {
|
|
507
|
+
type: exports.CrdtType.Register,
|
|
508
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
509
|
+
parentKey: this._parentKey,
|
|
510
|
+
data: this.data
|
|
511
|
+
};
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
_proto._attachChild = function _attachChild(_op, _isLocal) {
|
|
515
|
+
throw new Error("Method not implemented.");
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
_proto._detachChild = function _detachChild(_crdt) {
|
|
519
|
+
throw new Error("Method not implemented.");
|
|
520
|
+
};
|
|
521
|
+
|
|
522
|
+
_proto._apply = function _apply(op, isLocal) {
|
|
523
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
_createClass(LiveRegister, [{
|
|
527
|
+
key: "data",
|
|
528
|
+
get: function get() {
|
|
529
|
+
return this._data;
|
|
530
|
+
}
|
|
531
|
+
}]);
|
|
532
|
+
|
|
533
|
+
return LiveRegister;
|
|
534
|
+
}(AbstractCrdt);
|
|
535
|
+
|
|
536
|
+
var LiveList = function (_AbstractCrdt) {
|
|
537
|
+
_inheritsLoose(LiveList, _AbstractCrdt);
|
|
538
|
+
|
|
539
|
+
function LiveList(items) {
|
|
540
|
+
var _this;
|
|
541
|
+
|
|
542
|
+
if (items === void 0) {
|
|
543
|
+
items = [];
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
547
|
+
_this._items = [];
|
|
548
|
+
var position = undefined;
|
|
549
|
+
|
|
550
|
+
for (var i = 0; i < items.length; i++) {
|
|
551
|
+
var newPosition = makePosition(position);
|
|
552
|
+
|
|
553
|
+
var _item = selfOrRegister(items[i]);
|
|
554
|
+
|
|
555
|
+
_this._items.push([_item, newPosition]);
|
|
556
|
+
|
|
557
|
+
position = newPosition;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return _this;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
LiveList._deserialize = function _deserialize(_ref, parentToChildren, doc) {
|
|
564
|
+
var id = _ref[0];
|
|
565
|
+
var list = new LiveList([]);
|
|
566
|
+
|
|
567
|
+
list._attach(id, doc);
|
|
568
|
+
|
|
569
|
+
var children = parentToChildren.get(id);
|
|
570
|
+
|
|
571
|
+
if (children == null) {
|
|
572
|
+
return list;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
for (var _iterator = _createForOfIteratorHelperLoose(children), _step; !(_step = _iterator()).done;) {
|
|
576
|
+
var entry = _step.value;
|
|
577
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
578
|
+
|
|
579
|
+
child._setParentLink(list, entry[1].parentKey);
|
|
580
|
+
|
|
581
|
+
list._items.push([child, entry[1].parentKey]);
|
|
582
|
+
|
|
583
|
+
list._items.sort(function (itemA, itemB) {
|
|
584
|
+
return compare(itemA[1], itemB[1]);
|
|
585
|
+
});
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
return list;
|
|
589
|
+
};
|
|
590
|
+
|
|
591
|
+
var _proto = LiveList.prototype;
|
|
592
|
+
|
|
593
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
|
|
594
|
+
if (this._id == null) {
|
|
595
|
+
throw new Error("Cannot serialize item is not attached");
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
if (parentId == null || parentKey == null) {
|
|
599
|
+
throw new Error("Cannot serialize list if parentId or parentKey is undefined");
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
var ops = [];
|
|
603
|
+
var op = {
|
|
604
|
+
id: this._id,
|
|
605
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
606
|
+
intent: intent,
|
|
607
|
+
type: exports.OpType.CreateList,
|
|
608
|
+
parentId: parentId,
|
|
609
|
+
parentKey: parentKey
|
|
610
|
+
};
|
|
611
|
+
ops.push(op);
|
|
612
|
+
|
|
613
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(this._items), _step2; !(_step2 = _iterator2()).done;) {
|
|
614
|
+
var _step2$value = _step2.value,
|
|
615
|
+
_value = _step2$value[0],
|
|
616
|
+
key = _step2$value[1];
|
|
617
|
+
ops.push.apply(ops, _value._serialize(this._id, key, doc));
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
return ops;
|
|
621
|
+
};
|
|
622
|
+
|
|
623
|
+
_proto._indexOfPosition = function _indexOfPosition(position) {
|
|
624
|
+
return this._items.findIndex(function (item) {
|
|
625
|
+
return item[1] === position;
|
|
626
|
+
});
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
_proto._attach = function _attach(id, doc) {
|
|
630
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
631
|
+
|
|
632
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(this._items), _step3; !(_step3 = _iterator3()).done;) {
|
|
633
|
+
var _step3$value = _step3.value,
|
|
634
|
+
_item2 = _step3$value[0];
|
|
635
|
+
|
|
636
|
+
_item2._attach(doc.generateId(), doc);
|
|
637
|
+
}
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
_proto._detach = function _detach() {
|
|
641
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
642
|
+
|
|
643
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(this._items), _step4; !(_step4 = _iterator4()).done;) {
|
|
644
|
+
var _step4$value = _step4.value,
|
|
645
|
+
_value2 = _step4$value[0];
|
|
646
|
+
|
|
647
|
+
_value2._detach();
|
|
648
|
+
}
|
|
649
|
+
};
|
|
650
|
+
|
|
651
|
+
_proto._attachChild = function _attachChild(op, isLocal) {
|
|
652
|
+
if (this._doc == null) {
|
|
653
|
+
throw new Error("Can't attach child if doc is not present");
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
var id = op.id,
|
|
657
|
+
parentKey = op.parentKey,
|
|
658
|
+
intent = op.intent;
|
|
659
|
+
var key = parentKey;
|
|
660
|
+
var child = creationOpToLiveStructure(op);
|
|
661
|
+
|
|
662
|
+
if (this._doc.getItem(id) !== undefined) {
|
|
663
|
+
return {
|
|
664
|
+
modified: false
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
child._attach(id, this._doc);
|
|
669
|
+
|
|
670
|
+
child._setParentLink(this, key);
|
|
671
|
+
|
|
672
|
+
var index = this._items.findIndex(function (entry) {
|
|
673
|
+
return entry[1] === key;
|
|
674
|
+
});
|
|
675
|
+
|
|
676
|
+
var newKey = key;
|
|
677
|
+
|
|
678
|
+
if (index !== -1) {
|
|
679
|
+
if (intent === "set") {
|
|
680
|
+
var existingItem = this._items[index][0];
|
|
681
|
+
|
|
682
|
+
existingItem._detach();
|
|
683
|
+
|
|
684
|
+
var storageUpdate = {
|
|
685
|
+
node: this,
|
|
686
|
+
type: "LiveList",
|
|
687
|
+
updates: [{
|
|
688
|
+
index: index,
|
|
689
|
+
type: "set",
|
|
690
|
+
item: child instanceof LiveRegister ? child.data : child
|
|
691
|
+
}]
|
|
692
|
+
};
|
|
693
|
+
this._items[index][0] = child;
|
|
694
|
+
return {
|
|
695
|
+
modified: storageUpdate,
|
|
696
|
+
reverse: existingItem._serialize(this._id, key, this._doc, "set")
|
|
697
|
+
};
|
|
698
|
+
} else if (isLocal) {
|
|
699
|
+
var before = this._items[index] ? this._items[index][1] : undefined;
|
|
700
|
+
var after = this._items[index + 1] ? this._items[index + 1][1] : undefined;
|
|
701
|
+
newKey = makePosition(before, after);
|
|
702
|
+
|
|
703
|
+
child._setParentLink(this, newKey);
|
|
704
|
+
} else {
|
|
705
|
+
var _this$_items;
|
|
706
|
+
|
|
707
|
+
this._items[index][1] = makePosition(key, (_this$_items = this._items[index + 1]) == null ? void 0 : _this$_items[1]);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
this._items.push([child, newKey]);
|
|
712
|
+
|
|
713
|
+
this._items.sort(function (itemA, itemB) {
|
|
714
|
+
return compare(itemA[1], itemB[1]);
|
|
715
|
+
});
|
|
716
|
+
|
|
717
|
+
var newIndex = this._items.findIndex(function (entry) {
|
|
718
|
+
return entry[1] === newKey;
|
|
719
|
+
});
|
|
720
|
+
|
|
721
|
+
return {
|
|
722
|
+
reverse: [{
|
|
723
|
+
type: exports.OpType.DeleteCrdt,
|
|
724
|
+
id: id
|
|
725
|
+
}],
|
|
726
|
+
modified: {
|
|
727
|
+
node: this,
|
|
728
|
+
type: "LiveList",
|
|
729
|
+
updates: [{
|
|
730
|
+
index: newIndex,
|
|
731
|
+
type: "insert",
|
|
732
|
+
item: child instanceof LiveRegister ? child.data : child
|
|
733
|
+
}]
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
};
|
|
737
|
+
|
|
738
|
+
_proto._detachChild = function _detachChild(child) {
|
|
739
|
+
if (child) {
|
|
740
|
+
var reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
741
|
+
|
|
742
|
+
var indexToDelete = this._items.findIndex(function (item) {
|
|
743
|
+
return item[0] === child;
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
this._items.splice(indexToDelete, 1);
|
|
747
|
+
|
|
748
|
+
child._detach();
|
|
749
|
+
|
|
750
|
+
var storageUpdate = {
|
|
751
|
+
node: this,
|
|
752
|
+
type: "LiveList",
|
|
753
|
+
updates: [{
|
|
754
|
+
index: indexToDelete,
|
|
755
|
+
type: "delete"
|
|
756
|
+
}]
|
|
757
|
+
};
|
|
758
|
+
return {
|
|
759
|
+
modified: storageUpdate,
|
|
760
|
+
reverse: reverse
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
return {
|
|
765
|
+
modified: false
|
|
766
|
+
};
|
|
767
|
+
};
|
|
768
|
+
|
|
769
|
+
_proto._setChildKey = function _setChildKey(key, child, previousKey) {
|
|
770
|
+
child._setParentLink(this, key);
|
|
771
|
+
|
|
772
|
+
var previousIndex = this._items.findIndex(function (entry) {
|
|
773
|
+
return entry[0]._id === child._id;
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
var index = this._items.findIndex(function (entry) {
|
|
777
|
+
return entry[1] === key;
|
|
778
|
+
});
|
|
779
|
+
|
|
780
|
+
if (index !== -1) {
|
|
781
|
+
var _this$_items2;
|
|
782
|
+
|
|
783
|
+
this._items[index][1] = makePosition(key, (_this$_items2 = this._items[index + 1]) == null ? void 0 : _this$_items2[1]);
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
var item = this._items.find(function (item) {
|
|
787
|
+
return item[0] === child;
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
if (item) {
|
|
791
|
+
item[1] = key;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
this._items.sort(function (itemA, itemB) {
|
|
795
|
+
return compare(itemA[1], itemB[1]);
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
var newIndex = this._items.findIndex(function (entry) {
|
|
799
|
+
return entry[0]._id === child._id;
|
|
800
|
+
});
|
|
801
|
+
|
|
802
|
+
var updatesDelta = newIndex === previousIndex ? [] : [{
|
|
803
|
+
index: newIndex,
|
|
804
|
+
item: child instanceof LiveRegister ? child.data : child,
|
|
805
|
+
previousIndex: previousIndex,
|
|
806
|
+
type: "move"
|
|
807
|
+
}];
|
|
808
|
+
return {
|
|
809
|
+
modified: {
|
|
810
|
+
node: this,
|
|
811
|
+
type: "LiveList",
|
|
812
|
+
updates: updatesDelta
|
|
813
|
+
},
|
|
814
|
+
reverse: [{
|
|
815
|
+
type: exports.OpType.SetParentKey,
|
|
816
|
+
id: item == null ? void 0 : item[0]._id,
|
|
817
|
+
parentKey: previousKey
|
|
818
|
+
}]
|
|
819
|
+
};
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
_proto._apply = function _apply(op, isLocal) {
|
|
823
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
824
|
+
};
|
|
825
|
+
|
|
826
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
827
|
+
var _this$_parent;
|
|
828
|
+
|
|
829
|
+
return {
|
|
830
|
+
type: exports.CrdtType.List,
|
|
831
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
832
|
+
parentKey: this._parentKey
|
|
833
|
+
};
|
|
834
|
+
};
|
|
835
|
+
|
|
836
|
+
_proto.push = function push(element) {
|
|
837
|
+
return this.insert(element, this.length);
|
|
838
|
+
};
|
|
839
|
+
|
|
840
|
+
_proto.insert = function insert(element, index) {
|
|
841
|
+
if (index < 0 || index > this._items.length) {
|
|
842
|
+
throw new Error("Cannot insert list item at index \"\x1D" + index + "\". index should be between 0 and " + this._items.length);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
var before = this._items[index - 1] ? this._items[index - 1][1] : undefined;
|
|
846
|
+
var after = this._items[index] ? this._items[index][1] : undefined;
|
|
847
|
+
var position = makePosition(before, after);
|
|
848
|
+
var value = selfOrRegister(element);
|
|
849
|
+
|
|
850
|
+
value._setParentLink(this, position);
|
|
851
|
+
|
|
852
|
+
this._items.push([value, position]);
|
|
853
|
+
|
|
854
|
+
this._items.sort(function (itemA, itemB) {
|
|
855
|
+
return compare(itemA[1], itemB[1]);
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
var newIndex = this._items.findIndex(function (entry) {
|
|
859
|
+
return entry[1] === position;
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
if (this._doc && this._id) {
|
|
863
|
+
var _id = this._doc.generateId();
|
|
864
|
+
|
|
865
|
+
value._attach(_id, this._doc);
|
|
866
|
+
|
|
867
|
+
var storageUpdates = new Map();
|
|
868
|
+
storageUpdates.set(this._id, {
|
|
869
|
+
node: this,
|
|
870
|
+
type: "LiveList",
|
|
871
|
+
updates: [{
|
|
872
|
+
index: newIndex,
|
|
873
|
+
item: value instanceof LiveRegister ? value.data : value,
|
|
874
|
+
type: "insert"
|
|
875
|
+
}]
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
this._doc.dispatch(value._serialize(this._id, position, this._doc), [{
|
|
879
|
+
type: exports.OpType.DeleteCrdt,
|
|
880
|
+
id: _id
|
|
881
|
+
}], storageUpdates);
|
|
882
|
+
}
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
_proto.move = function move(index, targetIndex) {
|
|
886
|
+
if (targetIndex < 0) {
|
|
887
|
+
throw new Error("targetIndex cannot be less than 0");
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
if (targetIndex >= this._items.length) {
|
|
891
|
+
throw new Error("targetIndex cannot be greater or equal than the list length");
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
if (index < 0) {
|
|
895
|
+
throw new Error("index cannot be less than 0");
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
if (index >= this._items.length) {
|
|
899
|
+
throw new Error("index cannot be greater or equal than the list length");
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
var beforePosition = null;
|
|
903
|
+
var afterPosition = null;
|
|
904
|
+
|
|
905
|
+
if (index < targetIndex) {
|
|
906
|
+
afterPosition = targetIndex === this._items.length - 1 ? undefined : this._items[targetIndex + 1][1];
|
|
907
|
+
beforePosition = this._items[targetIndex][1];
|
|
908
|
+
} else {
|
|
909
|
+
afterPosition = this._items[targetIndex][1];
|
|
910
|
+
beforePosition = targetIndex === 0 ? undefined : this._items[targetIndex - 1][1];
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
var position = makePosition(beforePosition, afterPosition);
|
|
914
|
+
var item = this._items[index];
|
|
915
|
+
var previousPosition = item[1];
|
|
916
|
+
item[1] = position;
|
|
917
|
+
|
|
918
|
+
item[0]._setParentLink(this, position);
|
|
919
|
+
|
|
920
|
+
this._items.sort(function (itemA, itemB) {
|
|
921
|
+
return compare(itemA[1], itemB[1]);
|
|
922
|
+
});
|
|
923
|
+
|
|
924
|
+
var newIndex = this._items.findIndex(function (entry) {
|
|
925
|
+
return entry[1] === position;
|
|
926
|
+
});
|
|
927
|
+
|
|
928
|
+
if (this._doc && this._id) {
|
|
929
|
+
var storageUpdates = new Map();
|
|
930
|
+
storageUpdates.set(this._id, {
|
|
931
|
+
node: this,
|
|
932
|
+
type: "LiveList",
|
|
933
|
+
updates: [{
|
|
934
|
+
index: newIndex,
|
|
935
|
+
previousIndex: index,
|
|
936
|
+
item: item[0],
|
|
937
|
+
type: "move"
|
|
938
|
+
}]
|
|
939
|
+
});
|
|
940
|
+
|
|
941
|
+
this._doc.dispatch([{
|
|
942
|
+
type: exports.OpType.SetParentKey,
|
|
943
|
+
id: item[0]._id,
|
|
944
|
+
opId: this._doc.generateOpId(),
|
|
945
|
+
parentKey: position
|
|
946
|
+
}], [{
|
|
947
|
+
type: exports.OpType.SetParentKey,
|
|
948
|
+
id: item[0]._id,
|
|
949
|
+
parentKey: previousPosition
|
|
950
|
+
}], storageUpdates);
|
|
951
|
+
}
|
|
952
|
+
};
|
|
953
|
+
|
|
954
|
+
_proto.delete = function _delete(index) {
|
|
955
|
+
if (index < 0 || index >= this._items.length) {
|
|
956
|
+
throw new Error("Cannot delete list item at index \"\x1D" + index + "\". index should be between 0 and " + (this._items.length - 1));
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
var item = this._items[index];
|
|
960
|
+
|
|
961
|
+
item[0]._detach();
|
|
962
|
+
|
|
963
|
+
this._items.splice(index, 1);
|
|
964
|
+
|
|
965
|
+
if (this._doc) {
|
|
966
|
+
var childRecordId = item[0]._id;
|
|
967
|
+
|
|
968
|
+
if (childRecordId) {
|
|
969
|
+
var storageUpdates = new Map();
|
|
970
|
+
storageUpdates.set(this._id, {
|
|
971
|
+
node: this,
|
|
972
|
+
type: "LiveList",
|
|
973
|
+
updates: [{
|
|
974
|
+
index: index,
|
|
975
|
+
type: "delete"
|
|
976
|
+
}]
|
|
977
|
+
});
|
|
978
|
+
|
|
979
|
+
this._doc.dispatch([{
|
|
980
|
+
id: childRecordId,
|
|
981
|
+
opId: this._doc.generateOpId(),
|
|
982
|
+
type: exports.OpType.DeleteCrdt
|
|
983
|
+
}], item[0]._serialize(this._id, item[1]), storageUpdates);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
};
|
|
987
|
+
|
|
988
|
+
_proto.clear = function clear() {
|
|
989
|
+
if (this._doc) {
|
|
990
|
+
var ops = [];
|
|
991
|
+
var reverseOps = [];
|
|
992
|
+
var updateDelta = [];
|
|
993
|
+
var i = 0;
|
|
994
|
+
|
|
995
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(this._items), _step5; !(_step5 = _iterator5()).done;) {
|
|
996
|
+
var _item3 = _step5.value;
|
|
997
|
+
|
|
998
|
+
_item3[0]._detach();
|
|
999
|
+
|
|
1000
|
+
var childId = _item3[0]._id;
|
|
1001
|
+
|
|
1002
|
+
if (childId) {
|
|
1003
|
+
ops.push({
|
|
1004
|
+
id: childId,
|
|
1005
|
+
type: exports.OpType.DeleteCrdt
|
|
1006
|
+
});
|
|
1007
|
+
reverseOps.push.apply(reverseOps, _item3[0]._serialize(this._id, _item3[1]));
|
|
1008
|
+
updateDelta.push({
|
|
1009
|
+
index: i,
|
|
1010
|
+
type: "delete"
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
i++;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
this._items = [];
|
|
1018
|
+
var storageUpdates = new Map();
|
|
1019
|
+
storageUpdates.set(this._id, {
|
|
1020
|
+
node: this,
|
|
1021
|
+
type: "LiveList",
|
|
1022
|
+
updates: updateDelta
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
1026
|
+
} else {
|
|
1027
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(this._items), _step6; !(_step6 = _iterator6()).done;) {
|
|
1028
|
+
var _item4 = _step6.value;
|
|
1029
|
+
|
|
1030
|
+
_item4[0]._detach();
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
this._items = [];
|
|
1034
|
+
}
|
|
1035
|
+
};
|
|
1036
|
+
|
|
1037
|
+
_proto.set = function set(index, item) {
|
|
1038
|
+
if (index < 0 || index >= this._items.length) {
|
|
1039
|
+
throw new Error("Cannot set list item at index \"\x1D" + index + "\". index should be between 0 and " + (this._items.length - 1));
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
var _this$_items$index = this._items[index],
|
|
1043
|
+
existingItem = _this$_items$index[0],
|
|
1044
|
+
position = _this$_items$index[1];
|
|
1045
|
+
|
|
1046
|
+
existingItem._detach();
|
|
1047
|
+
|
|
1048
|
+
var value = selfOrRegister(item);
|
|
1049
|
+
|
|
1050
|
+
value._setParentLink(this, position);
|
|
1051
|
+
|
|
1052
|
+
this._items[index][0] = value;
|
|
1053
|
+
|
|
1054
|
+
if (this._doc && this._id) {
|
|
1055
|
+
var _id2 = this._doc.generateId();
|
|
1056
|
+
|
|
1057
|
+
value._attach(_id2, this._doc);
|
|
1058
|
+
|
|
1059
|
+
var storageUpdates = new Map();
|
|
1060
|
+
storageUpdates.set(this._id, {
|
|
1061
|
+
node: this,
|
|
1062
|
+
type: "LiveList",
|
|
1063
|
+
updates: [{
|
|
1064
|
+
index: index,
|
|
1065
|
+
item: value instanceof LiveRegister ? value.data : value,
|
|
1066
|
+
type: "set"
|
|
1067
|
+
}]
|
|
1068
|
+
});
|
|
1069
|
+
|
|
1070
|
+
this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, undefined, "set"), storageUpdates);
|
|
1071
|
+
}
|
|
1072
|
+
};
|
|
1073
|
+
|
|
1074
|
+
_proto.toArray = function toArray() {
|
|
1075
|
+
return this._items.map(function (entry) {
|
|
1076
|
+
return selfOrRegisterValue(entry[0]);
|
|
1077
|
+
});
|
|
1078
|
+
};
|
|
1079
|
+
|
|
1080
|
+
_proto.every = function every(predicate) {
|
|
1081
|
+
return this.toArray().every(predicate);
|
|
1082
|
+
};
|
|
1083
|
+
|
|
1084
|
+
_proto.filter = function filter(predicate) {
|
|
1085
|
+
return this.toArray().filter(predicate);
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
_proto.find = function find(predicate) {
|
|
1089
|
+
return this.toArray().find(predicate);
|
|
1090
|
+
};
|
|
1091
|
+
|
|
1092
|
+
_proto.findIndex = function findIndex(predicate) {
|
|
1093
|
+
return this.toArray().findIndex(predicate);
|
|
1094
|
+
};
|
|
1095
|
+
|
|
1096
|
+
_proto.forEach = function forEach(callbackfn) {
|
|
1097
|
+
return this.toArray().forEach(callbackfn);
|
|
1098
|
+
};
|
|
1099
|
+
|
|
1100
|
+
_proto.get = function get(index) {
|
|
1101
|
+
if (index < 0 || index >= this._items.length) {
|
|
1102
|
+
return undefined;
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
return selfOrRegisterValue(this._items[index][0]);
|
|
1106
|
+
};
|
|
1107
|
+
|
|
1108
|
+
_proto.indexOf = function indexOf(searchElement, fromIndex) {
|
|
1109
|
+
return this.toArray().indexOf(searchElement, fromIndex);
|
|
1110
|
+
};
|
|
1111
|
+
|
|
1112
|
+
_proto.lastIndexOf = function lastIndexOf(searchElement, fromIndex) {
|
|
1113
|
+
return this.toArray().lastIndexOf(searchElement, fromIndex);
|
|
1114
|
+
};
|
|
1115
|
+
|
|
1116
|
+
_proto.map = function map(callback) {
|
|
1117
|
+
return this._items.map(function (entry, i) {
|
|
1118
|
+
return callback(selfOrRegisterValue(entry[0]), i);
|
|
1119
|
+
});
|
|
1120
|
+
};
|
|
1121
|
+
|
|
1122
|
+
_proto.some = function some(predicate) {
|
|
1123
|
+
return this.toArray().some(predicate);
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
_proto[Symbol.iterator] = function () {
|
|
1127
|
+
return new LiveListIterator(this._items);
|
|
1128
|
+
};
|
|
1129
|
+
|
|
1130
|
+
_createClass(LiveList, [{
|
|
1131
|
+
key: "length",
|
|
1132
|
+
get: function get() {
|
|
1133
|
+
return this._items.length;
|
|
1134
|
+
}
|
|
1135
|
+
}]);
|
|
1136
|
+
|
|
1137
|
+
return LiveList;
|
|
1138
|
+
}(AbstractCrdt);
|
|
1139
|
+
|
|
1140
|
+
var LiveListIterator = function () {
|
|
1141
|
+
function LiveListIterator(items) {
|
|
1142
|
+
this._innerIterator = items[Symbol.iterator]();
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
var _proto2 = LiveListIterator.prototype;
|
|
1146
|
+
|
|
1147
|
+
_proto2[Symbol.iterator] = function () {
|
|
1148
|
+
return this;
|
|
1149
|
+
};
|
|
1150
|
+
|
|
1151
|
+
_proto2.next = function next() {
|
|
1152
|
+
var result = this._innerIterator.next();
|
|
1153
|
+
|
|
1154
|
+
if (result.done) {
|
|
1155
|
+
return {
|
|
1156
|
+
done: true,
|
|
1157
|
+
value: undefined
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
|
|
1161
|
+
return {
|
|
1162
|
+
value: selfOrRegisterValue(result.value[0])
|
|
1163
|
+
};
|
|
1164
|
+
};
|
|
1165
|
+
|
|
1166
|
+
return LiveListIterator;
|
|
1167
|
+
}();
|
|
1168
|
+
|
|
1169
|
+
var LiveMap = function (_AbstractCrdt) {
|
|
1170
|
+
_inheritsLoose(LiveMap, _AbstractCrdt);
|
|
1171
|
+
|
|
1172
|
+
function LiveMap(entries) {
|
|
1173
|
+
var _this;
|
|
1174
|
+
|
|
1175
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
1176
|
+
|
|
1177
|
+
if (entries) {
|
|
1178
|
+
var mappedEntries = [];
|
|
1179
|
+
|
|
1180
|
+
for (var _iterator = _createForOfIteratorHelperLoose(entries), _step; !(_step = _iterator()).done;) {
|
|
1181
|
+
var entry = _step.value;
|
|
1182
|
+
|
|
1183
|
+
var _value = selfOrRegister(entry[1]);
|
|
1184
|
+
|
|
1185
|
+
_value._setParentLink(_assertThisInitialized(_this), entry[0]);
|
|
1186
|
+
|
|
1187
|
+
mappedEntries.push([entry[0], _value]);
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
_this._map = new Map(mappedEntries);
|
|
1191
|
+
} else {
|
|
1192
|
+
_this._map = new Map();
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
return _this;
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
var _proto = LiveMap.prototype;
|
|
1199
|
+
|
|
1200
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
|
|
1201
|
+
if (this._id == null) {
|
|
1202
|
+
throw new Error("Cannot serialize item is not attached");
|
|
1203
|
+
}
|
|
1204
|
+
|
|
1205
|
+
if (parentId == null || parentKey == null) {
|
|
1206
|
+
throw new Error("Cannot serialize map if parentId or parentKey is undefined");
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
var ops = [];
|
|
1210
|
+
var op = {
|
|
1211
|
+
id: this._id,
|
|
1212
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
1213
|
+
type: exports.OpType.CreateMap,
|
|
1214
|
+
intent: intent,
|
|
1215
|
+
parentId: parentId,
|
|
1216
|
+
parentKey: parentKey
|
|
1217
|
+
};
|
|
1218
|
+
ops.push(op);
|
|
1219
|
+
|
|
1220
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(this._map), _step2; !(_step2 = _iterator2()).done;) {
|
|
1221
|
+
var _step2$value = _step2.value,
|
|
1222
|
+
_key2 = _step2$value[0],
|
|
1223
|
+
_value2 = _step2$value[1];
|
|
1224
|
+
ops.push.apply(ops, _value2._serialize(this._id, _key2, doc));
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
return ops;
|
|
1228
|
+
};
|
|
1229
|
+
|
|
1230
|
+
LiveMap._deserialize = function _deserialize(_ref, parentToChildren, doc) {
|
|
1231
|
+
var id = _ref[0],
|
|
1232
|
+
item = _ref[1];
|
|
1233
|
+
|
|
1234
|
+
if (item.type !== exports.CrdtType.Map) {
|
|
1235
|
+
throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
|
|
1236
|
+
}
|
|
1237
|
+
|
|
1238
|
+
var map = new LiveMap();
|
|
1239
|
+
|
|
1240
|
+
map._attach(id, doc);
|
|
1241
|
+
|
|
1242
|
+
var children = parentToChildren.get(id);
|
|
1243
|
+
|
|
1244
|
+
if (children == null) {
|
|
1245
|
+
return map;
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(children), _step3; !(_step3 = _iterator3()).done;) {
|
|
1249
|
+
var entry = _step3.value;
|
|
1250
|
+
var crdt = entry[1];
|
|
1251
|
+
|
|
1252
|
+
if (crdt.parentKey == null) {
|
|
1253
|
+
throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
1257
|
+
|
|
1258
|
+
child._setParentLink(map, crdt.parentKey);
|
|
1259
|
+
|
|
1260
|
+
map._map.set(crdt.parentKey, child);
|
|
1261
|
+
}
|
|
1262
|
+
|
|
1263
|
+
return map;
|
|
1264
|
+
};
|
|
1265
|
+
|
|
1266
|
+
_proto._attach = function _attach(id, doc) {
|
|
1267
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
1268
|
+
|
|
1269
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(this._map), _step4; !(_step4 = _iterator4()).done;) {
|
|
1270
|
+
var _step4$value = _step4.value;
|
|
1271
|
+
_step4$value[0];
|
|
1272
|
+
var _value3 = _step4$value[1];
|
|
1273
|
+
|
|
1274
|
+
if (isCrdt(_value3)) {
|
|
1275
|
+
_value3._attach(doc.generateId(), doc);
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
};
|
|
1279
|
+
|
|
1280
|
+
_proto._attachChild = function _attachChild(op, _isLocal) {
|
|
1281
|
+
var _updates;
|
|
1282
|
+
|
|
1283
|
+
if (this._doc == null) {
|
|
1284
|
+
throw new Error("Can't attach child if doc is not present");
|
|
1285
|
+
}
|
|
1286
|
+
|
|
1287
|
+
var id = op.id,
|
|
1288
|
+
parentKey = op.parentKey;
|
|
1289
|
+
var key = parentKey;
|
|
1290
|
+
var child = creationOpToLiveStructure(op);
|
|
1291
|
+
|
|
1292
|
+
if (this._doc.getItem(id) !== undefined) {
|
|
1293
|
+
return {
|
|
1294
|
+
modified: false
|
|
1295
|
+
};
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
var previousValue = this._map.get(key);
|
|
1299
|
+
|
|
1300
|
+
var reverse;
|
|
1301
|
+
|
|
1302
|
+
if (previousValue) {
|
|
1303
|
+
reverse = previousValue._serialize(this._id, key);
|
|
1304
|
+
|
|
1305
|
+
previousValue._detach();
|
|
1306
|
+
} else {
|
|
1307
|
+
reverse = [{
|
|
1308
|
+
type: exports.OpType.DeleteCrdt,
|
|
1309
|
+
id: id
|
|
1310
|
+
}];
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
child._setParentLink(this, key);
|
|
1314
|
+
|
|
1315
|
+
child._attach(id, this._doc);
|
|
1316
|
+
|
|
1317
|
+
this._map.set(key, child);
|
|
1318
|
+
|
|
1319
|
+
return {
|
|
1320
|
+
modified: {
|
|
1321
|
+
node: this,
|
|
1322
|
+
type: "LiveMap",
|
|
1323
|
+
updates: (_updates = {}, _updates[key] = {
|
|
1324
|
+
type: "update"
|
|
1325
|
+
}, _updates)
|
|
1326
|
+
},
|
|
1327
|
+
reverse: reverse
|
|
1328
|
+
};
|
|
1329
|
+
};
|
|
1330
|
+
|
|
1331
|
+
_proto._detach = function _detach() {
|
|
1332
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
1333
|
+
|
|
1334
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(this._map.values()), _step5; !(_step5 = _iterator5()).done;) {
|
|
1335
|
+
var item = _step5.value;
|
|
1336
|
+
|
|
1337
|
+
item._detach();
|
|
1338
|
+
}
|
|
1339
|
+
};
|
|
1340
|
+
|
|
1341
|
+
_proto._detachChild = function _detachChild(child) {
|
|
1342
|
+
var _updates2;
|
|
1343
|
+
|
|
1344
|
+
var reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
1345
|
+
|
|
1346
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(this._map), _step6; !(_step6 = _iterator6()).done;) {
|
|
1347
|
+
var _step6$value = _step6.value,
|
|
1348
|
+
_key3 = _step6$value[0],
|
|
1349
|
+
_value4 = _step6$value[1];
|
|
1350
|
+
|
|
1351
|
+
if (_value4 === child) {
|
|
1352
|
+
this._map.delete(_key3);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
child._detach();
|
|
1357
|
+
|
|
1358
|
+
var storageUpdate = {
|
|
1359
|
+
node: this,
|
|
1360
|
+
type: "LiveMap",
|
|
1361
|
+
updates: (_updates2 = {}, _updates2[child._parentKey] = {
|
|
1362
|
+
type: "delete"
|
|
1363
|
+
}, _updates2)
|
|
1364
|
+
};
|
|
1365
|
+
return {
|
|
1366
|
+
modified: storageUpdate,
|
|
1367
|
+
reverse: reverse
|
|
1368
|
+
};
|
|
1369
|
+
};
|
|
1370
|
+
|
|
1371
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
1372
|
+
var _this$_parent;
|
|
1373
|
+
|
|
1374
|
+
return {
|
|
1375
|
+
type: exports.CrdtType.Map,
|
|
1376
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
1377
|
+
parentKey: this._parentKey
|
|
1378
|
+
};
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
_proto.get = function get(key) {
|
|
1382
|
+
var value = this._map.get(key);
|
|
1383
|
+
|
|
1384
|
+
if (value == undefined) {
|
|
1385
|
+
return undefined;
|
|
1386
|
+
}
|
|
1387
|
+
|
|
1388
|
+
return selfOrRegisterValue(value);
|
|
1389
|
+
};
|
|
1390
|
+
|
|
1391
|
+
_proto.set = function set(key, value) {
|
|
1392
|
+
var oldValue = this._map.get(key);
|
|
1393
|
+
|
|
1394
|
+
if (oldValue) {
|
|
1395
|
+
oldValue._detach();
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
var item = selfOrRegister(value);
|
|
1399
|
+
|
|
1400
|
+
item._setParentLink(this, key);
|
|
1401
|
+
|
|
1402
|
+
this._map.set(key, item);
|
|
1403
|
+
|
|
1404
|
+
if (this._doc && this._id) {
|
|
1405
|
+
var _updates3;
|
|
1406
|
+
|
|
1407
|
+
var id = this._doc.generateId();
|
|
1408
|
+
|
|
1409
|
+
item._attach(id, this._doc);
|
|
1410
|
+
|
|
1411
|
+
var storageUpdates = new Map();
|
|
1412
|
+
storageUpdates.set(this._id, {
|
|
1413
|
+
node: this,
|
|
1414
|
+
type: "LiveMap",
|
|
1415
|
+
updates: (_updates3 = {}, _updates3[key] = {
|
|
1416
|
+
type: "update"
|
|
1417
|
+
}, _updates3)
|
|
1418
|
+
});
|
|
1419
|
+
|
|
1420
|
+
this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue ? oldValue._serialize(this._id, key) : [{
|
|
1421
|
+
type: exports.OpType.DeleteCrdt,
|
|
1422
|
+
id: id
|
|
1423
|
+
}], storageUpdates);
|
|
1424
|
+
}
|
|
1425
|
+
};
|
|
1426
|
+
|
|
1427
|
+
_proto.has = function has(key) {
|
|
1428
|
+
return this._map.has(key);
|
|
1429
|
+
};
|
|
1430
|
+
|
|
1431
|
+
_proto.delete = function _delete(key) {
|
|
1432
|
+
var item = this._map.get(key);
|
|
1433
|
+
|
|
1434
|
+
if (item == null) {
|
|
1435
|
+
return false;
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
item._detach();
|
|
1439
|
+
|
|
1440
|
+
this._map.delete(key);
|
|
1441
|
+
|
|
1442
|
+
if (this._doc && item._id) {
|
|
1443
|
+
var _updates4;
|
|
1444
|
+
|
|
1445
|
+
var storageUpdates = new Map();
|
|
1446
|
+
storageUpdates.set(this._id, {
|
|
1447
|
+
node: this,
|
|
1448
|
+
type: "LiveMap",
|
|
1449
|
+
updates: (_updates4 = {}, _updates4[key] = {
|
|
1450
|
+
type: "delete"
|
|
1451
|
+
}, _updates4)
|
|
1452
|
+
});
|
|
1453
|
+
|
|
1454
|
+
this._doc.dispatch([{
|
|
1455
|
+
type: exports.OpType.DeleteCrdt,
|
|
1456
|
+
id: item._id,
|
|
1457
|
+
opId: this._doc.generateOpId()
|
|
1458
|
+
}], item._serialize(this._id, key), storageUpdates);
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
return true;
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1464
|
+
_proto.entries = function entries() {
|
|
1465
|
+
var _ref2;
|
|
1466
|
+
|
|
1467
|
+
var innerIterator = this._map.entries();
|
|
1468
|
+
|
|
1469
|
+
return _ref2 = {}, _ref2[Symbol.iterator] = function () {
|
|
1470
|
+
return this;
|
|
1471
|
+
}, _ref2.next = function next() {
|
|
1472
|
+
var iteratorValue = innerIterator.next();
|
|
1473
|
+
|
|
1474
|
+
if (iteratorValue.done) {
|
|
1475
|
+
return {
|
|
1476
|
+
done: true,
|
|
1477
|
+
value: undefined
|
|
1478
|
+
};
|
|
1479
|
+
}
|
|
1480
|
+
|
|
1481
|
+
var entry = iteratorValue.value;
|
|
1482
|
+
return {
|
|
1483
|
+
value: [entry[0], selfOrRegisterValue(iteratorValue.value[1])]
|
|
1484
|
+
};
|
|
1485
|
+
}, _ref2;
|
|
1486
|
+
};
|
|
1487
|
+
|
|
1488
|
+
_proto[Symbol.iterator] = function () {
|
|
1489
|
+
return this.entries();
|
|
1490
|
+
};
|
|
1491
|
+
|
|
1492
|
+
_proto.keys = function keys() {
|
|
1493
|
+
return this._map.keys();
|
|
1494
|
+
};
|
|
1495
|
+
|
|
1496
|
+
_proto.values = function values() {
|
|
1497
|
+
var _ref3;
|
|
1498
|
+
|
|
1499
|
+
var innerIterator = this._map.values();
|
|
1500
|
+
|
|
1501
|
+
return _ref3 = {}, _ref3[Symbol.iterator] = function () {
|
|
1502
|
+
return this;
|
|
1503
|
+
}, _ref3.next = function next() {
|
|
1504
|
+
var iteratorValue = innerIterator.next();
|
|
1505
|
+
|
|
1506
|
+
if (iteratorValue.done) {
|
|
1507
|
+
return {
|
|
1508
|
+
done: true,
|
|
1509
|
+
value: undefined
|
|
1510
|
+
};
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
return {
|
|
1514
|
+
value: selfOrRegisterValue(iteratorValue.value)
|
|
1515
|
+
};
|
|
1516
|
+
}, _ref3;
|
|
1517
|
+
};
|
|
1518
|
+
|
|
1519
|
+
_proto.forEach = function forEach(callback) {
|
|
1520
|
+
for (var _iterator7 = _createForOfIteratorHelperLoose(this), _step7; !(_step7 = _iterator7()).done;) {
|
|
1521
|
+
var entry = _step7.value;
|
|
1522
|
+
callback(entry[1], entry[0], this);
|
|
1523
|
+
}
|
|
1524
|
+
};
|
|
1525
|
+
|
|
1526
|
+
_createClass(LiveMap, [{
|
|
1527
|
+
key: "size",
|
|
1528
|
+
get: function get() {
|
|
1529
|
+
return this._map.size;
|
|
1530
|
+
}
|
|
1531
|
+
}]);
|
|
1532
|
+
|
|
1533
|
+
return LiveMap;
|
|
1534
|
+
}(AbstractCrdt);
|
|
1535
|
+
|
|
1536
|
+
function parseJson(rawMessage) {
|
|
1537
|
+
try {
|
|
1538
|
+
return JSON.parse(rawMessage);
|
|
1539
|
+
} catch (e) {
|
|
1540
|
+
return undefined;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
function isJsonArray(data) {
|
|
1544
|
+
return Array.isArray(data);
|
|
1545
|
+
}
|
|
1546
|
+
function isJsonObject(data) {
|
|
1547
|
+
return data !== null && typeof data === "object" && !isJsonArray(data);
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
var _emittedDeprecationWarnings = new Set();
|
|
1551
|
+
|
|
1552
|
+
function deprecate(message, key) {
|
|
1553
|
+
if (key === void 0) {
|
|
1554
|
+
key = message;
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1558
|
+
if (!_emittedDeprecationWarnings.has(key)) {
|
|
1559
|
+
_emittedDeprecationWarnings.add(key);
|
|
1560
|
+
|
|
1561
|
+
console.warn("DEPRECATION WARNING: " + message);
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
function deprecateIf(condition, message, key) {
|
|
1566
|
+
if (key === void 0) {
|
|
1567
|
+
key = message;
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
if (process.env.NODE_ENV !== "production") {
|
|
1571
|
+
if (condition) {
|
|
1572
|
+
deprecate(message, key);
|
|
1573
|
+
}
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
function remove(array, item) {
|
|
1577
|
+
for (var i = 0; i < array.length; i++) {
|
|
1578
|
+
if (array[i] === item) {
|
|
1579
|
+
array.splice(i, 1);
|
|
1580
|
+
break;
|
|
1581
|
+
}
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
function compact(items) {
|
|
1585
|
+
return items.filter(function (item) {
|
|
1586
|
+
return item != null;
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
function creationOpToLiveStructure(op) {
|
|
1590
|
+
switch (op.type) {
|
|
1591
|
+
case exports.OpType.CreateRegister:
|
|
1592
|
+
return new LiveRegister(op.data);
|
|
1593
|
+
|
|
1594
|
+
case exports.OpType.CreateObject:
|
|
1595
|
+
return new LiveObject(op.data);
|
|
1596
|
+
|
|
1597
|
+
case exports.OpType.CreateMap:
|
|
1598
|
+
return new LiveMap();
|
|
1599
|
+
|
|
1600
|
+
case exports.OpType.CreateList:
|
|
1601
|
+
return new LiveList();
|
|
1602
|
+
}
|
|
1603
|
+
}
|
|
1604
|
+
function isSameNodeOrChildOf(node, parent) {
|
|
1605
|
+
if (node === parent) {
|
|
1606
|
+
return true;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
if (node._parent) {
|
|
1610
|
+
return isSameNodeOrChildOf(node._parent, parent);
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
return false;
|
|
1614
|
+
}
|
|
1615
|
+
function deserialize(entry, parentToChildren, doc) {
|
|
1616
|
+
switch (entry[1].type) {
|
|
1617
|
+
case exports.CrdtType.Object:
|
|
1618
|
+
{
|
|
1619
|
+
return LiveObject._deserialize(entry, parentToChildren, doc);
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
case exports.CrdtType.List:
|
|
1623
|
+
{
|
|
1624
|
+
return LiveList._deserialize(entry, parentToChildren, doc);
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
case exports.CrdtType.Map:
|
|
1628
|
+
{
|
|
1629
|
+
return LiveMap._deserialize(entry, parentToChildren, doc);
|
|
1630
|
+
}
|
|
1631
|
+
|
|
1632
|
+
case exports.CrdtType.Register:
|
|
1633
|
+
{
|
|
1634
|
+
return LiveRegister._deserialize(entry, parentToChildren, doc);
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
default:
|
|
1638
|
+
{
|
|
1639
|
+
throw new Error("Unexpected CRDT type");
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
function isCrdt(obj) {
|
|
1644
|
+
return obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList || obj instanceof LiveRegister;
|
|
1645
|
+
}
|
|
1646
|
+
function selfOrRegisterValue(obj) {
|
|
1647
|
+
if (obj instanceof LiveRegister) {
|
|
1648
|
+
return obj.data;
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
return obj;
|
|
1652
|
+
}
|
|
1653
|
+
function selfOrRegister(obj) {
|
|
1654
|
+
if (obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList) {
|
|
1655
|
+
return obj;
|
|
1656
|
+
} else if (obj instanceof LiveRegister) {
|
|
1657
|
+
throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
|
|
1658
|
+
} else {
|
|
1659
|
+
return new LiveRegister(obj);
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
function getTreesDiffOperations(currentItems, newItems) {
|
|
1663
|
+
var ops = [];
|
|
1664
|
+
currentItems.forEach(function (_, id) {
|
|
1665
|
+
if (!newItems.get(id)) {
|
|
1666
|
+
ops.push({
|
|
1667
|
+
type: exports.OpType.DeleteCrdt,
|
|
1668
|
+
id: id
|
|
1669
|
+
});
|
|
1670
|
+
}
|
|
1671
|
+
});
|
|
1672
|
+
newItems.forEach(function (crdt, id) {
|
|
1673
|
+
var currentCrdt = currentItems.get(id);
|
|
1674
|
+
|
|
1675
|
+
if (currentCrdt) {
|
|
1676
|
+
if (crdt.type === exports.CrdtType.Object) {
|
|
1677
|
+
if (JSON.stringify(crdt.data) !== JSON.stringify(currentCrdt.data)) {
|
|
1678
|
+
ops.push({
|
|
1679
|
+
type: exports.OpType.UpdateObject,
|
|
1680
|
+
id: id,
|
|
1681
|
+
data: crdt.data
|
|
1682
|
+
});
|
|
1683
|
+
}
|
|
1684
|
+
}
|
|
1685
|
+
|
|
1686
|
+
if (crdt.parentKey !== currentCrdt.parentKey) {
|
|
1687
|
+
ops.push({
|
|
1688
|
+
type: exports.OpType.SetParentKey,
|
|
1689
|
+
id: id,
|
|
1690
|
+
parentKey: crdt.parentKey
|
|
1691
|
+
});
|
|
1692
|
+
}
|
|
1693
|
+
} else {
|
|
1694
|
+
switch (crdt.type) {
|
|
1695
|
+
case exports.CrdtType.Register:
|
|
1696
|
+
ops.push({
|
|
1697
|
+
type: exports.OpType.CreateRegister,
|
|
1698
|
+
id: id,
|
|
1699
|
+
parentId: crdt.parentId,
|
|
1700
|
+
parentKey: crdt.parentKey,
|
|
1701
|
+
data: crdt.data
|
|
1702
|
+
});
|
|
1703
|
+
break;
|
|
1704
|
+
|
|
1705
|
+
case exports.CrdtType.List:
|
|
1706
|
+
ops.push({
|
|
1707
|
+
type: exports.OpType.CreateList,
|
|
1708
|
+
id: id,
|
|
1709
|
+
parentId: crdt.parentId,
|
|
1710
|
+
parentKey: crdt.parentKey
|
|
1711
|
+
});
|
|
1712
|
+
break;
|
|
1713
|
+
|
|
1714
|
+
case exports.CrdtType.Object:
|
|
1715
|
+
ops.push({
|
|
1716
|
+
type: exports.OpType.CreateObject,
|
|
1717
|
+
id: id,
|
|
1718
|
+
parentId: crdt.parentId,
|
|
1719
|
+
parentKey: crdt.parentKey,
|
|
1720
|
+
data: crdt.data
|
|
1721
|
+
});
|
|
1722
|
+
break;
|
|
1723
|
+
|
|
1724
|
+
case exports.CrdtType.Map:
|
|
1725
|
+
ops.push({
|
|
1726
|
+
type: exports.OpType.CreateMap,
|
|
1727
|
+
id: id,
|
|
1728
|
+
parentId: crdt.parentId,
|
|
1729
|
+
parentKey: crdt.parentKey
|
|
1730
|
+
});
|
|
1731
|
+
break;
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
});
|
|
1735
|
+
return ops;
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
function mergeObjectStorageUpdates(first, second) {
|
|
1739
|
+
var updates = first.updates;
|
|
1740
|
+
|
|
1741
|
+
for (var _iterator = _createForOfIteratorHelperLoose(entries(second.updates)), _step; !(_step = _iterator()).done;) {
|
|
1742
|
+
var _step$value = _step.value,
|
|
1743
|
+
_key = _step$value[0],
|
|
1744
|
+
value = _step$value[1];
|
|
1745
|
+
updates[_key] = value;
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
return _extends({}, second, {
|
|
1749
|
+
updates: updates
|
|
1750
|
+
});
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
function mergeMapStorageUpdates(first, second) {
|
|
1754
|
+
var updates = first.updates;
|
|
1755
|
+
|
|
1756
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(entries(second.updates)), _step2; !(_step2 = _iterator2()).done;) {
|
|
1757
|
+
var _step2$value = _step2.value,
|
|
1758
|
+
_key2 = _step2$value[0],
|
|
1759
|
+
value = _step2$value[1];
|
|
1760
|
+
updates[_key2] = value;
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
return _extends({}, second, {
|
|
1764
|
+
updates: updates
|
|
1765
|
+
});
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
function mergeListStorageUpdates(first, second) {
|
|
1769
|
+
var updates = first.updates;
|
|
1770
|
+
return _extends({}, second, {
|
|
1771
|
+
updates: updates.concat(second.updates)
|
|
1772
|
+
});
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
function mergeStorageUpdates(first, second) {
|
|
1776
|
+
if (!first) {
|
|
1777
|
+
return second;
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
if (first.type === "LiveObject" && second.type === "LiveObject") {
|
|
1781
|
+
return mergeObjectStorageUpdates(first, second);
|
|
1782
|
+
} else if (first.type === "LiveMap" && second.type === "LiveMap") {
|
|
1783
|
+
return mergeMapStorageUpdates(first, second);
|
|
1784
|
+
} else if (first.type === "LiveList" && second.type === "LiveList") {
|
|
1785
|
+
return mergeListStorageUpdates(first, second);
|
|
1786
|
+
} else ;
|
|
1787
|
+
|
|
1788
|
+
return second;
|
|
1789
|
+
}
|
|
1790
|
+
|
|
1791
|
+
function isPlain(value) {
|
|
1792
|
+
var type = typeof value;
|
|
1793
|
+
return type === "undefined" || value === null || type === "string" || type === "boolean" || type === "number" || Array.isArray(value) || isPlainObject(value);
|
|
1794
|
+
}
|
|
1795
|
+
|
|
1796
|
+
function isPlainObject(value) {
|
|
1797
|
+
if (typeof value !== "object" || value === null) return false;
|
|
1798
|
+
var proto = Object.getPrototypeOf(value);
|
|
1799
|
+
if (proto === null) return true;
|
|
1800
|
+
var baseProto = proto;
|
|
1801
|
+
|
|
1802
|
+
while (Object.getPrototypeOf(baseProto) !== null) {
|
|
1803
|
+
baseProto = Object.getPrototypeOf(baseProto);
|
|
1804
|
+
}
|
|
1805
|
+
|
|
1806
|
+
return proto === baseProto;
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
function findNonSerializableValue(value, path) {
|
|
1810
|
+
if (path === void 0) {
|
|
1811
|
+
path = "";
|
|
1812
|
+
}
|
|
1813
|
+
|
|
1814
|
+
if (!isPlain) {
|
|
1815
|
+
return {
|
|
1816
|
+
path: path || "root",
|
|
1817
|
+
value: value
|
|
1818
|
+
};
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
if (typeof value !== "object" || value === null) {
|
|
1822
|
+
return false;
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
for (var _i = 0, _Object$entries = Object.entries(value); _i < _Object$entries.length; _i++) {
|
|
1826
|
+
var _Object$entries$_i = _Object$entries[_i],
|
|
1827
|
+
_key3 = _Object$entries$_i[0],
|
|
1828
|
+
nestedValue = _Object$entries$_i[1];
|
|
1829
|
+
var nestedPath = path ? path + "." + _key3 : _key3;
|
|
1830
|
+
|
|
1831
|
+
if (!isPlain(nestedValue)) {
|
|
1832
|
+
return {
|
|
1833
|
+
path: nestedPath,
|
|
1834
|
+
value: nestedValue
|
|
1835
|
+
};
|
|
1836
|
+
}
|
|
1837
|
+
|
|
1838
|
+
if (typeof nestedValue === "object") {
|
|
1839
|
+
var nonSerializableNestedValue = findNonSerializableValue(nestedValue, nestedPath);
|
|
1840
|
+
|
|
1841
|
+
if (nonSerializableNestedValue) {
|
|
1842
|
+
return nonSerializableNestedValue;
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
}
|
|
1846
|
+
|
|
1847
|
+
return false;
|
|
1848
|
+
}
|
|
1849
|
+
function isTokenValid(token) {
|
|
1850
|
+
var tokenParts = token.split(".");
|
|
1851
|
+
|
|
1852
|
+
if (tokenParts.length !== 3) {
|
|
1853
|
+
return false;
|
|
1854
|
+
}
|
|
1855
|
+
|
|
1856
|
+
var data = parseJson(atob(tokenParts[1]));
|
|
1857
|
+
|
|
1858
|
+
if (data === undefined || !isJsonObject(data) || typeof data.exp !== "number") {
|
|
1859
|
+
return false;
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1862
|
+
var now = Date.now();
|
|
1863
|
+
|
|
1864
|
+
if (now / 1000 > data.exp - 300) {
|
|
1865
|
+
return false;
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
return true;
|
|
1869
|
+
}
|
|
1870
|
+
function entries(obj) {
|
|
1871
|
+
return Object.entries(obj);
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
var LiveObject = function (_AbstractCrdt) {
|
|
1875
|
+
_inheritsLoose(LiveObject, _AbstractCrdt);
|
|
1876
|
+
|
|
1877
|
+
function LiveObject(object) {
|
|
1878
|
+
var _this;
|
|
1879
|
+
|
|
1880
|
+
if (object === void 0) {
|
|
1881
|
+
object = {};
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
1885
|
+
_this._propToLastUpdate = new Map();
|
|
1886
|
+
|
|
1887
|
+
for (var key in object) {
|
|
1888
|
+
var value = object[key];
|
|
1889
|
+
|
|
1890
|
+
if (value instanceof AbstractCrdt) {
|
|
1891
|
+
value._setParentLink(_assertThisInitialized(_this), key);
|
|
1892
|
+
}
|
|
1893
|
+
}
|
|
1894
|
+
|
|
1895
|
+
_this._map = new Map(Object.entries(object));
|
|
1896
|
+
return _this;
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
var _proto = LiveObject.prototype;
|
|
1900
|
+
|
|
1901
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc, intent) {
|
|
1902
|
+
if (this._id == null) {
|
|
1903
|
+
throw new Error("Cannot serialize item is not attached");
|
|
1904
|
+
}
|
|
1905
|
+
|
|
1906
|
+
var ops = [];
|
|
1907
|
+
var op = {
|
|
1908
|
+
id: this._id,
|
|
1909
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
1910
|
+
intent: intent,
|
|
1911
|
+
type: exports.OpType.CreateObject,
|
|
1912
|
+
parentId: parentId,
|
|
1913
|
+
parentKey: parentKey,
|
|
1914
|
+
data: {}
|
|
1915
|
+
};
|
|
1916
|
+
ops.push(op);
|
|
1917
|
+
|
|
1918
|
+
for (var _iterator = _createForOfIteratorHelperLoose(this._map), _step; !(_step = _iterator()).done;) {
|
|
1919
|
+
var _step$value = _step.value,
|
|
1920
|
+
key = _step$value[0],
|
|
1921
|
+
value = _step$value[1];
|
|
1922
|
+
|
|
1923
|
+
if (value instanceof AbstractCrdt) {
|
|
1924
|
+
ops.push.apply(ops, value._serialize(this._id, key, doc));
|
|
1925
|
+
} else {
|
|
1926
|
+
op.data[key] = value;
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
return ops;
|
|
1931
|
+
};
|
|
1932
|
+
|
|
1933
|
+
LiveObject._deserialize = function _deserialize(_ref, parentToChildren, doc) {
|
|
1934
|
+
var id = _ref[0],
|
|
1935
|
+
item = _ref[1];
|
|
1936
|
+
|
|
1937
|
+
if (item.type !== exports.CrdtType.Object) {
|
|
1938
|
+
throw new Error("Tried to deserialize a record but item type is \"" + item.type + "\"");
|
|
1939
|
+
}
|
|
1940
|
+
|
|
1941
|
+
var object = new LiveObject(item.data);
|
|
1942
|
+
|
|
1943
|
+
object._attach(id, doc);
|
|
1944
|
+
|
|
1945
|
+
return this._deserializeChildren(object, parentToChildren, doc);
|
|
1946
|
+
};
|
|
1947
|
+
|
|
1948
|
+
LiveObject._deserializeChildren = function _deserializeChildren(object, parentToChildren, doc) {
|
|
1949
|
+
var children = parentToChildren.get(object._id);
|
|
1950
|
+
|
|
1951
|
+
if (children == null) {
|
|
1952
|
+
return object;
|
|
1953
|
+
}
|
|
1954
|
+
|
|
1955
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(children), _step2; !(_step2 = _iterator2()).done;) {
|
|
1956
|
+
var entry = _step2.value;
|
|
1957
|
+
var crdt = entry[1];
|
|
1958
|
+
|
|
1959
|
+
if (crdt.parentKey == null) {
|
|
1960
|
+
throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
|
|
1961
|
+
}
|
|
1962
|
+
|
|
1963
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
1964
|
+
|
|
1965
|
+
child._setParentLink(object, crdt.parentKey);
|
|
1966
|
+
|
|
1967
|
+
object._map.set(crdt.parentKey, child);
|
|
1968
|
+
}
|
|
1969
|
+
|
|
1970
|
+
return object;
|
|
1971
|
+
};
|
|
1972
|
+
|
|
1973
|
+
_proto._attach = function _attach(id, doc) {
|
|
1974
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
1975
|
+
|
|
1976
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(this._map), _step3; !(_step3 = _iterator3()).done;) {
|
|
1977
|
+
var _step3$value = _step3.value;
|
|
1978
|
+
_step3$value[0];
|
|
1979
|
+
var value = _step3$value[1];
|
|
1980
|
+
|
|
1981
|
+
if (value instanceof AbstractCrdt) {
|
|
1982
|
+
value._attach(doc.generateId(), doc);
|
|
1983
|
+
}
|
|
1984
|
+
}
|
|
1985
|
+
};
|
|
1986
|
+
|
|
1987
|
+
_proto._attachChild = function _attachChild(op, isLocal) {
|
|
1988
|
+
var _updates;
|
|
1989
|
+
|
|
1990
|
+
if (this._doc == null) {
|
|
1991
|
+
throw new Error("Can't attach child if doc is not present");
|
|
1992
|
+
}
|
|
1993
|
+
|
|
1994
|
+
var id = op.id,
|
|
1995
|
+
parentKey = op.parentKey,
|
|
1996
|
+
opId = op.opId;
|
|
1997
|
+
var key = parentKey;
|
|
1998
|
+
var child = creationOpToLiveStructure(op);
|
|
1999
|
+
|
|
2000
|
+
if (this._doc.getItem(id) !== undefined) {
|
|
2001
|
+
if (this._propToLastUpdate.get(key) === opId) {
|
|
2002
|
+
this._propToLastUpdate.delete(key);
|
|
2003
|
+
}
|
|
2004
|
+
|
|
2005
|
+
return {
|
|
2006
|
+
modified: false
|
|
2007
|
+
};
|
|
2008
|
+
}
|
|
2009
|
+
|
|
2010
|
+
if (isLocal) {
|
|
2011
|
+
this._propToLastUpdate.set(key, opId);
|
|
2012
|
+
} else if (this._propToLastUpdate.get(key) === undefined) ; else if (this._propToLastUpdate.get(key) === opId) {
|
|
2013
|
+
this._propToLastUpdate.delete(key);
|
|
2014
|
+
|
|
2015
|
+
return {
|
|
2016
|
+
modified: false
|
|
2017
|
+
};
|
|
2018
|
+
} else {
|
|
2019
|
+
return {
|
|
2020
|
+
modified: false
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
var previousValue = this._map.get(key);
|
|
2025
|
+
|
|
2026
|
+
var reverse;
|
|
2027
|
+
|
|
2028
|
+
if (isCrdt(previousValue)) {
|
|
2029
|
+
reverse = previousValue._serialize(this._id, key);
|
|
2030
|
+
|
|
2031
|
+
previousValue._detach();
|
|
2032
|
+
} else if (previousValue === undefined) {
|
|
2033
|
+
reverse = [{
|
|
2034
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2035
|
+
id: this._id,
|
|
2036
|
+
key: key
|
|
2037
|
+
}];
|
|
2038
|
+
} else {
|
|
2039
|
+
var _data;
|
|
2040
|
+
|
|
2041
|
+
reverse = [{
|
|
2042
|
+
type: exports.OpType.UpdateObject,
|
|
2043
|
+
id: this._id,
|
|
2044
|
+
data: (_data = {}, _data[key] = previousValue, _data)
|
|
2045
|
+
}];
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
this._map.set(key, child);
|
|
2049
|
+
|
|
2050
|
+
child._setParentLink(this, key);
|
|
2051
|
+
|
|
2052
|
+
child._attach(id, this._doc);
|
|
2053
|
+
|
|
2054
|
+
return {
|
|
2055
|
+
reverse: reverse,
|
|
2056
|
+
modified: {
|
|
2057
|
+
node: this,
|
|
2058
|
+
type: "LiveObject",
|
|
2059
|
+
updates: (_updates = {}, _updates[key] = {
|
|
2060
|
+
type: "update"
|
|
2061
|
+
}, _updates)
|
|
2062
|
+
}
|
|
2063
|
+
};
|
|
2064
|
+
};
|
|
2065
|
+
|
|
2066
|
+
_proto._detachChild = function _detachChild(child) {
|
|
2067
|
+
if (child) {
|
|
2068
|
+
var _updates2;
|
|
2069
|
+
|
|
2070
|
+
var reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
2071
|
+
|
|
2072
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(this._map), _step4; !(_step4 = _iterator4()).done;) {
|
|
2073
|
+
var _step4$value = _step4.value,
|
|
2074
|
+
key = _step4$value[0],
|
|
2075
|
+
value = _step4$value[1];
|
|
2076
|
+
|
|
2077
|
+
if (value === child) {
|
|
2078
|
+
this._map.delete(key);
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
|
|
2082
|
+
child._detach();
|
|
2083
|
+
|
|
2084
|
+
var storageUpdate = {
|
|
2085
|
+
node: this,
|
|
2086
|
+
type: "LiveObject",
|
|
2087
|
+
updates: (_updates2 = {}, _updates2[child._parentKey] = {
|
|
2088
|
+
type: "delete"
|
|
2089
|
+
}, _updates2)
|
|
2090
|
+
};
|
|
2091
|
+
return {
|
|
2092
|
+
modified: storageUpdate,
|
|
2093
|
+
reverse: reverse
|
|
2094
|
+
};
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
return {
|
|
2098
|
+
modified: false
|
|
2099
|
+
};
|
|
2100
|
+
};
|
|
2101
|
+
|
|
2102
|
+
_proto._detachChildren = function _detachChildren() {
|
|
2103
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(this._map), _step5; !(_step5 = _iterator5()).done;) {
|
|
2104
|
+
var _step5$value = _step5.value,
|
|
2105
|
+
key = _step5$value[0],
|
|
2106
|
+
value = _step5$value[1];
|
|
2107
|
+
|
|
2108
|
+
this._map.delete(key);
|
|
2109
|
+
|
|
2110
|
+
value._detach();
|
|
2111
|
+
}
|
|
2112
|
+
};
|
|
2113
|
+
|
|
2114
|
+
_proto._detach = function _detach() {
|
|
2115
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
2116
|
+
|
|
2117
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(this._map.values()), _step6; !(_step6 = _iterator6()).done;) {
|
|
2118
|
+
var value = _step6.value;
|
|
2119
|
+
|
|
2120
|
+
if (isCrdt(value)) {
|
|
2121
|
+
value._detach();
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
};
|
|
2125
|
+
|
|
2126
|
+
_proto._apply = function _apply(op, isLocal) {
|
|
2127
|
+
if (op.type === exports.OpType.UpdateObject) {
|
|
2128
|
+
return this._applyUpdate(op, isLocal);
|
|
2129
|
+
} else if (op.type === exports.OpType.DeleteObjectKey) {
|
|
2130
|
+
return this._applyDeleteObjectKey(op);
|
|
2131
|
+
}
|
|
2132
|
+
|
|
2133
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
2134
|
+
};
|
|
2135
|
+
|
|
2136
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
2137
|
+
var _this$_parent;
|
|
2138
|
+
|
|
2139
|
+
var data = {};
|
|
2140
|
+
|
|
2141
|
+
for (var _iterator7 = _createForOfIteratorHelperLoose(this._map), _step7; !(_step7 = _iterator7()).done;) {
|
|
2142
|
+
var _step7$value = _step7.value,
|
|
2143
|
+
key = _step7$value[0],
|
|
2144
|
+
value = _step7$value[1];
|
|
2145
|
+
|
|
2146
|
+
if (value instanceof AbstractCrdt === false) {
|
|
2147
|
+
data[key] = value;
|
|
2148
|
+
}
|
|
2149
|
+
}
|
|
2150
|
+
|
|
2151
|
+
return {
|
|
2152
|
+
type: exports.CrdtType.Object,
|
|
2153
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
2154
|
+
parentKey: this._parentKey,
|
|
2155
|
+
data: data
|
|
2156
|
+
};
|
|
2157
|
+
};
|
|
2158
|
+
|
|
2159
|
+
_proto._applyUpdate = function _applyUpdate(op, isLocal) {
|
|
2160
|
+
var isModified = false;
|
|
2161
|
+
var reverse = [];
|
|
2162
|
+
var reverseUpdate = {
|
|
2163
|
+
type: exports.OpType.UpdateObject,
|
|
2164
|
+
id: this._id,
|
|
2165
|
+
data: {}
|
|
2166
|
+
};
|
|
2167
|
+
reverse.push(reverseUpdate);
|
|
2168
|
+
|
|
2169
|
+
for (var key in op.data) {
|
|
2170
|
+
var oldValue = this._map.get(key);
|
|
2171
|
+
|
|
2172
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2173
|
+
reverse.push.apply(reverse, oldValue._serialize(this._id, key));
|
|
2174
|
+
|
|
2175
|
+
oldValue._detach();
|
|
2176
|
+
} else if (oldValue !== undefined) {
|
|
2177
|
+
reverseUpdate.data[key] = oldValue;
|
|
2178
|
+
} else if (oldValue === undefined) {
|
|
2179
|
+
reverse.push({
|
|
2180
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2181
|
+
id: this._id,
|
|
2182
|
+
key: key
|
|
2183
|
+
});
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
var updateDelta = {};
|
|
2188
|
+
|
|
2189
|
+
for (var _key2 in op.data) {
|
|
2190
|
+
if (isLocal) {
|
|
2191
|
+
this._propToLastUpdate.set(_key2, op.opId);
|
|
2192
|
+
} else if (this._propToLastUpdate.get(_key2) == null) {
|
|
2193
|
+
isModified = true;
|
|
2194
|
+
} else if (this._propToLastUpdate.get(_key2) === op.opId) {
|
|
2195
|
+
this._propToLastUpdate.delete(_key2);
|
|
2196
|
+
|
|
2197
|
+
continue;
|
|
2198
|
+
} else {
|
|
2199
|
+
continue;
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
var _oldValue = this._map.get(_key2);
|
|
2203
|
+
|
|
2204
|
+
if (isCrdt(_oldValue)) {
|
|
2205
|
+
_oldValue._detach();
|
|
2206
|
+
}
|
|
2207
|
+
|
|
2208
|
+
isModified = true;
|
|
2209
|
+
updateDelta[_key2] = {
|
|
2210
|
+
type: "update"
|
|
2211
|
+
};
|
|
2212
|
+
|
|
2213
|
+
this._map.set(_key2, op.data[_key2]);
|
|
2214
|
+
}
|
|
2215
|
+
|
|
2216
|
+
if (Object.keys(reverseUpdate.data).length !== 0) {
|
|
2217
|
+
reverse.unshift(reverseUpdate);
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
return isModified ? {
|
|
2221
|
+
modified: {
|
|
2222
|
+
node: this,
|
|
2223
|
+
type: "LiveObject",
|
|
2224
|
+
updates: updateDelta
|
|
2225
|
+
},
|
|
2226
|
+
reverse: reverse
|
|
2227
|
+
} : {
|
|
2228
|
+
modified: false
|
|
2229
|
+
};
|
|
2230
|
+
};
|
|
2231
|
+
|
|
2232
|
+
_proto._applyDeleteObjectKey = function _applyDeleteObjectKey(op) {
|
|
2233
|
+
var _updates3;
|
|
2234
|
+
|
|
2235
|
+
var key = op.key;
|
|
2236
|
+
|
|
2237
|
+
if (this._map.has(key) === false) {
|
|
2238
|
+
return {
|
|
2239
|
+
modified: false
|
|
2240
|
+
};
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
if (this._propToLastUpdate.get(key) !== undefined) {
|
|
2244
|
+
return {
|
|
2245
|
+
modified: false
|
|
2246
|
+
};
|
|
2247
|
+
}
|
|
2248
|
+
|
|
2249
|
+
var oldValue = this._map.get(key);
|
|
2250
|
+
|
|
2251
|
+
var reverse = [];
|
|
2252
|
+
|
|
2253
|
+
if (isCrdt(oldValue)) {
|
|
2254
|
+
reverse = oldValue._serialize(this._id, op.key);
|
|
2255
|
+
|
|
2256
|
+
oldValue._detach();
|
|
2257
|
+
} else if (oldValue !== undefined) {
|
|
2258
|
+
var _data2;
|
|
2259
|
+
|
|
2260
|
+
reverse = [{
|
|
2261
|
+
type: exports.OpType.UpdateObject,
|
|
2262
|
+
id: this._id,
|
|
2263
|
+
data: (_data2 = {}, _data2[key] = oldValue, _data2)
|
|
2264
|
+
}];
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
this._map.delete(key);
|
|
2268
|
+
|
|
2269
|
+
return {
|
|
2270
|
+
modified: {
|
|
2271
|
+
node: this,
|
|
2272
|
+
type: "LiveObject",
|
|
2273
|
+
updates: (_updates3 = {}, _updates3[op.key] = {
|
|
2274
|
+
type: "delete"
|
|
2275
|
+
}, _updates3)
|
|
2276
|
+
},
|
|
2277
|
+
reverse: reverse
|
|
2278
|
+
};
|
|
2279
|
+
};
|
|
2280
|
+
|
|
2281
|
+
_proto.toObject = function toObject() {
|
|
2282
|
+
return Object.fromEntries(this._map);
|
|
2283
|
+
};
|
|
2284
|
+
|
|
2285
|
+
_proto.set = function set(key, value) {
|
|
2286
|
+
var _this$update;
|
|
2287
|
+
|
|
2288
|
+
this.update((_this$update = {}, _this$update[key] = value, _this$update));
|
|
2289
|
+
};
|
|
2290
|
+
|
|
2291
|
+
_proto.get = function get(key) {
|
|
2292
|
+
return this._map.get(key);
|
|
2293
|
+
};
|
|
2294
|
+
|
|
2295
|
+
_proto.delete = function _delete(key) {
|
|
2296
|
+
var _updates4;
|
|
2297
|
+
|
|
2298
|
+
var keyAsString = key;
|
|
2299
|
+
|
|
2300
|
+
var oldValue = this._map.get(keyAsString);
|
|
2301
|
+
|
|
2302
|
+
if (oldValue === undefined) {
|
|
2303
|
+
return;
|
|
2304
|
+
}
|
|
2305
|
+
|
|
2306
|
+
if (this._doc == null || this._id == null) {
|
|
2307
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2308
|
+
oldValue._detach();
|
|
2309
|
+
}
|
|
2310
|
+
|
|
2311
|
+
this._map.delete(keyAsString);
|
|
2312
|
+
|
|
2313
|
+
return;
|
|
2314
|
+
}
|
|
2315
|
+
|
|
2316
|
+
var reverse;
|
|
2317
|
+
|
|
2318
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2319
|
+
oldValue._detach();
|
|
2320
|
+
|
|
2321
|
+
reverse = oldValue._serialize(this._id, keyAsString);
|
|
2322
|
+
} else {
|
|
2323
|
+
var _data3;
|
|
2324
|
+
|
|
2325
|
+
reverse = [{
|
|
2326
|
+
type: exports.OpType.UpdateObject,
|
|
2327
|
+
data: (_data3 = {}, _data3[keyAsString] = oldValue, _data3),
|
|
2328
|
+
id: this._id
|
|
2329
|
+
}];
|
|
2330
|
+
}
|
|
2331
|
+
|
|
2332
|
+
this._map.delete(keyAsString);
|
|
2333
|
+
|
|
2334
|
+
var storageUpdates = new Map();
|
|
2335
|
+
storageUpdates.set(this._id, {
|
|
2336
|
+
node: this,
|
|
2337
|
+
type: "LiveObject",
|
|
2338
|
+
updates: (_updates4 = {}, _updates4[key] = {
|
|
2339
|
+
type: "delete"
|
|
2340
|
+
}, _updates4)
|
|
2341
|
+
});
|
|
2342
|
+
|
|
2343
|
+
this._doc.dispatch([{
|
|
2344
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2345
|
+
key: keyAsString,
|
|
2346
|
+
id: this._id,
|
|
2347
|
+
opId: this._doc.generateOpId()
|
|
2348
|
+
}], reverse, storageUpdates);
|
|
2349
|
+
};
|
|
2350
|
+
|
|
2351
|
+
_proto.update = function update(overrides) {
|
|
2352
|
+
var _this2 = this;
|
|
2353
|
+
|
|
2354
|
+
if (this._doc == null || this._id == null) {
|
|
2355
|
+
for (var key in overrides) {
|
|
2356
|
+
var oldValue = this._map.get(key);
|
|
2357
|
+
|
|
2358
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2359
|
+
oldValue._detach();
|
|
2360
|
+
}
|
|
2361
|
+
|
|
2362
|
+
var newValue = overrides[key];
|
|
2363
|
+
|
|
2364
|
+
if (newValue instanceof AbstractCrdt) {
|
|
2365
|
+
newValue._setParentLink(this, key);
|
|
2366
|
+
}
|
|
2367
|
+
|
|
2368
|
+
this._map.set(key, newValue);
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
return;
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
var ops = [];
|
|
2375
|
+
var reverseOps = [];
|
|
2376
|
+
|
|
2377
|
+
var opId = this._doc.generateOpId();
|
|
2378
|
+
|
|
2379
|
+
var updatedProps = {};
|
|
2380
|
+
var reverseUpdateOp = {
|
|
2381
|
+
id: this._id,
|
|
2382
|
+
type: exports.OpType.UpdateObject,
|
|
2383
|
+
data: {}
|
|
2384
|
+
};
|
|
2385
|
+
var updateDelta = {};
|
|
2386
|
+
|
|
2387
|
+
for (var _key3 in overrides) {
|
|
2388
|
+
var _oldValue2 = this._map.get(_key3);
|
|
2389
|
+
|
|
2390
|
+
if (_oldValue2 instanceof AbstractCrdt) {
|
|
2391
|
+
reverseOps.push.apply(reverseOps, _oldValue2._serialize(this._id, _key3));
|
|
2392
|
+
|
|
2393
|
+
_oldValue2._detach();
|
|
2394
|
+
} else if (_oldValue2 === undefined) {
|
|
2395
|
+
reverseOps.push({
|
|
2396
|
+
type: exports.OpType.DeleteObjectKey,
|
|
2397
|
+
id: this._id,
|
|
2398
|
+
key: _key3
|
|
2399
|
+
});
|
|
2400
|
+
} else {
|
|
2401
|
+
reverseUpdateOp.data[_key3] = _oldValue2;
|
|
2402
|
+
}
|
|
2403
|
+
|
|
2404
|
+
var _newValue = overrides[_key3];
|
|
2405
|
+
|
|
2406
|
+
if (_newValue instanceof AbstractCrdt) {
|
|
2407
|
+
_newValue._setParentLink(this, _key3);
|
|
2408
|
+
|
|
2409
|
+
_newValue._attach(this._doc.generateId(), this._doc);
|
|
2410
|
+
|
|
2411
|
+
var newAttachChildOps = _newValue._serialize(this._id, _key3, this._doc);
|
|
2412
|
+
|
|
2413
|
+
var createCrdtOp = newAttachChildOps.find(function (op) {
|
|
2414
|
+
return op.parentId === _this2._id;
|
|
2415
|
+
});
|
|
2416
|
+
|
|
2417
|
+
if (createCrdtOp) {
|
|
2418
|
+
this._propToLastUpdate.set(_key3, createCrdtOp.opId);
|
|
2419
|
+
}
|
|
2420
|
+
|
|
2421
|
+
ops.push.apply(ops, newAttachChildOps);
|
|
2422
|
+
} else {
|
|
2423
|
+
updatedProps[_key3] = _newValue;
|
|
2424
|
+
|
|
2425
|
+
this._propToLastUpdate.set(_key3, opId);
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
this._map.set(_key3, _newValue);
|
|
2429
|
+
|
|
2430
|
+
updateDelta[_key3] = {
|
|
2431
|
+
type: "update"
|
|
2432
|
+
};
|
|
2433
|
+
}
|
|
2434
|
+
|
|
2435
|
+
if (Object.keys(reverseUpdateOp.data).length !== 0) {
|
|
2436
|
+
reverseOps.unshift(reverseUpdateOp);
|
|
2437
|
+
}
|
|
2438
|
+
|
|
2439
|
+
if (Object.keys(updatedProps).length !== 0) {
|
|
2440
|
+
ops.unshift({
|
|
2441
|
+
opId: opId,
|
|
2442
|
+
id: this._id,
|
|
2443
|
+
type: exports.OpType.UpdateObject,
|
|
2444
|
+
data: updatedProps
|
|
2445
|
+
});
|
|
2446
|
+
}
|
|
2447
|
+
|
|
2448
|
+
var storageUpdates = new Map();
|
|
2449
|
+
storageUpdates.set(this._id, {
|
|
2450
|
+
node: this,
|
|
2451
|
+
type: "LiveObject",
|
|
2452
|
+
updates: updateDelta
|
|
2453
|
+
});
|
|
2454
|
+
|
|
2455
|
+
this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
2456
|
+
};
|
|
2457
|
+
|
|
2458
|
+
return LiveObject;
|
|
2459
|
+
}(AbstractCrdt);
|
|
2460
|
+
|
|
2461
|
+
exports.AbstractCrdt = AbstractCrdt;
|
|
2462
|
+
exports.LiveList = LiveList;
|
|
2463
|
+
exports.LiveMap = LiveMap;
|
|
2464
|
+
exports.LiveObject = LiveObject;
|
|
2465
|
+
exports.LiveRegister = LiveRegister;
|
|
2466
|
+
exports._createForOfIteratorHelperLoose = _createForOfIteratorHelperLoose;
|
|
2467
|
+
exports._extends = _extends;
|
|
2468
|
+
exports._inheritsLoose = _inheritsLoose;
|
|
2469
|
+
exports._objectWithoutPropertiesLoose = _objectWithoutPropertiesLoose;
|
|
2470
|
+
exports._wrapNativeSuper = _wrapNativeSuper;
|
|
2471
|
+
exports.compact = compact;
|
|
2472
|
+
exports.compare = compare;
|
|
2473
|
+
exports.deprecate = deprecate;
|
|
2474
|
+
exports.deprecateIf = deprecateIf;
|
|
2475
|
+
exports.findNonSerializableValue = findNonSerializableValue;
|
|
2476
|
+
exports.getTreesDiffOperations = getTreesDiffOperations;
|
|
2477
|
+
exports.isJsonArray = isJsonArray;
|
|
2478
|
+
exports.isJsonObject = isJsonObject;
|
|
2479
|
+
exports.isSameNodeOrChildOf = isSameNodeOrChildOf;
|
|
2480
|
+
exports.isTokenValid = isTokenValid;
|
|
2481
|
+
exports.makePosition = makePosition;
|
|
2482
|
+
exports.max = max;
|
|
2483
|
+
exports.mergeStorageUpdates = mergeStorageUpdates;
|
|
2484
|
+
exports.min = min;
|
|
2485
|
+
exports.parseJson = parseJson;
|
|
2486
|
+
exports.pos = pos;
|
|
2487
|
+
exports.posCodes = posCodes;
|
|
2488
|
+
exports.remove = remove;
|