|
1 | | -import { ContextQueryStore, TStateImpl } from "@context-query/core"; |
| 1 | +import { TStateImpl } from "@context-query/core"; |
2 | 2 | import { createUseContextQuery } from "./hooks"; |
3 | 3 | import { createReactContextQuery } from "./provider"; |
4 | 4 |
|
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); |
36 | 9 | return { |
37 | | - Provider, |
| 10 | + ContextQueryProvider, |
38 | 11 | useContextQuery, |
39 | | - updateState, |
40 | | - setState, |
41 | 12 | }; |
42 | 13 | } |
0 commit comments