@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.mjs CHANGED
@@ -1,148 +1,283 @@
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
+ o as LiveList,
4
+ q as LiveMap,
5
+ s as LiveRegister,
6
+ a as isPlainObject,
7
+ u as findNonSerializableValue,
8
+ e as isLiveList,
9
+ v as isLiveObject,
10
+ } from "./shared.mjs";
11
+ export {
12
+ C as ClientMsgCode,
13
+ I as CrdtType,
14
+ d as OpCode,
15
+ S as ServerMsgCode,
16
+ W as WebsocketCloseCodes,
17
+ w as assertNever,
18
+ M as b64decode,
19
+ G as comparePosition,
20
+ D as deprecate,
21
+ E as deprecateIf,
22
+ l as errorIf,
23
+ x as isAppOnlyAuthToken,
24
+ y as isAuthToken,
25
+ K as isChildCrdt,
26
+ f as isJsonArray,
27
+ k as isJsonObject,
28
+ J as isJsonScalar,
29
+ a as isPlainObject,
30
+ z as isRoomAuthToken,
31
+ j as isRootCrdt,
32
+ A as isScope,
33
+ H as makePosition,
34
+ n as nn,
35
+ B as parseAuthToken,
36
+ F as throwUsageError,
37
+ t as tryParseJson,
38
+ } from "./shared.mjs";
5
39
  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
40
  const result = {};
25
- for (const [key, value] of map.entries()) result[key] = lsonToJson(value);
41
+ for (const key in obj) {
42
+ const val = obj[key];
43
+ void 0 !== val && (result[key] = lsonToJson(val));
44
+ }
26
45
  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
46
  }
32
-
33
- function isPlainObject(obj) {
34
- return null !== obj && "[object Object]" === Object.prototype.toString.call(obj);
47
+ function lsonListToJson(value) {
48
+ return value.map(lsonToJson);
35
49
  }
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;
50
+ function lsonToJson(value) {
51
+ return value instanceof LiveObject
52
+ ? lsonObjectToJson(value.toObject())
53
+ : value instanceof LiveList
54
+ ? (function (value) {
55
+ return lsonListToJson(value.toArray());
56
+ })(value)
57
+ : value instanceof LiveMap
58
+ ? (function (map) {
59
+ const result = {};
60
+ for (const [key, value] of map.entries())
61
+ result[key] = lsonToJson(value);
62
+ return result;
63
+ })(value)
64
+ : value instanceof LiveRegister
65
+ ? value.data
66
+ : Array.isArray(value)
67
+ ? lsonListToJson(value)
68
+ : isPlainObject(value)
69
+ ? lsonObjectToJson(value)
70
+ : value;
46
71
  }
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];
72
+ function deepLiveify(value) {
73
+ if (Array.isArray(value)) return new LiveList(value.map(deepLiveify));
74
+ if (isPlainObject(value)) {
75
+ const init = {};
76
+ for (const key in value) {
77
+ const val = value[key];
78
+ void 0 !== val && (init[key] = deepLiveify(val));
62
79
  }
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
- }
80
+ return new LiveObject(init);
81
+ }
82
+ return value;
87
83
  }
84
+ function patchLiveObjectKey(liveObject, key, prev, next) {
85
+ if ("production" !== process.env.NODE_ENV) {
86
+ const nonSerializableValue = findNonSerializableValue(next);
87
+ if (nonSerializableValue)
88
+ return void console.error(
89
+ `New state path: '${nonSerializableValue.path}' value: '${nonSerializableValue.value}' is not serializable.\nOnly serializable value can be synced with Liveblocks.`
90
+ );
91
+ }
92
+ const value = liveObject.get(key);
93
+ if (void 0 === next) liveObject.delete(key);
94
+ else if (void 0 === value) liveObject.set(key, deepLiveify(next));
95
+ else {
96
+ if (prev === next) return;
97
+ isLiveList(value) && Array.isArray(prev) && Array.isArray(next)
98
+ ? (function (liveList, prev, next) {
99
+ let i = 0,
100
+ prevEnd = prev.length - 1,
101
+ nextEnd = next.length - 1,
102
+ prevNode = prev[0],
103
+ nextNode = next[0];
104
+ outer: {
105
+ for (; prevNode === nextNode; ) {
106
+ if ((++i, i > prevEnd || i > nextEnd)) break outer;
107
+ (prevNode = prev[i]), (nextNode = next[i]);
108
+ }
109
+ for (
110
+ prevNode = prev[prevEnd], nextNode = next[nextEnd];
111
+ prevNode === nextNode;
88
112
 
113
+ ) {
114
+ if ((prevEnd--, nextEnd--, i > prevEnd || i > nextEnd))
115
+ break outer;
116
+ (prevNode = prev[prevEnd]), (nextNode = next[nextEnd]);
117
+ }
118
+ }
119
+ if (i > prevEnd) {
120
+ if (i <= nextEnd)
121
+ for (; i <= nextEnd; )
122
+ liveList.insert(deepLiveify(next[i]), i), i++;
123
+ } else if (i > nextEnd) {
124
+ let localI = i;
125
+ for (; localI <= prevEnd; ) liveList.delete(i), localI++;
126
+ } else {
127
+ for (; i <= prevEnd && i <= nextEnd; ) {
128
+ (prevNode = prev[i]), (nextNode = next[i]);
129
+ const liveListNode = liveList.get(i);
130
+ isLiveObject(liveListNode) &&
131
+ isPlainObject(prevNode) &&
132
+ isPlainObject(nextNode)
133
+ ? patchLiveObject(liveListNode, prevNode, nextNode)
134
+ : liveList.set(i, deepLiveify(nextNode)),
135
+ i++;
136
+ }
137
+ for (; i <= nextEnd; )
138
+ liveList.insert(deepLiveify(next[i]), i), i++;
139
+ let localI = i;
140
+ for (; localI <= prevEnd; ) liveList.delete(i), localI++;
141
+ }
142
+ })(value, prev, next)
143
+ : isLiveObject(value) && isPlainObject(prev) && isPlainObject(next)
144
+ ? patchLiveObject(value, prev, next)
145
+ : liveObject.set(key, deepLiveify(next));
146
+ }
147
+ }
89
148
  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);
