@liveblocks/client 0.16.13 → 0.16.14

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/internal.d.ts CHANGED
@@ -1,33 +1,22 @@
1
- import { d as JsonObject, J as Json, h as Op, I as IdTuple, i as SerializedCrdt, f as Lson, g as LsonObject, c as LiveObject, S as StorageUpdate } from './shared';
2
- export { G as CrdtType, j as CreateChildOp, k as CreateListOp, l as CreateMapOp, m as CreateObjectOp, n as CreateOp, o as CreateRegisterOp, p as CreateRootObjectOp, D as DeleteCrdtOp, q as DeleteObjectKeyOp, I as IdTuple, r as LiveNode, N as NodeMap, h as Op, K as OpCode, s as ParentToChildNodeMap, t as Resolve, u as RoomInitializers, v as SerializedChild, i as SerializedCrdt, w as SerializedList, x as SerializedMap, y as SerializedObject, z as SerializedRegister, A as SerializedRootObject, E as SetParentKeyOp, F as UpdateObjectOp, W as WebsocketCloseCodes, M as isChildCrdt, Q as isRootCrdt } from './shared';
1
+ import { d as JsonObject, J as Json, g as Resolve, e as Lson, A as AbstractCrdt, f as LsonObject, L as LiveObject, S as StorageUpdate } from './shared';
2
+ export { g as Resolve, h as RoomInitializers } from './shared';
3
3
 
4
- declare enum ClientMsgCode {
5
- UPDATE_PRESENCE = 100,
6
- BROADCAST_EVENT = 103,
7
- FETCH_STORAGE = 200,
8
- UPDATE_STORAGE = 201
9
- }
4
+ declare type IdTuple<T> = [id: string, value: T];
10
5
  /**
11
- * Messages that can be sent from the client to the server.
6
+ * Lookup table for nodes (= SerializedCrdt values) by their IDs.
12
7
  */
13
- declare type ClientMsg<TPresence extends JsonObject> = BroadcastEventClientMsg | UpdatePresenceClientMsg<TPresence> | UpdateStorageClientMsg | FetchStorageClientMsg;
14
- declare type BroadcastEventClientMsg = {
15
- type: ClientMsgCode.BROADCAST_EVENT;
16
- event: Json;
17
- };
18
- declare type UpdatePresenceClientMsg<TPresence extends JsonObject> = {
19
- type: ClientMsgCode.UPDATE_PRESENCE;
20
- data: TPresence;
21
- targetActor?: number;
22
- };
23
- declare type UpdateStorageClientMsg = {
24
- type: ClientMsgCode.UPDATE_STORAGE;
25
- ops: Op[];
26
- };
27
- declare type FetchStorageClientMsg = {
28
- type: ClientMsgCode.FETCH_STORAGE;
29
- };
30
-
8
+ declare type NodeMap = Map<string, // Node ID
9
+ SerializedCrdt>;
10
+ /**
11
+ * Reverse lookup table for all child nodes (= list of SerializedCrdt values)
12
+ * by their parent node's IDs.
13
+ */
14
+ declare type ParentToChildNodeMap = Map<string, // Parent's node ID
15
+ IdTuple<SerializedChild>[]>;
16
+ /**
17
+ * Messages that can be sent from the server to the client.
18
+ */
19
+ declare type ServerMsg<TPresence extends JsonObject> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg | UserLeftServerMsg | BroadcastedEventServerMsg | RoomStateServerMsg | InitialDocumentStateServerMsg | UpdateStorageServerMsg;
31
20
  declare enum ServerMsgCode {
32
21
  UPDATE_PRESENCE = 100,
33
22
  USER_JOINED = 101,
@@ -38,9 +27,19 @@ declare enum ServerMsgCode {
38
27
  UPDATE_STORAGE = 201
39
28
  }
40
29
  /**
41
- * Messages that can be sent from the server to the client.
30
+ * Sent by the WebSocket server to a single client in response to the client
31
+ * joining the Room, to provide the initial state of the Room. The payload
32
+ * includes a list of all other Users that already are in the Room.
42
33
  */
43
- declare type ServerMsg<TPresence extends JsonObject> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg | UserLeftServerMsg | BroadcastedEventServerMsg | RoomStateServerMsg | InitialDocumentStateServerMsg | UpdateStorageServerMsg;
34
+ declare type RoomStateServerMsg = {
35
+ type: ServerMsgCode.ROOM_STATE;
36
+ users: {
37
+ [actor: number]: {
38
+ id?: string;
39
+ info?: Json;
40
+ };
41
+ };
42
+ };
44
43
  /**
45
44
  * Sent by the WebSocket server and broadcasted to all clients to announce that
46
45
  * a User updated their presence. For example, when a user moves their cursor.
@@ -113,20 +112,6 @@ declare type BroadcastedEventServerMsg = {
113
112
  */
114
113
  event: Json;
115
114
  };
