@context-query/react 0.1.0-dev.1
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 +7 -0
- package/dist/hooks.d.ts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.esm.js +2 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/provider.d.ts +16 -0
- package/package.json +48 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ContextQueryStore, TStateImpl } from "@context-query/core";
|
|
2
|
+
export declare const createContextQuery: <TState extends TStateImpl>() => {
|
|
3
|
+
StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
|
|
4
|
+
ContextQuerySubscriptionContext: import("react").Context<{
|
|
5
|
+
subscribe: ContextQueryStore<TState>["subscribe"] | null;
|
|
6
|
+
} | null>;
|
|
7
|
+
};
|
package/dist/hooks.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TStateImpl } from "@context-query/core";
|
|
2
|
+
import { createContextQuery } from "./context";
|
|
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
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TStateImpl } from "@context-query/core";
|
|
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;
|
|
6
|
+
};
|
|
@@ -0,0 +1,2 @@
|
|
|
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
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../../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,EAAEA,IAAIR,EAAEN,EAAEc,EAAE,EA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QC,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"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
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
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../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,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,EAAEA,IAAIR,EAAEN,EAAEc,EAAE,EAAoF,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,MAAM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qBA1D2B,KAC3B,MAAME,MAAEA,GAAUD,IAalB,OAZiBK,eACdT,IACC,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMU,EAAWV,EACXW,EAAeN,EAAM3C,WAC3B,OAAO2C,EAAMzC,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"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ContextQueryStore, TStateImpl } from "@context-query/core";
|
|
2
|
+
import { FC, PropsWithChildren } from "react";
|
|
3
|
+
import { createContextQuery } from "./context";
|
|
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
|
+
contexts: {
|
|
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>;
|
|
15
|
+
};
|
|
16
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@context-query/react",
|
|
3
|
+
"version": "0.1.0-dev.1",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"description": "React bindings for ContextQuery",
|
|
6
|
+
"author": "Minyeoung Seo <tjalsdud89@naver.com>",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/load28/context-query.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"react",
|
|
14
|
+
"context",
|
|
15
|
+
"query",
|
|
16
|
+
"state",
|
|
17
|
+
"management"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "rollup -c",
|
|
21
|
+
"dev": "rollup -c -w"
|
|
22
|
+
},
|
|
23
|
+
"main": "dist/index.js",
|
|
24
|
+
"module": "dist/index.esm.js",
|
|
25
|
+
"types": "dist/index.d.ts",
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"@context-query/core": "0.1.0-dev.1",
|
|
34
|
+
"react": "^18.3.1"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@types/react": "^19.0.10"
|
|
38
|
+
},
|
|
39
|
+
"packageManager": "pnpm@9.0.0",
|
|
40
|
+
"exports": {
|
|
41
|
+
".": {
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"default": "./dist/index.js",
|
|
44
|
+
"import": "./dist/index.js",
|
|
45
|
+
"require": "./dist/index.cjs"
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|