@liveblocks/client 0.16.4-beta2 → 0.16.4

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.mjs CHANGED
@@ -1,5 +1,5 @@
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 LiveMap, h as LiveRegister, j as findNonSerializableValue } from './shared.mjs';
2
- export { b as LiveList, f as LiveMap, L as LiveObject } from './shared.mjs';
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';
2
+ export { b as LiveList, h as LiveMap, L as LiveObject } from './shared.mjs';
3
3
 
4
4
  /*! *****************************************************************************
5
5
  Copyright (c) Microsoft Corporation.
@@ -1006,7 +1006,7 @@ See v0.13 release notes for more information.
1006
1006
  },
1007
1007
  };
1008
1008
  }
1009
- function defaultState(me, defaultStorageRoot) {
1009
+ function defaultState(initialPresence, initialStorage) {
1010
1010
  return {
1011
1011
  connection: { state: "closed" },
1012
1012
  token: null,
@@ -1028,17 +1028,17 @@ function defaultState(me, defaultStorageRoot) {
1028
1028
  pongTimeout: 0,
1029
1029
  },
1030
1030
  buffer: {
1031
- presence: me == null ? {} : me,
1031
+ presence: initialPresence == null ? {} : initialPresence,
1032
1032
  messages: [],
1033
1033
  storageOperations: [],
1034
1034
  },
1035
1035
  intervalHandles: {
1036
1036
  heartbeat: 0,
1037
1037
  },
1038
- me: me == null ? {} : me,
1038
+ me: initialPresence == null ? {} : initialPresence,
1039
1039
  users: {},
1040
1040
  others: makeOthers({}),
1041
- defaultStorageRoot,
1041
+ defaultStorageRoot: initialStorage,
1042
1042
  idFactory: null,
1043
1043
  // Storage
1044
1044
  clock: 0,
@@ -1063,7 +1063,14 @@ function defaultState(me, defaultStorageRoot) {
1063
1063
  };
1064
1064
  }
1065
1065
  function createRoom(options, context) {
1066
- const state = defaultState(options.defaultPresence, options.defaultStorageRoot);
1066
+ var _a, _b;
1067
+ const initialPresence = (_a = options.initialPresence) !== null && _a !== void 0 ? _a : options.defaultPresence;
1068
+ const initialStorage = (_b = options.initialStorage) !== null && _b !== void 0 ? _b : options.defaultStorageRoot;
1069
+ const state = defaultState(typeof initialPresence === "function"
1070
+ ? initialPresence(context.roomId)
1071
+ : initialPresence, typeof initialStorage === "function"
1072
+ ? initialStorage(context.roomId)
1073
+ : initialStorage);
1067
1074
  const machine = makeStateMachine(state, context);
1068
1075
  const room = {
1069
1076
  id: context.roomId,
@@ -1229,9 +1236,13 @@ function createClient(options) {
1229
1236
  if (internalRoom) {
1230
1237
  return internalRoom.room;
1231
1238
  }
1239
+ deprecateIf(options.defaultPresence, "Argument `defaultPresence` will be removed in @liveblocks/client 0.18. Please use `initialPresence` instead. For more info, see https://bit.ly/3Niy5aP", "defaultPresence");
1240
+ deprecateIf(options.defaultStorageRoot, "Argument `defaultStorageRoot` will be removed in @liveblocks/client 0.18. Please use `initialStorage` instead. For more info, see https://bit.ly/3Niy5aP", "defaultStorageRoot");
1232
1241
  internalRoom = createRoom({
1242
+ initialPresence: options.initialPresence,
1243
+ initialStorage: options.initialStorage,
1233
1244
  defaultPresence: options.defaultPresence,
1234
- defaultStorageRoot: options.defaultStorageRoot,
1245
+ defaultStorageRoot: options.defaultStorageRoot, // Will get removed in 0.18
1235
1246
  }, {
1236
1247
  roomId,
1237
1248
  throttleDelay,
@@ -1310,332 +1321,4 @@ function prepareAuthentication(clientOptions) {
1310
1321
  throw new Error("Invalid Liveblocks client options. For more information: https://liveblocks.io/docs/api-reference/liveblocks-client#createClient");
1311
1322
  }
1312
1323
 
1313
- function lsonObjectToJson(obj) {
1314
- const result = {};
1315
- for (const key in obj) {
1316
- const val = obj[key];
1317
- if (val !== undefined) {
1318
- result[key] = lsonToJson(val);
1319
- }
1320
- }
1321
- return result;
1322
- }
1323
- function liveObjectToJson(liveObject) {
1324
- return lsonObjectToJson(liveObject.toObject());
1325
- }
1326
- function liveMapToJson(map) {
1327
- const result = {};
1328
- for (const [key, value] of map.entries()) {
1329
- result[key] = lsonToJson(value);
1330
- }
1331
- return result;
1332
- }
1333
- function lsonListToJson(value) {
1334
- return value.map(lsonToJson);
1335
- }
1336
- function liveListToJson(value) {
1337
- return lsonListToJson(value.toArray());
1338
- }
1339
- function lsonToJson(value) {
1340
- // ^^^^^^^^^^^^
1341
- // FIXME: Remove me later. This requires the
1342
- // addition of a concrete LiveStructure type first.
1343
- // Check for LiveStructure datastructures first
1344
- if (value instanceof LiveObject) {
1345
- return liveObjectToJson(value);
1346
- }
1347
- else if (value instanceof LiveList) {
1348
- return liveListToJson(value);
1349
- }
1350
- else if (value instanceof LiveMap) {
1351
- return liveMapToJson(value);
1352
- }
1353
- else if (value instanceof LiveRegister) {
1354
- return value.data;
1355
- }
1356
- else if (value instanceof AbstractCrdt) {
1357
- // This code path should never be taken
1358
- throw new Error("Unhandled subclass of AbstractCrdt encountered");
1359
- }
1360
- // Then for composite Lson values
1361
- if (Array.isArray(value)) {
1362
- return lsonListToJson(value);
1363
- }
1364
- else if (isPlainObject(value)) {
1365
- return lsonObjectToJson(value);
1366
- }
1367
- // Finally, if value is an LsonScalar, then it's also a valid JsonScalar
1368
- return value;
1369
- }
1370
- function isPlainObject(obj) {
1371
- return (obj !== null && Object.prototype.toString.call(obj) === "[object Object]");
1372
- }
1373
- function anyToCrdt(obj) {
1374
- // ^^^ AbstractCrdt?
1375
- if (obj == null) {
1376
- return obj;
1377
- }
1378
- if (Array.isArray(obj)) {
1379
- return new LiveList(obj.map(anyToCrdt));
1380
- }
1381
- if (isPlainObject(obj)) {
1382
- const init = {};
1383
- for (const key in obj) {
1384
- init[key] = anyToCrdt(obj[key]);
1385
- }
1386
- return new LiveObject(init);
1387
- }
1388
- return obj;
1389
- }
1390
- function patchLiveList(liveList, prev, next) {
1391
- let i = 0;
1392
- let prevEnd = prev.length - 1;
1393
- let nextEnd = next.length - 1;
1394
- let prevNode = prev[0];
1395
- let nextNode = next[0];
1396
- /**
1397
- * For A,B,C => A,B,C,D
1398
- * i = 3, prevEnd = 2, nextEnd = 3
1399
- *
1400
- * For A,B,C => B,C
1401
- * i = 2, prevEnd = 2, nextEnd = 1
1402
- *
1403
- * For B,C => A,B,C
1404
- * i = 0, pre
1405
- */
1406
- outer: {
1407
- while (prevNode === nextNode) {
1408
- ++i;
1409
- if (i > prevEnd || i > nextEnd) {
1410
- break outer;
1411
- }
1412
- prevNode = prev[i];
1413
- nextNode = next[i];
1414
- }
1415
- prevNode = prev[prevEnd];
1416
- nextNode = next[nextEnd];
1417
- while (prevNode === nextNode) {
1418
- prevEnd--;
1419
- nextEnd--;
1420
- if (i > prevEnd || i > nextEnd) {
1421
- break outer;
1422
- }
1423
- prevNode = prev[prevEnd];
1424
- nextNode = next[nextEnd];
1425
- }
1426
- }
1427
- if (i > prevEnd) {
1428
- if (i <= nextEnd) {
1429
- while (i <= nextEnd) {
1430
- liveList.insert(anyToCrdt(next[i]), i);
1431
- i++;
1432
- }
1433
- }
1434
- }
1435
- else if (i > nextEnd) {
1436
- let localI = i;
1437
- while (localI <= prevEnd) {
1438
- liveList.delete(i);
1439
- localI++;
1440
- }
1441
- }
1442
- else {
1443
- while (i <= prevEnd && i <= nextEnd) {
1444
- prevNode = prev[i];
1445
- nextNode = next[i];
1446
- const liveListNode = liveList.get(i);
1447
- if (liveListNode instanceof LiveObject &&
1448
- isPlainObject(prevNode) &&
1449
- isPlainObject(nextNode)) {
1450
- patchLiveObject(liveListNode, prevNode, nextNode);
1451
- }
1452
- else {
1453
- liveList.set(i, anyToCrdt(nextNode));
1454
- }
1455
- i++;
1456
- }
1457
- while (i <= nextEnd) {
1458
- liveList.insert(anyToCrdt(next[i]), i);
1459
- i++;
1460
- }
1461
- let localI = i;
1462
- while (localI <= prevEnd) {
1463
- liveList.delete(i);
1464
- localI++;
1465
- }
1466
- }
1467
- }
1468
- function patchLiveObjectKey(liveObject, key, prev, next) {
1469
- if (process.env.NODE_ENV !== "production") {
1470
- const nonSerializableValue = findNonSerializableValue(next);
1471
- if (nonSerializableValue) {
1472
- console.error(`New state path: '${nonSerializableValue.path}' value: '${nonSerializableValue.value}' is not serializable.\nOnly serializable value can be synced with Liveblocks.`);
1473
- return;
1474
- }
1475
- }
1476
- const value = liveObject.get(key);
1477
- if (next === undefined) {
1478
- liveObject.delete(key);
1479
- }
1480
- else if (value === undefined) {
1481
- liveObject.set(key, anyToCrdt(next));
1482
- }
1483
- else if (prev === next) {
1484
- return;
1485
- }
1486
- else if (value instanceof LiveList &&
1487
- Array.isArray(prev) &&
1488
- Array.isArray(next)) {
1489
- patchLiveList(value, prev, next);
1490
- }
1491
- else if (value instanceof LiveObject &&
1492
- isPlainObject(prev) &&
1493
- isPlainObject(next)) {
1494
- patchLiveObject(value, prev, next);
1495
- }
1496
- else {
1497
- liveObject.set(key, anyToCrdt(next));
1498
- }
1499
- }
1500
- function patchLiveObject(root, prev, next) {
1501
- const updates = {};
1502
- for (const key in next) {
1503
- patchLiveObjectKey(root, key, prev[key], next[key]);
1504
- }
1505
- for (const key in prev) {
1506
- if (next[key] === undefined) {
1507
- root.delete(key);
1508
- }
1509
- }
1510
- if (Object.keys(updates).length > 0) {
1511
- root.update(updates);
1512
- }
1513
- }
1514
- function getParentsPath(node) {
1515
- const path = [];
1516
- while (node._parentKey != null && node._parent != null) {
1517
- if (node._parent instanceof LiveList) {
1518
- path.push(node._parent._indexOfPosition(node._parentKey));
1519
- }
1520
- else {
1521
- path.push(node._parentKey);
1522
- }
1523
- node = node._parent;
1524
- }
1525
- return path;
1526
- }
1527
- function patchImmutableObject(state, updates) {
1528
- return updates.reduce((state, update) => patchImmutableObjectWithUpdate(state, update), state);
1529
- }
1530
- function patchImmutableObjectWithUpdate(state, update) {
1531
- const path = getParentsPath(update.node);
1532
- return patchImmutableNode(state, path, update);
1533
- }
1534
- function patchImmutableNode(state, path, update) {
1535
- var _a, _b, _c, _d;
1536
- const pathItem = path.pop();
1537
- if (pathItem === undefined) {
1538
- switch (update.type) {
1539
- case "LiveObject": {
1540
- if (typeof state !== "object") {
1541
- throw new Error("Internal: received update on LiveObject but state was not an object");
1542
- }
1543
- const newState = Object.assign({}, state);
1544
- for (const key in update.updates) {
1545
- if (((_a = update.updates[key]) === null || _a === void 0 ? void 0 : _a.type) === "update") {
1546
- const val = update.node.get(key);
1547
- if (val !== undefined) {
1548
- newState[key] = lsonToJson(val);
1549
- }
1550
- }
1551
- else if (((_b = update.updates[key]) === null || _b === void 0 ? void 0 : _b.type) === "delete") {
1552
- delete newState[key];
1553
- }
1554
- }
1555
- return newState;
1556
- }
1557
- case "LiveList": {
1558
- if (Array.isArray(state) === false) {
1559
- throw new Error("Internal: received update on LiveList but state was not an array");
1560
- }
1561
- let newState = state.map((x) => x);
1562
- for (const listUpdate of update.updates) {
1563
- if (listUpdate.type === "set") {
1564
- newState = newState.map((item, index) => index === listUpdate.index ? listUpdate.item : item);
1565
- }
1566
- else if (listUpdate.type === "insert") {
1567
- if (listUpdate.index === newState.length) {
1568
- newState.push(lsonToJson(listUpdate.item));
1569
- }
1570
- else {
1571
- newState = [
1572
- ...newState.slice(0, listUpdate.index),
1573
- lsonToJson(listUpdate.item),
1574
- ...newState.slice(listUpdate.index),
1575
- ];
1576
- }
1577
- }
1578
- else if (listUpdate.type === "delete") {
1579
- newState.splice(listUpdate.index, 1);
1580
- }
1581
- else if (listUpdate.type === "move") {
1582
- if (listUpdate.previousIndex > listUpdate.index) {
1583
- newState = [
1584
- ...newState.slice(0, listUpdate.index),
1585
- lsonToJson(listUpdate.item),
1586
- ...newState.slice(listUpdate.index, listUpdate.previousIndex),
1587
- ...newState.slice(listUpdate.previousIndex + 1),
1588
- ];
1589
- }
1590
- else {
1591
- newState = [
1592
- ...newState.slice(0, listUpdate.previousIndex),
1593
- ...newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1),
1594
- lsonToJson(listUpdate.item),
1595
- ...newState.slice(listUpdate.index + 1),
1596
- ];
1597
- }
1598
- }
1599
- }
1600
- return newState;
1601
- }
1602
- case "LiveMap": {
1603
- if (typeof state !== "object") {
1604
- throw new Error("Internal: received update on LiveMap but state was not an object");
1605
- }
1606
- const newState = Object.assign({}, state);
1607
- for (const key in update.updates) {
1608
- if (((_c = update.updates[key]) === null || _c === void 0 ? void 0 : _c.type) === "update") {
1609
- newState[key] = lsonToJson(update.node.get(key));
1610
- }
1611
- else if (((_d = update.updates[key]) === null || _d === void 0 ? void 0 : _d.type) === "delete") {
1612
- delete newState[key];
1613
- }
1614
- }
1615
- return newState;
1616
- }
1617
- }
1618
- }
1619
- if (Array.isArray(state)) {
1620
- const newArray = [...state];
1621
- newArray[pathItem] = patchImmutableNode(state[pathItem], path, update);
1622
- return newArray;
1623
- }
1624
- else {
1625
- return Object.assign(Object.assign({}, state), { [pathItem]: patchImmutableNode(state[pathItem], path, update) });
1626
- }
1627
- }
1628
-
1629
- /**
1630
- * @internal
1631
- */
1632
- const internals = {
1633
- liveObjectToJson,
1634
- lsonToJson,
1635
- patchLiveList,
1636
- patchImmutableObject,
1637
- patchLiveObject,
1638
- patchLiveObjectKey,
1639
- };
1640
-
1641
- export { createClient, internals };
1324
+ export { createClient };
package/internal.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { J as Json, P as Presence, d as JsonObject } from './shared';
2
- export { g as Resolve } from './shared';
1
+ import { J as Json, P as Presence, d as JsonObject, 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
4
  /**
5
5
  * Messages that can be sent from the server to the client.
@@ -121,32 +121,12 @@ declare type UpdateStorageMessage = {
121
121
  type: ServerMessageType.UpdateStorage;
122
122
  ops: Op[];
123
123
  };
124
- /**
125
- * Messages that can be sent from the client to the server.
126
- */
127
- declare type ClientMessage = ClientEventMessage | UpdatePresenceClientMessage | UpdateStorageClientMessage | FetchStorageClientMessage;
128
124
  declare enum ClientMessageType {
129
125
  UpdatePresence = 100,
130
126
  ClientEvent = 103,
131
127
  FetchStorage = 200,
132
128
  UpdateStorage = 201
133
129
  }
134
- declare type ClientEventMessage = {
135
- type: ClientMessageType.ClientEvent;
136
- event: Json;
137
- };
138
- declare type UpdatePresenceClientMessage = {
139
- type: ClientMessageType.UpdatePresence;
140
- data: Presence;
141
- targetActor?: number;
142
- };
143
- declare type UpdateStorageClientMessage = {
144
- type: ClientMessageType.UpdateStorage;
145
- ops: Op[];
146
- };
147
- declare type FetchStorageClientMessage = {
148
- type: ClientMessageType.FetchStorage;
149
- };
150
130
  declare enum CrdtType {
151
131
  Object = 0,
152
132
  List = 1,
@@ -192,7 +172,6 @@ declare enum OpType {
192
172
  * only.
193
173
  */
194
174
  declare type Op = CreateObjectOp | UpdateObjectOp | DeleteCrdtOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp;
195
- declare type CreateOp = CreateObjectOp | CreateRegisterOp | CreateMapOp | CreateListOp;
196
175
  declare type UpdateObjectOp = {
197
176
  opId?: string;
198
177
  id: string;
@@ -250,23 +229,6 @@ declare type DeleteObjectKeyOp = {
250
229
  type: OpType.DeleteObjectKey;
251
230
  key: string;
252
231
  };
253
- declare enum WebsocketCloseCodes {
254
- CLOSE_ABNORMAL = 1006,
255
- INVALID_MESSAGE_FORMAT = 4000,
256
- NOT_ALLOWED = 4001,
257
- MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
258
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
259
- MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
260
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
261
- CLOSE_WITHOUT_RETRY = 4999
262
- }
263
-
264
- declare const min = 32;
265
- declare const max = 126;
266
- declare function makePosition(before?: string, after?: string): string;
267
- declare function posCodes(str: string): number[];
268
- declare function pos(codes: number[]): string;
269
- declare function compare(posA: string, posB: string): number;
270
232
 
271
233
  /**
272
234
  * Displays a deprecation warning in the dev console. Only in dev mode, and
@@ -280,4 +242,8 @@ declare function deprecate(message: string, key?: string): void;
280
242
  */
281
243
  declare function deprecateIf(condition: unknown, message: string, key?: string): void;
282
244
 
283
- export { ClientEventMessage, ClientMessage, ClientMessageType, CrdtType, CreateListOp, CreateMapOp, CreateObjectOp, CreateOp, CreateRegisterOp, DeleteCrdtOp, DeleteObjectKeyOp, EventMessage, FetchStorageClientMessage, InitialDocumentStateMessage, Op, OpType, RoomStateMessage, SerializedCrdt, SerializedCrdtWithId, SerializedList, SerializedMap, SerializedObject, SerializedRegister, ServerMessage, ServerMessageType, SetParentKeyOp, UpdateObjectOp, UpdatePresenceClientMessage, UpdatePresenceMessage, UpdateStorageClientMessage, UpdateStorageMessage, UserJoinMessage, UserLeftMessage, WebsocketCloseCodes, compare, deprecate, deprecateIf, makePosition, max, min, pos, posCodes };
245
+ declare function lsonToJson(value: Lson | AbstractCrdt): Json;
246
+ declare function patchLiveObjectKey<O extends LsonObject>(liveObject: LiveObject<O>, key: keyof O, prev: unknown, next: unknown): void;
247
+ declare function patchImmutableObject<T>(state: T, updates: StorageUpdate[]): T;
248
+
249
+ export { ClientMessageType, CrdtType, OpType, SerializedCrdtWithId, ServerMessage, ServerMessageType, deprecate, deprecateIf, lsonToJson, patchImmutableObject, patchLiveObjectKey };