Skip to content

Commit 0347da8

Browse files
committed
avoid subscribing to space too many times
1 parent 92b0d27 commit 0347da8

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

apps/events/src/routes/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export const Route = createFileRoute('/')({
1111
component: Index,
1212
});
1313

14+
// @ts-expect-error
15+
window.HYPERGRAPH_STORE = store;
16+
1417
function Index() {
1518
const { data: publicSpaces } = useSpaces({ mode: 'public' });
1619
const { data: privateSpaces } = useSpaces({ mode: 'private' });

packages/hypergraph-react/src/HypergraphSpaceContext.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export function HypergraphSpaceProvider({ space, children }: { space: string; ch
3232
return <HypergraphReactContext.Provider value={{ space }}>{children}</HypergraphReactContext.Provider>;
3333
}
3434

35+
const subscribeToSpaceCache = new Map<string, boolean>();
36+
3537
function useSubscribeToSpaceAndGetHandle({ spaceId, enabled }: { spaceId: string; enabled: boolean }) {
3638
const handle = useSelector(store, (state) => {
3739
const space = state.context.spaces.find((space) => space.id === spaceId);
@@ -44,11 +46,15 @@ function useSubscribeToSpaceAndGetHandle({ spaceId, enabled }: { spaceId: string
4446
const { subscribeToSpace, isConnecting } = useHypergraphApp();
4547
useEffect(() => {
4648
if (!isConnecting && enabled) {
47-
// TODO: only subscribe to space if it is not already subscribed
49+
if (subscribeToSpaceCache.has(spaceId)) {
50+
return;
51+
}
52+
subscribeToSpaceCache.set(spaceId, true);
4853
subscribeToSpace({ spaceId });
4954
}
5055
return () => {
5156
// TODO: unsubscribe from space in case the space ID changes
57+
subscribeToSpaceCache.delete(spaceId);
5258
};
5359
}, [isConnecting, subscribeToSpace, spaceId, enabled]);
5460

0 commit comments

Comments
 (0)