@liveblocks/react 2.4.1-test1 → 2.4.1-test3

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.
@@ -1,6 +1,6 @@
1
1
  // src/version.ts
2
2
  var PKG_NAME = "@liveblocks/react";
3
- var PKG_VERSION = "2.4.1-test1";
3
+ var PKG_VERSION = "2.4.1-test3";
4
4
  var PKG_FORMAT = "esm";
5
5
 
6
6
  // src/ClientSideSuspense.tsx
@@ -13,6 +13,63 @@ function ClientSideSuspense(props) {
13
13
  return /* @__PURE__ */ React.createElement(React.Suspense, { fallback: props.fallback }, mounted ? typeof props.children === "function" ? props.children() : props.children : props.fallback);
14
14
  }
15
15
 
16
+ // src/comments/lib/selected-threads.ts
17
+ import {
18
+ applyOptimisticUpdates
19
+ } from "@liveblocks/core";
20
+ function selectedUserThreads(state) {
21
+ const result = applyOptimisticUpdates(state);
22
+ const threads = Object.values(result.threads).filter(
23
+ (thread) => {
24
+ if (thread.deletedAt !== void 0) {
25
+ return false;
26
+ }
27
+ return true;
28
+ }
29
+ );
30
+ return threads.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
31
+ }
32
+ function selectedThreads(roomId, state, options) {
33
+ const result = applyOptimisticUpdates(state);
34
+ const threads = Object.values(result.threads).filter(
35
+ (thread) => {
36
+ if (thread.roomId !== roomId) return false;
37
+ if (thread.deletedAt !== void 0) {
38
+ return false;
39
+ }
40
+ const query = options.query;
41
+ if (!query) return true;
42
+ if (query.resolved !== void 0 && thread.resolved !== query.resolved) {
43
+ return false;
44
+ }
45
+ for (const key in query.metadata) {
46
+ const metadataValue = thread.metadata[key];
47
+ const filterValue = query.metadata[key];
48
+ if (assertFilterIsStartsWithOperator(filterValue) && assertMetadataValueIsString(metadataValue)) {
49
+ if (metadataValue.startsWith(filterValue.startsWith)) {
50
+ return true;
51
+ }
52
+ }
53
+ if (metadataValue !== filterValue) {
54
+ return false;
55
+ }
56
+ }
57
+ return true;
58
+ }
59
+ );
60
+ return threads.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
61
+ }
62
+ var assertFilterIsStartsWithOperator = (filter) => {
63
+ if (typeof filter === "object" && typeof filter.startsWith === "string") {
64
+ return true;
65
+ } else {
66
+ return false;
67
+ }
68
+ };
69
+ var assertMetadataValueIsString = (value) => {
70
+ return typeof value === "string";
71
+ };
72
+
16
73
  // src/liveblocks.tsx
