@liveblocks/client 0.15.0-alpha.3 → 0.15.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -10
- package/lib/esm/index.js +2991 -5
- package/lib/esm/index.mjs +2991 -0
- package/lib/esm/internal.js +149 -0
- package/lib/esm/internal.mjs +149 -0
- package/lib/{esm/types.d.ts → index.d.ts} +253 -61
- package/lib/index.js +4237 -0
- package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
- package/lib/internal.js +193 -0
- package/package.json +32 -10
- package/lib/cjs/AbstractCrdt.d.ts +0 -68
- package/lib/cjs/AbstractCrdt.js +0 -95
- package/lib/cjs/LiveList.d.ts +0 -144
- package/lib/cjs/LiveList.js +0 -530
- package/lib/cjs/LiveMap.d.ts +0 -91
- package/lib/cjs/LiveMap.js +0 -325
- package/lib/cjs/LiveObject.d.ts +0 -80
- package/lib/cjs/LiveObject.js +0 -485
- package/lib/cjs/LiveRegister.d.ts +0 -29
- package/lib/cjs/LiveRegister.js +0 -88
- package/lib/cjs/client.d.ts +0 -27
- package/lib/cjs/client.js +0 -123
- package/lib/cjs/immutable.d.ts +0 -9
- package/lib/cjs/immutable.js +0 -299
- package/lib/cjs/index.d.ts +0 -6
- package/lib/cjs/index.js +0 -18
- package/lib/cjs/live.d.ts +0 -181
- package/lib/cjs/live.js +0 -49
- package/lib/cjs/position.d.ts +0 -6
- package/lib/cjs/position.js +0 -113
- package/lib/cjs/room.d.ts +0 -159
- package/lib/cjs/room.js +0 -1129
- package/lib/cjs/types.d.ts +0 -502
- package/lib/cjs/types.js +0 -2
- package/lib/cjs/utils.d.ts +0 -15
- package/lib/cjs/utils.js +0 -225
- package/lib/esm/AbstractCrdt.d.ts +0 -68
- package/lib/esm/AbstractCrdt.js +0 -91
- package/lib/esm/LiveList.d.ts +0 -144
- package/lib/esm/LiveList.js +0 -526
- package/lib/esm/LiveMap.d.ts +0 -91
- package/lib/esm/LiveMap.js +0 -321
- package/lib/esm/LiveObject.d.ts +0 -80
- package/lib/esm/LiveObject.js +0 -481
- package/lib/esm/LiveRegister.d.ts +0 -29
- package/lib/esm/LiveRegister.js +0 -84
- package/lib/esm/client.d.ts +0 -27
- package/lib/esm/client.js +0 -119
- package/lib/esm/immutable.d.ts +0 -9
- package/lib/esm/immutable.js +0 -290
- package/lib/esm/index.d.ts +0 -6
- package/lib/esm/live.js +0 -46
- package/lib/esm/position.d.ts +0 -6
- package/lib/esm/position.js +0 -106
- package/lib/esm/room.d.ts +0 -159
- package/lib/esm/room.js +0 -1123
- package/lib/esm/types.js +0 -1
- package/lib/esm/utils.d.ts +0 -15
- package/lib/esm/utils.js +0 -213
package/lib/index.js
ADDED
|
@@ -0,0 +1,4237 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
function _defineProperties(target, props) {
|
|
6
|
+
for (var i = 0; i < props.length; i++) {
|
|
7
|
+
var descriptor = props[i];
|
|
8
|
+
descriptor.enumerable = descriptor.enumerable || false;
|
|
9
|
+
descriptor.configurable = true;
|
|
10
|
+
if ("value" in descriptor) descriptor.writable = true;
|
|
11
|
+
Object.defineProperty(target, descriptor.key, descriptor);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function _createClass(Constructor, protoProps, staticProps) {
|
|
16
|
+
if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
|
17
|
+
if (staticProps) _defineProperties(Constructor, staticProps);
|
|
18
|
+
return Constructor;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function _extends() {
|
|
22
|
+
_extends = Object.assign || function (target) {
|
|
23
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
24
|
+
var source = arguments[i];
|
|
25
|
+
|
|
26
|
+
for (var key in source) {
|
|
27
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
28
|
+
target[key] = source[key];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return target;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
return _extends.apply(this, arguments);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function _inheritsLoose(subClass, superClass) {
|
|
40
|
+
subClass.prototype = Object.create(superClass.prototype);
|
|
41
|
+
subClass.prototype.constructor = subClass;
|
|
42
|
+
|
|
43
|
+
_setPrototypeOf(subClass, superClass);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function _getPrototypeOf(o) {
|
|
47
|
+
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
|
|
48
|
+
return o.__proto__ || Object.getPrototypeOf(o);
|
|
49
|
+
};
|
|
50
|
+
return _getPrototypeOf(o);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function _setPrototypeOf(o, p) {
|
|
54
|
+
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
|
|
55
|
+
o.__proto__ = p;
|
|
56
|
+
return o;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
return _setPrototypeOf(o, p);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function _isNativeReflectConstruct() {
|
|
63
|
+
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
|
64
|
+
if (Reflect.construct.sham) return false;
|
|
65
|
+
if (typeof Proxy === "function") return true;
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
|
69
|
+
return true;
|
|
70
|
+
} catch (e) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function _construct(Parent, args, Class) {
|
|
76
|
+
if (_isNativeReflectConstruct()) {
|
|
77
|
+
_construct = Reflect.construct;
|
|
78
|
+
} else {
|
|
79
|
+
_construct = function _construct(Parent, args, Class) {
|
|
80
|
+
var a = [null];
|
|
81
|
+
a.push.apply(a, args);
|
|
82
|
+
var Constructor = Function.bind.apply(Parent, a);
|
|
83
|
+
var instance = new Constructor();
|
|
84
|
+
if (Class) _setPrototypeOf(instance, Class.prototype);
|
|
85
|
+
return instance;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return _construct.apply(null, arguments);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function _isNativeFunction(fn) {
|
|
93
|
+
return Function.toString.call(fn).indexOf("[native code]") !== -1;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function _wrapNativeSuper(Class) {
|
|
97
|
+
var _cache = typeof Map === "function" ? new Map() : undefined;
|
|
98
|
+
|
|
99
|
+
_wrapNativeSuper = function _wrapNativeSuper(Class) {
|
|
100
|
+
if (Class === null || !_isNativeFunction(Class)) return Class;
|
|
101
|
+
|
|
102
|
+
if (typeof Class !== "function") {
|
|
103
|
+
throw new TypeError("Super expression must either be null or a function");
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (typeof _cache !== "undefined") {
|
|
107
|
+
if (_cache.has(Class)) return _cache.get(Class);
|
|
108
|
+
|
|
109
|
+
_cache.set(Class, Wrapper);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function Wrapper() {
|
|
113
|
+
return _construct(Class, arguments, _getPrototypeOf(this).constructor);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
Wrapper.prototype = Object.create(Class.prototype, {
|
|
117
|
+
constructor: {
|
|
118
|
+
value: Wrapper,
|
|
119
|
+
enumerable: false,
|
|
120
|
+
writable: true,
|
|
121
|
+
configurable: true
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
return _setPrototypeOf(Wrapper, Class);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
return _wrapNativeSuper(Class);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function _assertThisInitialized(self) {
|
|
131
|
+
if (self === void 0) {
|
|
132
|
+
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return self;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function _unsupportedIterableToArray(o, minLen) {
|
|
139
|
+
if (!o) return;
|
|
140
|
+
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
141
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
142
|
+
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
143
|
+
if (n === "Map" || n === "Set") return Array.from(o);
|
|
144
|
+
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function _arrayLikeToArray(arr, len) {
|
|
148
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
149
|
+
|
|
150
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
151
|
+
|
|
152
|
+
return arr2;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
156
|
+
var it;
|
|
157
|
+
|
|
158
|
+
if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
|
|
159
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
160
|
+
if (it) o = it;
|
|
161
|
+
var i = 0;
|
|
162
|
+
return function () {
|
|
163
|
+
if (i >= o.length) return {
|
|
164
|
+
done: true
|
|
165
|
+
};
|
|
166
|
+
return {
|
|
167
|
+
done: false,
|
|
168
|
+
value: o[i++]
|
|
169
|
+
};
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
it = o[Symbol.iterator]();
|
|
177
|
+
return it.next.bind(it);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function _classPrivateFieldGet(receiver, privateMap) {
|
|
181
|
+
var descriptor = privateMap.get(receiver);
|
|
182
|
+
|
|
183
|
+
if (!descriptor) {
|
|
184
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (descriptor.get) {
|
|
188
|
+
return descriptor.get.call(receiver);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return descriptor.value;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function _classPrivateFieldSet(receiver, privateMap, value) {
|
|
195
|
+
var descriptor = privateMap.get(receiver);
|
|
196
|
+
|
|
197
|
+
if (!descriptor) {
|
|
198
|
+
throw new TypeError("attempted to set private field on non-instance");
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (descriptor.set) {
|
|
202
|
+
descriptor.set.call(receiver, value);
|
|
203
|
+
} else {
|
|
204
|
+
if (!descriptor.writable) {
|
|
205
|
+
throw new TypeError("attempted to set read only private field");
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
descriptor.value = value;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return value;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function _classPrivateMethodGet(receiver, privateSet, fn) {
|
|
215
|
+
if (!privateSet.has(receiver)) {
|
|
216
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return fn;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
var ServerMessageType;
|
|
223
|
+
|
|
224
|
+
(function (ServerMessageType) {
|
|
225
|
+
ServerMessageType[ServerMessageType["UpdatePresence"] = 100] = "UpdatePresence";
|
|
226
|
+
ServerMessageType[ServerMessageType["UserJoined"] = 101] = "UserJoined";
|
|
227
|
+
ServerMessageType[ServerMessageType["UserLeft"] = 102] = "UserLeft";
|
|
228
|
+
ServerMessageType[ServerMessageType["Event"] = 103] = "Event";
|
|
229
|
+
ServerMessageType[ServerMessageType["RoomState"] = 104] = "RoomState";
|
|
230
|
+
ServerMessageType[ServerMessageType["InitialStorageState"] = 200] = "InitialStorageState";
|
|
231
|
+
ServerMessageType[ServerMessageType["UpdateStorage"] = 201] = "UpdateStorage";
|
|
232
|
+
})(ServerMessageType || (ServerMessageType = {}));
|
|
233
|
+
|
|
234
|
+
var ClientMessageType;
|
|
235
|
+
|
|
236
|
+
(function (ClientMessageType) {
|
|
237
|
+
ClientMessageType[ClientMessageType["UpdatePresence"] = 100] = "UpdatePresence";
|
|
238
|
+
ClientMessageType[ClientMessageType["ClientEvent"] = 103] = "ClientEvent";
|
|
239
|
+
ClientMessageType[ClientMessageType["FetchStorage"] = 200] = "FetchStorage";
|
|
240
|
+
ClientMessageType[ClientMessageType["UpdateStorage"] = 201] = "UpdateStorage";
|
|
241
|
+
})(ClientMessageType || (ClientMessageType = {}));
|
|
242
|
+
|
|
243
|
+
var CrdtType;
|
|
244
|
+
|
|
245
|
+
(function (CrdtType) {
|
|
246
|
+
CrdtType[CrdtType["Object"] = 0] = "Object";
|
|
247
|
+
CrdtType[CrdtType["List"] = 1] = "List";
|
|
248
|
+
CrdtType[CrdtType["Map"] = 2] = "Map";
|
|
249
|
+
CrdtType[CrdtType["Register"] = 3] = "Register";
|
|
250
|
+
})(CrdtType || (CrdtType = {}));
|
|
251
|
+
|
|
252
|
+
var OpType;
|
|
253
|
+
|
|
254
|
+
(function (OpType) {
|
|
255
|
+
OpType[OpType["Init"] = 0] = "Init";
|
|
256
|
+
OpType[OpType["SetParentKey"] = 1] = "SetParentKey";
|
|
257
|
+
OpType[OpType["CreateList"] = 2] = "CreateList";
|
|
258
|
+
OpType[OpType["UpdateObject"] = 3] = "UpdateObject";
|
|
259
|
+
OpType[OpType["CreateObject"] = 4] = "CreateObject";
|
|
260
|
+
OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt";
|
|
261
|
+
OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey";
|
|
262
|
+
OpType[OpType["CreateMap"] = 7] = "CreateMap";
|
|
263
|
+
OpType[OpType["CreateRegister"] = 8] = "CreateRegister";
|
|
264
|
+
})(OpType || (OpType = {}));
|
|
265
|
+
|
|
266
|
+
var WebsocketCloseCodes;
|
|
267
|
+
|
|
268
|
+
(function (WebsocketCloseCodes) {
|
|
269
|
+
WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
|
|
270
|
+
WebsocketCloseCodes[WebsocketCloseCodes["INVALID_MESSAGE_FORMAT"] = 4000] = "INVALID_MESSAGE_FORMAT";
|
|
271
|
+
WebsocketCloseCodes[WebsocketCloseCodes["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
|
|
272
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
|
|
273
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
|
|
274
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
|
|
275
|
+
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
|
|
276
|
+
})(WebsocketCloseCodes || (WebsocketCloseCodes = {}));
|
|
277
|
+
|
|
278
|
+
var _parent = new WeakMap();
|
|
279
|
+
|
|
280
|
+
var _doc = new WeakMap();
|
|
281
|
+
|
|
282
|
+
var _id = new WeakMap();
|
|
283
|
+
|
|
284
|
+
var _parentKey = new WeakMap();
|
|
285
|
+
|
|
286
|
+
var AbstractCrdt = function () {
|
|
287
|
+
function AbstractCrdt() {
|
|
288
|
+
_parent.set(this, {
|
|
289
|
+
writable: true,
|
|
290
|
+
value: void 0
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
_doc.set(this, {
|
|
294
|
+
writable: true,
|
|
295
|
+
value: void 0
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
_id.set(this, {
|
|
299
|
+
writable: true,
|
|
300
|
+
value: void 0
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
_parentKey.set(this, {
|
|
304
|
+
writable: true,
|
|
305
|
+
value: void 0
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
var _proto = AbstractCrdt.prototype;
|
|
310
|
+
|
|
311
|
+
_proto._apply = function _apply(op, isLocal) {
|
|
312
|
+
switch (op.type) {
|
|
313
|
+
case OpType.DeleteCrdt:
|
|
314
|
+
{
|
|
315
|
+
if (this._parent != null && this._parentKey != null) {
|
|
316
|
+
return this._parent._detachChild(this);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
modified: false
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return {
|
|
326
|
+
modified: false
|
|
327
|
+
};
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
_proto._setParentLink = function _setParentLink(parent, key) {
|
|
331
|
+
if (_classPrivateFieldGet(this, _parent) != null && _classPrivateFieldGet(this, _parent) !== parent) {
|
|
332
|
+
throw new Error("Cannot attach parent if it already exist");
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
_classPrivateFieldSet(this, _parentKey, key);
|
|
336
|
+
|
|
337
|
+
_classPrivateFieldSet(this, _parent, parent);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
_proto._attach = function _attach(id, doc) {
|
|
341
|
+
if (_classPrivateFieldGet(this, _id) || _classPrivateFieldGet(this, _doc)) {
|
|
342
|
+
throw new Error("Cannot attach if CRDT is already attached");
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
doc.addItem(id, this);
|
|
346
|
+
|
|
347
|
+
_classPrivateFieldSet(this, _id, id);
|
|
348
|
+
|
|
349
|
+
_classPrivateFieldSet(this, _doc, doc);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
_proto._detach = function _detach() {
|
|
353
|
+
if (_classPrivateFieldGet(this, _doc) && _classPrivateFieldGet(this, _id)) {
|
|
354
|
+
_classPrivateFieldGet(this, _doc).deleteItem(_classPrivateFieldGet(this, _id));
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
_classPrivateFieldSet(this, _parent, undefined);
|
|
358
|
+
|
|
359
|
+
_classPrivateFieldSet(this, _doc, undefined);
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
_createClass(AbstractCrdt, [{
|
|
363
|
+
key: "_doc",
|
|
364
|
+
get: function get() {
|
|
365
|
+
return _classPrivateFieldGet(this, _doc);
|
|
366
|
+
}
|
|
367
|
+
}, {
|
|
368
|
+
key: "roomId",
|
|
369
|
+
get: function get() {
|
|
370
|
+
return _classPrivateFieldGet(this, _doc) ? _classPrivateFieldGet(this, _doc).roomId : null;
|
|
371
|
+
}
|
|
372
|
+
}, {
|
|
373
|
+
key: "_id",
|
|
374
|
+
get: function get() {
|
|
375
|
+
return _classPrivateFieldGet(this, _id);
|
|
376
|
+
}
|
|
377
|
+
}, {
|
|
378
|
+
key: "_parent",
|
|
379
|
+
get: function get() {
|
|
380
|
+
return _classPrivateFieldGet(this, _parent);
|
|
381
|
+
}
|
|
382
|
+
}, {
|
|
383
|
+
key: "_parentKey",
|
|
384
|
+
get: function get() {
|
|
385
|
+
return _classPrivateFieldGet(this, _parentKey);
|
|
386
|
+
}
|
|
387
|
+
}]);
|
|
388
|
+
|
|
389
|
+
return AbstractCrdt;
|
|
390
|
+
}();
|
|
391
|
+
|
|
392
|
+
var min = 32;
|
|
393
|
+
var max = 126;
|
|
394
|
+
function makePosition(before, after) {
|
|
395
|
+
if (before == null && after == null) {
|
|
396
|
+
return pos([min + 1]);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
if (before != null && after == null) {
|
|
400
|
+
return getNextPosition(before);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (before == null && after != null) {
|
|
404
|
+
return getPreviousPosition(after);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function getPreviousPosition(after) {
|
|
411
|
+
var result = [];
|
|
412
|
+
var afterCodes = posCodes(after);
|
|
413
|
+
|
|
414
|
+
for (var i = 0; i < afterCodes.length; i++) {
|
|
415
|
+
var code = afterCodes[i];
|
|
416
|
+
|
|
417
|
+
if (code <= min + 1) {
|
|
418
|
+
result.push(min);
|
|
419
|
+
|
|
420
|
+
if (afterCodes.length - 1 === i) {
|
|
421
|
+
result.push(max);
|
|
422
|
+
break;
|
|
423
|
+
}
|
|
424
|
+
} else {
|
|
425
|
+
result.push(code - 1);
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return pos(result);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function getNextPosition(before) {
|
|
434
|
+
var result = [];
|
|
435
|
+
var beforeCodes = posCodes(before);
|
|
436
|
+
|
|
437
|
+
for (var i = 0; i < beforeCodes.length; i++) {
|
|
438
|
+
var code = beforeCodes[i];
|
|
439
|
+
|
|
440
|
+
if (code === max) {
|
|
441
|
+
result.push(code);
|
|
442
|
+
|
|
443
|
+
if (beforeCodes.length - 1 === i) {
|
|
444
|
+
result.push(min + 1);
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
} else {
|
|
448
|
+
result.push(code + 1);
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
return pos(result);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
function makePositionFromCodes(before, after) {
|
|
457
|
+
var index = 0;
|
|
458
|
+
var result = [];
|
|
459
|
+
|
|
460
|
+
while (true) {
|
|
461
|
+
var beforeDigit = before[index] || min;
|
|
462
|
+
var afterDigit = after[index] || max;
|
|
463
|
+
|
|
464
|
+
if (beforeDigit > afterDigit) {
|
|
465
|
+
throw new Error("Impossible to generate position between " + before + " and " + after);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
if (beforeDigit === afterDigit) {
|
|
469
|
+
result.push(beforeDigit);
|
|
470
|
+
index++;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
if (afterDigit - beforeDigit === 1) {
|
|
475
|
+
result.push(beforeDigit);
|
|
476
|
+
result.push.apply(result, makePositionFromCodes(before.slice(index + 1), []));
|
|
477
|
+
break;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
var mid = afterDigit + beforeDigit >> 1;
|
|
481
|
+
result.push(mid);
|
|
482
|
+
break;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
return result;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function posCodes(str) {
|
|
489
|
+
var codes = [];
|
|
490
|
+
|
|
491
|
+
for (var i = 0; i < str.length; i++) {
|
|
492
|
+
codes.push(str.charCodeAt(i));
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
return codes;
|
|
496
|
+
}
|
|
497
|
+
function pos(codes) {
|
|
498
|
+
return String.fromCharCode.apply(String, codes);
|
|
499
|
+
}
|
|
500
|
+
function compare(posA, posB) {
|
|
501
|
+
var aCodes = posCodes(posA);
|
|
502
|
+
var bCodes = posCodes(posB);
|
|
503
|
+
var maxLength = Math.max(aCodes.length, bCodes.length);
|
|
504
|
+
|
|
505
|
+
for (var i = 0; i < maxLength; i++) {
|
|
506
|
+
var a = aCodes[i] == null ? min : aCodes[i];
|
|
507
|
+
var b = bCodes[i] == null ? min : bCodes[i];
|
|
508
|
+
|
|
509
|
+
if (a === b) {
|
|
510
|
+
continue;
|
|
511
|
+
} else {
|
|
512
|
+
return a - b;
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
throw new Error("Impossible to compare similar position \"" + posA + "\" and \"" + posB + "\"");
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
var _data = new WeakMap();
|
|
520
|
+
|
|
521
|
+
var LiveRegister = function (_AbstractCrdt) {
|
|
522
|
+
_inheritsLoose(LiveRegister, _AbstractCrdt);
|
|
523
|
+
|
|
524
|
+
function LiveRegister(data) {
|
|
525
|
+
var _this;
|
|
526
|
+
|
|
527
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
528
|
+
|
|
529
|
+
_data.set(_assertThisInitialized(_this), {
|
|
530
|
+
writable: true,
|
|
531
|
+
value: void 0
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _data, data);
|
|
535
|
+
|
|
536
|
+
return _this;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
LiveRegister._deserialize = function _deserialize(_ref, parentToChildren, doc) {
|
|
540
|
+
var id = _ref[0],
|
|
541
|
+
item = _ref[1];
|
|
542
|
+
|
|
543
|
+
if (item.type !== CrdtType.Register) {
|
|
544
|
+
throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
var register = new LiveRegister(item.data);
|
|
548
|
+
|
|
549
|
+
register._attach(id, doc);
|
|
550
|
+
|
|
551
|
+
return register;
|
|
552
|
+
};
|
|
553
|
+
|
|
554
|
+
var _proto = LiveRegister.prototype;
|
|
555
|
+
|
|
556
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc) {
|
|
557
|
+
if (this._id == null || parentId == null || parentKey == null) {
|
|
558
|
+
throw new Error("Cannot serialize register if parentId or parentKey is undefined");
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
return [{
|
|
562
|
+
type: OpType.CreateRegister,
|
|
563
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
564
|
+
id: this._id,
|
|
565
|
+
parentId: parentId,
|
|
566
|
+
parentKey: parentKey,
|
|
567
|
+
data: this.data
|
|
568
|
+
}];
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
572
|
+
var _this$_parent;
|
|
573
|
+
|
|
574
|
+
return {
|
|
575
|
+
type: CrdtType.Register,
|
|
576
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
577
|
+
parentKey: this._parentKey,
|
|
578
|
+
data: this.data
|
|
579
|
+
};
|
|
580
|
+
};
|
|
581
|
+
|
|
582
|
+
_proto._attachChild = function _attachChild(id, key, crdt, opId, isLocal) {
|
|
583
|
+
throw new Error("Method not implemented.");
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
_proto._detachChild = function _detachChild(crdt) {
|
|
587
|
+
throw new Error("Method not implemented.");
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
_proto._apply = function _apply(op, isLocal) {
|
|
591
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
592
|
+
};
|
|
593
|
+
|
|
594
|
+
_createClass(LiveRegister, [{
|
|
595
|
+
key: "data",
|
|
596
|
+
get: function get() {
|
|
597
|
+
return _classPrivateFieldGet(this, _data);
|
|
598
|
+
}
|
|
599
|
+
}]);
|
|
600
|
+
|
|
601
|
+
return LiveRegister;
|
|
602
|
+
}(AbstractCrdt);
|
|
603
|
+
|
|
604
|
+
var _Symbol$iterator$1, _Symbol$iterator2;
|
|
605
|
+
|
|
606
|
+
var _items = new WeakMap();
|
|
607
|
+
|
|
608
|
+
_Symbol$iterator$1 = Symbol.iterator;
|
|
609
|
+
var LiveList = function (_AbstractCrdt) {
|
|
610
|
+
_inheritsLoose(LiveList, _AbstractCrdt);
|
|
611
|
+
|
|
612
|
+
function LiveList(items) {
|
|
613
|
+
var _this;
|
|
614
|
+
|
|
615
|
+
if (items === void 0) {
|
|
616
|
+
items = [];
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
620
|
+
|
|
621
|
+
_items.set(_assertThisInitialized(_this), {
|
|
622
|
+
writable: true,
|
|
623
|
+
value: []
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
var position = undefined;
|
|
627
|
+
|
|
628
|
+
for (var i = 0; i < items.length; i++) {
|
|
629
|
+
var newPosition = makePosition(position);
|
|
630
|
+
|
|
631
|
+
var _item = selfOrRegister(items[i]);
|
|
632
|
+
|
|
633
|
+
_classPrivateFieldGet(_assertThisInitialized(_this), _items).push([_item, newPosition]);
|
|
634
|
+
|
|
635
|
+
position = newPosition;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
return _this;
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
LiveList._deserialize = function _deserialize(_ref, parentToChildren, doc) {
|
|
642
|
+
var id = _ref[0];
|
|
643
|
+
_ref[1];
|
|
644
|
+
var list = new LiveList([]);
|
|
645
|
+
|
|
646
|
+
list._attach(id, doc);
|
|
647
|
+
|
|
648
|
+
var children = parentToChildren.get(id);
|
|
649
|
+
|
|
650
|
+
if (children == null) {
|
|
651
|
+
return list;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
for (var _iterator = _createForOfIteratorHelperLoose(children), _step; !(_step = _iterator()).done;) {
|
|
655
|
+
var entry = _step.value;
|
|
656
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
657
|
+
|
|
658
|
+
child._setParentLink(list, entry[1].parentKey);
|
|
659
|
+
|
|
660
|
+
_classPrivateFieldGet(list, _items).push([child, entry[1].parentKey]);
|
|
661
|
+
|
|
662
|
+
_classPrivateFieldGet(list, _items).sort(function (itemA, itemB) {
|
|
663
|
+
return compare(itemA[1], itemB[1]);
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
return list;
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
var _proto = LiveList.prototype;
|
|
671
|
+
|
|
672
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc) {
|
|
673
|
+
if (this._id == null) {
|
|
674
|
+
throw new Error("Cannot serialize item is not attached");
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
if (parentId == null || parentKey == null) {
|
|
678
|
+
throw new Error("Cannot serialize list if parentId or parentKey is undefined");
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
var ops = [];
|
|
682
|
+
var op = {
|
|
683
|
+
id: this._id,
|
|
684
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
685
|
+
type: OpType.CreateList,
|
|
686
|
+
parentId: parentId,
|
|
687
|
+
parentKey: parentKey
|
|
688
|
+
};
|
|
689
|
+
ops.push(op);
|
|
690
|
+
|
|
691
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _items)), _step2; !(_step2 = _iterator2()).done;) {
|
|
692
|
+
var _step2$value = _step2.value,
|
|
693
|
+
_value = _step2$value[0],
|
|
694
|
+
key = _step2$value[1];
|
|
695
|
+
ops.push.apply(ops, _value._serialize(this._id, key, doc));
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return ops;
|
|
699
|
+
};
|
|
700
|
+
|
|
701
|
+
_proto._indexOfPosition = function _indexOfPosition(position) {
|
|
702
|
+
return _classPrivateFieldGet(this, _items).findIndex(function (item) {
|
|
703
|
+
return item[1] === position;
|
|
704
|
+
});
|
|
705
|
+
};
|
|
706
|
+
|
|
707
|
+
_proto._attach = function _attach(id, doc) {
|
|
708
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
709
|
+
|
|
710
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _items)), _step3; !(_step3 = _iterator3()).done;) {
|
|
711
|
+
var _step3$value = _step3.value,
|
|
712
|
+
_item2 = _step3$value[0];
|
|
713
|
+
_step3$value[1];
|
|
714
|
+
|
|
715
|
+
_item2._attach(doc.generateId(), doc);
|
|
716
|
+
}
|
|
717
|
+
};
|
|
718
|
+
|
|
719
|
+
_proto._detach = function _detach() {
|
|
720
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
721
|
+
|
|
722
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _items)), _step4; !(_step4 = _iterator4()).done;) {
|
|
723
|
+
var _step4$value = _step4.value,
|
|
724
|
+
_value2 = _step4$value[0];
|
|
725
|
+
|
|
726
|
+
_value2._detach();
|
|
727
|
+
}
|
|
728
|
+
};
|
|
729
|
+
|
|
730
|
+
_proto._attachChild = function _attachChild(id, key, child, opId, isLocal) {
|
|
731
|
+
if (this._doc == null) {
|
|
732
|
+
throw new Error("Can't attach child if doc is not present");
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
if (this._doc.getItem(id) !== undefined) {
|
|
736
|
+
return {
|
|
737
|
+
modified: false
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
child._attach(id, this._doc);
|
|
742
|
+
|
|
743
|
+
child._setParentLink(this, key);
|
|
744
|
+
|
|
745
|
+
var index = _classPrivateFieldGet(this, _items).findIndex(function (entry) {
|
|
746
|
+
return entry[1] === key;
|
|
747
|
+
});
|
|
748
|
+
|
|
749
|
+
var newKey = key;
|
|
750
|
+
|
|
751
|
+
if (index !== -1) {
|
|
752
|
+
if (isLocal) {
|
|
753
|
+
var before = _classPrivateFieldGet(this, _items)[index] ? _classPrivateFieldGet(this, _items)[index][1] : undefined;
|
|
754
|
+
var after = _classPrivateFieldGet(this, _items)[index + 1] ? _classPrivateFieldGet(this, _items)[index + 1][1] : undefined;
|
|
755
|
+
newKey = makePosition(before, after);
|
|
756
|
+
|
|
757
|
+
child._setParentLink(this, newKey);
|
|
758
|
+
} else {
|
|
759
|
+
var _classPrivateFieldGet2;
|
|
760
|
+
|
|
761
|
+
_classPrivateFieldGet(this, _items)[index][1] = makePosition(key, (_classPrivateFieldGet2 = _classPrivateFieldGet(this, _items)[index + 1]) == null ? void 0 : _classPrivateFieldGet2[1]);
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
_classPrivateFieldGet(this, _items).push([child, newKey]);
|
|
766
|
+
|
|
767
|
+
_classPrivateFieldGet(this, _items).sort(function (itemA, itemB) {
|
|
768
|
+
return compare(itemA[1], itemB[1]);
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
var newIndex = _classPrivateFieldGet(this, _items).findIndex(function (entry) {
|
|
772
|
+
return entry[1] === newKey;
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
return {
|
|
776
|
+
reverse: [{
|
|
777
|
+
type: OpType.DeleteCrdt,
|
|
778
|
+
id: id
|
|
779
|
+
}],
|
|
780
|
+
modified: {
|
|
781
|
+
node: this,
|
|
782
|
+
type: "LiveList",
|
|
783
|
+
updates: [{
|
|
784
|
+
index: newIndex,
|
|
785
|
+
type: "insert",
|
|
786
|
+
item: child instanceof LiveRegister ? child.data : child
|
|
787
|
+
}]
|
|
788
|
+
}
|
|
789
|
+
};
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
_proto._detachChild = function _detachChild(child) {
|
|
793
|
+
if (child) {
|
|
794
|
+
var reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
795
|
+
|
|
796
|
+
var indexToDelete = _classPrivateFieldGet(this, _items).findIndex(function (item) {
|
|
797
|
+
return item[0] === child;
|
|
798
|
+
});
|
|
799
|
+
|
|
800
|
+
_classPrivateFieldGet(this, _items).splice(indexToDelete, 1);
|
|
801
|
+
|
|
802
|
+
child._detach();
|
|
803
|
+
|
|
804
|
+
var storageUpdate = {
|
|
805
|
+
node: this,
|
|
806
|
+
type: "LiveList",
|
|
807
|
+
updates: [{
|
|
808
|
+
index: indexToDelete,
|
|
809
|
+
type: "delete"
|
|
810
|
+
}]
|
|
811
|
+
};
|
|
812
|
+
return {
|
|
813
|
+
modified: storageUpdate,
|
|
814
|
+
reverse: reverse
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
return {
|
|
819
|
+
modified: false
|
|
820
|
+
};
|
|
821
|
+
};
|
|
822
|
+
|
|
823
|
+
_proto._setChildKey = function _setChildKey(key, child, previousKey) {
|
|
824
|
+
child._setParentLink(this, key);
|
|
825
|
+
|
|
826
|
+
var previousIndex = _classPrivateFieldGet(this, _items).findIndex(function (entry) {
|
|
827
|
+
return entry[0]._id === child._id;
|
|
828
|
+
});
|
|
829
|
+
|
|
830
|
+
var index = _classPrivateFieldGet(this, _items).findIndex(function (entry) {
|
|
831
|
+
return entry[1] === key;
|
|
832
|
+
});
|
|
833
|
+
|
|
834
|
+
if (index !== -1) {
|
|
835
|
+
var _classPrivateFieldGet3;
|
|
836
|
+
|
|
837
|
+
_classPrivateFieldGet(this, _items)[index][1] = makePosition(key, (_classPrivateFieldGet3 = _classPrivateFieldGet(this, _items)[index + 1]) == null ? void 0 : _classPrivateFieldGet3[1]);
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
var item = _classPrivateFieldGet(this, _items).find(function (item) {
|
|
841
|
+
return item[0] === child;
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
if (item) {
|
|
845
|
+
item[1] = key;
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
_classPrivateFieldGet(this, _items).sort(function (itemA, itemB) {
|
|
849
|
+
return compare(itemA[1], itemB[1]);
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
var newIndex = _classPrivateFieldGet(this, _items).findIndex(function (entry) {
|
|
853
|
+
return entry[0]._id === child._id;
|
|
854
|
+
});
|
|
855
|
+
|
|
856
|
+
var updatesDelta = newIndex === previousIndex ? [] : [{
|
|
857
|
+
index: newIndex,
|
|
858
|
+
item: child instanceof LiveRegister ? child.data : child,
|
|
859
|
+
previousIndex: previousIndex,
|
|
860
|
+
type: "move"
|
|
861
|
+
}];
|
|
862
|
+
return {
|
|
863
|
+
modified: {
|
|
864
|
+
node: this,
|
|
865
|
+
type: "LiveList",
|
|
866
|
+
updates: updatesDelta
|
|
867
|
+
},
|
|
868
|
+
reverse: [{
|
|
869
|
+
type: OpType.SetParentKey,
|
|
870
|
+
id: item == null ? void 0 : item[0]._id,
|
|
871
|
+
parentKey: previousKey
|
|
872
|
+
}]
|
|
873
|
+
};
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
_proto._apply = function _apply(op, isLocal) {
|
|
877
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
881
|
+
var _this$_parent;
|
|
882
|
+
|
|
883
|
+
return {
|
|
884
|
+
type: CrdtType.List,
|
|
885
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
886
|
+
parentKey: this._parentKey
|
|
887
|
+
};
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
_proto.push = function push(element) {
|
|
891
|
+
return this.insert(element, this.length);
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
_proto.insert = function insert(element, index) {
|
|
895
|
+
if (index < 0 || index > _classPrivateFieldGet(this, _items).length) {
|
|
896
|
+
throw new Error("Cannot insert list item at index \"\x1D" + index + "\". index should be between 0 and " + _classPrivateFieldGet(this, _items).length);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
var before = _classPrivateFieldGet(this, _items)[index - 1] ? _classPrivateFieldGet(this, _items)[index - 1][1] : undefined;
|
|
900
|
+
var after = _classPrivateFieldGet(this, _items)[index] ? _classPrivateFieldGet(this, _items)[index][1] : undefined;
|
|
901
|
+
var position = makePosition(before, after);
|
|
902
|
+
var value = selfOrRegister(element);
|
|
903
|
+
|
|
904
|
+
value._setParentLink(this, position);
|
|
905
|
+
|
|
906
|
+
_classPrivateFieldGet(this, _items).push([value, position]);
|
|
907
|
+
|
|
908
|
+
_classPrivateFieldGet(this, _items).sort(function (itemA, itemB) {
|
|
909
|
+
return compare(itemA[1], itemB[1]);
|
|
910
|
+
});
|
|
911
|
+
|
|
912
|
+
var newIndex = _classPrivateFieldGet(this, _items).findIndex(function (entry) {
|
|
913
|
+
return entry[1] === position;
|
|
914
|
+
});
|
|
915
|
+
|
|
916
|
+
if (this._doc && this._id) {
|
|
917
|
+
var _id = this._doc.generateId();
|
|
918
|
+
|
|
919
|
+
value._attach(_id, this._doc);
|
|
920
|
+
|
|
921
|
+
var storageUpdates = new Map();
|
|
922
|
+
storageUpdates.set(this._id, {
|
|
923
|
+
node: this,
|
|
924
|
+
type: "LiveList",
|
|
925
|
+
updates: [{
|
|
926
|
+
index: newIndex,
|
|
927
|
+
item: value instanceof LiveRegister ? value.data : value,
|
|
928
|
+
type: "insert"
|
|
929
|
+
}]
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
this._doc.dispatch(value._serialize(this._id, position, this._doc), [{
|
|
933
|
+
type: OpType.DeleteCrdt,
|
|
934
|
+
id: _id
|
|
935
|
+
}], storageUpdates);
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
_proto.move = function move(index, targetIndex) {
|
|
940
|
+
if (targetIndex < 0) {
|
|
941
|
+
throw new Error("targetIndex cannot be less than 0");
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
if (targetIndex >= _classPrivateFieldGet(this, _items).length) {
|
|
945
|
+
throw new Error("targetIndex cannot be greater or equal than the list length");
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
if (index < 0) {
|
|
949
|
+
throw new Error("index cannot be less than 0");
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
if (index >= _classPrivateFieldGet(this, _items).length) {
|
|
953
|
+
throw new Error("index cannot be greater or equal than the list length");
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
var beforePosition = null;
|
|
957
|
+
var afterPosition = null;
|
|
958
|
+
|
|
959
|
+
if (index < targetIndex) {
|
|
960
|
+
afterPosition = targetIndex === _classPrivateFieldGet(this, _items).length - 1 ? undefined : _classPrivateFieldGet(this, _items)[targetIndex + 1][1];
|
|
961
|
+
beforePosition = _classPrivateFieldGet(this, _items)[targetIndex][1];
|
|
962
|
+
} else {
|
|
963
|
+
afterPosition = _classPrivateFieldGet(this, _items)[targetIndex][1];
|
|
964
|
+
beforePosition = targetIndex === 0 ? undefined : _classPrivateFieldGet(this, _items)[targetIndex - 1][1];
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
var position = makePosition(beforePosition, afterPosition);
|
|
968
|
+
|
|
969
|
+
var item = _classPrivateFieldGet(this, _items)[index];
|
|
970
|
+
|
|
971
|
+
var previousPosition = item[1];
|
|
972
|
+
item[1] = position;
|
|
973
|
+
|
|
974
|
+
item[0]._setParentLink(this, position);
|
|
975
|
+
|
|
976
|
+
_classPrivateFieldGet(this, _items).sort(function (itemA, itemB) {
|
|
977
|
+
return compare(itemA[1], itemB[1]);
|
|
978
|
+
});
|
|
979
|
+
|
|
980
|
+
var newIndex = _classPrivateFieldGet(this, _items).findIndex(function (entry) {
|
|
981
|
+
return entry[1] === position;
|
|
982
|
+
});
|
|
983
|
+
|
|
984
|
+
if (this._doc && this._id) {
|
|
985
|
+
var storageUpdates = new Map();
|
|
986
|
+
storageUpdates.set(this._id, {
|
|
987
|
+
node: this,
|
|
988
|
+
type: "LiveList",
|
|
989
|
+
updates: [{
|
|
990
|
+
index: newIndex,
|
|
991
|
+
previousIndex: index,
|
|
992
|
+
item: item[0],
|
|
993
|
+
type: "move"
|
|
994
|
+
}]
|
|
995
|
+
});
|
|
996
|
+
|
|
997
|
+
this._doc.dispatch([{
|
|
998
|
+
type: OpType.SetParentKey,
|
|
999
|
+
id: item[0]._id,
|
|
1000
|
+
opId: this._doc.generateOpId(),
|
|
1001
|
+
parentKey: position
|
|
1002
|
+
}], [{
|
|
1003
|
+
type: OpType.SetParentKey,
|
|
1004
|
+
id: item[0]._id,
|
|
1005
|
+
parentKey: previousPosition
|
|
1006
|
+
}], storageUpdates);
|
|
1007
|
+
}
|
|
1008
|
+
};
|
|
1009
|
+
|
|
1010
|
+
_proto.delete = function _delete(index) {
|
|
1011
|
+
if (index < 0 || index >= _classPrivateFieldGet(this, _items).length) {
|
|
1012
|
+
throw new Error("Cannot delete list item at index \"\x1D" + index + "\". index should be between 0 and " + (_classPrivateFieldGet(this, _items).length - 1));
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
var item = _classPrivateFieldGet(this, _items)[index];
|
|
1016
|
+
|
|
1017
|
+
item[0]._detach();
|
|
1018
|
+
|
|
1019
|
+
_classPrivateFieldGet(this, _items).splice(index, 1);
|
|
1020
|
+
|
|
1021
|
+
if (this._doc) {
|
|
1022
|
+
var childRecordId = item[0]._id;
|
|
1023
|
+
|
|
1024
|
+
if (childRecordId) {
|
|
1025
|
+
var storageUpdates = new Map();
|
|
1026
|
+
storageUpdates.set(this._id, {
|
|
1027
|
+
node: this,
|
|
1028
|
+
type: "LiveList",
|
|
1029
|
+
updates: [{
|
|
1030
|
+
index: index,
|
|
1031
|
+
type: "delete"
|
|
1032
|
+
}]
|
|
1033
|
+
});
|
|
1034
|
+
|
|
1035
|
+
this._doc.dispatch([{
|
|
1036
|
+
id: childRecordId,
|
|
1037
|
+
opId: this._doc.generateOpId(),
|
|
1038
|
+
type: OpType.DeleteCrdt
|
|
1039
|
+
}], item[0]._serialize(this._id, item[1]), storageUpdates);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
};
|
|
1043
|
+
|
|
1044
|
+
_proto.clear = function clear() {
|
|
1045
|
+
if (this._doc) {
|
|
1046
|
+
var ops = [];
|
|
1047
|
+
var reverseOps = [];
|
|
1048
|
+
var updateDelta = [];
|
|
1049
|
+
var i = 0;
|
|
1050
|
+
|
|
1051
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _items)), _step5; !(_step5 = _iterator5()).done;) {
|
|
1052
|
+
var _item3 = _step5.value;
|
|
1053
|
+
|
|
1054
|
+
_item3[0]._detach();
|
|
1055
|
+
|
|
1056
|
+
var childId = _item3[0]._id;
|
|
1057
|
+
|
|
1058
|
+
if (childId) {
|
|
1059
|
+
ops.push({
|
|
1060
|
+
id: childId,
|
|
1061
|
+
type: OpType.DeleteCrdt
|
|
1062
|
+
});
|
|
1063
|
+
reverseOps.push.apply(reverseOps, _item3[0]._serialize(this._id, _item3[1]));
|
|
1064
|
+
updateDelta.push({
|
|
1065
|
+
index: i,
|
|
1066
|
+
type: "delete"
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
i++;
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
_classPrivateFieldSet(this, _items, []);
|
|
1074
|
+
|
|
1075
|
+
var storageUpdates = new Map();
|
|
1076
|
+
storageUpdates.set(this._id, {
|
|
1077
|
+
node: this,
|
|
1078
|
+
type: "LiveList",
|
|
1079
|
+
updates: updateDelta
|
|
1080
|
+
});
|
|
1081
|
+
|
|
1082
|
+
this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
1083
|
+
} else {
|
|
1084
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _items)), _step6; !(_step6 = _iterator6()).done;) {
|
|
1085
|
+
var _item4 = _step6.value;
|
|
1086
|
+
|
|
1087
|
+
_item4[0]._detach();
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
_classPrivateFieldSet(this, _items, []);
|
|
1091
|
+
}
|
|
1092
|
+
};
|
|
1093
|
+
|
|
1094
|
+
_proto.toArray = function toArray() {
|
|
1095
|
+
return _classPrivateFieldGet(this, _items).map(function (entry) {
|
|
1096
|
+
return selfOrRegisterValue(entry[0]);
|
|
1097
|
+
});
|
|
1098
|
+
};
|
|
1099
|
+
|
|
1100
|
+
_proto.every = function every(predicate) {
|
|
1101
|
+
return this.toArray().every(predicate);
|
|
1102
|
+
};
|
|
1103
|
+
|
|
1104
|
+
_proto.filter = function filter(predicate) {
|
|
1105
|
+
return this.toArray().filter(predicate);
|
|
1106
|
+
};
|
|
1107
|
+
|
|
1108
|
+
_proto.find = function find(predicate) {
|
|
1109
|
+
return this.toArray().find(predicate);
|
|
1110
|
+
};
|
|
1111
|
+
|
|
1112
|
+
_proto.findIndex = function findIndex(predicate) {
|
|
1113
|
+
return this.toArray().findIndex(predicate);
|
|
1114
|
+
};
|
|
1115
|
+
|
|
1116
|
+
_proto.forEach = function forEach(callbackfn) {
|
|
1117
|
+
return this.toArray().forEach(callbackfn);
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1120
|
+
_proto.get = function get(index) {
|
|
1121
|
+
if (index < 0 || index >= _classPrivateFieldGet(this, _items).length) {
|
|
1122
|
+
return undefined;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
return selfOrRegisterValue(_classPrivateFieldGet(this, _items)[index][0]);
|
|
1126
|
+
};
|
|
1127
|
+
|
|
1128
|
+
_proto.indexOf = function indexOf(searchElement, fromIndex) {
|
|
1129
|
+
return this.toArray().indexOf(searchElement, fromIndex);
|
|
1130
|
+
};
|
|
1131
|
+
|
|
1132
|
+
_proto.lastIndexOf = function lastIndexOf(searchElement, fromIndex) {
|
|
1133
|
+
return this.toArray().lastIndexOf(searchElement, fromIndex);
|
|
1134
|
+
};
|
|
1135
|
+
|
|
1136
|
+
_proto.map = function map(callback) {
|
|
1137
|
+
return _classPrivateFieldGet(this, _items).map(function (entry, i) {
|
|
1138
|
+
return callback(selfOrRegisterValue(entry[0]), i);
|
|
1139
|
+
});
|
|
1140
|
+
};
|
|
1141
|
+
|
|
1142
|
+
_proto.some = function some(predicate) {
|
|
1143
|
+
return this.toArray().some(predicate);
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
_proto[_Symbol$iterator$1] = function () {
|
|
1147
|
+
return new LiveListIterator(_classPrivateFieldGet(this, _items));
|
|
1148
|
+
};
|
|
1149
|
+
|
|
1150
|
+
_createClass(LiveList, [{
|
|
1151
|
+
key: "length",
|
|
1152
|
+
get: function get() {
|
|
1153
|
+
return _classPrivateFieldGet(this, _items).length;
|
|
1154
|
+
}
|
|
1155
|
+
}]);
|
|
1156
|
+
|
|
1157
|
+
return LiveList;
|
|
1158
|
+
}(AbstractCrdt);
|
|
1159
|
+
|
|
1160
|
+
var _innerIterator = new WeakMap();
|
|
1161
|
+
|
|
1162
|
+
_Symbol$iterator2 = Symbol.iterator;
|
|
1163
|
+
|
|
1164
|
+
var LiveListIterator = function () {
|
|
1165
|
+
function LiveListIterator(items) {
|
|
1166
|
+
_innerIterator.set(this, {
|
|
1167
|
+
writable: true,
|
|
1168
|
+
value: void 0
|
|
1169
|
+
});
|
|
1170
|
+
|
|
1171
|
+
_classPrivateFieldSet(this, _innerIterator, items[Symbol.iterator]());
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
var _proto2 = LiveListIterator.prototype;
|
|
1175
|
+
|
|
1176
|
+
_proto2[_Symbol$iterator2] = function () {
|
|
1177
|
+
return this;
|
|
1178
|
+
};
|
|
1179
|
+
|
|
1180
|
+
_proto2.next = function next() {
|
|
1181
|
+
var result = _classPrivateFieldGet(this, _innerIterator).next();
|
|
1182
|
+
|
|
1183
|
+
if (result.done) {
|
|
1184
|
+
return {
|
|
1185
|
+
done: true,
|
|
1186
|
+
value: undefined
|
|
1187
|
+
};
|
|
1188
|
+
}
|
|
1189
|
+
|
|
1190
|
+
return {
|
|
1191
|
+
value: selfOrRegisterValue(result.value[0])
|
|
1192
|
+
};
|
|
1193
|
+
};
|
|
1194
|
+
|
|
1195
|
+
return LiveListIterator;
|
|
1196
|
+
}();
|
|
1197
|
+
|
|
1198
|
+
var _Symbol$iterator;
|
|
1199
|
+
|
|
1200
|
+
var _map$1 = new WeakMap();
|
|
1201
|
+
|
|
1202
|
+
_Symbol$iterator = Symbol.iterator;
|
|
1203
|
+
var LiveMap = function (_AbstractCrdt) {
|
|
1204
|
+
_inheritsLoose(LiveMap, _AbstractCrdt);
|
|
1205
|
+
|
|
1206
|
+
function LiveMap(entries) {
|
|
1207
|
+
var _this;
|
|
1208
|
+
|
|
1209
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
1210
|
+
|
|
1211
|
+
_map$1.set(_assertThisInitialized(_this), {
|
|
1212
|
+
writable: true,
|
|
1213
|
+
value: void 0
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
if (entries) {
|
|
1217
|
+
var mappedEntries = [];
|
|
1218
|
+
|
|
1219
|
+
for (var _iterator = _createForOfIteratorHelperLoose(entries), _step; !(_step = _iterator()).done;) {
|
|
1220
|
+
var entry = _step.value;
|
|
1221
|
+
|
|
1222
|
+
var _value = selfOrRegister(entry[1]);
|
|
1223
|
+
|
|
1224
|
+
_value._setParentLink(_assertThisInitialized(_this), entry[0]);
|
|
1225
|
+
|
|
1226
|
+
mappedEntries.push([entry[0], _value]);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _map$1, new Map(mappedEntries));
|
|
1230
|
+
} else {
|
|
1231
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _map$1, new Map());
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
return _this;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
var _proto = LiveMap.prototype;
|
|
1238
|
+
|
|
1239
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc) {
|
|
1240
|
+
if (this._id == null) {
|
|
1241
|
+
throw new Error("Cannot serialize item is not attached");
|
|
1242
|
+
}
|
|
1243
|
+
|
|
1244
|
+
if (parentId == null || parentKey == null) {
|
|
1245
|
+
throw new Error("Cannot serialize map if parentId or parentKey is undefined");
|
|
1246
|
+
}
|
|
1247
|
+
|
|
1248
|
+
var ops = [];
|
|
1249
|
+
var op = {
|
|
1250
|
+
id: this._id,
|
|
1251
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
1252
|
+
type: OpType.CreateMap,
|
|
1253
|
+
parentId: parentId,
|
|
1254
|
+
parentKey: parentKey
|
|
1255
|
+
};
|
|
1256
|
+
ops.push(op);
|
|
1257
|
+
|
|
1258
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map$1)), _step2; !(_step2 = _iterator2()).done;) {
|
|
1259
|
+
var _step2$value = _step2.value,
|
|
1260
|
+
_key = _step2$value[0],
|
|
1261
|
+
_value2 = _step2$value[1];
|
|
1262
|
+
ops.push.apply(ops, _value2._serialize(this._id, _key, doc));
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
return ops;
|
|
1266
|
+
};
|
|
1267
|
+
|
|
1268
|
+
LiveMap._deserialize = function _deserialize(_ref, parentToChildren, doc) {
|
|
1269
|
+
var id = _ref[0],
|
|
1270
|
+
item = _ref[1];
|
|
1271
|
+
|
|
1272
|
+
if (item.type !== CrdtType.Map) {
|
|
1273
|
+
throw new Error("Tried to deserialize a map but item type is \"" + item.type + "\"");
|
|
1274
|
+
}
|
|
1275
|
+
|
|
1276
|
+
var map = new LiveMap();
|
|
1277
|
+
|
|
1278
|
+
map._attach(id, doc);
|
|
1279
|
+
|
|
1280
|
+
var children = parentToChildren.get(id);
|
|
1281
|
+
|
|
1282
|
+
if (children == null) {
|
|
1283
|
+
return map;
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(children), _step3; !(_step3 = _iterator3()).done;) {
|
|
1287
|
+
var entry = _step3.value;
|
|
1288
|
+
var crdt = entry[1];
|
|
1289
|
+
|
|
1290
|
+
if (crdt.parentKey == null) {
|
|
1291
|
+
throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
|
|
1292
|
+
}
|
|
1293
|
+
|
|
1294
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
1295
|
+
|
|
1296
|
+
child._setParentLink(map, crdt.parentKey);
|
|
1297
|
+
|
|
1298
|
+
_classPrivateFieldGet(map, _map$1).set(crdt.parentKey, child);
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
return map;
|
|
1302
|
+
};
|
|
1303
|
+
|
|
1304
|
+
_proto._attach = function _attach(id, doc) {
|
|
1305
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
1306
|
+
|
|
1307
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map$1)), _step4; !(_step4 = _iterator4()).done;) {
|
|
1308
|
+
var _step4$value = _step4.value;
|
|
1309
|
+
_step4$value[0];
|
|
1310
|
+
var _value3 = _step4$value[1];
|
|
1311
|
+
|
|
1312
|
+
if (isCrdt(_value3)) {
|
|
1313
|
+
_value3._attach(doc.generateId(), doc);
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
};
|
|
1317
|
+
|
|
1318
|
+
_proto._attachChild = function _attachChild(id, key, child, opId, isLocal) {
|
|
1319
|
+
var _updates;
|
|
1320
|
+
|
|
1321
|
+
if (this._doc == null) {
|
|
1322
|
+
throw new Error("Can't attach child if doc is not present");
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
if (this._doc.getItem(id) !== undefined) {
|
|
1326
|
+
return {
|
|
1327
|
+
modified: false
|
|
1328
|
+
};
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
var previousValue = _classPrivateFieldGet(this, _map$1).get(key);
|
|
1332
|
+
|
|
1333
|
+
var reverse;
|
|
1334
|
+
|
|
1335
|
+
if (previousValue) {
|
|
1336
|
+
reverse = previousValue._serialize(this._id, key);
|
|
1337
|
+
|
|
1338
|
+
previousValue._detach();
|
|
1339
|
+
} else {
|
|
1340
|
+
reverse = [{
|
|
1341
|
+
type: OpType.DeleteCrdt,
|
|
1342
|
+
id: id
|
|
1343
|
+
}];
|
|
1344
|
+
}
|
|
1345
|
+
|
|
1346
|
+
child._setParentLink(this, key);
|
|
1347
|
+
|
|
1348
|
+
child._attach(id, this._doc);
|
|
1349
|
+
|
|
1350
|
+
_classPrivateFieldGet(this, _map$1).set(key, child);
|
|
1351
|
+
|
|
1352
|
+
return {
|
|
1353
|
+
modified: {
|
|
1354
|
+
node: this,
|
|
1355
|
+
type: "LiveMap",
|
|
1356
|
+
updates: (_updates = {}, _updates[key] = {
|
|
1357
|
+
type: "update"
|
|
1358
|
+
}, _updates)
|
|
1359
|
+
},
|
|
1360
|
+
reverse: reverse
|
|
1361
|
+
};
|
|
1362
|
+
};
|
|
1363
|
+
|
|
1364
|
+
_proto._detach = function _detach() {
|
|
1365
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
1366
|
+
|
|
1367
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map$1).values()), _step5; !(_step5 = _iterator5()).done;) {
|
|
1368
|
+
var item = _step5.value;
|
|
1369
|
+
|
|
1370
|
+
item._detach();
|
|
1371
|
+
}
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
_proto._detachChild = function _detachChild(child) {
|
|
1375
|
+
var _updates2;
|
|
1376
|
+
|
|
1377
|
+
var reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
1378
|
+
|
|
1379
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map$1)), _step6; !(_step6 = _iterator6()).done;) {
|
|
1380
|
+
var _step6$value = _step6.value,
|
|
1381
|
+
_key3 = _step6$value[0],
|
|
1382
|
+
_value4 = _step6$value[1];
|
|
1383
|
+
|
|
1384
|
+
if (_value4 === child) {
|
|
1385
|
+
_classPrivateFieldGet(this, _map$1).delete(_key3);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
child._detach();
|
|
1390
|
+
|
|
1391
|
+
var storageUpdate = {
|
|
1392
|
+
node: this,
|
|
1393
|
+
type: "LiveMap",
|
|
1394
|
+
updates: (_updates2 = {}, _updates2[child._parentKey] = {
|
|
1395
|
+
type: "delete"
|
|
1396
|
+
}, _updates2)
|
|
1397
|
+
};
|
|
1398
|
+
return {
|
|
1399
|
+
modified: storageUpdate,
|
|
1400
|
+
reverse: reverse
|
|
1401
|
+
};
|
|
1402
|
+
};
|
|
1403
|
+
|
|
1404
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
1405
|
+
var _this$_parent;
|
|
1406
|
+
|
|
1407
|
+
return {
|
|
1408
|
+
type: CrdtType.Map,
|
|
1409
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
1410
|
+
parentKey: this._parentKey
|
|
1411
|
+
};
|
|
1412
|
+
};
|
|
1413
|
+
|
|
1414
|
+
_proto.get = function get(key) {
|
|
1415
|
+
var value = _classPrivateFieldGet(this, _map$1).get(key);
|
|
1416
|
+
|
|
1417
|
+
if (value == undefined) {
|
|
1418
|
+
return undefined;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
return selfOrRegisterValue(value);
|
|
1422
|
+
};
|
|
1423
|
+
|
|
1424
|
+
_proto.set = function set(key, value) {
|
|
1425
|
+
var oldValue = _classPrivateFieldGet(this, _map$1).get(key);
|
|
1426
|
+
|
|
1427
|
+
if (oldValue) {
|
|
1428
|
+
oldValue._detach();
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
var item = selfOrRegister(value);
|
|
1432
|
+
|
|
1433
|
+
item._setParentLink(this, key);
|
|
1434
|
+
|
|
1435
|
+
_classPrivateFieldGet(this, _map$1).set(key, item);
|
|
1436
|
+
|
|
1437
|
+
if (this._doc && this._id) {
|
|
1438
|
+
var _updates3;
|
|
1439
|
+
|
|
1440
|
+
var id = this._doc.generateId();
|
|
1441
|
+
|
|
1442
|
+
item._attach(id, this._doc);
|
|
1443
|
+
|
|
1444
|
+
var storageUpdates = new Map();
|
|
1445
|
+
storageUpdates.set(this._id, {
|
|
1446
|
+
node: this,
|
|
1447
|
+
type: "LiveMap",
|
|
1448
|
+
updates: (_updates3 = {}, _updates3[key] = {
|
|
1449
|
+
type: "update"
|
|
1450
|
+
}, _updates3)
|
|
1451
|
+
});
|
|
1452
|
+
|
|
1453
|
+
this._doc.dispatch(item._serialize(this._id, key, this._doc), oldValue ? oldValue._serialize(this._id, key) : [{
|
|
1454
|
+
type: OpType.DeleteCrdt,
|
|
1455
|
+
id: id
|
|
1456
|
+
}], storageUpdates);
|
|
1457
|
+
}
|
|
1458
|
+
};
|
|
1459
|
+
|
|
1460
|
+
_proto.has = function has(key) {
|
|
1461
|
+
return _classPrivateFieldGet(this, _map$1).has(key);
|
|
1462
|
+
};
|
|
1463
|
+
|
|
1464
|
+
_proto.delete = function _delete(key) {
|
|
1465
|
+
var item = _classPrivateFieldGet(this, _map$1).get(key);
|
|
1466
|
+
|
|
1467
|
+
if (item == null) {
|
|
1468
|
+
return false;
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
item._detach();
|
|
1472
|
+
|
|
1473
|
+
if (this._doc && item._id) {
|
|
1474
|
+
var _updates4;
|
|
1475
|
+
|
|
1476
|
+
var storageUpdates = new Map();
|
|
1477
|
+
storageUpdates.set(this._id, {
|
|
1478
|
+
node: this,
|
|
1479
|
+
type: "LiveMap",
|
|
1480
|
+
updates: (_updates4 = {}, _updates4[key] = {
|
|
1481
|
+
type: "delete"
|
|
1482
|
+
}, _updates4)
|
|
1483
|
+
});
|
|
1484
|
+
|
|
1485
|
+
this._doc.dispatch([{
|
|
1486
|
+
type: OpType.DeleteCrdt,
|
|
1487
|
+
id: item._id,
|
|
1488
|
+
opId: this._doc.generateOpId()
|
|
1489
|
+
}], item._serialize(this._id, key), storageUpdates);
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
_classPrivateFieldGet(this, _map$1).delete(key);
|
|
1493
|
+
|
|
1494
|
+
return true;
|
|
1495
|
+
};
|
|
1496
|
+
|
|
1497
|
+
_proto.entries = function entries() {
|
|
1498
|
+
var _ref2;
|
|
1499
|
+
|
|
1500
|
+
var innerIterator = _classPrivateFieldGet(this, _map$1).entries();
|
|
1501
|
+
|
|
1502
|
+
return _ref2 = {}, _ref2[Symbol.iterator] = function () {
|
|
1503
|
+
return this;
|
|
1504
|
+
}, _ref2.next = function next() {
|
|
1505
|
+
var iteratorValue = innerIterator.next();
|
|
1506
|
+
|
|
1507
|
+
if (iteratorValue.done) {
|
|
1508
|
+
return {
|
|
1509
|
+
done: true,
|
|
1510
|
+
value: undefined
|
|
1511
|
+
};
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
var entry = iteratorValue.value;
|
|
1515
|
+
return {
|
|
1516
|
+
value: [entry[0], selfOrRegisterValue(iteratorValue.value[1])]
|
|
1517
|
+
};
|
|
1518
|
+
}, _ref2;
|
|
1519
|
+
};
|
|
1520
|
+
|
|
1521
|
+
_proto[_Symbol$iterator] = function () {
|
|
1522
|
+
return this.entries();
|
|
1523
|
+
};
|
|
1524
|
+
|
|
1525
|
+
_proto.keys = function keys() {
|
|
1526
|
+
return _classPrivateFieldGet(this, _map$1).keys();
|
|
1527
|
+
};
|
|
1528
|
+
|
|
1529
|
+
_proto.values = function values() {
|
|
1530
|
+
var _ref3;
|
|
1531
|
+
|
|
1532
|
+
var innerIterator = _classPrivateFieldGet(this, _map$1).values();
|
|
1533
|
+
|
|
1534
|
+
return _ref3 = {}, _ref3[Symbol.iterator] = function () {
|
|
1535
|
+
return this;
|
|
1536
|
+
}, _ref3.next = function next() {
|
|
1537
|
+
var iteratorValue = innerIterator.next();
|
|
1538
|
+
|
|
1539
|
+
if (iteratorValue.done) {
|
|
1540
|
+
return {
|
|
1541
|
+
done: true,
|
|
1542
|
+
value: undefined
|
|
1543
|
+
};
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
return {
|
|
1547
|
+
value: selfOrRegisterValue(iteratorValue.value)
|
|
1548
|
+
};
|
|
1549
|
+
}, _ref3;
|
|
1550
|
+
};
|
|
1551
|
+
|
|
1552
|
+
_proto.forEach = function forEach(callback) {
|
|
1553
|
+
for (var _iterator7 = _createForOfIteratorHelperLoose(this), _step7; !(_step7 = _iterator7()).done;) {
|
|
1554
|
+
var entry = _step7.value;
|
|
1555
|
+
callback(entry[1], entry[0], this);
|
|
1556
|
+
}
|
|
1557
|
+
};
|
|
1558
|
+
|
|
1559
|
+
_createClass(LiveMap, [{
|
|
1560
|
+
key: "size",
|
|
1561
|
+
get: function get() {
|
|
1562
|
+
return _classPrivateFieldGet(this, _map$1).size;
|
|
1563
|
+
}
|
|
1564
|
+
}]);
|
|
1565
|
+
|
|
1566
|
+
return LiveMap;
|
|
1567
|
+
}(AbstractCrdt);
|
|
1568
|
+
|
|
1569
|
+
function remove(array, item) {
|
|
1570
|
+
for (var i = 0; i < array.length; i++) {
|
|
1571
|
+
if (array[i] === item) {
|
|
1572
|
+
array.splice(i, 1);
|
|
1573
|
+
break;
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
}
|
|
1577
|
+
function isSameNodeOrChildOf(node, parent) {
|
|
1578
|
+
if (node === parent) {
|
|
1579
|
+
return true;
|
|
1580
|
+
}
|
|
1581
|
+
|
|
1582
|
+
if (node._parent) {
|
|
1583
|
+
return isSameNodeOrChildOf(node._parent, parent);
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
return false;
|
|
1587
|
+
}
|
|
1588
|
+
function deserialize(entry, parentToChildren, doc) {
|
|
1589
|
+
switch (entry[1].type) {
|
|
1590
|
+
case CrdtType.Object:
|
|
1591
|
+
{
|
|
1592
|
+
return LiveObject._deserialize(entry, parentToChildren, doc);
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
case CrdtType.List:
|
|
1596
|
+
{
|
|
1597
|
+
return LiveList._deserialize(entry, parentToChildren, doc);
|
|
1598
|
+
}
|
|
1599
|
+
|
|
1600
|
+
case CrdtType.Map:
|
|
1601
|
+
{
|
|
1602
|
+
return LiveMap._deserialize(entry, parentToChildren, doc);
|
|
1603
|
+
}
|
|
1604
|
+
|
|
1605
|
+
case CrdtType.Register:
|
|
1606
|
+
{
|
|
1607
|
+
return LiveRegister._deserialize(entry, parentToChildren, doc);
|
|
1608
|
+
}
|
|
1609
|
+
|
|
1610
|
+
default:
|
|
1611
|
+
{
|
|
1612
|
+
throw new Error("Unexpected CRDT type");
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
}
|
|
1616
|
+
function isCrdt(obj) {
|
|
1617
|
+
return obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList || obj instanceof LiveRegister;
|
|
1618
|
+
}
|
|
1619
|
+
function selfOrRegisterValue(obj) {
|
|
1620
|
+
if (obj instanceof LiveRegister) {
|
|
1621
|
+
return obj.data;
|
|
1622
|
+
}
|
|
1623
|
+
|
|
1624
|
+
return obj;
|
|
1625
|
+
}
|
|
1626
|
+
function selfOrRegister(obj) {
|
|
1627
|
+
if (obj instanceof LiveObject || obj instanceof LiveMap || obj instanceof LiveList) {
|
|
1628
|
+
return obj;
|
|
1629
|
+
} else if (obj instanceof LiveRegister) {
|
|
1630
|
+
throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
|
|
1631
|
+
} else {
|
|
1632
|
+
return new LiveRegister(obj);
|
|
1633
|
+
}
|
|
1634
|
+
}
|
|
1635
|
+
function getTreesDiffOperations(currentItems, newItems) {
|
|
1636
|
+
var ops = [];
|
|
1637
|
+
currentItems.forEach(function (_, id) {
|
|
1638
|
+
if (!newItems.get(id)) {
|
|
1639
|
+
ops.push({
|
|
1640
|
+
type: OpType.DeleteCrdt,
|
|
1641
|
+
id: id
|
|
1642
|
+
});
|
|
1643
|
+
}
|
|
1644
|
+
});
|
|
1645
|
+
newItems.forEach(function (crdt, id) {
|
|
1646
|
+
var currentCrdt = currentItems.get(id);
|
|
1647
|
+
|
|
1648
|
+
if (currentCrdt) {
|
|
1649
|
+
if (crdt.type === CrdtType.Object) {
|
|
1650
|
+
if (JSON.stringify(crdt.data) !== JSON.stringify(currentCrdt.data)) {
|
|
1651
|
+
ops.push({
|
|
1652
|
+
type: OpType.UpdateObject,
|
|
1653
|
+
id: id,
|
|
1654
|
+
data: crdt.data
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
if (crdt.parentKey !== currentCrdt.parentKey) {
|
|
1660
|
+
ops.push({
|
|
1661
|
+
type: OpType.SetParentKey,
|
|
1662
|
+
id: id,
|
|
1663
|
+
parentKey: crdt.parentKey
|
|
1664
|
+
});
|
|
1665
|
+
}
|
|
1666
|
+
} else {
|
|
1667
|
+
switch (crdt.type) {
|
|
1668
|
+
case CrdtType.Register:
|
|
1669
|
+
ops.push({
|
|
1670
|
+
type: OpType.CreateRegister,
|
|
1671
|
+
id: id,
|
|
1672
|
+
parentId: crdt.parentId,
|
|
1673
|
+
parentKey: crdt.parentKey,
|
|
1674
|
+
data: crdt.data
|
|
1675
|
+
});
|
|
1676
|
+
break;
|
|
1677
|
+
|
|
1678
|
+
case CrdtType.List:
|
|
1679
|
+
ops.push({
|
|
1680
|
+
type: OpType.CreateList,
|
|
1681
|
+
id: id,
|
|
1682
|
+
parentId: crdt.parentId,
|
|
1683
|
+
parentKey: crdt.parentKey
|
|
1684
|
+
});
|
|
1685
|
+
break;
|
|
1686
|
+
|
|
1687
|
+
case CrdtType.Object:
|
|
1688
|
+
ops.push({
|
|
1689
|
+
type: OpType.CreateObject,
|
|
1690
|
+
id: id,
|
|
1691
|
+
parentId: crdt.parentId,
|
|
1692
|
+
parentKey: crdt.parentKey,
|
|
1693
|
+
data: crdt.data
|
|
1694
|
+
});
|
|
1695
|
+
break;
|
|
1696
|
+
|
|
1697
|
+
case CrdtType.Map:
|
|
1698
|
+
ops.push({
|
|
1699
|
+
type: OpType.CreateMap,
|
|
1700
|
+
id: id,
|
|
1701
|
+
parentId: crdt.parentId,
|
|
1702
|
+
parentKey: crdt.parentKey
|
|
1703
|
+
});
|
|
1704
|
+
break;
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
});
|
|
1708
|
+
return ops;
|
|
1709
|
+
}
|
|
1710
|
+
function mergeStorageUpdates(first, second) {
|
|
1711
|
+
if (!first) {
|
|
1712
|
+
return second;
|
|
1713
|
+
}
|
|
1714
|
+
|
|
1715
|
+
if (second.type === "LiveObject") {
|
|
1716
|
+
var updates = first.updates;
|
|
1717
|
+
|
|
1718
|
+
for (var _i = 0, _Object$entries = Object.entries(second.updates); _i < _Object$entries.length; _i++) {
|
|
1719
|
+
var _Object$entries$_i = _Object$entries[_i],
|
|
1720
|
+
key = _Object$entries$_i[0],
|
|
1721
|
+
value = _Object$entries$_i[1];
|
|
1722
|
+
updates[key] = value;
|
|
1723
|
+
}
|
|
1724
|
+
|
|
1725
|
+
return _extends({}, second, {
|
|
1726
|
+
updates: updates
|
|
1727
|
+
});
|
|
1728
|
+
} else if (second.type === "LiveMap") {
|
|
1729
|
+
var _updates = first.updates;
|
|
1730
|
+
|
|
1731
|
+
for (var _i2 = 0, _Object$entries2 = Object.entries(second.updates); _i2 < _Object$entries2.length; _i2++) {
|
|
1732
|
+
var _Object$entries2$_i = _Object$entries2[_i2],
|
|
1733
|
+
_key = _Object$entries2$_i[0],
|
|
1734
|
+
_value = _Object$entries2$_i[1];
|
|
1735
|
+
_updates[_key] = _value;
|
|
1736
|
+
}
|
|
1737
|
+
|
|
1738
|
+
return _extends({}, second, {
|
|
1739
|
+
updates: _updates
|
|
1740
|
+
});
|
|
1741
|
+
} else if (second.type === "LiveList") {
|
|
1742
|
+
var _updates2 = first.updates;
|
|
1743
|
+
return _extends({}, second, {
|
|
1744
|
+
updates: _updates2.concat(second.updates)
|
|
1745
|
+
});
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
return second;
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
function isPlain(value) {
|
|
1752
|
+
var type = typeof value;
|
|
1753
|
+
return type === "undefined" || value === null || type === "string" || type === "boolean" || type === "number" || Array.isArray(value) || isPlainObject$1(value);
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
function isPlainObject$1(value) {
|
|
1757
|
+
if (typeof value !== "object" || value === null) return false;
|
|
1758
|
+
var proto = Object.getPrototypeOf(value);
|
|
1759
|
+
if (proto === null) return true;
|
|
1760
|
+
var baseProto = proto;
|
|
1761
|
+
|
|
1762
|
+
while (Object.getPrototypeOf(baseProto) !== null) {
|
|
1763
|
+
baseProto = Object.getPrototypeOf(baseProto);
|
|
1764
|
+
}
|
|
1765
|
+
|
|
1766
|
+
return proto === baseProto;
|
|
1767
|
+
}
|
|
1768
|
+
|
|
1769
|
+
function findNonSerializableValue(value, path) {
|
|
1770
|
+
if (path === void 0) {
|
|
1771
|
+
path = "";
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
if (!isPlain) {
|
|
1775
|
+
return {
|
|
1776
|
+
path: path || "root",
|
|
1777
|
+
value: value
|
|
1778
|
+
};
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
if (typeof value !== "object" || value === null) {
|
|
1782
|
+
return false;
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
for (var _i3 = 0, _Object$entries3 = Object.entries(value); _i3 < _Object$entries3.length; _i3++) {
|
|
1786
|
+
var _Object$entries3$_i = _Object$entries3[_i3],
|
|
1787
|
+
key = _Object$entries3$_i[0],
|
|
1788
|
+
nestedValue = _Object$entries3$_i[1];
|
|
1789
|
+
var nestedPath = path ? path + "." + key : key;
|
|
1790
|
+
|
|
1791
|
+
if (!isPlain(nestedValue)) {
|
|
1792
|
+
return {
|
|
1793
|
+
path: nestedPath,
|
|
1794
|
+
value: nestedValue
|
|
1795
|
+
};
|
|
1796
|
+
}
|
|
1797
|
+
|
|
1798
|
+
if (typeof nestedValue === "object") {
|
|
1799
|
+
var nonSerializableNestedValue = findNonSerializableValue(nestedValue, nestedPath);
|
|
1800
|
+
|
|
1801
|
+
if (nonSerializableNestedValue) {
|
|
1802
|
+
return nonSerializableNestedValue;
|
|
1803
|
+
}
|
|
1804
|
+
}
|
|
1805
|
+
}
|
|
1806
|
+
|
|
1807
|
+
return false;
|
|
1808
|
+
}
|
|
1809
|
+
|
|
1810
|
+
var _map = new WeakMap();
|
|
1811
|
+
|
|
1812
|
+
var _propToLastUpdate = new WeakMap();
|
|
1813
|
+
|
|
1814
|
+
var _applyUpdate = new WeakSet();
|
|
1815
|
+
|
|
1816
|
+
var _applyDeleteObjectKey = new WeakSet();
|
|
1817
|
+
|
|
1818
|
+
var LiveObject = function (_AbstractCrdt) {
|
|
1819
|
+
_inheritsLoose(LiveObject, _AbstractCrdt);
|
|
1820
|
+
|
|
1821
|
+
function LiveObject(object) {
|
|
1822
|
+
var _this;
|
|
1823
|
+
|
|
1824
|
+
if (object === void 0) {
|
|
1825
|
+
object = {};
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
_this = _AbstractCrdt.call(this) || this;
|
|
1829
|
+
|
|
1830
|
+
_applyDeleteObjectKey.add(_assertThisInitialized(_this));
|
|
1831
|
+
|
|
1832
|
+
_applyUpdate.add(_assertThisInitialized(_this));
|
|
1833
|
+
|
|
1834
|
+
_map.set(_assertThisInitialized(_this), {
|
|
1835
|
+
writable: true,
|
|
1836
|
+
value: void 0
|
|
1837
|
+
});
|
|
1838
|
+
|
|
1839
|
+
_propToLastUpdate.set(_assertThisInitialized(_this), {
|
|
1840
|
+
writable: true,
|
|
1841
|
+
value: new Map()
|
|
1842
|
+
});
|
|
1843
|
+
|
|
1844
|
+
for (var key in object) {
|
|
1845
|
+
var value = object[key];
|
|
1846
|
+
|
|
1847
|
+
if (value instanceof AbstractCrdt) {
|
|
1848
|
+
value._setParentLink(_assertThisInitialized(_this), key);
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
|
|
1852
|
+
_classPrivateFieldSet(_assertThisInitialized(_this), _map, new Map(Object.entries(object)));
|
|
1853
|
+
|
|
1854
|
+
return _this;
|
|
1855
|
+
}
|
|
1856
|
+
|
|
1857
|
+
var _proto = LiveObject.prototype;
|
|
1858
|
+
|
|
1859
|
+
_proto._serialize = function _serialize(parentId, parentKey, doc) {
|
|
1860
|
+
if (this._id == null) {
|
|
1861
|
+
throw new Error("Cannot serialize item is not attached");
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
var ops = [];
|
|
1865
|
+
var op = {
|
|
1866
|
+
id: this._id,
|
|
1867
|
+
opId: doc == null ? void 0 : doc.generateOpId(),
|
|
1868
|
+
type: OpType.CreateObject,
|
|
1869
|
+
parentId: parentId,
|
|
1870
|
+
parentKey: parentKey,
|
|
1871
|
+
data: {}
|
|
1872
|
+
};
|
|
1873
|
+
ops.push(op);
|
|
1874
|
+
|
|
1875
|
+
for (var _iterator = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map)), _step; !(_step = _iterator()).done;) {
|
|
1876
|
+
var _step$value = _step.value,
|
|
1877
|
+
key = _step$value[0],
|
|
1878
|
+
value = _step$value[1];
|
|
1879
|
+
|
|
1880
|
+
if (value instanceof AbstractCrdt) {
|
|
1881
|
+
ops.push.apply(ops, value._serialize(this._id, key, doc));
|
|
1882
|
+
} else {
|
|
1883
|
+
op.data[key] = value;
|
|
1884
|
+
}
|
|
1885
|
+
}
|
|
1886
|
+
|
|
1887
|
+
return ops;
|
|
1888
|
+
};
|
|
1889
|
+
|
|
1890
|
+
LiveObject._deserialize = function _deserialize(_ref, parentToChildren, doc) {
|
|
1891
|
+
var id = _ref[0],
|
|
1892
|
+
item = _ref[1];
|
|
1893
|
+
|
|
1894
|
+
if (item.type !== CrdtType.Object) {
|
|
1895
|
+
throw new Error("Tried to deserialize a record but item type is \"" + item.type + "\"");
|
|
1896
|
+
}
|
|
1897
|
+
|
|
1898
|
+
var object = new LiveObject(item.data);
|
|
1899
|
+
|
|
1900
|
+
object._attach(id, doc);
|
|
1901
|
+
|
|
1902
|
+
return this._deserializeChildren(object, parentToChildren, doc);
|
|
1903
|
+
};
|
|
1904
|
+
|
|
1905
|
+
LiveObject._deserializeChildren = function _deserializeChildren(object, parentToChildren, doc) {
|
|
1906
|
+
var children = parentToChildren.get(object._id);
|
|
1907
|
+
|
|
1908
|
+
if (children == null) {
|
|
1909
|
+
return object;
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(children), _step2; !(_step2 = _iterator2()).done;) {
|
|
1913
|
+
var entry = _step2.value;
|
|
1914
|
+
var crdt = entry[1];
|
|
1915
|
+
|
|
1916
|
+
if (crdt.parentKey == null) {
|
|
1917
|
+
throw new Error("Tried to deserialize a crdt but it does not have a parentKey and is not the root");
|
|
1918
|
+
}
|
|
1919
|
+
|
|
1920
|
+
var child = deserialize(entry, parentToChildren, doc);
|
|
1921
|
+
|
|
1922
|
+
child._setParentLink(object, crdt.parentKey);
|
|
1923
|
+
|
|
1924
|
+
_classPrivateFieldGet(object, _map).set(crdt.parentKey, child);
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
return object;
|
|
1928
|
+
};
|
|
1929
|
+
|
|
1930
|
+
_proto._attach = function _attach(id, doc) {
|
|
1931
|
+
_AbstractCrdt.prototype._attach.call(this, id, doc);
|
|
1932
|
+
|
|
1933
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map)), _step3; !(_step3 = _iterator3()).done;) {
|
|
1934
|
+
var _step3$value = _step3.value;
|
|
1935
|
+
_step3$value[0];
|
|
1936
|
+
var value = _step3$value[1];
|
|
1937
|
+
|
|
1938
|
+
if (value instanceof AbstractCrdt) {
|
|
1939
|
+
value._attach(doc.generateId(), doc);
|
|
1940
|
+
}
|
|
1941
|
+
}
|
|
1942
|
+
};
|
|
1943
|
+
|
|
1944
|
+
_proto._attachChild = function _attachChild(id, key, child, opId, isLocal) {
|
|
1945
|
+
var _updates;
|
|
1946
|
+
|
|
1947
|
+
if (this._doc == null) {
|
|
1948
|
+
throw new Error("Can't attach child if doc is not present");
|
|
1949
|
+
}
|
|
1950
|
+
|
|
1951
|
+
if (this._doc.getItem(id) !== undefined) {
|
|
1952
|
+
if (_classPrivateFieldGet(this, _propToLastUpdate).get(key) === opId) {
|
|
1953
|
+
_classPrivateFieldGet(this, _propToLastUpdate).delete(key);
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
return {
|
|
1957
|
+
modified: false
|
|
1958
|
+
};
|
|
1959
|
+
}
|
|
1960
|
+
|
|
1961
|
+
if (isLocal) {
|
|
1962
|
+
_classPrivateFieldGet(this, _propToLastUpdate).set(key, opId);
|
|
1963
|
+
} else if (_classPrivateFieldGet(this, _propToLastUpdate).get(key) === undefined) ; else if (_classPrivateFieldGet(this, _propToLastUpdate).get(key) === opId) {
|
|
1964
|
+
_classPrivateFieldGet(this, _propToLastUpdate).delete(key);
|
|
1965
|
+
|
|
1966
|
+
return {
|
|
1967
|
+
modified: false
|
|
1968
|
+
};
|
|
1969
|
+
} else {
|
|
1970
|
+
return {
|
|
1971
|
+
modified: false
|
|
1972
|
+
};
|
|
1973
|
+
}
|
|
1974
|
+
|
|
1975
|
+
var previousValue = _classPrivateFieldGet(this, _map).get(key);
|
|
1976
|
+
|
|
1977
|
+
var reverse;
|
|
1978
|
+
|
|
1979
|
+
if (isCrdt(previousValue)) {
|
|
1980
|
+
reverse = previousValue._serialize(this._id, key);
|
|
1981
|
+
|
|
1982
|
+
previousValue._detach();
|
|
1983
|
+
} else if (previousValue === undefined) {
|
|
1984
|
+
reverse = [{
|
|
1985
|
+
type: OpType.DeleteObjectKey,
|
|
1986
|
+
id: this._id,
|
|
1987
|
+
key: key
|
|
1988
|
+
}];
|
|
1989
|
+
} else {
|
|
1990
|
+
var _data;
|
|
1991
|
+
|
|
1992
|
+
reverse = [{
|
|
1993
|
+
type: OpType.UpdateObject,
|
|
1994
|
+
id: this._id,
|
|
1995
|
+
data: (_data = {}, _data[key] = previousValue, _data)
|
|
1996
|
+
}];
|
|
1997
|
+
}
|
|
1998
|
+
|
|
1999
|
+
_classPrivateFieldGet(this, _map).set(key, child);
|
|
2000
|
+
|
|
2001
|
+
child._setParentLink(this, key);
|
|
2002
|
+
|
|
2003
|
+
child._attach(id, this._doc);
|
|
2004
|
+
|
|
2005
|
+
return {
|
|
2006
|
+
reverse: reverse,
|
|
2007
|
+
modified: {
|
|
2008
|
+
node: this,
|
|
2009
|
+
type: "LiveObject",
|
|
2010
|
+
updates: (_updates = {}, _updates[key] = {
|
|
2011
|
+
type: "update"
|
|
2012
|
+
}, _updates)
|
|
2013
|
+
}
|
|
2014
|
+
};
|
|
2015
|
+
};
|
|
2016
|
+
|
|
2017
|
+
_proto._detachChild = function _detachChild(child) {
|
|
2018
|
+
if (child) {
|
|
2019
|
+
var _updates2;
|
|
2020
|
+
|
|
2021
|
+
var reverse = child._serialize(this._id, child._parentKey, this._doc);
|
|
2022
|
+
|
|
2023
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map)), _step4; !(_step4 = _iterator4()).done;) {
|
|
2024
|
+
var _step4$value = _step4.value,
|
|
2025
|
+
key = _step4$value[0],
|
|
2026
|
+
value = _step4$value[1];
|
|
2027
|
+
|
|
2028
|
+
if (value === child) {
|
|
2029
|
+
_classPrivateFieldGet(this, _map).delete(key);
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
|
|
2033
|
+
child._detach();
|
|
2034
|
+
|
|
2035
|
+
var storageUpdate = {
|
|
2036
|
+
node: this,
|
|
2037
|
+
type: "LiveObject",
|
|
2038
|
+
updates: (_updates2 = {}, _updates2[child._parentKey] = {
|
|
2039
|
+
type: "delete"
|
|
2040
|
+
}, _updates2)
|
|
2041
|
+
};
|
|
2042
|
+
return {
|
|
2043
|
+
modified: storageUpdate,
|
|
2044
|
+
reverse: reverse
|
|
2045
|
+
};
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
return {
|
|
2049
|
+
modified: false
|
|
2050
|
+
};
|
|
2051
|
+
};
|
|
2052
|
+
|
|
2053
|
+
_proto._detachChildren = function _detachChildren() {
|
|
2054
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map)), _step5; !(_step5 = _iterator5()).done;) {
|
|
2055
|
+
var _step5$value = _step5.value,
|
|
2056
|
+
key = _step5$value[0],
|
|
2057
|
+
value = _step5$value[1];
|
|
2058
|
+
|
|
2059
|
+
_classPrivateFieldGet(this, _map).delete(key);
|
|
2060
|
+
|
|
2061
|
+
value._detach();
|
|
2062
|
+
}
|
|
2063
|
+
};
|
|
2064
|
+
|
|
2065
|
+
_proto._detach = function _detach() {
|
|
2066
|
+
_AbstractCrdt.prototype._detach.call(this);
|
|
2067
|
+
|
|
2068
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(_classPrivateFieldGet(this, _map).values()), _step6; !(_step6 = _iterator6()).done;) {
|
|
2069
|
+
var value = _step6.value;
|
|
2070
|
+
|
|
2071
|
+
if (isCrdt(value)) {
|
|
2072
|
+
value._detach();
|
|
2073
|
+
}
|
|
2074
|
+
}
|
|
2075
|
+
};
|
|
2076
|
+
|
|
2077
|
+
_proto._apply = function _apply(op, isLocal) {
|
|
2078
|
+
if (op.type === OpType.UpdateObject) {
|
|
2079
|
+
return _classPrivateMethodGet(this, _applyUpdate, _applyUpdate2).call(this, op, isLocal);
|
|
2080
|
+
} else if (op.type === OpType.DeleteObjectKey) {
|
|
2081
|
+
return _classPrivateMethodGet(this, _applyDeleteObjectKey, _applyDeleteObjectKey2).call(this, op);
|
|
2082
|
+
}
|
|
2083
|
+
|
|
2084
|
+
return _AbstractCrdt.prototype._apply.call(this, op, isLocal);
|
|
2085
|
+
};
|
|
2086
|
+
|
|
2087
|
+
_proto._toSerializedCrdt = function _toSerializedCrdt() {
|
|
2088
|
+
var _this$_parent;
|
|
2089
|
+
|
|
2090
|
+
return {
|
|
2091
|
+
type: CrdtType.Object,
|
|
2092
|
+
parentId: (_this$_parent = this._parent) == null ? void 0 : _this$_parent._id,
|
|
2093
|
+
parentKey: this._parentKey,
|
|
2094
|
+
data: this.toObject()
|
|
2095
|
+
};
|
|
2096
|
+
};
|
|
2097
|
+
|
|
2098
|
+
_proto.toObject = function toObject() {
|
|
2099
|
+
return Object.fromEntries(_classPrivateFieldGet(this, _map));
|
|
2100
|
+
};
|
|
2101
|
+
|
|
2102
|
+
_proto.set = function set(key, value) {
|
|
2103
|
+
var _this$update;
|
|
2104
|
+
|
|
2105
|
+
this.update((_this$update = {}, _this$update[key] = value, _this$update));
|
|
2106
|
+
};
|
|
2107
|
+
|
|
2108
|
+
_proto.get = function get(key) {
|
|
2109
|
+
return _classPrivateFieldGet(this, _map).get(key);
|
|
2110
|
+
};
|
|
2111
|
+
|
|
2112
|
+
_proto.delete = function _delete(key) {
|
|
2113
|
+
var _updates3;
|
|
2114
|
+
|
|
2115
|
+
var keyAsString = key;
|
|
2116
|
+
|
|
2117
|
+
var oldValue = _classPrivateFieldGet(this, _map).get(keyAsString);
|
|
2118
|
+
|
|
2119
|
+
if (oldValue === undefined) {
|
|
2120
|
+
return;
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
if (this._doc == null || this._id == null) {
|
|
2124
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2125
|
+
oldValue._detach();
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
_classPrivateFieldGet(this, _map).delete(keyAsString);
|
|
2129
|
+
|
|
2130
|
+
return;
|
|
2131
|
+
}
|
|
2132
|
+
|
|
2133
|
+
var reverse;
|
|
2134
|
+
|
|
2135
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2136
|
+
oldValue._detach();
|
|
2137
|
+
|
|
2138
|
+
reverse = oldValue._serialize(this._id, keyAsString);
|
|
2139
|
+
} else {
|
|
2140
|
+
var _data2;
|
|
2141
|
+
|
|
2142
|
+
reverse = [{
|
|
2143
|
+
type: OpType.UpdateObject,
|
|
2144
|
+
data: (_data2 = {}, _data2[keyAsString] = oldValue, _data2),
|
|
2145
|
+
id: this._id
|
|
2146
|
+
}];
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
_classPrivateFieldGet(this, _map).delete(keyAsString);
|
|
2150
|
+
|
|
2151
|
+
var storageUpdates = new Map();
|
|
2152
|
+
storageUpdates.set(this._id, {
|
|
2153
|
+
node: this,
|
|
2154
|
+
type: "LiveObject",
|
|
2155
|
+
updates: (_updates3 = {}, _updates3[key] = {
|
|
2156
|
+
type: "delete"
|
|
2157
|
+
}, _updates3)
|
|
2158
|
+
});
|
|
2159
|
+
|
|
2160
|
+
this._doc.dispatch([{
|
|
2161
|
+
type: OpType.DeleteObjectKey,
|
|
2162
|
+
key: keyAsString,
|
|
2163
|
+
id: this._id,
|
|
2164
|
+
opId: this._doc.generateOpId()
|
|
2165
|
+
}], reverse, storageUpdates);
|
|
2166
|
+
};
|
|
2167
|
+
|
|
2168
|
+
_proto.update = function update(overrides) {
|
|
2169
|
+
var _this2 = this;
|
|
2170
|
+
|
|
2171
|
+
if (this._doc == null || this._id == null) {
|
|
2172
|
+
for (var key in overrides) {
|
|
2173
|
+
var oldValue = _classPrivateFieldGet(this, _map).get(key);
|
|
2174
|
+
|
|
2175
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2176
|
+
oldValue._detach();
|
|
2177
|
+
}
|
|
2178
|
+
|
|
2179
|
+
var newValue = overrides[key];
|
|
2180
|
+
|
|
2181
|
+
if (newValue instanceof AbstractCrdt) {
|
|
2182
|
+
newValue._setParentLink(this, key);
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
_classPrivateFieldGet(this, _map).set(key, newValue);
|
|
2186
|
+
}
|
|
2187
|
+
|
|
2188
|
+
return;
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
var ops = [];
|
|
2192
|
+
var reverseOps = [];
|
|
2193
|
+
|
|
2194
|
+
var opId = this._doc.generateOpId();
|
|
2195
|
+
|
|
2196
|
+
var updatedProps = {};
|
|
2197
|
+
var reverseUpdateOp = {
|
|
2198
|
+
id: this._id,
|
|
2199
|
+
type: OpType.UpdateObject,
|
|
2200
|
+
data: {}
|
|
2201
|
+
};
|
|
2202
|
+
var updateDelta = {};
|
|
2203
|
+
|
|
2204
|
+
for (var _key in overrides) {
|
|
2205
|
+
var _oldValue = _classPrivateFieldGet(this, _map).get(_key);
|
|
2206
|
+
|
|
2207
|
+
if (_oldValue instanceof AbstractCrdt) {
|
|
2208
|
+
reverseOps.push.apply(reverseOps, _oldValue._serialize(this._id, _key));
|
|
2209
|
+
|
|
2210
|
+
_oldValue._detach();
|
|
2211
|
+
} else if (_oldValue === undefined) {
|
|
2212
|
+
reverseOps.push({
|
|
2213
|
+
type: OpType.DeleteObjectKey,
|
|
2214
|
+
id: this._id,
|
|
2215
|
+
key: _key
|
|
2216
|
+
});
|
|
2217
|
+
} else {
|
|
2218
|
+
reverseUpdateOp.data[_key] = _oldValue;
|
|
2219
|
+
}
|
|
2220
|
+
|
|
2221
|
+
var _newValue = overrides[_key];
|
|
2222
|
+
|
|
2223
|
+
if (_newValue instanceof AbstractCrdt) {
|
|
2224
|
+
_newValue._setParentLink(this, _key);
|
|
2225
|
+
|
|
2226
|
+
_newValue._attach(this._doc.generateId(), this._doc);
|
|
2227
|
+
|
|
2228
|
+
var newAttachChildOps = _newValue._serialize(this._id, _key, this._doc);
|
|
2229
|
+
|
|
2230
|
+
var createCrdtOp = newAttachChildOps.find(function (op) {
|
|
2231
|
+
return op.parentId === _this2._id;
|
|
2232
|
+
});
|
|
2233
|
+
|
|
2234
|
+
if (createCrdtOp) {
|
|
2235
|
+
_classPrivateFieldGet(this, _propToLastUpdate).set(_key, createCrdtOp.opId);
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
ops.push.apply(ops, newAttachChildOps);
|
|
2239
|
+
} else {
|
|
2240
|
+
updatedProps[_key] = _newValue;
|
|
2241
|
+
|
|
2242
|
+
_classPrivateFieldGet(this, _propToLastUpdate).set(_key, opId);
|
|
2243
|
+
}
|
|
2244
|
+
|
|
2245
|
+
_classPrivateFieldGet(this, _map).set(_key, _newValue);
|
|
2246
|
+
|
|
2247
|
+
updateDelta[_key] = {
|
|
2248
|
+
type: "update"
|
|
2249
|
+
};
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
if (Object.keys(reverseUpdateOp.data).length !== 0) {
|
|
2253
|
+
reverseOps.unshift(reverseUpdateOp);
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
if (Object.keys(updatedProps).length !== 0) {
|
|
2257
|
+
ops.unshift({
|
|
2258
|
+
opId: opId,
|
|
2259
|
+
id: this._id,
|
|
2260
|
+
type: OpType.UpdateObject,
|
|
2261
|
+
data: updatedProps
|
|
2262
|
+
});
|
|
2263
|
+
}
|
|
2264
|
+
|
|
2265
|
+
var storageUpdates = new Map();
|
|
2266
|
+
storageUpdates.set(this._id, {
|
|
2267
|
+
node: this,
|
|
2268
|
+
type: "LiveObject",
|
|
2269
|
+
updates: updateDelta
|
|
2270
|
+
});
|
|
2271
|
+
|
|
2272
|
+
this._doc.dispatch(ops, reverseOps, storageUpdates);
|
|
2273
|
+
};
|
|
2274
|
+
|
|
2275
|
+
return LiveObject;
|
|
2276
|
+
}(AbstractCrdt);
|
|
2277
|
+
|
|
2278
|
+
function _applyUpdate2(op, isLocal) {
|
|
2279
|
+
var isModified = false;
|
|
2280
|
+
var reverse = [];
|
|
2281
|
+
var reverseUpdate = {
|
|
2282
|
+
type: OpType.UpdateObject,
|
|
2283
|
+
id: this._id,
|
|
2284
|
+
data: {}
|
|
2285
|
+
};
|
|
2286
|
+
reverse.push(reverseUpdate);
|
|
2287
|
+
|
|
2288
|
+
for (var key in op.data) {
|
|
2289
|
+
var oldValue = _classPrivateFieldGet(this, _map).get(key);
|
|
2290
|
+
|
|
2291
|
+
if (oldValue instanceof AbstractCrdt) {
|
|
2292
|
+
reverse.push.apply(reverse, oldValue._serialize(this._id, key));
|
|
2293
|
+
|
|
2294
|
+
oldValue._detach();
|
|
2295
|
+
} else if (oldValue !== undefined) {
|
|
2296
|
+
reverseUpdate.data[key] = oldValue;
|
|
2297
|
+
} else if (oldValue === undefined) {
|
|
2298
|
+
reverse.push({
|
|
2299
|
+
type: OpType.DeleteObjectKey,
|
|
2300
|
+
id: this._id,
|
|
2301
|
+
key: key
|
|
2302
|
+
});
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
|
|
2306
|
+
var updateDelta = {};
|
|
2307
|
+
|
|
2308
|
+
for (var _key2 in op.data) {
|
|
2309
|
+
if (isLocal) {
|
|
2310
|
+
_classPrivateFieldGet(this, _propToLastUpdate).set(_key2, op.opId);
|
|
2311
|
+
} else if (_classPrivateFieldGet(this, _propToLastUpdate).get(_key2) == null) {
|
|
2312
|
+
isModified = true;
|
|
2313
|
+
} else if (_classPrivateFieldGet(this, _propToLastUpdate).get(_key2) === op.opId) {
|
|
2314
|
+
_classPrivateFieldGet(this, _propToLastUpdate).delete(_key2);
|
|
2315
|
+
|
|
2316
|
+
continue;
|
|
2317
|
+
} else {
|
|
2318
|
+
continue;
|
|
2319
|
+
}
|
|
2320
|
+
|
|
2321
|
+
var _oldValue2 = _classPrivateFieldGet(this, _map).get(_key2);
|
|
2322
|
+
|
|
2323
|
+
if (isCrdt(_oldValue2)) {
|
|
2324
|
+
_oldValue2._detach();
|
|
2325
|
+
}
|
|
2326
|
+
|
|
2327
|
+
isModified = true;
|
|
2328
|
+
updateDelta[_key2] = {
|
|
2329
|
+
type: "update"
|
|
2330
|
+
};
|
|
2331
|
+
|
|
2332
|
+
_classPrivateFieldGet(this, _map).set(_key2, op.data[_key2]);
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
if (Object.keys(reverseUpdate.data).length !== 0) {
|
|
2336
|
+
reverse.unshift(reverseUpdate);
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
return isModified ? {
|
|
2340
|
+
modified: {
|
|
2341
|
+
node: this,
|
|
2342
|
+
type: "LiveObject",
|
|
2343
|
+
updates: updateDelta
|
|
2344
|
+
},
|
|
2345
|
+
reverse: reverse
|
|
2346
|
+
} : {
|
|
2347
|
+
modified: false
|
|
2348
|
+
};
|
|
2349
|
+
}
|
|
2350
|
+
|
|
2351
|
+
function _applyDeleteObjectKey2(op) {
|
|
2352
|
+
var _updates4;
|
|
2353
|
+
|
|
2354
|
+
var key = op.key;
|
|
2355
|
+
|
|
2356
|
+
if (_classPrivateFieldGet(this, _map).has(key) === false) {
|
|
2357
|
+
return {
|
|
2358
|
+
modified: false
|
|
2359
|
+
};
|
|
2360
|
+
}
|
|
2361
|
+
|
|
2362
|
+
if (_classPrivateFieldGet(this, _propToLastUpdate).get(key) !== undefined) {
|
|
2363
|
+
return {
|
|
2364
|
+
modified: false
|
|
2365
|
+
};
|
|
2366
|
+
}
|
|
2367
|
+
|
|
2368
|
+
var oldValue = _classPrivateFieldGet(this, _map).get(key);
|
|
2369
|
+
|
|
2370
|
+
var reverse = [];
|
|
2371
|
+
|
|
2372
|
+
if (isCrdt(oldValue)) {
|
|
2373
|
+
reverse = oldValue._serialize(this._id, op.key);
|
|
2374
|
+
|
|
2375
|
+
oldValue._detach();
|
|
2376
|
+
} else if (oldValue !== undefined) {
|
|
2377
|
+
var _data3;
|
|
2378
|
+
|
|
2379
|
+
reverse = [{
|
|
2380
|
+
type: OpType.UpdateObject,
|
|
2381
|
+
id: this._id,
|
|
2382
|
+
data: (_data3 = {}, _data3[key] = oldValue, _data3)
|
|
2383
|
+
}];
|
|
2384
|
+
}
|
|
2385
|
+
|
|
2386
|
+
_classPrivateFieldGet(this, _map).delete(key);
|
|
2387
|
+
|
|
2388
|
+
return {
|
|
2389
|
+
modified: {
|
|
2390
|
+
node: this,
|
|
2391
|
+
type: "LiveObject",
|
|
2392
|
+
updates: (_updates4 = {}, _updates4[op.key] = {
|
|
2393
|
+
type: "delete"
|
|
2394
|
+
}, _updates4)
|
|
2395
|
+
},
|
|
2396
|
+
reverse: reverse
|
|
2397
|
+
};
|
|
2398
|
+
}
|
|
2399
|
+
|
|
2400
|
+
var BACKOFF_RETRY_DELAYS = [250, 500, 1000, 2000, 4000, 8000, 10000];
|
|
2401
|
+
var HEARTBEAT_INTERVAL = 30000;
|
|
2402
|
+
var PONG_TIMEOUT = 2000;
|
|
2403
|
+
|
|
2404
|
+
function isValidRoomEventType(value) {
|
|
2405
|
+
return value === "my-presence" || value === "others" || value === "event" || value === "error" || value === "connection";
|
|
2406
|
+
}
|
|
2407
|
+
|
|
2408
|
+
function makeIdFactory(connectionId) {
|
|
2409
|
+
var count = 0;
|
|
2410
|
+
return function () {
|
|
2411
|
+
return connectionId + ":" + count++;
|
|
2412
|
+
};
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2415
|
+
function makeOthers(presenceMap) {
|
|
2416
|
+
var _ref;
|
|
2417
|
+
|
|
2418
|
+
var array = Object.values(presenceMap);
|
|
2419
|
+
return _ref = {
|
|
2420
|
+
get count() {
|
|
2421
|
+
return array.length;
|
|
2422
|
+
}
|
|
2423
|
+
|
|
2424
|
+
}, _ref[Symbol.iterator] = function () {
|
|
2425
|
+
return array[Symbol.iterator]();
|
|
2426
|
+
}, _ref.map = function map(callback) {
|
|
2427
|
+
return array.map(callback);
|
|
2428
|
+
}, _ref.toArray = function toArray() {
|
|
2429
|
+
return array;
|
|
2430
|
+
}, _ref;
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2433
|
+
function makeStateMachine(state, context, mockedEffects) {
|
|
2434
|
+
var effects = mockedEffects || {
|
|
2435
|
+
authenticate: function authenticate(auth, createWebSocket) {
|
|
2436
|
+
return auth(context.room).then(function (_ref2) {
|
|
2437
|
+
var token = _ref2.token;
|
|
2438
|
+
var parsedToken = parseToken(token);
|
|
2439
|
+
var socket = createWebSocket(token);
|
|
2440
|
+
authenticationSuccess(parsedToken, socket);
|
|
2441
|
+
}).catch(function (er) {
|
|
2442
|
+
return authenticationFailure(er);
|
|
2443
|
+
});
|
|
2444
|
+
},
|
|
2445
|
+
send: function send(messageOrMessages) {
|
|
2446
|
+
if (state.socket == null) {
|
|
2447
|
+
throw new Error("Can't send message if socket is null");
|
|
2448
|
+
}
|
|
2449
|
+
|
|
2450
|
+
state.socket.send(JSON.stringify(messageOrMessages));
|
|
2451
|
+
},
|
|
2452
|
+
delayFlush: function delayFlush(delay) {
|
|
2453
|
+
return setTimeout(tryFlushing, delay);
|
|
2454
|
+
},
|
|
2455
|
+
startHeartbeatInterval: function startHeartbeatInterval() {
|
|
2456
|
+
return setInterval(heartbeat, HEARTBEAT_INTERVAL);
|
|
2457
|
+
},
|
|
2458
|
+
schedulePongTimeout: function schedulePongTimeout() {
|
|
2459
|
+
return setTimeout(pongTimeout, PONG_TIMEOUT);
|
|
2460
|
+
},
|
|
2461
|
+
scheduleReconnect: function scheduleReconnect(delay) {
|
|
2462
|
+
return setTimeout(connect, delay);
|
|
2463
|
+
}
|
|
2464
|
+
};
|
|
2465
|
+
|
|
2466
|
+
function genericSubscribe(callback) {
|
|
2467
|
+
state.listeners.storage.push(callback);
|
|
2468
|
+
return function () {
|
|
2469
|
+
return remove(state.listeners.storage, callback);
|
|
2470
|
+
};
|
|
2471
|
+
}
|
|
2472
|
+
|
|
2473
|
+
function crdtSubscribe(crdt, innerCallback, options) {
|
|
2474
|
+
var cb = function cb(updates) {
|
|
2475
|
+
var relatedUpdates = [];
|
|
2476
|
+
|
|
2477
|
+
for (var _iterator = _createForOfIteratorHelperLoose(updates), _step; !(_step = _iterator()).done;) {
|
|
2478
|
+
var update = _step.value;
|
|
2479
|
+
|
|
2480
|
+
if (options != null && options.isDeep && isSameNodeOrChildOf(update.node, crdt)) {
|
|
2481
|
+
relatedUpdates.push(update);
|
|
2482
|
+
} else if (update.node._id === crdt._id) {
|
|
2483
|
+
innerCallback(update.node);
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
|
|
2487
|
+
if (options != null && options.isDeep && relatedUpdates.length > 0) {
|
|
2488
|
+
innerCallback(relatedUpdates);
|
|
2489
|
+
}
|
|
2490
|
+
};
|
|
2491
|
+
|
|
2492
|
+
return genericSubscribe(cb);
|
|
2493
|
+
}
|
|
2494
|
+
|
|
2495
|
+
function createOrUpdateRootFromMessage(message) {
|
|
2496
|
+
if (message.items.length === 0) {
|
|
2497
|
+
throw new Error("Internal error: cannot load storage without items");
|
|
2498
|
+
}
|
|
2499
|
+
|
|
2500
|
+
if (state.root) {
|
|
2501
|
+
updateRoot(message.items);
|
|
2502
|
+
} else {
|
|
2503
|
+
state.root = load(message.items);
|
|
2504
|
+
}
|
|
2505
|
+
|
|
2506
|
+
for (var _key2 in state.defaultStorageRoot) {
|
|
2507
|
+
if (state.root.get(_key2) == null) {
|
|
2508
|
+
state.root.set(_key2, state.defaultStorageRoot[_key2]);
|
|
2509
|
+
}
|
|
2510
|
+
}
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
function buildRootAndParentToChildren(items) {
|
|
2514
|
+
var parentToChildren = new Map();
|
|
2515
|
+
var root = null;
|
|
2516
|
+
|
|
2517
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(items), _step2; !(_step2 = _iterator2()).done;) {
|
|
2518
|
+
var tuple = _step2.value;
|
|
2519
|
+
var parentId = tuple[1].parentId;
|
|
2520
|
+
|
|
2521
|
+
if (parentId == null) {
|
|
2522
|
+
root = tuple;
|
|
2523
|
+
} else {
|
|
2524
|
+
var children = parentToChildren.get(parentId);
|
|
2525
|
+
|
|
2526
|
+
if (children != null) {
|
|
2527
|
+
children.push(tuple);
|
|
2528
|
+
} else {
|
|
2529
|
+
parentToChildren.set(parentId, [tuple]);
|
|
2530
|
+
}
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
|
|
2534
|
+
if (root == null) {
|
|
2535
|
+
throw new Error("Root can't be null");
|
|
2536
|
+
}
|
|
2537
|
+
|
|
2538
|
+
return [root, parentToChildren];
|
|
2539
|
+
}
|
|
2540
|
+
|
|
2541
|
+
function updateRoot(items) {
|
|
2542
|
+
if (!state.root) {
|
|
2543
|
+
return;
|
|
2544
|
+
}
|
|
2545
|
+
|
|
2546
|
+
var currentItems = new Map();
|
|
2547
|
+
state.items.forEach(function (liveCrdt, id) {
|
|
2548
|
+
currentItems.set(id, liveCrdt._toSerializedCrdt());
|
|
2549
|
+
});
|
|
2550
|
+
var ops = getTreesDiffOperations(currentItems, new Map(items));
|
|
2551
|
+
var result = apply(ops, false);
|
|
2552
|
+
notify(result.updates);
|
|
2553
|
+
}
|
|
2554
|
+
|
|
2555
|
+
function load(items) {
|
|
2556
|
+
var _buildRootAndParentTo = buildRootAndParentToChildren(items),
|
|
2557
|
+
root = _buildRootAndParentTo[0],
|
|
2558
|
+
parentToChildren = _buildRootAndParentTo[1];
|
|
2559
|
+
|
|
2560
|
+
return LiveObject._deserialize(root, parentToChildren, {
|
|
2561
|
+
getItem: getItem,
|
|
2562
|
+
addItem: addItem,
|
|
2563
|
+
deleteItem: deleteItem,
|
|
2564
|
+
generateId: generateId,
|
|
2565
|
+
generateOpId: generateOpId,
|
|
2566
|
+
dispatch: storageDispatch,
|
|
2567
|
+
roomId: context.room
|
|
2568
|
+
});
|
|
2569
|
+
}
|
|
2570
|
+
|
|
2571
|
+
function addItem(id, item) {
|
|
2572
|
+
state.items.set(id, item);
|
|
2573
|
+
}
|
|
2574
|
+
|
|
2575
|
+
function deleteItem(id) {
|
|
2576
|
+
state.items.delete(id);
|
|
2577
|
+
}
|
|
2578
|
+
|
|
2579
|
+
function getItem(id) {
|
|
2580
|
+
return state.items.get(id);
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
function addToUndoStack(historyItem) {
|
|
2584
|
+
if (state.undoStack.length >= 50) {
|
|
2585
|
+
state.undoStack.shift();
|
|
2586
|
+
}
|
|
2587
|
+
|
|
2588
|
+
if (state.isHistoryPaused) {
|
|
2589
|
+
var _state$pausedHistory;
|
|
2590
|
+
|
|
2591
|
+
(_state$pausedHistory = state.pausedHistory).unshift.apply(_state$pausedHistory, historyItem);
|
|
2592
|
+
} else {
|
|
2593
|
+
state.undoStack.push(historyItem);
|
|
2594
|
+
}
|
|
2595
|
+
}
|
|
2596
|
+
|
|
2597
|
+
function storageDispatch(ops, reverse, storageUpdates) {
|
|
2598
|
+
if (state.isBatching) {
|
|
2599
|
+
var _state$batch$ops, _state$batch$reverseO;
|
|
2600
|
+
|
|
2601
|
+
(_state$batch$ops = state.batch.ops).push.apply(_state$batch$ops, ops);
|
|
2602
|
+
|
|
2603
|
+
storageUpdates.forEach(function (value, key) {
|
|
2604
|
+
state.batch.updates.storageUpdates.set(key, mergeStorageUpdates(state.batch.updates.storageUpdates.get(key), value));
|
|
2605
|
+
});
|
|
2606
|
+
|
|
2607
|
+
(_state$batch$reverseO = state.batch.reverseOps).push.apply(_state$batch$reverseO, reverse);
|
|
2608
|
+
} else {
|
|
2609
|
+
addToUndoStack(reverse);
|
|
2610
|
+
state.redoStack = [];
|
|
2611
|
+
dispatch(ops);
|
|
2612
|
+
notify({
|
|
2613
|
+
storageUpdates: storageUpdates
|
|
2614
|
+
});
|
|
2615
|
+
}
|
|
2616
|
+
}
|
|
2617
|
+
|
|
2618
|
+
function notify(_ref3) {
|
|
2619
|
+
var _ref3$storageUpdates = _ref3.storageUpdates,
|
|
2620
|
+
storageUpdates = _ref3$storageUpdates === void 0 ? new Map() : _ref3$storageUpdates,
|
|
2621
|
+
_ref3$presence = _ref3.presence,
|
|
2622
|
+
presence = _ref3$presence === void 0 ? false : _ref3$presence,
|
|
2623
|
+
_ref3$others = _ref3.others,
|
|
2624
|
+
others = _ref3$others === void 0 ? [] : _ref3$others;
|
|
2625
|
+
|
|
2626
|
+
if (others.length > 0) {
|
|
2627
|
+
state.others = makeOthers(state.users);
|
|
2628
|
+
|
|
2629
|
+
for (var _iterator3 = _createForOfIteratorHelperLoose(others), _step3; !(_step3 = _iterator3()).done;) {
|
|
2630
|
+
var event = _step3.value;
|
|
2631
|
+
|
|
2632
|
+
for (var _iterator4 = _createForOfIteratorHelperLoose(state.listeners["others"]), _step4; !(_step4 = _iterator4()).done;) {
|
|
2633
|
+
var _listener = _step4.value;
|
|
2634
|
+
|
|
2635
|
+
_listener(state.others, event);
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
}
|
|
2639
|
+
|
|
2640
|
+
if (presence) {
|
|
2641
|
+
for (var _iterator5 = _createForOfIteratorHelperLoose(state.listeners["my-presence"]), _step5; !(_step5 = _iterator5()).done;) {
|
|
2642
|
+
var _listener2 = _step5.value;
|
|
2643
|
+
|
|
2644
|
+
_listener2(state.me);
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
|
|
2648
|
+
if (storageUpdates.size > 0) {
|
|
2649
|
+
for (var _iterator6 = _createForOfIteratorHelperLoose(state.listeners.storage), _step6; !(_step6 = _iterator6()).done;) {
|
|
2650
|
+
var subscriber = _step6.value;
|
|
2651
|
+
subscriber(Array.from(storageUpdates.values()));
|
|
2652
|
+
}
|
|
2653
|
+
}
|
|
2654
|
+
}
|
|
2655
|
+
|
|
2656
|
+
function getConnectionId() {
|
|
2657
|
+
if (state.connection.state === "open" || state.connection.state === "connecting") {
|
|
2658
|
+
return state.connection.id;
|
|
2659
|
+
} else if (state.lastConnectionId !== null) {
|
|
2660
|
+
return state.lastConnectionId;
|
|
2661
|
+
}
|
|
2662
|
+
|
|
2663
|
+
throw new Error("Internal. Tried to get connection id but connection was never open");
|
|
2664
|
+
}
|
|
2665
|
+
|
|
2666
|
+
function generateId() {
|
|
2667
|
+
return getConnectionId() + ":" + state.clock++;
|
|
2668
|
+
}
|
|
2669
|
+
|
|
2670
|
+
function generateOpId() {
|
|
2671
|
+
return getConnectionId() + ":" + state.opClock++;
|
|
2672
|
+
}
|
|
2673
|
+
|
|
2674
|
+
function apply(item, isLocal) {
|
|
2675
|
+
var result = {
|
|
2676
|
+
reverse: [],
|
|
2677
|
+
updates: {
|
|
2678
|
+
storageUpdates: new Map(),
|
|
2679
|
+
presence: false
|
|
2680
|
+
}
|
|
2681
|
+
};
|
|
2682
|
+
|
|
2683
|
+
for (var _iterator7 = _createForOfIteratorHelperLoose(item), _step7; !(_step7 = _iterator7()).done;) {
|
|
2684
|
+
var op = _step7.value;
|
|
2685
|
+
|
|
2686
|
+
if (op.type === "presence") {
|
|
2687
|
+
var reverse = {
|
|
2688
|
+
type: "presence",
|
|
2689
|
+
data: {}
|
|
2690
|
+
};
|
|
2691
|
+
|
|
2692
|
+
for (var _key3 in op.data) {
|
|
2693
|
+
reverse.data[_key3] = state.me[_key3];
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
state.me = _extends({}, state.me, op.data);
|
|
2697
|
+
|
|
2698
|
+
if (state.buffer.presence == null) {
|
|
2699
|
+
state.buffer.presence = op.data;
|
|
2700
|
+
} else {
|
|
2701
|
+
for (var _key4 in op.data) {
|
|
2702
|
+
state.buffer.presence[_key4] = op.data;
|
|
2703
|
+
}
|
|
2704
|
+
}
|
|
2705
|
+
|
|
2706
|
+
result.reverse.unshift(reverse);
|
|
2707
|
+
result.updates.presence = true;
|
|
2708
|
+
} else {
|
|
2709
|
+
if (isLocal && !op.opId) {
|
|
2710
|
+
op.opId = generateOpId();
|
|
2711
|
+
}
|
|
2712
|
+
|
|
2713
|
+
var applyOpResult = applyOp(op, isLocal);
|
|
2714
|
+
|
|
2715
|
+
if (applyOpResult.modified) {
|
|
2716
|
+
var _result$reverse;
|
|
2717
|
+
|
|
2718
|
+
result.updates.storageUpdates.set(applyOpResult.modified.node._id, mergeStorageUpdates(result.updates.storageUpdates.get(applyOpResult.modified.node._id), applyOpResult.modified));
|
|
2719
|
+
|
|
2720
|
+
(_result$reverse = result.reverse).unshift.apply(_result$reverse, applyOpResult.reverse);
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
}
|
|
2724
|
+
|
|
2725
|
+
return result;
|
|
2726
|
+
}
|
|
2727
|
+
|
|
2728
|
+
function applyOp(op, isLocal) {
|
|
2729
|
+
if (op.opId) {
|
|
2730
|
+
state.offlineOperations.delete(op.opId);
|
|
2731
|
+
}
|
|
2732
|
+
|
|
2733
|
+
switch (op.type) {
|
|
2734
|
+
case OpType.DeleteObjectKey:
|
|
2735
|
+
case OpType.UpdateObject:
|
|
2736
|
+
case OpType.DeleteCrdt:
|
|
2737
|
+
{
|
|
2738
|
+
var item = state.items.get(op.id);
|
|
2739
|
+
|
|
2740
|
+
if (item == null) {
|
|
2741
|
+
return {
|
|
2742
|
+
modified: false
|
|
2743
|
+
};
|
|
2744
|
+
}
|
|
2745
|
+
|
|
2746
|
+
return item._apply(op, isLocal);
|
|
2747
|
+
}
|
|
2748
|
+
|
|
2749
|
+
case OpType.SetParentKey:
|
|
2750
|
+
{
|
|
2751
|
+
var _item = state.items.get(op.id);
|
|
2752
|
+
|
|
2753
|
+
if (_item == null) {
|
|
2754
|
+
return {
|
|
2755
|
+
modified: false
|
|
2756
|
+
};
|
|
2757
|
+
}
|
|
2758
|
+
|
|
2759
|
+
if (_item._parent instanceof LiveList) {
|
|
2760
|
+
var previousKey = _item._parentKey;
|
|
2761
|
+
|
|
2762
|
+
if (previousKey === op.parentKey) {
|
|
2763
|
+
return {
|
|
2764
|
+
modified: false
|
|
2765
|
+
};
|
|
2766
|
+
} else {
|
|
2767
|
+
return _item._parent._setChildKey(op.parentKey, _item, previousKey);
|
|
2768
|
+
}
|
|
2769
|
+
}
|
|
2770
|
+
|
|
2771
|
+
return {
|
|
2772
|
+
modified: false
|
|
2773
|
+
};
|
|
2774
|
+
}
|
|
2775
|
+
|
|
2776
|
+
case OpType.CreateObject:
|
|
2777
|
+
{
|
|
2778
|
+
var parent = state.items.get(op.parentId);
|
|
2779
|
+
|
|
2780
|
+
if (parent == null) {
|
|
2781
|
+
return {
|
|
2782
|
+
modified: false
|
|
2783
|
+
};
|
|
2784
|
+
}
|
|
2785
|
+
|
|
2786
|
+
return parent._attachChild(op.id, op.parentKey, new LiveObject(op.data), op.opId, isLocal);
|
|
2787
|
+
}
|
|
2788
|
+
|
|
2789
|
+
case OpType.CreateList:
|
|
2790
|
+
{
|
|
2791
|
+
var _parent = state.items.get(op.parentId);
|
|
2792
|
+
|
|
2793
|
+
if (_parent == null) {
|
|
2794
|
+
return {
|
|
2795
|
+
modified: false
|
|
2796
|
+
};
|
|
2797
|
+
}
|
|
2798
|
+
|
|
2799
|
+
return _parent._attachChild(op.id, op.parentKey, new LiveList(), op.opId, isLocal);
|
|
2800
|
+
}
|
|
2801
|
+
|
|
2802
|
+
case OpType.CreateRegister:
|
|
2803
|
+
{
|
|
2804
|
+
var _parent2 = state.items.get(op.parentId);
|
|
2805
|
+
|
|
2806
|
+
if (_parent2 == null) {
|
|
2807
|
+
return {
|
|
2808
|
+
modified: false
|
|
2809
|
+
};
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
return _parent2._attachChild(op.id, op.parentKey, new LiveRegister(op.data), op.opId, isLocal);
|
|
2813
|
+
}
|
|
2814
|
+
|
|
2815
|
+
case OpType.CreateMap:
|
|
2816
|
+
{
|
|
2817
|
+
var _parent3 = state.items.get(op.parentId);
|
|
2818
|
+
|
|
2819
|
+
if (_parent3 == null) {
|
|
2820
|
+
return {
|
|
2821
|
+
modified: false
|
|
2822
|
+
};
|
|
2823
|
+
}
|
|
2824
|
+
|
|
2825
|
+
return _parent3._attachChild(op.id, op.parentKey, new LiveMap(), op.opId, isLocal);
|
|
2826
|
+
}
|
|
2827
|
+
}
|
|
2828
|
+
|
|
2829
|
+
return {
|
|
2830
|
+
modified: false
|
|
2831
|
+
};
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
function subscribe(firstParam, listener, options) {
|
|
2835
|
+
if (firstParam instanceof AbstractCrdt) {
|
|
2836
|
+
return crdtSubscribe(firstParam, listener, options);
|
|
2837
|
+
} else if (typeof firstParam === "function") {
|
|
2838
|
+
return genericSubscribe(firstParam);
|
|
2839
|
+
} else if (!isValidRoomEventType(firstParam)) {
|
|
2840
|
+
throw new Error("\"" + firstParam + "\" is not a valid event name");
|
|
2841
|
+
}
|
|
2842
|
+
|
|
2843
|
+
state.listeners[firstParam].push(listener);
|
|
2844
|
+
return function () {
|
|
2845
|
+
var callbacks = state.listeners[firstParam];
|
|
2846
|
+
remove(callbacks, listener);
|
|
2847
|
+
};
|
|
2848
|
+
}
|
|
2849
|
+
|
|
2850
|
+
function unsubscribe(event, callback) {
|
|
2851
|
+
console.warn("unsubscribe is depreacted and will be removed in a future version.\nuse the callback returned by subscribe instead.\nSee v0.13 release notes for more information.\n");
|
|
2852
|
+
|
|
2853
|
+
if (!isValidRoomEventType(event)) {
|
|
2854
|
+
throw new Error("\"" + event + "\" is not a valid event name");
|
|
2855
|
+
}
|
|
2856
|
+
|
|
2857
|
+
var callbacks = state.listeners[event];
|
|
2858
|
+
remove(callbacks, callback);
|
|
2859
|
+
}
|
|
2860
|
+
|
|
2861
|
+
function getConnectionState() {
|
|
2862
|
+
return state.connection.state;
|
|
2863
|
+
}
|
|
2864
|
+
|
|
2865
|
+
function getSelf() {
|
|
2866
|
+
return state.connection.state === "open" || state.connection.state === "connecting" ? {
|
|
2867
|
+
connectionId: state.connection.id,
|
|
2868
|
+
id: state.connection.userId,
|
|
2869
|
+
info: state.connection.userInfo,
|
|
2870
|
+
presence: getPresence()
|
|
2871
|
+
} : null;
|
|
2872
|
+
}
|
|
2873
|
+
|
|
2874
|
+
function connect() {
|
|
2875
|
+
if (state.connection.state !== "closed" && state.connection.state !== "unavailable") {
|
|
2876
|
+
return null;
|
|
2877
|
+
}
|
|
2878
|
+
|
|
2879
|
+
var auth = prepareAuthEndpoint(context.authentication, context.fetchPolyfill);
|
|
2880
|
+
var createWebSocket = prepareCreateWebSocket(context.liveblocksServer, context.WebSocketPolyfill);
|
|
2881
|
+
updateConnection({
|
|
2882
|
+
state: "authenticating"
|
|
2883
|
+
});
|
|
2884
|
+
effects.authenticate(auth, createWebSocket);
|
|
2885
|
+
}
|
|
2886
|
+
|
|
2887
|
+
function updatePresence(overrides, options) {
|
|
2888
|
+
var oldValues = {};
|
|
2889
|
+
|
|
2890
|
+
if (state.buffer.presence == null) {
|
|
2891
|
+
state.buffer.presence = {};
|
|
2892
|
+
}
|
|
2893
|
+
|
|
2894
|
+
for (var _key5 in overrides) {
|
|
2895
|
+
state.buffer.presence[_key5] = overrides[_key5];
|
|
2896
|
+
oldValues[_key5] = state.me[_key5];
|
|
2897
|
+
}
|
|
2898
|
+
|
|
2899
|
+
state.me = _extends({}, state.me, overrides);
|
|
2900
|
+
|
|
2901
|
+
if (state.isBatching) {
|
|
2902
|
+
if (options != null && options.addToHistory) {
|
|
2903
|
+
state.batch.reverseOps.push({
|
|
2904
|
+
type: "presence",
|
|
2905
|
+
data: oldValues
|
|
2906
|
+
});
|
|
2907
|
+
}
|
|
2908
|
+
|
|
2909
|
+
state.batch.updates.presence = true;
|
|
2910
|
+
} else {
|
|
2911
|
+
tryFlushing();
|
|
2912
|
+
|
|
2913
|
+
if (options != null && options.addToHistory) {
|
|
2914
|
+
addToUndoStack([{
|
|
2915
|
+
type: "presence",
|
|
2916
|
+
data: oldValues
|
|
2917
|
+
}]);
|
|
2918
|
+
}
|
|
2919
|
+
|
|
2920
|
+
notify({
|
|
2921
|
+
presence: true
|
|
2922
|
+
});
|
|
2923
|
+
}
|
|
2924
|
+
}
|
|
2925
|
+
|
|
2926
|
+
function authenticationSuccess(token, socket) {
|
|
2927
|
+
socket.addEventListener("message", onMessage);
|
|
2928
|
+
socket.addEventListener("open", onOpen);
|
|
2929
|
+
socket.addEventListener("close", onClose);
|
|
2930
|
+
socket.addEventListener("error", onError);
|
|
2931
|
+
updateConnection({
|
|
2932
|
+
state: "connecting",
|
|
2933
|
+
id: token.actor,
|
|
2934
|
+
userInfo: token.info,
|
|
2935
|
+
userId: token.id
|
|
2936
|
+
});
|
|
2937
|
+
state.idFactory = makeIdFactory(token.actor);
|
|
2938
|
+
state.socket = socket;
|
|
2939
|
+
}
|
|
2940
|
+
|
|
2941
|
+
function authenticationFailure(error) {
|
|
2942
|
+
if (process.env.NODE_ENV !== "production") {
|
|
2943
|
+
console.error("Call to authentication endpoint failed", error);
|
|
2944
|
+
}
|
|
2945
|
+
|
|
2946
|
+
updateConnection({
|
|
2947
|
+
state: "unavailable"
|
|
2948
|
+
});
|
|
2949
|
+
state.numberOfRetry++;
|
|
2950
|
+
state.timeoutHandles.reconnect = effects.scheduleReconnect(getRetryDelay());
|
|
2951
|
+
}
|
|
2952
|
+
|
|
2953
|
+
function onVisibilityChange(visibilityState) {
|
|
2954
|
+
if (visibilityState === "visible" && state.connection.state === "open") {
|
|
2955
|
+
heartbeat();
|
|
2956
|
+
}
|
|
2957
|
+
}
|
|
2958
|
+
|
|
2959
|
+
function onUpdatePresenceMessage(message) {
|
|
2960
|
+
var user = state.users[message.actor];
|
|
2961
|
+
|
|
2962
|
+
if (user == null) {
|
|
2963
|
+
state.users[message.actor] = {
|
|
2964
|
+
connectionId: message.actor,
|
|
2965
|
+
presence: message.data
|
|
2966
|
+
};
|
|
2967
|
+
} else {
|
|
2968
|
+
state.users[message.actor] = {
|
|
2969
|
+
id: user.id,
|
|
2970
|
+
info: user.info,
|
|
2971
|
+
connectionId: message.actor,
|
|
2972
|
+
presence: _extends({}, user.presence, message.data)
|
|
2973
|
+
};
|
|
2974
|
+
}
|
|
2975
|
+
|
|
2976
|
+
return {
|
|
2977
|
+
type: "update",
|
|
2978
|
+
updates: message.data,
|
|
2979
|
+
user: state.users[message.actor]
|
|
2980
|
+
};
|
|
2981
|
+
}
|
|
2982
|
+
|
|
2983
|
+
function onUserLeftMessage(message) {
|
|
2984
|
+
var userLeftMessage = message;
|
|
2985
|
+
var user = state.users[userLeftMessage.actor];
|
|
2986
|
+
|
|
2987
|
+
if (user) {
|
|
2988
|
+
delete state.users[userLeftMessage.actor];
|
|
2989
|
+
return {
|
|
2990
|
+
type: "leave",
|
|
2991
|
+
user: user
|
|
2992
|
+
};
|
|
2993
|
+
}
|
|
2994
|
+
|
|
2995
|
+
return null;
|
|
2996
|
+
}
|
|
2997
|
+
|
|
2998
|
+
function onRoomStateMessage(message) {
|
|
2999
|
+
var newUsers = {};
|
|
3000
|
+
|
|
3001
|
+
for (var _key6 in message.users) {
|
|
3002
|
+
var _connectionId = Number.parseInt(_key6);
|
|
3003
|
+
|
|
3004
|
+
var user = message.users[_key6];
|
|
3005
|
+
newUsers[_connectionId] = {
|
|
3006
|
+
connectionId: _connectionId,
|
|
3007
|
+
info: user.info,
|
|
3008
|
+
id: user.id
|
|
3009
|
+
};
|
|
3010
|
+
}
|
|
3011
|
+
|
|
3012
|
+
state.users = newUsers;
|
|
3013
|
+
return {
|
|
3014
|
+
type: "reset"
|
|
3015
|
+
};
|
|
3016
|
+
}
|
|
3017
|
+
|
|
3018
|
+
function onNavigatorOnline() {
|
|
3019
|
+
if (state.connection.state === "unavailable") {
|
|
3020
|
+
reconnect();
|
|
3021
|
+
}
|
|
3022
|
+
}
|
|
3023
|
+
|
|
3024
|
+
function onEvent(message) {
|
|
3025
|
+
for (var _iterator8 = _createForOfIteratorHelperLoose(state.listeners.event), _step8; !(_step8 = _iterator8()).done;) {
|
|
3026
|
+
var _listener3 = _step8.value;
|
|
3027
|
+
|
|
3028
|
+
_listener3({
|
|
3029
|
+
connectionId: message.actor,
|
|
3030
|
+
event: message.event
|
|
3031
|
+
});
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
|
|
3035
|
+
function onUserJoinedMessage(message) {
|
|
3036
|
+
state.users[message.actor] = {
|
|
3037
|
+
connectionId: message.actor,
|
|
3038
|
+
info: message.info,
|
|
3039
|
+
id: message.id
|
|
3040
|
+
};
|
|
3041
|
+
|
|
3042
|
+
if (state.me) {
|
|
3043
|
+
state.buffer.messages.push({
|
|
3044
|
+
type: ClientMessageType.UpdatePresence,
|
|
3045
|
+
data: state.me,
|
|
3046
|
+
targetActor: message.actor
|
|
3047
|
+
});
|
|
3048
|
+
tryFlushing();
|
|
3049
|
+
}
|
|
3050
|
+
|
|
3051
|
+
return {
|
|
3052
|
+
type: "enter",
|
|
3053
|
+
user: state.users[message.actor]
|
|
3054
|
+
};
|
|
3055
|
+
}
|
|
3056
|
+
|
|
3057
|
+
function onMessage(event) {
|
|
3058
|
+
if (event.data === "pong") {
|
|
3059
|
+
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3060
|
+
return;
|
|
3061
|
+
}
|
|
3062
|
+
|
|
3063
|
+
var message = JSON.parse(event.data);
|
|
3064
|
+
var subMessages = [];
|
|
3065
|
+
|
|
3066
|
+
if (Array.isArray(message)) {
|
|
3067
|
+
subMessages = message;
|
|
3068
|
+
} else {
|
|
3069
|
+
subMessages.push(message);
|
|
3070
|
+
}
|
|
3071
|
+
|
|
3072
|
+
var updates = {
|
|
3073
|
+
storageUpdates: new Map(),
|
|
3074
|
+
others: []
|
|
3075
|
+
};
|
|
3076
|
+
|
|
3077
|
+
for (var _iterator9 = _createForOfIteratorHelperLoose(subMessages), _step9; !(_step9 = _iterator9()).done;) {
|
|
3078
|
+
var subMessage = _step9.value;
|
|
3079
|
+
|
|
3080
|
+
switch (subMessage.type) {
|
|
3081
|
+
case ServerMessageType.UserJoined:
|
|
3082
|
+
{
|
|
3083
|
+
updates.others.push(onUserJoinedMessage(message));
|
|
3084
|
+
break;
|
|
3085
|
+
}
|
|
3086
|
+
|
|
3087
|
+
case ServerMessageType.UpdatePresence:
|
|
3088
|
+
{
|
|
3089
|
+
updates.others.push(onUpdatePresenceMessage(subMessage));
|
|
3090
|
+
break;
|
|
3091
|
+
}
|
|
3092
|
+
|
|
3093
|
+
case ServerMessageType.Event:
|
|
3094
|
+
{
|
|
3095
|
+
onEvent(subMessage);
|
|
3096
|
+
break;
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
case ServerMessageType.UserLeft:
|
|
3100
|
+
{
|
|
3101
|
+
var _event = onUserLeftMessage(subMessage);
|
|
3102
|
+
|
|
3103
|
+
if (_event) {
|
|
3104
|
+
updates.others.push(_event);
|
|
3105
|
+
}
|
|
3106
|
+
|
|
3107
|
+
break;
|
|
3108
|
+
}
|
|
3109
|
+
|
|
3110
|
+
case ServerMessageType.RoomState:
|
|
3111
|
+
{
|
|
3112
|
+
updates.others.push(onRoomStateMessage(subMessage));
|
|
3113
|
+
break;
|
|
3114
|
+
}
|
|
3115
|
+
|
|
3116
|
+
case ServerMessageType.InitialStorageState:
|
|
3117
|
+
{
|
|
3118
|
+
createOrUpdateRootFromMessage(subMessage);
|
|
3119
|
+
applyAndSendOfflineOps();
|
|
3120
|
+
_getInitialStateResolver == null ? void 0 : _getInitialStateResolver();
|
|
3121
|
+
break;
|
|
3122
|
+
}
|
|
3123
|
+
|
|
3124
|
+
case ServerMessageType.UpdateStorage:
|
|
3125
|
+
{
|
|
3126
|
+
var applyResult = apply(subMessage.ops, false);
|
|
3127
|
+
applyResult.updates.storageUpdates.forEach(function (value, key) {
|
|
3128
|
+
updates.storageUpdates.set(key, mergeStorageUpdates(updates.storageUpdates.get(key), value));
|
|
3129
|
+
});
|
|
3130
|
+
break;
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
}
|
|
3134
|
+
|
|
3135
|
+
notify(updates);
|
|
3136
|
+
}
|
|
3137
|
+
|
|
3138
|
+
function onClose(event) {
|
|
3139
|
+
state.socket = null;
|
|
3140
|
+
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3141
|
+
clearInterval(state.intervalHandles.heartbeat);
|
|
3142
|
+
|
|
3143
|
+
if (state.timeoutHandles.flush) {
|
|
3144
|
+
clearTimeout(state.timeoutHandles.flush);
|
|
3145
|
+
}
|
|
3146
|
+
|
|
3147
|
+
clearTimeout(state.timeoutHandles.reconnect);
|
|
3148
|
+
state.users = {};
|
|
3149
|
+
notify({
|
|
3150
|
+
others: [{
|
|
3151
|
+
type: "reset"
|
|
3152
|
+
}]
|
|
3153
|
+
});
|
|
3154
|
+
|
|
3155
|
+
if (event.code >= 4000 && event.code <= 4100) {
|
|
3156
|
+
updateConnection({
|
|
3157
|
+
state: "failed"
|
|
3158
|
+
});
|
|
3159
|
+
var error = new LiveblocksError(event.reason, event.code);
|
|
3160
|
+
|
|
3161
|
+
for (var _iterator10 = _createForOfIteratorHelperLoose(state.listeners.error), _step10; !(_step10 = _iterator10()).done;) {
|
|
3162
|
+
var _listener4 = _step10.value;
|
|
3163
|
+
|
|
3164
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3165
|
+
console.error("Connection to Liveblocks websocket server closed. Reason: " + error.message + " (code: " + error.code + ")");
|
|
3166
|
+
}
|
|
3167
|
+
|
|
3168
|
+
_listener4(error);
|
|
3169
|
+
}
|
|
3170
|
+
} else if (event.wasClean === false) {
|
|
3171
|
+
state.numberOfRetry++;
|
|
3172
|
+
|
|
3173
|
+
var _delay = getRetryDelay();
|
|
3174
|
+
|
|
3175
|
+
if (process.env.NODE_ENV !== "production") {
|
|
3176
|
+
console.warn("Connection to Liveblocks websocket server closed (code: " + event.code + "). Retrying in " + _delay + "ms.");
|
|
3177
|
+
}
|
|
3178
|
+
|
|
3179
|
+
updateConnection({
|
|
3180
|
+
state: "unavailable"
|
|
3181
|
+
});
|
|
3182
|
+
state.timeoutHandles.reconnect = effects.scheduleReconnect(_delay);
|
|
3183
|
+
} else {
|
|
3184
|
+
updateConnection({
|
|
3185
|
+
state: "closed"
|
|
3186
|
+
});
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
|
|
3190
|
+
function updateConnection(connection) {
|
|
3191
|
+
state.connection = connection;
|
|
3192
|
+
|
|
3193
|
+
for (var _iterator11 = _createForOfIteratorHelperLoose(state.listeners.connection), _step11; !(_step11 = _iterator11()).done;) {
|
|
3194
|
+
var _listener5 = _step11.value;
|
|
3195
|
+
|
|
3196
|
+
_listener5(connection.state);
|
|
3197
|
+
}
|
|
3198
|
+
}
|
|
3199
|
+
|
|
3200
|
+
function getRetryDelay() {
|
|
3201
|
+
return BACKOFF_RETRY_DELAYS[state.numberOfRetry < BACKOFF_RETRY_DELAYS.length ? state.numberOfRetry : BACKOFF_RETRY_DELAYS.length - 1];
|
|
3202
|
+
}
|
|
3203
|
+
|
|
3204
|
+
function onError() {}
|
|
3205
|
+
|
|
3206
|
+
function onOpen() {
|
|
3207
|
+
clearInterval(state.intervalHandles.heartbeat);
|
|
3208
|
+
state.intervalHandles.heartbeat = effects.startHeartbeatInterval();
|
|
3209
|
+
|
|
3210
|
+
if (state.connection.state === "connecting") {
|
|
3211
|
+
updateConnection(_extends({}, state.connection, {
|
|
3212
|
+
state: "open"
|
|
3213
|
+
}));
|
|
3214
|
+
state.numberOfRetry = 0;
|
|
3215
|
+
|
|
3216
|
+
if (state.lastConnectionId !== undefined) {
|
|
3217
|
+
state.buffer.presence = state.me;
|
|
3218
|
+
tryFlushing();
|
|
3219
|
+
}
|
|
3220
|
+
|
|
3221
|
+
state.lastConnectionId = state.connection.id;
|
|
3222
|
+
|
|
3223
|
+
if (state.root) {
|
|
3224
|
+
state.buffer.messages.push({
|
|
3225
|
+
type: ClientMessageType.FetchStorage
|
|
3226
|
+
});
|
|
3227
|
+
}
|
|
3228
|
+
|
|
3229
|
+
tryFlushing();
|
|
3230
|
+
}
|
|
3231
|
+
}
|
|
3232
|
+
|
|
3233
|
+
function heartbeat() {
|
|
3234
|
+
if (state.socket == null) {
|
|
3235
|
+
return;
|
|
3236
|
+
}
|
|
3237
|
+
|
|
3238
|
+
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3239
|
+
state.timeoutHandles.pongTimeout = effects.schedulePongTimeout();
|
|
3240
|
+
|
|
3241
|
+
if (state.socket.readyState === state.socket.OPEN) {
|
|
3242
|
+
state.socket.send("ping");
|
|
3243
|
+
}
|
|
3244
|
+
}
|
|
3245
|
+
|
|
3246
|
+
function pongTimeout() {
|
|
3247
|
+
reconnect();
|
|
3248
|
+
}
|
|
3249
|
+
|
|
3250
|
+
function reconnect() {
|
|
3251
|
+
if (state.socket) {
|
|
3252
|
+
state.socket.removeEventListener("open", onOpen);
|
|
3253
|
+
state.socket.removeEventListener("message", onMessage);
|
|
3254
|
+
state.socket.removeEventListener("close", onClose);
|
|
3255
|
+
state.socket.removeEventListener("error", onError);
|
|
3256
|
+
state.socket.close();
|
|
3257
|
+
state.socket = null;
|
|
3258
|
+
}
|
|
3259
|
+
|
|
3260
|
+
updateConnection({
|
|
3261
|
+
state: "unavailable"
|
|
3262
|
+
});
|
|
3263
|
+
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3264
|
+
|
|
3265
|
+
if (state.timeoutHandles.flush) {
|
|
3266
|
+
clearTimeout(state.timeoutHandles.flush);
|
|
3267
|
+
}
|
|
3268
|
+
|
|
3269
|
+
clearTimeout(state.timeoutHandles.reconnect);
|
|
3270
|
+
clearInterval(state.intervalHandles.heartbeat);
|
|
3271
|
+
connect();
|
|
3272
|
+
}
|
|
3273
|
+
|
|
3274
|
+
function applyAndSendOfflineOps() {
|
|
3275
|
+
if (state.offlineOperations.size === 0) {
|
|
3276
|
+
return;
|
|
3277
|
+
}
|
|
3278
|
+
|
|
3279
|
+
var messages = [];
|
|
3280
|
+
var ops = Array.from(state.offlineOperations.values());
|
|
3281
|
+
var result = apply(ops, true);
|
|
3282
|
+
messages.push({
|
|
3283
|
+
type: ClientMessageType.UpdateStorage,
|
|
3284
|
+
ops: ops
|
|
3285
|
+
});
|
|
3286
|
+
notify(result.updates);
|
|
3287
|
+
effects.send(messages);
|
|
3288
|
+
}
|
|
3289
|
+
|
|
3290
|
+
function tryFlushing() {
|
|
3291
|
+
var storageOps = state.buffer.storageOperations;
|
|
3292
|
+
|
|
3293
|
+
if (storageOps.length > 0) {
|
|
3294
|
+
storageOps.forEach(function (op) {
|
|
3295
|
+
state.offlineOperations.set(op.opId, op);
|
|
3296
|
+
});
|
|
3297
|
+
}
|
|
3298
|
+
|
|
3299
|
+
if (state.socket == null || state.socket.readyState !== state.socket.OPEN) {
|
|
3300
|
+
state.buffer.storageOperations = [];
|
|
3301
|
+
return;
|
|
3302
|
+
}
|
|
3303
|
+
|
|
3304
|
+
var now = Date.now();
|
|
3305
|
+
var elapsedTime = now - state.lastFlushTime;
|
|
3306
|
+
|
|
3307
|
+
if (elapsedTime > context.throttleDelay) {
|
|
3308
|
+
var _messages = flushDataToMessages(state);
|
|
3309
|
+
|
|
3310
|
+
if (_messages.length === 0) {
|
|
3311
|
+
return;
|
|
3312
|
+
}
|
|
3313
|
+
|
|
3314
|
+
effects.send(_messages);
|
|
3315
|
+
state.buffer = {
|
|
3316
|
+
messages: [],
|
|
3317
|
+
storageOperations: [],
|
|
3318
|
+
presence: null
|
|
3319
|
+
};
|
|
3320
|
+
state.lastFlushTime = now;
|
|
3321
|
+
} else {
|
|
3322
|
+
if (state.timeoutHandles.flush != null) {
|
|
3323
|
+
clearTimeout(state.timeoutHandles.flush);
|
|
3324
|
+
}
|
|
3325
|
+
|
|
3326
|
+
state.timeoutHandles.flush = effects.delayFlush(context.throttleDelay - (now - state.lastFlushTime));
|
|
3327
|
+
}
|
|
3328
|
+
}
|
|
3329
|
+
|
|
3330
|
+
function flushDataToMessages(state) {
|
|
3331
|
+
var messages = [];
|
|
3332
|
+
|
|
3333
|
+
if (state.buffer.presence) {
|
|
3334
|
+
messages.push({
|
|
3335
|
+
type: ClientMessageType.UpdatePresence,
|
|
3336
|
+
data: state.buffer.presence
|
|
3337
|
+
});
|
|
3338
|
+
}
|
|
3339
|
+
|
|
3340
|
+
for (var _iterator12 = _createForOfIteratorHelperLoose(state.buffer.messages), _step12; !(_step12 = _iterator12()).done;) {
|
|
3341
|
+
var event = _step12.value;
|
|
3342
|
+
messages.push(event);
|
|
3343
|
+
}
|
|
3344
|
+
|
|
3345
|
+
if (state.buffer.storageOperations.length > 0) {
|
|
3346
|
+
messages.push({
|
|
3347
|
+
type: ClientMessageType.UpdateStorage,
|
|
3348
|
+
ops: state.buffer.storageOperations
|
|
3349
|
+
});
|
|
3350
|
+
}
|
|
3351
|
+
|
|
3352
|
+
return messages;
|
|
3353
|
+
}
|
|
3354
|
+
|
|
3355
|
+
function disconnect() {
|
|
3356
|
+
if (state.socket) {
|
|
3357
|
+
state.socket.removeEventListener("open", onOpen);
|
|
3358
|
+
state.socket.removeEventListener("message", onMessage);
|
|
3359
|
+
state.socket.removeEventListener("close", onClose);
|
|
3360
|
+
state.socket.removeEventListener("error", onError);
|
|
3361
|
+
state.socket.close();
|
|
3362
|
+
state.socket = null;
|
|
3363
|
+
}
|
|
3364
|
+
|
|
3365
|
+
updateConnection({
|
|
3366
|
+
state: "closed"
|
|
3367
|
+
});
|
|
3368
|
+
|
|
3369
|
+
if (state.timeoutHandles.flush) {
|
|
3370
|
+
clearTimeout(state.timeoutHandles.flush);
|
|
3371
|
+
}
|
|
3372
|
+
|
|
3373
|
+
clearTimeout(state.timeoutHandles.reconnect);
|
|
3374
|
+
clearTimeout(state.timeoutHandles.pongTimeout);
|
|
3375
|
+
clearInterval(state.intervalHandles.heartbeat);
|
|
3376
|
+
state.users = {};
|
|
3377
|
+
notify({
|
|
3378
|
+
others: [{
|
|
3379
|
+
type: "reset"
|
|
3380
|
+
}]
|
|
3381
|
+
});
|
|
3382
|
+
clearListeners();
|
|
3383
|
+
}
|
|
3384
|
+
|
|
3385
|
+
function clearListeners() {
|
|
3386
|
+
for (var _key7 in state.listeners) {
|
|
3387
|
+
state.listeners[_key7] = [];
|
|
3388
|
+
}
|
|
3389
|
+
}
|
|
3390
|
+
|
|
3391
|
+
function getPresence() {
|
|
3392
|
+
return state.me;
|
|
3393
|
+
}
|
|
3394
|
+
|
|
3395
|
+
function getOthers() {
|
|
3396
|
+
return state.others;
|
|
3397
|
+
}
|
|
3398
|
+
|
|
3399
|
+
function broadcastEvent(event, options) {
|
|
3400
|
+
if (options === void 0) {
|
|
3401
|
+
options = {
|
|
3402
|
+
shouldQueueEventIfNotReady: false
|
|
3403
|
+
};
|
|
3404
|
+
}
|
|
3405
|
+
|
|
3406
|
+
if (state.socket == null && options.shouldQueueEventIfNotReady == false) {
|
|
3407
|
+
return;
|
|
3408
|
+
}
|
|
3409
|
+
|
|
3410
|
+
state.buffer.messages.push({
|
|
3411
|
+
type: ClientMessageType.ClientEvent,
|
|
3412
|
+
event: event
|
|
3413
|
+
});
|
|
3414
|
+
tryFlushing();
|
|
3415
|
+
}
|
|
3416
|
+
|
|
3417
|
+
function dispatch(ops) {
|
|
3418
|
+
var _state$buffer$storage;
|
|
3419
|
+
|
|
3420
|
+
(_state$buffer$storage = state.buffer.storageOperations).push.apply(_state$buffer$storage, ops);
|
|
3421
|
+
|
|
3422
|
+
tryFlushing();
|
|
3423
|
+
}
|
|
3424
|
+
|
|
3425
|
+
var _getInitialStatePromise = null;
|
|
3426
|
+
var _getInitialStateResolver = null;
|
|
3427
|
+
|
|
3428
|
+
function getStorage() {
|
|
3429
|
+
if (state.root) {
|
|
3430
|
+
return new Promise(function (resolve) {
|
|
3431
|
+
return resolve({
|
|
3432
|
+
root: state.root
|
|
3433
|
+
});
|
|
3434
|
+
});
|
|
3435
|
+
}
|
|
3436
|
+
|
|
3437
|
+
if (_getInitialStatePromise == null) {
|
|
3438
|
+
state.buffer.messages.push({
|
|
3439
|
+
type: ClientMessageType.FetchStorage
|
|
3440
|
+
});
|
|
3441
|
+
tryFlushing();
|
|
3442
|
+
_getInitialStatePromise = new Promise(function (resolve) {
|
|
3443
|
+
return _getInitialStateResolver = resolve;
|
|
3444
|
+
});
|
|
3445
|
+
}
|
|
3446
|
+
|
|
3447
|
+
return _getInitialStatePromise.then(function () {
|
|
3448
|
+
return {
|
|
3449
|
+
root: state.root
|
|
3450
|
+
};
|
|
3451
|
+
});
|
|
3452
|
+
}
|
|
3453
|
+
|
|
3454
|
+
function undo() {
|
|
3455
|
+
if (state.isBatching) {
|
|
3456
|
+
throw new Error("undo is not allowed during a batch");
|
|
3457
|
+
}
|
|
3458
|
+
|
|
3459
|
+
var historyItem = state.undoStack.pop();
|
|
3460
|
+
|
|
3461
|
+
if (historyItem == null) {
|
|
3462
|
+
return;
|
|
3463
|
+
}
|
|
3464
|
+
|
|
3465
|
+
state.isHistoryPaused = false;
|
|
3466
|
+
var result = apply(historyItem, true);
|
|
3467
|
+
notify(result.updates);
|
|
3468
|
+
state.redoStack.push(result.reverse);
|
|
3469
|
+
|
|
3470
|
+
for (var _iterator13 = _createForOfIteratorHelperLoose(historyItem), _step13; !(_step13 = _iterator13()).done;) {
|
|
3471
|
+
var op = _step13.value;
|
|
3472
|
+
|
|
3473
|
+
if (op.type !== "presence") {
|
|
3474
|
+
state.buffer.storageOperations.push(op);
|
|
3475
|
+
}
|
|
3476
|
+
}
|
|
3477
|
+
|
|
3478
|
+
tryFlushing();
|
|
3479
|
+
}
|
|
3480
|
+
|
|
3481
|
+
function redo() {
|
|
3482
|
+
if (state.isBatching) {
|
|
3483
|
+
throw new Error("redo is not allowed during a batch");
|
|
3484
|
+
}
|
|
3485
|
+
|
|
3486
|
+
var historyItem = state.redoStack.pop();
|
|
3487
|
+
|
|
3488
|
+
if (historyItem == null) {
|
|
3489
|
+
return;
|
|
3490
|
+
}
|
|
3491
|
+
|
|
3492
|
+
state.isHistoryPaused = false;
|
|
3493
|
+
var result = apply(historyItem, true);
|
|
3494
|
+
notify(result.updates);
|
|
3495
|
+
state.undoStack.push(result.reverse);
|
|
3496
|
+
|
|
3497
|
+
for (var _iterator14 = _createForOfIteratorHelperLoose(historyItem), _step14; !(_step14 = _iterator14()).done;) {
|
|
3498
|
+
var op = _step14.value;
|
|
3499
|
+
|
|
3500
|
+
if (op.type !== "presence") {
|
|
3501
|
+
state.buffer.storageOperations.push(op);
|
|
3502
|
+
}
|
|
3503
|
+
}
|
|
3504
|
+
|
|
3505
|
+
tryFlushing();
|
|
3506
|
+
}
|
|
3507
|
+
|
|
3508
|
+
function batch(callback) {
|
|
3509
|
+
if (state.isBatching) {
|
|
3510
|
+
throw new Error("batch should not be called during a batch");
|
|
3511
|
+
}
|
|
3512
|
+
|
|
3513
|
+
state.isBatching = true;
|
|
3514
|
+
|
|
3515
|
+
try {
|
|
3516
|
+
callback();
|
|
3517
|
+
} finally {
|
|
3518
|
+
state.isBatching = false;
|
|
3519
|
+
|
|
3520
|
+
if (state.batch.reverseOps.length > 0) {
|
|
3521
|
+
addToUndoStack(state.batch.reverseOps);
|
|
3522
|
+
}
|
|
3523
|
+
|
|
3524
|
+
if (state.batch.ops.length > 0) {
|
|
3525
|
+
state.redoStack = [];
|
|
3526
|
+
}
|
|
3527
|
+
|
|
3528
|
+
if (state.batch.ops.length > 0) {
|
|
3529
|
+
dispatch(state.batch.ops);
|
|
3530
|
+
}
|
|
3531
|
+
|
|
3532
|
+
notify(state.batch.updates);
|
|
3533
|
+
state.batch = {
|
|
3534
|
+
ops: [],
|
|
3535
|
+
reverseOps: [],
|
|
3536
|
+
updates: {
|
|
3537
|
+
others: [],
|
|
3538
|
+
storageUpdates: new Map(),
|
|
3539
|
+
presence: false
|
|
3540
|
+
}
|
|
3541
|
+
};
|
|
3542
|
+
tryFlushing();
|
|
3543
|
+
}
|
|
3544
|
+
}
|
|
3545
|
+
|
|
3546
|
+
function pauseHistory() {
|
|
3547
|
+
state.pausedHistory = [];
|
|
3548
|
+
state.isHistoryPaused = true;
|
|
3549
|
+
}
|
|
3550
|
+
|
|
3551
|
+
function resumeHistory() {
|
|
3552
|
+
state.isHistoryPaused = false;
|
|
3553
|
+
|
|
3554
|
+
if (state.pausedHistory.length > 0) {
|
|
3555
|
+
addToUndoStack(state.pausedHistory);
|
|
3556
|
+
}
|
|
3557
|
+
|
|
3558
|
+
state.pausedHistory = [];
|
|
3559
|
+
}
|
|
3560
|
+
|
|
3561
|
+
function simulateSocketClose() {
|
|
3562
|
+
if (state.socket) {
|
|
3563
|
+
state.socket.close();
|
|
3564
|
+
}
|
|
3565
|
+
}
|
|
3566
|
+
|
|
3567
|
+
function simulateSendCloseEvent(event) {
|
|
3568
|
+
if (state.socket) {
|
|
3569
|
+
onClose(event);
|
|
3570
|
+
}
|
|
3571
|
+
}
|
|
3572
|
+
|
|
3573
|
+
return {
|
|
3574
|
+
onClose: onClose,
|
|
3575
|
+
onMessage: onMessage,
|
|
3576
|
+
authenticationSuccess: authenticationSuccess,
|
|
3577
|
+
heartbeat: heartbeat,
|
|
3578
|
+
onNavigatorOnline: onNavigatorOnline,
|
|
3579
|
+
simulateSocketClose: simulateSocketClose,
|
|
3580
|
+
simulateSendCloseEvent: simulateSendCloseEvent,
|
|
3581
|
+
onVisibilityChange: onVisibilityChange,
|
|
3582
|
+
getUndoStack: function getUndoStack() {
|
|
3583
|
+
return state.undoStack;
|
|
3584
|
+
},
|
|
3585
|
+
getItemsCount: function getItemsCount() {
|
|
3586
|
+
return state.items.size;
|
|
3587
|
+
},
|
|
3588
|
+
connect: connect,
|
|
3589
|
+
disconnect: disconnect,
|
|
3590
|
+
subscribe: subscribe,
|
|
3591
|
+
unsubscribe: unsubscribe,
|
|
3592
|
+
updatePresence: updatePresence,
|
|
3593
|
+
broadcastEvent: broadcastEvent,
|
|
3594
|
+
batch: batch,
|
|
3595
|
+
undo: undo,
|
|
3596
|
+
redo: redo,
|
|
3597
|
+
pauseHistory: pauseHistory,
|
|
3598
|
+
resumeHistory: resumeHistory,
|
|
3599
|
+
getStorage: getStorage,
|
|
3600
|
+
selectors: {
|
|
3601
|
+
getConnectionState: getConnectionState,
|
|
3602
|
+
getSelf: getSelf,
|
|
3603
|
+
getPresence: getPresence,
|
|
3604
|
+
getOthers: getOthers
|
|
3605
|
+
}
|
|
3606
|
+
};
|
|
3607
|
+
}
|
|
3608
|
+
function defaultState(me, defaultStorageRoot) {
|
|
3609
|
+
return {
|
|
3610
|
+
connection: {
|
|
3611
|
+
state: "closed"
|
|
3612
|
+
},
|
|
3613
|
+
lastConnectionId: null,
|
|
3614
|
+
socket: null,
|
|
3615
|
+
listeners: {
|
|
3616
|
+
event: [],
|
|
3617
|
+
others: [],
|
|
3618
|
+
"my-presence": [],
|
|
3619
|
+
error: [],
|
|
3620
|
+
connection: [],
|
|
3621
|
+
storage: []
|
|
3622
|
+
},
|
|
3623
|
+
numberOfRetry: 0,
|
|
3624
|
+
lastFlushTime: 0,
|
|
3625
|
+
timeoutHandles: {
|
|
3626
|
+
flush: null,
|
|
3627
|
+
reconnect: 0,
|
|
3628
|
+
pongTimeout: 0
|
|
3629
|
+
},
|
|
3630
|
+
buffer: {
|
|
3631
|
+
presence: me == null ? {} : me,
|
|
3632
|
+
messages: [],
|
|
3633
|
+
storageOperations: []
|
|
3634
|
+
},
|
|
3635
|
+
intervalHandles: {
|
|
3636
|
+
heartbeat: 0
|
|
3637
|
+
},
|
|
3638
|
+
me: me == null ? {} : me,
|
|
3639
|
+
users: {},
|
|
3640
|
+
others: makeOthers({}),
|
|
3641
|
+
defaultStorageRoot: defaultStorageRoot,
|
|
3642
|
+
idFactory: null,
|
|
3643
|
+
clock: 0,
|
|
3644
|
+
opClock: 0,
|
|
3645
|
+
items: new Map(),
|
|
3646
|
+
root: undefined,
|
|
3647
|
+
undoStack: [],
|
|
3648
|
+
redoStack: [],
|
|
3649
|
+
isHistoryPaused: false,
|
|
3650
|
+
pausedHistory: [],
|
|
3651
|
+
isBatching: false,
|
|
3652
|
+
batch: {
|
|
3653
|
+
ops: [],
|
|
3654
|
+
updates: {
|
|
3655
|
+
storageUpdates: new Map(),
|
|
3656
|
+
presence: false,
|
|
3657
|
+
others: []
|
|
3658
|
+
},
|
|
3659
|
+
reverseOps: []
|
|
3660
|
+
},
|
|
3661
|
+
offlineOperations: new Map()
|
|
3662
|
+
};
|
|
3663
|
+
}
|
|
3664
|
+
function createRoom(options, context) {
|
|
3665
|
+
var state = defaultState(options.defaultPresence, options.defaultStorageRoot);
|
|
3666
|
+
var machine = makeStateMachine(state, context);
|
|
3667
|
+
var room = {
|
|
3668
|
+
id: context.room,
|
|
3669
|
+
getConnectionState: machine.selectors.getConnectionState,
|
|
3670
|
+
getSelf: machine.selectors.getSelf,
|
|
3671
|
+
subscribe: machine.subscribe,
|
|
3672
|
+
unsubscribe: machine.unsubscribe,
|
|
3673
|
+
getPresence: machine.selectors.getPresence,
|
|
3674
|
+
updatePresence: machine.updatePresence,
|
|
3675
|
+
getOthers: machine.selectors.getOthers,
|
|
3676
|
+
broadcastEvent: machine.broadcastEvent,
|
|
3677
|
+
getStorage: machine.getStorage,
|
|
3678
|
+
batch: machine.batch,
|
|
3679
|
+
history: {
|
|
3680
|
+
undo: machine.undo,
|
|
3681
|
+
redo: machine.redo,
|
|
3682
|
+
pause: machine.pauseHistory,
|
|
3683
|
+
resume: machine.resumeHistory
|
|
3684
|
+
},
|
|
3685
|
+
internalDevTools: {
|
|
3686
|
+
closeWebsocket: machine.simulateSocketClose,
|
|
3687
|
+
sendCloseEvent: machine.simulateSendCloseEvent
|
|
3688
|
+
}
|
|
3689
|
+
};
|
|
3690
|
+
return {
|
|
3691
|
+
connect: machine.connect,
|
|
3692
|
+
disconnect: machine.disconnect,
|
|
3693
|
+
onNavigatorOnline: machine.onNavigatorOnline,
|
|
3694
|
+
onVisibilityChange: machine.onVisibilityChange,
|
|
3695
|
+
room: room
|
|
3696
|
+
};
|
|
3697
|
+
}
|
|
3698
|
+
|
|
3699
|
+
var LiveblocksError = function (_Error) {
|
|
3700
|
+
_inheritsLoose(LiveblocksError, _Error);
|
|
3701
|
+
|
|
3702
|
+
function LiveblocksError(message, code) {
|
|
3703
|
+
var _this;
|
|
3704
|
+
|
|
3705
|
+
_this = _Error.call(this, message) || this;
|
|
3706
|
+
_this.code = code;
|
|
3707
|
+
return _this;
|
|
3708
|
+
}
|
|
3709
|
+
|
|
3710
|
+
return LiveblocksError;
|
|
3711
|
+
}(_wrapNativeSuper(Error));
|
|
3712
|
+
|
|
3713
|
+
function parseToken(token) {
|
|
3714
|
+
var tokenParts = token.split(".");
|
|
3715
|
+
|
|
3716
|
+
if (tokenParts.length !== 3) {
|
|
3717
|
+
throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
|
|
3718
|
+
}
|
|
3719
|
+
|
|
3720
|
+
var data = JSON.parse(atob(tokenParts[1]));
|
|
3721
|
+
|
|
3722
|
+
if (typeof data.actor !== "number") {
|
|
3723
|
+
throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
|
|
3724
|
+
}
|
|
3725
|
+
|
|
3726
|
+
return data;
|
|
3727
|
+
}
|
|
3728
|
+
|
|
3729
|
+
function prepareCreateWebSocket(liveblocksServer, WebSocketPolyfill) {
|
|
3730
|
+
if (typeof window === "undefined" && WebSocketPolyfill == null) {
|
|
3731
|
+
throw new Error("To use Liveblocks client in a non-dom environment, you need to provide a WebSocket polyfill.");
|
|
3732
|
+
}
|
|
3733
|
+
|
|
3734
|
+
var ws = WebSocketPolyfill || WebSocket;
|
|
3735
|
+
return function (token) {
|
|
3736
|
+
return new ws(liveblocksServer + "/?token=" + token);
|
|
3737
|
+
};
|
|
3738
|
+
}
|
|
3739
|
+
|
|
3740
|
+
function prepareAuthEndpoint(authentication, fetchPolyfill) {
|
|
3741
|
+
if (authentication.type === "public") {
|
|
3742
|
+
if (typeof window === "undefined" && fetchPolyfill == null) {
|
|
3743
|
+
throw new Error("To use Liveblocks client in a non-dom environment with a publicApiKey, you need to provide a fetch polyfill.");
|
|
3744
|
+
}
|
|
3745
|
+
|
|
3746
|
+
return function (room) {
|
|
3747
|
+
return fetchAuthEndpoint(fetchPolyfill || fetch, authentication.url, {
|
|
3748
|
+
room: room,
|
|
3749
|
+
publicApiKey: authentication.publicApiKey
|
|
3750
|
+
});
|
|
3751
|
+
};
|
|
3752
|
+
}
|
|
3753
|
+
|
|
3754
|
+
if (authentication.type === "private") {
|
|
3755
|
+
if (typeof window === "undefined" && fetchPolyfill == null) {
|
|
3756
|
+
throw new Error("To use Liveblocks client in a non-dom environment with a url as auth endpoint, you need to provide a fetch polyfill.");
|
|
3757
|
+
}
|
|
3758
|
+
|
|
3759
|
+
return function (room) {
|
|
3760
|
+
return fetchAuthEndpoint(fetchPolyfill || fetch, authentication.url, {
|
|
3761
|
+
room: room
|
|
3762
|
+
});
|
|
3763
|
+
};
|
|
3764
|
+
}
|
|
3765
|
+
|
|
3766
|
+
if (authentication.type === "custom") {
|
|
3767
|
+
return authentication.callback;
|
|
3768
|
+
}
|
|
3769
|
+
|
|
3770
|
+
throw new Error("Internal error. Unexpected authentication type");
|
|
3771
|
+
}
|
|
3772
|
+
|
|
3773
|
+
function fetchAuthEndpoint(fetch, endpoint, body) {
|
|
3774
|
+
return fetch(endpoint, {
|
|
3775
|
+
method: "POST",
|
|
3776
|
+
headers: {
|
|
3777
|
+
"Content-Type": "application/json"
|
|
3778
|
+
},
|
|
3779
|
+
body: JSON.stringify(body)
|
|
3780
|
+
}).then(function (res) {
|
|
3781
|
+
if (!res.ok) {
|
|
3782
|
+
throw new AuthenticationError("Expected a status 200 but got " + res.status + " when doing a POST request on \"" + endpoint + "\"");
|
|
3783
|
+
}
|
|
3784
|
+
|
|
3785
|
+
return res.json().catch(function (er) {
|
|
3786
|
+
throw new AuthenticationError("Expected a json when doing a POST request on \"" + endpoint + "\". " + er);
|
|
3787
|
+
});
|
|
3788
|
+
}).then(function (authResponse) {
|
|
3789
|
+
if (typeof authResponse.token !== "string") {
|
|
3790
|
+
throw new AuthenticationError("Expected a json with a string token when doing a POST request on \"" + endpoint + "\", but got " + JSON.stringify(authResponse));
|
|
3791
|
+
}
|
|
3792
|
+
|
|
3793
|
+
return authResponse;
|
|
3794
|
+
});
|
|
3795
|
+
}
|
|
3796
|
+
|
|
3797
|
+
var AuthenticationError = function (_Error2) {
|
|
3798
|
+
_inheritsLoose(AuthenticationError, _Error2);
|
|
3799
|
+
|
|
3800
|
+
function AuthenticationError(message) {
|
|
3801
|
+
return _Error2.call(this, message) || this;
|
|
3802
|
+
}
|
|
3803
|
+
|
|
3804
|
+
return AuthenticationError;
|
|
3805
|
+
}(_wrapNativeSuper(Error));
|
|
3806
|
+
|
|
3807
|
+
function createClient(options) {
|
|
3808
|
+
var clientOptions = options;
|
|
3809
|
+
var throttleDelay = getThrottleDelayFromOptions(options);
|
|
3810
|
+
var rooms = new Map();
|
|
3811
|
+
|
|
3812
|
+
function getRoom(roomId) {
|
|
3813
|
+
var internalRoom = rooms.get(roomId);
|
|
3814
|
+
return internalRoom ? internalRoom.room : null;
|
|
3815
|
+
}
|
|
3816
|
+
|
|
3817
|
+
function enter(roomId, options) {
|
|
3818
|
+
if (options === void 0) {
|
|
3819
|
+
options = {};
|
|
3820
|
+
}
|
|
3821
|
+
|
|
3822
|
+
var internalRoom = rooms.get(roomId);
|
|
3823
|
+
|
|
3824
|
+
if (internalRoom) {
|
|
3825
|
+
return internalRoom.room;
|
|
3826
|
+
}
|
|
3827
|
+
|
|
3828
|
+
internalRoom = createRoom({
|
|
3829
|
+
defaultPresence: options.defaultPresence,
|
|
3830
|
+
defaultStorageRoot: options.defaultStorageRoot
|
|
3831
|
+
}, {
|
|
3832
|
+
room: roomId,
|
|
3833
|
+
throttleDelay: throttleDelay,
|
|
3834
|
+
WebSocketPolyfill: clientOptions.WebSocketPolyfill,
|
|
3835
|
+
fetchPolyfill: clientOptions.fetchPolyfill,
|
|
3836
|
+
liveblocksServer: clientOptions.liveblocksServer || "wss://liveblocks.net/v5",
|
|
3837
|
+
authentication: prepareAuthentication(clientOptions)
|
|
3838
|
+
});
|
|
3839
|
+
rooms.set(roomId, internalRoom);
|
|
3840
|
+
|
|
3841
|
+
if (!options.DO_NOT_USE_withoutConnecting) {
|
|
3842
|
+
internalRoom.connect();
|
|
3843
|
+
}
|
|
3844
|
+
|
|
3845
|
+
return internalRoom.room;
|
|
3846
|
+
}
|
|
3847
|
+
|
|
3848
|
+
function leave(roomId) {
|
|
3849
|
+
var room = rooms.get(roomId);
|
|
3850
|
+
|
|
3851
|
+
if (room) {
|
|
3852
|
+
room.disconnect();
|
|
3853
|
+
rooms.delete(roomId);
|
|
3854
|
+
}
|
|
3855
|
+
}
|
|
3856
|
+
|
|
3857
|
+
if (typeof window !== "undefined") {
|
|
3858
|
+
window.addEventListener("online", function () {
|
|
3859
|
+
for (var _iterator = _createForOfIteratorHelperLoose(rooms), _step; !(_step = _iterator()).done;) {
|
|
3860
|
+
var _step$value = _step.value,
|
|
3861
|
+
room = _step$value[1];
|
|
3862
|
+
room.onNavigatorOnline();
|
|
3863
|
+
}
|
|
3864
|
+
});
|
|
3865
|
+
}
|
|
3866
|
+
|
|
3867
|
+
if (typeof document !== "undefined") {
|
|
3868
|
+
document.addEventListener("visibilitychange", function () {
|
|
3869
|
+
for (var _iterator2 = _createForOfIteratorHelperLoose(rooms), _step2; !(_step2 = _iterator2()).done;) {
|
|
3870
|
+
var _step2$value = _step2.value,
|
|
3871
|
+
room = _step2$value[1];
|
|
3872
|
+
room.onVisibilityChange(document.visibilityState);
|
|
3873
|
+
}
|
|
3874
|
+
});
|
|
3875
|
+
}
|
|
3876
|
+
|
|
3877
|
+
return {
|
|
3878
|
+
getRoom: getRoom,
|
|
3879
|
+
enter: enter,
|
|
3880
|
+
leave: leave
|
|
3881
|
+
};
|
|
3882
|
+
}
|
|
3883
|
+
|
|
3884
|
+
function getThrottleDelayFromOptions(options) {
|
|
3885
|
+
if (options.throttle === undefined) {
|
|
3886
|
+
return 100;
|
|
3887
|
+
}
|
|
3888
|
+
|
|
3889
|
+
if (typeof options.throttle !== "number" || options.throttle < 80 || options.throttle > 1000) {
|
|
3890
|
+
throw new Error("throttle should be a number between 80 and 1000.");
|
|
3891
|
+
}
|
|
3892
|
+
|
|
3893
|
+
return options.throttle;
|
|
3894
|
+
}
|
|
3895
|
+
|
|
3896
|
+
function prepareAuthentication(clientOptions) {
|
|
3897
|
+
if (typeof clientOptions.publicApiKey === "string") {
|
|
3898
|
+
return {
|
|
3899
|
+
type: "public",
|
|
3900
|
+
publicApiKey: clientOptions.publicApiKey,
|
|
3901
|
+
url: clientOptions.publicAuthorizeEndpoint || "https://liveblocks.io/api/public/authorize"
|
|
3902
|
+
};
|
|
3903
|
+
} else if (typeof clientOptions.authEndpoint === "string") {
|
|
3904
|
+
return {
|
|
3905
|
+
type: "private",
|
|
3906
|
+
url: clientOptions.authEndpoint
|
|
3907
|
+
};
|
|
3908
|
+
} else if (typeof clientOptions.authEndpoint === "function") {
|
|
3909
|
+
return {
|
|
3910
|
+
type: "custom",
|
|
3911
|
+
callback: clientOptions.authEndpoint
|
|
3912
|
+
};
|
|
3913
|
+
}
|
|
3914
|
+
|
|
3915
|
+
throw new Error("Invalid Liveblocks client options. For more information: https://liveblocks.io/docs/api-reference/liveblocks-client#createClient");
|
|
3916
|
+
}
|
|
3917
|
+
|
|
3918
|
+
function liveObjectToJson(liveObject) {
|
|
3919
|
+
var result = {};
|
|
3920
|
+
var obj = liveObject.toObject();
|
|
3921
|
+
|
|
3922
|
+
for (var _key in obj) {
|
|
3923
|
+
result[_key] = liveNodeToJson(obj[_key]);
|
|
3924
|
+
}
|
|
3925
|
+
|
|
3926
|
+
return result;
|
|
3927
|
+
}
|
|
3928
|
+
|
|
3929
|
+
function liveMapToJson(map) {
|
|
3930
|
+
var result = {};
|
|
3931
|
+
var obj = Object.fromEntries(map);
|
|
3932
|
+
|
|
3933
|
+
for (var _key2 in obj) {
|
|
3934
|
+
result[_key2] = liveNodeToJson(obj[_key2]);
|
|
3935
|
+
}
|
|
3936
|
+
|
|
3937
|
+
return result;
|
|
3938
|
+
}
|
|
3939
|
+
|
|
3940
|
+
function liveListToJson(value) {
|
|
3941
|
+
return value.toArray().map(liveNodeToJson);
|
|
3942
|
+
}
|
|
3943
|
+
|
|
3944
|
+
function liveNodeToJson(value) {
|
|
3945
|
+
if (value instanceof LiveObject) {
|
|
3946
|
+
return liveObjectToJson(value);
|
|
3947
|
+
} else if (value instanceof LiveList) {
|
|
3948
|
+
return liveListToJson(value);
|
|
3949
|
+
} else if (value instanceof LiveMap) {
|
|
3950
|
+
return liveMapToJson(value);
|
|
3951
|
+
} else if (value instanceof LiveRegister) {
|
|
3952
|
+
return value.data;
|
|
3953
|
+
}
|
|
3954
|
+
|
|
3955
|
+
return value;
|
|
3956
|
+
}
|
|
3957
|
+
|
|
3958
|
+
function isPlainObject(obj) {
|
|
3959
|
+
return Object.prototype.toString.call(obj) === "[object Object]";
|
|
3960
|
+
}
|
|
3961
|
+
|
|
3962
|
+
function anyToCrdt(obj) {
|
|
3963
|
+
if (obj == null) {
|
|
3964
|
+
return obj;
|
|
3965
|
+
}
|
|
3966
|
+
|
|
3967
|
+
if (Array.isArray(obj)) {
|
|
3968
|
+
return new LiveList(obj.map(anyToCrdt));
|
|
3969
|
+
}
|
|
3970
|
+
|
|
3971
|
+
if (isPlainObject(obj)) {
|
|
3972
|
+
var init = {};
|
|
3973
|
+
|
|
3974
|
+
for (var _key3 in obj) {
|
|
3975
|
+
init[_key3] = anyToCrdt(obj[_key3]);
|
|
3976
|
+
}
|
|
3977
|
+
|
|
3978
|
+
return new LiveObject(init);
|
|
3979
|
+
}
|
|
3980
|
+
|
|
3981
|
+
return obj;
|
|
3982
|
+
}
|
|
3983
|
+
|
|
3984
|
+
function patchLiveList(liveList, prev, next) {
|
|
3985
|
+
var i = 0;
|
|
3986
|
+
var prevEnd = prev.length - 1;
|
|
3987
|
+
var nextEnd = next.length - 1;
|
|
3988
|
+
var prevNode = prev[0];
|
|
3989
|
+
var nextNode = next[0];
|
|
3990
|
+
|
|
3991
|
+
outer: {
|
|
3992
|
+
while (prevNode === nextNode) {
|
|
3993
|
+
++i;
|
|
3994
|
+
|
|
3995
|
+
if (i > prevEnd || i > nextEnd) {
|
|
3996
|
+
break outer;
|
|
3997
|
+
}
|
|
3998
|
+
|
|
3999
|
+
prevNode = prev[i];
|
|
4000
|
+
nextNode = next[i];
|
|
4001
|
+
}
|
|
4002
|
+
|
|
4003
|
+
prevNode = prev[prevEnd];
|
|
4004
|
+
nextNode = next[nextEnd];
|
|
4005
|
+
|
|
4006
|
+
while (prevNode === nextNode) {
|
|
4007
|
+
prevEnd--;
|
|
4008
|
+
nextEnd--;
|
|
4009
|
+
|
|
4010
|
+
if (i > prevEnd || i > nextEnd) {
|
|
4011
|
+
break outer;
|
|
4012
|
+
}
|
|
4013
|
+
|
|
4014
|
+
prevNode = prev[prevEnd];
|
|
4015
|
+
nextNode = next[nextEnd];
|
|
4016
|
+
}
|
|
4017
|
+
}
|
|
4018
|
+
|
|
4019
|
+
if (i > prevEnd) {
|
|
4020
|
+
if (i <= nextEnd) {
|
|
4021
|
+
while (i <= nextEnd) {
|
|
4022
|
+
liveList.insert(anyToCrdt(next[i]), i);
|
|
4023
|
+
i++;
|
|
4024
|
+
}
|
|
4025
|
+
}
|
|
4026
|
+
} else if (i > nextEnd) {
|
|
4027
|
+
var localI = i;
|
|
4028
|
+
|
|
4029
|
+
while (localI <= prevEnd) {
|
|
4030
|
+
liveList.delete(i);
|
|
4031
|
+
localI++;
|
|
4032
|
+
}
|
|
4033
|
+
} else {
|
|
4034
|
+
while (i <= prevEnd && i <= nextEnd) {
|
|
4035
|
+
prevNode = prev[i];
|
|
4036
|
+
nextNode = next[i];
|
|
4037
|
+
var liveListNode = liveList.get(i);
|
|
4038
|
+
|
|
4039
|
+
if (liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
|
|
4040
|
+
patchLiveObject(liveListNode, prevNode, nextNode);
|
|
4041
|
+
} else {
|
|
4042
|
+
liveList.delete(i);
|
|
4043
|
+
liveList.insert(anyToCrdt(nextNode), i);
|
|
4044
|
+
}
|
|
4045
|
+
|
|
4046
|
+
i++;
|
|
4047
|
+
}
|
|
4048
|
+
|
|
4049
|
+
while (i <= nextEnd) {
|
|
4050
|
+
liveList.insert(anyToCrdt(next[i]), i);
|
|
4051
|
+
i++;
|
|
4052
|
+
}
|
|
4053
|
+
|
|
4054
|
+
while (i <= prevEnd) {
|
|
4055
|
+
liveList.delete(i);
|
|
4056
|
+
i++;
|
|
4057
|
+
}
|
|
4058
|
+
}
|
|
4059
|
+
}
|
|
4060
|
+
function patchLiveObjectKey(liveObject, key, prev, next) {
|
|
4061
|
+
if (process.env.NODE_ENV !== "production") {
|
|
4062
|
+
var nonSerializableValue = findNonSerializableValue(next);
|
|
4063
|
+
|
|
4064
|
+
if (nonSerializableValue) {
|
|
4065
|
+
console.error("New state path: '" + nonSerializableValue.path + "' value: '" + nonSerializableValue.value + "' is not serializable.\nOnly serializable value can be synced with Liveblocks.");
|
|
4066
|
+
return;
|
|
4067
|
+
}
|
|
4068
|
+
}
|
|
4069
|
+
|
|
4070
|
+
var value = liveObject.get(key);
|
|
4071
|
+
|
|
4072
|
+
if (next === undefined) {
|
|
4073
|
+
liveObject.delete(key);
|
|
4074
|
+
} else if (value === undefined) {
|
|
4075
|
+
liveObject.set(key, anyToCrdt(next));
|
|
4076
|
+
} else if (prev === next) {
|
|
4077
|
+
return;
|
|
4078
|
+
} else if (value instanceof LiveList && Array.isArray(prev) && Array.isArray(next)) {
|
|
4079
|
+
patchLiveList(value, prev, next);
|
|
4080
|
+
} else if (value instanceof LiveObject && isPlainObject(prev) && isPlainObject(next)) {
|
|
4081
|
+
patchLiveObject(value, prev, next);
|
|
4082
|
+
} else {
|
|
4083
|
+
liveObject.set(key, anyToCrdt(next));
|
|
4084
|
+
}
|
|
4085
|
+
}
|
|
4086
|
+
function patchLiveObject(root, prev, next) {
|
|
4087
|
+
var updates = {};
|
|
4088
|
+
|
|
4089
|
+
for (var _key4 in next) {
|
|
4090
|
+
patchLiveObjectKey(root, _key4, prev[_key4], next[_key4]);
|
|
4091
|
+
}
|
|
4092
|
+
|
|
4093
|
+
for (var _key5 in prev) {
|
|
4094
|
+
if (next[_key5] === undefined) {
|
|
4095
|
+
root.delete(_key5);
|
|
4096
|
+
}
|
|
4097
|
+
}
|
|
4098
|
+
|
|
4099
|
+
if (Object.keys(updates).length > 0) {
|
|
4100
|
+
root.update(updates);
|
|
4101
|
+
}
|
|
4102
|
+
}
|
|
4103
|
+
|
|
4104
|
+
function getParentsPath(node) {
|
|
4105
|
+
var path = [];
|
|
4106
|
+
|
|
4107
|
+
while (node._parentKey != null && node._parent != null) {
|
|
4108
|
+
if (node._parent instanceof LiveList) {
|
|
4109
|
+
path.push(node._parent._indexOfPosition(node._parentKey));
|
|
4110
|
+
} else {
|
|
4111
|
+
path.push(node._parentKey);
|
|
4112
|
+
}
|
|
4113
|
+
|
|
4114
|
+
node = node._parent;
|
|
4115
|
+
}
|
|
4116
|
+
|
|
4117
|
+
return path;
|
|
4118
|
+
}
|
|
4119
|
+
|
|
4120
|
+
function patchImmutableObject(state, updates) {
|
|
4121
|
+
return updates.reduce(function (state, update) {
|
|
4122
|
+
return patchImmutableObjectWithUpdate(state, update);
|
|
4123
|
+
}, state);
|
|
4124
|
+
}
|
|
4125
|
+
|
|
4126
|
+
function patchImmutableObjectWithUpdate(state, update) {
|
|
4127
|
+
var path = getParentsPath(update.node);
|
|
4128
|
+
return patchImmutableNode(state, path, update);
|
|
4129
|
+
}
|
|
4130
|
+
|
|
4131
|
+
function patchImmutableNode(state, path, update) {
|
|
4132
|
+
var pathItem = path.pop();
|
|
4133
|
+
|
|
4134
|
+
if (pathItem === undefined) {
|
|
4135
|
+
switch (update.type) {
|
|
4136
|
+
case "LiveObject":
|
|
4137
|
+
{
|
|
4138
|
+
if (typeof state !== "object") {
|
|
4139
|
+
throw new Error("Internal: received update on LiveObject but state was not an object");
|
|
4140
|
+
}
|
|
4141
|
+
|
|
4142
|
+
var newState = Object.assign({}, state);
|
|
4143
|
+
|
|
4144
|
+
for (var _key6 in update.updates) {
|
|
4145
|
+
var _update$updates$_key, _update$updates$_key2;
|
|
4146
|
+
|
|
4147
|
+
if (((_update$updates$_key = update.updates[_key6]) == null ? void 0 : _update$updates$_key.type) === "update") {
|
|
4148
|
+
newState[_key6] = liveNodeToJson(update.node.get(_key6));
|
|
4149
|
+
} else if (((_update$updates$_key2 = update.updates[_key6]) == null ? void 0 : _update$updates$_key2.type) === "delete") {
|
|
4150
|
+
delete newState[_key6];
|
|
4151
|
+
}
|
|
4152
|
+
}
|
|
4153
|
+
|
|
4154
|
+
return newState;
|
|
4155
|
+
}
|
|
4156
|
+
|
|
4157
|
+
case "LiveList":
|
|
4158
|
+
{
|
|
4159
|
+
if (Array.isArray(state) === false) {
|
|
4160
|
+
throw new Error("Internal: received update on LiveList but state was not an array");
|
|
4161
|
+
}
|
|
4162
|
+
|
|
4163
|
+
var _newState = state.map(function (x) {
|
|
4164
|
+
return x;
|
|
4165
|
+
});
|
|
4166
|
+
|
|
4167
|
+
for (var _iterator = _createForOfIteratorHelperLoose(update.updates), _step; !(_step = _iterator()).done;) {
|
|
4168
|
+
var listUpdate = _step.value;
|
|
4169
|
+
|
|
4170
|
+
if (listUpdate.type === "insert") {
|
|
4171
|
+
if (listUpdate.index === _newState.length) {
|
|
4172
|
+
_newState.push(liveNodeToJson(listUpdate.item));
|
|
4173
|
+
} else {
|
|
4174
|
+
_newState = [].concat(_newState.slice(0, listUpdate.index), [liveNodeToJson(listUpdate.item)], _newState.slice(listUpdate.index));
|
|
4175
|
+
}
|
|
4176
|
+
} else if (listUpdate.type === "delete") {
|
|
4177
|
+
_newState.splice(listUpdate.index, 1);
|
|
4178
|
+
} else if (listUpdate.type === "move") {
|
|
4179
|
+
if (listUpdate.previousIndex > listUpdate.index) {
|
|
4180
|
+
_newState = [].concat(_newState.slice(0, listUpdate.index), [liveNodeToJson(listUpdate.item)], _newState.slice(listUpdate.index, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1));
|
|
4181
|
+
} else {
|
|
4182
|
+
_newState = [].concat(_newState.slice(0, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), [liveNodeToJson(listUpdate.item)], _newState.slice(listUpdate.index + 1));
|
|
4183
|
+
}
|
|
4184
|
+
}
|
|
4185
|
+
}
|
|
4186
|
+
|
|
4187
|
+
return _newState;
|
|
4188
|
+
}
|
|
4189
|
+
|
|
4190
|
+
case "LiveMap":
|
|
4191
|
+
{
|
|
4192
|
+
if (typeof state !== "object") {
|
|
4193
|
+
throw new Error("Internal: received update on LiveMap but state was not an object");
|
|
4194
|
+
}
|
|
4195
|
+
|
|
4196
|
+
var _newState2 = Object.assign({}, state);
|
|
4197
|
+
|
|
4198
|
+
for (var _key7 in update.updates) {
|
|
4199
|
+
var _update$updates$_key3, _update$updates$_key4;
|
|
4200
|
+
|
|
4201
|
+
if (((_update$updates$_key3 = update.updates[_key7]) == null ? void 0 : _update$updates$_key3.type) === "update") {
|
|
4202
|
+
_newState2[_key7] = liveNodeToJson(update.node.get(_key7));
|
|
4203
|
+
} else if (((_update$updates$_key4 = update.updates[_key7]) == null ? void 0 : _update$updates$_key4.type) === "delete") {
|
|
4204
|
+
delete _newState2[_key7];
|
|
4205
|
+
}
|
|
4206
|
+
}
|
|
4207
|
+
|
|
4208
|
+
return _newState2;
|
|
4209
|
+
}
|
|
4210
|
+
}
|
|
4211
|
+
}
|
|
4212
|
+
|
|
4213
|
+
if (Array.isArray(state)) {
|
|
4214
|
+
var newArray = [].concat(state);
|
|
4215
|
+
newArray[pathItem] = patchImmutableNode(state[pathItem], path, update);
|
|
4216
|
+
return newArray;
|
|
4217
|
+
} else {
|
|
4218
|
+
var _extends2;
|
|
4219
|
+
|
|
4220
|
+
return _extends({}, state, (_extends2 = {}, _extends2[pathItem] = patchImmutableNode(state[pathItem], path, update), _extends2));
|
|
4221
|
+
}
|
|
4222
|
+
}
|
|
4223
|
+
|
|
4224
|
+
var internals = {
|
|
4225
|
+
liveObjectToJson: liveObjectToJson,
|
|
4226
|
+
liveNodeToJson: liveNodeToJson,
|
|
4227
|
+
patchLiveList: patchLiveList,
|
|
4228
|
+
patchImmutableObject: patchImmutableObject,
|
|
4229
|
+
patchLiveObject: patchLiveObject,
|
|
4230
|
+
patchLiveObjectKey: patchLiveObjectKey
|
|
4231
|
+
};
|
|
4232
|
+
|
|
4233
|
+
exports.LiveList = LiveList;
|
|
4234
|
+
exports.LiveMap = LiveMap;
|
|
4235
|
+
exports.LiveObject = LiveObject;
|
|
4236
|
+
exports.createClient = createClient;
|
|
4237
|
+
exports.internals = internals;
|