@liveblocks/react 1.2.2-comments5 → 1.2.2-comments6

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.d.mts CHANGED
@@ -530,10 +530,6 @@ declare type RoomContextBundle<TPresence extends JsonObject, TStorage extends Ls
530
530
  * const { user, error, isLoading } = useUser("user-id");
531
531
  */
532
532
  useUser(userId: string): UserState<TUserMeta["info"]>;
533
- /**
534
- * @private
535
- */
536
- useMentionSuggestions(search?: string): string[] | undefined;
537
533
  /**
538
534
  * Returns the LiveList associated with the provided key. The hook triggers
539
535
  * a re-render if the LiveList is updated, however it does not triggers
@@ -686,6 +682,11 @@ declare type Options<TUserMeta extends BaseUserMeta> = {
686
682
  */
687
683
  resolveMentionSuggestions?: (options: ResolveMentionSuggestionsOptions) => Promise<string[]>;
688
684
  };
685
+ /**
686
+ * @private
687
+ *
688
+ * This is an internal API, use `createRoomContext` instead.
689
+ */
689
690
  declare function useRoomContextBundle(): {
690
691
  RoomContext: React$1.Context<Room<JsonObject, LsonObject, BaseUserMeta, never> | null>;
691
692
  RoomProvider: (props: {
@@ -1038,7 +1039,6 @@ declare function useRoomContextBundle(): {
1038
1039
  name?: string | undefined;
1039
1040
  avatar?: string | undefined;
1040
1041
  } | undefined>;
1041
- useMentionSuggestions: (search?: string | undefined) => string[] | undefined;
1042
1042
  useList: <TKey extends string>(key: TKey) => _liveblocks_core.Lson | undefined;
1043
1043
  useMap: <TKey_1 extends string>(key: TKey_1) => _liveblocks_core.Lson | undefined;
1044
1044
  useObject: <TKey_2 extends string>(key: TKey_2) => _liveblocks_core.Lson | undefined;
@@ -1402,6 +1402,8 @@ declare function useRoomContextBundle(): {
1402
1402
  useMap: <TKey_4 extends string>(key: TKey_4) => _liveblocks_core.Lson | undefined;
1403
1403
  useObject: <TKey_5 extends string>(key: TKey_5) => _liveblocks_core.Lson | undefined;
1404
1404
  };
1405
+ hasResolveMentionSuggestions: boolean;
1406
+ useMentionSuggestions: (search?: string | undefined) => string[] | undefined;
1405
1407
  };
1406
1408
  declare function createRoomContext<TPresence extends JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = never, TThreadMetadata extends BaseMetadata = never>(client: Client, options?: Options<TUserMeta>): RoomContextBundle<TPresence, TStorage, TUserMeta, TRoomEvent, TThreadMetadata>;
1407
1409
 
package/dist/index.d.ts CHANGED
@@ -530,10 +530,6 @@ declare type RoomContextBundle<TPresence extends JsonObject, TStorage extends Ls
530
530
  * const { user, error, isLoading } = useUser("user-id");
531
531
  */
532
532
  useUser(userId: string): UserState<TUserMeta["info"]>;
533
- /**
534
- * @private
535
- */
536
- useMentionSuggestions(search?: string): string[] | undefined;
537
533
  /**
538
534
  * Returns the LiveList associated with the provided key. The hook triggers
539
535
  * a re-render if the LiveList is updated, however it does not triggers
@@ -686,6 +682,11 @@ declare type Options<TUserMeta extends BaseUserMeta> = {
686
682
  */
687
683
  resolveMentionSuggestions?: (options: ResolveMentionSuggestionsOptions) => Promise<string[]>;
688
684
  };
685
+ /**
686
+ * @private
687
+ *
688
+ * This is an internal API, use `createRoomContext` instead.
689
+ */
689
690
  declare function useRoomContextBundle(): {
690
691
  RoomContext: React$1.Context<Room<JsonObject, LsonObject, BaseUserMeta, never> | null>;
691
692
  RoomProvider: (props: {
@@ -1038,7 +1039,6 @@ declare function useRoomContextBundle(): {
1038
1039
  name?: string | undefined;
1039
1040
  avatar?: string | undefined;
1040
1041
  } | undefined>;
1041
- useMentionSuggestions: (search?: string | undefined) => string[] | undefined;
1042
1042
  useList: <TKey extends string>(key: TKey) => _liveblocks_core.Lson | undefined;
1043
1043
  useMap: <TKey_1 extends string>(key: TKey_1) => _liveblocks_core.Lson | undefined;
1044
1044
  useObject: <TKey_2 extends string>(key: TKey_2) => _liveblocks_core.Lson | undefined;
@@ -1402,6 +1402,8 @@ declare function useRoomContextBundle(): {
1402
1402
  useMap: <TKey_4 extends string>(key: TKey_4) => _liveblocks_core.Lson | undefined;
1403
1403
  useObject: <TKey_5 extends string>(key: TKey_5) => _liveblocks_core.Lson | undefined;
1404
1404
  };
1405
+ hasResolveMentionSuggestions: boolean;
1406
+ useMentionSuggestions: (search?: string | undefined) => string[] | undefined;
1405
1407
  };
1406
1408
  declare function createRoomContext<TPresence extends JsonObject, TStorage extends LsonObject = LsonObject, TUserMeta extends BaseUserMeta = BaseUserMeta, TRoomEvent extends Json = never, TThreadMetadata extends BaseMetadata = never>(client: Client, options?: Options<TUserMeta>): RoomContextBundle<TPresence, TStorage, TUserMeta, TRoomEvent, TThreadMetadata>;
1407
1409
 
package/dist/index.js CHANGED
@@ -5,7 +5,7 @@ var _core = require('@liveblocks/core');
5
5
 
6
6
  // src/version.ts
7
7
  var PKG_NAME = "@liveblocks/react";
8
- var PKG_VERSION = "1.2.2-comments5";
8
+ var PKG_VERSION = "1.2.2-comments6";
9
9
  var PKG_FORMAT = "cjs";
10
10
 
11
11
  // src/ClientSideSuspense.tsx
@@ -106,6 +106,7 @@ function createStore(initialState) {
106
106
  }
107
107
 
108
108
  // src/comments/CommentsRoom.ts
109
+
109
110
  var POLLING_INTERVAL_REALTIME = 3e4;
110
111
  var POLLING_INTERVAL = 5e3;
111
112
  var THREAD_ID_PREFIX = "th";
@@ -117,6 +118,7 @@ function createCommentsRoom(room, errorEventSource) {
117
118
  const store = createStore({
118
119
  isLoading: true
119
120
  });
121
+ let fetchThreadsPromise = null;
120
122
  let numberOfMutations = 0;
121
123
  function endMutation() {
122
124
  numberOfMutations--;
@@ -150,7 +152,11 @@ function createCommentsRoom(room, errorEventSource) {
150
152
  async function revalidateThreads() {
151
153
  pollingHub.threads.pause();
152
154
  if (numberOfMutations === 0) {
153
- setThreads(await room.getThreads());
155
+ if (fetchThreadsPromise === null) {
156
+ fetchThreadsPromise = room.getThreads();
157
+ }
158
+ setThreads(await fetchThreadsPromise);
159
+ fetchThreadsPromise = null;
154
160
  }
155
161
  pollingHub.threads.resume();
156
162
  }
@@ -171,8 +177,10 @@ function createCommentsRoom(room, errorEventSource) {
171
177
  });
172
178
  }
173
179
  pollingHub.threads.start(getPollingInterval());
174
- revalidateThreads();
175
180
  return () => {
181
+ if (store.subscribersCount() > 1) {
182
+ return;
183
+ }
176
184
  pollingHub.threads.stop();
177
185
  _optionalChain([unsubscribeRealtimeEvents, 'optionalCall', _ => _()]);
178
186
  unsubscribeRealtimeEvents = void 0;
@@ -186,6 +194,30 @@ function createCommentsRoom(room, errorEventSource) {
186
194
  isLoading: false
187
195
  });
188
196
  }
197
+ function useThreadsInternal() {
198
+ _react.useEffect.call(void 0, subscribe, []);
199
+ return _indexjs.useSyncExternalStore.call(void 0,
200
+ store.subscribe,
201
+ store.get,
202
+ store.get
203
+ );
204
+ }
205
+ function useThreads() {
206
+ _react.useEffect.call(void 0, () => {
207
+ void revalidateThreads();
208
+ }, []);
209
+ return useThreadsInternal();
210
+ }
211
+ function useThreadsSuspense() {
212
+ const result = useThreadsInternal();
213
+ if (result.isLoading) {
214
+ throw revalidateThreads();
215
+ }
216
+ if (result.error) {
217
+ throw result.error;
218
+ }
219
+ return result.threads;
220
+ }
189
221
  function getCurrentUserId() {
190
222
  const self = room.getSelf();
191
223
  if (self === null || self.id === void 0) {
@@ -359,23 +391,6 @@ function createCommentsRoom(room, errorEventSource) {
359
391
  )
360
392
  ).finally(endMutation);
361
393
  }
362
- function useThreads() {
363
- return _indexjs.useSyncExternalStore.call(void 0,
364
- store.subscribe,
365
- store.get,
366
- store.get
367
- );
368
- }
369
- function useThreadsSuspense() {
370
- const result = useThreads();
371
- if (result.isLoading) {
372
- throw new Promise(store.subscribeOnce);
373
- }
374
- if (result.error) {
375
- throw result.error;
376
- }
377
- return result.threads;
378
- }
379
394
  return {
380
395
  useThreads,
381
396
  useThreadsSuspense,
@@ -383,8 +398,7 @@ function createCommentsRoom(room, errorEventSource) {
383
398
  editThreadMetadata,
384
399
  createComment,
385
400
  editComment,
386
- deleteComment,
387
- subscribe
401
+ deleteComment
388
402
  };
389
403
  }
390
404
 
@@ -488,6 +502,18 @@ function useInitial2(value) {
488
502
  return _react.useRef.call(void 0, value).current;
489
503
  }
490
504
 
505
+ // src/lib/stable-stringify.ts
506
+ function stableStringify(object, ...args) {
507
+ const sortedObject = Object.keys(object).sort().reduce(
508
+ (sortedObject2, key) => {
509
+ sortedObject2[key] = object[key];
510
+ return sortedObject2;
511
+ },
512
+ {}
513
+ );
514
+ return JSON.stringify(sortedObject, ...args);
515
+ }
516
+
491
517
  // src/factory.tsx
492
518
  var noop2 = () => {
493
519
  };
@@ -552,7 +578,11 @@ function warnIfNoResolveUser(usersCache) {
552
578
  }
553
579
  var ContextBundle = React2.createContext(null);
554
580
  function useRoomContextBundle() {
555
- return React2.useContext(ContextBundle);
581
+ const bundle = React2.useContext(ContextBundle);
582
+ if (bundle === null) {
583
+ throw new Error("RoomProvider is missing from the React tree.");
584
+ }
585
+ return bundle;
556
586
  }
557
587
  function createRoomContext(client, options) {
558
588
  const RoomContext = React2.createContext(null);
@@ -609,14 +639,21 @@ function createRoomContext(client, options) {
609
639
  }
610
640
  );
611
641
  setRoom(room2);
612
- const unsubscribe = getCommentsRoom(room2).subscribe();
613
642
  return () => {
614
- unsubscribe();
615
- commentsRooms.delete(room2.id);
643
+ const commentsRoom = commentsRooms.get(roomId);
644
+ if (commentsRoom) {
645
+ commentsRooms.delete(roomId);
646
+ }
616
647
  client.leave(roomId);
617
648
  };
618
649
  }, [roomId, frozen]);
619
- return /* @__PURE__ */ React2.createElement(RoomContext.Provider, { value: room }, /* @__PURE__ */ React2.createElement(ContextBundle.Provider, { value: bundle }, props.children));
650
+ return /* @__PURE__ */ React2.createElement(RoomContext.Provider, { value: room }, /* @__PURE__ */ React2.createElement(
651
+ ContextBundle.Provider,
652
+ {
653
+ value: internalBundle
654
+ },
655
+ props.children
656
+ ));
620
657
  }
