@context-query/react 0.2.0-dev.1 → 0.2.0-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/context.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import type { ContextQueryStore, TStateImpl } from "@context-query/core";
2
2
  export declare const createContextQuery: <TState extends TStateImpl>() => {
3
3
  StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
4
+ ContextQuerySubscriptionContext: import("react").Context<{
5
+ subscribe: ContextQueryStore<TState>["subscribe"] | null;
6
+ } | null>;
4
7
  };
package/dist/hooks.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import { TStateImpl } from "@context-query/core";
2
2
  import { createContextQuery } from "./context";
3
- export declare function createUseContextQuery<TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>): <TKey extends keyof TState>(keys?: TKey[]) => readonly [{ [K in TKey]: TState[K]; }, (value: { [K in TKey]: TState[K]; } | ((prev: { [K in TKey]: TState[K]; }) => { [K in TKey]: TState[K]; })) => void];
3
+ export declare function createUseContextQuery<TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>): {
4
+ useContextBatchQuery: () => (value: TState | ((prev: TState) => TState)) => boolean;
5
+ useContextQuery: <TKey extends keyof TState>(key: TKey) => [TState[TKey], (value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => boolean];
6
+ };
package/dist/index.d.ts CHANGED
@@ -1,9 +1,6 @@
1
1
  import { TStateImpl } from "@context-query/core";
2
- export declare function createContextQuery<TState extends TStateImpl>(initialState: TState): {
3
- Provider: import("react").FC<{
4
- children?: import("react").ReactNode | undefined;
5
- }>;
6
- useContextQuery: <TKey extends keyof TState>(keys?: TKey[] | undefined) => readonly [{ [K in TKey]: TState[K]; }, (value: { [K in TKey]: TState[K]; } | ((prev: { [K in TKey]: TState[K]; }) => { [K in TKey]: TState[K]; })) => void];
7
- updateState: (state: TState | ((prev: TState) => TState)) => void;
8
- setState: <TKey extends keyof TState>(key: TKey, value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => void;
2
+ export declare function createContextQuery<TState extends TStateImpl>(): {
3
+ Provider: import("react").FC<import("./provider").ProviderProps<TState>>;
4
+ useContextQuery: <TKey extends keyof TState>(key: TKey) => [TState[TKey], (value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => boolean];
5
+ useContextBatchQuery: () => (value: TState | ((prev: TState) => TState)) => boolean;
9
6
  };
package/dist/index.esm.js CHANGED
@@ -1,2 +1,2 @@
1
- import{useState as t,useEffect as e,useCallback as s,useContext as r,createContext as n}from"react";import{jsx as i}from"react/jsx-runtime";var o,a={};var c=(o||(o=1,a.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const e={...this.state};this.state={...t},Object.keys(t).forEach((t=>{Object.is(e[t],this.state[t])||this.notifyListeners(t)}))}setState(t,e){Object.is(this.state[t],e)||(this.state={...this.state,[t]:e},this.notifyListeners(t))}notifyListeners(t){const e=this.listeners.get(t);if(e){const s=this.state[t];e.forEach((t=>t(s)))}}subscribe(t,e){const s=s=>{e(t,s)};return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(s),{unsubscribe:()=>{const e=this.listeners.get(t);e&&(e.delete(s),0===e.size&&this.listeners.delete(t))}}}}),a);function u(n){const{StoreContext:i}=n;return n=>{const o=(()=>{const t=r(i);if(!t)throw new Error("useContextQuery must be used within a ContextQueryProvider");return t})(),a=n??Object.keys(o.getState()),c=()=>a.reduce(((t,e)=>({...t,[e]:o.getStateByKey(e)})),{}),[u,h]=t(c);e((()=>{const t=(t,e)=>{h((s=>({...s,[t]:e})))},e=a.map((e=>o.subscribe(e,t)));return()=>{e.forEach((t=>t.unsubscribe()))}}),[a,o]);return[u,s((t=>{const e=c(),s="function"==typeof t?t(e):t;Object.entries(s).forEach((([t,e])=>{o.setState(t,e)}))}),[a,o])]}}function h(t){const e={StoreContext:n(null)},s=((t,e)=>{const{StoreContext:s}=t;return function({children:t}){return i(s.Provider,{value:e,children:t})}})(e,t);return{Provider:s,contexts:e}}function f(t){const e=new c.ContextQueryStore(t),{Provider:s,contexts:r}=h(e);return{Provider:s,useContextQuery:u(r),updateState:t=>{if("function"==typeof t){const s=t,r=e.getState();e.updateState(s(r))}else e.updateState(t)},setState:(t,s)=>{if("function"==typeof s){const r=s,n=e.getStateByKey(t);e.setState(t,r(n))}else e.setState(t,s)}}}export{f as createContextQuery};
1
+ import{useState as t,useEffect as e,useCallback as r,useContext as n,createContext as s,useRef as i,useMemo as u}from"react";import{jsx as o}from"react/jsx-runtime";var c,a={};var h=(c||(c=1,a.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const e={...this.state};return this.state={...t},Object.keys(t).forEach((t=>{Object.is(e[t],this.state[t])||this.notifyListeners(t)})),!0}setState(t,e){return!Object.is(this.state[t],e)&&(this.state={...this.state,[t]:e},this.notifyListeners(t),!0)}notifyListeners(t){const e=this.listeners.get(t);if(e){const r=this.state[t];e.forEach((t=>t(r)))}}subscribe(t,e){const r=r=>{e(t,r)};return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(r),{unsubscribe:()=>{const e=this.listeners.get(t);e&&(e.delete(r),0===e.size&&this.listeners.delete(t))}}}}),a);function b(){const t={StoreContext:s(null),ContextQuerySubscriptionContext:s(null)},r=(t=>{const{StoreContext:r,ContextQuerySubscriptionContext:n}=t;return function({children:t,initialState:s}){const c=i(null),a=i(null);c.current||(c.current=new h.ContextQueryStore(s),a.current=s),e((()=>{c.current&&!Object.is(a.current,s)&&(c.current.updateState(s),a.current=s)}),[s]);const b=u((()=>({subscribe:c.current?c.current.subscribe.bind(c.current):null})),[c.current]);return o(r.Provider,{value:c.current,children:o(n.Provider,{value:b,children:t})})}})(t);return{Provider:r,contexts:t}}function l(){const{Provider:s,contexts:i}=b(),{useContextQuery:u,useContextBatchQuery:o}=function(s){const{StoreContext:i,ContextQuerySubscriptionContext:u}=s,o=()=>{const t=n(i),e=n(u);if(!t)throw new Error("useContextQuery must be used within a ContextQueryProvider");if(!e||!e.subscribe)throw new Error("ContextQuerySubscriptionContext not properly initialized");return{store:t,subscription:e}};return{useContextBatchQuery:()=>{const{store:t}=o();return r((e=>{if("function"==typeof e){const r=e,n=t.getState();return t.updateState(r(n))}return t.updateState(e)}),[t])},useContextQuery:n=>{const{store:s,subscription:i}=o(),[u,c]=t((()=>s.getStateByKey(n)));return e((()=>{if(!i.subscribe)return;const t=i.subscribe(n,((t,e)=>{c(e)}));return()=>{t.unsubscribe()}}),[n,i.subscribe,s]),[u,r((t=>{if("function"==typeof t){const e=t,r=s.getStateByKey(n);return s.setState(n,e(r))}return s.setState(n,t)}),[n,s])]}}}(i);return{Provider:s,useContextQuery:u,useContextBatchQuery:o}}export{l as createContextQuery};
2
2
  //# sourceMappingURL=index.esm.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.esm.js","sources":["../../core/dist/index.js","../src/hooks.ts","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["\"use strict\";exports.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const s={...this.state};this.state={...t};Object.keys(t).forEach((t=>{Object.is(s[t],this.state[t])||this.notifyListeners(t)}))}setState(t,s){Object.is(this.state[t],s)||(this.state={...this.state,[t]:s},this.notifyListeners(t))}notifyListeners(t){const s=this.listeners.get(t);if(s){const e=this.state[t];s.forEach((t=>t(e)))}}subscribe(t,s){const e=e=>{s(t,e)};this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(e);return{unsubscribe:()=>{const s=this.listeners.get(t);s&&(s.delete(e),0===s.size&&this.listeners.delete(t))}}}};\n//# sourceMappingURL=index.js.map\n","import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n) {\n const { StoreContext } = contexts;\n\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextQuery must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TState>(keys?: TKey[]) => {\n const store = useStore();\n const localKeys = keys ?? (Object.keys(store.getState()) as TKey[]);\n\n type StateSubset = { [K in TKey]: TState[K] };\n\n const getStateSubset = (): StateSubset => {\n return localKeys.reduce(\n (acc, key) => ({ ...acc, [key]: store.getStateByKey(key) }),\n {} as StateSubset\n );\n };\n\n const [state, setLocalState] = useState<StateSubset>(getStateSubset);\n\n useEffect(() => {\n const handleChange = (key: TKey, newValue: TState[TKey]) => {\n setLocalState((prev) => ({ ...prev, [key]: newValue }));\n };\n\n const subscriptions = localKeys.map((key) =>\n store.subscribe(key, handleChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n }, [localKeys, store]);\n\n const setState = useCallback(\n (value: StateSubset | ((prev: StateSubset) => StateSubset)) => {\n const currentValue = getStateSubset();\n const updatedValue =\n typeof value === \"function\"\n ? (value as Function)(currentValue)\n : value;\n\n Object.entries(updatedValue).forEach(([k, v]) => {\n store.setState(k as TKey, v as TState[TKey]);\n });\n },\n [localKeys, store]\n );\n\n return [state, setState] as const;\n };\n}\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { FC, PropsWithChildren } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport type ProviderProps<TState extends TStateImpl> = PropsWithChildren;\n\nexport const createContextQueryProvider = <TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>,\n store: ContextQueryStore<TState>\n): FC<ProviderProps<TState>> => {\n const { StoreContext } = contexts;\n\n return function ContextQueryProvider({ children }: ProviderProps<TState>) {\n return (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n );\n };\n};\n\nexport function createReactContextQuery<TState extends TStateImpl>(\n store: ContextQueryStore<TState>\n) {\n const contexts = createContextQuery<TState>();\n const ContextQueryProvider = createContextQueryProvider<TState>(\n contexts,\n store\n );\n\n return {\n Provider: ContextQueryProvider,\n contexts,\n };\n}\n","import type { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createContextQuery = <TState extends TStateImpl>() => {\n const StoreContext = createContext<ContextQueryStore<TState> | null>(null);\n return {\n StoreContext,\n };\n};\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createUseContextQuery } from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TState extends TStateImpl>(\n initialState: TState\n) {\n const store = new ContextQueryStore<TState>(initialState);\n\n const { Provider, contexts } = createReactContextQuery<TState>(store);\n const useContextQuery = createUseContextQuery<TState>(contexts);\n\n const updateState = (state: TState | ((prev: TState) => TState)) => {\n if (typeof state === \"function\") {\n const updateFn = state as (prev: TState) => TState;\n const currentValue = store.getState();\n store.updateState(updateFn(currentValue));\n } else {\n store.updateState(state);\n }\n };\n\n const setState = <TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])\n ) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState[TKey]) => TState[TKey];\n const currentValue = store.getStateByKey(key);\n store.setState(key, updateFn(currentValue));\n } else {\n store.setState(key, value);\n }\n };\n\n return {\n Provider,\n useContextQuery,\n updateState,\n setState,\n };\n}\n"],"names":["dist","ContextQueryStore","state","listeners","constructor","t","this","Map","getState","getStateByKey","updateState","s","Object","keys","forEach","is","notifyListeners","setState","get","e","subscribe","has","set","Set","add","unsubscribe","delete","size","createUseContextQuery","contexts","StoreContext","store","useContext","Error","useStore","localKeys","getStateSubset","reduce","acc","key","setLocalState","useState","useEffect","handleChange","newValue","prev","subscriptions","map","sub","useCallback","value","currentValue","updatedValue","entries","k","v","createReactContextQuery","createContext","ContextQueryProvider","children","_jsx","Provider","createContextQueryProvider","createContextQuery","initialState","useContextQuery","updateFn"],"mappings":"sKAAaA,EAAAC,kBAA0B,MAAMC,MAAMC,UAAU,WAAAC,CAAYC,GAAGC,KAAKJ,MAAMG,EAAEC,KAAKH,UAAU,IAAII,GAAG,CAAC,QAAAC,GAAW,OAAOF,KAAKJ,KAAK,CAAC,aAAAO,CAAcJ,GAAG,OAAOC,KAAKJ,MAAMG,EAAE,CAAC,WAAAK,CAAYL,GAAG,MAAMM,EAAE,IAAIL,KAAKJ,OAAOI,KAAKJ,MAAM,IAAIG,GAAGO,OAAOC,KAAKR,GAAGS,SAAST,IAAIO,OAAOG,GAAGJ,EAAEN,GAAGC,KAAKJ,MAAMG,KAAKC,KAAKU,gBAAgBX,EAAG,GAAE,CAAC,QAAAY,CAASZ,EAAEM,GAAGC,OAAOG,GAAGT,KAAKJ,MAAMG,GAAGM,KAAKL,KAAKJ,MAAM,IAAII,KAAKJ,MAAMG,CAACA,GAAGM,GAAGL,KAAKU,gBAAgBX,GAAG,CAAC,eAAAW,CAAgBX,GAAG,MAAMM,EAAEL,KAAKH,UAAUe,IAAIb,GAAG,GAAGM,EAAE,CAAC,MAAMQ,EAAEb,KAAKJ,MAAMG,GAAGM,EAAEG,SAAST,GAAGA,EAAEc,IAAI,CAAC,CAAC,SAAAC,CAAUf,EAAEM,GAAG,MAAMQ,EAAEA,IAAIR,EAAEN,EAAEc,EAAC,EAAqF,OAAlFb,KAAKH,UAAUkB,IAAIhB,IAAIC,KAAKH,UAAUmB,IAAIjB,EAAE,IAAIkB,KAAKjB,KAAKH,UAAUe,IAAIb,GAAGmB,IAAIL,GAAS,CAACM,YAAY,KAAK,MAAMd,EAAEL,KAAKH,UAAUe,IAAIb,GAAGM,IAAIA,EAAEe,OAAOP,GAAG,IAAIR,EAAEgB,MAAMrB,KAAKH,UAAUuB,OAAOrB,GAAE,EAAG,OCItuB,SAAU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EAAgCzB,GA0B/D,MAAO,CACL8B,WACAI,gBA3BsBrC,EAA8BC,GA4BpDnB,YA1BmBR,IACnB,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMgE,EAAWhE,EACXiD,EAAepB,EAAMvB,WAC3BuB,EAAMrB,YAAYwD,EAASf,SAE3BpB,EAAMrB,YAAYR,IAqBpBe,SAjBe,CACfsB,EACAW,KAEA,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMgB,EAAWhB,EACXC,EAAepB,EAAMtB,cAAc8B,GACzCR,EAAMd,SAASsB,EAAK2B,EAASf,SAE7BpB,EAAMd,SAASsB,EAAKW,IAU1B"}
