Skip to content

Commit 59dd94f

Browse files
committed
feat: move store to provider
- 프로바이더 안에 스토어를 이동시키므로써 프로바디어를 재활용 가
1 parent c658dbf commit 59dd94f

File tree

4 files changed

+25
-67
lines changed

4 files changed

+25
-67
lines changed

packages/react/src/context.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import type { ContextQueryStore, TStateImpl } from "@context-query/core";
22
import { createContext } from "react";
33

4-
export const createContextQuery = <TState extends TStateImpl>() => {
5-
const StoreContext = createContext<ContextQueryStore<TState> | null>(null);
6-
return {
7-
StoreContext,
8-
};
4+
export const createStoreContext = <TState extends TStateImpl>() => {
5+
return createContext<ContextQueryStore<TState> | null>(null);
96
};

packages/react/src/hooks.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { TStateImpl } from "@context-query/core";
22
import { useCallback, useContext, useEffect, useState } from "react";
3-
import { createContextQuery } from "./context";
3+
import { createStoreContext } from "./context";
44

55
export function createUseContextQuery<TState extends TStateImpl>(
6-
contexts: ReturnType<typeof createContextQuery<TState>>
6+
contexts: ReturnType<typeof createStoreContext<TState>>
77
) {
8-
const { StoreContext } = contexts;
9-
108
const useStore = () => {
11-
const store = useContext(StoreContext);
9+
const store = useContext(contexts);
1210

1311
if (!store) {
1412
throw new Error(

packages/react/src/index.ts

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,13 @@
1-
import { ContextQueryStore, TStateImpl } from "@context-query/core";
1+
import { TStateImpl } from "@context-query/core";
22
import { createUseContextQuery } from "./hooks";
33
import { createReactContextQuery } from "./provider";
44

5-
export function createContextQuery<TState extends TStateImpl>(
6-
initialState: TState
7-
) {
8-
const store = new ContextQueryStore<TState>(initialState);
9-
10-
const { Provider, contexts } = createReactContextQuery<TState>(store);
11-
const useContextQuery = createUseContextQuery<TState>(contexts);
12-
13-
const updateState = (state: TState | ((prev: TState) => TState)) => {
14-
if (typeof state === "function") {
15-
const updateFn = state as (prev: TState) => TState;
16-
const currentValue = store.getState();
17-
store.updateState(updateFn(currentValue));
18-
} else {
19-
store.updateState(state);
20-
}
21-
};
22-
23-
const setState = <TKey extends keyof TState>(
24-
key: TKey,
25-
value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])
26-
) => {
27-
if (typeof value === "function") {
28-
const updateFn = value as (prev: TState[TKey]) => TState[TKey];
29-
const currentValue = store.getStateByKey(key);
30-
store.setState(key, updateFn(currentValue));
31-
} else {
32-
store.setState(key, value);
33-
}
34-
};
35-
5+
export function createContextQuery<TState extends TStateImpl>() {
6+
const { ContextQueryProvider, StoreContext } =
7+
createReactContextQuery<TState>();
8+
const useContextQuery = createUseContextQuery<TState>(StoreContext);
369
return {
37-
Provider,
10+
ContextQueryProvider,
3811
useContextQuery,
39-
updateState,
40-
setState,
4112
};
4213
}

packages/react/src/provider.tsx

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

5-
export type ProviderProps<TState extends TStateImpl> = PropsWithChildren;
6-
7-
export const createContextQueryProvider = <TState extends TStateImpl>(
8-
contexts: ReturnType<typeof createContextQuery<TState>>,
9-
store: ContextQueryStore<TState>
10-
): FC<ProviderProps<TState>> => {
11-
const { StoreContext } = contexts;
5+
export function createReactContextQuery<TState extends TStateImpl>() {
6+
const StoreContext = createStoreContext<TState>();
7+
const ContextQueryProvider = function ContextQueryProvider({
8+
children,
9+
initialState,
10+
}: PropsWithChildren<TState>) {
11+
const store = useMemo(
12+
() => new ContextQueryStore<TState>(initialState),
13+
[initialState]
14+
);
1215

13-
return function ContextQueryProvider({ children }: ProviderProps<TState>) {
1416
return (
1517
<StoreContext.Provider value={store}>{children}</StoreContext.Provider>
1618
);
1719
};
18-
};
19-
20-
export function createReactContextQuery<TState extends TStateImpl>(
21-
store: ContextQueryStore<TState>
22-
) {
23-
const contexts = createContextQuery<TState>();
24-
const ContextQueryProvider = createContextQueryProvider<TState>(
25-
contexts,
26-
store
27-
);
2820

2921
return {
30-
Provider: ContextQueryProvider,
31-
contexts,
22+
ContextQueryProvider,
23+
StoreContext,
3224
};
3325
}

0 commit comments

Comments
 (0)