621
658
  function connectionIdSelector(others) {
622
659
  return others.map((user) => user.connectionId);
@@ -1026,7 +1063,7 @@ function createRoomContext(client, options) {
1026
1063
  }) : void 0;
1027
1064
  function useUser(userId) {
1028
1065
  const resolverKey = React2.useMemo(
1029
- () => JSON.stringify({ userId }),
1066
+ () => stableStringify({ userId }),
1030
1067
  [userId]
1031
1068
  );
1032
1069
  const state = useAsyncCache(usersCache, resolverKey);
@@ -1045,7 +1082,7 @@ function createRoomContext(client, options) {
1045
1082
  }
1046
1083
  function useUserSuspense(userId) {
1047
1084
  const resolverKey = React2.useMemo(
1048
- () => JSON.stringify({ userId }),
1085
+ () => stableStringify({ userId }),
1049
1086
  [userId]
1050
1087
  );
1051
1088
  const state = useAsyncCache(usersCache, resolverKey, {
@@ -1069,7 +1106,7 @@ function createRoomContext(client, options) {
1069
1106
  const room = useRoom();
1070
1107
  const debouncedSearch = useDebounce(search, 500);
1071
1108
  const resolverKey = React2.useMemo(
1072
- () => debouncedSearch !== void 0 ? JSON.stringify({ text: debouncedSearch, roomId: room.id }) : null,
1109
+ () => debouncedSearch !== void 0 ? stableStringify({ text: debouncedSearch, roomId: room.id }) : null,
1073
1110
  [debouncedSearch, room.id]
1074
1111
  );
1075
1112
  const { data } = useAsyncCache(mentionSuggestionsCache, resolverKey, {
@@ -1113,7 +1150,6 @@ function createRoomContext(client, options) {
1113
1150
  useCreateComment,
1114
1151
  useEditComment,
1115
1152
  useDeleteComment,
1116
- useMentionSuggestions,
1117
1153
  suspense: {
1118
1154
  RoomContext,
1119
1155
  RoomProvider,
@@ -1152,6 +1188,11 @@ function createRoomContext(client, options) {
1152
1188
  useDeleteComment
1153
1189
  }
1154
1190
  };
1191
+ const internalBundle = {
1192
+ ...bundle,
1193
+ hasResolveMentionSuggestions: resolveMentionSuggestions !== void 0,
1194
+ useMentionSuggestions
1195
+ };
1155
1196
  return bundle;
1156
1197
  }
1157
1198