116
- /**
117
- * Sent by the WebSocket server to a single client in response to the client
118
- * joining the Room, to provide the initial state of the Room. The payload
119
- * includes a list of all other Users that already are in the Room.
120
- */
121
- declare type RoomStateServerMsg = {
122
- type: ServerMsgCode.ROOM_STATE;
123
- users: {
124
- [actor: number]: {
125
- id?: string;
126
- info?: Json;
127
- };
128
- };
129
- };
130
115
  /**
131
116
  * Sent by the WebSocket server to a single client in response to the client
132
117
  * joining the Room, to provide the initial Storage state of the Room. The
@@ -147,42 +132,159 @@ declare type UpdateStorageServerMsg = {
147
132
  type: ServerMsgCode.UPDATE_STORAGE;
148
133
  ops: Op[];
149
134
  };
150
-
151
135
  /**
152
- * Helper function that can be used to implement exhaustive switch statements
153
- * with TypeScript. Example usage:
154
- *
155
- * type Fruit = "🍎" | "🍌";
156
- *
157
- * switch (fruit) {
158
- * case "🍎":
159
- * case "🍌":
160
- * return doSomething();
161
- *
162
- * default:
163
- * return assertNever(fruit, "Unknown fruit");
164
- * }
165
- *
166
- * If now the Fruit union is extended (i.e. add "🍒"), TypeScript will catch
167
- * this *statically*, rather than at runtime, and force you to handle the
168
- * 🍒 case.
136
+ * Messages that can be sent from the client to the server.
169
137
  */
170
- declare function assertNever(_value: never, errmsg: string): never;
138
+ declare type ClientMsg<TPresence extends JsonObject> = BroadcastEventClientMsg | UpdatePresenceClientMsg<TPresence> | UpdateStorageClientMsg | FetchStorageClientMsg;
139
+ declare enum ClientMsgCode {
140
+ UPDATE_PRESENCE = 100,
141
+ BROADCAST_EVENT = 103,
142
+ FETCH_STORAGE = 200,
143
+ UPDATE_STORAGE = 201
144
+ }
145
+ declare type BroadcastEventClientMsg = {
146
+ type: ClientMsgCode.BROADCAST_EVENT;
147
+ event: Json;
148
+ };
149
+ declare type UpdatePresenceClientMsg<TPresence extends JsonObject> = {
150
+ type: ClientMsgCode.UPDATE_PRESENCE;
151
+ data: TPresence;
152
+ targetActor?: number;
153
+ };
154
+ declare type UpdateStorageClientMsg = {
155
+ type: ClientMsgCode.UPDATE_STORAGE;
156
+ ops: Op[];
157
+ };
158
+ declare type FetchStorageClientMsg = {
159
+ type: ClientMsgCode.FETCH_STORAGE;
160
+ };
161
+ declare enum CrdtType {
162
+ OBJECT = 0,
163
+ LIST = 1,
164
+ MAP = 2,
165
+ REGISTER = 3
166
+ }
167
+ declare type SerializedRootObject = {
168
+ type: CrdtType.OBJECT;
169
+ data: JsonObject;
170
+ parentId?: never;
171
+ parentKey?: never;
172
+ };
173
+ declare type SerializedObject = {
174
+ type: CrdtType.OBJECT;
175
+ parentId: string;
176
+ parentKey: string;
177
+ data: JsonObject;
178
+ };
179
+ declare type SerializedList = {
180
+ type: CrdtType.LIST;
181
+ parentId: string;
182
+ parentKey: string;
183
+ };
184
+ declare type SerializedMap = {
185
+ type: CrdtType.MAP;
186
+ parentId: string;
187
+ parentKey: string;
188
+ };
189
+ declare type SerializedRegister = {
190
+ type: CrdtType.REGISTER;
191
+ parentId: string;
192
+ parentKey: string;
193
+ data: Json;
194
+ };
195
+ declare type SerializedCrdt = SerializedRootObject | SerializedChild;
196
+ declare type SerializedChild = SerializedObject | SerializedList | SerializedMap | SerializedRegister;
197
+ declare function isRootCrdt(crdt: SerializedCrdt): crdt is SerializedRootObject;
198
+ declare function isChildCrdt(crdt: SerializedCrdt): crdt is SerializedChild;
199
+ declare enum OpCode {
200
+ INIT = 0,
201
+ SET_PARENT_KEY = 1,
202
+ CREATE_LIST = 2,
203
+ UPDATE_OBJECT = 3,
204
+ CREATE_OBJECT = 4,
205
+ DELETE_CRDT = 5,
206
+ DELETE_OBJECT_KEY = 6,
207
+ CREATE_MAP = 7,
208
+ CREATE_REGISTER = 8
209
+ }
171
210
  /**
172
- * Asserts that a given value is non-nullable. This is similar to TypeScript's
173
- * `!` operator, but will throw an error at runtime (dev-mode only) indicating
174
- * an incorrect assumption.
175
- *
176
- * Instead of:
177
- *
178
- * foo!.bar
179
- *
180
- * Use:
181
- *
182
- * nn(foo).bar
183
- *
211
+ * These operations are the payload for {@link UpdateStorageServerMsg} messages
212
+ * only.
184
213
  */
