@context-query/react 0.2.0-dev.3 → 0.2.0-dev.5
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/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +14 -14
package/dist/index.cjs
ADDED
|
@@ -0,0 +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)}}};
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +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,G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CR,EAAMd,SAASsB,EAAK4B,EAAShB,SAE7BpB,EAAMd,SAASsB,EAAKW,IAU1B"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var t
|
|
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
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../core/dist/index.
|
|
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,O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0,0 +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};
|
|
2
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +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,IAA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ACdC,IACC,MAAMC,EAAef,IACfgB,EACa,mBAAVF,EACFA,EAAmBC,GACpBD,EAENtC,OAAOyC,QAAQD,GAActC,SAAQ,EAAEwC,EAAGC,MACxCxB,EAAMd,SAASqC,EAAWC,EAAkB,GAC5C,GAEJ,CAACpB,EAAWJ,IAGmB,CAErC,CChDM,SAAUyB,EACdzB,GAEA,MAAMF,ECjBC,CACLC,aAFmB2B,EAAgD,ODmB/DC,EAjBkC,EACxC7B,EACAE,KAEA,MAAMD,aAAEA,GAAiBD,EAEzB,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,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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@context-query/react",
|
|
3
|
-
"version": "0.2.0-dev.
|
|
3
|
+
"version": "0.2.0-dev.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "React bindings for ContextQuery",
|
|
6
6
|
"author": "Minyeoung Seo <tjalsdud89@naver.com>",
|
|
@@ -20,29 +20,29 @@
|
|
|
20
20
|
"build": "rollup -c",
|
|
21
21
|
"dev": "rollup -c -w"
|
|
22
22
|
},
|
|
23
|
-
"main": "dist/index.
|
|
24
|
-
"module": "dist/index.
|
|
25
|
-
"types": "dist/index.d.ts",
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.mjs",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
26
|
"files": [
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"import": "./dist/index.mjs",
|
|
32
|
+
"require": "./dist/index.cjs",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
34
|
+
"default": "./dist/index.mjs"
|
|
35
|
+
}
|
|
36
|
+
},
|
|
29
37
|
"peerDependencies": {
|
|
30
38
|
"react": "^18.0.0 || ^19.0.0"
|
|
31
39
|
},
|
|
32
40
|
"dependencies": {
|
|
33
|
-
"@context-query/core": "0.2.0",
|
|
41
|
+
"@context-query/core": "0.2.0-dev.5",
|
|
34
42
|
"react": "^18.3.1"
|
|
35
43
|
},
|
|
36
44
|
"devDependencies": {
|
|
37
45
|
"@types/react": "^19.0.10"
|
|
38
46
|
},
|
|
39
|
-
"packageManager": "pnpm@9.0.0"
|
|
40
|
-
"exports": {
|
|
41
|
-
".": {
|
|
42
|
-
"import": "./dist/index.js",
|
|
43
|
-
"require": "./dist/index.cjs",
|
|
44
|
-
"types": "./dist/index.d.ts",
|
|
45
|
-
"default": "./dist/index.js"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
47
|
+
"packageManager": "pnpm@9.0.0"
|
|
48
48
|
}
|