@context-query/react 0.2.1 → 0.3.0-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 CHANGED
@@ -1,4 +1,2 @@
1
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
- };
2
+ export declare const createStoreContext: <TState extends TStateImpl>() => import("react").Context<ContextQueryStore<TState> | null>;
package/dist/hooks.d.ts CHANGED
@@ -1,3 +1,3 @@
1
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>>): <TKey extends keyof TState>(keys?: TKey[]) => readonly [{ [K in TKey]: TState[K]; }, (value: { [K in TKey]: TState[K]; } | ((prev: { [K in TKey]: TState[K]; }) => { [K in TKey]: TState[K]; })) => void];
2
+ import { createStoreContext } from "./context";
3
+ export declare function createUseContextQuery<TState extends TStateImpl>(contexts: ReturnType<typeof createStoreContext<TState>>): <TKey extends keyof TState>(keys?: TKey[]) => readonly [{ [K in TKey]: TState[K]; }, (value: { [K in TKey]: TState[K]; } | ((prev: { [K in TKey]: TState[K]; }) => { [K in TKey]: TState[K]; })) => void];
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
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)}}};
1
+ "use strict";var t=require("react"),e=require("react/jsx-runtime");function s(e){return s=>{const r=(()=>{const s=t.useContext(e);if(!s)throw new Error("useContextQuery must be used within a ContextQueryProvider");return s})(),n=s??Object.keys(r.getState()),i=()=>n.reduce(((t,e)=>({...t,[e]:r.getStateByKey(e)})),{}),[o,u]=t.useState(i);t.useEffect((()=>{const t=(t,e)=>{u((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])]}}class r{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 n(){const s=t.createContext(null);return{ContextQueryProvider:function({children:n,initialState:i}){const o=t.useMemo((()=>new r(i)),[i]);return e.jsx(s.Provider,{value:o,children:n})},StoreContext:s}}exports.createContextQuery=function(){const{ContextQueryProvider:t,StoreContext:e}=n();return{ContextQueryProvider:t,useContextQuery:s(e)}};
2
2
  //# sourceMappingURL=index.cjs.map
@@ -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,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,OAAOP,GAAG,IAAIR,EAAEgB,MAAMrB,KAAKF,UAAUsB,OAAOxB,GAAG,EAAE,ECIjsB,SAAU0B,EACdC,GAE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AAa2B,EAAgCzB,GA0B/D,MAAO,CACL+B,WACAI,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.cjs","sources":["../src/hooks.ts","../../../node_modules/.pnpm/@context-query+core@0.3.0-beta.1/node_modules/@context-query/core/dist/index.mjs","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createStoreContext<TState>>\n) {\n const useStore = () => {\n const store = useContext(contexts);\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","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 { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { PropsWithChildren, useMemo } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nexport function createReactContextQuery<TState extends TStateImpl>() {\n const StoreContext = createStoreContext<TState>();\n const ContextQueryProvider = function ContextQueryProvider({\n children,\n initialState,\n }: PropsWithChildren<TState>) {\n const store = useMemo(\n () => new ContextQueryStore<TState>(initialState),\n [initialState]\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, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createStoreContext = <TState extends TStateImpl>() => {\n return createContext<ContextQueryStore<TState> | null>(null);\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 { ContextQueryProvider, StoreContext } =\n createReactContextQuery<TState>();\n const useContextQuery = createUseContextQuery<TState>(StoreContext);\n return {\n ContextQueryProvider,\n useContextQuery,\n };\n}\n"],"names":["createUseContextQuery","contexts","keys","store","useContext","Error","useStore","localKeys","Object","getState","getStateSubset","reduce","acc","key","getStateByKey","state","setLocalState","useState","useEffect","handleChange","newValue","prev","subscriptions","map","subscribe","forEach","sub","unsubscribe","useCallback","value","currentValue","updatedValue","entries","k","v","setState","t","listeners","constructor","this","Map","updateState","s","is","notifyListeners","get","e","has","set","Set","add","delete","size","createReactContextQuery","StoreContext","createContext","ContextQueryProvider","children","initialState","useMemo","ContextQueryStore","_jsx","jsx","Provider","useContextQuery"],"mappings":"mEAIM,SAAUA,EACdC,GAcA,OAAmCC,IACjC,MAAMC,EAbS,MACf,MAAMA,EAAQC,EAAUA,WAACH,GAEzB,IAAKE,EACH,MAAM,IAAIE,MACR,8DAIJ,OAAOF,CAAK,EAIEG,GACRC,EAAYL,GAASM,OAAON,KAAKC,EAAMM,YAIvCC,EAAiB,IACdH,EAAUI,QACf,CAACC,EAAKC,KAAS,IAAKD,EAAKC,CAACA,GAAMV,EAAMW,cAAcD,MACpD,CAAA,IAIGE,EAAOC,GAAiBC,EAAAA,SAAsBP,GAErDQ,EAAAA,WAAU,KACR,MAAMC,EAAe,CAACN,EAAWO,KAC/BJ,GAAeK,QAAeA,EAAMR,CAACA,GAAMO,KAAY,EAGnDE,EAAgBf,EAAUgB,KAAKV,GACnCV,EAAMqB,UAAUX,EAAKM,KAGvB,MAAO,KACLG,EAAcG,SAASC,GAAQA,EAAIC,eAAc,CAClD,GACA,CAACpB,EAAWJ,IAiBf,MAAO,CAACY,EAfSa,eACdC,IACC,MAAMC,EAAepB,IACfqB,EACa,mBAAVF,EACFA,EAAmBC,GACpBD,EAENrB,OAAOwB,QAAQD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WCIvrBiB,IACd,MAAMC,ECDCC,EAAAA,cAAgD,MDgBvD,MAAO,CACLC,qBAf2B,UAA8BC,SACzDA,EAAQC,aACRA,IAEA,MAAMvD,EAAQwD,EAAAA,SACZ,IAAM,IAAIC,EAA0BF,IACpC,CAACA,IAGH,OACEG,EAAAC,IAACR,EAAaS,SAAQ,CAAClC,MAAO1B,EAAKsD,SAAGA,GAEzC,EAICH,eAEJ,uCEnBE,MAAME,qBAAEA,EAAoBF,aAAEA,GAC5BD,IAEF,MAAO,CACLG,uBACAQ,gBAHsBhE,EAA8BsD,GAKxD","x_google_ignoreList":[1]}
package/dist/index.d.ts CHANGED
@@ -1,9 +1,5 @@
1
1
  import { TStateImpl } from "@context-query/core";
2
- export declare function createContextQuery<TState extends TStateImpl>(initialState: TState): {
3
- Provider: import("react").FC<{
4
- children?: import("react").ReactNode | undefined;
5
- }>;
2
+ export declare function createContextQuery<TState extends TStateImpl>(): {
3
+ ContextQueryProvider: ({ children, initialState, }: import("react").PropsWithChildren<TState>) => import("react/jsx-runtime").JSX.Element;
6
4
  useContextQuery: <TKey extends keyof TState>(keys?: TKey[] | undefined) => readonly [{ [K in TKey]: TState[K]; }, (value: { [K in TKey]: TState[K]; } | ((prev: { [K in TKey]: TState[K]; }) => { [K in TKey]: TState[K]; })) => void];
7
- updateState: (state: TState | ((prev: TState) => TState)) => void;
8
- setState: <TKey extends keyof TState>(key: TKey, value: TState[TKey] | ((prev: TState[TKey]) => TState[TKey])) => void;
9
5
  };
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{useState as t,useEffect as e,useCallback as s,useContext as n,createContext as r}from"react";import{jsx as i}from"react/jsx-runtime";class o{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 c(r){const{StoreContext:i}=r;return r=>{const o=(()=>{const t=n(i);if(!t)throw new Error("useContextQuery must be used within a ContextQueryProvider");return t})(),c=r??Object.keys(o.getState()),a=()=>c.reduce(((t,e)=>({...t,[e]:o.getStateByKey(e)})),{}),[u,h]=t(a);e((()=>{const t=(t,e)=>{h((s=>({...s,[t]:e})))},e=c.map((e=>o.subscribe(e,t)));return()=>{e.forEach((t=>t.unsubscribe()))}}),[c,o]);return[u,s((t=>{const e=a(),s="function"==typeof t?t(e):t;Object.entries(s).forEach((([t,e])=>{o.setState(t,e)}))}),[c,o])]}}function a(t){const e={StoreContext:r(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 u(t){const e=new o(t),{Provider:s,contexts:n}=a(e);return{Provider:s,useContextQuery:c(n),updateState:t=>{if("function"==typeof t){const s=t,n=e.getState();e.updateState(s(n))}else e.updateState(t)},setState:(t,s)=>{if("function"==typeof s){const n=s,r=e.getStateByKey(t);e.setState(t,n(r))}else e.setState(t,s)}}}export{u as createContextQuery};
1
+ import{useState as t,useEffect as e,useCallback as s,useContext as r,createContext as n,useMemo as i}from"react";import{jsx as o}from"react/jsx-runtime";function c(n){return i=>{const o=(()=>{const t=r(n);if(!t)throw new Error("useContextQuery must be used within a ContextQueryProvider");return t})(),c=i??Object.keys(o.getState()),u=()=>c.reduce(((t,e)=>({...t,[e]:o.getStateByKey(e)})),{}),[a,h]=t(u);e((()=>{const t=(t,e)=>{h((s=>({...s,[t]:e})))},e=c.map((e=>o.subscribe(e,t)));return()=>{e.forEach((t=>t.unsubscribe()))}}),[c,o]);return[a,s((t=>{const e=u(),s="function"==typeof t?t(e):t;Object.entries(s).forEach((([t,e])=>{o.setState(t,e)}))}),[c,o])]}}class u{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 a(){const t=n(null);return{ContextQueryProvider:function({children:e,initialState:s}){const r=i((()=>new u(s)),[s]);return o(t.Provider,{value:r,children:e})},StoreContext:t}}function h(){const{ContextQueryProvider:t,StoreContext:e}=a();return{ContextQueryProvider:t,useContextQuery:c(e)}}export{h as createContextQuery};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -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,KAAKF,UAAUc,IAAIhB,GAAGsB,IAAIL,GAAS,CAACM,YAAY,KAAK,MAAMd,EAAEL,KAAKF,UAAUc,IAAIhB,GAAGS,IAAIA,EAAEe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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.0-beta.1/node_modules/@context-query/core/dist/index.mjs","../src/provider.tsx","../src/context.ts","../src/index.ts"],"sourcesContent":["import { TStateImpl } from \"@context-query/core\";\nimport { useCallback, useContext, useEffect, useState } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nexport function createUseContextQuery<TState extends TStateImpl>(\n contexts: ReturnType<typeof createStoreContext<TState>>\n) {\n const useStore = () => {\n const store = useContext(contexts);\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","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 { ContextQueryStore, TStateImpl } from \"@context-query/core\";\nimport { PropsWithChildren, useMemo } from \"react\";\nimport { createStoreContext } from \"./context\";\n\nexport function createReactContextQuery<TState extends TStateImpl>() {\n const StoreContext = createStoreContext<TState>();\n const ContextQueryProvider = function ContextQueryProvider({\n children,\n initialState,\n }: PropsWithChildren<TState>) {\n const store = useMemo(\n () => new ContextQueryStore<TState>(initialState),\n [initialState]\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, TStateImpl } from \"@context-query/core\";\nimport { createContext } from \"react\";\n\nexport const createStoreContext = <TState extends TStateImpl>() => {\n return createContext<ContextQueryStore<TState> | null>(null);\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 { ContextQueryProvider, StoreContext } =\n createReactContextQuery<TState>();\n const useContextQuery = createUseContextQuery<TState>(StoreContext);\n return {\n ContextQueryProvider,\n useContextQuery,\n };\n}\n"],"names":["createUseContextQuery","contexts","keys","store","useContext","Error","useStore","localKeys","Object","getState","getStateSubset","reduce","acc","key","getStateByKey","state","setLocalState","useState","useEffect","handleChange","newValue","prev","subscriptions","map","subscribe","forEach","sub","unsubscribe","useCallback","value","currentValue","updatedValue","entries","k","v","setState","t","listeners","constructor","this","Map","updateState","s","is","notifyListeners","get","e","has","set","Set","add","delete","size","createReactContextQuery","StoreContext","createContext","ContextQueryProvider","children","initialState","useMemo","ContextQueryStore","_jsx","Provider","createContextQuery","useContextQuery"],"mappings":"yJAIM,SAAUA,EACdC,GAcA,OAAmCC,IACjC,MAAMC,EAbS,MACf,MAAMA,EAAQC,EAAWH,GAEzB,IAAKE,EACH,MAAM,IAAIE,MACR,8DAIJ,OAAOF,CAAK,EAIEG,GACRC,EAAYL,GAASM,OAAON,KAAKC,EAAMM,YAIvCC,EAAiB,IACdH,EAAUI,QACf,CAACC,EAAKC,KAAS,IAAKD,EAAKC,CAACA,GAAMV,EAAMW,cAAcD,MACpD,CAAA,IAIGE,EAAOC,GAAiBC,EAAsBP,GAErDQ,GAAU,KACR,MAAMC,EAAe,CAACN,EAAWO,KAC/BJ,GAAeK,QAAeA,EAAMR,CAACA,GAAMO,KAAY,EAGnDE,EAAgBf,EAAUgB,KAAKV,GACnCV,EAAMqB,UAAUX,EAAKM,KAGvB,MAAO,KACLG,EAAcG,SAASC,GAAQA,EAAIC,eAAc,CAClD,GACA,CAACpB,EAAWJ,IAiBf,MAAO,CAACY,EAfSa,GACdC,IACC,MAAMC,EAAepB,IACfqB,EAC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AIJ,EAAEU,MAAMb,KAAKF,UAAUc,OAAOf,GAAG,EAAE,WCIvrBiB,IACd,MAAMC,ECDCC,EAAgD,MDgBvD,MAAO,CACLC,qBAf2B,UAA8BC,SACzDA,EAAQC,aACRA,IAEA,MAAMvD,EAAQwD,GACZ,IAAM,IAAIC,EAA0BF,IACpC,CAACA,IAGH,OACEG,EAACP,EAAaQ,SAAQ,CAACjC,MAAO1B,EAAKsD,SAAGA,GAEzC,EAICH,eAEJ,UEpBgBS,IACd,MAAMP,qBAAEA,EAAoBF,aAAEA,GAC5BD,IAEF,MAAO,CACLG,uBACAQ,gBAHsBhE,EAA8BsD,GAKxD","x_google_ignoreList":[1]}
@@ -1,13 +1,6 @@
1
1
  import { ContextQueryStore, TStateImpl } from "@context-query/core";
2
- import { FC, PropsWithChildren } from "react";
3
- import { createContextQuery } from "./context";
4
- export type ProviderProps<TState extends TStateImpl> = PropsWithChildren;
5
- export declare const createContextQueryProvider: <TState extends TStateImpl>(contexts: ReturnType<typeof createContextQuery<TState>>, store: ContextQueryStore<TState>) => FC<ProviderProps<TState>>;
6
- export declare function createReactContextQuery<TState extends TStateImpl>(store: ContextQueryStore<TState>): {
7
- Provider: FC<{
8
- children?: import("react").ReactNode | undefined;
9
- }>;
10
- contexts: {
11
- StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
12
- };
2
+ import { PropsWithChildren } from "react";
3
+ export declare function createReactContextQuery<TState extends TStateImpl>(): {
4
+ ContextQueryProvider: ({ children, initialState, }: PropsWithChildren<TState>) => import("react/jsx-runtime").JSX.Element;
5
+ StoreContext: import("react").Context<ContextQueryStore<TState> | null>;
13
6
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@context-query/react",
3
- "version": "0.2.1",
3
+ "version": "0.3.0-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.2.1",
41
+ "@context-query/core": "0.3.0-beta.1",
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
@@ -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,OAA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@@ -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,GAAG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