185
- declare function nn<T>(value: T, errmsg?: string): NonNullable<T>;
214
+ declare type Op = CreateOp | UpdateObjectOp | DeleteCrdtOp | SetParentKeyOp | DeleteObjectKeyOp;
215
+ declare type CreateOp = CreateRootObjectOp | CreateChildOp;
216
+ declare type CreateChildOp = CreateObjectOp | CreateRegisterOp | CreateMapOp | CreateListOp;
217
+ declare type UpdateObjectOp = {
218
+ opId?: string;
219
+ id: string;
220
+ type: OpCode.UPDATE_OBJECT;
221
+ data: Partial<JsonObject>;
222
+ };
223
+ declare type CreateObjectOp = {
224
+ opId?: string;
225
+ id: string;
226
+ intent?: "set";
227
+ type: OpCode.CREATE_OBJECT;
228
+ parentId: string;
229
+ parentKey: string;
230
+ data: JsonObject;
231
+ };
232
+ declare type CreateRootObjectOp = Resolve<Omit<CreateObjectOp, "parentId" | "parentKey"> & {
233
+ parentId?: never;
234
+ parentKey?: never;
235
+ }>;
236
+ declare type CreateListOp = {
237
+ opId?: string;
238
+ id: string;
239
+ intent?: "set";
240
+ type: OpCode.CREATE_LIST;
241
+ parentId: string;
242
+ parentKey: string;
243
+ };
244
+ declare type CreateMapOp = {
245
+ opId?: string;
246
+ id: string;
247
+ intent?: "set";
248
+ type: OpCode.CREATE_MAP;
249
+ parentId: string;
250
+ parentKey: string;
251
+ };
252
+ declare type CreateRegisterOp = {
253
+ opId?: string;
254
+ id: string;
255
+ intent?: "set";
256
+ type: OpCode.CREATE_REGISTER;
257
+ parentId: string;
258
+ parentKey: string;
259
+ data: Json;
260
+ };
261
+ declare type DeleteCrdtOp = {
262
+ opId?: string;
263
+ id: string;
264
+ type: OpCode.DELETE_CRDT;
265
+ };
266
+ declare type SetParentKeyOp = {
267
+ opId?: string;
268
+ id: string;
269
+ type: OpCode.SET_PARENT_KEY;
270
+ parentKey: string;
271
+ };
272
+ declare type DeleteObjectKeyOp = {
273
+ opId?: string;
274
+ id: string;
275
+ type: OpCode.DELETE_OBJECT_KEY;
276
+ key: string;
277
+ };
278
+ declare enum WebsocketCloseCodes {
279
+ CLOSE_ABNORMAL = 1006,
280
+ INVALID_MESSAGE_FORMAT = 4000,
281
+ NOT_ALLOWED = 4001,
282
+ MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
283
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
284
+ MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
285
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
286
+ CLOSE_WITHOUT_RETRY = 4999
287
+ }
186
288
 
