@liveblocks/client 0.16.15 → 0.17.0-beta1

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 +28 -8
  2. package/index.js +1290 -830
  3. package/index.mjs +1091 -782
  4. package/internal.d.ts +418 -248
  5. package/internal.js +313 -168
  6. package/internal.mjs +264 -130
  7. package/package.json +15 -10
  8. package/shared.d.ts +719 -650
  9. package/shared.js +2571 -1331
  10. package/shared.mjs +1992 -1209
package/internal.mjs CHANGED
@@ -1,148 +1,282 @@
1
- import { L as LiveObject, b as LiveList, k as LiveMap, l as LiveRegister, A as AbstractCrdt, n as findNonSerializableValue } from "./shared.mjs";
2
-
3
- export { C as ClientMsgCode, s as CrdtType, O as OpCode, S as ServerMsgCode, W as WebsocketCloseCodes, x as assertNever, e as b64decode, u as comparePosition, o as deprecate, j as deprecateIf, p as errorIf, w as isChildCrdt, c as isJsonArray, f as isJsonObject, y as isJsonScalar, h as isRootCrdt, v as makePosition, q as throwUsageError, t as tryParseJson } from "./shared.mjs";
4
-
1
+ import {
2
+ L as LiveObject,
3
+ q as LiveList,
4
+ s as LiveMap,
5
+ u as LiveRegister,
6
+ b as isPlainObject,
7
+ v as findNonSerializableValue,
8
+ f as isLiveList,
9
+ w as isLiveObject,
10
+ } from "./shared.mjs";
11
+ export {
12
+ C as ClientMsgCode,
13
+ I as CrdtType,
14
+ e as OpCode,
15
+ S as ServerMsgCode,
16
+ W as WebsocketCloseCodes,
17
+ x as assertNever,
18
+ M as b64decode,
19
+ G as comparePosition,
20
+ D as deprecate,
21
+ E as deprecateIf,
22
+ o as errorIf,
23
+ y as isAppOnlyAuthToken,
24
+ z as isAuthToken,
25
+ K as isChildCrdt,
26
+ h as isJsonArray,
27
+ l as isJsonObject,
28
+ J as isJsonScalar,
29
+ b as isPlainObject,
30
+ A as isRoomAuthToken,
31
+ k as isRootCrdt,
32
+ B as isScope,
33
+ H as makePosition,
34
+ n as nn,
35
+ F as throwUsageError,
36
+ t as tryParseJson,
37
+ } from "./shared.mjs";
5
38
  function lsonObjectToJson(obj) {
6
- const result = {};
7
- for (const key in obj) {
8
- const val = obj[key];
9
- void 0 !== val && (result[key] = lsonToJson(val));
10
- }
11
- return result;
12
- }
13
-
14
- function lsonListToJson(value) {
15
- return value.map(lsonToJson);
16
- }
17
-
18
- function lsonToJson(value) {
19
- if (value instanceof LiveObject) return lsonObjectToJson(value.toObject());
20
- if (value instanceof LiveList) return function(value) {
21
- return lsonListToJson(value.toArray());
22
- }(value);
23
- if (value instanceof LiveMap) return function(map) {
24
39
  const result = {};
25
- for (const [key, value] of map.entries()) result[key] = lsonToJson(value);
40
+ for (const key in obj) {
41
+ const val = obj[key];
42
+ void 0 !== val && (result[key] = lsonToJson(val));
43
+ }
26
44
  return result;
27
- }(value);
28
- if (value instanceof LiveRegister) return value.data;
29
- if (value instanceof AbstractCrdt) throw new Error("Unhandled subclass of AbstractCrdt encountered");
30
- return Array.isArray(value) ? lsonListToJson(value) : isPlainObject(value) ? lsonObjectToJson(value) : value;
31
45
  }