149
+ const updates = {};
150
+ for (const key in next) patchLiveObjectKey(root, key, prev[key], next[key]);
151
+ for (const key in prev) void 0 === next[key] && root.delete(key);
152
+ Object.keys(updates).length > 0 && root.update(updates);
94
153
  }
95
-
96
154
  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);
155
+ return updates.reduce(
156
+ (state, update) =>
157
+ (function (state, update) {
158
+ const path = (function (node) {
159
+ const path = [];
160
+ for (; "HasParent" === node.parent.type; )
161
+ isLiveList(node.parent.node)
162
+ ? path.push(node.parent.node._indexOfPosition(node.parent.key))
163
+ : path.push(node.parent.key),
164
+ (node = node.parent.node);
165
+ return path;
166
+ })(update.node);
167
+ return patchImmutableNode(state, path, update);
168
+ })(state, update),
169
+ state
170
+ );
106
171
  }
107
-
108
172
  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;
173
+ var _a, _b, _c, _d;
174
+ const pathItem = path.pop();
175
+ if (void 0 === pathItem)
176
+ switch (update.type) {
177
+ case "LiveObject": {
178
+ if (null === state || "object" != typeof state || Array.isArray(state))
179
+ throw new Error(
180
+ "Internal: received update on LiveObject but state was not an object"
181
+ );
182
+ const newState = Object.assign({}, state);
183
+ for (const key in update.updates)
184
+ if (
185
+ "update" ===
186
+ (null === (_a = update.updates[key]) || void 0 === _a
187
+ ? void 0
188
+ : _a.type)
189
+ ) {
190
+ const val = update.node.get(key);
191
+ void 0 !== val && (newState[key] = lsonToJson(val));
192
+ } else
193
+ "delete" ===
194
+ (null === (_b = update.updates[key]) || void 0 === _b
195
+ ? void 0
196
+ : _b.type) && delete newState[key];
197
+ return newState;
198
+ }
199
+ case "LiveList": {
200
+ if (!Array.isArray(state))
201
+ throw new Error(
202
+ "Internal: received update on LiveList but state was not an array"
203
+ );
204
+ let newState = state.map((x) => x);
205
+ for (const listUpdate of update.updates)
206
+ "set" === listUpdate.type
207
+ ? (newState = newState.map((item, index) =>
208
+ index === listUpdate.index ? lsonToJson(listUpdate.item) : item
209
+ ))
210
+ : "insert" === listUpdate.type
211
+ ? listUpdate.index === newState.length
212
+ ? newState.push(lsonToJson(listUpdate.item))
213
+ : (newState = [
214
+ ...newState.slice(0, listUpdate.index),
215
+ lsonToJson(listUpdate.item),
216
+ ...newState.slice(listUpdate.index),
217
+ ])
218
+ : "delete" === listUpdate.type
219
+ ? newState.splice(listUpdate.index, 1)
220
+ : "move" === listUpdate.type &&
221
+ (newState =
222
+ listUpdate.previousIndex > listUpdate.index
223
+ ? [
224
+ ...newState.slice(0, listUpdate.index),
225
+ lsonToJson(listUpdate.item),
226
+ ...newState.slice(
227
+ listUpdate.index,
228
+ listUpdate.previousIndex
229
+ ),
230
+ ...newState.slice(listUpdate.previousIndex + 1),
231
+ ]
232
+ : [
233
+ ...newState.slice(0, listUpdate.previousIndex),
234
+ ...newState.slice(
235
+ listUpdate.previousIndex + 1,
236
+ listUpdate.index + 1
237
+ ),
238
+ lsonToJson(listUpdate.item),
239
+ ...newState.slice(listUpdate.index + 1),
240
+ ]);
241
+ return newState;
242
+ }
243
+ case "LiveMap": {
244
+ if (null === state || "object" != typeof state || Array.isArray(state))
245
+ throw new Error(
246
+ "Internal: received update on LiveMap but state was not an object"
247
+ );
248
+ const newState = Object.assign({}, state);
249
+ for (const key in update.updates)
250
+ if (
251
+ "update" ===
252
+ (null === (_c = update.updates[key]) || void 0 === _c
253
+ ? void 0
254
+ : _c.type)
255
+ ) {
256
+ const value = update.node.get(key);
257
+ void 0 !== value && (newState[key] = lsonToJson(value));
258
+ } else
259
+ "delete" ===
260
+ (null === (_d = update.updates[key]) || void 0 === _d
261
+ ? void 0
262
+ : _d.type) && delete newState[key];
263
+ return newState;
264
+ }
265
+ }
266
+ if (Array.isArray(state)) {
267
+ const newArray = [...state];
268
+ return (
269
+ (newArray[pathItem] = patchImmutableNode(state[pathItem], path, update)),
270
+ newArray
271
+ );
129
272
  }
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;
273
+ if (null !== state && "object" == typeof state) {
274
+ const node = state[pathItem];
275
+ return void 0 === node
276
+ ? state
277
+ : Object.assign(Object.assign({}, state), {
278
+ [pathItem]: patchImmutableNode(node, path, update),
279
+ });
137
280
  }
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
- });
281
+ return state;
146
282
  }
147
-
148
283
  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-test1",
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
  },