@liveblocks/react 1.4.4 → 1.4.6-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.
package/dist/index.mjs CHANGED
@@ -5,7 +5,7 @@ import { detectDupes } from "@liveblocks/core";
5
5
 
6
6
  // src/version.ts
7
7
  var PKG_NAME = "@liveblocks/react";
8
- var PKG_VERSION = "1.4.4";
8
+ var PKG_VERSION = "1.4.6-test1";
9
9
  var PKG_FORMAT = "esm";
10
10
 
11
11
  // src/ClientSideSuspense.tsx
@@ -25,7 +25,8 @@ import {
25
25
  deprecateIf,
26
26
  errorIf,
27
27
  isLiveNode,
28
- makeEventSource as makeEventSource2
28
+ makeEventSource as makeEventSource2,
29
+ stringify
29
30
  } from "@liveblocks/core";
30
31
  import * as React2 from "react";
31
32
  import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector.js";
@@ -257,21 +258,23 @@ function createCommentsRoom(room, errorEventSource) {
257
258
  const threadId = createOptimisticId(THREAD_ID_PREFIX);
258
259
  const commentId = createOptimisticId(COMMENT_ID_PREFIX);
259
260
  const now = (/* @__PURE__ */ new Date()).toISOString();
261
+ const newComment = {
262
+ id: commentId,
263
+ threadId,
264
+ roomId: room.id,
265
+ createdAt: now,
266
+ type: "comment",
267
+ userId: getCurrentUserId(),
268
+ body,
269
+ reactions: []
270
+ };
260
271
  const newThread = {
261
272
  id: threadId,
262
273
  type: "thread",
263
274
  createdAt: now,
264
275
  roomId: room.id,
265
276
  metadata,
266
- comments: [
267
- {
268
- id: commentId,
269
- createdAt: now,
270
- type: "comment",
271
- userId: getCurrentUserId(),
272
- body
273
- }
274
- ]
277
+ comments: [newComment]
275
278
  };
276
279
  mutate(room.createThread({ threadId, commentId, body, metadata }), {
277
280
  optimisticData: [...threads, newThread]
@@ -624,18 +627,6 @@ function useDebounce(value, delay = DEFAULT_DELAY) {
624
627
  return debouncedValue;
625
628
  }
626
629
 
627
- // src/lib/stable-stringify.ts
628
- function stableStringify(object, ...args) {
629
- const sortedObject = Object.keys(object).sort().reduce(
630
- (sortedObject2, key) => {
631
- sortedObject2[key] = object[key];
632
- return sortedObject2;
633
- },
634
- {}
635
- );
636
- return JSON.stringify(sortedObject, ...args);
637
- }
638
-
639
630
  // src/lib/use-async-cache.ts
640
631
  import { useCallback, useEffect as useEffect4, useMemo, useRef as useRef3 } from "react";
641
632
  import { useSyncExternalStore as useSyncExternalStore2 } from "use-sync-external-store/shim/index.js";
@@ -660,10 +651,10 @@ function useAsyncCache(cache, key, options) {
660
651
  if (key === null || !cache) {
661
652
  return null;
662
653
  }
663
- const cacheItem2 = cache.create(key, frozenOptions?.overrideFunction);
654
+ const cacheItem2 = cache.create(key);
664
655
  void cacheItem2.get();
665
656
  return cacheItem2;
666
- }, [cache, frozenOptions, key]);
657
+ }, [cache, key]);
667
658
  const subscribe = useCallback(
668
659
  (callback) => cacheItem?.subscribe(callback) ?? noop,
669
660
  [cacheItem]
@@ -678,18 +669,33 @@ function useAsyncCache(cache, key, options) {
678
669
  let data = state.data;
679
670
  useEffect4(() => {
680
671
  previousData.current = { key, data: state.data };
681
- }, [key, state]);
682
- if (frozenOptions?.suspense && state.isLoading && cacheItem) {
683
- throw new Promise((resolve) => {
684
- cacheItem.subscribeOnce(() => resolve());
685
- });
672
+ }, [key, state.data]);
673
+ if (!cacheItem) {
674
+ return {
675
+ isLoading: false,
676
+ data: void 0,
677
+ error: void 0,
678
+ getState,
679
+ revalidate
680
+ };
681
+ }
682
+ if (frozenOptions?.suspense) {
683
+ const error = getState().error;
684
+ if (error) {
685
+ throw error;
686
+ } else if (getState().isLoading) {
687
+ throw new Promise((resolve) => {
688
+ cacheItem.subscribeOnce((state2) => {
689
+ if (!state2.isLoading) {
690
+ resolve();
691
+ }
692
+ });
693
+ });
694
+ }
686
695
  }
687
696
  if (state.isLoading && frozenOptions?.keepPreviousDataWhileLoading && typeof state.data === "undefined" && previousData.current?.key !== key && typeof previousData.current?.data !== "undefined") {
688
697
  data = previousData.current.data;
689
698
  }
690
- if (frozenOptions?.suspense && state.error) {
691
- throw state.error;
692
- }
693
699
  return {
694
700
  isLoading: state.isLoading,
695
701
  data,
@@ -738,6 +744,9 @@ function alwaysEmptyList() {
738
744
  function alwaysNull() {
739
745
  return null;
740
746
  }
747
+ function alwaysConnecting() {
748
+ return "connecting";
749
+ }
741
750
  function makeMutationContext(room) {
742
751
  const errmsg = "This mutation cannot be used until connected to the Liveblocks room";
743
752
  return {
@@ -765,13 +774,13 @@ function makeMutationContext(room) {
765
774
  setMyPresence: room.updatePresence
766
775
  };
767
776
  }
768
- var hasWarnedIfNoResolveUser = false;
769
- function warnIfNoResolveUser(usersCache) {
770
- if (!hasWarnedIfNoResolveUser && !usersCache && process.env.NODE_ENV !== "production") {
777
+ var hasWarnedIfNoResolveUsers = false;
778
+ function warnIfNoResolveUsers(usersCache) {
779
+ if (!hasWarnedIfNoResolveUsers && !usersCache && process.env.NODE_ENV !== "production") {
771
780
  console.warn(
772
- "Set the resolveUser option in createRoomContext to specify user info."
781
+ "Set the resolveUsers option in createRoomContext to specify user info."
773
782
  );
774
- hasWarnedIfNoResolveUser = true;
783
+ hasWarnedIfNoResolveUsers = true;
775
784
  }
776
785
  }
777
786
  var hasWarnedAboutCommentsBeta = false;
@@ -876,7 +885,8 @@ function createRoomContext(client, options) {
876
885
  const room = useRoom();
877
886
  const subscribe = room.events.status.subscribe;
878
887
  const getSnapshot = room.getStatus;
879
- return useSyncExternalStore3(subscribe, getSnapshot, getSnapshot);
888
+ const getServerSnapshot = alwaysConnecting;
889
+ return useSyncExternalStore3(subscribe, getSnapshot, getServerSnapshot);
880
890
  }
881
891
  function useMyPresence() {
882
892
  const room = useRoom();
@@ -1141,6 +1151,7 @@ function createRoomContext(client, options) {
1141
1151
  }
1142
1152
  ensureNotServerSide();
1143
1153
  throw new Promise((res) => {
1154
+ room.events.self.subscribeOnce(() => res());
1144
1155
  room.events.status.subscribeOnce(() => res());
1145
1156
  });
1146
1157
  }
@@ -1296,19 +1307,21 @@ function createRoomContext(client, options) {
1296
1307
  [room]
1297
1308
  );
1298
1309
  }
1299
- const { resolveUser, resolveMentionSuggestions } = options ?? {};
1300
- const usersCache = resolveUser ? createAsyncCache((stringifiedOptions) => {
1301
- return resolveUser(
1310
+ const { resolveUsers, resolveMentionSuggestions } = options ?? {};
1311
+ const usersCache = resolveUsers ? createAsyncCache(async (stringifiedOptions) => {
1312
+ const users = await resolveUsers(
1302
1313
  JSON.parse(stringifiedOptions)
1303
1314
  );
1315
+ return users?.[0];
1304
1316
  }) : void 0;
1305
1317
  function useUser(userId) {
1318
+ const room = useRoom();
1306
1319
  const resolverKey = React2.useMemo(
1307
- () => stableStringify({ userId }),
1308
- [userId]
1320
+ () => stringify({ userIds: [userId], roomId: room.id }),
1321
+ [userId, room.id]
1309
1322
  );
1310
1323
  const state = useAsyncCache(usersCache, resolverKey);
1311
- React2.useEffect(() => warnIfNoResolveUser(usersCache), []);
1324
+ React2.useEffect(() => warnIfNoResolveUsers(usersCache), []);
1312
1325
  if (state.isLoading) {
1313
1326
  return {
1314
1327
  isLoading: true
@@ -1322,14 +1335,15 @@ function createRoomContext(client, options) {
1322
1335
  }
1323
1336
  }
1324
1337
  function useUserSuspense(userId) {
1338
+ const room = useRoom();
1325
1339
  const resolverKey = React2.useMemo(
1326
- () => stableStringify({ userId }),
1327
- [userId]
1340
+ () => stringify({ userIds: [userId], roomId: room.id }),
1341
+ [userId, room.id]
1328
1342
  );
1329
1343
  const state = useAsyncCache(usersCache, resolverKey, {
1330
1344
  suspense: true
1331
1345
  });
1332
- React2.useEffect(() => warnIfNoResolveUser(usersCache), []);
1346
+ React2.useEffect(() => warnIfNoResolveUsers(usersCache), []);
1333
1347
  return {
1334
1348
  user: state.data,
1335
1349
  isLoading: false
@@ -1346,7 +1360,7 @@ function createRoomContext(client, options) {
1346
1360
  const room = useRoom();
1347
1361
  const debouncedSearch = useDebounce(search, 500);
1348
1362
  const resolverKey = React2.useMemo(
1349
- () => debouncedSearch !== void 0 ? stableStringify({ text: debouncedSearch, roomId: room.id }) : null,
1363
+ () => debouncedSearch !== void 0 ? stringify({ text: debouncedSearch, roomId: room.id }) : null,
1350
1364
  [debouncedSearch, room.id]
1351
1365
  );
1352
1366
  const { data } = useAsyncCache(mentionSuggestionsCache, resolverKey, {