32
-
33
- function isPlainObject(obj) {
34
- return null !== obj && "[object Object]" === Object.prototype.toString.call(obj);
46
+ function lsonListToJson(value) {
47
+ return value.map(lsonToJson);
35
48
  }
36
-
37
- function anyToCrdt(obj) {
38
- if (null == obj) return obj;
39
- if (Array.isArray(obj)) return new LiveList(obj.map(anyToCrdt));
40
- if (isPlainObject(obj)) {
41
- const init = {};
42
- for (const key in obj) init[key] = anyToCrdt(obj[key]);
43
- return new LiveObject(init);
44
- }
45
- return obj;
49
+ function lsonToJson(value) {
50
+ return value instanceof LiveObject
51
+ ? lsonObjectToJson(value.toObject())
52
+ : value instanceof LiveList
53
+ ? (function (value) {
54
+ return lsonListToJson(value.toArray());
55
+ })(value)
56
+ : value instanceof LiveMap
57
+ ? (function (map) {
58
+ const result = {};
59
+ for (const [key, value] of map.entries())
60
+ result[key] = lsonToJson(value);
61
+ return result;
62
+ })(value)
63
+ : value instanceof LiveRegister
64
+ ? value.data
65
+ : Array.isArray(value)
66
+ ? lsonListToJson(value)
67
+ : isPlainObject(value)
68
+ ? lsonObjectToJson(value)
69
+ : value;
46
70
  }
47
-
48
- function patchLiveObjectKey(liveObject, key, prev, next) {
49
- if ("production" !== process.env.NODE_ENV) {
50
- const nonSerializableValue = findNonSerializableValue(next);
51
- 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.`);
52
- }
53
- const value = liveObject.get(key);
54
- if (void 0 === next) liveObject.delete(key); else if (void 0 === value) liveObject.set(key, anyToCrdt(next)); else {
55
- if (prev === next) return;
56
- value instanceof LiveList && Array.isArray(prev) && Array.isArray(next) ? function(liveList, prev, next) {
57
- let i = 0, prevEnd = prev.length - 1, nextEnd = next.length - 1, prevNode = prev[0], nextNode = next[0];
58
- outer: {
59
- for (;prevNode === nextNode; ) {
60
- if (++i, i > prevEnd || i > nextEnd) break outer;
61
- prevNode = prev[i], nextNode = next[i];
71
+ function deepLiveify(value) {
72
+ if (Array.isArray(value)) return new LiveList(value.map(deepLiveify));
73
+ if (isPlainObject(value)) {
74
+ const init = {};
75
+ for (const key in value) {
76
+ const val = value[key];
77
+ void 0 !== val && (init[key] = deepLiveify(val));
62
78
  }
63
- for (prevNode = prev[prevEnd], nextNode = next[nextEnd]; prevNode === nextNode; ) {
64
- if (prevEnd--, nextEnd--, i > prevEnd || i > nextEnd) break outer;
65
- prevNode = prev[prevEnd], nextNode = next[nextEnd];
66
- }
67
- }
68
- if (i > prevEnd) {
69
- if (i <= nextEnd) for (;i <= nextEnd; ) liveList.insert(anyToCrdt(next[i]), i),
70
- i++;
71
- } else if (i > nextEnd) {
72
- let localI = i;
73
- for (;localI <= prevEnd; ) liveList.delete(i), localI++;
74
- } else {
75
- for (;i <= prevEnd && i <= nextEnd; ) {
76
- prevNode = prev[i], nextNode = next[i];
77
- const liveListNode = liveList.get(i);
78
- liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode) ? patchLiveObject(liveListNode, prevNode, nextNode) : liveList.set(i, anyToCrdt(nextNode)),
79
- i++;
80
- }
81
- for (;i <= nextEnd; ) liveList.insert(anyToCrdt(next[i]), i), i++;
82
- let localI = i;
83
- for (;localI <= prevEnd; ) liveList.delete(i), localI++;
84
- }
85
- }(value, prev, next) : value instanceof LiveObject && isPlainObject(prev) && isPlainObject(next) ? patchLiveObject(value, prev, next) : liveObject.set(key, anyToCrdt(next));
86
- }
79
+ return new LiveObject(init);
80
+ }
81
+ return value;
87
82
  }
83
+ function patchLiveObjectKey(liveObject, key, prev, next) {
84
+ if ("production" !== process.env.NODE_ENV) {
85
+ const nonSerializableValue = findNonSerializableValue(next);
86
+ if (nonSerializableValue)
87
+ return void console.error(
88
+ `New state path: '${nonSerializableValue.path}' value: '${nonSerializableValue.value}' is not serializable.\nOnly serializable value can be synced with Liveblocks.`
89
+ );
90
+ }
91
+ const value = liveObject.get(key);
92
+ if (void 0 === next) liveObject.delete(key);
93
+ else if (void 0 === value) liveObject.set(key, deepLiveify(next));
94
+ else {
95
+ if (prev === next) return;
96
+ isLiveList(value) && Array.isArray(prev) && Array.isArray(next)
97
+ ? (function (liveList, prev, next) {
98
+ let i = 0,
99
+ prevEnd = prev.length - 1,
100
+ nextEnd = next.length - 1,
101
+ prevNode = prev[0],
102
+ nextNode = next[0];
103
+ outer: {
104
+ for (; prevNode === nextNode; ) {
105
+ if ((++i, i > prevEnd || i > nextEnd)) break outer;
106
+ (prevNode = prev[i]), (nextNode = next[i]);
107
+ }
108
+ for (
109
+ prevNode = prev[prevEnd], nextNode = next[nextEnd];
110
+ prevNode === nextNode;
88
111
 
112
+ ) {
113
+ if ((prevEnd--, nextEnd--, i > prevEnd || i > nextEnd))
114
+ break outer;
115
+ (prevNode = prev[prevEnd]), (nextNode = next[nextEnd]);
116
+ }
117
+ }
118
+ if (i > prevEnd) {
119
+ if (i <= nextEnd)
120
+ for (; i <= nextEnd; )
121
+ liveList.insert(deepLiveify(next[i]), i), i++;
122
+ } else if (i > nextEnd) {
123
+ let localI = i;
124
+ for (; localI <= prevEnd; ) liveList.delete(i), localI++;
125
+ } else {
126
+ for (; i <= prevEnd && i <= nextEnd; ) {
127
+ (prevNode = prev[i]), (nextNode = next[i]);
128
+ const liveListNode = liveList.get(i);
129
+ isLiveObject(liveListNode) &&
130
+ isPlainObject(prevNode) &&
131
+ isPlainObject(nextNode)
132
+ ? patchLiveObject(liveListNode, prevNode, nextNode)
133
+ : liveList.set(i, deepLiveify(nextNode)),
134
+ i++;
135
+ }
136
+ for (; i <= nextEnd; )
137
+ liveList.insert(deepLiveify(next[i]), i), i++;
138
+ let localI = i;
139
+ for (; localI <= prevEnd; ) liveList.delete(i), localI++;
140
+ }
141
+ })(value, prev, next)
142
+ : isLiveObject(value) && isPlainObject(prev) && isPlainObject(next)
143
+ ? patchLiveObject(value, prev, next)
144
+ : liveObject.set(key, deepLiveify(next));
145
+ }
146
+ }
89
147
  function patchLiveObject(root, prev, next) {
90
- const updates = {};
91
- for (const key in next) patchLiveObjectKey(root, key, prev[key], next[key]);
92
- for (const key in prev) void 0 === next[key] && root.delete(key);
93
- Object.keys(updates).length > 0 && root.update(updates);
148
+ const updates = {};
149
+ for (const key in next) patchLiveObjectKey(root, key, prev[key], next[key]);
150
+ for (const key in prev) void 0 === next[key] && root.delete(key);
151
+ Object.keys(updates).length > 0 && root.update(updates);
94
152
  }
95
-
96
153
  function patchImmutableObject(state, updates) {
97
- return updates.reduce(((state, update) => function(state, update) {
98
- const path = function(node) {
99
- const path = [];
100
- for (;null != node._parentKey && null != node._parent; ) node._parent instanceof LiveList ? path.push(node._parent._indexOfPosition(node._parentKey)) : path.push(node._parentKey),
101
- node = node._parent;
102
- return path;
103
- }(update.node);
104
- return patchImmutableNode(state, path, update);
105
- }(state, update)), state);
154
+ return updates.reduce(
155
+ (state, update) =>
156
+ (function (state, update) {
157
+ const path = (function (node) {
158
+ const path = [];
159
+ for (; "HasParent" === node.parent.type; )
160
+ isLiveList(node.parent.node)
161
+ ? path.push(node.parent.node._indexOfPosition(node.parent.key))
162
+ : path.push(node.parent.key),
163
+ (node = node.parent.node);
164
+ return path;
165
+ })(update.node);
166
+ return patchImmutableNode(state, path, update);
167
+ })(state, update),
168
+ state
169
+ );
106
170
  }
107
-
108
171
  function patchImmutableNode(state, path, update) {
109
- var _a, _b, _c, _d;
110
- const pathItem = path.pop();
111
- if (void 0 === pathItem) switch (update.type) {
112
- case "LiveObject":
113
- {
114
- if ("object" != typeof state) throw new Error("Internal: received update on LiveObject but state was not an object");
115
- const newState = Object.assign({}, state);
116
- for (const key in update.updates) if ("update" === (null === (_a = update.updates[key]) || void 0 === _a ? void 0 : _a.type)) {
117
- const val = update.node.get(key);
118
- void 0 !== val && (newState[key] = lsonToJson(val));
119
- } else "delete" === (null === (_b = update.updates[key]) || void 0 === _b ? void 0 : _b.type) && delete newState[key];
120
- return newState;
121
- }
122
-
123
- case "LiveList":
124
- {
125
- if (!1 === Array.isArray(state)) throw new Error("Internal: received update on LiveList but state was not an array");
126
- let newState = state.map((x => x));
127
- for (const listUpdate of update.updates) "set" === listUpdate.type ? newState = newState.map(((item, index) => index === listUpdate.index ? listUpdate.item : item)) : "insert" === listUpdate.type ? listUpdate.index === newState.length ? newState.push(lsonToJson(listUpdate.item)) : newState = [ ...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 ? [ ...newState.slice(0, listUpdate.index), lsonToJson(listUpdate.item), ...newState.slice(listUpdate.index, listUpdate.previousIndex), ...newState.slice(listUpdate.previousIndex + 1) ] : [ ...newState.slice(0, listUpdate.previousIndex), ...newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), lsonToJson(listUpdate.item), ...newState.slice(listUpdate.index + 1) ]);
128
- return newState;
172
+ var _a, _b, _c, _d;
173
+ const pathItem = path.pop();
174
+ if (void 0 === pathItem)
175
+ switch (update.type) {
176
+ case "LiveObject": {
177
+ if (null === state || "object" != typeof state || Array.isArray(state))
178
+ throw new Error(
179
+ "Internal: received update on LiveObject but state was not an object"
180
+ );
181
+ const newState = Object.assign({}, state);
182
+ for (const key in update.updates)
183
+ if (
184
+ "update" ===
185
+ (null === (_a = update.updates[key]) || void 0 === _a
186
+ ? void 0
187
+ : _a.type)
188
+ ) {
189
+ const val = update.node.get(key);
190
+ void 0 !== val && (newState[key] = lsonToJson(val));
191
+ } else
192
+ "delete" ===
193
+ (null === (_b = update.updates[key]) || void 0 === _b
194
+ ? void 0
195
+ : _b.type) && delete newState[key];
196
+ return newState;
197
+ }
198
+ case "LiveList": {
199
+ if (!Array.isArray(state))
200
+ throw new Error(
201
+ "Internal: received update on LiveList but state was not an array"
202
+ );
203
+ let newState = state.map((x) => x);
204
+ for (const listUpdate of update.updates)
205
+ "set" === listUpdate.type
206
+ ? (newState = newState.map((item, index) =>
207
+ index === listUpdate.index ? lsonToJson(listUpdate.item) : item
208
+ ))
209
+ : "insert" === listUpdate.type
210
+ ? listUpdate.index === newState.length
211
+ ? newState.push(lsonToJson(listUpdate.item))
212
+ : (newState = [
213
+ ...newState.slice(0, listUpdate.index),
214
+ lsonToJson(listUpdate.item),
215
+ ...newState.slice(listUpdate.index),
216
+ ])
217
+ : "delete" === listUpdate.type
218
+ ? newState.splice(listUpdate.index, 1)
219
+ : "move" === listUpdate.type &&
220
+ (newState =
221
+ listUpdate.previousIndex > listUpdate.index
222
+ ? [
223
+ ...newState.slice(0, listUpdate.index),
224
+ lsonToJson(listUpdate.item),
225
+ ...newState.slice(
226
+ listUpdate.index,
227
+ listUpdate.previousIndex
228
+ ),
229
+ ...newState.slice(listUpdate.previousIndex + 1),
230
+ ]
231
+ : [
232
+ ...newState.slice(0, listUpdate.previousIndex),
233
+ ...newState.slice(
234
+ listUpdate.previousIndex + 1,
235
+ listUpdate.index + 1
236
+ ),
237
+ lsonToJson(listUpdate.item),
238
+ ...newState.slice(listUpdate.index + 1),
239
+ ]);
240
+ return newState;
241
+ }
242
+ case "LiveMap": {
243
+ if (null === state || "object" != typeof state || Array.isArray(state))
244
+ throw new Error(
245
+ "Internal: received update on LiveMap but state was not an object"
246
+ );
247
+ const newState = Object.assign({}, state);
248
+ for (const key in update.updates)
249
+ if (
250
+ "update" ===
251
+ (null === (_c = update.updates[key]) || void 0 === _c
252
+ ? void 0
253
+ : _c.type)
254
+ ) {
255
+ const value = update.node.get(key);
256
+ void 0 !== value && (newState[key] = lsonToJson(value));
257
+ } else
258
+ "delete" ===
259
+ (null === (_d = update.updates[key]) || void 0 === _d
260
+ ? void 0
261
+ : _d.type) && delete newState[key];
262
+ return newState;
263
+ }
264
+ }
265
+ if (Array.isArray(state)) {
266
+ const newArray = [...state];
267
+ return (
268
+ (newArray[pathItem] = patchImmutableNode(state[pathItem], path, update)),
269
+ newArray
270
+ );
129
271
  }
130
-
131
- case "LiveMap":
132
- {
133
- if ("object" != typeof state) throw new Error("Internal: received update on LiveMap but state was not an object");
134
- const newState = Object.assign({}, state);
135
- for (const key in update.updates) "update" === (null === (_c = update.updates[key]) || void 0 === _c ? void 0 : _c.type) ? newState[key] = lsonToJson(update.node.get(key)) : "delete" === (null === (_d = update.updates[key]) || void 0 === _d ? void 0 : _d.type) && delete newState[key];
136
- return newState;
272
+ if (null !== state && "object" == typeof state) {
273
+ const node = state[pathItem];
274
+ return void 0 === node
275
+ ? state
276
+ : Object.assign(Object.assign({}, state), {
277
+ [pathItem]: patchImmutableNode(node, path, update),
278
+ });
137
279
  }
138
- }
139
- if (Array.isArray(state)) {
140
- const newArray = [ ...state ];
141
- return newArray[pathItem] = patchImmutableNode(state[pathItem], path, update), newArray;
142
- }
143
- return Object.assign(Object.assign({}, state), {
144
- [pathItem]: patchImmutableNode(state[pathItem], path, update)
145
- });
280
+ return state;
146
281
  }
147
-
148
282
  export { lsonToJson, patchImmutableObject, patchLiveObjectKey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/client",
3
- "version": "0.16.15",
3
+ "version": "0.17.0-beta1",
4
4
  "description": "A client that lets you interact with Liveblocks servers.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -33,8 +33,9 @@
33
33
  "build": "rollup -c && cp ./package.json ./README.md ./lib",
34
34
  "format": "eslint --fix src/ test/ && prettier --write src/ test/",
35
35
  "lint": "eslint src/ test/",
36
- "test": "jest --watch",
37
- "test-ci": "jest"
36
+ "test": "jest --watch --silent --verbose --config=./jest.config.js",
37
+ "test-ci": "jest --silent --verbose",
38
+ "test-e2e": "jest --silent --verbose --config=./jest.config.e2e.js"
38
39
  },
39
40
  "license": "Apache-2.0",
40
41
  "devDependencies": {
@@ -45,23 +46,27 @@
45
46
  "@rollup/plugin-node-resolve": "^13.1.3",
46
47
  "@rollup/plugin-replace": "^4.0.0",
47
48
  "@rollup/plugin-typescript": "^8.3.1",
48
- "@types/jest": "^26.0.21",
49
+ "@types/jest": "^26.0.24",
49
50
  "@types/node-fetch": "^2.6.1",
50
- "@types/ws": "^8.2.2",
51
- "@typescript-eslint/eslint-plugin": "^5.17.0",
52
- "@typescript-eslint/parser": "^5.17.0",
51
+ "@types/ws": "^8.5.3",
52
+ "@typescript-eslint/eslint-plugin": "^5.26.0",
53
+ "@typescript-eslint/parser": "^5.26.0",
54
+ "dotenv": "^16.0.0",
53
55
  "eslint": "^8.12.0",
54
56
  "eslint-plugin-import": "^2.26.0",
55
57
  "eslint-plugin-simple-import-sort": "^7.0.0",
56
- "jest": "^26.6.3",
58
+ "jest": "^28.0.3",
57
59
  "jest-each": "^27.5.1",
60
+ "jest-environment-jsdom": "^28.1.0",
58
61
  "msw": "^0.39.1",
59
62
  "node-fetch": "2.6.7",
63
+ "regenerator-runtime": "^0.13.9",
60
64
  "rollup": "^2.68.0",
61
65
  "rollup-plugin-command": "^1.1.3",
62
- "rollup-plugin-dts": "^4.1.0",
66
+ "rollup-plugin-dts": "^4.2.2",
67
+ "rollup-plugin-prettier": "^2.2.2",
63
68
  "rollup-plugin-terser": "^7.0.2",
64
- "typescript": "^4.4.0",
69
+ "typescript": "^4.7.2",
65
70
  "whatwg-fetch": "^3.6.2",
66
71
  "ws": "^8.5.0"
67
72
  },