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