17
74
  import {
18
75
  assert,
@@ -35,9 +92,9 @@ import { useSyncExternalStore } from "use-sync-external-store/shim/index.js";
35
92
  import { useSyncExternalStoreWithSelector } from "use-sync-external-store/shim/with-selector.js";
36
93
 
37
94
  // src/comments/lib/selected-inbox-notifications.ts
38
- import { applyOptimisticUpdates } from "@liveblocks/core";
95
+ import { applyOptimisticUpdates as applyOptimisticUpdates2 } from "@liveblocks/core";
39
96
  function selectedInboxNotifications(state) {
40
- const result = applyOptimisticUpdates(state);
97
+ const result = applyOptimisticUpdates2(state);
41
98
  return Object.values(result.inboxNotifications).sort(
42
99
  // Sort so that the most recent notifications are first
43
100
  (a, b) => b.notifiedAt.getTime() - a.notifiedAt.getTime()
@@ -144,6 +201,7 @@ var _extras = /* @__PURE__ */ new WeakMap();
144
201
  var _bundles = /* @__PURE__ */ new WeakMap();
145
202
  var POLLING_INTERVAL = 60 * 1e3;
146
203
  var INBOX_NOTIFICATIONS_QUERY = "INBOX_NOTIFICATIONS";
204
+ var USER_THREADS_QUERY = "USER_THREADS";
147
205
  function selectorFor_useInboxNotifications(state) {
148
206
  const query = state.queries[INBOX_NOTIFICATIONS_QUERY];
149
207
  if (query === void 0 || query.isLoading) {
@@ -162,6 +220,25 @@ function selectorFor_useInboxNotifications(state) {
162
220
  isLoading: false
163
221
  };
164
222
  }
223
+ function selectorFor_useUserThreads(state) {
224
+ const query = state.queries[USER_THREADS_QUERY];
225
+ if (query === void 0 || query.isLoading) {
226
+ return {
227
+ isLoading: true
228
+ };
229
+ }
230
+ if (query.error !== void 0) {
231
+ return {
232
+ threads: [],
233
+ error: query.error,
234
+ isLoading: false
235
+ };
236
+ }
237
+ return {
238
+ threads: selectedUserThreads(state),
239
+ isLoading: false
240
+ };
241
+ }
165
242
  function selectUnreadInboxNotificationsCount(state) {
166
243
  let count = 0;
167
244
  for (const notification of selectedInboxNotifications(state)) {
@@ -321,11 +398,87 @@ function makeExtrasForClient(client) {
321
398
  };
322
399
  }, []);
323
400
  }
401
+ let userThreadsPollerSubscribers = 0;
402
+ const userThreadsPoller = makePoller(async () => {
403
+ try {
404
+ await waitUntilUserThreadsLoaded();
405
+ await fetchUserThreads();
406
+ } catch (err) {
407
+ console.warn(`Polling new user threads failed: ${String(err)}`);
408
+ }
409
+ });
410
+ let userThreadslastRequestedAt;
411
+ function loadUserThreads() {
412
+ void waitUntilUserThreadsLoaded().catch(() => {
413
+ });
414
+ }
415
+ const waitUntilUserThreadsLoaded = memoizeOnSuccess(async () => {
416
+ store.setQueryState(USER_THREADS_QUERY, {
417
+ isLoading: true
418
+ });
419
+ try {
420
+ await autoRetry(() => fetchUserThreads(), 5, [5e3, 5e3, 1e4, 15e3]);
421
+ } catch (err) {
422
+ store.setQueryState(USER_THREADS_QUERY, {
423
+ isLoading: false,
424
+ error: err
425
+ });
426
+ throw err;
427
+ }
428
+ });
429
+ async function fetchUserThreads() {
430
+ if (userThreadslastRequestedAt === void 0) {
431
+ const result = await client[kInternal].getThreads();
432
+ store.updateThreadsAndNotifications(
433
+ result.threads,
434
+ result.inboxNotifications,
435
+ [],
436
+ [],
437
+ USER_THREADS_QUERY
438
+ );
439
+ userThreadslastRequestedAt = result.requestedAt;
440
+ } else {
441
+ const result = await client[kInternal].getThreadsSince({
442
+ since: userThreadslastRequestedAt
443
+ });
444
+ store.updateThreadsAndNotifications(
445
+ result.threads.updated,
446
+ result.inboxNotifications.updated,
447
+ result.threads.deleted,
448
+ result.inboxNotifications.deleted,
449
+ USER_THREADS_QUERY
450
+ );
451
+ if (userThreadslastRequestedAt < result.requestedAt) {
452
+ userThreadslastRequestedAt = result.requestedAt;
453
+ }
454
+ }
455
+ }
456
+ function useEnableUserThreadsPolling() {
457
+ useEffect3(() => {
458
+ userThreadsPollerSubscribers++;
459
+ userThreadsPoller.start(POLLING_INTERVAL);
460
+ return () => {
461
+ if (userThreadsPollerSubscribers <= 0) {
462
+ console.warn(
463
+ `Internal unexpected behavior. Cannot decrease subscriber count for query "${USER_THREADS_QUERY}"`
464
+ );
465
+ return;
466
+ }
467
+ userThreadsPollerSubscribers--;
468
+ if (userThreadsPollerSubscribers <= 0) {
469
+ userThreadsPoller.stop();
470
+ }
471
+ };
472
+ }, []);
473
+ }
324
474
  return {
325
475
  store,
326
476
  useEnableInboxNotificationsPolling,
327
477
  waitUntilInboxNotificationsLoaded,
328
- loadInboxNotifications
478
+ loadInboxNotifications,
479
+ useEnableUserThreadsPolling,
480
+ waitUntilUserThreadsLoaded,
481
+ loadUserThreads
329
482
  };
330
483
  }
331
484
  function makeLiveblocksContextBundle(client) {
@@ -363,6 +516,28 @@ function makeLiveblocksContextBundle(client) {
363
516
  };
364
517
  return bundle;
365
518
  }
519
+ function useUserThreads_withClient(client) {
520
+ const { loadUserThreads, store, useEnableUserThreadsPolling } = getExtrasForClient(client);
521
+ useEffect3(() => {
522
+ loadUserThreads();
523
+ }, [loadUserThreads]);
524
+ useEnableUserThreadsPolling();
525
+ return useSyncExternalStoreWithSelector(
526
+ store.subscribe,
527
+ store.get,
528
+ store.get,
529
+ selectorFor_useUserThreads,
530
+ shallow
531
+ );
532
+ }
533
+ function useUserThreadsSuspense_withClient(client) {
534
+ const { waitUntilUserThreadsLoaded } = getExtrasForClient(client);
535
+ use(waitUntilUserThreadsLoaded());
536
+ const result = useUserThreads_withClient(client);
537
+ assert(!result.error, "Did not expect error");
538
+ assert(!result.isLoading, "Did not expect loading");
539
+ return result;
540
+ }
366
541
  function useInboxNotifications_withClient(client) {
367
542
  const { loadInboxNotifications, store, useEnableInboxNotificationsPolling } = getExtrasForClient(client);
368
543
  useEffect3(() => {
@@ -766,6 +941,12 @@ function LiveblocksProvider(props) {
766
941
  function createLiveblocksContext(client) {
767
942
  return getOrCreateContextBundle(client);
768
943
  }
944
+ function useUserThreads() {
945
+ return useUserThreads_withClient(useClient());
946
+ }
947
+ function useUserThreadsSuspense() {
948
+ return useUserThreadsSuspense_withClient(useClient());
949
+ }
769
950
  function useInboxNotifications() {
770
951
  return useInboxNotifications_withClient(useClient());
771
952
  }
@@ -912,51 +1093,6 @@ var UpdateNotificationSettingsError = class extends Error {
912
1093
  }
913
1094
  };
914
1095
 
915
- // src/comments/lib/selected-threads.ts
916
- import {
917
- applyOptimisticUpdates as applyOptimisticUpdates2
918
- } from "@liveblocks/core";
919
- function selectedThreads(roomId, state, options) {
920
- const result = applyOptimisticUpdates2(state);
921
- const threads = Object.values(result.threads).filter(
922
- (thread) => {
923
- if (thread.roomId !== roomId) return false;
924
- if (thread.deletedAt !== void 0) {
925
- return false;
926
- }
927
- const query = options.query;
928
- if (!query) return true;
929
- if (query.resolved !== void 0 && thread.resolved !== query.resolved) {
930
- return false;
931
- }
932
- for (const key in query.metadata) {
933
- const metadataValue = thread.metadata[key];
934
- const filterValue = query.metadata[key];
935
- if (assertFilterIsStartsWithOperator(filterValue) && assertMetadataValueIsString(metadataValue)) {
936
- if (metadataValue.startsWith(filterValue.startsWith)) {
937
- return true;
938
- }
939
- }
940
- if (metadataValue !== filterValue) {
941
- return false;
942
- }
943
- }
944
- return true;
945
- }
946
- );
947
- return threads.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
948
- }
949
- var assertFilterIsStartsWithOperator = (filter) => {
950
- if (typeof filter === "object" && typeof filter.startsWith === "string") {
951
- return true;
952
- } else {
953
- return false;
954
- }
955
- };
956
- var assertMetadataValueIsString = (value) => {
957
- return typeof value === "string";
958
- };
959
-
960
1096
  // src/room.tsx
961
1097
  import { shallow as shallow2 } from "@liveblocks/client";
962
1098
  import {
@@ -2790,10 +2926,13 @@ export {
2790
2926
  PKG_VERSION,
2791
2927
  PKG_FORMAT,
2792
2928
  ClientSideSuspense,
2929
+ selectedThreads,
2793
2930
  ClientContext,
2794
2931
  useClient,
2795
2932
  LiveblocksProvider,
2796
2933
  createLiveblocksContext,
2934
+ useUserThreads,
2935
+ useUserThreadsSuspense,
2797
2936
  useInboxNotifications,
2798
2937
  useInboxNotificationsSuspense,
2799
2938
  useMarkAllInboxNotificationsAsRead,
@@ -2808,7 +2947,6 @@ export {
2808
2947
  _useUser,
2809
2948
  _useUserSuspense,
2810
2949
  CreateThreadError,
2811
- selectedThreads,
2812
2950
  RoomContext,
2813
2951
  useStatus,
2814
2952
  useStorageStatus,
@@ -2861,4 +2999,4 @@ export {
2861
2999
  _useStorageRoot,
2862
3000
  _useUpdateMyPresence
2863
3001
  };
2864
- //# sourceMappingURL=chunk-OUY5PIZQ.mjs.map
3002
+ //# sourceMappingURL=chunk-T6YOAS5Z.mjs.map