@liveblocks/client 0.16.4 → 0.16.7
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.js +815 -1540
- package/index.mjs +778 -1295
- package/internal.d.ts +37 -5
- package/internal.js +152 -337
- package/internal.mjs +127 -299
- package/package.json +8 -7
- package/shared.d.ts +15 -3
- package/shared.js +1276 -2380
- package/shared.mjs +1183 -1887
package/shared.js
CHANGED
|
@@ -1,2482 +1,1378 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
3
|
function _defineProperties(target, props) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Object.defineProperty(target, descriptor.key, descriptor);
|
|
10
|
-
}
|
|
4
|
+
for (var i = 0; i < props.length; i++) {
|
|
5
|
+
var descriptor = props[i];
|
|
6
|
+
descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0,
|
|
7
|
+
"value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor);
|
|
8
|
+
}
|
|
11
9
|
}
|
|
12
10
|
|
|
13
11
|
function _createClass(Constructor, protoProps, staticProps) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps),
|
|
13
|
+
Object.defineProperty(Constructor, "prototype", {
|
|
14
|
+
writable: !1
|
|
15
|
+
}), Constructor;
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
function _extends() {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return target;
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
return _extends.apply(this, arguments);
|
|
19
|
+
return _extends = Object.assign || function(target) {
|
|
20
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
21
|
+
var source = arguments[i];
|
|
22
|
+
for (var key in source) Object.prototype.hasOwnProperty.call(source, key) && (target[key] = source[key]);
|
|
23
|
+
}
|
|
24
|
+
return target;
|
|
25
|
+
}, _extends.apply(this, arguments);
|
|
35
26
|
}
|
|
36
27
|
|
|
37
28
|
function _inheritsLoose(subClass, superClass) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
_setPrototypeOf(subClass, superClass);
|
|
29
|
+
subClass.prototype = Object.create(superClass.prototype), subClass.prototype.constructor = subClass,
|
|
30
|
+
_setPrototypeOf(subClass, superClass);
|
|
42
31
|
}
|
|
43
32
|
|
|
44
33
|
function _getPrototypeOf(o) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return _getPrototypeOf(o);
|
|
34
|
+
return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function(o) {
|
|
35
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
36
|
+
}, _getPrototypeOf(o);
|
|
49
37
|
}
|
|
50
38
|
|
|
51
39
|
function _setPrototypeOf(o, p) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
return _setPrototypeOf(o, p);
|
|
40
|
+
return _setPrototypeOf = Object.setPrototypeOf || function(o, p) {
|
|
41
|
+
return o.__proto__ = p, o;
|
|
42
|
+
}, _setPrototypeOf(o, p);
|
|
58
43
|
}
|
|
59
44
|
|
|
60
45
|
function _isNativeReflectConstruct() {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
46
|
+
if ("undefined" == typeof Reflect || !Reflect.construct) return !1;
|
|
47
|
+
if (Reflect.construct.sham) return !1;
|
|
48
|
+
if ("function" == typeof Proxy) return !0;
|
|
49
|
+
try {
|
|
50
|
+
return Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], (function() {}))),
|
|
51
|
+
!0;
|
|
52
|
+
} catch (e) {
|
|
53
|
+
return !1;
|
|
54
|
+
}
|
|
71
55
|
}
|
|
72
56
|
|
|
73
57
|
function _construct(Parent, args, Class) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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;
|
|
58
|
+
return _construct = _isNativeReflectConstruct() ? Reflect.construct : function(Parent, args, Class) {
|
|
59
|
+
var a = [ null ];
|
|
60
|
+
a.push.apply(a, args);
|
|
61
|
+
var instance = new (Function.bind.apply(Parent, a));
|
|
62
|
+
return Class && _setPrototypeOf(instance, Class.prototype), instance;
|
|
63
|
+
}, _construct.apply(null, arguments);
|
|
92
64
|
}
|
|
93
65
|
|
|
94
66
|
function _wrapNativeSuper(Class) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
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];
|
|
67
|
+
var _cache = "function" == typeof Map ? new Map : void 0;
|
|
68
|
+
return _wrapNativeSuper = function(Class) {
|
|
69
|
+
if (null === Class || (fn = Class, -1 === Function.toString.call(fn).indexOf("[native code]"))) return Class;
|
|
70
|
+
var fn;
|
|
71
|
+
if ("function" != typeof Class) throw new TypeError("Super expression must either be null or a function");
|
|
72
|
+
if (void 0 !== _cache) {
|
|
73
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
74
|
+
_cache.set(Class, Wrapper);
|
|
138
75
|
}
|
|
139
|
-
|
|
140
|
-
|
|
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");
|
|
76
|
+
function Wrapper() {
|
|
77
|
+
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
|
|
146
78
|
}
|
|
147
|
-
|
|
148
|
-
|
|
79
|
+
return Wrapper.prototype = Object.create(Class.prototype, {
|
|
80
|
+
constructor: {
|
|
81
|
+
value: Wrapper,
|
|
82
|
+
enumerable: !1,
|
|
83
|
+
writable: !0,
|
|
84
|
+
configurable: !0
|
|
85
|
+
}
|
|
86
|
+
}), _setPrototypeOf(Wrapper, Class);
|
|
87
|
+
}, _wrapNativeSuper(Class);
|
|
149
88
|
}
|
|
150
89
|
|
|
151
|
-
function
|
|
152
|
-
|
|
153
|
-
|
|
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);
|
|
90
|
+
function _assertThisInitialized(self) {
|
|
91
|
+
if (void 0 === self) throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
92
|
+
return self;
|
|
158
93
|
}
|
|
159
94
|
|
|
160
95
|
function _arrayLikeToArray(arr, len) {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return arr2;
|
|
96
|
+
(null == len || len > arr.length) && (len = arr.length);
|
|
97
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
98
|
+
return arr2;
|
|
166
99
|
}
|
|
167
100
|
|
|
168
101
|
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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.");
|
|
102
|
+
var it = "undefined" != typeof Symbol && o[Symbol.iterator] || o["@@iterator"];
|
|
103
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
104
|
+
if (Array.isArray(o) || (it = function(o, minLen) {
|
|
105
|
+
if (o) {
|
|
106
|
+
if ("string" == typeof o) return _arrayLikeToArray(o, minLen);
|
|
107
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
108
|
+
return "Object" === n && o.constructor && (n = o.constructor.name), "Map" === n || "Set" === n ? Array.from(o) : "Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n) ? _arrayLikeToArray(o, minLen) : void 0;
|
|
187
109
|
}
|
|
188
|
-
|
|
189
|
-
it =
|
|
190
|
-
|
|
110
|
+
}(o)) || allowArrayLike && o && "number" == typeof o.length) {
|
|
111
|
+
it && (o = it);
|
|
112
|
+
var i = 0;
|
|
113
|
+
return function() {
|
|
114
|
+
return i >= o.length ? {
|
|
115
|
+
done: !0
|
|
116
|
+
} : {
|
|
117
|
+
done: !1,
|
|
118
|
+
value: o[i++]
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
191
123
|
}
|
|
192
124
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
(
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
exports.
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
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]);
|
|
125
|
+
var ServerMessageType, ClientMessageType, CrdtType, OpType, WebsocketCloseCodes;
|
|
126
|
+
|
|
127
|
+
exports.ServerMessageType = void 0, (ServerMessageType = exports.ServerMessageType || (exports.ServerMessageType = {}))[ServerMessageType.UpdatePresence = 100] = "UpdatePresence",
|
|
128
|
+
ServerMessageType[ServerMessageType.UserJoined = 101] = "UserJoined", ServerMessageType[ServerMessageType.UserLeft = 102] = "UserLeft",
|
|
129
|
+
ServerMessageType[ServerMessageType.Event = 103] = "Event", ServerMessageType[ServerMessageType.RoomState = 104] = "RoomState",
|
|
130
|
+
ServerMessageType[ServerMessageType.InitialStorageState = 200] = "InitialStorageState",
|
|
131
|
+
ServerMessageType[ServerMessageType.UpdateStorage = 201] = "UpdateStorage", exports.ClientMessageType = void 0,
|
|
132
|
+
(ClientMessageType = exports.ClientMessageType || (exports.ClientMessageType = {}))[ClientMessageType.UpdatePresence = 100] = "UpdatePresence",
|
|
133
|
+
ClientMessageType[ClientMessageType.ClientEvent = 103] = "ClientEvent", ClientMessageType[ClientMessageType.FetchStorage = 200] = "FetchStorage",
|
|
134
|
+
ClientMessageType[ClientMessageType.UpdateStorage = 201] = "UpdateStorage", exports.CrdtType = void 0,
|
|
135
|
+
(CrdtType = exports.CrdtType || (exports.CrdtType = {}))[CrdtType.Object = 0] = "Object",
|
|
136
|
+
CrdtType[CrdtType.List = 1] = "List", CrdtType[CrdtType.Map = 2] = "Map", CrdtType[CrdtType.Register = 3] = "Register",
|
|
137
|
+
exports.OpType = void 0, (OpType = exports.OpType || (exports.OpType = {}))[OpType.Init = 0] = "Init",
|
|
138
|
+
OpType[OpType.SetParentKey = 1] = "SetParentKey", OpType[OpType.CreateList = 2] = "CreateList",
|
|
139
|
+
OpType[OpType.UpdateObject = 3] = "UpdateObject", OpType[OpType.CreateObject = 4] = "CreateObject",
|
|
140
|
+
OpType[OpType.DeleteCrdt = 5] = "DeleteCrdt", OpType[OpType.DeleteObjectKey = 6] = "DeleteObjectKey",
|
|
141
|
+
OpType[OpType.CreateMap = 7] = "CreateMap", OpType[OpType.CreateRegister = 8] = "CreateRegister",
|
|
142
|
+
exports.WebsocketCloseCodes = void 0, (WebsocketCloseCodes = exports.WebsocketCloseCodes || (exports.WebsocketCloseCodes = {}))[WebsocketCloseCodes.CLOSE_ABNORMAL = 1006] = "CLOSE_ABNORMAL",
|
|
143
|
+
WebsocketCloseCodes[WebsocketCloseCodes.INVALID_MESSAGE_FORMAT = 4e3] = "INVALID_MESSAGE_FORMAT",
|
|
144
|
+
WebsocketCloseCodes[WebsocketCloseCodes.NOT_ALLOWED = 4001] = "NOT_ALLOWED", WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS",
|
|
145
|
+
WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS",
|
|
146
|
+
WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP",
|
|
147
|
+
WebsocketCloseCodes[WebsocketCloseCodes.MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM",
|
|
148
|
+
WebsocketCloseCodes[WebsocketCloseCodes.CLOSE_WITHOUT_RETRY = 4999] = "CLOSE_WITHOUT_RETRY";
|
|
149
|
+
|
|
150
|
+
var AbstractCrdt = function() {
|
|
151
|
+
function AbstractCrdt() {
|
|
152
|
+
this.__parent = void 0, this.__doc = void 0, this.__id = void 0, this.__parentKey = void 0;
|
|
153
|
+
}
|
|
154
|
+
var _proto = AbstractCrdt.prototype;
|
|
155
|
+
return _proto._apply = function(op, _isLocal) {
|
|
156
|
+
return op.type === exports.OpType.DeleteCrdt && null != this._parent && null != this._parentKey ? this._parent._detachChild(this) : {
|
|
157
|
+
modified: !1
|
|
158
|
+
};
|
|
159
|
+
}, _proto._setParentLink = function(parent, key) {
|
|
160
|
+
if (null != this.__parent && this.__parent !== parent) throw new Error("Cannot attach parent if it already exist");
|
|
161
|
+
this.__parentKey = key, this.__parent = parent;
|
|
162
|
+
}, _proto._attach = function(id, doc) {
|
|
163
|
+
if (this.__id || this.__doc) throw new Error("Cannot attach if CRDT is already attached");
|
|
164
|
+
doc.addItem(id, this), this.__id = id, this.__doc = doc;
|
|
165
|
+
}, _proto._detach = function() {
|
|
166
|
+
this.__doc && this.__id && this.__doc.deleteItem(this.__id), this.__parent = void 0,
|
|
167
|
+
this.__doc = void 0;
|
|
168
|
+
}, _createClass(AbstractCrdt, [ {
|
|
169
|
+
key: "_doc",
|
|
170
|
+
get: function() {
|
|
171
|
+
return this.__doc;
|
|
337
172
|
}
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
173
|
+
}, {
|
|
174
|
+
key: "roomId",
|
|
175
|
+
get: function() {
|
|
176
|
+
return this.__doc ? this.__doc.roomId : null;
|
|
341
177
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
178
|
+
}, {
|
|
179
|
+
key: "_id",
|
|
180
|
+
get: function() {
|
|
181
|
+
return this.__id;
|
|
345
182
|
}
|
|
183
|
+
}, {
|
|
184
|
+
key: "_parent",
|
|
185
|
+
get: function() {
|
|
186
|
+
return this.__parent;
|
|
187
|
+
}
|
|
188
|
+
}, {
|
|
189
|
+
key: "_parentKey",
|
|
190
|
+
get: function() {
|
|
191
|
+
return this.__parentKey;
|
|
192
|
+
}
|
|
193
|
+
} ]), AbstractCrdt;
|
|
194
|
+
}();
|
|
346
195
|
|
|
347
|
-
|
|
196
|
+
function parseJson(rawMessage) {
|
|
197
|
+
try {
|
|
198
|
+
return JSON.parse(rawMessage);
|
|
199
|
+
} catch (e) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
348
202
|
}
|
|
349
203
|
|
|
350
|
-
function
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
for (var i = 0; i < afterCodes.length; i++) {
|
|
355
|
-
var code = afterCodes[i];
|
|
204
|
+
function isJsonArray(data) {
|
|
205
|
+
return Array.isArray(data);
|
|
206
|
+
}
|
|
356
207
|
|
|
357
|
-
|
|
358
|
-
|
|
208
|
+
function isJsonObject(data) {
|
|
209
|
+
return null !== data && "object" == typeof data && !isJsonArray(data);
|
|
210
|
+
}
|
|
359
211
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
212
|
+
var _Symbol$iterator$1, _Symbol$iterator2, LiveRegister = function(_AbstractCrdt) {
|
|
213
|
+
function LiveRegister(data) {
|
|
214
|
+
var _this;
|
|
215
|
+
return (_this = _AbstractCrdt.call(this) || this)._data = void 0, _this._data = data,
|
|
216
|
+
_this;
|
|
217
|
+
}
|
|
218
|
+
_inheritsLoose(LiveRegister, _AbstractCrdt), LiveRegister._deserialize = function(_ref, _parentToChildren, doc) {
|
|
219
|
+
var id = _ref[0], item = _ref[1];
|
|
220
|
+
if (item.type !== exports.CrdtType.Register) throw new Error('Tried to deserialize a map but item type is "' + item.type + '"');
|
|
221
|
+
var register = new LiveRegister(item.data);
|
|
222
|
+
return register._attach(id, doc), register;
|
|
223
|
+
};
|
|
224
|
+
var _proto = LiveRegister.prototype;
|
|
225
|
+
return _proto._serialize = function(parentId, parentKey, doc, intent) {
|
|
226
|
+
if (null == this._id || null == parentId || null == parentKey) throw new Error("Cannot serialize register if parentId or parentKey is undefined");
|
|
227
|
+
return [ {
|
|
228
|
+
type: exports.OpType.CreateRegister,
|
|
229
|
+
opId: null == doc ? void 0 : doc.generateOpId(),
|
|
230
|
+
id: this._id,
|
|
231
|
+
intent: intent,
|
|
232
|
+
parentId: parentId,
|
|
233
|
+
parentKey: parentKey,
|
|
234
|
+
data: this.data
|
|
235
|
+
} ];
|
|
236
|
+
}, _proto._toSerializedCrdt = function() {
|
|
237
|
+
var _this$_parent;
|
|
238
|
+
return {
|
|
239
|
+
type: exports.CrdtType.Register,
|
|
240
|
+
parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
|
|
241
|
+
parentKey: this._parentKey,
|
|
242
|
+
data: this.data
|
|
243
|
+
};
|
|
244
|
+
}, _proto._attachChild = function(_op, _isLocal) {
|
|
245
|
+
throw new Error("Method not implemented.");
|
|
246
|
+
}, _proto._detachChild = function(_crdt) {
|
|
247
|
+
throw new Error("Method not implemented.");
|
|
248
|
+
}, _proto._apply = function(op, isLocal) {
|
|
249
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
250
|
+
}, _createClass(LiveRegister, [ {
|
|
251
|
+
key: "data",
|
|
252
|
+
get: function() {
|
|
253
|
+
return this._data;
|
|
368
254
|
}
|
|
255
|
+
} ]), LiveRegister;
|
|
256
|
+
}(AbstractCrdt);
|
|
369
257
|
|
|
258
|
+
function makePosition(before, after) {
|
|
259
|
+
return null == before && null == after ? pos([ 33 ]) : null != before && null == after ? function(before) {
|
|
260
|
+
for (var result = [], beforeCodes = posCodes(before), i = 0; i < beforeCodes.length; i++) {
|
|
261
|
+
var code = beforeCodes[i];
|
|
262
|
+
if (126 !== code) {
|
|
263
|
+
result.push(code + 1);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
if (result.push(code), beforeCodes.length - 1 === i) {
|
|
267
|
+
result.push(33);
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
370
271
|
return pos(result);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
-
}
|
|
272
|
+
}(before) : null == before && null != after ? function(after) {
|
|
273
|
+
for (var result = [], afterCodes = posCodes(after), i = 0; i < afterCodes.length; i++) {
|
|
274
|
+
var code = afterCodes[i];
|
|
275
|
+
if (!(code <= 33)) {
|
|
276
|
+
result.push(code - 1);
|
|
277
|
+
break;
|
|
278
|
+
}
|
|
279
|
+
if (result.push(32), afterCodes.length - 1 === i) {
|
|
280
|
+
result.push(126);
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
391
283
|
}
|
|
392
|
-
|
|
393
284
|
return pos(result);
|
|
285
|
+
}(after) : pos(makePositionFromCodes(posCodes(before), posCodes(after)));
|
|
394
286
|
}
|
|
395
287
|
|
|
396
288
|
function makePositionFromCodes(before, after) {
|
|
397
|
-
|
|
398
|
-
var
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
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);
|
|
289
|
+
for (var index = 0, result = []; ;) {
|
|
290
|
+
var beforeDigit = before[index] || 32, afterDigit = after[index] || 126;
|
|
291
|
+
if (beforeDigit > afterDigit) throw new Error("Impossible to generate position between " + before + " and " + after);
|
|
292
|
+
if (beforeDigit !== afterDigit) {
|
|
293
|
+
if (afterDigit - beforeDigit == 1) {
|
|
294
|
+
result.push(beforeDigit), result.push.apply(result, makePositionFromCodes(before.slice(index + 1), []));
|
|
422
295
|
break;
|
|
296
|
+
}
|
|
297
|
+
var mid = afterDigit + beforeDigit >> 1;
|
|
298
|
+
result.push(mid);
|
|
299
|
+
break;
|
|
423
300
|
}
|
|
424
|
-
|
|
425
|
-
|
|
301
|
+
result.push(beforeDigit), index++;
|
|
302
|
+
}
|
|
303
|
+
return result;
|
|
426
304
|
}
|
|
427
305
|
|
|
428
306
|
function posCodes(str) {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
for (var i = 0; i < str.length; i++) {
|
|
432
|
-
codes.push(str.charCodeAt(i));
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return codes;
|
|
307
|
+
for (var codes = [], i = 0; i < str.length; i++) codes.push(str.charCodeAt(i));
|
|
308
|
+
return codes;
|
|
436
309
|
}
|
|
310
|
+
|
|
437
311
|
function pos(codes) {
|
|
438
|
-
|
|
312
|
+
return String.fromCharCode.apply(String, codes);
|
|
439
313
|
}
|
|
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
314
|
|
|
456
|
-
|
|
315
|
+
function comparePosition(posA, posB) {
|
|
316
|
+
for (var aCodes = posCodes(posA), bCodes = posCodes(posB), maxLength = Math.max(aCodes.length, bCodes.length), i = 0; i < maxLength; i++) {
|
|
317
|
+
var a = null == aCodes[i] ? 32 : aCodes[i], b = null == bCodes[i] ? 32 : bCodes[i];
|
|
318
|
+
if (a !== b) return a - b;
|
|
319
|
+
}
|
|
320
|
+
throw new Error('Impossible to compare similar position "' + posA + '" and "' + posB + '"');
|
|
457
321
|
}
|
|
458
322
|
|
|
459
|
-
|
|
460
|
-
_inheritsLoose(LiveRegister, _AbstractCrdt);
|
|
461
|
-
|
|
462
|
-
function LiveRegister(data) {
|
|
463
|
-
var _this;
|
|
323
|
+
_Symbol$iterator$1 = Symbol.iterator;
|
|
464
324
|
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
325
|
+
var LiveList = function(_AbstractCrdt) {
|
|
326
|
+
function LiveList(items) {
|
|
327
|
+
var _this;
|
|
328
|
+
void 0 === items && (items = []), (_this = _AbstractCrdt.call(this) || this)._items = void 0,
|
|
329
|
+
_this._items = [];
|
|
330
|
+
for (var position = void 0, i = 0; i < items.length; i++) {
|
|
331
|
+
var newPosition = makePosition(position), _item = selfOrRegister(items[i]);
|
|
332
|
+
_this._items.push([ _item, newPosition ]), position = newPosition;
|
|
468
333
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
334
|
+
return _this;
|
|
335
|
+
}
|
|
336
|
+
_inheritsLoose(LiveList, _AbstractCrdt), LiveList._deserialize = function(_ref, parentToChildren, doc) {
|
|
337
|
+
var id = _ref[0], list = new LiveList([]);
|
|
338
|
+
list._attach(id, doc);
|
|
339
|
+
var children = parentToChildren.get(id);
|
|
340
|
+
if (null == children) return list;
|
|
341
|
+
for (var _step, _iterator = _createForOfIteratorHelperLoose(children); !(_step = _iterator()).done; ) {
|
|
342
|
+
var entry = _step.value, child = deserialize(entry, parentToChildren, doc);
|
|
343
|
+
child._setParentLink(list, entry[1].parentKey), list._items.push([ child, entry[1].parentKey ]),
|
|
344
|
+
list._items.sort((function(itemA, itemB) {
|
|
345
|
+
return comparePosition(itemA[1], itemB[1]);
|
|
346
|
+
}));
|
|
347
|
+
}
|
|
348
|
+
return list;
|
|
349
|
+
};
|
|
350
|
+
var _proto = LiveList.prototype;
|
|
351
|
+
return _proto._serialize = function(parentId, parentKey, doc, intent) {
|
|
352
|
+
if (null == this._id) throw new Error("Cannot serialize item is not attached");
|
|
353
|
+
if (null == parentId || null == parentKey) throw new Error("Cannot serialize list if parentId or parentKey is undefined");
|
|
354
|
+
var ops = [], op = {
|
|
355
|
+
id: this._id,
|
|
356
|
+
opId: null == doc ? void 0 : doc.generateOpId(),
|
|
357
|
+
intent: intent,
|
|
358
|
+
type: exports.OpType.CreateList,
|
|
359
|
+
parentId: parentId,
|
|
360
|
+
parentKey: parentKey
|
|
361
|
+
};
|
|
362
|
+
ops.push(op);
|
|
363
|
+
for (var _step2, _iterator2 = _createForOfIteratorHelperLoose(this._items); !(_step2 = _iterator2()).done; ) {
|
|
364
|
+
var _step2$value = _step2.value, _value = _step2$value[0], key = _step2$value[1];
|
|
365
|
+
ops.push.apply(ops, _value._serialize(this._id, key, doc));
|
|
366
|
+
}
|
|
367
|
+
return ops;
|
|
368
|
+
}, _proto._indexOfPosition = function(position) {
|
|
369
|
+
return this._items.findIndex((function(item) {
|
|
370
|
+
return item[1] === position;
|
|
371
|
+
}));
|
|
372
|
+
}, _proto._attach = function(id, doc) {
|
|
373
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
374
|
+
for (var _step3, _iterator3 = _createForOfIteratorHelperLoose(this._items); !(_step3 = _iterator3()).done; ) {
|
|
375
|
+
_step3.value[0]._attach(doc.generateId(), doc);
|
|
376
|
+
}
|
|
377
|
+
}, _proto._detach = function() {
|
|
378
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
379
|
+
for (var _step4, _iterator4 = _createForOfIteratorHelperLoose(this._items); !(_step4 = _iterator4()).done; ) {
|
|
380
|
+
_step4.value[0]._detach();
|
|
381
|
+
}
|
|
382
|
+
}, _proto._attachChild = function(op, isLocal) {
|
|
383
|
+
if (null == this._doc) throw new Error("Can't attach child if doc is not present");
|
|
384
|
+
var id = op.id, parentKey = op.parentKey, intent = op.intent, key = parentKey, child = creationOpToLiveStructure(op);
|
|
385
|
+
if (void 0 !== this._doc.getItem(id)) return {
|
|
386
|
+
modified: !1
|
|
387
|
+
};
|
|
388
|
+
child._attach(id, this._doc), child._setParentLink(this, key);
|
|
389
|
+
var index = this._items.findIndex((function(entry) {
|
|
390
|
+
return entry[1] === key;
|
|
391
|
+
})), newKey = key;
|
|
392
|
+
if (-1 !== index) {
|
|
393
|
+
if ("set" === intent) {
|
|
394
|
+
var existingItem = this._items[index][0];
|
|
395
|
+
existingItem._detach();
|
|
396
|
+
var storageUpdate = {
|
|
397
|
+
node: this,
|
|
398
|
+
type: "LiveList",
|
|
399
|
+
updates: [ {
|
|
400
|
+
index: index,
|
|
401
|
+
type: "set",
|
|
402
|
+
item: child instanceof LiveRegister ? child.data : child
|
|
403
|
+
} ]
|
|
511
404
|
};
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
405
|
+
return this._items[index][0] = child, {
|
|
406
|
+
modified: storageUpdate,
|
|
407
|
+
reverse: existingItem._serialize(this._id, key, this._doc, "set")
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
if (isLocal) {
|
|
411
|
+
var before = this._items[index] ? this._items[index][1] : void 0, after = this._items[index + 1] ? this._items[index + 1][1] : void 0;
|
|
412
|
+
newKey = makePosition(before, after), child._setParentLink(this, newKey);
|
|
413
|
+
} else {
|
|
414
|
+
var _this$_items;
|
|
415
|
+
this._items[index][1] = makePosition(key, null == (_this$_items = this._items[index + 1]) ? void 0 : _this$_items[1]);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
this._items.push([ child, newKey ]), this._items.sort((function(itemA, itemB) {
|
|
419
|
+
return comparePosition(itemA[1], itemB[1]);
|
|
420
|
+
}));
|
|
421
|
+
var newIndex = this._items.findIndex((function(entry) {
|
|
422
|
+
return entry[1] === newKey;
|
|
423
|
+
}));
|
|
424
|
+
return {
|
|
425
|
+
reverse: [ {
|
|
426
|
+
type: exports.OpType.DeleteCrdt,
|
|
427
|
+
id: id
|
|
428
|
+
} ],
|
|
429
|
+
modified: {
|
|
430
|
+
node: this,
|
|
431
|
+
type: "LiveList",
|
|
432
|
+
updates: [ {
|
|
433
|
+
index: newIndex,
|
|
434
|
+
type: "insert",
|
|
435
|
+
item: child instanceof LiveRegister ? child.data : child
|
|
436
|
+
} ]
|
|
437
|
+
}
|
|
438
|
+
};
|
|
439
|
+
}, _proto._detachChild = function(child) {
|
|
440
|
+
if (child) {
|
|
441
|
+
var reverse = child._serialize(this._id, child._parentKey, this._doc), indexToDelete = this._items.findIndex((function(item) {
|
|
442
|
+
return item[0] === child;
|
|
443
|
+
}));
|
|
444
|
+
return this._items.splice(indexToDelete, 1), child._detach(), {
|
|
445
|
+
modified: {
|
|
446
|
+
node: this,
|
|
447
|
+
type: "LiveList",
|
|
448
|
+
updates: [ {
|
|
449
|
+
index: indexToDelete,
|
|
450
|
+
type: "delete"
|
|
451
|
+
} ]
|
|
452
|
+
},
|
|
453
|
+
reverse: reverse
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
return {
|
|
457
|
+
modified: !1
|
|
458
|
+
};
|
|
459
|
+
}, _proto._setChildKey = function(key, child, previousKey) {
|
|
460
|
+
child._setParentLink(this, key);
|
|
461
|
+
var _this$_items2, previousIndex = this._items.findIndex((function(entry) {
|
|
462
|
+
return entry[0]._id === child._id;
|
|
463
|
+
})), index = this._items.findIndex((function(entry) {
|
|
464
|
+
return entry[1] === key;
|
|
465
|
+
}));
|
|
466
|
+
-1 !== index && (this._items[index][1] = makePosition(key, null == (_this$_items2 = this._items[index + 1]) ? void 0 : _this$_items2[1]));
|
|
467
|
+
var item = this._items.find((function(item) {
|
|
468
|
+
return item[0] === child;
|
|
469
|
+
}));
|
|
470
|
+
item && (item[1] = key), this._items.sort((function(itemA, itemB) {
|
|
471
|
+
return comparePosition(itemA[1], itemB[1]);
|
|
472
|
+
}));
|
|
473
|
+
var newIndex = this._items.findIndex((function(entry) {
|
|
474
|
+
return entry[0]._id === child._id;
|
|
475
|
+
}));
|
|
476
|
+
return {
|
|
477
|
+
modified: {
|
|
478
|
+
node: this,
|
|
479
|
+
type: "LiveList",
|
|
480
|
+
updates: newIndex === previousIndex ? [] : [ {
|
|
481
|
+
index: newIndex,
|
|
482
|
+
item: child instanceof LiveRegister ? child.data : child,
|
|
483
|
+
previousIndex: previousIndex,
|
|
484
|
+
type: "move"
|
|
485
|
+
} ]
|
|
486
|
+
},
|
|
487
|
+
reverse: [ {
|
|
488
|
+
type: exports.OpType.SetParentKey,
|
|
489
|
+
id: null == item ? void 0 : item[0]._id,
|
|
490
|
+
parentKey: previousKey
|
|
491
|
+
} ]
|
|
492
|
+
};
|
|
493
|
+
}, _proto._apply = function(op, isLocal) {
|
|
494
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
495
|
+
}, _proto._toSerializedCrdt = function() {
|
|
496
|
+
var _this$_parent;
|
|
497
|
+
return {
|
|
498
|
+
type: exports.CrdtType.List,
|
|
499
|
+
parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
|
|
500
|
+
parentKey: this._parentKey
|
|
501
|
+
};
|
|
502
|
+
}, _proto.push = function(element) {
|
|
503
|
+
return this.insert(element, this.length);
|
|
504
|
+
}, _proto.insert = function(element, index) {
|
|
505
|
+
if (index < 0 || index > this._items.length) throw new Error('Cannot insert list item at index "' + index + '". index should be between 0 and ' + this._items.length);
|
|
506
|
+
var position = makePosition(this._items[index - 1] ? this._items[index - 1][1] : void 0, this._items[index] ? this._items[index][1] : void 0), value = selfOrRegister(element);
|
|
507
|
+
value._setParentLink(this, position), this._items.push([ value, position ]), this._items.sort((function(itemA, itemB) {
|
|
508
|
+
return comparePosition(itemA[1], itemB[1]);
|
|
509
|
+
}));
|
|
510
|
+
var newIndex = this._items.findIndex((function(entry) {
|
|
511
|
+
return entry[1] === position;
|
|
512
|
+
}));
|
|
513
|
+
if (this._doc && this._id) {
|
|
514
|
+
var _id = this._doc.generateId();
|
|
515
|
+
value._attach(_id, this._doc);
|
|
516
|
+
var storageUpdates = new Map;
|
|
517
|
+
storageUpdates.set(this._id, {
|
|
518
|
+
node: this,
|
|
519
|
+
type: "LiveList",
|
|
520
|
+
updates: [ {
|
|
521
|
+
index: newIndex,
|
|
522
|
+
item: value instanceof LiveRegister ? value.data : value,
|
|
523
|
+
type: "insert"
|
|
524
|
+
} ]
|
|
525
|
+
}), this._doc.dispatch(value._serialize(this._id, position, this._doc), [ {
|
|
526
|
+
type: exports.OpType.DeleteCrdt,
|
|
527
|
+
id: _id
|
|
528
|
+
} ], storageUpdates);
|
|
529
|
+
}
|
|
530
|
+
}, _proto.move = function(index, targetIndex) {
|
|
531
|
+
if (targetIndex < 0) throw new Error("targetIndex cannot be less than 0");
|
|
532
|
+
if (targetIndex >= this._items.length) throw new Error("targetIndex cannot be greater or equal than the list length");
|
|
533
|
+
if (index < 0) throw new Error("index cannot be less than 0");
|
|
534
|
+
if (index >= this._items.length) throw new Error("index cannot be greater or equal than the list length");
|
|
535
|
+
var beforePosition = null, afterPosition = null;
|
|
536
|
+
index < targetIndex ? (afterPosition = targetIndex === this._items.length - 1 ? void 0 : this._items[targetIndex + 1][1],
|
|
537
|
+
beforePosition = this._items[targetIndex][1]) : (afterPosition = this._items[targetIndex][1],
|
|
538
|
+
beforePosition = 0 === targetIndex ? void 0 : this._items[targetIndex - 1][1]);
|
|
539
|
+
var position = makePosition(beforePosition, afterPosition), item = this._items[index], previousPosition = item[1];
|
|
540
|
+
item[1] = position, item[0]._setParentLink(this, position), this._items.sort((function(itemA, itemB) {
|
|
541
|
+
return comparePosition(itemA[1], itemB[1]);
|
|
542
|
+
}));
|
|
543
|
+
var newIndex = this._items.findIndex((function(entry) {
|
|
544
|
+
return entry[1] === position;
|
|
545
|
+
}));
|
|
546
|
+
if (this._doc && this._id) {
|
|
547
|
+
var storageUpdates = new Map;
|
|
548
|
+
storageUpdates.set(this._id, {
|
|
549
|
+
node: this,
|
|
550
|
+
type: "LiveList",
|
|
551
|
+
updates: [ {
|
|
552
|
+
index: newIndex,
|
|
553
|
+
previousIndex: index,
|
|
554
|
+
item: item[0],
|
|
555
|
+
type: "move"
|
|
556
|
+
} ]
|
|
557
|
+
}), this._doc.dispatch([ {
|
|
558
|
+
type: exports.OpType.SetParentKey,
|
|
559
|
+
id: item[0]._id,
|
|
560
|
+
opId: this._doc.generateOpId(),
|
|
561
|
+
parentKey: position
|
|
562
|
+
} ], [ {
|
|
563
|
+
type: exports.OpType.SetParentKey,
|
|
564
|
+
id: item[0]._id,
|
|
565
|
+
parentKey: previousPosition
|
|
566
|
+
} ], storageUpdates);
|
|
567
|
+
}
|
|
568
|
+
}, _proto.delete = function(index) {
|
|
569
|
+
if (index < 0 || index >= this._items.length) throw new Error('Cannot delete list item at index "' + index + '". index should be between 0 and ' + (this._items.length - 1));
|
|
570
|
+
var item = this._items[index];
|
|
571
|
+
if (item[0]._detach(), this._items.splice(index, 1), this._doc) {
|
|
572
|
+
var childRecordId = item[0]._id;
|
|
573
|
+
if (childRecordId) {
|
|
574
|
+
var storageUpdates = new Map;
|
|
575
|
+
storageUpdates.set(this._id, {
|
|
576
|
+
node: this,
|
|
577
|
+
type: "LiveList",
|
|
578
|
+
updates: [ {
|
|
579
|
+
index: index,
|
|
580
|
+
type: "delete"
|
|
581
|
+
} ]
|
|
582
|
+
}), this._doc.dispatch([ {
|
|
583
|
+
id: childRecordId,
|
|
584
|
+
opId: this._doc.generateOpId(),
|
|
585
|
+
type: exports.OpType.DeleteCrdt
|
|
586
|
+
} ], item[0]._serialize(this._id, item[1]), storageUpdates);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
}, _proto.clear = function() {
|
|
590
|
+
if (this._doc) {
|
|
591
|
+
for (var _step5, ops = [], reverseOps = [], updateDelta = [], i = 0, _iterator5 = _createForOfIteratorHelperLoose(this._items); !(_step5 = _iterator5()).done; ) {
|
|
592
|
+
var _item3 = _step5.value;
|
|
593
|
+
_item3[0]._detach();
|
|
594
|
+
var childId = _item3[0]._id;
|
|
595
|
+
childId && (ops.push({
|
|
596
|
+
id: childId,
|
|
597
|
+
type: exports.OpType.DeleteCrdt
|
|
598
|
+
}), reverseOps.push.apply(reverseOps, _item3[0]._serialize(this._id, _item3[1])),
|
|
599
|
+
updateDelta.push({
|
|
600
|
+
index: i,
|
|
601
|
+
type: "delete"
|
|
602
|
+
})), i++;
|
|
603
|
+
}
|
|
604
|
+
this._items = [];
|
|
605
|
+
var storageUpdates = new Map;
|
|
606
|
+
storageUpdates.set(this._id, {
|
|
607
|
+
node: this,
|
|
608
|
+
type: "LiveList",
|
|
609
|
+
updates: updateDelta
|
|
610
|
+
}), this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
611
|
+
} else {
|
|
612
|
+
for (var _step6, _iterator6 = _createForOfIteratorHelperLoose(this._items); !(_step6 = _iterator6()).done; ) {
|
|
613
|
+
_step6.value[0]._detach();
|
|
614
|
+
}
|
|
615
|
+
this._items = [];
|
|
616
|
+
}
|
|
617
|
+
}, _proto.set = function(index, item) {
|
|
618
|
+
if (index < 0 || index >= this._items.length) throw new Error('Cannot set list item at index "' + index + '". index should be between 0 and ' + (this._items.length - 1));
|
|
619
|
+
var _this$_items$index = this._items[index], existingItem = _this$_items$index[0], position = _this$_items$index[1];
|
|
620
|
+
existingItem._detach();
|
|
621
|
+
var value = selfOrRegister(item);
|
|
622
|
+
if (value._setParentLink(this, position), this._items[index][0] = value, this._doc && this._id) {
|
|
623
|
+
var _id2 = this._doc.generateId();
|
|
624
|
+
value._attach(_id2, this._doc);
|
|
625
|
+
var storageUpdates = new Map;
|
|
626
|
+
storageUpdates.set(this._id, {
|
|
627
|
+
node: this,
|
|
628
|
+
type: "LiveList",
|
|
629
|
+
updates: [ {
|
|
630
|
+
index: index,
|
|
631
|
+
item: value instanceof LiveRegister ? value.data : value,
|
|
632
|
+
type: "set"
|
|
633
|
+
} ]
|
|
634
|
+
}), this._doc.dispatch(value._serialize(this._id, position, this._doc, "set"), existingItem._serialize(this._id, position, void 0, "set"), storageUpdates);
|
|
635
|
+
}
|
|
636
|
+
}, _proto.toArray = function() {
|
|
637
|
+
return this._items.map((function(entry) {
|
|
638
|
+
return selfOrRegisterValue(entry[0]);
|
|
639
|
+
}));
|
|
640
|
+
}, _proto.every = function(predicate) {
|
|
641
|
+
return this.toArray().every(predicate);
|
|
642
|
+
}, _proto.filter = function(predicate) {
|
|
643
|
+
return this.toArray().filter(predicate);
|
|
644
|
+
}, _proto.find = function(predicate) {
|
|
645
|
+
return this.toArray().find(predicate);
|
|
646
|
+
}, _proto.findIndex = function(predicate) {
|
|
647
|
+
return this.toArray().findIndex(predicate);
|
|
648
|
+
}, _proto.forEach = function(callbackfn) {
|
|
649
|
+
return this.toArray().forEach(callbackfn);
|
|
650
|
+
}, _proto.get = function(index) {
|
|
651
|
+
if (!(index < 0 || index >= this._items.length)) return selfOrRegisterValue(this._items[index][0]);
|
|
652
|
+
}, _proto.indexOf = function(searchElement, fromIndex) {
|
|
653
|
+
return this.toArray().indexOf(searchElement, fromIndex);
|
|
654
|
+
}, _proto.lastIndexOf = function(searchElement, fromIndex) {
|
|
655
|
+
return this.toArray().lastIndexOf(searchElement, fromIndex);
|
|
656
|
+
}, _proto.map = function(callback) {
|
|
657
|
+
return this._items.map((function(entry, i) {
|
|
658
|
+
return callback(selfOrRegisterValue(entry[0]), i);
|
|
659
|
+
}));
|
|
660
|
+
}, _proto.some = function(predicate) {
|
|
661
|
+
return this.toArray().some(predicate);
|
|
662
|
+
}, _proto[_Symbol$iterator$1] = function() {
|
|
663
|
+
return new LiveListIterator(this._items);
|
|
664
|
+
}, _createClass(LiveList, [ {
|
|
665
|
+
key: "length",
|
|
666
|
+
get: function() {
|
|
667
|
+
return this._items.length;
|
|
668
|
+
}
|
|
669
|
+
} ]), LiveList;
|
|
534
670
|
}(AbstractCrdt);
|
|
535
671
|
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
672
|
+
_Symbol$iterator2 = Symbol.iterator;
|
|
673
|
+
|
|
674
|
+
var _Symbol$iterator, LiveListIterator = function() {
|
|
675
|
+
function LiveListIterator(items) {
|
|
676
|
+
this._innerIterator = void 0, this._innerIterator = items[Symbol.iterator]();
|
|
677
|
+
}
|
|
678
|
+
var _proto2 = LiveListIterator.prototype;
|
|
679
|
+
return _proto2[_Symbol$iterator2] = function() {
|
|
680
|
+
return this;
|
|
681
|
+
}, _proto2.next = function() {
|
|
682
|
+
var result = this._innerIterator.next();
|
|
683
|
+
return result.done ? {
|
|
684
|
+
done: !0,
|
|
685
|
+
value: void 0
|
|
686
|
+
} : {
|
|
687
|
+
value: selfOrRegisterValue(result.value[0])
|
|
688
|
+
};
|
|
689
|
+
}, LiveListIterator;
|
|
690
|
+
}(), _emittedDeprecationWarnings = new Set;
|
|
549
691
|
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
692
|
+
function deprecate(message, key) {
|
|
693
|
+
void 0 === key && (key = message), "production" !== process.env.NODE_ENV && (_emittedDeprecationWarnings.has(key) || (_emittedDeprecationWarnings.add(key),
|
|
694
|
+
console.error("DEPRECATION WARNING: " + message)));
|
|
695
|
+
}
|
|
554
696
|
|
|
555
|
-
|
|
697
|
+
function deprecateIf(condition, message, key) {
|
|
698
|
+
void 0 === key && (key = message), "production" !== process.env.NODE_ENV && condition && deprecate(message, key);
|
|
699
|
+
}
|
|
556
700
|
|
|
557
|
-
|
|
558
|
-
|
|
701
|
+
function throwUsageError(message) {
|
|
702
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
703
|
+
var usageError = new Error(message);
|
|
704
|
+
throw usageError.name = "Usage error", usageError;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
559
707
|
|
|
560
|
-
|
|
708
|
+
_Symbol$iterator = Symbol.iterator;
|
|
709
|
+
|
|
710
|
+
var LiveMap = function(_AbstractCrdt) {
|
|
711
|
+
function LiveMap(entries) {
|
|
712
|
+
var _this;
|
|
713
|
+
if ((_this = _AbstractCrdt.call(this) || this)._map = void 0, deprecateIf(null === entries, "Support for calling `new LiveMap(null)` will be removed in @liveblocks/client 0.18. Please call as `new LiveMap()`, or `new LiveMap([])`."),
|
|
714
|
+
entries) {
|
|
715
|
+
for (var _step, mappedEntries = [], _iterator = _createForOfIteratorHelperLoose(entries); !(_step = _iterator()).done; ) {
|
|
716
|
+
var entry = _step.value, _value = selfOrRegister(entry[1]);
|
|
717
|
+
_value._setParentLink(_assertThisInitialized(_this), entry[0]), mappedEntries.push([ entry[0], _value ]);
|
|
718
|
+
}
|
|
719
|
+
_this._map = new Map(mappedEntries);
|
|
720
|
+
} else _this._map = new Map;
|
|
721
|
+
return _this;
|
|
722
|
+
}
|
|
723
|
+
_inheritsLoose(LiveMap, _AbstractCrdt);
|
|
724
|
+
var _proto = LiveMap.prototype;
|
|
725
|
+
return _proto._serialize = function(parentId, parentKey, doc, intent) {
|
|
726
|
+
if (null == this._id) throw new Error("Cannot serialize item is not attached");
|
|
727
|
+
if (null == parentId || null == parentKey) throw new Error("Cannot serialize map if parentId or parentKey is undefined");
|
|
728
|
+
var ops = [], op = {
|
|
729
|
+
id: this._id,
|
|
730
|
+
opId: null == doc ? void 0 : doc.generateOpId(),
|
|
731
|
+
type: exports.OpType.CreateMap,
|
|
732
|
+
intent: intent,
|
|
733
|
+
parentId: parentId,
|
|
734
|
+
parentKey: parentKey
|
|
735
|
+
};
|
|
736
|
+
ops.push(op);
|
|
737
|
+
for (var _step2, _iterator2 = _createForOfIteratorHelperLoose(this._map); !(_step2 = _iterator2()).done; ) {
|
|
738
|
+
var _step2$value = _step2.value, _key2 = _step2$value[0], _value2 = _step2$value[1];
|
|
739
|
+
ops.push.apply(ops, _value2._serialize(this._id, _key2, doc));
|
|
561
740
|
}
|
|
741
|
+
return ops;
|
|
742
|
+
}, LiveMap._deserialize = function(_ref, parentToChildren, doc) {
|
|
743
|
+
var id = _ref[0], item = _ref[1];
|
|
744
|
+
if (item.type !== exports.CrdtType.Map) throw new Error('Tried to deserialize a map but item type is "' + item.type + '"');
|
|
745
|
+
var map = new LiveMap;
|
|
746
|
+
map._attach(id, doc);
|
|
747
|
+
var children = parentToChildren.get(id);
|
|
748
|
+
if (null == children) return map;
|
|
749
|
+
for (var _step3, _iterator3 = _createForOfIteratorHelperLoose(children); !(_step3 = _iterator3()).done; ) {
|
|
750
|
+
var entry = _step3.value, crdt = entry[1];
|
|
751
|
+
if (null == crdt.parentKey) throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
|
|
752
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
753
|
+
child._setParentLink(map, crdt.parentKey), map._map.set(crdt.parentKey, child);
|
|
754
|
+
}
|
|
755
|
+
return map;
|
|
756
|
+
}, _proto._attach = function(id, doc) {
|
|
757
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
758
|
+
for (var _step4, _iterator4 = _createForOfIteratorHelperLoose(this._map); !(_step4 = _iterator4()).done; ) {
|
|
759
|
+
var _step4$value = _step4.value;
|
|
760
|
+
_step4$value[0];
|
|
761
|
+
var _value3 = _step4$value[1];
|
|
762
|
+
isCrdt(_value3) && _value3._attach(doc.generateId(), doc);
|
|
763
|
+
}
|
|
764
|
+
}, _proto._attachChild = function(op, _isLocal) {
|
|
765
|
+
var _updates;
|
|
766
|
+
if (null == this._doc) throw new Error("Can't attach child if doc is not present");
|
|
767
|
+
var id = op.id, key = op.parentKey, child = creationOpToLiveStructure(op);
|
|
768
|
+
if (void 0 !== this._doc.getItem(id)) return {
|
|
769
|
+
modified: !1
|
|
770
|
+
};
|
|
771
|
+
var reverse, previousValue = this._map.get(key);
|
|
772
|
+
return previousValue ? (reverse = previousValue._serialize(this._id, key), previousValue._detach()) : reverse = [ {
|
|
773
|
+
type: exports.OpType.DeleteCrdt,
|
|
774
|
+
id: id
|
|
775
|
+
} ], child._setParentLink(this, key), child._attach(id, this._doc), this._map.set(key, child),
|
|
776
|
+
{
|
|
777
|
+
modified: {
|
|
778
|
+
node: this,
|
|
779
|
+
type: "LiveMap",
|
|
780
|
+
updates: (_updates = {}, _updates[key] = {
|
|
781
|
+
type: "update"
|
|
782
|
+
}, _updates)
|
|
783
|
+
},
|
|
784
|
+
reverse: reverse
|
|
785
|
+
};
|
|
786
|
+
}, _proto._detach = function() {
|
|
787
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
788
|
+
for (var _step5, _iterator5 = _createForOfIteratorHelperLoose(this._map.values()); !(_step5 = _iterator5()).done; ) {
|
|
789
|
+
_step5.value._detach();
|
|
790
|
+
}
|
|
791
|
+
}, _proto._detachChild = function(child) {
|
|
792
|
+
for (var _updates2, _step6, reverse = child._serialize(this._id, child._parentKey, this._doc), _iterator6 = _createForOfIteratorHelperLoose(this._map); !(_step6 = _iterator6()).done; ) {
|
|
793
|
+
var _step6$value = _step6.value, _key3 = _step6$value[0];
|
|
794
|
+
_step6$value[1] === child && this._map.delete(_key3);
|
|
795
|
+
}
|
|
796
|
+
return child._detach(), {
|
|
797
|
+
modified: {
|
|
798
|
+
node: this,
|
|
799
|
+
type: "LiveMap",
|
|
800
|
+
updates: (_updates2 = {}, _updates2[child._parentKey] = {
|
|
801
|
+
type: "delete"
|
|
802
|
+
}, _updates2)
|
|
803
|
+
},
|
|
804
|
+
reverse: reverse
|
|
805
|
+
};
|
|
806
|
+
}, _proto._toSerializedCrdt = function() {
|
|
807
|
+
var _this$_parent;
|
|
808
|
+
return {
|
|
809
|
+
type: exports.CrdtType.Map,
|
|
810
|
+
parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
|
|
811
|
+
parentKey: this._parentKey
|
|
812
|
+
};
|
|
813
|
+
}, _proto.get = function(key) {
|
|
814
|
+
var value = this._map.get(key);
|
|
815
|
+
if (null != value) return selfOrRegisterValue(value);
|
|
816
|
+
}, _proto.set = function(key, value) {
|
|
817
|
+
var oldValue = this._map.get(key);
|
|
818
|
+
oldValue && oldValue._detach();
|
|
819
|
+
var item = selfOrRegister(value);
|
|
820
|
+
if (item._setParentLink(this, key), this._map.set(key, item), this._doc && this._id) {
|
|
821
|
+
var _updates3, id = this._doc.generateId();
|
|
822
|
+
item._attach(id, this._doc);
|
|
823
|
+
var storageUpdates = new Map;
|
|
824
|
+
storageUpdates.set(this._id, {
|
|
825
|
+
node: this,
|
|
826
|
+
type: "LiveMap",
|
|
827
|
+
updates: (_updates3 = {}, _updates3[key] = {
|
|
828
|
+
type: "update"
|
|
829
|
+
}, _updates3)
|
|
830
|
+
}), this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue ? oldValue._serialize(this._id, key) : [ {
|
|
831
|
+
type: exports.OpType.DeleteCrdt,
|
|
832
|
+
id: id
|
|
833
|
+
} ], storageUpdates);
|
|
834
|
+
}
|
|
835
|
+
}, _proto.has = function(key) {
|
|
836
|
+
return this._map.has(key);
|
|
837
|
+
}, _proto.delete = function(key) {
|
|
838
|
+
var item = this._map.get(key);
|
|
839
|
+
if (null == item) return !1;
|
|
840
|
+
if (item._detach(), this._map.delete(key), this._doc && item._id) {
|
|
841
|
+
var _updates4, storageUpdates = new Map;
|
|
842
|
+
storageUpdates.set(this._id, {
|
|
843
|
+
node: this,
|
|
844
|
+
type: "LiveMap",
|
|
845
|
+
updates: (_updates4 = {}, _updates4[key] = {
|
|
846
|
+
type: "delete"
|
|
847
|
+
}, _updates4)
|
|
848
|
+
}), this._doc.dispatch([ {
|
|
849
|
+
type: exports.OpType.DeleteCrdt,
|
|
850
|
+
id: item._id,
|
|
851
|
+
opId: this._doc.generateOpId()
|
|
852
|
+
} ], item._serialize(this._id, key), storageUpdates);
|
|
853
|
+
}
|
|
854
|
+
return !0;
|
|
855
|
+
}, _proto.entries = function() {
|
|
856
|
+
var _ref2, innerIterator = this._map.entries();
|
|
857
|
+
return (_ref2 = {})[Symbol.iterator] = function() {
|
|
858
|
+
return this;
|
|
859
|
+
}, _ref2.next = function() {
|
|
860
|
+
var iteratorValue = innerIterator.next();
|
|
861
|
+
return iteratorValue.done ? {
|
|
862
|
+
done: !0,
|
|
863
|
+
value: void 0
|
|
864
|
+
} : {
|
|
865
|
+
value: [ iteratorValue.value[0], selfOrRegisterValue(iteratorValue.value[1]) ]
|
|
866
|
+
};
|
|
867
|
+
}, _ref2;
|
|
868
|
+
}, _proto[_Symbol$iterator] = function() {
|
|
869
|
+
return this.entries();
|
|
870
|
+
}, _proto.keys = function() {
|
|
871
|
+
return this._map.keys();
|
|
872
|
+
}, _proto.values = function() {
|
|
873
|
+
var _ref3, innerIterator = this._map.values();
|
|
874
|
+
return (_ref3 = {})[Symbol.iterator] = function() {
|
|
875
|
+
return this;
|
|
876
|
+
}, _ref3.next = function() {
|
|
877
|
+
var iteratorValue = innerIterator.next();
|
|
878
|
+
return iteratorValue.done ? {
|
|
879
|
+
done: !0,
|
|
880
|
+
value: void 0
|
|
881
|
+
} : {
|
|
882
|
+
value: selfOrRegisterValue(iteratorValue.value)
|
|
883
|
+
};
|
|
884
|
+
}, _ref3;
|
|
885
|
+
}, _proto.forEach = function(callback) {
|
|
886
|
+
for (var _step7, _iterator7 = _createForOfIteratorHelperLoose(this); !(_step7 = _iterator7()).done; ) {
|
|
887
|
+
var entry = _step7.value;
|
|
888
|
+
callback(entry[1], entry[0], this);
|
|
889
|
+
}
|
|
890
|
+
}, _createClass(LiveMap, [ {
|
|
891
|
+
key: "size",
|
|
892
|
+
get: function() {
|
|
893
|
+
return this._map.size;
|
|
894
|
+
}
|
|
895
|
+
} ]), LiveMap;
|
|
896
|
+
}(AbstractCrdt);
|
|
562
897
|
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
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
|
-
}
|
|
898
|
+
function creationOpToLiveStructure(op) {
|
|
899
|
+
switch (op.type) {
|
|
900
|
+
case exports.OpType.CreateRegister:
|
|
901
|
+
return new LiveRegister(op.data);
|
|
587
902
|
|
|
588
|
-
|
|
589
|
-
|
|
903
|
+
case exports.OpType.CreateObject:
|
|
904
|
+
return new LiveObject(op.data);
|
|
590
905
|
|
|
591
|
-
|
|
906
|
+
case exports.OpType.CreateMap:
|
|
907
|
+
return new LiveMap;
|
|
592
908
|
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
909
|
+
case exports.OpType.CreateList:
|
|
910
|
+
return new LiveList;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
597
913
|
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
914
|
+
function deserialize(entry, parentToChildren, doc) {
|
|
915
|
+
switch (entry[1].type) {
|
|
916
|
+
case exports.CrdtType.Object:
|
|
917
|
+
return LiveObject._deserialize(entry, parentToChildren, doc);
|
|
601
918
|
|
|
602
|
-
|
|
603
|
-
|
|
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);
|
|
919
|
+
case exports.CrdtType.List:
|
|
920
|
+
return LiveList._deserialize(entry, parentToChildren, doc);
|
|
612
921
|
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
_value = _step2$value[0],
|
|
616
|
-
key = _step2$value[1];
|
|
617
|
-
ops.push.apply(ops, _value._serialize(this._id, key, doc));
|
|
618
|
-
}
|
|
922
|
+
case exports.CrdtType.Map:
|
|
923
|
+
return LiveMap._deserialize(entry, parentToChildren, doc);
|
|
619
924
|
|
|
620
|
-
|
|
621
|
-
|
|
925
|
+
case exports.CrdtType.Register:
|
|
926
|
+
return LiveRegister._deserialize(entry, parentToChildren, doc);
|
|
622
927
|
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
};
|
|
928
|
+
default:
|
|
929
|
+
throw new Error("Unexpected CRDT type");
|
|
930
|
+
}
|
|
931
|
+
}
|
|
628
932
|
|
|
629
|
-
|
|
630
|
-
|
|
933
|
+
function isCrdt(obj) {
|
|
934
|
+
return obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList || obj instanceof LiveRegister;
|
|
935
|
+
}
|
|
631
936
|
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
937
|
+
function selfOrRegisterValue(obj) {
|
|
938
|
+
return obj instanceof LiveRegister ? obj.data : obj;
|
|
939
|
+
}
|
|
635
940
|
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
941
|
+
function selfOrRegister(obj) {
|
|
942
|
+
if (obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList) return obj;
|
|
943
|
+
if (obj instanceof LiveRegister) throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
|
|
944
|
+
return new LiveRegister(obj);
|
|
945
|
+
}
|
|
639
946
|
|
|
640
|
-
|
|
641
|
-
|
|
947
|
+
function isPlain(value) {
|
|
948
|
+
var type = typeof value;
|
|
949
|
+
return "undefined" === type || null === value || "string" === type || "boolean" === type || "number" === type || Array.isArray(value) || function(value) {
|
|
950
|
+
if ("object" != typeof value || null === value) return !1;
|
|
951
|
+
var proto = Object.getPrototypeOf(value);
|
|
952
|
+
if (null === proto) return !0;
|
|
953
|
+
var baseProto = proto;
|
|
954
|
+
for (;null !== Object.getPrototypeOf(baseProto); ) baseProto = Object.getPrototypeOf(baseProto);
|
|
955
|
+
return proto === baseProto;
|
|
956
|
+
}(value);
|
|
957
|
+
}
|
|
642
958
|
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
959
|
+
function entries(obj) {
|
|
960
|
+
return Object.entries(obj);
|
|
961
|
+
}
|
|
646
962
|
|
|
647
|
-
|
|
648
|
-
|
|
963
|
+
var LiveObject = function(_AbstractCrdt) {
|
|
964
|
+
function LiveObject(obj) {
|
|
965
|
+
var _this;
|
|
966
|
+
for (var key in void 0 === obj && (obj = {}), (_this = _AbstractCrdt.call(this) || this)._map = void 0,
|
|
967
|
+
_this._propToLastUpdate = void 0, _this._propToLastUpdate = new Map, obj) {
|
|
968
|
+
var value = obj[key];
|
|
969
|
+
value instanceof AbstractCrdt && value._setParentLink(_assertThisInitialized(_this), key);
|
|
970
|
+
}
|
|
971
|
+
return _this._map = new Map(Object.entries(obj)), _this;
|
|
972
|
+
}
|
|
973
|
+
_inheritsLoose(LiveObject, _AbstractCrdt);
|
|
974
|
+
var _proto = LiveObject.prototype;
|
|
975
|
+
return _proto._serialize = function(parentId, parentKey, doc, intent) {
|
|
976
|
+
if (null == this._id) throw new Error("Cannot serialize item is not attached");
|
|
977
|
+
var ops = [], op = {
|
|
978
|
+
id: this._id,
|
|
979
|
+
opId: null == doc ? void 0 : doc.generateOpId(),
|
|
980
|
+
intent: intent,
|
|
981
|
+
type: exports.OpType.CreateObject,
|
|
982
|
+
parentId: parentId,
|
|
983
|
+
parentKey: parentKey,
|
|
984
|
+
data: {}
|
|
985
|
+
};
|
|
986
|
+
ops.push(op);
|
|
987
|
+
for (var _step, _iterator = _createForOfIteratorHelperLoose(this._map); !(_step = _iterator()).done; ) {
|
|
988
|
+
var _step$value = _step.value, key = _step$value[0], value = _step$value[1];
|
|
989
|
+
value instanceof AbstractCrdt ? ops.push.apply(ops, value._serialize(this._id, key, doc)) : op.data[key] = value;
|
|
990
|
+
}
|
|
991
|
+
return ops;
|
|
992
|
+
}, LiveObject._deserialize = function(_ref, parentToChildren, doc) {
|
|
993
|
+
var id = _ref[0], item = _ref[1];
|
|
994
|
+
if (item.type !== exports.CrdtType.Object) throw new Error('Tried to deserialize a record but item type is "' + item.type + '"');
|
|
995
|
+
var liveObj = new LiveObject(item.data);
|
|
996
|
+
return liveObj._attach(id, doc), this._deserializeChildren(liveObj, parentToChildren, doc);
|
|
997
|
+
}, LiveObject._deserializeChildren = function(liveObj, parentToChildren, doc) {
|
|
998
|
+
var children = parentToChildren.get(liveObj._id);
|
|
999
|
+
if (null == children) return liveObj;
|
|
1000
|
+
for (var _step2, _iterator2 = _createForOfIteratorHelperLoose(children); !(_step2 = _iterator2()).done; ) {
|
|
1001
|
+
var entry = _step2.value, crdt = entry[1];
|
|
1002
|
+
if (null == crdt.parentKey) throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
|
|
1003
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
1004
|
+
child._setParentLink(liveObj, crdt.parentKey), liveObj._map.set(crdt.parentKey, child);
|
|
1005
|
+
}
|
|
1006
|
+
return liveObj;
|
|
1007
|
+
}, _proto._attach = function(id, doc) {
|
|
1008
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
1009
|
+
for (var _step3, _iterator3 = _createForOfIteratorHelperLoose(this._map); !(_step3 = _iterator3()).done; ) {
|
|
1010
|
+
var _step3$value = _step3.value;
|
|
1011
|
+
_step3$value[0];
|
|
1012
|
+
var value = _step3$value[1];
|
|
1013
|
+
value instanceof AbstractCrdt && value._attach(doc.generateId(), doc);
|
|
1014
|
+
}
|
|
1015
|
+
}, _proto._attachChild = function(op, isLocal) {
|
|
1016
|
+
var _updates;
|
|
1017
|
+
if (null == this._doc) throw new Error("Can't attach child if doc is not present");
|
|
1018
|
+
var id = op.id, parentKey = op.parentKey, opId = op.opId, key = parentKey, child = creationOpToLiveStructure(op);
|
|
1019
|
+
if (void 0 !== this._doc.getItem(id)) return this._propToLastUpdate.get(key) === opId && this._propToLastUpdate.delete(key),
|
|
1020
|
+
{
|
|
1021
|
+
modified: !1
|
|
1022
|
+
};
|
|
1023
|
+
if (isLocal) this._propToLastUpdate.set(key, opId); else if (void 0 !== this._propToLastUpdate.get(key)) return this._propToLastUpdate.get(key) === opId ? (this._propToLastUpdate.delete(key),
|
|
1024
|
+
{
|
|
1025
|
+
modified: !1
|
|
1026
|
+
}) : {
|
|
1027
|
+
modified: !1
|
|
1028
|
+
};
|
|
1029
|
+
var reverse, previousValue = this._map.get(key);
|
|
1030
|
+
if (isCrdt(previousValue)) reverse = previousValue._serialize(this._id, key), previousValue._detach(); else if (void 0 === previousValue) reverse = [ {
|
|
1031
|
+
type: exports.OpType.DeleteObjectKey,
|
|
1032
|
+
id: this._id,
|
|
1033
|
+
key: key
|
|
1034
|
+
} ]; else {
|
|
1035
|
+
var _data;
|
|
1036
|
+
reverse = [ {
|
|
1037
|
+
type: exports.OpType.UpdateObject,
|
|
1038
|
+
id: this._id,
|
|
1039
|
+
data: (_data = {}, _data[key] = previousValue, _data)
|
|
1040
|
+
} ];
|
|
1041
|
+
}
|
|
1042
|
+
return this._map.set(key, child), child._setParentLink(this, key), child._attach(id, this._doc),
|
|
1043
|
+
{
|
|
1044
|
+
reverse: reverse,
|
|
1045
|
+
modified: {
|
|
1046
|
+
node: this,
|
|
1047
|
+
type: "LiveObject",
|
|
1048
|
+
updates: (_updates = {}, _updates[key] = {
|
|
1049
|
+
type: "update"
|
|
1050
|
+
}, _updates)
|
|
1051
|
+
}
|
|
1052
|
+
};
|
|
1053
|
+
}, _proto._detachChild = function(child) {
|
|
1054
|
+
if (child) {
|
|
1055
|
+
for (var _updates2, _step4, reverse = child._serialize(this._id, child._parentKey, this._doc), _iterator4 = _createForOfIteratorHelperLoose(this._map); !(_step4 = _iterator4()).done; ) {
|
|
1056
|
+
var _step4$value = _step4.value, key = _step4$value[0];
|
|
1057
|
+
_step4$value[1] === child && this._map.delete(key);
|
|
1058
|
+
}
|
|
1059
|
+
return child._detach(), {
|
|
1060
|
+
modified: {
|
|
1061
|
+
node: this,
|
|
1062
|
+
type: "LiveObject",
|
|
1063
|
+
updates: (_updates2 = {}, _updates2[child._parentKey] = {
|
|
1064
|
+
type: "delete"
|
|
1065
|
+
}, _updates2)
|
|
1066
|
+
},
|
|
1067
|
+
reverse: reverse
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
return {
|
|
1071
|
+
modified: !1
|
|
649
1072
|
};
|
|
1073
|
+
}, _proto._detachChildren = function() {
|
|
1074
|
+
for (var _step5, _iterator5 = _createForOfIteratorHelperLoose(this._map); !(_step5 = _iterator5()).done; ) {
|
|
1075
|
+
var _step5$value = _step5.value, key = _step5$value[0], value = _step5$value[1];
|
|
1076
|
+
this._map.delete(key), value._detach();
|
|
1077
|
+
}
|
|
1078
|
+
}, _proto._detach = function() {
|
|
1079
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
1080
|
+
for (var _step6, _iterator6 = _createForOfIteratorHelperLoose(this._map.values()); !(_step6 = _iterator6()).done; ) {
|
|
1081
|
+
var value = _step6.value;
|
|
1082
|
+
isCrdt(value) && value._detach();
|
|
1083
|
+
}
|
|
1084
|
+
}, _proto._apply = function(op, isLocal) {
|
|
1085
|
+
return op.type === exports.OpType.UpdateObject ? this._applyUpdate(op, isLocal) : op.type === exports.OpType.DeleteObjectKey ? this._applyDeleteObjectKey(op) : _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
1086
|
+
}, _proto._toSerializedCrdt = function() {
|
|
1087
|
+
for (var _this$_parent, _step7, data = {}, _iterator7 = _createForOfIteratorHelperLoose(this._map); !(_step7 = _iterator7()).done; ) {
|
|
1088
|
+
var _step7$value = _step7.value, key = _step7$value[0], value = _step7$value[1];
|
|
1089
|
+
value instanceof AbstractCrdt == !1 && (data[key] = value);
|
|
1090
|
+
}
|
|
1091
|
+
return {
|
|
1092
|
+
type: exports.CrdtType.Object,
|
|
1093
|
+
parentId: null == (_this$_parent = this._parent) ? void 0 : _this$_parent._id,
|
|
1094
|
+
parentKey: this._parentKey,
|
|
1095
|
+
data: data
|
|
1096
|
+
};
|
|
1097
|
+
}, _proto._applyUpdate = function(op, isLocal) {
|
|
1098
|
+
var isModified = !1, reverse = [], reverseUpdate = {
|
|
1099
|
+
type: exports.OpType.UpdateObject,
|
|
1100
|
+
id: this._id,
|
|
1101
|
+
data: {}
|
|
1102
|
+
};
|
|
1103
|
+
for (var key in reverse.push(reverseUpdate), op.data) {
|
|
1104
|
+
var oldValue = this._map.get(key);
|
|
1105
|
+
oldValue instanceof AbstractCrdt ? (reverse.push.apply(reverse, oldValue._serialize(this._id, key)),
|
|
1106
|
+
oldValue._detach()) : void 0 !== oldValue ? reverseUpdate.data[key] = oldValue : void 0 === oldValue && reverse.push({
|
|
1107
|
+
type: exports.OpType.DeleteObjectKey,
|
|
1108
|
+
id: this._id,
|
|
1109
|
+
key: key
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
var updateDelta = {};
|
|
1113
|
+
for (var _key2 in op.data) {
|
|
1114
|
+
if (isLocal) this._propToLastUpdate.set(_key2, op.opId); else {
|
|
1115
|
+
if (null != this._propToLastUpdate.get(_key2)) {
|
|
1116
|
+
if (this._propToLastUpdate.get(_key2) === op.opId) {
|
|
1117
|
+
this._propToLastUpdate.delete(_key2);
|
|
1118
|
+
continue;
|
|
1119
|
+
}
|
|
1120
|
+
continue;
|
|
1121
|
+
}
|
|
1122
|
+
isModified = !0;
|
|
1123
|
+
}
|
|
1124
|
+
var _oldValue = this._map.get(_key2);
|
|
1125
|
+
isCrdt(_oldValue) && _oldValue._detach(), isModified = !0, updateDelta[_key2] = {
|
|
1126
|
+
type: "update"
|
|
1127
|
+
}, this._map.set(_key2, op.data[_key2]);
|
|
1128
|
+
}
|
|
1129
|
+
return 0 !== Object.keys(reverseUpdate.data).length && reverse.unshift(reverseUpdate),
|
|
1130
|
+
isModified ? {
|
|
1131
|
+
modified: {
|
|
1132
|
+
node: this,
|
|
1133
|
+
type: "LiveObject",
|
|
1134
|
+
updates: updateDelta
|
|
1135
|
+
},
|
|
1136
|
+
reverse: reverse
|
|
1137
|
+
} : {
|
|
1138
|
+
modified: !1
|
|
1139
|
+
};
|
|
1140
|
+
}, _proto._applyDeleteObjectKey = function(op) {
|
|
1141
|
+
var _updates3, key = op.key;
|
|
1142
|
+
if (!1 === this._map.has(key)) return {
|
|
1143
|
+
modified: !1
|
|
1144
|
+
};
|
|
1145
|
+
if (void 0 !== this._propToLastUpdate.get(key)) return {
|
|
1146
|
+
modified: !1
|
|
1147
|
+
};
|
|
1148
|
+
var oldValue = this._map.get(key), reverse = [];
|
|
1149
|
+
if (isCrdt(oldValue)) reverse = oldValue._serialize(this._id, op.key), oldValue._detach(); else if (void 0 !== oldValue) {
|
|
1150
|
+
var _data2;
|
|
1151
|
+
reverse = [ {
|
|
1152
|
+
type: exports.OpType.UpdateObject,
|
|
1153
|
+
id: this._id,
|
|
1154
|
+
data: (_data2 = {}, _data2[key] = oldValue, _data2)
|
|
1155
|
+
} ];
|
|
1156
|
+
}
|
|
1157
|
+
return this._map.delete(key), {
|
|
1158
|
+
modified: {
|
|
1159
|
+
node: this,
|
|
1160
|
+
type: "LiveObject",
|
|
1161
|
+
updates: (_updates3 = {}, _updates3[op.key] = {
|
|
1162
|
+
type: "delete"
|
|
1163
|
+
}, _updates3)
|
|
1164
|
+
},
|
|
1165
|
+
reverse: reverse
|
|
1166
|
+
};
|
|
1167
|
+
}, _proto.toObject = function() {
|
|
1168
|
+
return function(iterable) {
|
|
1169
|
+
for (var _step3, obj = {}, _iterator3 = _createForOfIteratorHelperLoose(iterable); !(_step3 = _iterator3()).done; ) {
|
|
1170
|
+
var _step3$value = _step3.value, _key4 = _step3$value[0], val = _step3$value[1];
|
|
1171
|
+
obj[_key4] = val;
|
|
1172
|
+
}
|
|
1173
|
+
return obj;
|
|
1174
|
+
}(this._map);
|
|
1175
|
+
}, _proto.set = function(key, value) {
|
|
1176
|
+
var _this$update;
|
|
1177
|
+
this.update(((_this$update = {})[key] = value, _this$update));
|
|
1178
|
+
}, _proto.get = function(key) {
|
|
1179
|
+
return this._map.get(key);
|
|
1180
|
+
}, _proto.delete = function(key) {
|
|
1181
|
+
var _updates4, keyAsString = key, oldValue = this._map.get(keyAsString);
|
|
1182
|
+
if (void 0 !== oldValue) {
|
|
1183
|
+
if (null == this._doc || null == this._id) return oldValue instanceof AbstractCrdt && oldValue._detach(),
|
|
1184
|
+
void this._map.delete(keyAsString);
|
|
1185
|
+
var reverse, _data3;
|
|
1186
|
+
if (oldValue instanceof AbstractCrdt) oldValue._detach(), reverse = oldValue._serialize(this._id, keyAsString); else reverse = [ {
|
|
1187
|
+
type: exports.OpType.UpdateObject,
|
|
1188
|
+
data: (_data3 = {}, _data3[keyAsString] = oldValue, _data3),
|
|
1189
|
+
id: this._id
|
|
1190
|
+
} ];
|
|
1191
|
+
this._map.delete(keyAsString);
|
|
1192
|
+
var storageUpdates = new Map;
|
|
1193
|
+
storageUpdates.set(this._id, {
|
|
1194
|
+
node: this,
|
|
1195
|
+
type: "LiveObject",
|
|
1196
|
+
updates: (_updates4 = {}, _updates4[key] = {
|
|
1197
|
+
type: "delete"
|
|
1198
|
+
}, _updates4)
|
|
1199
|
+
}), this._doc.dispatch([ {
|
|
1200
|
+
type: exports.OpType.DeleteObjectKey,
|
|
1201
|
+
key: keyAsString,
|
|
1202
|
+
id: this._id,
|
|
1203
|
+
opId: this._doc.generateOpId()
|
|
1204
|
+
} ], reverse, storageUpdates);
|
|
1205
|
+
}
|
|
1206
|
+
}, _proto.update = function(overrides) {
|
|
1207
|
+
var _this2 = this;
|
|
1208
|
+
if (null != this._doc && null != this._id) {
|
|
1209
|
+
var ops = [], reverseOps = [], opId = this._doc.generateOpId(), updatedProps = {}, reverseUpdateOp = {
|
|
1210
|
+
id: this._id,
|
|
1211
|
+
type: exports.OpType.UpdateObject,
|
|
1212
|
+
data: {}
|
|
1213
|
+
}, updateDelta = {};
|
|
1214
|
+
for (var _key3 in overrides) {
|
|
1215
|
+
var _oldValue2 = this._map.get(_key3);
|
|
1216
|
+
_oldValue2 instanceof AbstractCrdt ? (reverseOps.push.apply(reverseOps, _oldValue2._serialize(this._id, _key3)),
|
|
1217
|
+
_oldValue2._detach()) : void 0 === _oldValue2 ? reverseOps.push({
|
|
1218
|
+
type: exports.OpType.DeleteObjectKey,
|
|
1219
|
+
id: this._id,
|
|
1220
|
+
key: _key3
|
|
1221
|
+
}) : reverseUpdateOp.data[_key3] = _oldValue2;
|
|
1222
|
+
var _newValue = overrides[_key3];
|
|
1223
|
+
if (_newValue instanceof AbstractCrdt) {
|
|
1224
|
+
_newValue._setParentLink(this, _key3), _newValue._attach(this._doc.generateId(), this._doc);
|
|
1225
|
+
var newAttachChildOps = _newValue._serialize(this._id, _key3, this._doc), createCrdtOp = newAttachChildOps.find((function(op) {
|
|
1226
|
+
return op.parentId === _this2._id;
|
|
1227
|
+
}));
|
|
1228
|
+
createCrdtOp && this._propToLastUpdate.set(_key3, createCrdtOp.opId), ops.push.apply(ops, newAttachChildOps);
|
|
1229
|
+
} else updatedProps[_key3] = _newValue, this._propToLastUpdate.set(_key3, opId);
|
|
1230
|
+
this._map.set(_key3, _newValue), updateDelta[_key3] = {
|
|
1231
|
+
type: "update"
|
|
1232
|
+
};
|
|
1233
|
+
}
|
|
1234
|
+
0 !== Object.keys(reverseUpdateOp.data).length && reverseOps.unshift(reverseUpdateOp),
|
|
1235
|
+
0 !== Object.keys(updatedProps).length && ops.unshift({
|
|
1236
|
+
opId: opId,
|
|
1237
|
+
id: this._id,
|
|
1238
|
+
type: exports.OpType.UpdateObject,
|
|
1239
|
+
data: updatedProps
|
|
1240
|
+
});
|
|
1241
|
+
var storageUpdates = new Map;
|
|
1242
|
+
storageUpdates.set(this._id, {
|
|
1243
|
+
node: this,
|
|
1244
|
+
type: "LiveObject",
|
|
1245
|
+
updates: updateDelta
|
|
1246
|
+
}), this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
1247
|
+
} else for (var key in overrides) {
|
|
1248
|
+
var oldValue = this._map.get(key);
|
|
1249
|
+
oldValue instanceof AbstractCrdt && oldValue._detach();
|
|
1250
|
+
var newValue = overrides[key];
|
|
1251
|
+
newValue instanceof AbstractCrdt && newValue._setParentLink(this, key), this._map.set(key, newValue);
|
|
1252
|
+
}
|
|
1253
|
+
}, LiveObject;
|
|
1254
|
+
}(AbstractCrdt);
|
|
650
1255
|
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
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.error("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
|
-
}
|
|
1256
|
+
exports.AbstractCrdt = AbstractCrdt, exports.LiveList = LiveList, exports.LiveMap = LiveMap,
|
|
1257
|
+
exports.LiveObject = LiveObject, exports.LiveRegister = LiveRegister, exports._createForOfIteratorHelperLoose = _createForOfIteratorHelperLoose,
|
|
1258
|
+
exports._extends = _extends, exports._inheritsLoose = _inheritsLoose, exports._objectWithoutPropertiesLoose = function(source, excluded) {
|
|
1259
|
+
if (null == source) return {};
|
|
1260
|
+
var key, i, target = {}, sourceKeys = Object.keys(source);
|
|
1261
|
+
for (i = 0; i < sourceKeys.length; i++) key = sourceKeys[i], excluded.indexOf(key) >= 0 || (target[key] = source[key]);
|
|
1262
|
+
return target;
|
|
1263
|
+
}, exports._wrapNativeSuper = _wrapNativeSuper, exports.compact = function(items) {
|
|
1264
|
+
return items.filter((function(item) {
|
|
1265
|
+
return null != item;
|
|
1266
|
+
}));
|
|
1267
|
+
}, exports.comparePosition = comparePosition, exports.deprecate = deprecate, exports.deprecateIf = deprecateIf,
|
|
1268
|
+
exports.errorIf = function(condition, message) {
|
|
1269
|
+
"production" !== process.env.NODE_ENV && condition && throwUsageError(message);
|
|
1270
|
+
}, exports.findNonSerializableValue = function findNonSerializableValue(value, path) {
|
|
1271
|
+
if (void 0 === path && (path = ""), !isPlain) return {
|
|
1272
|
+
path: path || "root",
|
|
1273
|
+
value: value
|
|
1274
|
+
};
|
|
1275
|
+
if ("object" != typeof value || null === value) return !1;
|
|
1276
|
+
for (var _i = 0, _Object$entries = Object.entries(value); _i < _Object$entries.length; _i++) {
|
|
1277
|
+
var _Object$entries$_i = _Object$entries[_i], _key3 = _Object$entries$_i[0], nestedValue = _Object$entries$_i[1], nestedPath = path ? path + "." + _key3 : _key3;
|
|
1278
|
+
if (!isPlain(nestedValue)) return {
|
|
1279
|
+
path: nestedPath,
|
|
1280
|
+
value: nestedValue
|
|
1281
|
+
};
|
|
1282
|
+
if ("object" == typeof nestedValue) {
|
|
1283
|
+
var nonSerializableNestedValue = findNonSerializableValue(nestedValue, nestedPath);
|
|
1284
|
+
if (nonSerializableNestedValue) return nonSerializableNestedValue;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
return !1;
|
|
1288
|
+
}, exports.getTreesDiffOperations = function(currentItems, newItems) {
|
|
1289
|
+
var ops = [];
|
|
1290
|
+
return currentItems.forEach((function(_, id) {
|
|
1291
|
+
newItems.get(id) || ops.push({
|
|
1292
|
+
type: exports.OpType.DeleteCrdt,
|
|
1293
|
+
id: id
|
|
1734
1294
|
});
|
|
1735
|
-
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1295
|
+
})), newItems.forEach((function(crdt, id) {
|
|
1296
|
+
var currentCrdt = currentItems.get(id);
|
|
1297
|
+
if (currentCrdt) crdt.type === exports.CrdtType.Object && JSON.stringify(crdt.data) !== JSON.stringify(currentCrdt.data) && ops.push({
|
|
1298
|
+
type: exports.OpType.UpdateObject,
|
|
1299
|
+
id: id,
|
|
1300
|
+
data: crdt.data
|
|
1301
|
+
}), crdt.parentKey !== currentCrdt.parentKey && ops.push({
|
|
1302
|
+
type: exports.OpType.SetParentKey,
|
|
1303
|
+
id: id,
|
|
1304
|
+
parentKey: crdt.parentKey
|
|
1305
|
+
}); else switch (crdt.type) {
|
|
1306
|
+
case exports.CrdtType.Register:
|
|
1307
|
+
ops.push({
|
|
1308
|
+
type: exports.OpType.CreateRegister,
|
|
1309
|
+
id: id,
|
|
1310
|
+
parentId: crdt.parentId,
|
|
1311
|
+
parentKey: crdt.parentKey,
|
|
1312
|
+
data: crdt.data
|
|
1313
|
+
});
|
|
1314
|
+
break;
|
|
1315
|
+
|
|
1316
|
+
case exports.CrdtType.List:
|
|
1317
|
+
ops.push({
|
|
1318
|
+
type: exports.OpType.CreateList,
|
|
1319
|
+
id: id,
|
|
1320
|
+
parentId: crdt.parentId,
|
|
1321
|
+
parentKey: crdt.parentKey
|
|
1322
|
+
});
|
|
1323
|
+
break;
|
|
1324
|
+
|
|
1325
|
+
case exports.CrdtType.Object:
|
|
1326
|
+
ops.push({
|
|
1327
|
+
type: exports.OpType.CreateObject,
|
|
1328
|
+
id: id,
|
|
1329
|
+
parentId: crdt.parentId,
|
|
1330
|
+
parentKey: crdt.parentKey,
|
|
1331
|
+
data: crdt.data
|
|
1332
|
+
});
|
|
1333
|
+
break;
|
|
1334
|
+
|
|
1335
|
+
case exports.CrdtType.Map:
|
|
1336
|
+
ops.push({
|
|
1337
|
+
type: exports.OpType.CreateMap,
|
|
1338
|
+
id: id,
|
|
1339
|
+
parentId: crdt.parentId,
|
|
1340
|
+
parentKey: crdt.parentKey
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
})), ops;
|
|
1344
|
+
}, exports.isJsonArray = isJsonArray, exports.isJsonObject = isJsonObject, exports.isSameNodeOrChildOf = function isSameNodeOrChildOf(node, parent) {
|
|
1345
|
+
return node === parent || !!node._parent && isSameNodeOrChildOf(node._parent, parent);
|
|
1346
|
+
}, exports.isTokenValid = function(token) {
|
|
1347
|
+
var tokenParts = token.split(".");
|
|
1348
|
+
if (3 !== tokenParts.length) return !1;
|
|
1349
|
+
var data = parseJson(atob(tokenParts[1]));
|
|
1350
|
+
return !(void 0 === data || !isJsonObject(data) || "number" != typeof data.exp) && !(Date.now() / 1e3 > data.exp - 300);
|
|
1351
|
+
}, exports.makePosition = makePosition, exports.mergeStorageUpdates = function(first, second) {
|
|
1352
|
+
return first ? "LiveObject" === first.type && "LiveObject" === second.type ? function(first, second) {
|
|
1353
|
+
for (var _step, updates = first.updates, _iterator = _createForOfIteratorHelperLoose(entries(second.updates)); !(_step = _iterator()).done; ) {
|
|
1354
|
+
var _step$value = _step.value, _key = _step$value[0], value = _step$value[1];
|
|
1355
|
+
updates[_key] = value;
|
|
1746
1356
|
}
|
|
1747
|
-
|
|
1748
1357
|
return _extends({}, second, {
|
|
1749
|
-
|
|
1358
|
+
updates: updates
|
|
1750
1359
|
});
|
|
1751
|
-
}
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
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;
|
|
1360
|
+
}(first, second) : "LiveMap" === first.type && "LiveMap" === second.type ? function(first, second) {
|
|
1361
|
+
for (var _step2, updates = first.updates, _iterator2 = _createForOfIteratorHelperLoose(entries(second.updates)); !(_step2 = _iterator2()).done; ) {
|
|
1362
|
+
var _step2$value = _step2.value, _key2 = _step2$value[0], value = _step2$value[1];
|
|
1363
|
+
updates[_key2] = value;
|
|
1761
1364
|
}
|
|
1762
|
-
|
|
1763
1365
|
return _extends({}, second, {
|
|
1764
|
-
|
|
1366
|
+
updates: updates
|
|
1765
1367
|
});
|
|
1766
|
-
}
|
|
1767
|
-
|
|
1768
|
-
function mergeListStorageUpdates(first, second) {
|
|
1769
|
-
var updates = first.updates;
|
|
1368
|
+
}(first, second) : "LiveList" === first.type && "LiveList" === second.type ? function(first, second) {
|
|
1770
1369
|
return _extends({}, second, {
|
|
1771
|
-
|
|
1370
|
+
updates: first.updates.concat(second.updates)
|
|
1772
1371
|
});
|
|
1773
|
-
}
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
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.deprecate = deprecate;
|
|
2473
|
-
exports.deprecateIf = deprecateIf;
|
|
2474
|
-
exports.findNonSerializableValue = findNonSerializableValue;
|
|
2475
|
-
exports.getTreesDiffOperations = getTreesDiffOperations;
|
|
2476
|
-
exports.isJsonArray = isJsonArray;
|
|
2477
|
-
exports.isJsonObject = isJsonObject;
|
|
2478
|
-
exports.isSameNodeOrChildOf = isSameNodeOrChildOf;
|
|
2479
|
-
exports.isTokenValid = isTokenValid;
|
|
2480
|
-
exports.mergeStorageUpdates = mergeStorageUpdates;
|
|
2481
|
-
exports.parseJson = parseJson;
|
|
2482
|
-
exports.remove = remove;
|
|
1372
|
+
}(first, second) : second : second;
|
|
1373
|
+
}, exports.parseJson = parseJson, exports.remove = function(array, item) {
|
|
1374
|
+
for (var i = 0; i < array.length; i++) if (array[i] === item) {
|
|
1375
|
+
array.splice(i, 1);
|
|
1376
|
+
break;
|
|
1377
|
+
}
|
|
1378
|
+
}, exports.throwUsageError = throwUsageError;
|