@context-query/react 0.2.1 → 0.2.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 +2 -4
- package/dist/hooks.d.ts +9 -3
- package/dist/index.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +11 -8
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/provider.d.ts +11 -12
- package/package.json +2 -2
- package/dist/index.esm.js +0 -2
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
package/dist/context.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import type { ContextQueryStore
|
|
2
|
-
export declare const
|
|
3
|
-
StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
|
|
4
|
-
};
|
|
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,3 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
export declare function
|
|
1
|
+
import type { ContextQueryStore } from "@context-query/core";
|
|
2
|
+
import { createStoreContext } from "./context";
|
|
3
|
+
export declare function createUseStore<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): () => ContextQueryStore<TAtoms>;
|
|
4
|
+
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];
|
|
5
|
+
export declare function createUseContextAtomValue<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): <TKey extends keyof TAtoms>(key: TKey) => TAtoms[TKey];
|
|
6
|
+
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);
|
|
7
|
+
export declare function createUseAllAtoms<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): () => [TAtoms, (newAtoms: Partial<TAtoms>) => void];
|
|
8
|
+
export declare function createUseAllAtomsValue<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): () => TAtoms;
|
|
9
|
+
export declare function createUseUpdateAllAtoms<TAtoms extends Record<string, any>>(StoreContext: ReturnType<typeof createStoreContext<TAtoms>>): () => ((newAtoms: Partial<TAtoms>) => void);
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var t=require("react"),e=require("react/jsx-runtime");
|
|
1
|
+
"use strict";var t=require("react"),e=require("react/jsx-runtime");function s(e,s){return()=>{const o=t.useContext(e);if(!o)throw new Error(`${s} must be used within a ContextQueryProvider`);return o}}class o{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()))}subscribe(t){return this.listeners.add(t),{unsubscribe:()=>{this.listeners.delete(t)}}}}class r{atoms;constructor(t){this.atoms=new Map,Object.entries(t).forEach((([t,e])=>{this.atoms.set(t,new o(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 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(e)}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)}))}}function n(){const s=t.createContext(null);return{ContextQueryProvider:function({children:o,atoms:n}){const u=t.useMemo((()=>new r(n)),[n]);return e.jsx(s.Provider,{value:u,children:o})},StoreContext:s}}exports.createContextQuery=function(){const{ContextQueryProvider:e,StoreContext:o}=n(),r=function(e){const o=s(e,"useContextAtom");return e=>{const s=o();return[t.useSyncExternalStore(t.useCallback((t=>{const o=s.subscribeToAtom(e,t);return()=>o.unsubscribe()}),[s,e]),t.useCallback((()=>s.getAtomValue(e)),[s,e]),t.useCallback((()=>s.getAtomValue(e)),[s,e])),t.useCallback((t=>{const o=s.getAtomValue(e),r="function"==typeof t?t(o):t;s.setAtomValue(e,r)}),[e,s])]}}(o),u=function(e){const o=s(e,"useContextAtomValue");return e=>{const s=o();return t.useSyncExternalStore(t.useCallback((t=>{const o=s.subscribeToAtom(e,t);return()=>o.unsubscribe()}),[s,e]),t.useCallback((()=>s.getAtomValue(e)),[s,e]),t.useCallback((()=>s.getAtomValue(e)),[s,e]))}}(o),l=function(e){const o=s(e,"useContextSetAtom");return e=>{const s=o();return t.useCallback((t=>{const o=s.getAtomValue(e),r="function"==typeof t?t(o):t;s.setAtomValue(e,r)}),[e,s])}}(o),a=function(t){return s(t,"useStore")}(o),c=function(e){const o=s(e,"useAllAtoms");return()=>{const e=o(),s=t.useCallback((t=>{const s=e.getAllAtomValues(),o=Object.keys(s).map((s=>e.subscribeToAtom(s,t)));return()=>{o.forEach((t=>t.unsubscribe()))}}),[e]),r=t.useCallback((()=>e.getAllAtomValues()),[e]);return[t.useSyncExternalStore(s,r,r),t.useCallback((t=>{e.updateAllAtoms(t)}),[e])]}}(o),i=function(e){const o=s(e,"useAllAtomsValue");return()=>{const e=o(),s=t.useCallback((t=>{const s=e.getAllAtomValues(),o=Object.keys(s).map((s=>e.subscribeToAtom(s,t)));return()=>{o.forEach((t=>t.unsubscribe()))}}),[e]),r=t.useCallback((()=>e.getAllAtomValues()),[e]);return t.useSyncExternalStore(s,r,r)}}(o),m=function(e){const o=s(e,"useUpdateAllAtoms");return()=>{const e=o();return t.useCallback((t=>{e.updateAllAtoms(t)}),[e])}}(o);return{ContextQueryProvider:e,useContextAtom:r,useContextAtomValue:u,useContextSetAtom:l,useStore:a,useAllAtoms:c,useAllAtomsValue:i,useUpdateAllAtoms:m}};
|
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../core/dist/index.mjs","../src/hooks.ts","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["class t{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))}}}}export{t as ContextQueryStore};\n//# sourceMappingURL=index.mjs.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":["t","state","listeners","constructor","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","ContextQueryStore","useContextQuery","updateFn"],"mappings":"mEAAA,MAAMA,EAAEC,MAAMC,UAAU,WAAAC,CAAYH,GAAGI,KAAKH,MAAMD,EAAEI,KAAKF,UAAU,IAAIG,GAAG,CAAC,QAAAC,GAAW,OAAOF,KAAKH,KAAK,CAAC,aAAAM,CAAcP,GAAG,OAAOI,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,EAAWjB,EACXC,EAAepB,EAAMtB,cAAc8B,GACzCR,EAAMd,SAASsB,EAAK4B,EAAShB,SAE7BpB,EAAMd,SAASsB,EAAKW,IAU1B"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../src/hooks.ts","../../../node_modules/.pnpm/@context-query+core@0.3.2/node_modules/@context-query/core/dist/index.mjs","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["import type { ContextQueryStore } from \"@context-query/core\";\nimport { useCallback, useContext, useSyncExternalStore } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nfunction createUseStoreInternal<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>,\n hookName: string\n): () => ContextQueryStore<TAtoms> {\n return () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(`${hookName} must be used within a ContextQueryProvider`);\n }\n\n return store;\n };\n}\n\nexport function createUseStore<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n): () => ContextQueryStore<TAtoms> {\n return createUseStoreInternal(StoreContext, \"useStore\");\n}\n\nexport function createUseContextAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useContextAtom\");\n\n return <TKey extends keyof TAtoms>(key: TKey) => {\n const store = useStore();\n\n const value = useSyncExternalStore(\n useCallback(\n (onStoreChange: () => void) => {\n const subscription = store.subscribeToAtom(key, onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, key]\n ),\n useCallback(() => store.getAtomValue(key), [store, key]),\n useCallback(() => store.getAtomValue(key), [store, key])\n );\n\n const setAtom = useCallback(\n (newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue =\n typeof newValue === \"function\"\n ? (newValue as Function)(currentValue)\n : newValue;\n\n store.setAtomValue(key, updatedValue);\n },\n [key, store]\n );\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 = createUseStoreInternal(StoreContext, \"useContextAtomValue\");\n\n return <TKey extends keyof TAtoms>(key: TKey): TAtoms[TKey] => {\n const store = useStore();\n\n return useSyncExternalStore(\n useCallback(\n (onStoreChange: () => void) => {\n const subscription = store.subscribeToAtom(key, onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, key]\n ),\n useCallback(() => store.getAtomValue(key), [store, key]),\n useCallback(() => store.getAtomValue(key), [store, key])\n );\n };\n}\n\nexport function createUseContextSetAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useContextSetAtom\");\n\n return <TKey extends keyof TAtoms>(\n key: TKey\n ): ((\n value: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])\n ) => void) => {\n const store = useStore();\n\n return useCallback(\n (newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue =\n typeof newValue === \"function\"\n ? (newValue as Function)(currentValue)\n : newValue;\n\n store.setAtomValue(key, updatedValue);\n },\n [key, store]\n );\n };\n}\n\nexport function createUseAllAtoms<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useAllAtoms\");\n\n return (): [TAtoms, (newAtoms: Partial<TAtoms>) => void] => {\n const store = useStore();\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const currentAtoms = store.getAllAtomValues();\n const subscriptions = Object.keys(currentAtoms).map((key) =>\n store.subscribeToAtom(key as keyof TAtoms, onStoreChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n },\n [store]\n );\n\n const getSnapshot = useCallback(() => store.getAllAtomValues(), [store]);\n\n const allValues = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const updateAllAtoms = useCallback(\n (newAtoms: Partial<TAtoms>) => {\n store.updateAllAtoms(newAtoms);\n },\n [store]\n );\n\n return [allValues, updateAllAtoms] as const;\n };\n}\n\nexport function createUseAllAtomsValue<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useAllAtomsValue\");\n\n return (): TAtoms => {\n const store = useStore();\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const currentAtoms = store.getAllAtomValues();\n const subscriptions = Object.keys(currentAtoms).map((key) =>\n store.subscribeToAtom(key as keyof TAtoms, onStoreChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n },\n [store]\n );\n\n const getSnapshot = useCallback(() => store.getAllAtomValues(), [store]);\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n}\n\nexport function createUseUpdateAllAtoms<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useUpdateAllAtoms\");\n\n return (): ((newAtoms: Partial<TAtoms>) => void) => {\n const store = useStore();\n\n return useCallback(\n (newAtoms: Partial<TAtoms>) => {\n store.updateAllAtoms(newAtoms);\n },\n [store]\n );\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()))}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(e)}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 createUseStore,\n createUseAllAtoms,\n createUseAllAtomsValue,\n createUseUpdateAllAtoms\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 const useStore = createUseStore<TAtoms>(StoreContext);\n const useAllAtoms = createUseAllAtoms<TAtoms>(StoreContext);\n const useAllAtomsValue = createUseAllAtomsValue<TAtoms>(StoreContext);\n const useUpdateAllAtoms = createUseUpdateAllAtoms<TAtoms>(StoreContext);\n \n return {\n ContextQueryProvider,\n useContextAtom,\n useContextAtomValue,\n useContextSetAtom,\n useStore,\n useAllAtoms,\n useAllAtomsValue,\n useUpdateAllAtoms,\n };\n}\n"],"names":["createUseStoreInternal","StoreContext","hookName","store","useContext","Error","t","value","listeners","constructor","this","Set","getValue","setValue","Object","is","notifyListeners","forEach","subscribe","add","unsubscribe","delete","e","atoms","Map","entries","s","set","getAtomValue","get","String","setAtomValue","subscribeToAtom","getAllAtomValues","updateAllAtoms","createReactContextQuery","createContext","ContextQueryProvider","children","useMemo","ContextQueryStore","_jsx","jsx","Provider","useContextAtom","useStore","key","useSyncExternalStore","useCallback","onStoreChange","subscription","newValue","currentValue","updatedValue","createUseContextAtom","useContextAtomValue","createUseContextAtomValue","useContextSetAtom","createUseContextSetAtom","createUseStore","useAllAtoms","currentAtoms","subscriptions","keys","map","sub","getSnapshot","newAtoms","createUseAllAtoms","useAllAtomsValue","createUseAllAtomsValue","useUpdateAllAtoms","createUseUpdateAllAtoms"],"mappings":"mEAIA,SAASA,EACPC,EACAC,GAEA,MAAO,KACL,MAAMC,EAAQC,EAAUA,WAACH,GAEzB,IAAKE,EACH,MAAM,IAAIE,MAAM,GAAGH,gDAGrB,OAAOC,CAAK,CAEhB,CCjBA,MAAMG,EAAEC,MAAMC,UAAU,WAAAC,CAAYH,GAAGI,KAAKH,MAAMD,EAAEI,KAAKF,UAAU,IAAIG,GAAG,CAAC,QAAAC,GAAW,OAAOF,KAAKH,KAAK,CAAC,QAAAM,CAASP,GAAGQ,OAAOC,GAAGL,KAAKH,MAAMD,KAAKI,KAAKH,MAAMD,EAAEI,KAAKM,kBAAkB,CAAC,eAAAA,GAAkBN,KAAKF,UAAUS,SAASX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EpE,CI1J2BI,CAA+BrE,GAClDsE,EJ2JF,SACJtE,GAEA,MAAM4C,EAAW7C,EAAuBC,EAAc,qBAEtD,MAAO,KACL,MAAME,EAAQ0C,IAEd,OAAOG,EAAAA,aACJmB,IACChE,EAAM+B,eAAeiC,EAAS,GAEhC,CAAChE,GACF,CAEL,CI1K4BqE,CAAgCvE,GAE1D,MAAO,CACLoC,uBACAO,iBACAW,sBACAE,oBACAZ,WACAe,cACAS,mBACAE,oBAEJ","x_google_ignoreList":[1]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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;
|
|
8
|
+
useStore: () => import("@context-query/core").ContextQueryStore<TAtoms>;
|
|
9
|
+
useAllAtoms: () => [TAtoms, (newAtoms: Partial<TAtoms>) => void];
|
|
10
|
+
useAllAtomsValue: () => TAtoms;
|
|
11
|
+
useUpdateAllAtoms: () => (newAtoms: Partial<TAtoms>) => void;
|
|
9
12
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{useCallback as t,useSyncExternalStore as e,useContext as o,createContext as s,useMemo as n}from"react";import{jsx as r}from"react/jsx-runtime";function u(t,e){return()=>{const s=o(t);if(!s)throw new Error(`${e} must be used within a ContextQueryProvider`);return s}}class i{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()))}subscribe(t){return this.listeners.add(t),{unsubscribe:()=>{this.listeners.delete(t)}}}}class c{atoms;constructor(t){this.atoms=new Map,Object.entries(t).forEach((([t,e])=>{this.atoms.set(t,new i(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 o=this.atoms.get(t);if(!o)throw new Error(`Atom with key "${String(t)}" not found`);o.setValue(e)}subscribeToAtom(t,e){const o=this.atoms.get(t);if(!o)throw new Error(`Atom with key "${String(t)}" not found`);return o.subscribe(e)}getAllAtomValues(){const t={};return this.atoms.forEach(((e,o)=>{t[o]=e.getValue()})),t}updateAllAtoms(t){Object.entries(t).forEach((([t,e])=>{const o=this.atoms.get(t);o&&o.setValue(e)}))}}function l(){const t=s(null);return{ContextQueryProvider:function({children:e,atoms:o}){const s=n((()=>new c(o)),[o]);return r(t.Provider,{value:s,children:e})},StoreContext:t}}function a(){const{ContextQueryProvider:o,StoreContext:s}=l(),n=function(o){const s=u(o,"useContextAtom");return o=>{const n=s();return[e(t((t=>{const e=n.subscribeToAtom(o,t);return()=>e.unsubscribe()}),[n,o]),t((()=>n.getAtomValue(o)),[n,o]),t((()=>n.getAtomValue(o)),[n,o])),t((t=>{const e=n.getAtomValue(o),s="function"==typeof t?t(e):t;n.setAtomValue(o,s)}),[o,n])]}}(s),r=function(o){const s=u(o,"useContextAtomValue");return o=>{const n=s();return e(t((t=>{const e=n.subscribeToAtom(o,t);return()=>e.unsubscribe()}),[n,o]),t((()=>n.getAtomValue(o)),[n,o]),t((()=>n.getAtomValue(o)),[n,o]))}}(s),i=function(e){const o=u(e,"useContextSetAtom");return e=>{const s=o();return t((t=>{const o=s.getAtomValue(e),n="function"==typeof t?t(o):t;s.setAtomValue(e,n)}),[e,s])}}(s),c=function(t){return u(t,"useStore")}(s),a=function(o){const s=u(o,"useAllAtoms");return()=>{const o=s(),n=t((t=>{const e=o.getAllAtomValues(),s=Object.keys(e).map((e=>o.subscribeToAtom(e,t)));return()=>{s.forEach((t=>t.unsubscribe()))}}),[o]),r=t((()=>o.getAllAtomValues()),[o]);return[e(n,r,r),t((t=>{o.updateAllAtoms(t)}),[o])]}}(s),m=function(o){const s=u(o,"useAllAtomsValue");return()=>{const o=s(),n=t((t=>{const e=o.getAllAtomValues(),s=Object.keys(e).map((e=>o.subscribeToAtom(e,t)));return()=>{s.forEach((t=>t.unsubscribe()))}}),[o]),r=t((()=>o.getAllAtomValues()),[o]);return e(n,r,r)}}(s),A=function(e){const o=u(e,"useUpdateAllAtoms");return()=>{const e=o();return t((t=>{e.updateAllAtoms(t)}),[e])}}(s);return{ContextQueryProvider:o,useContextAtom:n,useContextAtomValue:r,useContextSetAtom:i,useStore:c,useAllAtoms:a,useAllAtomsValue:m,useUpdateAllAtoms:A}}export{a as createContextQuery};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../core/dist/index.mjs","../src/hooks.ts","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["class t{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))}}}}export{t as ContextQueryStore};\n//# sourceMappingURL=index.mjs.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":["t","state","listeners","constructor","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","ContextQueryStore","useContextQuery","updateFn"],"mappings":"4IAAA,MAAMA,EAAEC,MAAMC,UAAU,WAAAC,CAAYH,GAAGI,KAAKH,MAAMD,EAAEI,KAAKF,UAAU,IAAIG,GAAG,CAAC,QAAAC,GAAW,OAAOF,KAAKH,KAAK,CAAC,aAAAM,CAAcP,GAAG,OAAOI,KAAKH,MAAMD,EAAE,CAAC,WAAAQ,CAAYR,GAAG,MAAMS,EAAE,IAAIL,KAAKH,OAAOG,KAAKH,MAAM,IAAID,GAAGU,OAAOC,KAAKX,GAAGY,SAASZ,IAAIU,OAAOG,GAAGJ,EAAET,GAAGI,KAAKH,MAAMD,KAAKI,KAAKU,gBAAgBd,EAAG,GAAE,CAAC,QAAAe,CAASf,EAAES,GAAGC,OAAOG,GAAGT,KAAKH,MAAMD,GAAGS,KAAKL,KAAKH,MAAM,IAAIG,KAAKH,MAAMD,CAACA,GAAGS,GAAGL,KAAKU,gBAAgBd,GAAG,CAAC,eAAAc,CAAgBd,GAAG,MAAMS,EAAEL,KAAKF,UAAUc,IAAIhB,GAAG,GAAGS,EAAE,CAAC,MAAMQ,EAAEb,KAAKH,MAAMD,GAAGS,EAAEG,SAASZ,GAAGA,EAAEiB,IAAI,CAAC,CAAC,SAAAC,CAAUlB,EAAES,GAAG,MAAMQ,EAAEA,IAAIR,EAAET,EAAEiB,EAAC,EAAqF,OAAlFb,KAAKF,UAAUiB,IAAInB,IAAII,KAAKF,UAAUkB,IAAIpB,EAAE,IAAIqB,KAAKjB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AOnB,EAAK4B,SAAGA,GAEzC,CAAA,EAO4BG,CAC3BjC,EACAE,GAGF,MAAO,CACL8B,SAAUH,EACV7B,WAEJ,CE5BM,SAAUkC,EACdC,GAEA,MAAMjC,EAAQ,IAAIkC,EAA0BD,IAEtCH,SAAEA,EAAQhC,SAAEA,GAAa2B,EAAgCzB,GA0B/D,MAAO,CACL8B,WACAK,gBA3BsBtC,EAA8BC,GA4BpDnB,YA1BmBP,IACnB,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMgE,EAAWhE,EACXgD,EAAepB,EAAMvB,WAC3BuB,EAAMrB,YAAYyD,EAAShB,SAE3BpB,EAAMrB,YAAYP,IAqBpBc,SAjBe,CACfsB,EACAW,KAEA,GAAqB,mBAAVA,EAAsB,CAC/B,MAAMiB,EAAWjB,EACXC,EAAepB,EAAMtB,cAAc8B,GACzCR,EAAMd,SAASsB,EAAK4B,EAAShB,SAE7BpB,EAAMd,SAASsB,EAAKW,IAU1B"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/hooks.ts","../../../node_modules/.pnpm/@context-query+core@0.3.2/node_modules/@context-query/core/dist/index.mjs","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["import type { ContextQueryStore } from \"@context-query/core\";\nimport { useCallback, useContext, useSyncExternalStore } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nfunction createUseStoreInternal<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>,\n hookName: string\n): () => ContextQueryStore<TAtoms> {\n return () => {\n const store = useContext(StoreContext);\n\n if (!store) {\n throw new Error(`${hookName} must be used within a ContextQueryProvider`);\n }\n\n return store;\n };\n}\n\nexport function createUseStore<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n): () => ContextQueryStore<TAtoms> {\n return createUseStoreInternal(StoreContext, \"useStore\");\n}\n\nexport function createUseContextAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useContextAtom\");\n\n return <TKey extends keyof TAtoms>(key: TKey) => {\n const store = useStore();\n\n const value = useSyncExternalStore(\n useCallback(\n (onStoreChange: () => void) => {\n const subscription = store.subscribeToAtom(key, onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, key]\n ),\n useCallback(() => store.getAtomValue(key), [store, key]),\n useCallback(() => store.getAtomValue(key), [store, key])\n );\n\n const setAtom = useCallback(\n (newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue =\n typeof newValue === \"function\"\n ? (newValue as Function)(currentValue)\n : newValue;\n\n store.setAtomValue(key, updatedValue);\n },\n [key, store]\n );\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 = createUseStoreInternal(StoreContext, \"useContextAtomValue\");\n\n return <TKey extends keyof TAtoms>(key: TKey): TAtoms[TKey] => {\n const store = useStore();\n\n return useSyncExternalStore(\n useCallback(\n (onStoreChange: () => void) => {\n const subscription = store.subscribeToAtom(key, onStoreChange);\n return () => subscription.unsubscribe();\n },\n [store, key]\n ),\n useCallback(() => store.getAtomValue(key), [store, key]),\n useCallback(() => store.getAtomValue(key), [store, key])\n );\n };\n}\n\nexport function createUseContextSetAtom<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useContextSetAtom\");\n\n return <TKey extends keyof TAtoms>(\n key: TKey\n ): ((\n value: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])\n ) => void) => {\n const store = useStore();\n\n return useCallback(\n (newValue: TAtoms[TKey] | ((prev: TAtoms[TKey]) => TAtoms[TKey])) => {\n const currentValue = store.getAtomValue(key);\n const updatedValue =\n typeof newValue === \"function\"\n ? (newValue as Function)(currentValue)\n : newValue;\n\n store.setAtomValue(key, updatedValue);\n },\n [key, store]\n );\n };\n}\n\nexport function createUseAllAtoms<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useAllAtoms\");\n\n return (): [TAtoms, (newAtoms: Partial<TAtoms>) => void] => {\n const store = useStore();\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const currentAtoms = store.getAllAtomValues();\n const subscriptions = Object.keys(currentAtoms).map((key) =>\n store.subscribeToAtom(key as keyof TAtoms, onStoreChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n },\n [store]\n );\n\n const getSnapshot = useCallback(() => store.getAllAtomValues(), [store]);\n\n const allValues = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n\n const updateAllAtoms = useCallback(\n (newAtoms: Partial<TAtoms>) => {\n store.updateAllAtoms(newAtoms);\n },\n [store]\n );\n\n return [allValues, updateAllAtoms] as const;\n };\n}\n\nexport function createUseAllAtomsValue<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useAllAtomsValue\");\n\n return (): TAtoms => {\n const store = useStore();\n\n const subscribe = useCallback(\n (onStoreChange: () => void) => {\n const currentAtoms = store.getAllAtomValues();\n const subscriptions = Object.keys(currentAtoms).map((key) =>\n store.subscribeToAtom(key as keyof TAtoms, onStoreChange)\n );\n\n return () => {\n subscriptions.forEach((sub) => sub.unsubscribe());\n };\n },\n [store]\n );\n\n const getSnapshot = useCallback(() => store.getAllAtomValues(), [store]);\n\n return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);\n };\n}\n\nexport function createUseUpdateAllAtoms<TAtoms extends Record<string, any>>(\n StoreContext: ReturnType<typeof createStoreContext<TAtoms>>\n) {\n const useStore = createUseStoreInternal(StoreContext, \"useUpdateAllAtoms\");\n\n return (): ((newAtoms: Partial<TAtoms>) => void) => {\n const store = useStore();\n\n return useCallback(\n (newAtoms: Partial<TAtoms>) => {\n store.updateAllAtoms(newAtoms);\n },\n [store]\n );\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()))}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(e)}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 createUseStore,\n createUseAllAtoms,\n createUseAllAtomsValue,\n createUseUpdateAllAtoms\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 const useStore = createUseStore<TAtoms>(StoreContext);\n const useAllAtoms = createUseAllAtoms<TAtoms>(StoreContext);\n const useAllAtomsValue = createUseAllAtomsValue<TAtoms>(StoreContext);\n const useUpdateAllAtoms = createUseUpdateAllAtoms<TAtoms>(StoreContext);\n \n return {\n ContextQueryProvider,\n useContextAtom,\n useContextAtomValue,\n useContextSetAtom,\n useStore,\n useAllAtoms,\n useAllAtomsValue,\n useUpdateAllAtoms,\n };\n}\n"],"names":["createUseStoreInternal","StoreContext","hookName","store","useContext","Error","t","value","listeners","constructor","this","Set","getValue","setValue","Object","is","notifyListeners","forEach","subscribe","add","unsubscribe","delete","e","atoms","Map","entries","s","set","getAtomValue","get","String","setAtomValue","subscribeToAtom","getAllAtomValues","updateAllAtoms","createReactContextQuery","createContext","ContextQueryProvider","children","useMemo","ContextQueryStore","_jsx","Provider","createContextQuery","useContextAtom","useStore","key","useSyncExternalStore","useCallback","onStoreChange","subscription","newValue","currentValue","updatedValue","createUseContextAtom","useContextAtomValue","createUseContextAtomValue","useContextSetAtom","createUseContextSetAtom","createUseStore","useAllAtoms","currentAtoms","subscriptions","keys","map","sub","getSnapshot","newAtoms","createUseAllAtoms","useAllAtom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JpE,GAEA,MAAM4C,EAAW7C,EAAuBC,EAAc,oBAEtD,MAAO,KACL,MAAME,EAAQ0C,IAER3B,EAAY8B,GACfC,IACC,MAAMY,EAAe1D,EAAM8B,mBACrB6B,EAAgBhD,OAAOiD,KAAKF,GAAcG,KAAKlB,GACnD3C,EAAM6B,gBAAgBc,EAAqBG,KAG7C,MAAO,KACLa,EAAc7C,SAASgD,GAAQA,EAAI7C,eAAc,CAClD,GAEH,CAACjB,IAGG+D,EAAclB,GAAY,IAAM7C,EAAM8B,oBAAoB,CAAC9B,IAEjE,OAAO4C,EAAqB7B,EAAWgD,EAAaA,EAAY,CAEpE,CI1J2BI,CAA+BrE,GAClDsE,EJ2JF,SACJtE,GAEA,MAAM4C,EAAW7C,EAAuBC,EAAc,qBAEtD,MAAO,KACL,MAAME,EAAQ0C,IAEd,OAAOG,GACJmB,IACChE,EAAM+B,eAAeiC,EAAS,GAEhC,CAAChE,GACF,CAEL,CI1K4BqE,CAAgCvE,GAE1D,MAAO,CACLoC,uBACAO,iBACAW,sBACAE,oBACAZ,WACAe,cACAS,mBACAE,oBAEJ","x_google_ignoreList":[1]}
|
package/dist/provider.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { ContextQueryStore
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
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
|
-
}>;
|
|
10
|
-
contexts: {
|
|
11
|
-
StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
|
|
12
|
-
};
|
|
1
|
+
import { ContextQueryStore } from "@context-query/core";
|
|
2
|
+
import { PropsWithChildren } from "react";
|
|
3
|
+
type AtomValues<T extends Record<string, any>> = {
|
|
4
|
+
[K in keyof T]: T[K];
|
|
13
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.2.
|
|
3
|
+
"version": "0.2.2",
|
|
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.2
|
|
41
|
+
"@context-query/core": "0.3.2",
|
|
42
42
|
"react": "^18.3.1"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
package/dist/index.esm.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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};
|
|
2
|
-
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,K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EzB,OAAO,UAA8B8B,SAAEA,IACrC,OACEC,EAAC9B,EAAa+B,SAAQ,CAACX,MAAOnB,EAAK4B,SAAGA,GAEzC,CAAA,EAO4BG,CAC3BjC,EACAE,GAGF,MAAO,CACL8B,SAAUH,EACV7B,WAEJ,CE5BM,SAAUkC,EACdC,GAEA,MAAMjC,EAAQ,IAAI9B,EAAiBA,kBAAS+D,IAEtCH,SAAEA,EAAQhC,SAAEA,GAAa2B,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"}
|
package/dist/index.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var t=require("react"),e=require("react/jsx-runtime");class s{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))}}}}function r(e){const{StoreContext:s}=e;return e=>{const r=(()=>{const e=t.useContext(s);if(!e)throw new Error("useContextQuery must be used within a ContextQueryProvider");return e})(),n=e??Object.keys(r.getState()),i=()=>n.reduce(((t,e)=>({...t,[e]:r.getStateByKey(e)})),{}),[o,c]=t.useState(i);t.useEffect((()=>{const t=(t,e)=>{c((s=>({...s,[t]:e})))},e=n.map((e=>r.subscribe(e,t)));return()=>{e.forEach((t=>t.unsubscribe()))}}),[n,r]);return[o,t.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 n(s){const r={StoreContext:t.createContext(null)},n=((t,s)=>{const{StoreContext:r}=t;return function({children:t}){return e.jsx(r.Provider,{value:s,children:t})}})(r,s);return{Provider:n,contexts:r}}exports.createContextQuery=function(t){const e=new s(t),{Provider:i,contexts:o}=n(e);return{Provider:i,useContextQuery:r(o),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)}}};
|
|
2
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../core/dist/index.mjs","../src/hooks.ts","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["class t{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))}}}}export{t as ContextQueryStore};\n//# sourceMappingURL=index.mjs.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":["t","state","listeners","constructor","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","ContextQueryStore","useContextQuery","updateFn"],"mappings":"mEAAA,MAAMA,EAAEC,MAAMC,UAAU,WAAAC,CAAYH,GAAGI,KAAKH,MAAMD,EAAEI,KAAKF,UAAU,IAAIG,GAAG,CAAC,QAAAC,GAAW,OAAOF,KAAKH,KAAK,CAAC,aAAAM,CAAcP,GAAG,OAAOI,KAAKH,MAAMD,EAAE,CAAC,WAAAQ,CAAYR,GAAG,MAAMS,EAAE,IAAIL,KAAKH,OAAOG,KAAKH,MAAM,IAAID,GAAGU,OAAOC,KAAKX,GAAGY,SAASZ,IAAIU,OAAOG,GAAGJ,EAAET,GAAGI,KAAKH,MAAMD,KAAKI,KAAKU,gBAAgBd,EAAG,GAAE,CAAC,QAAAe,CAASf,EAAES,GAAGC,OAAOG,GAAGT,KAAKH,MAAMD,GAAGS,KAAKL,KAAKH,MAAM,IAAIG,KAAKH,MAAMD,CAACA,GAAGS,GAAGL,KAAKU,gBAAgBd,GAAG,CAAC,eAAAc,CAAgBd,GAAG,MAAMS,EAAEL,KAAKF,UAAUc,IAAIhB,GAAG,GAAGS,EAAE,CAAC,MAAMQ,EAAEb,KAAKH,MAAMD,GAAGS,EAAEG,SAASZ,GAAGA,EAAEiB,IAAI,CAAC,CAAC,SAAAC,CAAUlB,EAAES,GAAG,MAAMQ,EAAEA,IAAIR,EAAET,EAAEiB,EAAC,EAAqF,OAAlFb,KAAKF,UAAUiB,IAAInB,IAAII,KAAKF,UAAUkB,IAAIpB,EAAE,IAAIqB,KAAKjB,KAAKF,UAAUc,IAAIhB,GAAGsB,IAAIL,GAAS,CAACM,YAAY,KAAK,MAAMd,EAAEL,KAAKF,UAAUc,IAAIhB,GAAGS,IAAIA,EAAEe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