@context-query/core 0.2.0-dev.5 → 0.2.0

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.mjs CHANGED
@@ -1,2 +1,106 @@
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.mjs.map
1
+ // src/index.ts
2
+ function createStore(initialState) {
3
+ const store = {
4
+ state: initialState,
5
+ listeners: /* @__PURE__ */ new Set()
6
+ };
7
+ const getState = () => store.state;
8
+ const setState = (updater) => {
9
+ const nextState = typeof updater === "function" ? updater(store.state) : updater;
10
+ if (nextState !== store.state) {
11
+ store.state = nextState;
12
+ notifyListeners();
13
+ }
14
+ };
15
+ const subscribe = (listener) => {
16
+ store.listeners.add(listener);
17
+ return () => {
18
+ store.listeners.delete(listener);
19
+ };
20
+ };
21
+ const notifyListeners = () => {
22
+ store.listeners.forEach((listener) => listener());
23
+ };
24
+ return {
25
+ getState,
26
+ setState,
27
+ subscribe
28
+ };
29
+ }
30
+ function batch(updates) {
31
+ updates.forEach((update) => update());
32
+ }
33
+ function createKeyedStore(initialState) {
34
+ const state = { ...initialState };
35
+ const keyListeners = /* @__PURE__ */ new Map();
36
+ const globalListeners = /* @__PURE__ */ new Set();
37
+ const getState = () => ({ ...state });
38
+ const getStateByKey = (key) => state[key];
39
+ const setState = (key, updater) => {
40
+ const prevValue = state[key];
41
+ const nextValue = typeof updater === "function" ? updater(prevValue) : updater;
42
+ if (prevValue !== nextValue) {
43
+ state[key] = nextValue;
44
+ notifyKeyListeners(key);
45
+ notifyGlobalListeners();
46
+ }
47
+ };
48
+ const subscribeToKey = (key, listener) => {
49
+ if (!keyListeners.has(key)) {
50
+ keyListeners.set(key, /* @__PURE__ */ new Set());
51
+ }
52
+ keyListeners.get(key).add(listener);
53
+ return () => {
54
+ const listeners = keyListeners.get(key);
55
+ if (listeners) {
56
+ listeners.delete(listener);
57
+ if (listeners.size === 0) {
58
+ keyListeners.delete(key);
59
+ }
60
+ }
61
+ };
62
+ };
63
+ const subscribeToAll = (listener) => {
64
+ globalListeners.add(listener);
65
+ return () => {
66
+ globalListeners.delete(listener);
67
+ };
68
+ };
69
+ const notifyKeyListeners = (key) => {
70
+ const listeners = keyListeners.get(key);
71
+ if (listeners) {
72
+ listeners.forEach((listener) => listener());
73
+ }
74
+ };
75
+ const notifyGlobalListeners = () => {
76
+ globalListeners.forEach((listener) => listener());
77
+ };
78
+ const batchUpdate = (updates) => {
79
+ const updatedKeys = /* @__PURE__ */ new Set();
80
+ updates.forEach(([key, updater]) => {
81
+ const prevValue = state[key];
82
+ const nextValue = typeof updater === "function" ? updater(prevValue) : updater;
83
+ if (prevValue !== nextValue) {
84
+ state[key] = nextValue;
85
+ updatedKeys.add(key);
86
+ }
87
+ });
88
+ updatedKeys.forEach((key) => notifyKeyListeners(key));
89
+ if (updatedKeys.size > 0) {
90
+ notifyGlobalListeners();
91
+ }
92
+ };
93
+ return {
94
+ getState,
95
+ getStateByKey,
96
+ setState,
97
+ subscribeToKey,
98
+ subscribeToAll,
99
+ batchUpdate
100
+ };
101
+ }
102
+ export {
103
+ batch,
104
+ createKeyedStore,
105
+ createStore
106
+ };
package/package.json CHANGED
@@ -1,24 +1,24 @@
1
1
  {
2
2
  "name": "@context-query/core",
3
- "version": "0.2.0-dev.5",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "scripts": {
7
7
  "build": "rollup -c",
8
8
  "dev": "rollup -c -w"
9
9
  },
10
- "main": "./dist/index.cjs",
11
- "module": "./dist/index.mjs",
12
- "types": "./dist/index.d.ts",
10
+ "main": "dist/index.js",
11
+ "module": "dist/index.esm.js",
12
+ "types": "dist/index.d.ts",
13
13
  "files": [
14
14
  "dist"
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "import": "./dist/index.mjs",
18
+ "import": "./dist/index.js",
19
19
  "require": "./dist/index.cjs",
20
20
  "types": "./dist/index.d.ts",
21
- "default": "./dist/index.mjs"
21
+ "default": "./dist/index.js"
22
22
  }
23
23
  },
24
24
  "packageManager": "pnpm@9.0.0"
package/dist/index.cjs 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.cjs.map
@@ -1 +0,0 @@
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 +0,0 @@
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"}