1
+ {"version":3,"file":"index.esm.js","sources":["../../../node_modules/.pnpm/@context-query+core@0.1.0-dev.1/node_modules/@context-query/core/dist/index.js","../src/provider.tsx","../src/context.ts","../src/index.ts","../src/hooks.ts"],"sourcesContent":["\"use strict\";exports.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const s={...this.state};this.state={...t};return Object.keys(t).forEach((t=>{Object.is(s[t],this.state[t])||this.notifyListeners(t)})),!0}setState(t,s){return!Object.is(this.state[t],s)&&(this.state={...this.state,[t]:s},this.notifyListeners(t),!0)}notifyListeners(t){const s=this.listeners.get(t);if(s){const e=this.state[t];s.forEach((t=>t(e)))}}subscribe(t,s){const e=e=>{s(t,e)};this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(e);return{unsubscribe:()=>{const s=this.listeners.get(t);s&&(s.delete(e),0===s.size&&this.listeners.delete(t))}}}};\n//# sourceMappingURL=index.js.map\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { FC, PropsWithChildren, useEffect, useMemo, useRef } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport interface ProviderProps<TState extends TStateImpl>\n extends PropsWithChildren {\n initialState: TState;\n}\n\nexport const createContextQueryProvider = <TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n): FC<ProviderProps<TState>> => {\n const { StoreContext, ContextQuerySubscriptionContext } = contexts;\n\n return function ContextQueryProvider({\n children,\n initialState,\n }: ProviderProps<TState>) {\n const storeRef = useRef<ContextQueryStore<TState> | null>(null);\n const previousStateRef = useRef<TState | null>(null);\n\n if (!storeRef.current) {\n storeRef.current = new ContextQueryStore<TState>(initialState);\n previousStateRef.current = initialState;\n }\n\n useEffect(() => {\n if (\n storeRef.current &&\n !Object.is(previousStateRef.current, initialState)\n ) {\n storeRef.current.updateState(initialState);\n previousStateRef.current = initialState;\n }\n }, [initialState]);\n\n const contextQuerySubscriptionContextValue = useMemo(\n () => ({\n subscribe: storeRef.current\n ? storeRef.current.subscribe.bind(storeRef.current)\n : null,\n }),\n [storeRef.current]\n );\n\n return (\n <StoreContext.Provider value={storeRef.current}>\n <ContextQuerySubscriptionContext.Provider\n value={contextQuerySubscriptionContextValue}\n >\n {children}\n </ContextQuerySubscriptionContext.Provider>\n </StoreContext.Provider>\n );\n };\n};\n\nexport function createReactContextQuery<TState extends TStateImpl>() {\n const contexts = createContextQuery<TState>();\n const ContextQueryProvider = createContextQueryProvider<TState>(contexts);\n\n return {\n Provider: ContextQueryProvider,\n contexts,\n };\n}\n","import type { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createContextQuery = <TState extends TStateImpl>() => {\n const StoreContext = createContext<ContextQueryStore<TState> | null>(null);\n const ContextQuerySubscriptionContext = createContext<{\n subscribe: ContextQueryStore<TState>[\"subscribe\"] | null;\n } | null>(null);\n\n return {\n StoreContext,\n ContextQuerySubscriptionContext,\n };\n};\n","import { TStateImpl } from \"@context-query/core\";\nimport { createUseContextQuery } from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TState extends TStateImpl>() {\n const { Provider, contexts } = createReactContextQuery<TState>();\n\n const { useContextQuery, useContextBatchQuery } =\n createUseContextQuery<TState>(contexts);\n\n return {\n Provider,\n useContextQuery,\n useContextBatchQuery,\n };\n}\n","import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n) {\n const { StoreContext, ContextQuerySubscriptionContext } = contexts;\n\n const useLocalContexts = () => {\n const store = useContext(StoreContext);\n const subscription = useContext(ContextQuerySubscriptionContext);\n\n if (!store) {\n throw new Error(\n \"useContextQuery must be used within a ContextQueryProvider\"\n );\n }\n\n if (!subscription || !subscription.subscribe) {\n throw new Error(\n \"ContextQuerySubscriptionContext not properly initialized\"\n );\n }\n\n return { store, subscription };\n };\n\n const useContextBatchQuery = () => {\n const { store } = useLocalContexts();\n const setState = useCallback(\n (value: TState | ((prev: TState) => TState)) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState) => TState;\n const currentValue = store.getState();\n return store.updateState(updateFn(currentValue));\n }\n return store.updateState(value);\n },\n [store]\n );\n\n return setState;\n };\n\n const useContextQuery = <TKey extends keyof TState>(\n key: TKey\n ): [\n TState[TKey],\n (value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => boolean,\n ] => {\n const { store, subscription } = useLocalContexts();\n const [state, setLocalState] = useState<TState[TKey]>(() =>\n store.getStateByKey(key)\n );\n\n useEffect(() => {\n const handleChange = (_: TKey, newValue: TState[TKey]) => {\n setLocalState(newValue);\n };\n\n if (!subscription.subscribe) return;\n\n const sub = subscription.subscribe(key, handleChange);\n\n return () => {\n sub.unsubscribe();\n };\n }, [key, subscription.subscribe, store]);\n\n const setState = useCallback(\n (value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState[TKey]) => TState[TKey];\n const currentValue = store.getStateByKey(key);\n return store.setState(key, updateFn(currentValue));\n }\n return store.setState(key, value);\n },\n [key, store]\n );\n\n return [state, setState];\n };\n\n return {\n useContextBatchQuery,\n useContextQuery,\n };\n}\n"],"names":["dist","ContextQueryStore","state","listeners","constructor","t","this","Map","getState","getStateByKey","updateState","s","Object","keys","forEach","is","notifyListeners","setState","get","e","subscribe","has","set","Set","add","unsubscribe","delete","size","createReactContextQuery","contexts","StoreContext","createContext","ContextQuerySubscriptionContext","ContextQueryProvider","children","initialState","storeRef","useRef","previousStateRef","current","useEffect","contextQuerySubscriptionContextValue","useMemo","bind","_jsx","Provider","value","createContextQueryProvider","createContextQuery","useContextQuery","useContextBatchQuery","useLocalContexts","store","useContext","subscription","Error","useCallback","updateFn","currentValue","key","setLocalState","useState","sub","_","newValue","createUseContextQuery"],"mappings":"+LAAaA,EAAAC,kBAA0B,MAAMC,MAAMC,UAAU,WAAAC,CAAYC,GAAGC,KAAKJ,MAAMG,EAAEC,KAAKH,UAAU,IAAII,GAAG,CAAC,QAAAC,GAAW,OAAOF,KAAKJ,KAAK,CAAC,aAAAO,CAAcJ,GAAG,OAAOC,KAAKJ,MAAMG,EAAE,CAAC,WAAAK,CAAYL,GAAG,MAAMM,EAAE,IAAIL,KAAKJ,OAAyB,OAAlBI,KAAKJ,MAAM,IAAIG,GAAUO,OAAOC,KAAKR,GAAGS,SAAST,IAAIO,OAAOG,GAAGJ,EAAEN,GAAGC,KAAKJ,MAAMG,KAAKC,KAAKU,gBAAgBX,EAAG,KAAG,CAAE,CAAC,QAAAY,CAASZ,EAAEM,GAAG,OAAOC,OAAOG,GAAGT,KAAKJ,MAAMG,GAAGM,KAAKL,KAAKJ,MAAM,IAAII,KAAKJ,MAAMG,CAACA,GAAGM,GAAGL,KAAKU,gBAAgBX,IAAG,EAAG,CAAC,eAAAW,CAAgBX,GAAG,MAAMM,EAAEL,KAAKH,UAAUe,IAAIb,GAAG,GAAGM,EAAE,CAAC,MAAMQ,EAAEb,KAAKJ,MAAMG,GAAGM,EAAEG,SAAST,GAAGA,EAAEc,IAAI,CAAC,CAAC,SAAAC,CAAUf,EAAEM,GAAG,MAAMQ,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CH,EAEpDsB,EAAmB,KACvB,MAAMC,EAAQC,EAAWvB,GACnBwB,EAAeD,EAAWrB,GAEhC,IAAKoB,EACH,MAAM,IAAIG,MACR,8DAIJ,IAAKD,IAAiBA,EAAalC,UACjC,MAAM,IAAImC,MACR,4DAIJ,MAAO,CAAEH,QAAOE,eAAc,EA4DhC,MAAO,CACLJ,qBA1D2B,KAC3B,MAAME,MAAEA,GAAUD,IAalB,OAZiBK,GACdV,IACC,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMW,EAAWX,EACXY,EAAeN,EAAM5C,WAC3B,OAAO4C,EAAM1C,YAAY+C,EAASC,IAEpC,OAAON,EAAM1C,YAAYoC,EAAM,GAEjC,CAACM,GAGY,EA6CfH,gBAzCAU,IAKA,MAAMP,MAAEA,EAAKE,aAAEA,GAAiBH,KACzBjD,EAAO0D,GAAiBC,GAAuB,IACpDT,EAAM3C,cAAckD,KA6BtB,OA1BAnB,GAAU,KAKR,IAAKc,EAAalC,UAAW,OAE7B,MAAM0C,EAAMR,EAAalC,UAAUuC,GANd,CAACI,EAASC,KAC7BJ,EAAcI,EAAS,IAOzB,MAAO,KACLF,EAAIrC,aAAa,CAClB,GACA,CAACkC,EAAKL,EAAalC,UAAWgC,IAc1B,CAAClD,EAZSsD,GACdV,IACC,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMW,EAAWX,EACXY,EAAeN,EAAM3C,cAAckD,GACzC,OAAOP,EAAMnC,SAAS0C,EAAKF,EAASC,IAEtC,OAAON,EAAMnC,SAAS0C,EAAKb,EAAM,GAEnC,CAACa,EAAKP,IAGgB,EAO5B,CDjFIa,CAA8BpC,GAEhC,MAAO,CACLgB,WACAI,kBACAC,uBAEJ","x_google_ignoreList":[0]}
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";var t,e=require("react"),s=require("react/jsx-runtime"),r={};var n=(t||(t=1,r.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const e={...this.state};this.state={...t},Object.keys(t).forEach((t=>{Object.is(e[t],this.state[t])||this.notifyListeners(t)}))}setState(t,e){Object.is(this.state[t],e)||(this.state={...this.state,[t]:e},this.notifyListeners(t))}notifyListeners(t){const e=this.listeners.get(t);if(e){const s=this.state[t];e.forEach((t=>t(s)))}}subscribe(t,e){const s=s=>{e(t,s)};return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(s),{unsubscribe:()=>{const e=this.listeners.get(t);e&&(e.delete(s),0===e.size&&this.listeners.delete(t))}}}}),r);function i(t){const{StoreContext:s}=t;return t=>{const r=(()=>{const t=e.useContext(s);if(!t)throw new Error("useContextQuery must be used within a ContextQueryProvider");return t})(),n=t??Object.keys(r.getState()),i=()=>n.reduce(((t,e)=>({...t,[e]:r.getStateByKey(e)})),{}),[o,a]=e.useState(i);e.useEffect((()=>{const t=(t,e)=>{a((s=>({...s,[t]:e})))},e=n.map((e=>r.subscribe(e,t)));return()=>{e.forEach((t=>t.unsubscribe()))}}),[n,r]);return[o,e.useCallback((t=>{const e=i(),s="function"==typeof t?t(e):t;Object.entries(s).forEach((([t,e])=>{r.setState(t,e)}))}),[n,r])]}}function o(t){const r={StoreContext:e.createContext(null)},n=((t,e)=>{const{StoreContext:r}=t;return function({children:t}){return s.jsx(r.Provider,{value:e,children:t})}})(r,t);return{Provider:n,contexts:r}}exports.createContextQuery=function(t){const e=new n.ContextQueryStore(t),{Provider:s,contexts:r}=o(e);return{Provider:s,useContextQuery:i(r),updateState:t=>{if("function"==typeof t){const s=t,r=e.getState();e.updateState(s(r))}else e.updateState(t)},setState:(t,s)=>{if("function"==typeof s){const r=s,n=e.getStateByKey(t);e.setState(t,r(n))}else e.setState(t,s)}}};
1
+ "use strict";var t=require("react"),e=require("react/jsx-runtime");var r,s={};var n=(r||(r=1,s.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const e={...this.state};return this.state={...t},Object.keys(t).forEach((t=>{Object.is(e[t],this.state[t])||this.notifyListeners(t)})),!0}setState(t,e){return!Object.is(this.state[t],e)&&(this.state={...this.state,[t]:e},this.notifyListeners(t),!0)}notifyListeners(t){const e=this.listeners.get(t);if(e){const r=this.state[t];e.forEach((t=>t(r)))}}subscribe(t,e){const r=r=>{e(t,r)};return this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(r),{unsubscribe:()=>{const e=this.listeners.get(t);e&&(e.delete(r),0===e.size&&this.listeners.delete(t))}}}}),s);function u(){const r={StoreContext:t.createContext(null),ContextQuerySubscriptionContext:t.createContext(null)},s=(r=>{const{StoreContext:s,ContextQuerySubscriptionContext:u}=r;return function({children:r,initialState:i}){const o=t.useRef(null),c=t.useRef(null);o.current||(o.current=new n.ContextQueryStore(i),c.current=i),t.useEffect((()=>{o.current&&!Object.is(c.current,i)&&(o.current.updateState(i),c.current=i)}),[i]);const a=t.useMemo((()=>({subscribe:o.current?o.current.subscribe.bind(o.current):null})),[o.current]);return e.jsx(s.Provider,{value:o.current,children:e.jsx(u.Provider,{value:a,children:r})})}})(r);return{Provider:s,contexts:r}}exports.createContextQuery=function(){const{Provider:e,contexts:r}=u(),{useContextQuery:s,useContextBatchQuery:n}=function(e){const{StoreContext:r,ContextQuerySubscriptionContext:s}=e,n=()=>{const e=t.useContext(r),n=t.useContext(s);if(!e)throw new Error("useContextQuery must be used within a ContextQueryProvider");if(!n||!n.subscribe)throw new Error("ContextQuerySubscriptionContext not properly initialized");return{store:e,subscription:n}};return{useContextBatchQuery:()=>{const{store:e}=n();return t.useCallback((t=>{if("function"==typeof t){const r=t,s=e.getState();return e.updateState(r(s))}return e.updateState(t)}),[e])},useContextQuery:e=>{const{store:r,subscription:s}=n(),[u,i]=t.useState((()=>r.getStateByKey(e)));return t.useEffect((()=>{if(!s.subscribe)return;const t=s.subscribe(e,((t,e)=>{i(e)}));return()=>{t.unsubscribe()}}),[e,s.subscribe,r]),[u,t.useCallback((t=>{if("function"==typeof t){const s=t,n=r.getStateByKey(e);return r.setState(e,s(n))}return r.setState(e,t)}),[e,r])]}}}(r);return{Provider:e,useContextQuery:s,useContextBatchQuery:n}};
2
2
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../core/dist/index.js","../src/hooks.ts","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["\"use strict\";exports.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const s={...this.state};this.state={...t};Object.keys(t).forEach((t=>{Object.is(s[t],this.state[t])||this.notifyListeners(t)}))}setState(t,s){Object.is(this.state[t],s)||(this.state={...this.state,[t]:s},this.notifyListeners(t))}notifyListeners(t){const s=this.listeners.get(t);if(s){const e=this.state[t];s.forEach((t=>t(e)))}}subscribe(t,s){const e=e=>{s(t,e)};this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(e);return{unsubscribe:()=>{const s=this.listeners.get(t);s&&(s.delete(e),0===s.size&&this.listeners.delete(t))}}}};\n//# sourceMappingURL=index.js.map\n","import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n) {\n const { StoreContext } = contexts;\n\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextQuery must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TState>(keys?: TKey[]) => {\n const store = useStore();\n const localKeys = keys ?? (Object.keys(store.getState()) as TKey[]);\n\n type StateSubset = { [K in TKey]: TState[K] };\n\n const getStateSubset = (): StateSubset => {\n return localKeys.reduce(\n (acc, key) => ({ ...acc, [key]: store.getStateByKey(key) }),\n {} as StateSubset\n );\n };\n\n const [state, setLocalState] = useState<StateSubset>(getStateSubset);\n\n useEffect(() => {\n const handleChange = (key: TKey, newValue: TState[TKey]) => {\n setLocalState((prev) => ({ ...prev, [key]: newValue }));\n };\n\n const subscriptions = localKeys.map((key) =>\n store.subscribe(key, handleChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n }, [localKeys, store]);\n\n const setState = useCallback(\n (value: StateSubset | ((prev: StateSubset) => StateSubset)) => {\n const currentValue = getStateSubset();\n const updatedValue =\n typeof value === \"function\"\n ? (value as Function)(currentValue)\n : value;\n\n Object.entries(updatedValue).forEach(([k, v]) => {\n store.setState(k as TKey, v as TState[TKey]);\n });\n },\n [localKeys, store]\n );\n\n return [state, setState] as const;\n };\n}\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { FC, PropsWithChildren } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport type ProviderProps<TState extends TStateImpl> = PropsWithChildren;\n\nexport const createContextQueryProvider = <TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>,\n store: ContextQueryStore<TState>\n): FC<ProviderProps<TState>> => {\n const { StoreContext } = contexts;\n\n return function ContextQueryProvider({ children }: ProviderProps<TState>) {\n return (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n );\n };\n};\n\nexport function createReactContextQuery<TState extends TStateImpl>(\n store: ContextQueryStore<TState>\n) {\n const contexts = createContextQuery<TState>();\n const ContextQueryProvider = createContextQueryProvider<TState>(\n contexts,\n store\n );\n\n return {\n Provider: ContextQueryProvider,\n contexts,\n };\n}\n","import type { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createContextQuery = <TState extends TStateImpl>() => {\n const StoreContext = createContext<ContextQueryStore<TState> | null>(null);\n return {\n StoreContext,\n };\n};\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createUseContextQuery } from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TState extends TStateImpl>(\n initialState: TState\n) {\n const store = new ContextQueryStore<TState>(initialState);\n\n const { Provider, contexts } = createReactContextQuery<TState>(store);\n const useContextQuery = createUseContextQuery<TState>(contexts);\n\n const updateState = (state: TState | ((prev: TState) => TState)) => {\n if (typeof state === \"function\") {\n const updateFn = state as (prev: TState) => TState;\n const currentValue = store.getState();\n store.updateState(updateFn(currentValue));\n } else {\n store.updateState(state);\n }\n };\n\n const setState = <TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])\n ) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState[TKey]) => TState[TKey];\n const currentValue = store.getStateByKey(key);\n store.setState(key, updateFn(currentValue));\n } else {\n store.setState(key, value);\n }\n };\n\n return {\n Provider,\n useContextQuery,\n updateState,\n setState,\n };\n}\n"],"names":["dist","ContextQueryStore","state","listeners","constructor","t","this","Map","getState","getStateByKey","updateState","s","Object","keys","forEach","is","notifyListeners","setState","get","e","subscribe","has","set","Set","add","unsubscribe","delete","size","createUseContextQuery","contexts","StoreContext","store","useContext","Error","useStore","localKeys","getStateSubset","reduce","acc","key","setLocalState","useState","useEffect","handleChange","newValue","prev","subscriptions","map","sub","useCallback","value","currentValue","updatedValue","entries","k","v","createReactContextQuery","createContext","ContextQueryProvider","children","_jsx","jsx","Provider","createContextQueryProvider","initialState","useContextQuery","updateFn"],"mappings":"yFAAaA,EAAAC,kBAA0B,MAAMC,MAAMC,UAAU,WAAAC,CAAYC,GAAGC,KAAKJ,MAAMG,EAAEC,KAAKH,UAAU,IAAII,GAAG,CAAC,QAAAC,GAAW,OAAOF,KAAKJ,KAAK,CAAC,aAAAO,CAAcJ,GAAG,OAAOC,KAAKJ,MAAMG,EAAE,CAAC,WAAAK,CAAYL,GAAG,MAAMM,EAAE,IAAIL,KAAKJ,OAAOI,KAAKJ,MAAM,IAAIG,GAAGO,OAAOC,KAAKR,GAAGS,SAAST,IAAI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":"index.js","sources":["../../../node_modules/.pnpm/@context-query+core@0.1.0-dev.1/node_modules/@context-query/core/dist/index.js","../src/provider.tsx","../src/context.ts","../src/index.ts","../src/hooks.ts"],"sourcesContent":["\"use strict\";exports.ContextQueryStore=class{state;listeners;constructor(t){this.state=t,this.listeners=new Map}getState(){return this.state}getStateByKey(t){return this.state[t]}updateState(t){const s={...this.state};this.state={...t};return Object.keys(t).forEach((t=>{Object.is(s[t],this.state[t])||this.notifyListeners(t)})),!0}setState(t,s){return!Object.is(this.state[t],s)&&(this.state={...this.state,[t]:s},this.notifyListeners(t),!0)}notifyListeners(t){const s=this.listeners.get(t);if(s){const e=this.state[t];s.forEach((t=>t(e)))}}subscribe(t,s){const e=e=>{s(t,e)};this.listeners.has(t)||this.listeners.set(t,new Set),this.listeners.get(t).add(e);return{unsubscribe:()=>{const s=this.listeners.get(t);s&&(s.delete(e),0===s.size&&this.listeners.delete(t))}}}};\n//# sourceMappingURL=index.js.map\n","import { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { FC, PropsWithChildren, useEffect, useMemo, useRef } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport interface ProviderProps<TState extends TStateImpl>\n extends PropsWithChildren {\n initialState: TState;\n}\n\nexport const createContextQueryProvider = <TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n): FC<ProviderProps<TState>> => {\n const { StoreContext, ContextQuerySubscriptionContext } = contexts;\n\n return function ContextQueryProvider({\n children,\n initialState,\n }: ProviderProps<TState>) {\n const storeRef = useRef<ContextQueryStore<TState> | null>(null);\n const previousStateRef = useRef<TState | null>(null);\n\n if (!storeRef.current) {\n storeRef.current = new ContextQueryStore<TState>(initialState);\n previousStateRef.current = initialState;\n }\n\n useEffect(() => {\n if (\n storeRef.current &&\n !Object.is(previousStateRef.current, initialState)\n ) {\n storeRef.current.updateState(initialState);\n previousStateRef.current = initialState;\n }\n }, [initialState]);\n\n const contextQuerySubscriptionContextValue = useMemo(\n () => ({\n subscribe: storeRef.current\n ? storeRef.current.subscribe.bind(storeRef.current)\n : null,\n }),\n [storeRef.current]\n );\n\n return (\n <StoreContext.Provider value={storeRef.current}>\n <ContextQuerySubscriptionContext.Provider\n value={contextQuerySubscriptionContextValue}\n >\n {children}\n </ContextQuerySubscriptionContext.Provider>\n </StoreContext.Provider>\n );\n };\n};\n\nexport function createReactContextQuery<TState extends TStateImpl>() {\n const contexts = createContextQuery<TState>();\n const ContextQueryProvider = createContextQueryProvider<TState>(contexts);\n\n return {\n Provider: ContextQueryProvider,\n contexts,\n };\n}\n","import type { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createContextQuery = <TState extends TStateImpl>() => {\n const StoreContext = createContext<ContextQueryStore<TState> | null>(null);\n const ContextQuerySubscriptionContext = createContext<{\n subscribe: ContextQueryStore<TState>[\"subscribe\"] | null;\n } | null>(null);\n\n return {\n StoreContext,\n ContextQuerySubscriptionContext,\n };\n};\n","import { TStateImpl } from \"@context-query/core\";\nimport { createUseContextQuery } from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TState extends TStateImpl>() {\n const { Provider, contexts } = createReactContextQuery<TState>();\n\n const { useContextQuery, useContextBatchQuery } =\n createUseContextQuery<TState>(contexts);\n\n return {\n Provider,\n useContextQuery,\n useContextBatchQuery,\n };\n}\n","import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createContextQuery } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createContextQuery<TState>>\n) {\n const { StoreContext, ContextQuerySubscriptionContext } = contexts;\n\n const useLocalContexts = () => {\n const store = useContext(StoreContext);\n const subscription = useContext(ContextQuerySubscriptionContext);\n\n if (!store) {\n throw new Error(\n \"useContextQuery must be used within a ContextQueryProvider\"\n );\n }\n\n if (!subscription || !subscription.subscribe) {\n throw new Error(\n \"ContextQuerySubscriptionContext not properly initialized\"\n );\n }\n\n return { store, subscription };\n };\n\n const useContextBatchQuery = () => {\n const { store } = useLocalContexts();\n const setState = useCallback(\n (value: TState | ((prev: TState) => TState)) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState) => TState;\n const currentValue = store.getState();\n return store.updateState(updateFn(currentValue));\n }\n return store.updateState(value);\n },\n [store]\n );\n\n return setState;\n };\n\n const useContextQuery = <TKey extends keyof TState>(\n key: TKey\n ): [\n TState[TKey],\n (value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => boolean,\n ] => {\n const { store, subscription } = useLocalContexts();\n const [state, setLocalState] = useState<TState[TKey]>(() =>\n store.getStateByKey(key)\n );\n\n useEffect(() => {\n const handleChange = (_: TKey, newValue: TState[TKey]) => {\n setLocalState(newValue);\n };\n\n if (!subscription.subscribe) return;\n\n const sub = subscription.subscribe(key, handleChange);\n\n return () => {\n sub.unsubscribe();\n };\n }, [key, subscription.subscribe, store]);\n\n const setState = useCallback(\n (value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => {\n if (typeof value === \"function\") {\n const updateFn = value as (prev: TState[TKey]) => TState[TKey];\n const currentValue = store.getStateByKey(key);\n return store.setState(key, updateFn(currentValue));\n }\n return store.setState(key, value);\n },\n [key, store]\n );\n\n return [state, setState];\n };\n\n return {\n useContextBatchQuery,\n useContextQuery,\n };\n}\n"],"names":["dist","ContextQueryStore","state","listeners","constructor","t","this","Map","getState","getStateByKey","updateState","s","Object","keys","forEach","is","notifyListeners","setState","get","e","subscribe","has","set","Set","add","unsubscribe","delete","size","createReactContextQuery","contexts","StoreContext","createContext","ContextQuerySubscriptionContext","ContextQueryProvider","children","initialState","storeRef","useRef","previousStateRef","current","useEffect","contextQuerySubscriptionContextValue","useMemo","bind","_jsx","Provider","value","createContextQueryProvider","useContextQuery","useContextBatchQuery","useLocalContexts","store","useContext","subscription","Error","useCallback","updateFn","currentValue","key","setLocalState","useState","sub","_","newValue","createUseContextQuery"],"mappings":"6FAAaA,EA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AMzC,YAAY8C,EAASC,IAEpC,OAAON,EAAMzC,YAAYoC,EAAM,GAEjC,CAACK,GAGY,EA6CfH,gBAzCAU,IAKA,MAAMP,MAAEA,EAAKE,aAAEA,GAAiBH,KACzBhD,EAAOyD,GAAiBC,EAAAA,UAAuB,IACpDT,EAAM1C,cAAciD,KA6BtB,OA1BAlB,EAAAA,WAAU,KAKR,IAAKa,EAAajC,UAAW,OAE7B,MAAMyC,EAAMR,EAAajC,UAAUsC,GANd,CAACI,EAASC,KAC7BJ,EAAcI,EAAS,IAOzB,MAAO,KACLF,EAAIpC,aAAa,CAClB,GACA,CAACiC,EAAKL,EAAajC,UAAW+B,IAc1B,CAACjD,EAZSqD,eACdT,IACC,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMU,EAAWV,EACXW,EAAeN,EAAM1C,cAAciD,GACzC,OAAOP,EAAMlC,SAASyC,EAAKF,EAASC,IAEtC,OAAON,EAAMlC,SAASyC,EAAKZ,EAAM,GAEnC,CAACY,EAAKP,IAGgB,EAO5B,CDjFIa,CAA8BnC,GAEhC,MAAO,CACLgB,WACAG,kBACAC,uBAEJ","x_google_ignoreList":[0]}
@@ -1,13 +1,16 @@
1
1
  import { ContextQueryStore, TStateImpl } from "@context-query/core";
