@liveblocks/client 0.16.15 → 0.17.0-test1

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.
Files changed (10) hide show
  1. package/index.d.ts +26 -7
  2. package/index.js +1230 -830
  3. package/index.mjs +1030 -782
  4. package/internal.d.ts +271 -251
  5. package/internal.js +314 -168
  6. package/internal.mjs +265 -130
  7. package/package.json +15 -10
  8. package/shared.d.ts +973 -628
  9. package/shared.js +2568 -1331
  10. package/shared.mjs +1989 -1210
package/internal.js CHANGED
@@ -1,186 +1,332 @@
1
1
  "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: !0
5
- });
6
-
7
- var LiveObject = require("./shared.js");
8
-
2
+ Object.defineProperty(exports, "__esModule", { value: !0 });
3
+ var Json = require("./shared.js");
9
4
  function lsonObjectToJson(obj) {
10
- var result = {};
11
- for (var _key in obj) {
12
- var val = obj[_key];
13
- void 0 !== val && (result[_key] = lsonToJson(val));
14
- }
15
- return result;
16
- }
17
-
18
- function lsonListToJson(value) {
19
- return value.map(lsonToJson);
20
- }
21
-
22
- function lsonToJson(value) {
23
- if (value instanceof LiveObject.LiveObject) return lsonObjectToJson(value.toObject());
24
- if (value instanceof LiveObject.LiveList) return function(value) {
25
- return lsonListToJson(value.toArray());
26
- }(value);
27
- if (value instanceof LiveObject.LiveMap) return function(map) {
28
- for (var _step, result = {}, _iterator = LiveObject._createForOfIteratorHelperLoose(map.entries()); !(_step = _iterator()).done; ) {
29
- var _step$value = _step.value, _key2 = _step$value[0], value = _step$value[1];
30
- result[_key2] = lsonToJson(value);
5
+ var result = {};
6
+ for (var key in obj) {
7
+ var val = obj[key];
8
+ void 0 !== val && (result[key] = lsonToJson(val));
31
9
  }
32
10
  return result;
33
- }(value);
34
- if (value instanceof LiveObject.LiveRegister) return value.data;
35
- if (value instanceof LiveObject.AbstractCrdt) throw new Error("Unhandled subclass of AbstractCrdt encountered");
36
- return Array.isArray(value) ? lsonListToJson(value) : isPlainObject(value) ? lsonObjectToJson(value) : value;
37
11
  }
38
-
39
- function isPlainObject(obj) {
40
- return null !== obj && "[object Object]" === Object.prototype.toString.call(obj);
12
+ function lsonListToJson(value) {
13
+ return value.map(lsonToJson);
41
14
  }
15
+ function lsonToJson(value) {
16
+ return value instanceof Json.LiveObject
17
+ ? lsonObjectToJson(value.toObject())
18
+ : value instanceof Json.LiveList
19
+ ? (function (value) {
20
+ return lsonListToJson(value.toArray());
21
+ })(value)
22
+ : value instanceof Json.LiveMap
23
+ ? (function (map) {
24
+ for (
25
+ var _step,
26
+ result = {},
27
+ _iterator = Json._createForOfIteratorHelperLoose(map.entries());
28
+ !(_step = _iterator()).done;
42
29
 
43
- function anyToCrdt(obj) {
44
- if (null == obj) return obj;
45
- if (Array.isArray(obj)) return new LiveObject.LiveList(obj.map(anyToCrdt));
46
- if (isPlainObject(obj)) {
47
- var init = {};
48
- for (var _key3 in obj) init[_key3] = anyToCrdt(obj[_key3]);
49
- return new LiveObject.LiveObject(init);
50
- }
51
- return obj;
30
+ ) {
31
+ var _step$value = _step.value,
32
+ key = _step$value[0],
33
+ value = _step$value[1];
34
+ result[key] = lsonToJson(value);
35
+ }
36
+ return result;
37
+ })(value)
38
+ : value instanceof Json.LiveRegister
39
+ ? value.data
40
+ : Array.isArray(value)
41
+ ? lsonListToJson(value)
42
+ : Json.isPlainObject(value)
43
+ ? lsonObjectToJson(value)
44
+ : value;
52
45
  }
53
-
54
- function patchLiveObjectKey(liveObject, key, prev, next) {
55
- if ("production" !== process.env.NODE_ENV) {
56
- var nonSerializableValue = LiveObject.findNonSerializableValue(next);
57
- if (nonSerializableValue) return void console.error("New state path: '" + nonSerializableValue.path + "' value: '" + nonSerializableValue.value + "' is not serializable.\nOnly serializable value can be synced with Liveblocks.");
58
- }
59
- var value = liveObject.get(key);
60
- if (void 0 === next) liveObject.delete(key); else if (void 0 === value) liveObject.set(key, anyToCrdt(next)); else {
61
- if (prev === next) return;
62
- value instanceof LiveObject.LiveList && Array.isArray(prev) && Array.isArray(next) ? function(liveList, prev, next) {
63
- var i = 0, prevEnd = prev.length - 1, nextEnd = next.length - 1, prevNode = prev[0], nextNode = next[0];
64
- outer: {
65
- for (;prevNode === nextNode; ) {
66
- if (++i > prevEnd || i > nextEnd) break outer;
67
- prevNode = prev[i], nextNode = next[i];
46
+ function deepLiveify(value) {
47
+ if (Array.isArray(value)) return new Json.LiveList(value.map(deepLiveify));
48
+ if (Json.isPlainObject(value)) {
49
+ var init = {};
50
+ for (var key in value) {
51
+ var val = value[key];
52
+ void 0 !== val && (init[key] = deepLiveify(val));
68
53
  }
69
- for (prevNode = prev[prevEnd], nextNode = next[nextEnd]; prevNode === nextNode; ) {
70
- if (nextEnd--, i > --prevEnd || i > nextEnd) break outer;
71
- prevNode = prev[prevEnd], nextNode = next[nextEnd];
72
- }
73
- }
74
- if (i > prevEnd) {
75
- if (i <= nextEnd) for (;i <= nextEnd; ) liveList.insert(anyToCrdt(next[i]), i),
76
- i++;
77
- } else if (i > nextEnd) for (var localI = i; localI <= prevEnd; ) liveList.delete(i),
78
- localI++; else {
79
- for (;i <= prevEnd && i <= nextEnd; ) {
80
- prevNode = prev[i], nextNode = next[i];
81
- var liveListNode = liveList.get(i);
82
- liveListNode instanceof LiveObject.LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode) ? patchLiveObject(liveListNode, prevNode, nextNode) : liveList.set(i, anyToCrdt(nextNode)),
83
- i++;
84
- }
85
- for (;i <= nextEnd; ) liveList.insert(anyToCrdt(next[i]), i), i++;
86
- for (var _localI = i; _localI <= prevEnd; ) liveList.delete(i), _localI++;
87
- }
88
- }(value, prev, next) : value instanceof LiveObject.LiveObject && isPlainObject(prev) && isPlainObject(next) ? patchLiveObject(value, prev, next) : liveObject.set(key, anyToCrdt(next));
89
- }
54
+ return new Json.LiveObject(init);
55
+ }
56
+ return value;
90
57
  }
58
+ function patchLiveObjectKey(liveObject, key, prev, next) {
59
+ if ("production" !== process.env.NODE_ENV) {
60
+ var nonSerializableValue = Json.findNonSerializableValue(next);
61
+ if (nonSerializableValue)
62
+ return void console.error(
63
+ "New state path: '" +
64
+ nonSerializableValue.path +
65
+ "' value: '" +
66
+ nonSerializableValue.value +
67
+ "' is not serializable.\nOnly serializable value can be synced with Liveblocks."
68
+ );
69
+ }
70
+ var value = liveObject.get(key);
71
+ if (void 0 === next) liveObject.delete(key);
72
+ else if (void 0 === value) liveObject.set(key, deepLiveify(next));
73
+ else {
74
+ if (prev === next) return;
75
+ Json.isLiveList(value) && Array.isArray(prev) && Array.isArray(next)
76
+ ? (function (liveList, prev, next) {
77
+ var i = 0,
78
+ prevEnd = prev.length - 1,
79
+ nextEnd = next.length - 1,
80
+ prevNode = prev[0],
81
+ nextNode = next[0];
82
+ outer: {
83
+ for (; prevNode === nextNode; ) {
84
+ if (++i > prevEnd || i > nextEnd) break outer;
85
+ (prevNode = prev[i]), (nextNode = next[i]);
86
+ }
87
+ for (
88
+ prevNode = prev[prevEnd], nextNode = next[nextEnd];
89
+ prevNode === nextNode;
91
90
 
91
+ ) {
92
+ if ((nextEnd--, i > --prevEnd || i > nextEnd)) break outer;
93
+ (prevNode = prev[prevEnd]), (nextNode = next[nextEnd]);
94
+ }
95
+ }
96
+ if (i > prevEnd) {
97
+ if (i <= nextEnd)
98
+ for (; i <= nextEnd; )
99
+ liveList.insert(deepLiveify(next[i]), i), i++;
100
+ } else if (i > nextEnd)
101
+ for (var localI = i; localI <= prevEnd; )
102
+ liveList.delete(i), localI++;
103
+ else {
104
+ for (; i <= prevEnd && i <= nextEnd; ) {
105
+ (prevNode = prev[i]), (nextNode = next[i]);
106
+ var liveListNode = liveList.get(i);
107
+ Json.isLiveObject(liveListNode) &&
108
+ Json.isPlainObject(prevNode) &&
109
+ Json.isPlainObject(nextNode)
110
+ ? patchLiveObject(liveListNode, prevNode, nextNode)
111
+ : liveList.set(i, deepLiveify(nextNode)),
112
+ i++;
113
+ }
114
+ for (; i <= nextEnd; )
115
+ liveList.insert(deepLiveify(next[i]), i), i++;
116
+ for (var _localI = i; _localI <= prevEnd; )
117
+ liveList.delete(i), _localI++;
118
+ }
119
+ })(value, prev, next)
120
+ : Json.isLiveObject(value) &&
121
+ Json.isPlainObject(prev) &&
122
+ Json.isPlainObject(next)
123
+ ? patchLiveObject(value, prev, next)
124
+ : liveObject.set(key, deepLiveify(next));
125
+ }
126
+ }
92
127
  function patchLiveObject(root, prev, next) {
93
- var updates = {};
94
- for (var _key4 in next) patchLiveObjectKey(root, _key4, prev[_key4], next[_key4]);
95
- for (var _key5 in prev) void 0 === next[_key5] && root.delete(_key5);
96
- Object.keys(updates).length > 0 && root.update(updates);
128
+ var updates = {};
129
+ for (var key in next) patchLiveObjectKey(root, key, prev[key], next[key]);
130
+ for (var _key in prev) void 0 === next[_key] && root.delete(_key);
131
+ Object.keys(updates).length > 0 && root.update(updates);
97
132
  }
98
-
99
133
  function patchImmutableNode(state, path, update) {
100
- var _extends2, pathItem = path.pop();
101
- if (void 0 === pathItem) switch (update.type) {
102
- case "LiveObject":
103
- if ("object" != typeof state) throw new Error("Internal: received update on LiveObject but state was not an object");
104
- var newState = Object.assign({}, state);
105
- for (var _key6 in update.updates) {
106
- var _update$updates$_key, _update$updates$_key2;
107
- if ("update" === (null == (_update$updates$_key = update.updates[_key6]) ? void 0 : _update$updates$_key.type)) {
108
- var val = update.node.get(_key6);
109
- void 0 !== val && (newState[_key6] = lsonToJson(val));
110
- } else "delete" === (null == (_update$updates$_key2 = update.updates[_key6]) ? void 0 : _update$updates$_key2.type) && delete newState[_key6];
111
- }
112
- return newState;
113
-
114
- case "LiveList":
115
- if (!1 === Array.isArray(state)) throw new Error("Internal: received update on LiveList but state was not an array");
116
- for (var _step2, _newState = state.map((function(x) {
117
- return x;
118
- })), _loop = function() {
119
- var listUpdate = _step2.value;
120
- "set" === listUpdate.type ? _newState = _newState.map((function(item, index) {
121
- return index === listUpdate.index ? listUpdate.item : item;
122
- })) : "insert" === listUpdate.type ? listUpdate.index === _newState.length ? _newState.push(lsonToJson(listUpdate.item)) : _newState = [].concat(_newState.slice(0, listUpdate.index), [ lsonToJson(listUpdate.item) ], _newState.slice(listUpdate.index)) : "delete" === listUpdate.type ? _newState.splice(listUpdate.index, 1) : "move" === listUpdate.type && (_newState = listUpdate.previousIndex > listUpdate.index ? [].concat(_newState.slice(0, listUpdate.index), [ lsonToJson(listUpdate.item) ], _newState.slice(listUpdate.index, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1)) : [].concat(_newState.slice(0, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), [ lsonToJson(listUpdate.item) ], _newState.slice(listUpdate.index + 1)));
123
- }, _iterator2 = LiveObject._createForOfIteratorHelperLoose(update.updates); !(_step2 = _iterator2()).done; ) _loop();
124
- return _newState;
134
+ var pathItem = path.pop();
135
+ if (void 0 === pathItem)
136
+ switch (update.type) {
137
+ case "LiveObject":
138
+ if (null === state || "object" != typeof state || Array.isArray(state))
139
+ throw new Error(
140
+ "Internal: received update on LiveObject but state was not an object"
141
+ );
142
+ var newState = Object.assign({}, state);
143
+ for (var key in update.updates) {
144
+ var _update$updates$key, _update$updates$key2;
145
+ if (
146
+ "update" ===
147
+ (null == (_update$updates$key = update.updates[key])
148
+ ? void 0
149
+ : _update$updates$key.type)
150
+ ) {
151
+ var val = update.node.get(key);
152
+ void 0 !== val && (newState[key] = lsonToJson(val));
153
+ } else
154
+ "delete" ===
155
+ (null == (_update$updates$key2 = update.updates[key])
156
+ ? void 0
157
+ : _update$updates$key2.type) && delete newState[key];
158
+ }
159
+ return newState;
160
+ case "LiveList":
161
+ if (!Array.isArray(state))
162
+ throw new Error(
163
+ "Internal: received update on LiveList but state was not an array"
164
+ );
165
+ for (
166
+ var _step2,
167
+ _newState = state.map(function (x) {
168
+ return x;
169
+ }),
170
+ _loop = function () {
171
+ var listUpdate = _step2.value;
172
+ "set" === listUpdate.type
173
+ ? (_newState = _newState.map(function (item, index) {
174
+ return index === listUpdate.index
175
+ ? lsonToJson(listUpdate.item)
176
+ : item;
177
+ }))
178
+ : "insert" === listUpdate.type
179
+ ? listUpdate.index === _newState.length
180
+ ? _newState.push(lsonToJson(listUpdate.item))
181
+ : (_newState = [].concat(
182
+ _newState.slice(0, listUpdate.index),
183
+ [lsonToJson(listUpdate.item)],
184
+ _newState.slice(listUpdate.index)
185
+ ))
186
+ : "delete" === listUpdate.type
187
+ ? _newState.splice(listUpdate.index, 1)
188
+ : "move" === listUpdate.type &&
189
+ (_newState =
190
+ listUpdate.previousIndex > listUpdate.index
191
+ ? [].concat(
192
+ _newState.slice(0, listUpdate.index),
193
+ [lsonToJson(listUpdate.item)],
194
+ _newState.slice(
195
+ listUpdate.index,
196
+ listUpdate.previousIndex
197
+ ),
198
+ _newState.slice(listUpdate.previousIndex + 1)
199
+ )
200
+ : [].concat(
201
+ _newState.slice(0, listUpdate.previousIndex),
202
+ _newState.slice(
203
+ listUpdate.previousIndex + 1,
204
+ listUpdate.index + 1
205
+ ),
206
+ [lsonToJson(listUpdate.item)],
207
+ _newState.slice(listUpdate.index + 1)
208
+ ));
209
+ },
210
+ _iterator2 = Json._createForOfIteratorHelperLoose(update.updates);
211
+ !(_step2 = _iterator2()).done;
125
212
 
126
- case "LiveMap":
127
- if ("object" != typeof state) throw new Error("Internal: received update on LiveMap but state was not an object");
128
- var _newState2 = Object.assign({}, state);
129
- for (var _key7 in update.updates) {
130
- var _update$updates$_key3, _update$updates$_key4;
131
- "update" === (null == (_update$updates$_key3 = update.updates[_key7]) ? void 0 : _update$updates$_key3.type) ? _newState2[_key7] = lsonToJson(update.node.get(_key7)) : "delete" === (null == (_update$updates$_key4 = update.updates[_key7]) ? void 0 : _update$updates$_key4.type) && delete _newState2[_key7];
213
+ )
214
+ _loop();
215
+ return _newState;
216
+ case "LiveMap":
217
+ if (null === state || "object" != typeof state || Array.isArray(state))
218
+ throw new Error(
219
+ "Internal: received update on LiveMap but state was not an object"
220
+ );
221
+ var _newState2 = Object.assign({}, state);
222
+ for (var _key2 in update.updates) {
223
+ var _update$updates$_key, _update$updates$_key2;
224
+ if (
225
+ "update" ===
226
+ (null == (_update$updates$_key = update.updates[_key2])
227
+ ? void 0
228
+ : _update$updates$_key.type)
229
+ ) {
230
+ var value = update.node.get(_key2);
231
+ void 0 !== value && (_newState2[_key2] = lsonToJson(value));
232
+ } else
233
+ "delete" ===
234
+ (null == (_update$updates$_key2 = update.updates[_key2])
235
+ ? void 0
236
+ : _update$updates$_key2.type) && delete _newState2[_key2];
237
+ }
238
+ return _newState2;
239
+ }
240
+ if (Array.isArray(state)) {
241
+ var newArray = [].concat(state);
242
+ return (
243
+ (newArray[pathItem] = patchImmutableNode(state[pathItem], path, update)),
244
+ newArray
245
+ );
246
+ }
247
+ if (null !== state && "object" == typeof state) {
248
+ var _extends2,
249
+ node = state[pathItem];
250
+ return void 0 === node
251
+ ? state
252
+ : Json._extends(
253
+ {},
254
+ state,
255
+ (((_extends2 = {})[pathItem] = patchImmutableNode(
256
+ node,
257
+ path,
258
+ update
259
+ )),
260
+ _extends2)
261
+ );
132
262
  }
133
- return _newState2;
134
- }
135
- if (Array.isArray(state)) {
136
- var newArray = [].concat(state);
137
- return newArray[pathItem] = patchImmutableNode(state[pathItem], path, update), newArray;
138
- }
139
- return LiveObject._extends({}, state, ((_extends2 = {})[pathItem] = patchImmutableNode(state[pathItem], path, update),
140
- _extends2));
263
+ return state;
141
264
  }
142
-
143
265
  Object.defineProperty(exports, "ClientMsgCode", {
144
- enumerable: !0,
145
- get: function() {
146
- return LiveObject.ClientMsgCode;
147
- }
148
- }), Object.defineProperty(exports, "CrdtType", {
149
- enumerable: !0,
150
- get: function() {
151
- return LiveObject.CrdtType;
152
- }
153
- }), Object.defineProperty(exports, "OpCode", {
154
- enumerable: !0,
155
- get: function() {
156
- return LiveObject.OpCode;
157
- }
158
- }), Object.defineProperty(exports, "ServerMsgCode", {
159
- enumerable: !0,
160
- get: function() {
161
- return LiveObject.ServerMsgCode;
162
- }
163
- }), Object.defineProperty(exports, "WebsocketCloseCodes", {
164
- enumerable: !0,
165
- get: function() {
166
- return LiveObject.WebsocketCloseCodes;
167
- }
168
- }), exports.assertNever = LiveObject.assertNever, exports.b64decode = LiveObject.b64decode,
169
- exports.comparePosition = LiveObject.comparePosition, exports.deprecate = LiveObject.deprecate,
170
- exports.deprecateIf = LiveObject.deprecateIf, exports.errorIf = LiveObject.errorIf,
171
- exports.isChildCrdt = LiveObject.isChildCrdt, exports.isJsonArray = LiveObject.isJsonArray,
172
- exports.isJsonObject = LiveObject.isJsonObject, exports.isJsonScalar = LiveObject.isJsonScalar,
173
- exports.isRootCrdt = LiveObject.isRootCrdt, exports.makePosition = LiveObject.makePosition,
174
- exports.throwUsageError = LiveObject.throwUsageError, exports.tryParseJson = LiveObject.tryParseJson,
175
- exports.lsonToJson = lsonToJson, exports.patchImmutableObject = function(state, updates) {
176
- return updates.reduce((function(state, update) {
177
- return function(state, update) {
178
- var path = function(node) {
179
- for (var path = []; null != node._parentKey && null != node._parent; ) node._parent instanceof LiveObject.LiveList ? path.push(node._parent._indexOfPosition(node._parentKey)) : path.push(node._parentKey),
180
- node = node._parent;
181
- return path;
182
- }(update.node);
183
- return patchImmutableNode(state, path, update);
184
- }(state, update);
185
- }), state);
186
- }, exports.patchLiveObjectKey = patchLiveObjectKey;
266
+ enumerable: !0,
267
+ get: function () {
268
+ return Json.ClientMsgCode;
269
+ },
270
+ }),
271
+ Object.defineProperty(exports, "CrdtType", {
272
+ enumerable: !0,
273
+ get: function () {
274
+ return Json.CrdtType;
275
+ },
276
+ }),
277
+ Object.defineProperty(exports, "OpCode", {
278
+ enumerable: !0,
279
+ get: function () {
280
+ return Json.OpCode;
281
+ },
282
+ }),
283
+ Object.defineProperty(exports, "ServerMsgCode", {
284
+ enumerable: !0,
285
+ get: function () {
286
+ return Json.ServerMsgCode;
287
+ },
288
+ }),
289
+ Object.defineProperty(exports, "WebsocketCloseCodes", {
290
+ enumerable: !0,
291
+ get: function () {
292
+ return Json.WebsocketCloseCodes;
293
+ },
294
+ }),
295
+ (exports.assertNever = Json.assertNever),
296
+ (exports.b64decode = Json.b64decode),
297
+ (exports.comparePosition = Json.comparePosition),
298
+ (exports.deprecate = Json.deprecate),
299
+ (exports.deprecateIf = Json.deprecateIf),
300
+ (exports.errorIf = Json.errorIf),
301
+ (exports.isAppOnlyAuthToken = Json.isAppOnlyAuthToken),
302
+ (exports.isAuthToken = Json.isAuthToken),
303
+ (exports.isChildCrdt = Json.isChildCrdt),
304
+ (exports.isJsonArray = Json.isJsonArray),
305
+ (exports.isJsonObject = Json.isJsonObject),
306
+ (exports.isJsonScalar = Json.isJsonScalar),
307
+ (exports.isPlainObject = Json.isPlainObject),
308
+ (exports.isRoomAuthToken = Json.isRoomAuthToken),
309
+ (exports.isRootCrdt = Json.isRootCrdt),
310
+ (exports.isScope = Json.isScope),
311
+ (exports.makePosition = Json.makePosition),
312
+ (exports.nn = Json.nn),
313
+ (exports.parseAuthToken = Json.parseAuthToken),
314
+ (exports.throwUsageError = Json.throwUsageError),
315
+ (exports.tryParseJson = Json.tryParseJson),
316
+ (exports.lsonToJson = lsonToJson),
317
+ (exports.patchImmutableObject = function (state, updates) {
318
+ return updates.reduce(function (state, update) {
319
+ return (function (state, update) {
320
+ var path = (function (node) {
321
+ for (var path = []; "HasParent" === node.parent.type; )
322
+ Json.isLiveList(node.parent.node)
323
+ ? path.push(node.parent.node._indexOfPosition(node.parent.key))
324
+ : path.push(node.parent.key),
325
+ (node = node.parent.node);
326
+ return path;
327
+ })(update.node);
328
+ return patchImmutableNode(state, path, update);
329
+ })(state, update);
330
+ }, state);
331
+ }),
332
+ (exports.patchLiveObjectKey = patchLiveObjectKey);