@liveblocks/react 2.4.0 → 2.4.1-test2

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.0";
3
+ var PKG_VERSION = "2.4.1-test2";
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,91 @@ 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(
421
+ () => fetchInboxNotifications(),
422
+ 5,
423
+ [5e3, 5e3, 1e4, 15e3]
424
+ );
425
+ } catch (err) {
426
+ store.setQueryState(USER_THREADS_QUERY, {
427
+ isLoading: false,
428
+ error: err
429
+ });
430
+ throw err;
431
+ }
432
+ });
433
+ async function fetchUserThreads() {
434
+ if (userThreadslastRequestedAt === void 0) {
435
+ const result = await client[kInternal].getThreads();
436
+ store.updateThreadsAndNotifications(
437
+ result.threads,
438
+ result.inboxNotifications,
439
+ [],
440
+ [],
441
+ USER_THREADS_QUERY
442
+ );
443
+ userThreadslastRequestedAt = result.requestedAt;
444
+ } else {
445
+ const result = await client[kInternal].getThreadsSince({
446
+ since: userThreadslastRequestedAt
447
+ });
448
+ store.updateThreadsAndNotifications(
449
+ result.threads.updated,
450
+ result.inboxNotifications.updated,
451
+ result.threads.deleted,
452
+ result.inboxNotifications.deleted,
453
+ USER_THREADS_QUERY
454
+ );
455
+ if (userThreadslastRequestedAt < result.requestedAt) {
456
+ userThreadslastRequestedAt = result.requestedAt;
457
+ }
458
+ }
459
+ }
460
+ function useEnableUserThreadsPolling() {
461
+ useEffect3(() => {
462
+ userThreadsPollerSubscribers++;
463
+ userThreadsPoller.start(POLLING_INTERVAL);
464
+ return () => {
465
+ if (userThreadsPollerSubscribers <= 0) {
466
+ console.warn(
467
+ `Internal unexpected behavior. Cannot decrease subscriber count for query "${USER_THREADS_QUERY}"`
468
+ );
469
+ return;
470
+ }
471
+ userThreadsPollerSubscribers--;
472
+ if (userThreadsPollerSubscribers <= 0) {
473
+ userThreadsPoller.stop();
474
+ }
475
+ };
476
+ }, []);
477
+ }
324
478
  return {
325
479
  store,
326
480
  useEnableInboxNotificationsPolling,
327
481
  waitUntilInboxNotificationsLoaded,
328
- loadInboxNotifications
482
+ loadInboxNotifications,
483
+ useEnableUserThreadsPolling,
484
+ waitUntilUserThreadsLoaded,
485
+ loadUserThreads
329
486
  };
330
487
  }
331
488
  function makeLiveblocksContextBundle(client) {
@@ -363,6 +520,28 @@ function makeLiveblocksContextBundle(client) {
363
520
  };
364
521
  return bundle;
365
522
  }
523
+ function useUserThreads_withClient(client) {
524
+ const { loadUserThreads, store, useEnableUserThreadsPolling } = getExtrasForClient(client);
525
+ useEffect3(() => {
526
+ loadUserThreads();
527
+ }, [loadUserThreads]);
528
+ useEnableUserThreadsPolling();
529
+ return useSyncExternalStoreWithSelector(
530
+ store.subscribe,
531
+ store.get,
532
+ store.get,
533
+ selectorFor_useUserThreads,
534
+ shallow
535
+ );
536
+ }
537
+ function useUserThreadsSuspense_withClient(client) {
538
+ const { waitUntilUserThreadsLoaded } = getExtrasForClient(client);
539
+ use(waitUntilUserThreadsLoaded());
540
+ const result = useUserThreads_withClient(client);
541
+ assert(!result.error, "Did not expect error");
542
+ assert(!result.isLoading, "Did not expect loading");
543
+ return result;
544
+ }
366
545
  function useInboxNotifications_withClient(client) {
367
546
  const { loadInboxNotifications, store, useEnableInboxNotificationsPolling } = getExtrasForClient(client);
368
547
  useEffect3(() => {
@@ -766,6 +945,12 @@ function LiveblocksProvider(props) {
766
945
  function createLiveblocksContext(client) {
767
946
  return getOrCreateContextBundle(client);
768
947
  }
948
+ function useUserThreads() {
949
+ return useUserThreads_withClient(useClient());
950
+ }
951
+ function useUserThreadsSuspense() {
952
+ return useUserThreadsSuspense_withClient(useClient());
953
+ }
769
954
  function useInboxNotifications() {
770
955
  return useInboxNotifications_withClient(useClient());
771
956
  }
@@ -912,51 +1097,6 @@ var UpdateNotificationSettingsError = class extends Error {
912
1097
  }
913
1098
  };
914
1099
 
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
1100
  // src/room.tsx
961
1101
  import { shallow as shallow2 } from "@liveblocks/client";
962
1102
  import {
@@ -2790,10 +2930,13 @@ export {
2790
2930
  PKG_VERSION,
2791
2931
  PKG_FORMAT,
2792
2932
  ClientSideSuspense,
2933
+ selectedThreads,
2793
2934
  ClientContext,
2794
2935
  useClient,
2795
2936
  LiveblocksProvider,
2796
2937
  createLiveblocksContext,
2938
+ useUserThreads,
2939
+ useUserThreadsSuspense,
2797
2940
  useInboxNotifications,
2798
2941
  useInboxNotificationsSuspense,
2799
2942
  useMarkAllInboxNotificationsAsRead,
@@ -2808,7 +2951,6 @@ export {
2808
2951
  _useUser,
2809
2952
  _useUserSuspense,
2810
2953
  CreateThreadError,
2811
- selectedThreads,
2812
2954
  RoomContext,
2813
2955
  useStatus,
2814
2956
  useStorageStatus,
@@ -2861,4 +3003,4 @@ export {
2861
3003
  _useStorageRoot,
2862
3004
  _useUpdateMyPresence
2863
3005
  };
2864
- //# sourceMappingURL=chunk-7KAWYNVI.mjs.map
3006
+ //# sourceMappingURL=chunk-U4TTA3CR.mjs.map