2
2
  import { FC, PropsWithChildren } from "react";
3
3
  import { createContextQuery } from "./context";
4
- export type ProviderProps<TState extends TStateImpl> = PropsWithChildren;
5
- export declare const createContextQueryProvider: <TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>, store: ContextQueryStore<TState>) => FC<ProviderProps<TState>>;
6
- export declare function createReactContextQuery<TState extends TStateImpl>(store: ContextQueryStore<TState>): {
7
- Provider: FC<{
8
- children?: import("react").ReactNode | undefined;
9
- }>;
4
+ export interface ProviderProps<TState extends TStateImpl> extends PropsWithChildren {
5
+ initialState: TState;
6
+ }
7
+ export declare const createContextQueryProvider: <TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>) => FC<ProviderProps<TState>>;
8
+ export declare function createReactContextQuery<TState extends TStateImpl>(): {
9
+ Provider: FC<ProviderProps<TState>>;
10
10
  contexts: {
11
11
  StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
12
+ ContextQuerySubscriptionContext: import("react").Context<{
13
+ subscribe: (<TKey extends keyof TState>(key: TKey, listener: (key: TKey, value: TState[TKey]) => void) => import("@context-query/core").Subscription) | null;
14
+ } | null>;
12
15
  };
13
16
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@context-query/react",
3
- "version": "0.2.0-dev.1",
3
+ "version": "0.2.0-dev.2",
4
4
  "license": "MIT",
5
5
  "description": "React bindings for ContextQuery",
6
6
  "author": "Minyeoung Seo <tjalsdud89@naver.com>",
@@ -30,7 +30,7 @@
30
30
  "react": "^18.0.0 || ^19.0.0"
31
31
  },
32
32
  "dependencies": {
33
- "@context-query/core": "0.2.0-dev.1",
33
+ "@context-query/core": "0.2.0-dev.2",
34
34
  "react": "^18.3.1"
35
35
  },
36
36
  "devDependencies": {