@context-query/core 0.2.1 → 0.3.2-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ import { AtomListener, Subscription } from "./types";
2
+ export declare class AtomStore<T> {
3
+ private value;
4
+ private listeners;
5
+ constructor(initialValue: T);
6
+ getValue(): T;
7
+ setValue(newValue: T): void;
8
+ private notifyListeners;
9
+ subscribe(listener: AtomListener<T>): Subscription;
10
+ }
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "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))}}}};
1
+ "use strict";class t{value;listeners;constructor(t){this.value=t,this.listeners=new Set}getValue(){return this.value}setValue(t){Object.is(this.value,t)||(this.value=t,this.notifyListeners())}notifyListeners(){this.listeners.forEach((t=>t(this.value)))}subscribe(t){this.listeners.add(t);return{unsubscribe:()=>{this.listeners.delete(t)}}}}exports.AtomStore=t,exports.ContextQueryStore=class{atoms;constructor(e){this.atoms=new Map,Object.entries(e).forEach((([e,s])=>{this.atoms.set(e,new t(s))}))}getAtomValue(t){const e=this.atoms.get(t);if(!e)throw new Error(`Atom with key "${String(t)}" not found`);return e.getValue()}setAtomValue(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key "${String(t)}" not found`);s.setValue(e)}subscribeToAtom(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key "${String(t)}" not found`);return s.subscribe((s=>{e(t,s)}))}getAllAtomValues(){const t={};return this.atoms.forEach(((e,s)=>{t[s]=e.getValue()})),t}updateAllAtoms(t){Object.entries(t).forEach((([t,e])=>{const s=this.atoms.get(t);s&&s.setValue(e)}))}};
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/store.ts"],"sourcesContent":["import { Listener, Subscription, TStateImpl } from \"./types\";\n\nexport class ContextQueryStore<TState extends TStateImpl> {\n private state: TState;\n private listeners: Map<keyof TState, Set<Listener<any>>>;\n\n constructor(initialState: TState) {\n this.state = initialState;\n this.listeners = new Map();\n }\n\n public getState(): TState {\n return this.state;\n }\n\n public getStateByKey<TKey extends keyof TState>(key: TKey): TState[TKey] {\n return this.state[key];\n }\n\n public updateState<TKey extends keyof TState>(state: TState): void {\n const prevState = { ...this.state };\n this.state = { ...state };\n const keys = Object.keys(state) as TKey[];\n\n keys.forEach((key: TKey) => {\n if (!Object.is(prevState[key], this.state[key])) {\n this.notifyListeners(key);\n }\n });\n }\n\n public setState<TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey]\n ): void {\n if (Object.is(this.state[key], value)) {\n return;\n }\n\n this.state = { ...this.state, [key]: value };\n\n this.notifyListeners(key);\n }\n\n private notifyListeners<TKey extends keyof TState>(key: TKey) {\n const listeners = this.listeners.get(key);\n if (listeners) {\n const value = this.state[key];\n listeners.forEach((listener) => listener(value));\n }\n }\n\n public subscribe<TKey extends keyof TState>(\n key: TKey,\n listener: (key: TKey, value: TState[TKey]) => void\n ): Subscription {\n const keyListener = (value: TState[TKey]) => {\n listener(key, value);\n };\n\n if (!this.listeners.has(key)) {\n this.listeners.set(key, new Set());\n }\n\n this.listeners.get(key)!.add(keyListener);\n\n const unsubscribe = () => {\n const keyListeners = this.listeners.get(key);\n if (keyListeners) {\n keyListeners.delete(keyListener);\n if (keyListeners.size === 0) {\n this.listeners.delete(key);\n }\n }\n };\n\n return {\n unsubscribe,\n };\n }\n}\n"],"names":["state","listeners","constructor","initialState","this","Map","getState","getStateByKey","key","updateState","prevState","Object","keys","forEach","is","notifyListeners","setState","value","get","listener","subscribe","keyListener","has","set","Set","add","unsubscribe","keyListeners","delete","size"],"mappings":"6CAGUA,MACAC,UAER,WAAAC,CAAYC,GACVC,KAAKJ,MAAQG,EACbC,KAAKH,UAAY,IAAII,IAGhB,QAAAC,GACL,OAAOF,KAAKJ,MAGP,aAAAO,CAAyCC,GAC9C,OAAOJ,KAAKJ,MAAMQ,GAGb,WAAAC,CAAuCT,GAC5C,MAAMU,EAAY,IAAKN,KAAKJ,OAC5BI,KAAKJ,MAAQ,IAAKA,GACLW,OAAOC,KAAKZ,GAEpBa,SAASL,IACPG,OAAOG,GAAGJ,EAAUF,GAAMJ,KAAKJ,MAAMQ,KACxCJ,KAAKW,gBAAgBP,MAKpB,QAAAQ,CACLR,EACAS,GAEIN,OAAOG,GAAGV,KAAKJ,MAAMQ,GAAMS,KAI/Bb,KAAKJ,MAAQ,IAAKI,KAAKJ,MAAOQ,CAACA,GAAMS,GAErCb,KAAKW,gBAAgBP,IAGf,eAAAO,CAA2CP,GACjD,MAAMP,EAAYG,KAAKH,UAAUiB,IAAIV,GACrC,GAAIP,EAAW,CACb,MAAMgB,EAAQb,KAAKJ,MAAMQ,GACzBP,EAAUY,SAASM,GAAaA,EAASF,MAItC,SAAAG,CACLZ,EACAW,GAEA,MAAME,EAAeJ,IACnBE,EAASX,EAAKS,EAAM,EAGjBb,KAAKH,UAAUqB,IAAId,IACtBJ,KAAKH,UAAUsB,IAAIf,EAAK,IAAIgB,KAG9BpB,KAAKH,UAAUiB,IAAIV,GAAMiB,IAAIJ,GAY7B,MAAO,CACLK,YAXkB,KAClB,MAAMC,EAAevB,KAAKH,UAAUiB,IAAIV,GACpCmB,IACFA,EAAaC,OAAOP,GACM,IAAtBM,EAAaE,MACfzB,KAAKH,UAAU2B,OAAOpB"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/atom-store.ts","../src/store.ts"],"sourcesContent":["import { AtomListener, Subscription } from \"./types\";\n\nexport class AtomStore<T> {\n private value: T;\n private listeners: Set<AtomListener<T>>;\n\n constructor(initialValue: T) {\n this.value = initialValue;\n this.listeners = new Set();\n }\n\n public getValue(): T {\n return this.value;\n }\n\n public setValue(newValue: T): void {\n if (Object.is(this.value, newValue)) {\n return;\n }\n\n this.value = newValue;\n this.notifyListeners();\n }\n\n private notifyListeners(): void {\n this.listeners.forEach((listener) => listener(this.value));\n }\n\n public subscribe(listener: AtomListener<T>): Subscription {\n this.listeners.add(listener);\n\n const unsubscribe = () => {\n this.listeners.delete(listener);\n };\n\n return {\n unsubscribe,\n };\n }\n}","import { AtomStore } from \"./atom-store\";\nimport { Subscription } from \"./types\";\n\nexport class ContextQueryStore<TAtoms extends Record<string, any>> {\n private atoms: Map<keyof TAtoms, AtomStore<any>>;\n\n constructor(initialValues: TAtoms) {\n this.atoms = new Map();\n\n Object.entries(initialValues).forEach(([key, value]) => {\n this.atoms.set(key as keyof TAtoms, new AtomStore(value));\n });\n }\n\n public getAtomValue<TKey extends keyof TAtoms>(key: TKey): TAtoms[TKey] {\n const atomStore = this.atoms.get(key);\n if (!atomStore) {\n throw new Error(`Atom with key \"${String(key)}\" not found`);\n }\n return atomStore.getValue();\n }\n\n public setAtomValue<TKey extends keyof TAtoms>(\n key: TKey,\n value: TAtoms[TKey]\n ): void {\n const atomStore = this.atoms.get(key);\n if (!atomStore) {\n throw new Error(`Atom with key \"${String(key)}\" not found`);\n }\n atomStore.setValue(value);\n }\n\n public subscribeToAtom<TKey extends keyof TAtoms>(\n key: TKey,\n listener: (key: TKey, value: TAtoms[TKey]) => void\n ): Subscription {\n const atomStore = this.atoms.get(key);\n if (!atomStore) {\n throw new Error(`Atom with key \"${String(key)}\" not found`);\n }\n\n const atomListener = (value: TAtoms[TKey]) => {\n listener(key, value);\n };\n\n return atomStore.subscribe(atomListener);\n }\n\n public getAllAtomValues(): TAtoms {\n const result = {} as TAtoms;\n\n this.atoms.forEach((atomStore, key) => {\n result[key] = atomStore.getValue();\n });\n\n return result;\n }\n\n public updateAllAtoms(newAtoms: Partial<TAtoms>): void {\n Object.entries(newAtoms).forEach(([key, value]) => {\n const atomStore = this.atoms.get(key as keyof TAtoms);\n if (atomStore) {\n atomStore.setValue(value);\n }\n });\n }\n}\n"],"names":["AtomStore","value","listeners","constructor","initialValue","this","Set","getValue","setValue","newValue","Object","is","notifyListeners","forEach","listener","subscribe","add","unsubscribe","delete","atoms","initialValues","Map","entries","key","set","getAtomValue","atomStore","get","Error","String","setAtomValue","subscribeToAtom","getAllAtomValues","result","updateAllAtoms","newAtoms"],"mappings":"mBAEaA,EACHC,MACAC,UAER,WAAAC,CAAYC,GACVC,KAAKJ,MAAQG,EACbC,KAAKH,UAAY,IAAII,IAGhB,QAAAC,GACL,OAAOF,KAAKJ,MAGP,QAAAO,CAASC,GACVC,OAAOC,GAAGN,KAAKJ,MAAOQ,KAI1BJ,KAAKJ,MAAQQ,EACbJ,KAAKO,mBAGC,eAAAA,GACNP,KAAKH,UAAUW,SAASC,GAAaA,EAAST,KAAKJ,SAG9C,SAAAc,CAAUD,GACfT,KAAKH,UAAUc,IAAIF,GAMnB,MAAO,CACLG,YALkB,KAClBZ,KAAKH,UAAUgB,OAAOJ,EAAS,wDC5B3BK,MAER,WAAAhB,CAAYiB,GACVf,KAAKc,MAAQ,IAAIE,IAEjBX,OAAOY,QAAQF,GAAeP,SAAQ,EAAEU,EAAKtB,MAC3CI,KAAKc,MAAMK,IAAID,EAAqB,IAAIvB,EAAUC,GAAO,IAItD,YAAAwB,CAAwCF,GAC7C,MAAMG,EAAYrB,KAAKc,MAAMQ,IAAIJ,GACjC,IAAKG,EACH,MAAM,IAAIE,MAAM,kBAAkBC,OAAON,iBAE3C,OAAOG,EAAUnB,WAGZ,YAAAuB,CACLP,EACAtB,GAEA,MAAMyB,EAAYrB,KAAKc,MAAMQ,IAAIJ,GACjC,IAAKG,EACH,MAAM,IAAIE,MAAM,kBAAkBC,OAAON,iBAE3CG,EAAUlB,SAASP,GAGd,eAAA8B,CACLR,EACAT,GAEA,MAAMY,EAAYrB,KAAKc,MAAMQ,IAAIJ,GACjC,IAAKG,EACH,MAAM,IAAIE,MAAM,kBAAkBC,OAAON,iBAO3C,OAAOG,EAAUX,WAJKd,IACpBa,EAASS,EAAKtB,EAAM,IAMjB,gBAAA+B,GACL,MAAMC,EAAS,CAAY,EAM3B,OAJA5B,KAAKc,MAAMN,SAAQ,CAACa,EAAWH,KAC7BU,EAAOV,GAAOG,EAAUnB,UAAU,IAG7B0B,EAGF,cAAAC,CAAeC,GACpBzB,OAAOY,QAAQa,GAAUtB,SAAQ,EAAEU,EAAKtB,MACtC,MAAMyB,EAAYrB,KAAKc,MAAMQ,IAAIJ,GAC7BG,GACFA,EAAUlB,SAASP"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { ContextQueryStore } from "./store";
2
+ export { AtomStore } from "./atom-store";
2
3
  export * from "./types";
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- 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};
1
+ class t{value;listeners;constructor(t){this.value=t,this.listeners=new Set}getValue(){return this.value}setValue(t){Object.is(this.value,t)||(this.value=t,this.notifyListeners())}notifyListeners(){this.listeners.forEach((t=>t(this.value)))}subscribe(t){this.listeners.add(t);return{unsubscribe:()=>{this.listeners.delete(t)}}}}class e{atoms;constructor(e){this.atoms=new Map,Object.entries(e).forEach((([e,s])=>{this.atoms.set(e,new t(s))}))}getAtomValue(t){const e=this.atoms.get(t);if(!e)throw new Error(`Atom with key "${String(t)}" not found`);return e.getValue()}setAtomValue(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key "${String(t)}" not found`);s.setValue(e)}subscribeToAtom(t,e){const s=this.atoms.get(t);if(!s)throw new Error(`Atom with key "${String(t)}" not found`);return s.subscribe((s=>{e(t,s)}))}getAllAtomValues(){const t={};return this.atoms.forEach(((e,s)=>{t[s]=e.getValue()})),t}updateAllAtoms(t){Object.entries(t).forEach((([t,e])=>{const s=this.atoms.get(t);s&&s.setValue(e)}))}}export{t as AtomStore,e as ContextQueryStore};
2
2
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/store.ts"],"sourcesContent":["import { Listener, Subscription, TStateImpl } from \"./types\";\n\nexport class ContextQueryStore<TState extends TStateImpl> {\n private state: TState;\n private listeners: Map<keyof TState, Set<Listener<any>>>;\n\n constructor(initialState: TState) {\n this.state = initialState;\n this.listeners = new Map();\n }\n\n public getState(): TState {\n return this.state;\n }\n\n public getStateByKey<TKey extends keyof TState>(key: TKey): TState[TKey] {\n return this.state[key];\n }\n\n public updateState<TKey extends keyof TState>(state: TState): void {\n const prevState = { ...this.state };\n this.state = { ...state };\n const keys = Object.keys(state) as TKey[];\n\n keys.forEach((key: TKey) => {\n if (!Object.is(prevState[key], this.state[key])) {\n this.notifyListeners(key);\n }\n });\n }\n\n public setState<TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey]\n ): void {\n if (Object.is(this.state[key], value)) {\n return;\n }\n\n this.state = { ...this.state, [key]: value };\n\n this.notifyListeners(key);\n }\n\n private notifyListeners<TKey extends keyof TState>(key: TKey) {\n const listeners = this.listeners.get(key);\n if (listeners) {\n const value = this.state[key];\n listeners.forEach((listener) => listener(value));\n }\n }\n\n public subscribe<TKey extends keyof TState>(\n key: TKey,\n listener: (key: TKey, value: TState[TKey]) => void\n ): Subscription {\n const keyListener = (value: TState[TKey]) => {\n listener(key, value);\n };\n\n if (!this.listeners.has(key)) {\n this.listeners.set(key, new Set());\n }\n\n this.listeners.get(key)!.add(keyListener);\n\n const unsubscribe = () => {\n const keyListeners = this.listeners.get(key);\n if (keyListeners) {\n keyListeners.delete(keyListener);\n if (keyListeners.size === 0) {\n this.listeners.delete(key);\n }\n }\n };\n\n return {\n unsubscribe,\n };\n }\n}\n"],"names":["ContextQueryStore","state","listeners","constructor","initialState","this","Map","getState","getStateByKey","key","updateState","prevState","Object","keys","forEach","is","notifyListeners","setState","value","get","listener","subscribe","keyListener","has","set","Set","add","unsubscribe","keyListeners","delete","size"],"mappings":"MAEaA,EACHC,MACAC,UAER,WAAAC,CAAYC,GACVC,KAAKJ,MAAQG,EACbC,KAAKH,UAAY,IAAII,IAGhB,QAAAC,GACL,OAAOF,KAAKJ,MAGP,aAAAO,CAAyCC,GAC9C,OAAOJ,KAAKJ,MAAMQ,GAGb,WAAAC,CAAuCT,GAC5C,MAAMU,EAAY,IAAKN,KAAKJ,OAC5BI,KAAKJ,MAAQ,IAAKA,GACLW,OAAOC,KAAKZ,GAEpBa,SAASL,IACPG,OAAOG,GAAGJ,EAAUF,GAAMJ,KAAKJ,MAAMQ,KACxCJ,KAAKW,gBAAgBP,MAKpB,QAAAQ,CACLR,EACAS,GAEIN,OAAOG,GAAGV,KAAKJ,MAAMQ,GAAMS,KAI/Bb,KAAKJ,MAAQ,IAAKI,KAAKJ,MAAOQ,CAACA,GAAMS,GAErCb,KAAKW,gBAAgBP,IAGf,eAAAO,CAA2CP,GACjD,MAAMP,EAAYG,KAAKH,UAAUiB,IAAIV,GACrC,GAAIP,EAAW,CACb,MAAMgB,EAAQb,KAAKJ,MAAMQ,GACzBP,EAAUY,SAASM,GAAaA,EAASF,MAItC,SAAAG,CACLZ,EACAW,GAEA,MAAME,EAAeJ,IACnBE,EAASX,EAAKS,EAAM,EAGjBb,KAAKH,UAAUqB,IAAId,IACtBJ,KAAKH,UAAUsB,IAAIf,EAAK,IAAIgB,KAG9BpB,KAAKH,UAAUiB,IAAIV,GAAMiB,IAAIJ,GAY7B,MAAO,CACLK,YAXkB,KAClB,MAAMC,EAAevB,KAAKH,UAAUiB,IAAIV,GACpCmB,IACFA,EAAaC,OAAOP,GACM,IAAtBM,EAAaE,MACfzB,KAAKH,UAAU2B,OAAOpB"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/atom-store.ts","../src/store.ts"],"sourcesContent":["import { AtomListener, Subscription } from \"./types\";\n\nexport class AtomStore<T> {\n private value: T;\n private listeners: Set<AtomListener<T>>;\n\n constructor(initialValue: T) {\n this.value = initialValue;\n this.listeners = new Set();\n }\n\n public getValue(): T {\n return this.value;\n }\n\n public setValue(newValue: T): void {\n if (Object.is(this.value, newValue)) {\n return;\n }\n\n this.value = newValue;\n this.notifyListeners();\n }\n\n private notifyListeners(): void {\n this.listeners.forEach((listener) => listener(this.value));\n }\n\n public subscribe(listener: AtomListener<T>): Subscription {\n this.listeners.add(listener);\n\n const unsubscribe = () => {\n this.listeners.delete(listener);\n };\n\n return {\n unsubscribe,\n };\n }\n}","import { AtomStore } from \"./atom-store\";\nimport { Subscription } from \"./types\";\n\nexport class ContextQueryStore<TAtoms extends Record<string, any>> {\n private atoms: Map<keyof TAtoms, AtomStore<any>>;\n\n constructor(initialValues: TAtoms) {\n this.atoms = new Map();\n\n Object.entries(initialValues).forEach(([key, value]) => {\n this.atoms.set(key as keyof TAtoms, new AtomStore(value));\n });\n }\n\n public getAtomValue<TKey extends keyof TAtoms>(key: TKey): TAtoms[TKey] {\n const atomStore = this.atoms.get(key);\n if (!atomStore) {\n throw new Error(`Atom with key \"${String(key)}\" not found`);\n }\n return atomStore.getValue();\n }\n\n public setAtomValue<TKey extends keyof TAtoms>(\n key: TKey,\n value: TAtoms[TKey]\n ): void {\n const atomStore = this.atoms.get(key);\n if (!atomStore) {\n throw new Error(`Atom with key \"${String(key)}\" not found`);\n }\n atomStore.setValue(value);\n }\n\n public subscribeToAtom<TKey extends keyof TAtoms>(\n key: TKey,\n listener: (key: TKey, value: TAtoms[TKey]) => void\n ): Subscription {\n const atomStore = this.atoms.get(key);\n if (!atomStore) {\n throw new Error(`Atom with key \"${String(key)}\" not found`);\n }\n\n const atomListener = (value: TAtoms[TKey]) => {\n listener(key, value);\n };\n\n return atomStore.subscribe(atomListener);\n }\n\n public getAllAtomValues(): TAtoms {\n const result = {} as TAtoms;\n\n this.atoms.forEach((atomStore, key) => {\n result[key] = atomStore.getValue();\n });\n\n return result;\n }\n\n public updateAllAtoms(newAtoms: Partial<TAtoms>): void {\n Object.entries(newAtoms).forEach(([key, value]) => {\n const atomStore = this.atoms.get(key as keyof TAtoms);\n if (atomStore) {\n atomStore.setValue(value);\n }\n });\n }\n}\n"],"names":["AtomStore","value","listeners","constructor","initialValue","this","Set","getValue","setValue","newValue","Object","is","notifyListeners","forEach","listener","subscribe","add","unsubscribe","delete","ContextQueryStore","atoms","initialValues","Map","entries","key","set","getAtomValue","atomStore","get","Error","String","setAtomValue","subscribeToAtom","getAllAtomValues","result","updateAllAtoms","newAtoms"],"mappings":"MAEaA,EACHC,MACAC,UAER,WAAAC,CAAYC,GACVC,KAAKJ,MAAQG,EACbC,KAAKH,UAAY,IAAII,IAGhB,QAAAC,GACL,OAAOF,KAAKJ,MAGP,QAAAO,CAASC,GACVC,OAAOC,GAAGN,KAAKJ,MAAOQ,KAI1BJ,KAAKJ,MAAQQ,EACbJ,KAAKO,mBAGC,eAAAA,GACNP,KAAKH,UAAUW,SAASC,GAAaA,EAAST,KAAKJ,SAG9C,SAAAc,CAAUD,GACfT,KAAKH,UAAUc,IAAIF,GAMnB,MAAO,CACLG,YALkB,KAClBZ,KAAKH,UAAUgB,OAAOJ,EAAS,UC7BxBK,EACHC,MAER,WAAAjB,CAAYkB,GACVhB,KAAKe,MAAQ,IAAIE,IAEjBZ,OAAOa,QAAQF,GAAeR,SAAQ,EAAEW,EAAKvB,MAC3CI,KAAKe,MAAMK,IAAID,EAAqB,IAAIxB,EAAUC,GAAO,IAItD,YAAAyB,CAAwCF,GAC7C,MAAMG,EAAYtB,KAAKe,MAAMQ,IAAIJ,GACjC,IAAKG,EACH,MAAM,IAAIE,MAAM,kBAAkBC,OAAON,iBAE3C,OAAOG,EAAUpB,WAGZ,YAAAwB,CACLP,EACAvB,GAEA,MAAM0B,EAAYtB,KAAKe,MAAMQ,IAAIJ,GACjC,IAAKG,EACH,MAAM,IAAIE,MAAM,kBAAkBC,OAAON,iBAE3CG,EAAUnB,SAASP,GAGd,eAAA+B,CACLR,EACAV,GAEA,MAAMa,EAAYtB,KAAKe,MAAMQ,IAAIJ,GACjC,IAAKG,EACH,MAAM,IAAIE,MAAM,kBAAkBC,OAAON,iBAO3C,OAAOG,EAAUZ,WAJKd,IACpBa,EAASU,EAAKvB,EAAM,IAMjB,gBAAAgC,GACL,MAAMC,EAAS,CAAY,EAM3B,OAJA7B,KAAKe,MAAMP,SAAQ,CAACc,EAAWH,KAC7BU,EAAOV,GAAOG,EAAUpB,UAAU,IAG7B2B,EAGF,cAAAC,CAAeC,GACpB1B,OAAOa,QAAQa,GAAUvB,SAAQ,EAAEW,EAAKvB,MACtC,MAAM0B,EAAYtB,KAAKe,MAAMQ,IAAIJ,GAC7BG,GACFA,EAAUnB,SAASP"}
package/dist/store.d.ts CHANGED
@@ -1,12 +1,10 @@
1
- import { Subscription, TStateImpl } from "./types";
2
- export declare class ContextQueryStore<TState extends TStateImpl> {
3
- private state;
4
- private listeners;
5
- constructor(initialState: TState);
6
- getState(): TState;
7
- getStateByKey<TKey extends keyof TState>(key: TKey): TState[TKey];
8
- updateState<TKey extends keyof TState>(state: TState): void;
9
- setState<TKey extends keyof TState>(key: TKey, value: TState[TKey]): void;
10
- private notifyListeners;
11
- subscribe<TKey extends keyof TState>(key: TKey, listener: (key: TKey, value: TState[TKey]) => void): Subscription;
1
+ import { Subscription } from "./types";
2
+ export declare class ContextQueryStore<TAtoms extends Record<string, any>> {
3
+ private atoms;
4
+ constructor(initialValues: TAtoms);
5
+ getAtomValue<TKey extends keyof TAtoms>(key: TKey): TAtoms[TKey];
6
+ setAtomValue<TKey extends keyof TAtoms>(key: TKey, value: TAtoms[TKey]): void;
7
+ subscribeToAtom<TKey extends keyof TAtoms>(key: TKey, listener: (key: TKey, value: TAtoms[TKey]) => void): Subscription;
8
+ getAllAtomValues(): TAtoms;
9
+ updateAllAtoms(newAtoms: Partial<TAtoms>): void;
12
10
  }
package/dist/types.d.ts CHANGED
@@ -2,5 +2,7 @@ export type Subscription = {
2
2
  unsubscribe: () => void;
3
3
  };
4
4
  export type Listener<T> = (value: T) => void;
5
- export type Updater<TState> = (state: TState) => Partial<TState>;
5
+ export type AtomListener<T> = (value: T) => void;
6
+ export type Updater<TAtoms> = (atoms: TAtoms) => Partial<TAtoms>;
6
7
  export type TStateImpl = Record<string, any>;
8
+ export type TAtoms = Record<string, any>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@context-query/core",
3
- "version": "0.2.1",
3
+ "version": "0.3.2-beta.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "scripts": {
package/dist/index.esm.js DELETED
@@ -1,2 +0,0 @@
1
- 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};
2
- //# sourceMappingURL=index.esm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.esm.js","sources":["../src/store.ts"],"sourcesContent":["import { Listener, Subscription, TStateImpl } from \"./types\";\n\nexport class ContextQueryStore<TState extends TStateImpl> {\n private state: TState;\n private listeners: Map<keyof TState, Set<Listener<any>>>;\n\n constructor(initialState: TState) {\n this.state = initialState;\n this.listeners = new Map();\n }\n\n public getState(): TState {\n return this.state;\n }\n\n public getStateByKey<TKey extends keyof TState>(key: TKey): TState[TKey] {\n return this.state[key];\n }\n\n public updateState<TKey extends keyof TState>(state: TState): void {\n const prevState = { ...this.state };\n this.state = { ...state };\n const keys = Object.keys(state) as TKey[];\n\n keys.forEach((key: TKey) => {\n if (!Object.is(prevState[key], this.state[key])) {\n this.notifyListeners(key);\n }\n });\n }\n\n public setState<TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey]\n ): void {\n if (Object.is(this.state[key], value)) {\n return;\n }\n\n this.state = { ...this.state, [key]: value };\n\n this.notifyListeners(key);\n }\n\n private notifyListeners<TKey extends keyof TState>(key: TKey) {\n const listeners = this.listeners.get(key);\n if (listeners) {\n const value = this.state[key];\n listeners.forEach((listener) => listener(value));\n }\n }\n\n public subscribe<TKey extends keyof TState>(\n key: TKey,\n listener: (key: TKey, value: TState[TKey]) => void\n ): Subscription {\n const keyListener = (value: TState[TKey]) => {\n listener(key, value);\n };\n\n if (!this.listeners.has(key)) {\n this.listeners.set(key, new Set());\n }\n\n this.listeners.get(key)!.add(keyListener);\n\n const unsubscribe = () => {\n const keyListeners = this.listeners.get(key);\n if (keyListeners) {\n keyListeners.delete(keyListener);\n if (keyListeners.size === 0) {\n this.listeners.delete(key);\n }\n }\n };\n\n return {\n unsubscribe,\n };\n }\n}\n"],"names":["ContextQueryStore","state","listeners","constructor","initialState","this","Map","getState","getStateByKey","key","updateState","prevState","Object","keys","forEach","is","notifyListeners","setState","value","get","listener","subscribe","keyListener","has","set","Set","add","unsubscribe","keyListeners","delete","size"],"mappings":"MAEaA,EACHC,MACAC,UAER,WAAAC,CAAYC,GACVC,KAAKJ,MAAQG,EACbC,KAAKH,UAAY,IAAII,IAGhB,QAAAC,GACL,OAAOF,KAAKJ,MAGP,aAAAO,CAAyCC,GAC9C,OAAOJ,KAAKJ,MAAMQ,GAGb,WAAAC,CAAuCT,GAC5C,MAAMU,EAAY,IAAKN,KAAKJ,OAC5BI,KAAKJ,MAAQ,IAAKA,GACLW,OAAOC,KAAKZ,GAEpBa,SAASL,IACPG,OAAOG,GAAGJ,EAAUF,GAAMJ,KAAKJ,MAAMQ,KACxCJ,KAAKW,gBAAgBP,MAKpB,QAAAQ,CACLR,EACAS,GAEIN,OAAOG,GAAGV,KAAKJ,MAAMQ,GAAMS,KAI/Bb,KAAKJ,MAAQ,IAAKI,KAAKJ,MAAOQ,CAACA,GAAMS,GAErCb,KAAKW,gBAAgBP,IAGf,eAAAO,CAA2CP,GACjD,MAAMP,EAAYG,KAAKH,UAAUiB,IAAIV,GACrC,GAAIP,EAAW,CACb,MAAMgB,EAAQb,KAAKJ,MAAMQ,GACzBP,EAAUY,SAASM,GAAaA,EAASF,MAItC,SAAAG,CACLZ,EACAW,GAEA,MAAME,EAAeJ,IACnBE,EAASX,EAAKS,EAAM,EAGjBb,KAAKH,UAAUqB,IAAId,IACtBJ,KAAKH,UAAUsB,IAAIf,EAAK,IAAIgB,KAG9BpB,KAAKH,UAAUiB,IAAIV,GAAMiB,IAAIJ,GAY7B,MAAO,CACLK,YAXkB,KAClB,MAAMC,EAAevB,KAAKH,UAAUiB,IAAIV,GACpCmB,IACFA,EAAaC,OAAOP,GACM,IAAtBM,EAAaE,MACfzB,KAAKH,UAAU2B,OAAOpB"}
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- "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))}}}};
2
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sources":["../src/store.ts"],"sourcesContent":["import { Listener, Subscription, TStateImpl } from \"./types\";\n\nexport class ContextQueryStore<TState extends TStateImpl> {\n private state: TState;\n private listeners: Map<keyof TState, Set<Listener<any>>>;\n\n constructor(initialState: TState) {\n this.state = initialState;\n this.listeners = new Map();\n }\n\n public getState(): TState {\n return this.state;\n }\n\n public getStateByKey<TKey extends keyof TState>(key: TKey): TState[TKey] {\n return this.state[key];\n }\n\n public updateState<TKey extends keyof TState>(state: TState): void {\n const prevState = { ...this.state };\n this.state = { ...state };\n const keys = Object.keys(state) as TKey[];\n\n keys.forEach((key: TKey) => {\n if (!Object.is(prevState[key], this.state[key])) {\n this.notifyListeners(key);\n }\n });\n }\n\n public setState<TKey extends keyof TState>(\n key: TKey,\n value: TState[TKey]\n ): void {\n if (Object.is(this.state[key], value)) {\n return;\n }\n\n this.state = { ...this.state, [key]: value };\n\n this.notifyListeners(key);\n }\n\n private notifyListeners<TKey extends keyof TState>(key: TKey) {\n const listeners = this.listeners.get(key);\n if (listeners) {\n const value = this.state[key];\n listeners.forEach((listener) => listener(value));\n }\n }\n\n public subscribe<TKey extends keyof TState>(\n key: TKey,\n listener: (key: TKey, value: TState[TKey]) => void\n ): Subscription {\n const keyListener = (value: TState[TKey]) => {\n listener(key, value);\n };\n\n if (!this.listeners.has(key)) {\n this.listeners.set(key, new Set());\n }\n\n this.listeners.get(key)!.add(keyListener);\n\n const unsubscribe = () => {\n const keyListeners = this.listeners.get(key);\n if (keyListeners) {\n keyListeners.delete(keyListener);\n if (keyListeners.size === 0) {\n this.listeners.delete(key);\n }\n }\n };\n\n return {\n unsubscribe,\n };\n }\n}\n"],"names":["state","listeners","constructor","initialState","this","Map","getState","getStateByKey","key","updateState","prevState","Object","keys","forEach","is","notifyListeners","setState","value","get","listener","subscribe","keyListener","has","set","Set","add","unsubscribe","keyListeners","delete","size"],"mappings":"6CAGUA,MACAC,UAER,WAAAC,CAAYC,GACVC,KAAKJ,MAAQG,EACbC,KAAKH,UAAY,IAAII,IAGhB,QAAAC,GACL,OAAOF,KAAKJ,MAGP,aAAAO,CAAyCC,GAC9C,OAAOJ,KAAKJ,MAAMQ,GAGb,WAAAC,CAAuCT,GAC5C,MAAMU,EAAY,IAAKN,KAAKJ,OAC5BI,KAAKJ,MAAQ,IAAKA,GACLW,OAAOC,KAAKZ,GAEpBa,SAASL,IACPG,OAAOG,GAAGJ,EAAUF,GAAMJ,KAAKJ,MAAMQ,KACxCJ,KAAKW,gBAAgBP,MAKpB,QAAAQ,CACLR,EACAS,GAEIN,OAAOG,GAAGV,KAAKJ,MAAMQ,GAAMS,KAI/Bb,KAAKJ,MAAQ,IAAKI,KAAKJ,MAAOQ,CAACA,GAAMS,GAErCb,KAAKW,gBAAgBP,IAGf,eAAAO,CAA2CP,GACjD,MAAMP,EAAYG,KAAKH,UAAUiB,IAAIV,GACrC,GAAIP,EAAW,CACb,MAAMgB,EAAQb,KAAKJ,MAAMQ,GACzBP,EAAUY,SAASM,GAAaA,EAASF,MAItC,SAAAG,CACLZ,EACAW,GAEA,MAAME,EAAeJ,IACnBE,EAASX,EAAKS,EAAM,EAGjBb,KAAKH,UAAUqB,IAAId,IACtBJ,KAAKH,UAAUsB,IAAIf,EAAK,IAAIgB,KAG9BpB,KAAKH,UAAUiB,IAAIV,GAAMiB,IAAIJ,GAY7B,MAAO,CACLK,YAXkB,KAClB,MAAMC,EAAevB,KAAKH,UAAUiB,IAAIV,GACpCmB,IACFA,EAAaC,OAAOP,GACM,IAAtBM,EAAaE,MACfzB,KAAKH,UAAU2B,OAAOpB"}