Move websocketSubscriptions to useConnectionListeners

This commit is contained in:
binwiederhier 2023-06-14 08:18:55 -04:00
parent 790fd43369
commit 7083ed9f6b
2 changed files with 16 additions and 14 deletions

View file

@ -69,15 +69,6 @@ const Layout = () => {
const users = useLiveQuery(() => userManager.all()); const users = useLiveQuery(() => userManager.all());
const subscriptions = useLiveQuery(() => subscriptionManager.all()); const subscriptions = useLiveQuery(() => subscriptionManager.all());
const webPushTopics = useLiveQuery(() => subscriptionManager.webPushTopics()); const webPushTopics = useLiveQuery(() => subscriptionManager.webPushTopics());
const websocketSubscriptions = useMemo(
() => (subscriptions && webPushTopics ? subscriptions.filter((s) => !webPushTopics.includes(s.topic)) : []),
// websocketSubscriptions should stay stable unless the list of subscription ids changes.
// without the memoization, the connection listener calls a refresh for no reason.
// this isn't a problem due to the makeConnectionId, but it triggers an
// unnecessary recomputation for every received message.
[JSON.stringify({ subscriptions: subscriptions?.map(({ id }) => id), webPushTopics })]
);
const subscriptionsWithoutInternal = subscriptions?.filter((s) => !s.internal); const subscriptionsWithoutInternal = subscriptions?.filter((s) => !s.internal);
const newNotificationsCount = subscriptionsWithoutInternal?.reduce((prev, cur) => prev + cur.new, 0) || 0; const newNotificationsCount = subscriptionsWithoutInternal?.reduce((prev, cur) => prev + cur.new, 0) || 0;
const [selected] = (subscriptionsWithoutInternal || []).filter( const [selected] = (subscriptionsWithoutInternal || []).filter(
@ -86,7 +77,7 @@ const Layout = () => {
(config.base_url === s.baseUrl && params.topic === s.topic) (config.base_url === s.baseUrl && params.topic === s.topic)
); );
useConnectionListeners(account, websocketSubscriptions, users); useConnectionListeners(account, subscriptions, users, webPushTopics);
useAccountListener(setAccount); useAccountListener(setAccount);
useBackgroundProcesses(); useBackgroundProcesses();
useEffect(() => updateTitle(newNotificationsCount), [newNotificationsCount]); useEffect(() => updateTitle(newNotificationsCount), [newNotificationsCount]);

View file

@ -1,5 +1,5 @@
import { useNavigate, useParams } from "react-router-dom"; import { useNavigate, useParams } from "react-router-dom";
import { useEffect, useState } from "react"; import { useEffect, useMemo, useState } from "react";
import subscriptionManager from "../app/SubscriptionManager"; import subscriptionManager from "../app/SubscriptionManager";
import { disallowedTopic, expandSecureUrl, topicUrl } from "../app/utils"; import { disallowedTopic, expandSecureUrl, topicUrl } from "../app/utils";
import routes from "./routes"; import routes from "./routes";
@ -15,9 +15,20 @@ import { webPush, useWebPushTopicListener } from "../app/WebPush";
* Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection * Wire connectionManager and subscriptionManager so that subscriptions are updated when the connection
* state changes. Conversely, when the subscription changes, the connection is refreshed (which may lead * state changes. Conversely, when the subscription changes, the connection is refreshed (which may lead
* to the connection being re-established). * to the connection being re-established).
*
* When Web Push is enabled, we do not need to connect to our home server via WebSocket, since notifications
* will be delivered via Web Push. However, we still need to connect to other servers via WebSocket, or for internal
* topics, such as sync topics (st_...).
*/ */
export const useConnectionListeners = (account, subscriptions, users) => { export const useConnectionListeners = (account, subscriptions, users, webPushTopics) => {
const navigate = useNavigate(); const navigate = useNavigate();
const wsSubscriptions = useMemo(
() => (subscriptions && webPushTopics ? subscriptions.filter((s) => !webPushTopics.includes(s.topic)) : []),
// wsSubscriptions should stay stable unless the list of subscription IDs changes. Without the memo, the connection
// listener calls a refresh for no reason. This isn't a problem due to the makeConnectionId, but it triggers an
// unnecessary recomputation for every received message.
[JSON.stringify({ subscriptions: subscriptions?.map(({ id }) => id), webPushTopics })]
);
// Register listeners for incoming messages, and connection state changes // Register listeners for incoming messages, and connection state changes
useEffect( useEffect(
@ -84,8 +95,8 @@ export const useConnectionListeners = (account, subscriptions, users) => {
// When subscriptions or users change, refresh the connections // When subscriptions or users change, refresh the connections
useEffect(() => { useEffect(() => {
connectionManager.refresh(subscriptions, users); // Dangle connectionManager.refresh(wsSubscriptions, users); // Dangle
}, [subscriptions, users]); }, [wsSubscriptions, users]);
}; };
/** /**