Skip to content

Commit 293bd93

Browse files
committed
fix: remove subscription provider
1 parent 5ea52b0 commit 293bd93

File tree

3 files changed

+8
-35
lines changed

3 files changed

+8
-35
lines changed

packages/react/src/context.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import { createContext } from "react";
33

44
export const createContextQuery = <TState extends TStateImpl>() => {
55
const StoreContext = createContext<ContextQueryStore<TState> | null>(null);
6-
const ContextQuerySubscriptionContext = createContext<{
7-
subscribe: ContextQueryStore<TState>["subscribe"] | null;
8-
} | null>(null);
9-
106
return {
117
StoreContext,
12-
ContextQuerySubscriptionContext,
138
};
149
};

packages/react/src/hooks.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,18 @@ import { createContextQuery } from "./context";
55
export function createUseContextQuery<TState extends TStateImpl>(
66
contexts: ReturnType<typeof createContextQuery<TState>>
77
) {
8-
const { StoreContext, ContextQuerySubscriptionContext } = contexts;
8+
const { StoreContext } = contexts;
99

1010
const useLocalContexts = () => {
1111
const store = useContext(StoreContext);
12-
const subscription = useContext(ContextQuerySubscriptionContext);
1312

1413
if (!store) {
1514
throw new Error(
1615
"useContextQuery must be used within a ContextQueryProvider"
1716
);
1817
}
1918

20-
if (!subscription || !subscription.subscribe) {
21-
throw new Error(
22-
"ContextQuerySubscriptionContext not properly initialized"
23-
);
24-
}
25-
26-
return { store, subscription };
19+
return { store };
2720
};
2821

2922
const useContextBatchQuery = () => {
@@ -49,7 +42,7 @@ export function createUseContextQuery<TState extends TStateImpl>(
4942
TState[TKey],
5043
(value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => boolean,
5144
] => {
52-
const { store, subscription } = useLocalContexts();
45+
const { store } = useLocalContexts();
5346
const [state, setLocalState] = useState<TState[TKey]>(() =>
5447
store.getStateByKey(key)
5548
);
@@ -59,14 +52,12 @@ export function createUseContextQuery<TState extends TStateImpl>(
5952
setLocalState(newValue);
6053
};
6154

62-
if (!subscription.subscribe) return;
63-
64-
const sub = subscription.subscribe(key, handleChange);
55+
const sub = store.subscribe(key, handleChange);
6556

6657
return () => {
6758
sub.unsubscribe();
6859
};
69-
}, [key, subscription.subscribe, store]);
60+
}, [key, store]);
7061

7162
const setState = useCallback(
7263
(value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => {

packages/react/src/provider.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ContextQueryStore, TStateImpl } from "@context-query/core";
2-
import { FC, PropsWithChildren, useEffect, useMemo, useRef } from "react";
2+
import { FC, PropsWithChildren, useEffect, useRef } from "react";
33
import { createContextQuery } from "./context";
44

55
export interface ProviderProps<TState extends TStateImpl>
@@ -10,7 +10,7 @@ export interface ProviderProps<TState extends TStateImpl>
1010
export const createContextQueryProvider = <TState extends TStateImpl>(
1111
contexts: ReturnType<typeof createContextQuery<TState>>
1212
): FC<ProviderProps<TState>> => {
13-
const { StoreContext, ContextQuerySubscriptionContext } = contexts;
13+
const { StoreContext } = contexts;
1414

1515
return function ContextQueryProvider({
1616
children,
@@ -34,22 +34,9 @@ export const createContextQueryProvider = <TState extends TStateImpl>(
3434
}
3535
}, [initialState]);
3636

37-
const contextQuerySubscriptionContextValue = useMemo(
38-
() => ({
39-
subscribe: storeRef.current
40-
? storeRef.current.subscribe.bind(storeRef.current)
41-
: null,
42-
}),
43-
[storeRef.current]
44-
);
45-
4637
return (
4738
<StoreContext.Provider value={storeRef.current}>
48-
<ContextQuerySubscriptionContext.Provider
49-
value={contextQuerySubscriptionContextValue}
50-
>
51-
{children}
52-
</ContextQuerySubscriptionContext.Provider>
39+
{children}
5340
</StoreContext.Provider>
5441
);
5542
};

0 commit comments

Comments
 (0)