@liveblocks/client 0.16.8 → 0.16.11

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 CHANGED
@@ -28,4 +28,16 @@ export { B as BroadcastOptions, a as Client, H as History, J as Json, d as JsonO
28
28
  */
29
29
  declare function createClient(options: ClientOptions): Client;
30
30
 
31
- export { createClient };
31
+ /**
32
+ * Helper type to help users adopt to Lson types from interface definitions.
33
+ * You should only use this to wrap interfaces you don't control. For more
34
+ * information, see
35
+ * https://liveblocks.io/docs/guides/limits#lson-constraint-and-interfaces
36
+ */
37
+ declare type EnsureJson<T> = [
38
+ unknown
39
+ ] extends [T] ? T : T extends (...args: any[]) => any ? T : {
40
+ [K in keyof T]: EnsureJson<T[K]>;
41
+ };
42
+
43
+ export { EnsureJson, createClient };
package/index.js CHANGED
@@ -78,10 +78,10 @@ function makeStateMachine(state, context, mockedEffects) {
78
78
  })), notify(apply(LiveObject.getTreesDiffOperations(currentItems, new Map(items)), !1).updates);
79
79
  }(message.items) : state.root = (items = message.items, _buildRootAndParentTo = function(items) {
80
80
  for (var _step2, parentToChildren = new Map, root = null, _iterator2 = LiveObject._createForOfIteratorHelperLoose(items); !(_step2 = _iterator2()).done; ) {
81
- var tuple = _step2.value, parentId = tuple[1].parentId;
82
- if (null == parentId) root = tuple; else {
83
- var children = parentToChildren.get(parentId);
84
- null != children ? children.push(tuple) : parentToChildren.set(parentId, [ tuple ]);
81
+ var _step2$value = _step2.value, id = _step2$value[0], crdt = _step2$value[1];
82
+ if (LiveObject.isRootCrdt(crdt)) root = [ id, crdt ]; else {
83
+ var tuple = [ id, crdt ], children = parentToChildren.get(crdt.parentId);
84
+ null != children ? children.push(tuple) : parentToChildren.set(crdt.parentId, [ tuple ]);
85
85
  }
86
86
  }
87
87
  if (null == root) throw new Error("Root can't be null");
@@ -170,7 +170,7 @@ function makeStateMachine(state, context, mockedEffects) {
170
170
  var _applyOpResult$modifi, _result$reverse, parentId = null == (_applyOpResult$modifi = applyOpResult.modified.node._parent) ? void 0 : _applyOpResult$modifi._id;
171
171
  if (!createdNodeIds.has(parentId)) result.updates.storageUpdates.set(applyOpResult.modified.node._id, LiveObject.mergeStorageUpdates(result.updates.storageUpdates.get(applyOpResult.modified.node._id), applyOpResult.modified)),
172
172
  (_result$reverse = result.reverse).unshift.apply(_result$reverse, applyOpResult.reverse);
173
- op.type !== LiveObject.OpType.CreateList && op.type !== LiveObject.OpType.CreateMap && op.type !== LiveObject.OpType.CreateObject || createdNodeIds.add(applyOpResult.modified.node._id);
173
+ op.type !== LiveObject.OpCode.CREATE_LIST && op.type !== LiveObject.OpCode.CREATE_MAP && op.type !== LiveObject.OpCode.CREATE_OBJECT || createdNodeIds.add(applyOpResult.modified.node._id);
174
174
  }
175
175
  }
176
176
  }
@@ -178,15 +178,15 @@ function makeStateMachine(state, context, mockedEffects) {
178
178
  }
179
179
  function applyOp(op, isLocal) {
180
180
  switch (op.opId && state.offlineOperations.delete(op.opId), op.type) {
181
- case LiveObject.OpType.DeleteObjectKey:
182
- case LiveObject.OpType.UpdateObject:
183
- case LiveObject.OpType.DeleteCrdt:
181
+ case LiveObject.OpCode.DELETE_OBJECT_KEY:
182
+ case LiveObject.OpCode.UPDATE_OBJECT:
183
+ case LiveObject.OpCode.DELETE_CRDT:
184
184
  var item = state.items.get(op.id);
185
185
  return null == item ? {
186
186
  modified: !1
187
187
  } : item._apply(op, isLocal);
188
188
 
189
- case LiveObject.OpType.SetParentKey:
189
+ case LiveObject.OpCode.SET_PARENT_KEY:
190
190
  var _item = state.items.get(op.id);
191
191
  if (null == _item) return {
192
192
  modified: !1
@@ -201,10 +201,10 @@ function makeStateMachine(state, context, mockedEffects) {
201
201
  modified: !1
202
202
  };
203
203
 
204
- case LiveObject.OpType.CreateObject:
205
- case LiveObject.OpType.CreateList:
206
- case LiveObject.OpType.CreateMap:
207
- case LiveObject.OpType.CreateRegister:
204
+ case LiveObject.OpCode.CREATE_OBJECT:
205
+ case LiveObject.OpCode.CREATE_LIST:
206
+ case LiveObject.OpCode.CREATE_MAP:
207
+ case LiveObject.OpCode.CREATE_REGISTER:
208
208
  var parent = state.items.get(op.parentId);
209
209
  return null == parent ? {
210
210
  modified: !1
@@ -311,7 +311,7 @@ function makeStateMachine(state, context, mockedEffects) {
311
311
  id: message.id,
312
312
  _hasReceivedInitialPresence: !0
313
313
  }, state.me && (state.buffer.messages.push({
314
- type: LiveObject.ClientMessageType.UpdatePresence,
314
+ type: LiveObject.ClientMsgCode.UPDATE_PRESENCE,
315
315
  data: state.me,
316
316
  targetActor: message.actor
317
317
  }), tryFlushing()), {
@@ -334,34 +334,34 @@ function makeStateMachine(state, context, mockedEffects) {
334
334
  }, _iterator9 = LiveObject._createForOfIteratorHelperLoose(messages); !(_step9 = _iterator9()).done; ) {
335
335
  var message = _step9.value;
336
336
  switch (message.type) {
337
- case LiveObject.ServerMessageType.UserJoined:
337
+ case LiveObject.ServerMsgCode.USER_JOINED:
338
338
  updates.others.push(onUserJoinedMessage(message));
339
339
  break;
340
340
 
341
- case LiveObject.ServerMessageType.UpdatePresence:
341
+ case LiveObject.ServerMsgCode.UPDATE_PRESENCE:
342
342
  var othersPresenceUpdate = onUpdatePresenceMessage(message);
343
343
  othersPresenceUpdate && updates.others.push(othersPresenceUpdate);
344
344
  break;
345
345
 
346
- case LiveObject.ServerMessageType.Event:
346
+ case LiveObject.ServerMsgCode.BROADCASTED_EVENT:
347
347
  onEvent(message);
348
348
  break;
349
349
 
350
- case LiveObject.ServerMessageType.UserLeft:
350
+ case LiveObject.ServerMsgCode.USER_LEFT:
351
351
  var _event2 = onUserLeftMessage(message);
352
352
  _event2 && updates.others.push(_event2);
353
353
  break;
354
354
 
355
- case LiveObject.ServerMessageType.RoomState:
355
+ case LiveObject.ServerMsgCode.ROOM_STATE:
356
356
  updates.others.push(onRoomStateMessage(message));
357
357
  break;
358
358
 
359
- case LiveObject.ServerMessageType.InitialStorageState:
359
+ case LiveObject.ServerMsgCode.INITIAL_STORAGE_STATE:
360
360
  var offlineOps = new Map(state.offlineOperations);
361
361
  createOrUpdateRootFromMessage(message), applyAndSendOfflineOps(offlineOps), null == _getInitialStateResolver || _getInitialStateResolver();
362
362
  break;
363
363
 
364
- case LiveObject.ServerMessageType.UpdateStorage:
364
+ case LiveObject.ServerMsgCode.UPDATE_STORAGE:
365
365
  apply(message.ops, !1).updates.storageUpdates.forEach((function(value, key) {
366
366
  updates.storageUpdates.set(key, LiveObject.mergeStorageUpdates(updates.storageUpdates.get(key), value));
367
367
  }));
@@ -416,7 +416,7 @@ function makeStateMachine(state, context, mockedEffects) {
416
416
  state: "open"
417
417
  })), state.numberOfRetry = 0, void 0 !== state.lastConnectionId && (state.buffer.presence = state.me,
418
418
  tryFlushing()), state.lastConnectionId = state.connection.id, state.root && state.buffer.messages.push({
419
- type: LiveObject.ClientMessageType.FetchStorage
419
+ type: LiveObject.ClientMsgCode.FETCH_STORAGE
420
420
  }), tryFlushing());
421
421
  }
422
422
  function heartbeat() {
@@ -439,7 +439,7 @@ function makeStateMachine(state, context, mockedEffects) {
439
439
  if (0 !== offlineOps.size) {
440
440
  var messages = [], ops = Array.from(offlineOps.values()), result = apply(ops, !0);
441
441
  messages.push({
442
- type: LiveObject.ClientMessageType.UpdateStorage,
442
+ type: LiveObject.ClientMsgCode.UPDATE_STORAGE,
443
443
  ops: ops
444
444
  }), notify(result.updates), effects.send(messages);
445
445
  }
@@ -454,7 +454,7 @@ function makeStateMachine(state, context, mockedEffects) {
454
454
  var _messages = function(state) {
455
455
  var messages = [];
456
456
  state.buffer.presence && messages.push({
457
- type: LiveObject.ClientMessageType.UpdatePresence,
457
+ type: LiveObject.ClientMsgCode.UPDATE_PRESENCE,
458
458
  data: state.buffer.presence
459
459
  });
460
460
  for (var _step12, _iterator12 = LiveObject._createForOfIteratorHelperLoose(state.buffer.messages); !(_step12 = _iterator12()).done; ) {
@@ -462,7 +462,7 @@ function makeStateMachine(state, context, mockedEffects) {
462
462
  messages.push(_event3);
463
463
  }
464
464
  state.buffer.storageOperations.length > 0 && messages.push({
465
- type: LiveObject.ClientMessageType.UpdateStorage,
465
+ type: LiveObject.ClientMsgCode.UPDATE_STORAGE,
466
466
  ops: state.buffer.storageOperations
467
467
  });
468
468
  return messages;
@@ -566,7 +566,7 @@ function makeStateMachine(state, context, mockedEffects) {
566
566
  void 0 === options && (options = {
567
567
  shouldQueueEventIfNotReady: !1
568
568
  }), null == state.socket && 0 == options.shouldQueueEventIfNotReady || (state.buffer.messages.push({
569
- type: LiveObject.ClientMessageType.ClientEvent,
569
+ type: LiveObject.ClientMsgCode.BROADCAST_EVENT,
570
570
  event: event
571
571
  }), tryFlushing());
572
572
  },
@@ -630,7 +630,7 @@ function makeStateMachine(state, context, mockedEffects) {
630
630
  root: state.root
631
631
  });
632
632
  })) : (null == _getInitialStatePromise && (state.buffer.messages.push({
633
- type: LiveObject.ClientMessageType.FetchStorage
633
+ type: LiveObject.ClientMsgCode.FETCH_STORAGE
634
634
  }), tryFlushing(), _getInitialStatePromise = new Promise((function(resolve) {
635
635
  return _getInitialStateResolver = resolve;
636
636
  }))), _getInitialStatePromise.then((function() {
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { A as AbstractCrdt, r as remove, S as ServerMessageType, m as mergeStorageUpdates, W as WebsocketCloseCodes, C as ClientMessageType, i as isTokenValid, a as isSameNodeOrChildOf, L as LiveObject, g as getTreesDiffOperations, O as OpType, b as LiveList, p as parseJson, c as isJsonArray, d as compact, e as isJsonObject, f as deprecateIf } from "./shared.mjs";
1
+ import { A as AbstractCrdt, r as remove, S as ServerMsgCode, m as mergeStorageUpdates, W as WebsocketCloseCodes, C as ClientMsgCode, i as isTokenValid, a as isSameNodeOrChildOf, L as LiveObject, g as getTreesDiffOperations, O as OpCode, b as LiveList, p as parseJson, c as isJsonArray, d as compact, e as isJsonObject, f as isRootCrdt, h as deprecateIf } from "./shared.mjs";
2
2
 
3
- export { b as LiveList, h as LiveMap, L as LiveObject } from "./shared.mjs";
3
+ export { b as LiveList, j as LiveMap, L as LiveObject } from "./shared.mjs";
4
4
 
5
5
  const BACKOFF_RETRY_DELAYS = [ 250, 500, 1e3, 2e3, 4e3, 8e3, 1e4 ], BACKOFF_RETRY_DELAYS_SLOW = [ 2e3, 3e4, 6e4, 3e5 ];
6
6
 
@@ -69,12 +69,9 @@ function makeStateMachine(state, context, mockedEffects) {
69
69
  const [root, parentToChildren] = function(items) {
70
70
  const parentToChildren = new Map;
71
71
  let root = null;
72
- for (const tuple of items) {
73
- const parentId = tuple[1].parentId;
74
- if (null == parentId) root = tuple; else {
75
- const children = parentToChildren.get(parentId);
76
- null != children ? children.push(tuple) : parentToChildren.set(parentId, [ tuple ]);
77
- }
72
+ for (const [id, crdt] of items) if (isRootCrdt(crdt)) root = [ id, crdt ]; else {
73
+ const tuple = [ id, crdt ], children = parentToChildren.get(crdt.parentId);
74
+ null != children ? children.push(tuple) : parentToChildren.set(crdt.parentId, [ tuple ]);
78
75
  }
79
76
  if (null == root) throw new Error("Root can't be null");
80
77
  return [ root, parentToChildren ];
@@ -153,16 +150,16 @@ function makeStateMachine(state, context, mockedEffects) {
153
150
  if (applyOpResult.modified) {
154
151
  const parentId = null === (_a = applyOpResult.modified.node._parent) || void 0 === _a ? void 0 : _a._id;
155
152
  createdNodeIds.has(parentId) || (result.updates.storageUpdates.set(applyOpResult.modified.node._id, mergeStorageUpdates(result.updates.storageUpdates.get(applyOpResult.modified.node._id), applyOpResult.modified)),
156
- result.reverse.unshift(...applyOpResult.reverse)), op.type !== OpType.CreateList && op.type !== OpType.CreateMap && op.type !== OpType.CreateObject || createdNodeIds.add(applyOpResult.modified.node._id);
153
+ result.reverse.unshift(...applyOpResult.reverse)), op.type !== OpCode.CREATE_LIST && op.type !== OpCode.CREATE_MAP && op.type !== OpCode.CREATE_OBJECT || createdNodeIds.add(applyOpResult.modified.node._id);
157
154
  }
158
155
  }
159
156
  return result;
160
157
  }
161
158
  function applyOp(op, isLocal) {
162
159
  switch (op.opId && state.offlineOperations.delete(op.opId), op.type) {
163
- case OpType.DeleteObjectKey:
164
- case OpType.UpdateObject:
165
- case OpType.DeleteCrdt:
160
+ case OpCode.DELETE_OBJECT_KEY:
161
+ case OpCode.UPDATE_OBJECT:
162
+ case OpCode.DELETE_CRDT:
166
163
  {
167
164
  const item = state.items.get(op.id);
168
165
  return null == item ? {
@@ -170,7 +167,7 @@ function makeStateMachine(state, context, mockedEffects) {
170
167
  } : item._apply(op, isLocal);
171
168
  }
172
169
 
173
- case OpType.SetParentKey:
170
+ case OpCode.SET_PARENT_KEY:
174
171
  {
175
172
  const item = state.items.get(op.id);
176
173
  if (null == item) return {
@@ -187,10 +184,10 @@ function makeStateMachine(state, context, mockedEffects) {
187
184
  };
188
185
  }
189
186
 
190
- case OpType.CreateObject:
191
- case OpType.CreateList:
192
- case OpType.CreateMap:
193
- case OpType.CreateRegister:
187
+ case OpCode.CREATE_OBJECT:
188
+ case OpCode.CREATE_LIST:
189
+ case OpCode.CREATE_MAP:
190
+ case OpCode.CREATE_REGISTER:
194
191
  {
195
192
  const parent = state.items.get(op.parentId);
196
193
  return null == parent ? {
@@ -291,7 +288,7 @@ function makeStateMachine(state, context, mockedEffects) {
291
288
  id: message.id,
292
289
  _hasReceivedInitialPresence: !0
293
290
  }, state.me && (state.buffer.messages.push({
294
- type: ClientMessageType.UpdatePresence,
291
+ type: ClientMsgCode.UPDATE_PRESENCE,
295
292
  data: state.me,
296
293
  targetActor: message.actor
297
294
  }), tryFlushing()), {
@@ -314,40 +311,40 @@ function makeStateMachine(state, context, mockedEffects) {
314
311
  others: []
315
312
  };
316
313
  for (const message of messages) switch (message.type) {
317
- case ServerMessageType.UserJoined:
314
+ case ServerMsgCode.USER_JOINED:
318
315
  updates.others.push(onUserJoinedMessage(message));
319
316
  break;
320
317
 
321
- case ServerMessageType.UpdatePresence:
318
+ case ServerMsgCode.UPDATE_PRESENCE:
322
319
  {
323
320
  const othersPresenceUpdate = onUpdatePresenceMessage(message);
324
321
  othersPresenceUpdate && updates.others.push(othersPresenceUpdate);
325
322
  break;
326
323
  }
327
324
 
328
- case ServerMessageType.Event:
325
+ case ServerMsgCode.BROADCASTED_EVENT:
329
326
  onEvent(message);
330
327
  break;
331
328
 
332
- case ServerMessageType.UserLeft:
329
+ case ServerMsgCode.USER_LEFT:
333
330
  {
334
331
  const event = onUserLeftMessage(message);
335
332
  event && updates.others.push(event);
336
333
  break;
337
334
  }
338
335
 
339
- case ServerMessageType.RoomState:
336
+ case ServerMsgCode.ROOM_STATE:
340
337
  updates.others.push(onRoomStateMessage(message));
341
338
  break;
342
339
 
343
- case ServerMessageType.InitialStorageState:
340
+ case ServerMsgCode.INITIAL_STORAGE_STATE:
344
341
  {
345
342
  const offlineOps = new Map(state.offlineOperations);
346
343
  createOrUpdateRootFromMessage(message), applyAndSendOfflineOps(offlineOps), null == _getInitialStateResolver || _getInitialStateResolver();
347
344
  break;
348
345
  }
349
346
 
350
- case ServerMessageType.UpdateStorage:
347
+ case ServerMsgCode.UPDATE_STORAGE:
351
348
  apply(message.ops, !1).updates.storageUpdates.forEach(((value, key) => {
352
349
  updates.storageUpdates.set(key, mergeStorageUpdates(updates.storageUpdates.get(key), value));
353
350
  }));
@@ -397,7 +394,7 @@ function makeStateMachine(state, context, mockedEffects) {
397
394
  state: "open"
398
395
  })), state.numberOfRetry = 0, void 0 !== state.lastConnectionId && (state.buffer.presence = state.me,
399
396
  tryFlushing()), state.lastConnectionId = state.connection.id, state.root && state.buffer.messages.push({
400
- type: ClientMessageType.FetchStorage
397
+ type: ClientMsgCode.FETCH_STORAGE
401
398
  }), tryFlushing());
402
399
  }
403
400
  function heartbeat() {
@@ -420,7 +417,7 @@ function makeStateMachine(state, context, mockedEffects) {
420
417
  if (0 === offlineOps.size) return;
421
418
  const messages = [], ops = Array.from(offlineOps.values()), result = apply(ops, !0);
422
419
  messages.push({
423
- type: ClientMessageType.UpdateStorage,
420
+ type: ClientMsgCode.UPDATE_STORAGE,
424
421
  ops: ops
425
422
  }), notify(result.updates), effects.send(messages);
426
423
  }
@@ -434,12 +431,12 @@ function makeStateMachine(state, context, mockedEffects) {
434
431
  const messages = function(state) {
435
432
  const messages = [];
436
433
  state.buffer.presence && messages.push({
437
- type: ClientMessageType.UpdatePresence,
434
+ type: ClientMsgCode.UPDATE_PRESENCE,
438
435
  data: state.buffer.presence
439
436
  });
440
437
  for (const event of state.buffer.messages) messages.push(event);
441
438
  state.buffer.storageOperations.length > 0 && messages.push({
442
- type: ClientMessageType.UpdateStorage,
439
+ type: ClientMsgCode.UPDATE_STORAGE,
443
440
  ops: state.buffer.storageOperations
444
441
  });
445
442
  return messages;
@@ -534,7 +531,7 @@ function makeStateMachine(state, context, mockedEffects) {
534
531
  shouldQueueEventIfNotReady: !1
535
532
  }) {
536
533
  null == state.socket && 0 == options.shouldQueueEventIfNotReady || (state.buffer.messages.push({
537
- type: ClientMessageType.ClientEvent,
534
+ type: ClientMsgCode.BROADCAST_EVENT,
538
535
  event: event
539
536
  }), tryFlushing());
540
537
  },
@@ -588,7 +585,7 @@ function makeStateMachine(state, context, mockedEffects) {
588
585
  return state.root ? new Promise((resolve => resolve({
589
586
  root: state.root
590
587
  }))) : (null == _getInitialStatePromise && (state.buffer.messages.push({
591
- type: ClientMessageType.FetchStorage
588
+ type: ClientMsgCode.FETCH_STORAGE
592
589
  }), tryFlushing(), _getInitialStatePromise = new Promise((resolve => _getInitialStateResolver = resolve))),
593
590
  _getInitialStatePromise.then((() => ({
594
591
  root: state.root
package/internal.d.ts CHANGED
@@ -1,26 +1,38 @@
1
- import { d as JsonObject, J as Json, 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';
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, p as parseJson } from './shared';
3
3
 
4
+ declare type IdTuple<T> = [id: string, value: T];
5
+ /**
6
+ * Lookup table for nodes (= SerializedCrdt values) by their IDs.
7
+ */
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>[]>;
4
16
  /**
5
17
  * Messages that can be sent from the server to the client.
6
18
  */
7
- declare type ServerMessage<TPresence extends JsonObject> = UpdatePresenceMessage<TPresence> | UserJoinMessage | UserLeftMessage | EventMessage | RoomStateMessage | InitialDocumentStateMessage | UpdateStorageMessage;
8
- declare enum ServerMessageType {
9
- UpdatePresence = 100,
10
- UserJoined = 101,
11
- UserLeft = 102,
12
- Event = 103,
13
- RoomState = 104,
14
- InitialStorageState = 200,
15
- UpdateStorage = 201
19
+ declare type ServerMsg<TPresence extends JsonObject> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg | UserLeftServerMsg | BroadcastedEventServerMsg | RoomStateServerMsg | InitialDocumentStateServerMsg | UpdateStorageServerMsg;
20
+ declare enum ServerMsgCode {
21
+ UPDATE_PRESENCE = 100,
22
+ USER_JOINED = 101,
23
+ USER_LEFT = 102,
24
+ BROADCASTED_EVENT = 103,
25
+ ROOM_STATE = 104,
26
+ INITIAL_STORAGE_STATE = 200,
27
+ UPDATE_STORAGE = 201
16
28
  }
17
29
  /**
18
30
  * Sent by the WebSocket server to a single client in response to the client
19
31
  * joining the Room, to provide the initial state of the Room. The payload
20
32
  * includes a list of all other Users that already are in the Room.
21
33
  */
22
- declare type RoomStateMessage = {
23
- type: ServerMessageType.RoomState;
34
+ declare type RoomStateServerMsg = {
35
+ type: ServerMsgCode.ROOM_STATE;
24
36
  users: {
25
37
  [actor: number]: {
26
38
  id?: string;
@@ -39,8 +51,8 @@ declare type RoomStateMessage = {
39
51
  * those cases, the `targetActor` field indicates the newly connected client,
40
52
  * so all other existing clients can ignore this broadcasted message.
41
53
  */
42
- declare type UpdatePresenceMessage<TPresence extends JsonObject> = {
43
- type: ServerMessageType.UpdatePresence;
54
+ declare type UpdatePresenceServerMsg<TPresence extends JsonObject> = {
55
+ type: ServerMsgCode.UPDATE_PRESENCE;
44
56
  /**
45
57
  * The User whose Presence has changed.
46
58
  */
@@ -62,8 +74,8 @@ declare type UpdatePresenceMessage<TPresence extends JsonObject> = {
62
74
  * Sent by the WebSocket server and broadcasted to all clients to announce that
63
75
  * a new User has joined the Room.
64
76
  */
65
- declare type UserJoinMessage = {
66
- type: ServerMessageType.UserJoined;
77
+ declare type UserJoinServerMsg = {
78
+ type: ServerMsgCode.USER_JOINED;
67
79
  actor: number;
68
80
  /**
69
81
  * The id of the User that has been set in the authentication endpoint.
@@ -80,16 +92,16 @@ declare type UserJoinMessage = {
80
92
  * Sent by the WebSocket server and broadcasted to all clients to announce that
81
93
  * a new User has left the Room.
82
94
  */
83
- declare type UserLeftMessage = {
84
- type: ServerMessageType.UserLeft;
95
+ declare type UserLeftServerMsg = {
96
+ type: ServerMsgCode.USER_LEFT;
85
97
  actor: number;
86
98
  };
87
99
  /**
88
100
  * Sent by the WebSocket server and broadcasted to all clients to announce that
89
101
  * a User broadcasted an Event to everyone in the Room.
90
102
  */
91
- declare type EventMessage = {
92
- type: ServerMessageType.Event;
103
+ declare type BroadcastedEventServerMsg = {
104
+ type: ServerMsgCode.BROADCASTED_EVENT;
93
105
  /**
94
106
  * The User who broadcasted the Event.
95
107
  */
@@ -100,15 +112,14 @@ declare type EventMessage = {
100
112
  */
101
113
  event: Json;
102
114
  };
103
- declare type SerializedCrdtWithId = [id: string, crdt: SerializedCrdt];
104
115
  /**
105
116
  * Sent by the WebSocket server to a single client in response to the client
106
117
  * joining the Room, to provide the initial Storage state of the Room. The
107
118
  * payload includes the entire Storage document.
108
119
  */
109
- declare type InitialDocumentStateMessage = {
110
- type: ServerMessageType.InitialStorageState;
111
- items: SerializedCrdtWithId[];
120
+ declare type InitialDocumentStateServerMsg = {
121
+ type: ServerMsgCode.INITIAL_STORAGE_STATE;
122
+ items: IdTuple<SerializedCrdt>[];
112
123
  };
113
124
  /**
114
125
  * Sent by the WebSocket server and broadcasted to all clients to announce that
@@ -117,81 +128,99 @@ declare type InitialDocumentStateMessage = {
117
128
  * The payload of this message contains a list of Ops (aka incremental
118
129
  * mutations to make to the initially loaded document).
119
130
  */
120
- declare type UpdateStorageMessage = {
121
- type: ServerMessageType.UpdateStorage;
131
+ declare type UpdateStorageServerMsg = {
132
+ type: ServerMsgCode.UPDATE_STORAGE;
122
133
  ops: Op[];
123
134
  };
124
- declare enum ClientMessageType {
125
- UpdatePresence = 100,
126
- ClientEvent = 103,
127
- FetchStorage = 200,
128
- UpdateStorage = 201
135
+ declare enum ClientMsgCode {
136
+ UPDATE_PRESENCE = 100,
137
+ BROADCAST_EVENT = 103,
138
+ FETCH_STORAGE = 200,
139
+ UPDATE_STORAGE = 201
129
140
  }
141
+ declare type FetchStorageClientMsg = {
142
+ type: ClientMsgCode.FETCH_STORAGE;
143
+ };
130
144
  declare enum CrdtType {
131
- Object = 0,
132
- List = 1,
133
- Map = 2,
134
- Register = 3
145
+ OBJECT = 0,
146
+ LIST = 1,
147
+ MAP = 2,
148
+ REGISTER = 3
135
149
  }
150
+ declare type SerializedRootObject = {
151
+ type: CrdtType.OBJECT;
152
+ data: JsonObject;
153
+ parentId?: never;
154
+ parentKey?: never;
155
+ };
136
156
  declare type SerializedObject = {
137
- type: CrdtType.Object;
138
- parentId?: string;
139
- parentKey?: string;
157
+ type: CrdtType.OBJECT;
158
+ parentId: string;
159
+ parentKey: string;
140
160
  data: JsonObject;
141
161
  };
142
162
  declare type SerializedList = {
143
- type: CrdtType.List;
163
+ type: CrdtType.LIST;
144
164
  parentId: string;
145
165
  parentKey: string;
146
166
  };
147
167
  declare type SerializedMap = {
148
- type: CrdtType.Map;
168
+ type: CrdtType.MAP;
149
169
  parentId: string;
150
170
  parentKey: string;
151
171
  };
152
172
  declare type SerializedRegister = {
153
- type: CrdtType.Register;
173
+ type: CrdtType.REGISTER;
154
174
  parentId: string;
155
175
  parentKey: string;
156
176
  data: Json;
157
177
  };
158
- declare type SerializedCrdt = SerializedObject | SerializedList | SerializedMap | SerializedRegister;
159
- declare enum OpType {
160
- Init = 0,
161
- SetParentKey = 1,
162
- CreateList = 2,
163
- UpdateObject = 3,
164
- CreateObject = 4,
165
- DeleteCrdt = 5,
166
- DeleteObjectKey = 6,
167
- CreateMap = 7,
168
- CreateRegister = 8
178
+ declare type SerializedCrdt = SerializedRootObject | SerializedChild;
179
+ declare type SerializedChild = SerializedObject | SerializedList | SerializedMap | SerializedRegister;
180
+ declare function isRootCrdt(crdt: SerializedCrdt): crdt is SerializedRootObject;
181
+ declare function isChildCrdt(crdt: SerializedCrdt): crdt is SerializedChild;
182
+ declare enum OpCode {
183
+ INIT = 0,
184
+ SET_PARENT_KEY = 1,
185
+ CREATE_LIST = 2,
186
+ UPDATE_OBJECT = 3,
187
+ CREATE_OBJECT = 4,
188
+ DELETE_CRDT = 5,
189
+ DELETE_OBJECT_KEY = 6,
190
+ CREATE_MAP = 7,
191
+ CREATE_REGISTER = 8
169
192
  }
170
193
  /**
171
- * These operations are the payload for {@link UpdateStorageMessage} messages
194
+ * These operations are the payload for {@link UpdateStorageServerMsg} messages
172
195
  * only.
173
196
  */
174
- declare type Op = CreateObjectOp | UpdateObjectOp | DeleteCrdtOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp;
197
+ declare type Op = CreateOp | UpdateObjectOp | DeleteCrdtOp | SetParentKeyOp | DeleteObjectKeyOp;
198
+ declare type CreateOp = CreateRootObjectOp | CreateChildOp;
199
+ declare type CreateChildOp = CreateObjectOp | CreateRegisterOp | CreateMapOp | CreateListOp;
175
200
  declare type UpdateObjectOp = {
176
201
  opId?: string;
177
202
  id: string;
178
- type: OpType.UpdateObject;
203
+ type: OpCode.UPDATE_OBJECT;
179
204
  data: Partial<JsonObject>;
180
205
  };
181
206
  declare type CreateObjectOp = {
182
207
  opId?: string;
183
208
  id: string;
184
209
  intent?: "set";
185
- type: OpType.CreateObject;
186
- parentId?: string;
187
- parentKey?: string;
210
+ type: OpCode.CREATE_OBJECT;
211
+ parentId: string;
212
+ parentKey: string;
188
213
  data: JsonObject;
189
214
  };
215
+ declare type CreateRootObjectOp = Resolve<Omit<CreateObjectOp, "parentId" | "parentKey"> & {
216
+ parentId?: never;
217
+ parentKey?: never;
218
+ }>;
190
219
  declare type CreateListOp = {
191
220
  opId?: string;
192
221
  id: string;
193
222
  intent?: "set";
194
- type: OpType.CreateList;
223
+ type: OpCode.CREATE_LIST;
195
224
  parentId: string;
196
225
  parentKey: string;
197
226
  };
@@ -199,7 +228,7 @@ declare type CreateMapOp = {
199
228
  opId?: string;
200
229
  id: string;
201
230
  intent?: "set";
202
- type: OpType.CreateMap;
231
+ type: OpCode.CREATE_MAP;
203
232
  parentId: string;
204
233
  parentKey: string;
205
234
  };
@@ -207,7 +236,7 @@ declare type CreateRegisterOp = {
207
236
  opId?: string;
208
237
  id: string;
209
238
  intent?: "set";
210
- type: OpType.CreateRegister;
239
+ type: OpCode.CREATE_REGISTER;
211
240
  parentId: string;
212
241
  parentKey: string;
213
242
  data: Json;
@@ -215,18 +244,18 @@ declare type CreateRegisterOp = {
215
244
  declare type DeleteCrdtOp = {
216
245
  opId?: string;
217
246
  id: string;
218
- type: OpType.DeleteCrdt;
247
+ type: OpCode.DELETE_CRDT;
219
248
  };
220
249
  declare type SetParentKeyOp = {
221
250
  opId?: string;
222
251
  id: string;
223
- type: OpType.SetParentKey;
252
+ type: OpCode.SET_PARENT_KEY;
224
253
  parentKey: string;
225
254
  };
226
255
  declare type DeleteObjectKeyOp = {
227
256
  opId?: string;
228
257
  id: string;
229
- type: OpType.DeleteObjectKey;
258
+ type: OpCode.DELETE_OBJECT_KEY;
230
259
  key: string;
231
260
  };
232
261
  declare enum WebsocketCloseCodes {
@@ -278,4 +307,25 @@ declare function patchImmutableObject<T>(state: T, updates: StorageUpdate[]): T;
278
307
  declare function makePosition(before?: string, after?: string): string;
279
308
  declare function comparePosition(posA: string, posB: string): number;
280
309
 
281
- export { ClientMessageType, CrdtType, CreateListOp, CreateMapOp, CreateObjectOp, CreateRegisterOp, DeleteCrdtOp, DeleteObjectKeyOp, Op, OpType, RoomStateMessage, SerializedCrdt, SerializedCrdtWithId, SerializedList, SerializedMap, SerializedObject, SerializedRegister, ServerMessage, ServerMessageType, SetParentKeyOp, UpdateObjectOp, UserJoinMessage, WebsocketCloseCodes, comparePosition, deprecate, deprecateIf, errorIf, lsonToJson, makePosition, patchImmutableObject, patchLiveObjectKey, throwUsageError };
310
+ /**
311
+ * Helper function that can be used to implement exhaustive switch statements
312
+ * with TypeScript. Example usage:
313
+ *
314
+ * type Fruit = "🍎" | "🍌";
315
+ *
316
+ * switch (fruit) {
317
+ * case "🍎":
318
+ * case "🍌":
319
+ * return doSomething();
320
+ *
321
+ * default:
322
+ * return assertNever(fruit, "Unknown fruit");
323
+ * }
324
+ *
325
+ * If now the Fruit union is extended (i.e. add "🍒"), TypeScript will catch
326
+ * this *statically*, rather than at runtime, and force you to handle the
327
+ * 🍒 case.
328
+ */
329
+ declare function assertNever(_value: never, errmsg: string): never;
330
+
331
+ export { 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, UserJoinServerMsg, WebsocketCloseCodes, assertNever, comparePosition, deprecate, deprecateIf, errorIf, isChildCrdt, isRootCrdt, lsonToJson, makePosition, patchImmutableObject, patchLiveObjectKey, throwUsageError };