187
289
  /**
188
290
  * Tools to help with the controlled deprecation of public APIs.
@@ -215,13 +317,33 @@ declare function throwUsageError(message: string): void;
215
317
  */
216
318
  declare function errorIf(condition: unknown, message: string): void;
217
319
 
218
- declare function lsonToJson(value: Lson): Json;
320
+ declare function lsonToJson(value: Lson | AbstractCrdt): Json;
219
321
  declare function patchLiveObjectKey<O extends LsonObject>(liveObject: LiveObject<O>, key: keyof O, prev: unknown, next: unknown): void;
220
322
  declare function patchImmutableObject<T>(state: T, updates: StorageUpdate[]): T;
221
323
 
222
324
  declare function makePosition(before?: string, after?: string): string;
223
325
  declare function comparePosition(posA: string, posB: string): number;
224
326
 
327
+ /**
328
+ * Helper function that can be used to implement exhaustive switch statements
329
+ * with TypeScript. Example usage:
330
+ *
331
+ * type Fruit = "🍎" | "🍌";
332
+ *
333
+ * switch (fruit) {
334
+ * case "🍎":
335
+ * case "🍌":
336
+ * return doSomething();
337
+ *
338
+ * default:
339
+ * return assertNever(fruit, "Unknown fruit");
340
+ * }
341
+ *
342
+ * If now the Fruit union is extended (i.e. add "🍒"), TypeScript will catch
343
+ * this *statically*, rather than at runtime, and force you to handle the
344
+ * 🍒 case.
345
+ */
346
+ declare function assertNever(_value: never, errmsg: string): never;
225
347
  /**
226
348
  * Alternative to JSON.parse() that will not throw in production. If the passed
227
349
  * string cannot be parsed, this will return `undefined`.
@@ -232,4 +354,4 @@ declare function tryParseJson(rawMessage: string): Json | undefined;
232
354
  */
233
355
  declare function b64decode(b64value: string): string;
234
356
 
235
- export { BroadcastEventClientMsg, BroadcastedEventServerMsg, ClientMsg, ClientMsgCode, FetchStorageClientMsg, InitialDocumentStateServerMsg, RoomStateServerMsg, ServerMsg, ServerMsgCode, UpdatePresenceClientMsg, UpdatePresenceServerMsg, UpdateStorageClientMsg, UpdateStorageServerMsg, UserJoinServerMsg, UserLeftServerMsg, assertNever, b64decode, comparePosition, deprecate, deprecateIf, errorIf, lsonToJson, makePosition, nn, patchImmutableObject, patchLiveObjectKey, throwUsageError, tryParseJson };
357
+ export { BroadcastEventClientMsg, ClientMsg, ClientMsgCode, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateRegisterOp, CreateRootObjectOp, DeleteCrdtOp, DeleteObjectKeyOp, FetchStorageClientMsg, IdTuple, InitialDocumentStateServerMsg, NodeMap, Op, OpCode, ParentToChildNodeMap, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, UpdateObjectOp, UpdatePresenceClientMsg, UpdateStorageClientMsg, UserJoinServerMsg, WebsocketCloseCodes, assertNever, b64decode, comparePosition, deprecate, deprecateIf, errorIf, isChildCrdt, isRootCrdt, lsonToJson, makePosition, patchImmutableObject, patchLiveObjectKey, throwUsageError, tryParseJson };
package/internal.js CHANGED
@@ -20,15 +20,20 @@ function lsonListToJson(value) {
20
20
  }
21
21
 
