@context-query/react 0.3.1 → 0.3.2-beta.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 +2 -2
- package/dist/hooks.d.ts +3 -3
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/provider.d.ts +10 -4
- package/package.json +2 -2
package/dist/context.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { ContextQueryStore
|
|
2
|
-
export declare const createStoreContext: <
|
|
1
|
+
import type { ContextQueryStore } from "@context-query/core";
|
|
2
|
+
export declare const createStoreContext: <TAtoms extends Record<string, any>>() => import("react").Context<ContextQueryStore<TAtoms> | null>;
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TStateImpl } from "@context-query/core";
|
|
2
1
|
import { createStoreContext } from "./context";
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
2
|
+
export declare function createUseContextAtom<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): <TKey extends keyof TAtoms>(key: TKey) => readonly [TAtoms[TKey], (newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => void];
|
|
3
|
+
export declare function createUseContextAtomValue<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): <TKey extends keyof TAtoms>(key: TKey) => TAtoms[TKey];
|
|
4
|
+
export declare function createUseContextSetAtom<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): <TKey extends keyof TAtoms>(key: TKey) => ((value: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => void);
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var t=require("react"),e=require("react/jsx-runtime");function
|
|
1
|
+
"use strict";var t=require("react"),e=require("react/jsx-runtime");function r(e){return r=>{const o=(()=>{const r=t.useContext(e);if(!r)throw new Error("useContextAtom must be used within a ContextQueryProvider");return r})(),[s,n]=t.useState((()=>o.getAtomValue(r)));t.useEffect((()=>{const t=o.subscribeToAtom(r,((t,e)=>{n(e)}));return()=>{t.unsubscribe()}}),[r,o]);return[s,t.useCallback((t=>{const e=o.getAtomValue(r),s="function"==typeof t?t(e):t;o.setAtomValue(r,s)}),[r,o])]}}function o(e){return r=>{const o=(()=>{const r=t.useContext(e);if(!r)throw new Error("useContextAtomValue must be used within a ContextQueryProvider");return r})(),[s,n]=t.useState((()=>o.getAtomValue(r)));return t.useEffect((()=>{const t=o.subscribeToAtom(r,((t,e)=>{n(e)}));return()=>{t.unsubscribe()}}),[r,o]),s}}function s(e){return r=>{const o=(()=>{const r=t.useContext(e);if(!r)throw new Error("useContextSetAtom must be used within a ContextQueryProvider");return r})();return t.useCallback((t=>{const e=o.getAtomValue(r),s="function"==typeof t?t(e):t;o.setAtomValue(r,s)}),[r,o])}}class n{value;listeners;constructor(t){this.value=t,this.listeners=new Set}getValue(){return this.value}setValue(t){Object.is(this.value,t)||(this.value=t,this.notifyListeners())}notifyListeners(){this.listeners.forEach((t=>t(this.value)))}subscribe(t){return this.listeners.add(t),{unsubscribe:()=>{this.listeners.delete(t)}}}}class u{atoms;constructor(t){this.atoms=new Map,Object.entries(t).forEach((([t,e])=>{this.atoms.set(t,new n(e))}))}getAtomValue(t){const e=this.atoms.get(t);if(!e)throw new Error(`Atom with key "${String(t)}" not found`);return e.getValue()}setAtomValue(t,e){const r=this.atoms.get(t);if(!r)throw new Error(`Atom with key "${String(t)}" not found`);r.setValue(e)}subscribeToAtom(t,e){const r=this.atoms.get(t);if(!r)throw new Error(`Atom with key "${String(t)}" not found`);return r.subscribe((r=>{e(t,r)}))}getAllAtomValues(){const t={};return this.atoms.forEach(((e,r)=>{t[r]=e.getValue()})),t}updateAllAtoms(t){Object.entries(t).forEach((([t,e])=>{const r=this.atoms.get(t);r&&r.setValue(e)}))}}function i(){const r=t.createContext(null);return{ContextQueryProvider:function({children:o,atoms:s}){const n=t.useMemo((()=>new u(s)),[s]);return e.jsx(r.Provider,{value:n,children:o})},StoreContext:r}}exports.createContextQuery=function(){const{ContextQueryProvider:t,StoreContext:e}=i();return{ContextQueryProvider:t,useContextAtom:r(e),useContextAtomValue:o(e),useContextSetAtom:s(e)}};
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../src/hooks.ts","../../../node_modules/.pnpm/@context-query+core@0.3.
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/hooks.ts","../../../node_modules/.pnpm/@context-query+core@0.3.2-beta.1/node_modules/@context-query/core/dist/index.mjs","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["import { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nexport function createUseContextAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextAtom must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TAtoms>(key: TKey) => {\n const store = useStore();\n const [value, setValue] = useState<TAtoms[TKey]>(() => store.getAtomValue(key));\n\n useEffect(() => {\n const handleChange = (_: TKey, newValue: TAtoms[TKey]) => {\n setValue(newValue);\n };\n\n const subscription = store.subscribeToAtom(key, handleChange);\n\n return () => {\n subscription.unsubscribe();\n };\n }, [key, store]);\n\n const setAtom = useCallback((newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue = typeof newValue === \"function\" \n ? (newValue as Function)(currentValue) \n : newValue;\n \n store.setAtomValue(key, updatedValue);\n }, [key, store]);\n\n return [value, setAtom] as const;\n };\n}\n\nexport function createUseContextAtomValue<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextAtomValue must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TAtoms>(key: TKey): TAtoms[TKey] => {\n const store = useStore();\n const [value, setValue] = useState<TAtoms[TKey]>(() => store.getAtomValue(key));\n\n useEffect(() => {\n const handleChange = (_: TKey, newValue: TAtoms[TKey]) => {\n setValue(newValue);\n };\n\n const subscription = store.subscribeToAtom(key, handleChange);\n\n return () => {\n subscription.unsubscribe();\n };\n }, [key, store]);\n\n return value;\n };\n}\n\nexport function createUseContextSetAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextSetAtom must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TAtoms>(\n key: TKey\n ): ((value: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => void) => {\n const store = useStore();\n\n return useCallback((newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue = typeof newValue === \"function\" \n ? (newValue as Function)(currentValue) \n : newValue;\n \n store.setAtomValue(key, updatedValue);\n }, [key, store]);\n };\n}\n","class t{value;listeners;constructor(t){this.value=t,this.listeners=new Set}getValue(){return this.value}setValue(t){Object.is(this.value,t)||(this.value=t,this.notifyListeners())}notifyListeners(){this.listeners.forEach((t=>t(this.value)))}subscribe(t){this.listeners.add(t);return{unsubscribe:()=>{this.listeners.delete(t)}}}}class e{atoms;constructor(e){this.atoms=new Map,Object.entries(e).forEach((([e,s])=>{this.atoms.set(e,new t(s))}))}getAtomValue(t){const e=this.atoms.get(t);if(!e)throw new Error(`Atom with key \"${String(t)}\" not found`);return e.getValue()}setAtomValue(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key \"${String(t)}\" not found`);s.setValue(e)}subscribeToAtom(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key \"${String(t)}\" not found`);return s.subscribe((s=>{e(t,s)}))}getAllAtomValues(){const t={};return this.atoms.forEach(((e,s)=>{t[s]=e.getValue()})),t}updateAllAtoms(t){Object.entries(t).forEach((([t,e])=>{const s=this.atoms.get(t);s&&s.setValue(e)}))}}export{t as AtomStore,e as ContextQueryStore};\n//# sourceMappingURL=index.mjs.map\n","import { ContextQueryStore } from \"@context-query/core\";\nimport { PropsWithChildren, useMemo } from \"react\";\nimport { createStoreContext } from \"./context\";\n\ntype AtomValues<T extends Record<string, any>> = {\n [K in keyof T]: T[K];\n};\n\nexport function createReactContextQuery<TAtoms extends Record<string, any>>() {\n const StoreContext = createStoreContext<TAtoms>();\n \n const ContextQueryProvider = function ContextQueryProvider({\n children,\n atoms,\n }: PropsWithChildren<{ atoms: AtomValues<TAtoms> }>) {\n const store = useMemo(\n () => new ContextQueryStore<TAtoms>(atoms),\n [atoms]\n );\n\n return (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n );\n };\n\n return {\n ContextQueryProvider,\n StoreContext,\n };\n}\n","import type { ContextQueryStore } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createStoreContext = <TAtoms extends Record<string, any>>() => {\n return createContext<ContextQueryStore<TAtoms> | null>(null);\n};\n","import { \n createUseContextAtom, \n createUseContextAtomValue, \n createUseContextSetAtom \n} from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TAtoms extends Record<string, any>>() {\n const { ContextQueryProvider, StoreContext } =\n createReactContextQuery<TAtoms>();\n \n const useContextAtom = createUseContextAtom<TAtoms>(StoreContext);\n const useContextAtomValue = createUseContextAtomValue<TAtoms>(StoreContext);\n const useContextSetAtom = createUseContextSetAtom<TAtoms>(StoreContext);\n \n return {\n ContextQueryProvider,\n useContextAtom,\n useContextAtomValue,\n useContextSetAtom,\n };\n}\n"],"names":["createUseContextAtom","StoreContext","key","store","useContext","Error","useStore","value","setValue","useState","getAtomValue","useEffect","subscription","subscribeToAtom","_","newValue","unsubscribe","useCallback","currentValue","updatedValue","setAtomValue","createUseContextAtomValue","createUseContextSetAtom","t","listeners","constructor","this","Set","getValue","Object","is","notifyListeners","forEach","subscribe","add","delete","e","atoms","Map","entries","s","set","get","String","getAllAtomValues","updateAllAtoms","createReactContextQuery","createContext","ContextQueryProvider","children","useMemo","ContextQueryStore","_jsx","jsx","Provider","useContextAtom","useContextAtomValue","useContextSetAtom"],"mappings":"mEAGM,SAAUA,EACdC,GAcA,OAAmCC,IACjC,MAAMC,EAbS,MACf,MAAMA,EAAQC,EAAUA,WAACH,GAEzB,IAAKE,EACH,MAAM,IAAIE,MACR,6DAIJ,OAAOF,CAAK,EAIEG,IACPC,EAAOC,GAAYC,EAAAA,UAAuB,IAAMN,EAAMO,aAAaR,KAE1ES,EAAAA,WAAU,KACR,MAIMC,EAAeT,EAAMU,gBAAgBX,GAJtB,CAACY,EAASC,KAC7BP,EAASO,EAAS,IAKpB,MAAO,KACLH,EAAaI,aAAa,CAC3B,GACA,CAACd,EAAKC,IAWT,MAAO,CAACI,EATQU,eAAaF,IAC3B,MAAMG,EAAef,EAAMO,aAAaR,GAClCiB,EAAmC,mBAAbJ,EACvBA,EAAsBG,GACvBH,EAEJZ,EAAMiB,aAAalB,EAAKiB,EAAa,GACpC,CAACjB,EAAKC,IAEuB,CAEpC,CAEM,SAAUkB,EACdpB,GAcA,OAAmCC,IACjC,MAAMC,EAbS,MACf,MAAMA,EAAQC,EAAUA,WAACH,GAEzB,IAAKE,EACH,MAAM,IAAIE,MACR,kEAIJ,OAAOF,CAAK,EAIEG,IACPC,EAAOC,GAAYC,EAAAA,U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1,6 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
export declare function createContextQuery<TAtoms extends Record<string, any>>(): {
|
|
2
|
+
ContextQueryProvider: ({ children, atoms, }: import("react").PropsWithChildren<{
|
|
3
|
+
atoms: { [K in keyof TAtoms]: TAtoms[K]; };
|
|
4
|
+
}>) => import("react/jsx-runtime").JSX.Element;
|
|
5
|
+
useContextAtom: <TKey extends keyof TAtoms>(key: TKey) => readonly [TAtoms[TKey], (newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => void];
|
|
6
|
+
useContextAtomValue: <TKey extends keyof TAtoms>(key: TKey) => TAtoms[TKey];
|
|
7
|
+
useContextSetAtom: <TKey extends keyof TAtoms>(key: TKey) => (value: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => void;
|
|
6
8
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{useCallback as t,useState as e,useEffect as r,useContext as o,createContext as n,useMemo as s}from"react";import{jsx as u}from"react/jsx-runtime";function i(n){return s=>{const u=(()=>{const t=o(n);if(!t)throw new Error("useContextAtom must be used within a ContextQueryProvider");return t})(),[i,c]=e((()=>u.getAtomValue(s)));r((()=>{const t=u.subscribeToAtom(s,((t,e)=>{c(e)}));return()=>{t.unsubscribe()}}),[s,u]);return[i,t((t=>{const e=u.getAtomValue(s),r="function"==typeof t?t(e):t;u.setAtomValue(s,r)}),[s,u])]}}function c(t){return n=>{const s=(()=>{const e=o(t);if(!e)throw new Error("useContextAtomValue must be used within a ContextQueryProvider");return e})(),[u,i]=e((()=>s.getAtomValue(n)));return r((()=>{const t=s.subscribeToAtom(n,((t,e)=>{i(e)}));return()=>{t.unsubscribe()}}),[n,s]),u}}function a(e){return r=>{const n=(()=>{const t=o(e);if(!t)throw new Error("useContextSetAtom must be used within a ContextQueryProvider");return t})();return t((t=>{const e=n.getAtomValue(r),o="function"==typeof t?t(e):t;n.setAtomValue(r,o)}),[r,n])}}class l{value;listeners;constructor(t){this.value=t,this.listeners=new Set}getValue(){return this.value}setValue(t){Object.is(this.value,t)||(this.value=t,this.notifyListeners())}notifyListeners(){this.listeners.forEach((t=>t(this.value)))}subscribe(t){return this.listeners.add(t),{unsubscribe:()=>{this.listeners.delete(t)}}}}class m{atoms;constructor(t){this.atoms=new Map,Object.entries(t).forEach((([t,e])=>{this.atoms.set(t,new l(e))}))}getAtomValue(t){const e=this.atoms.get(t);if(!e)throw new Error(`Atom with key "${String(t)}" not found`);return e.getValue()}setAtomValue(t,e){const r=this.atoms.get(t);if(!r)throw new Error(`Atom with key "${String(t)}" not found`);r.setValue(e)}subscribeToAtom(t,e){const r=this.atoms.get(t);if(!r)throw new Error(`Atom with key "${String(t)}" not found`);return r.subscribe((r=>{e(t,r)}))}getAllAtomValues(){const t={};return this.atoms.forEach(((e,r)=>{t[r]=e.getValue()})),t}updateAllAtoms(t){Object.entries(t).forEach((([t,e])=>{const r=this.atoms.get(t);r&&r.setValue(e)}))}}function h(){const t=n(null);return{ContextQueryProvider:function({children:e,atoms:r}){const o=s((()=>new m(r)),[r]);return u(t.Provider,{value:o,children:e})},StoreContext:t}}function f(){const{ContextQueryProvider:t,StoreContext:e}=h();return{ContextQueryProvider:t,useContextAtom:i(e),useContextAtomValue:c(e),useContextSetAtom:a(e)}}export{f as createContextQuery};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../src/hooks.ts","../../../node_modules/.pnpm/@context-query+core@0.3.
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/hooks.ts","../../../node_modules/.pnpm/@context-query+core@0.3.2-beta.1/node_modules/@context-query/core/dist/index.mjs","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["import { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nexport function createUseContextAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextAtom must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TAtoms>(key: TKey) => {\n const store = useStore();\n const [value, setValue] = useState<TAtoms[TKey]>(() => store.getAtomValue(key));\n\n useEffect(() => {\n const handleChange = (_: TKey, newValue: TAtoms[TKey]) => {\n setValue(newValue);\n };\n\n const subscription = store.subscribeToAtom(key, handleChange);\n\n return () => {\n subscription.unsubscribe();\n };\n }, [key, store]);\n\n const setAtom = useCallback((newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue = typeof newValue === \"function\" \n ? (newValue as Function)(currentValue) \n : newValue;\n \n store.setAtomValue(key, updatedValue);\n }, [key, store]);\n\n return [value, setAtom] as const;\n };\n}\n\nexport function createUseContextAtomValue<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextAtomValue must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TAtoms>(key: TKey): TAtoms[TKey] => {\n const store = useStore();\n const [value, setValue] = useState<TAtoms[TKey]>(() => store.getAtomValue(key));\n\n useEffect(() => {\n const handleChange = (_: TKey, newValue: TAtoms[TKey]) => {\n setValue(newValue);\n };\n\n const subscription = store.subscribeToAtom(key, handleChange);\n\n return () => {\n subscription.unsubscribe();\n };\n }, [key, store]);\n\n return value;\n };\n}\n\nexport function createUseContextSetAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(\n \"useContextSetAtom must be used within a ContextQueryProvider\"\n );\n }\n\n return store;\n };\n\n return <TKey extends keyof TAtoms>(\n key: TKey\n ): ((value: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => void) => {\n const store = useStore();\n\n return useCallback((newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue = typeof newValue === \"function\" \n ? (newValue as Function)(currentValue) \n : newValue;\n \n store.setAtomValue(key, updatedValue);\n }, [key, store]);\n };\n}\n","class t{value;listeners;constructor(t){this.value=t,this.listeners=new Set}getValue(){return this.value}setValue(t){Object.is(this.value,t)||(this.value=t,this.notifyListeners())}notifyListeners(){this.listeners.forEach((t=>t(this.value)))}subscribe(t){this.listeners.add(t);return{unsubscribe:()=>{this.listeners.delete(t)}}}}class e{atoms;constructor(e){this.atoms=new Map,Object.entries(e).forEach((([e,s])=>{this.atoms.set(e,new t(s))}))}getAtomValue(t){const e=this.atoms.get(t);if(!e)throw new Error(`Atom with key \"${String(t)}\" not found`);return e.getValue()}setAtomValue(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key \"${String(t)}\" not found`);s.setValue(e)}subscribeToAtom(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key \"${String(t)}\" not found`);return s.subscribe((s=>{e(t,s)}))}getAllAtomValues(){const t={};return this.atoms.forEach(((e,s)=>{t[s]=e.getValue()})),t}updateAllAtoms(t){Object.entries(t).forEach((([t,e])=>{const s=this.atoms.get(t);s&&s.setValue(e)}))}}export{t as AtomStore,e as ContextQueryStore};\n//# sourceMappingURL=index.mjs.map\n","import { ContextQueryStore } from \"@context-query/core\";\nimport { PropsWithChildren, useMemo } from \"react\";\nimport { createStoreContext } from \"./context\";\n\ntype AtomValues<T extends Record<string, any>> = {\n [K in keyof T]: T[K];\n};\n\nexport function createReactContextQuery<TAtoms extends Record<string, any>>() {\n const StoreContext = createStoreContext<TAtoms>();\n \n const ContextQueryProvider = function ContextQueryProvider({\n children,\n atoms,\n }: PropsWithChildren<{ atoms: AtomValues<TAtoms> }>) {\n const store = useMemo(\n () => new ContextQueryStore<TAtoms>(atoms),\n [atoms]\n );\n\n return (\n <StoreContext.Provider value={store}>{children}</StoreContext.Provider>\n );\n };\n\n return {\n ContextQueryProvider,\n StoreContext,\n };\n}\n","import type { ContextQueryStore } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createStoreContext = <TAtoms extends Record<string, any>>() => {\n return createContext<ContextQueryStore<TAtoms> | null>(null);\n};\n","import { \n createUseContextAtom, \n createUseContextAtomValue, \n createUseContextSetAtom \n} from \"./hooks\";\nimport { createReactContextQuery } from \"./provider\";\n\nexport function createContextQuery<TAtoms extends Record<string, any>>() {\n const { ContextQueryProvider, StoreContext } =\n createReactContextQuery<TAtoms>();\n \n const useContextAtom = createUseContextAtom<TAtoms>(StoreContext);\n const useContextAtomValue = createUseContextAtomValue<TAtoms>(StoreContext);\n const useContextSetAtom = createUseContextSetAtom<TAtoms>(StoreContext);\n \n return {\n ContextQueryProvider,\n useContextAtom,\n useContextAtomValue,\n useContextSetAtom,\n };\n}\n"],"names":["createUseContextAtom","StoreContext","key","store","useContext","Error","useStore","value","setValue","useState","getAtomValue","useEffect","subscription","subscribeToAtom","_","newValue","unsubscribe","useCallback","currentValue","updatedValue","setAtomValue","createUseContextAtomValue","createUseContextSetAtom","t","listeners","constructor","this","Set","getValue","Object","is","notifyListeners","forEach","subscribe","add","delete","e","atoms","Map","entries","s","set","get","String","getAllAtomValues","updateAllAtoms","createReactContextQuery","createContext","ContextQueryProvider","children","useMemo","ContextQueryStore","_jsx","Provider","createContextQuery","useContextAtom","useContextAtomValue","useContextSetAtom"],"mappings":"yJAGM,SAAUA,EACdC,GAcA,OAAmCC,IACjC,MAAMC,EAbS,MACf,MAAMA,EAAQC,EAAWH,GAEzB,IAAKE,EACH,MAAM,IAAIE,MACR,6DAIJ,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AEd,KAAKW,MAAMK,IAAInB,GAAGiB,GAAGA,EAAEhC,SAAS4B,EAAG,GAAE,WCQ/+BU,IACd,MAAM7C,ECLC8C,EAAgD,MDqBvD,MAAO,CACLC,qBAf2B,UAA8BC,SACzDA,EAAQZ,MACRA,IAEA,MAAMlC,EAAQ+C,GACZ,IAAM,IAAIC,EAA0Bd,IACpC,CAACA,IAGH,OACEe,EAACnD,EAAaoD,SAAQ,CAAC9C,MAAOJ,EAAK8C,SAAGA,GAEzC,EAIChD,eAEJ,UEtBgBqD,IACd,MAAMN,qBAAEA,EAAoB/C,aAAEA,GAC5B6C,IAMF,MAAO,CACLE,uBACAO,eANqBvD,EAA6BC,GAOlDuD,oBAN0BnC,EAAkCpB,GAO5DwD,kBANwBnC,EAAgCrB,GAQ5D","x_google_ignoreList":[1]}
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import { ContextQueryStore
|
|
1
|
+
import { ContextQueryStore } from "@context-query/core";
|
|
2
2
|
import { PropsWithChildren } from "react";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
|
|
3
|
+
type AtomValues<T extends Record<string, any>> = {
|
|
4
|
+
[K in keyof T]: T[K];
|
|
6
5
|
};
|
|
6
|
+
export declare function createReactContextQuery<TAtoms extends Record<string, any>>(): {
|
|
7
|
+
ContextQueryProvider: ({ children, atoms, }: PropsWithChildren<{
|
|
8
|
+
atoms: AtomValues<TAtoms>;
|
|
9
|
+
}>) => import("react/jsx-runtime").JSX.Element;
|
|
10
|
+
StoreContext: import("react").Context<ContextQueryStore<TAtoms> | null>;
|
|
11
|
+
};
|
|
12
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@context-query/react",
|
|
3
|
-
"version": "0.3.1",
|
|
3
|
+
"version": "0.3.2-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "React bindings for ContextQuery",
|
|
6
6
|
"author": "Minyeoung Seo <tjalsdud89@naver.com>",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"react": "^18.0.0 || ^19.0.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@context-query/core": "0.3.
|
|
41
|
+
"@context-query/core": "0.3.2-beta.1",
|
|
42
42
|
"react": "^18.3.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|