@liveblocks/core 2.0.0-alpha1 → 2.0.0-alpha3

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/dist/index.mjs CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "2.0.0-alpha1";
9
+ var PKG_VERSION = "2.0.0-alpha3";
10
10
  var PKG_FORMAT = "esm";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -2591,7 +2591,7 @@ function compareNodePosition(itemA, itemB) {
2591
2591
  return posA === posB ? 0 : posA < posB ? -1 : 1;
2592
2592
  }
2593
2593
  var LiveList = class _LiveList extends AbstractCrdt {
2594
- constructor(items = []) {
2594
+ constructor(items) {
2595
2595
  super();
2596
2596
  this._items = [];
2597
2597
  this._implicitlyDeletedItems = /* @__PURE__ */ new WeakSet();
@@ -2607,7 +2607,7 @@ var LiveList = class _LiveList extends AbstractCrdt {
2607
2607
  }
2608
2608
  /** @internal */
2609
2609
  static _deserialize([id], parentToChildren, pool) {
2610
- const list = new _LiveList();
2610
+ const list = new _LiveList([]);
2611
2611
  list._attach(id, pool);
2612
2612
  const children = parentToChildren.get(id);
2613
2613
  if (children === void 0) {
@@ -3954,18 +3954,6 @@ var LiveMap = class _LiveMap extends AbstractCrdt {
3954
3954
 
3955
3955
  // src/crdts/LiveObject.ts
3956
3956
  var LiveObject = class _LiveObject extends AbstractCrdt {
3957
- constructor(obj = {}) {
3958
- super();
3959
- this._propToLastUpdate = /* @__PURE__ */ new Map();
3960
- const o = compactObject(obj);
3961
- for (const key of Object.keys(o)) {
3962
- const value = o[key];
3963
- if (isLiveNode(value)) {
3964
- value._setParentLink(this, key);
3965
- }
3966
- }
3967
- this._map = new Map(Object.entries(o));
3968
- }
3969
3957
  /** @internal */
3970
3958
  static _buildRootAndParentToChildren(items) {
3971
3959
  const parentToChildren = /* @__PURE__ */ new Map();
@@ -3997,6 +3985,18 @@ var LiveObject = class _LiveObject extends AbstractCrdt {
3997
3985
  pool
3998
3986
  );
3999
3987
  }
3988
+ constructor(obj = {}) {
3989
+ super();
3990
+ this._propToLastUpdate = /* @__PURE__ */ new Map();
3991
+ const o = compactObject(obj);
3992
+ for (const key of Object.keys(o)) {
3993
+ const value = o[key];
3994
+ if (isLiveNode(value)) {
3995
+ value._setParentLink(this, key);
3996
+ }
3997
+ }
3998
+ this._map = new Map(Object.entries(o));
3999
+ }
4000
4000
  /** @internal */
4001
4001
  _toOps(parentId, parentKey, pool) {
4002
4002
  if (this._id === void 0) {
@@ -4481,7 +4481,7 @@ function creationOpToLson(op) {
4481
4481
  case 7 /* CREATE_MAP */:
4482
4482
  return new LiveMap();
4483
4483
  case 2 /* CREATE_LIST */:
4484
- return new LiveList();
4484
+ return new LiveList([]);
4485
4485
  default:
4486
4486
  return assertNever(op, "Unknown creation Op");
4487
4487
  }
@@ -5610,6 +5610,52 @@ function createRoom(options, config) {
5610
5610
  body: JSON.stringify(body)
5611
5611
  });
5612
5612
  }
5613
+ async function createTextMention(userId, mentionId) {
5614
+ if (!managedSocket.authValue) {
5615
+ throw new Error("Not authorized");
5616
+ }
5617
+ return fetchClientApi(
5618
+ config.roomId,
5619
+ "/text-mentions",
5620
+ managedSocket.authValue,
5621
+ {
5622
+ method: "POST",
5623
+ headers: {
5624
+ "Content-Type": "application/json"
5625
+ },
5626
+ body: JSON.stringify({
5627
+ userId,
5628
+ mentionId
5629
+ })
5630
+ }
5631
+ );
5632
+ }
5633
+ async function deleteTextMention(mentionId) {
5634
+ if (!managedSocket.authValue) {
5635
+ throw new Error("Not authorized");
5636
+ }
5637
+ return fetchClientApi(
5638
+ config.roomId,
5639
+ `/text-mentions/${mentionId}`,
5640
+ managedSocket.authValue,
5641
+ {
5642
+ method: "DELETE"
5643
+ }
5644
+ );
5645
+ }
5646
+ async function reportTextEditor(type, rootKey) {
5647
+ const authValue = await delegates.authenticate();
5648
+ return fetchClientApi(config.roomId, "/text-metadata", authValue, {
5649
+ method: "POST",
5650
+ headers: {
5651
+ "Content-Type": "application/json"
5652
+ },
5653
+ body: JSON.stringify({
5654
+ type,
5655
+ rootKey
5656
+ })
5657
+ });
5658
+ }
5613
5659
  function sendMessages(messages) {
5614
5660
  const serializedPayload = JSON.stringify(messages);
5615
5661
  const nonce = context.dynamicSessionInfo.current?.nonce;
@@ -6498,6 +6544,12 @@ ${Array.from(traces).join("\n\n")}`
6498
6544
  return context.nodes.size;
6499
6545
  },
6500
6546
  // prettier-ignore
6547
+ // send metadata when using a text editor
6548
+ reportTextEditor,
6549
+ // create a text mention when using a text editor
6550
+ createTextMention,
6551
+ // delete a text mention when using a text editor
6552
+ deleteTextMention,
6501
6553
  // Support for the Liveblocks browser extension
6502
6554
  getSelf_forDevTools: () => selfAsTreeNode.current,
6503
6555
  getOthers_forDevTools: () => others_forDevTools.current,
@@ -6684,6 +6736,7 @@ function createClientStore() {
6684
6736
  inboxNotifications: {},
6685
6737
  notificationSettings: {}
6686
6738
  });
6739
+ const optimisticUpdatesEventSource = makeEventSource();
6687
6740
  return {
6688
6741
  ...store,
6689
6742
  deleteThread(threadId) {
@@ -6750,6 +6803,7 @@ function createClientStore() {
6750
6803
  }));
6751
6804
  },
6752
6805
  pushOptimisticUpdate(optimisticUpdate) {
6806
+ optimisticUpdatesEventSource.notify(optimisticUpdate);
6753
6807
  store.set((state) => ({
6754
6808
  ...state,
6755
6809
  optimisticUpdates: [...state.optimisticUpdates, optimisticUpdate]
@@ -6763,7 +6817,8 @@ function createClientStore() {
6763
6817
  [queryKey]: queryState
6764
6818
  }
6765
6819
  }));
6766
- }
6820
+ },
6821
+ optimisticUpdatesEventSource
6767
6822
  };
6768
6823
  }
6769
6824
  function deleteKeyImmutable(record, key) {
@@ -6928,15 +6983,13 @@ function applyThreadUpdates(existingThreads, updates) {
6928
6983
  const existingThread = updatedThreads[thread.id];
6929
6984
  if (existingThread) {
6930
6985
  const result = compareThreads(existingThread, thread);
6931
- if (result === 1)
6932
- return;
6986
+ if (result === 1) return;
6933
6987
  }
6934
6988
  updatedThreads[thread.id] = thread;
6935
6989
  });
6936
6990
  updates.deletedThreads.forEach(({ id, deletedAt }) => {
6937
6991
  const existingThread = updatedThreads[id];
6938
- if (existingThread === void 0)
6939
- return;
6992
+ if (existingThread === void 0) return;
6940
6993
  existingThread.deletedAt = deletedAt;
6941
6994
  existingThread.updatedAt = deletedAt;
6942
6995
  existingThread.comments = [];
@@ -6952,8 +7005,7 @@ function applyNotificationsUpdates(existingInboxNotifications, updates) {
6952
7005
  existingNotification,
6953
7006
  notification
6954
7007
  );
6955
- if (result === 1)
6956
- return;
7008
+ if (result === 1) return;
6957
7009
  }
6958
7010
  updatedInboxNotifications[notification.id] = notification;
6959
7011
  });
@@ -7181,22 +7233,22 @@ function createClient(options) {
7181
7233
  roomsById.delete(room.id);
7182
7234
  room.destroy();
7183
7235
  }
7184
- function leaseRoom(info) {
7236
+ function leaseRoom(details) {
7185
7237
  const leave = () => {
7186
7238
  const self = leave;
7187
- if (!info.unsubs.delete(self)) {
7239
+ if (!details.unsubs.delete(self)) {
7188
7240
  warn(
7189
7241
  "This leave function was already called. Calling it more than once has no effect."
7190
7242
  );
7191
7243
  } else {
7192
- if (info.unsubs.size === 0) {
7193
- teardownRoom(info.room);
7244
+ if (details.unsubs.size === 0) {
7245
+ teardownRoom(details.room);
7194
7246
  }
7195
7247
  }
7196
7248
  };
7197
- info.unsubs.add(leave);
7249
+ details.unsubs.add(leave);
7198
7250
  return {
7199
- room: info.room,
7251
+ room: details.room,
7200
7252
  leave
7201
7253
  };
7202
7254
  }
@@ -7235,11 +7287,11 @@ function createClient(options) {
7235
7287
  unstable_streamData: !!clientOptions.unstable_streamData
7236
7288
  }
7237
7289
  );
7238
- const newRoomInfo = {
7290
+ const newRoomDetails = {
7239
7291
  room: newRoom,
7240
7292
  unsubs: /* @__PURE__ */ new Set()
7241
7293
  };
7242
- roomsById.set(roomId, newRoomInfo);
7294
+ roomsById.set(roomId, newRoomDetails);
7243
7295
  setupDevTools(() => Array.from(roomsById.keys()));
7244
7296
  linkDevTools(roomId, newRoom);
7245
7297
  const shouldConnect = options2.autoConnect ?? true;
@@ -7254,22 +7306,12 @@ function createClient(options) {
7254
7306
  }
7255
7307
  newRoom.connect();
7256
7308
  }
7257
- return leaseRoom(newRoomInfo);
7258
- }
7259
- function enter(roomId, options2) {
7260
- const { room, leave: _ } = enterRoom(roomId, options2);
7261
- return room;
7309
+ return leaseRoom(newRoomDetails);
7262
7310
  }
7263
7311
  function getRoom(roomId) {
7264
7312
  const room = roomsById.get(roomId)?.room;
7265
7313
  return room ? room : null;
7266
7314
  }
7267
- function forceLeave(roomId) {
7268
- const unsubs = roomsById.get(roomId)?.unsubs ?? /* @__PURE__ */ new Set();
7269
- for (const unsub of unsubs) {
7270
- unsub();
7271
- }
7272
- }
7273
7315
  function logout() {
7274
7316
  authManager.reset();
7275
7317
  for (const { room } of roomsById.values()) {
@@ -7322,13 +7364,9 @@ function createClient(options) {
7322
7364
  );
7323
7365
  return Object.defineProperty(
7324
7366
  {
7325
- logout,
7326
- // Old, deprecated APIs
7327
- enter,
7328
- getRoom,
7329
- leave: forceLeave,
7330
- // New, preferred API
7331
7367
  enterRoom,
7368
+ getRoom,
7369
+ logout,
7332
7370
  // Internal
7333
7371
  [kInternal]: {
7334
7372
  notifications: {
@@ -7370,8 +7408,7 @@ function checkBounds(option, value, min, max, recommendedMin) {
7370
7408
  return value;
7371
7409
  }
7372
7410
  function getBackgroundKeepAliveTimeout(value) {
7373
- if (value === void 0)
7374
- return void 0;
7411
+ if (value === void 0) return void 0;
7375
7412
  return checkBounds(
7376
7413
  "backgroundKeepAliveTimeout",
7377
7414
  value,