22
22
  function lsonToJson(value) {
23
- return value instanceof LiveObject.LiveObject ? lsonObjectToJson(value.toObject()) : value instanceof LiveObject.LiveList ? function(value) {
23
+ if (value instanceof LiveObject.LiveObject) return lsonObjectToJson(value.toObject());
24
+ if (value instanceof LiveObject.LiveList) return function(value) {
24
25
  return lsonListToJson(value.toArray());
25
- }(value) : value instanceof LiveObject.LiveMap ? function(map) {
26
+ }(value);
27
+ if (value instanceof LiveObject.LiveMap) return function(map) {
26
28
  for (var _step, result = {}, _iterator = LiveObject._createForOfIteratorHelperLoose(map.entries()); !(_step = _iterator()).done; ) {
27
29
  var _step$value = _step.value, _key2 = _step$value[0], value = _step$value[1];
28
30
  result[_key2] = lsonToJson(value);
29
31
  }
30
32
  return result;
31
- }(value) : value instanceof LiveObject.LiveRegister ? value.data : Array.isArray(value) ? lsonListToJson(value) : isPlainObject(value) ? lsonObjectToJson(value) : value;
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;
32
37
  }
33
38
 
34
39
  function isPlainObject(obj) {
@@ -54,7 +59,7 @@ function patchLiveObjectKey(liveObject, key, prev, next) {
54
59
  var value = liveObject.get(key);
55
60
  if (void 0 === next) liveObject.delete(key); else if (void 0 === value) liveObject.set(key, anyToCrdt(next)); else {
56
61
  if (prev === next) return;
57
- LiveObject.isLiveList(value) && Array.isArray(prev) && Array.isArray(next) ? function(liveList, prev, next) {
62
+ value instanceof LiveObject.LiveList && Array.isArray(prev) && Array.isArray(next) ? function(liveList, prev, next) {
58
63
  var i = 0, prevEnd = prev.length - 1, nextEnd = next.length - 1, prevNode = prev[0], nextNode = next[0];
59
64
  outer: {
60
65
  for (;prevNode === nextNode; ) {
@@ -74,13 +79,13 @@ function patchLiveObjectKey(liveObject, key, prev, next) {
74
79
  for (;i <= prevEnd && i <= nextEnd; ) {
75
80
  prevNode = prev[i], nextNode = next[i];
76
81
  var liveListNode = liveList.get(i);
77
- LiveObject.isLiveObject(liveListNode) && isPlainObject(prevNode) && isPlainObject(nextNode) ? patchLiveObject(liveListNode, prevNode, nextNode) : liveList.set(i, anyToCrdt(nextNode)),
82
+ liveListNode instanceof LiveObject.LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode) ? patchLiveObject(liveListNode, prevNode, nextNode) : liveList.set(i, anyToCrdt(nextNode)),
78
83
  i++;
79
84
  }
80
85
  for (;i <= nextEnd; ) liveList.insert(anyToCrdt(next[i]), i), i++;
81
86
  for (var _localI = i; _localI <= prevEnd; ) liveList.delete(i), _localI++;
82
87
  }
83
- }(value, prev, next) : LiveObject.isLiveObject(value) && isPlainObject(prev) && isPlainObject(next) ? patchLiveObject(value, prev, next) : liveObject.set(key, anyToCrdt(next));
88
+ }(value, prev, next) : value instanceof LiveObject.LiveObject && isPlainObject(prev) && isPlainObject(next) ? patchLiveObject(value, prev, next) : liveObject.set(key, anyToCrdt(next));
84
89
  }
85
90
  }
86
91
 
@@ -123,10 +128,7 @@ function patchImmutableNode(state, path, update) {
123
128
  var _newState2 = Object.assign({}, state);
124
129
  for (var _key7 in update.updates) {
125
130
  var _update$updates$_key3, _update$updates$_key4;
126
- if ("update" === (null == (_update$updates$_key3 = update.updates[_key7]) ? void 0 : _update$updates$_key3.type)) {
127
- var value = update.node.get(_key7);
128
- void 0 !== value && (_newState2[_key7] = lsonToJson(value));
129
- } else "delete" === (null == (_update$updates$_key4 = update.updates[_key7]) ? void 0 : _update$updates$_key4.type) && delete _newState2[_key7];
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];
130
132
  }
131
133
  return _newState2;
132
134
  }
@@ -167,14 +169,14 @@ Object.defineProperty(exports, "ClientMsgCode", {
167
169
  exports.comparePosition = LiveObject.comparePosition, exports.deprecate = LiveObject.deprecate,
168
170
  exports.deprecateIf = LiveObject.deprecateIf, exports.errorIf = LiveObject.errorIf,
169
171
  exports.isChildCrdt = LiveObject.isChildCrdt, exports.isRootCrdt = LiveObject.isRootCrdt,
170
- exports.makePosition = LiveObject.makePosition, exports.nn = LiveObject.nn, exports.throwUsageError = LiveObject.throwUsageError,
172
+ exports.makePosition = LiveObject.makePosition, exports.throwUsageError = LiveObject.throwUsageError,
171
173
  exports.tryParseJson = LiveObject.tryParseJson, exports.lsonToJson = lsonToJson,
172
174
  exports.patchImmutableObject = function(state, updates) {
173
175
  return updates.reduce((function(state, update) {
174
176
  return function(state, update) {
175
177
  var path = function(node) {
176
- for (var path = []; "HasParent" === node.parent.type; ) LiveObject.isLiveList(node.parent.node) ? path.push(node.parent.node._indexOfPosition(node.parent.key)) : path.push(node.parent.key),
177
- node = node.parent.node;
178
+ 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),
179
+ node = node._parent;
178
180
  return path;
179
181
  }(update.node);
180
182
  return patchImmutableNode(state, path, update);
package/internal.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { L as LiveObject, o as LiveList, p as LiveMap, q as LiveRegister, s as findNonSerializableValue, d as isLiveList, u as isLiveObject } from "./shared.mjs";
1
+ import { L as LiveObject, b as LiveList, k as LiveMap, l as LiveRegister, A as AbstractCrdt, n as findNonSerializableValue } from "./shared.mjs";
2
2
 
3
- export { C as ClientMsgCode, B as CrdtType, c as OpCode, S as ServerMsgCode, W as WebsocketCloseCodes, v as assertNever, h as b64decode, z as comparePosition, w as deprecate, x as deprecateIf, l as errorIf, D as isChildCrdt, k as isRootCrdt, A as makePosition, n as nn, y as throwUsageError, t as tryParseJson } from "./shared.mjs";
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, h as isRootCrdt, v as makePosition, q as throwUsageError, t as tryParseJson } from "./shared.mjs";
4
4
 
5
5
  function lsonObjectToJson(obj) {
6
6
  const result = {};
@@ -16,13 +16,18 @@ function lsonListToJson(value) {
16
16
  }
17
17
 
18
18
  function lsonToJson(value) {
19
- return value instanceof LiveObject ? lsonObjectToJson(value.toObject()) : value instanceof LiveList ? function(value) {
19
+ if (value instanceof LiveObject) return lsonObjectToJson(value.toObject());
20
+ if (value instanceof LiveList) return function(value) {
20
21
  return lsonListToJson(value.toArray());
21
- }(value) : value instanceof LiveMap ? function(map) {
22
+ }(value);
23
+ if (value instanceof LiveMap) return function(map) {
22
24
  const result = {};
23
25
  for (const [key, value] of map.entries()) result[key] = lsonToJson(value);
24
26
  return result;
25
- }(value) : value instanceof LiveRegister ? value.data : Array.isArray(value) ? lsonListToJson(value) : isPlainObject(value) ? lsonObjectToJson(value) : value;
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;
26
31
  }
27
32
 
28
33
  function isPlainObject(obj) {
@@ -48,7 +53,7 @@ function patchLiveObjectKey(liveObject, key, prev, next) {
48
53
  const value = liveObject.get(key);
49
54
  if (void 0 === next) liveObject.delete(key); else if (void 0 === value) liveObject.set(key, anyToCrdt(next)); else {
50
55
  if (prev === next) return;
51
- isLiveList(value) && Array.isArray(prev) && Array.isArray(next) ? function(liveList, prev, next) {
56
+ value instanceof LiveList && Array.isArray(prev) && Array.isArray(next) ? function(liveList, prev, next) {
52
57
  let i = 0, prevEnd = prev.length - 1, nextEnd = next.length - 1, prevNode = prev[0], nextNode = next[0];
53
58
  outer: {
54
59
  for (;prevNode === nextNode; ) {
@@ -70,14 +75,14 @@ function patchLiveObjectKey(liveObject, key, prev, next) {
70
75
  for (;i <= prevEnd && i <= nextEnd; ) {
71
76
  prevNode = prev[i], nextNode = next[i];
72
77
  const liveListNode = liveList.get(i);
73
- isLiveObject(liveListNode) && isPlainObject(prevNode) && isPlainObject(nextNode) ? patchLiveObject(liveListNode, prevNode, nextNode) : liveList.set(i, anyToCrdt(nextNode)),
78
+ liveListNode instanceof LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode) ? patchLiveObject(liveListNode, prevNode, nextNode) : liveList.set(i, anyToCrdt(nextNode)),
74
79
  i++;
75
80
  }
76
81
  for (;i <= nextEnd; ) liveList.insert(anyToCrdt(next[i]), i), i++;
77
82
  let localI = i;
78
83
  for (;localI <= prevEnd; ) liveList.delete(i), localI++;
79
84
  }
80
- }(value, prev, next) : isLiveObject(value) && isPlainObject(prev) && isPlainObject(next) ? patchLiveObject(value, prev, next) : liveObject.set(key, anyToCrdt(next));
85
+ }(value, prev, next) : value instanceof LiveObject && isPlainObject(prev) && isPlainObject(next) ? patchLiveObject(value, prev, next) : liveObject.set(key, anyToCrdt(next));
81
86
  }
82
87
  }
83
88
 
@@ -92,8 +97,8 @@ function patchImmutableObject(state, updates) {
92
97
  return updates.reduce(((state, update) => function(state, update) {
93
98
  const path = function(node) {
94
99
  const path = [];
95
- for (;"HasParent" === node.parent.type; ) isLiveList(node.parent.node) ? path.push(node.parent.node._indexOfPosition(node.parent.key)) : path.push(node.parent.key),
96
- node = node.parent.node;
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;
97
102
  return path;
98
103
  }(update.node);
99
104
  return patchImmutableNode(state, path, update);
@@ -127,10 +132,7 @@ function patchImmutableNode(state, path, update) {
127
132
  {
128
133
  if ("object" != typeof state) throw new Error("Internal: received update on LiveMap but state was not an object");
129
134
  const newState = Object.assign({}, state);
130
- for (const key in update.updates) if ("update" === (null === (_c = update.updates[key]) || void 0 === _c ? void 0 : _c.type)) {
131
- const value = update.node.get(key);
132
- void 0 !== value && (newState[key] = lsonToJson(value));
133
- } else "delete" === (null === (_d = update.updates[key]) || void 0 === _d ? void 0 : _d.type) && delete newState[key];
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];
134
136
  return newState;
135
137
  }
136
138
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/client",
3
- "version": "0.16.13",
3
+ "version": "0.16.14",
4
4
  "description": "A client that lets you interact with Liveblocks servers.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
@@ -33,9 +33,8 @@
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 --silent --verbose --config=./jest.config.js",
37
- "test-ci": "jest --silent --verbose",
38
- "test-e2e": "jest --silent --verbose --config=./jest.config.e2e.js"
36
+ "test": "jest --watch",
37
+ "test-ci": "jest"
39
38
  },
40
39
  "license": "Apache-2.0",
41
40
  "devDependencies": {
@@ -46,26 +45,23 @@
46
45
  "@rollup/plugin-node-resolve": "^13.1.3",
47
46
  "@rollup/plugin-replace": "^4.0.0",
48
47
  "@rollup/plugin-typescript": "^8.3.1",
49
- "@types/jest": "^26.0.24",
48
+ "@types/jest": "^26.0.21",
50
49
  "@types/node-fetch": "^2.6.1",
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",
50
+ "@types/ws": "^8.2.2",
51
+ "@typescript-eslint/eslint-plugin": "^5.17.0",
52
+ "@typescript-eslint/parser": "^5.17.0",
55
53
  "eslint": "^8.12.0",
56
54
  "eslint-plugin-import": "^2.26.0",
57
55
  "eslint-plugin-simple-import-sort": "^7.0.0",
58
- "jest": "^28.0.3",
56
+ "jest": "^26.6.3",
59
57
  "jest-each": "^27.5.1",
60
- "jest-environment-jsdom": "^28.1.0",
61
58
  "msw": "^0.39.1",
62
59
  "node-fetch": "2.6.7",
63
- "regenerator-runtime": "^0.13.9",
64
60
  "rollup": "^2.68.0",
65
61
  "rollup-plugin-command": "^1.1.3",
66
62
  "rollup-plugin-dts": "^4.1.0",
67
63
  "rollup-plugin-terser": "^7.0.2",
68
- "typescript": "^4.7.2",
64
+ "typescript": "^4.4.0",
69
65
  "whatwg-fetch": "^3.6.2",
70
66
  "ws": "^8.5.0"
71
67
  },