Skip to content

Commit 92acb54

Browse files
committed
extract use-subscribe-to-space
1 parent f0617d8 commit 92acb54

File tree

4 files changed

+41
-97
lines changed

4 files changed

+41
-97
lines changed

packages/hypergraph-react/src/HypergraphSpaceContext.tsx

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import { Entity, store } from '@graphprotocol/hypergraph';
44
import { useSelector } from '@xstate/store/react';
5-
import { createContext, type ReactNode, useContext, useEffect } from 'react';
6-
import { useHypergraphApp } from './HypergraphAppContext.js';
5+
import { createContext, type ReactNode, useContext } from 'react';
76
import { usePublicSpace } from './internal/use-public-space.js';
7+
import { useSubscribeToSpaceAndGetHandle } from './internal/use-subscribe-to-space.js';
88

99
// TODO space can be undefined
1010
export type HypergraphContext = { space: string };
@@ -20,35 +20,6 @@ export function HypergraphSpaceProvider({ space, children }: { space: string; ch
2020
return <HypergraphReactContext.Provider value={{ space }}>{children}</HypergraphReactContext.Provider>;
2121
}
2222

23-
const subscribeToSpaceCache = new Map<string, boolean>();
24-
25-
function useSubscribeToSpaceAndGetHandle({ spaceId, enabled }: { spaceId: string; enabled: boolean }) {
26-
const handle = useSelector(store, (state) => {
27-
const space = state.context.spaces.find((space) => space.id === spaceId);
28-
if (!space) {
29-
return undefined;
30-
}
31-
return space.automergeDocHandle;
32-
});
33-
34-
const { subscribeToSpace, isConnecting } = useHypergraphApp();
35-
useEffect(() => {
36-
if (!isConnecting && enabled) {
37-
if (subscribeToSpaceCache.has(spaceId)) {
38-
return;
39-
}
40-
subscribeToSpaceCache.set(spaceId, true);
41-
subscribeToSpace({ spaceId });
42-
}
43-
return () => {
44-
// TODO: unsubscribe from space in case the space ID changes
45-
subscribeToSpaceCache.delete(spaceId);
46-
};
47-
}, [isConnecting, subscribeToSpace, spaceId, enabled]);
48-
49-
return handle;
50-
}
51-
5223
export function useSpace(options: { space?: string; mode: 'private' | 'public' }) {
5324
const { space: spaceIdFromContext } = useHypergraphSpaceInternal();
5425
const { space: spaceIdFromParams } = options ?? {};

packages/hypergraph-react/src/internal/use-entity-private.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,8 @@
1-
import { Entity, type Id, store } from '@graphprotocol/hypergraph';
2-
import { useSelector } from '@xstate/store/react';
1+
import { Entity, type Id } from '@graphprotocol/hypergraph';
32
import * as Schema from 'effect/Schema';
4-
import { useEffect, useRef, useSyncExternalStore } from 'react';
5-
import { useHypergraphApp } from '../HypergraphAppContext.js';
3+
import { useRef, useSyncExternalStore } from 'react';
64
import { useHypergraphSpaceInternal } from '../HypergraphSpaceContext.js';
7-
8-
const subscribeToSpaceCache = new Map<string, boolean>();
9-
10-
function useSubscribeToSpaceAndGetHandle({ spaceId, enabled }: { spaceId: string; enabled: boolean }) {
11-
const handle = useSelector(store, (state) => {
12-
const space = state.context.spaces.find((space) => space.id === spaceId);
13-
if (!space) {
14-
return undefined;
15-
}
16-
return space.automergeDocHandle;
17-
});
18-
19-
const { subscribeToSpace, isConnecting } = useHypergraphApp();
20-
useEffect(() => {
21-
if (!isConnecting && enabled) {
22-
if (subscribeToSpaceCache.has(spaceId)) {
23-
return;
24-
}
25-
subscribeToSpaceCache.set(spaceId, true);
26-
subscribeToSpace({ spaceId });
27-
}
28-
return () => {
29-
// TODO: unsubscribe from space in case the space ID changes
30-
subscribeToSpaceCache.delete(spaceId);
31-
};
32-
}, [isConnecting, subscribeToSpace, spaceId, enabled]);
33-
34-
return handle;
35-
}
5+
import { useSubscribeToSpaceAndGetHandle } from './use-subscribe-to-space.js';
366

377
export function useEntityPrivate<const S extends Entity.AnyNoContext>(
388
type: S,

packages/hypergraph-react/src/internal/use-query-private.tsx

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,10 @@
11
'use client';
22

3-
import { Entity, store } from '@graphprotocol/hypergraph';
4-
import { useSelector } from '@xstate/store/react';
3+
import { Entity } from '@graphprotocol/hypergraph';
54
import type * as Schema from 'effect/Schema';
6-
import { useEffect, useLayoutEffect, useMemo, useRef, useSyncExternalStore } from 'react';
7-
import { useHypergraphApp } from '../HypergraphAppContext.js';
5+
import { useLayoutEffect, useMemo, useRef, useSyncExternalStore } from 'react';
86
import { useHypergraphSpaceInternal } from '../HypergraphSpaceContext.js';
9-
10-
const subscribeToSpaceCache = new Map<string, boolean>();
11-
12-
function useSubscribeToSpaceAndGetHandle({ spaceId, enabled }: { spaceId: string; enabled: boolean }) {
13-
const handle = useSelector(store, (state) => {
14-
const space = state.context.spaces.find((space) => space.id === spaceId);
15-
if (!space) {
16-
return undefined;
17-
}
18-
return space.automergeDocHandle;
19-
});
20-
21-
const { subscribeToSpace, isConnecting } = useHypergraphApp();
22-
useEffect(() => {
23-
if (!isConnecting && enabled) {
24-
if (subscribeToSpaceCache.has(spaceId)) {
25-
return;
26-
}
27-
subscribeToSpaceCache.set(spaceId, true);
28-
subscribeToSpace({ spaceId });
29-
}
30-
return () => {
31-
// TODO: unsubscribe from space in case the space ID changes
32-
subscribeToSpaceCache.delete(spaceId);
33-
};
34-
}, [isConnecting, subscribeToSpace, spaceId, enabled]);
35-
36-
return handle;
37-
}
7+
import { useSubscribeToSpaceAndGetHandle } from './use-subscribe-to-space.js';
388

399
type QueryParams<S extends Entity.AnyNoContext> = {
4010
space?: string | undefined;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { store } from '@graphprotocol/hypergraph';
2+
import { useSelector } from '@xstate/store/react';
3+
import { useEffect } from 'react';
4+
import { useHypergraphApp } from '../HypergraphAppContext.js';
5+
6+
const subscribeToSpaceCache = new Map<string, boolean>();
7+
8+
export function useSubscribeToSpaceAndGetHandle({ spaceId, enabled }: { spaceId: string; enabled: boolean }) {
9+
const handle = useSelector(store, (state) => {
10+
const space = state.context.spaces.find((space) => space.id === spaceId);
11+
if (!space) {
12+
return undefined;
13+
}
14+
return space.automergeDocHandle;
15+
});
16+
17+
const { subscribeToSpace, isConnecting } = useHypergraphApp();
18+
useEffect(() => {
19+
if (!isConnecting && enabled) {
20+
if (subscribeToSpaceCache.has(spaceId)) {
21+
return;
22+
}
23+
subscribeToSpaceCache.set(spaceId, true);
24+
subscribeToSpace({ spaceId });
25+
}
26+
return () => {
27+
// TODO: unsubscribe from space in case the space ID changes
28+
subscribeToSpaceCache.delete(spaceId);
29+
};
30+
}, [isConnecting, subscribeToSpace, spaceId, enabled]);
31+
32+
return handle;
33+
}

0 commit comments

Comments
 (0)