qortex-core 0.3.3 → 0.3.4

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/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { D as DefaultConfig, Q as QueryKey, a as QueryOptions, F as Fetcher, I as InferFetcherResult, S as SetDataUpdater, b as QueryState } from './types-15a3228d.js';
2
- export { E as EqualityFn, c as EqualityStrategy, d as QueryStatus, R as RefetchOnSubscribeOptions } from './types-15a3228d.js';
1
+ import { D as DefaultConfig, Q as QueryKey, a as QueryOptions, F as Fetcher, I as InferFetcherResult, S as SetDataUpdater, b as QueryState } from './types-45e8205b.js';
2
+ export { E as EqualityFn, c as EqualityStrategy, d as QueryStatus, R as RefetchOnSubscribeOptions } from './types-45e8205b.js';
3
3
 
4
4
  /**
5
5
  * Core query manager that handles caching, fetching, and state management
@@ -212,10 +212,16 @@ declare class QueryManagerCore {
212
212
  fetcher: F;
213
213
  }): QueryState<InferFetcherResult<F>>;
214
214
  };
215
+ /**
216
+ * Finds all states that match the given key
217
+ * @param key - Unique identifier for the query
218
+ * @returns An array of matching states
219
+ */
220
+ private findMatchingStates;
215
221
  /**
216
222
  * Invalidates a query, marking it as stale and triggering a refetch
217
223
  *
218
- * @param key - Unique identifier for the query
224
+ * @param key - Unique identifier for the query or partial key
219
225
  *
220
226
  * Marks the query as invalidated and immediately triggers a refetch operation.
221
227
  * Useful for forcing data refresh after mutations or when you know data is outdated.
@@ -441,7 +447,7 @@ declare const dangerClearCache: () => void;
441
447
 
442
448
  /**
443
449
  * Normalizes query keys to a consistent string format for internal storage
444
- * Arrays are joined with commas, primitives are converted to strings
450
+ * Arrays are joined with #, primitives are converted to strings
445
451
  */
446
452
  declare function serializeKey(key: QueryKey): string;
447
453
 
package/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
 
3
- function c(t){return Array.isArray(t)?t.join("-"):String(t)}function l(t,a,r="shallow"){if(t===a)return !0;if(t==null||a==null)return t===a;if(typeof t!="object"||typeof a!="object")return !1;try{let e=t,s=a;if(Array.isArray(e)&&Array.isArray(s)){if(e.length!==s.length)return !1;for(let u=0;u<e.length;u++)if(r==="deep"){if(!l(e[u],s[u],r))return !1}else if(e[u]!==s[u])return !1;return !0}if(Array.isArray(e)||Array.isArray(s))return !1;let n=Object.keys(e),i=Object.keys(s);if(n.length!==i.length)return !1;for(let u=0;u<n.length;u++){let f=n[u];if(r==="deep"){if(!l(e[f],s[f],r))return !1}else if(e[f]!==s[f])return !1}return !0}catch{return !1}}function y(t,a){return a||((r,e)=>l(r,e,t||"shallow"))}function p(t,a){return {status:"idle",updatedAt:void 0,staleTime:t?.staleTime??0,isInvalidated:!1,fetcher:t?.fetcher,equalityFn:y(t?.equalityStrategy,t?.equalityFn),equalityStrategy:t?.equalityStrategy??"shallow",placeholderData:t?.placeholderData,usePreviousDataOnError:t?.usePreviousDataOnError??!1,usePlaceholderOnError:t?.usePlaceholderOnError??!1,refetchOnSubscribe:t?.refetchOnSubscribe??"stale",enabled:t?.enabled!==!1,refetch:a||(()=>Promise.resolve(void 0)),isSuccess:!1,isError:!1,lastReturnedState:void 0,persist:t?.persist!==!1}}function d(t){let a=Date.now(),r=t.updatedAt!==void 0&&a-t.updatedAt>t.staleTime||t.isInvalidated,e,s=!1;switch(t.status){case"error":t.usePreviousDataOnError&&t.data!==void 0?e=t.data:t.usePlaceholderOnError&&t.placeholderData!==void 0&&(e=t.placeholderData,s=!0);break;case"fetching":t.data!==void 0?(e=t.data,s=!1):t.placeholderData&&(e=t.placeholderData,s=!0);break;case"success":case"idle":e=t.data??t.placeholderData,s=t.data?!1:!!t.placeholderData;break}return {data:e,error:t.error,status:t.status,updatedAt:t.updatedAt,isStale:r,isPlaceholderData:s,isLoading:t.status==="fetching"&&!t.updatedAt,isFetching:t.status==="fetching",isError:t.isError,isSuccess:t.isSuccess,refetch:t.refetch}}function Q(t){console.warn(`[qortex] No fetcher or data for key "${c(t)}". Register a fetcher or set initial data.`);}var h=class{constructor(){this.cache=new Map;this.subs=new Map;this.defaultConfig={};this.throttleTime=500;this.persister=null;this.hasQueriesBeenUsed=!1;this.dangerClearCache=()=>{this.cache.clear(),this.subs.clear(),this.persister?.clear();};this.setDefaultConfig=({throttleTime:a,persister:r,...e})=>{this.defaultConfig={...this.defaultConfig,...e},a!==void 0&&(this.throttleTime=a),r&&(this.persister=r,this.persister?.load(this.cache,this.hasQueriesBeenUsed));};this.registerFetcher=(a,r)=>{let e=this.ensureState(a,r);this.handleMountLogic(a,e);};this.fetchQuery=(a,r)=>{let e=this.ensureState(a,r);if(e.fetchPromise)return e.fetchPromise;let s=e.fetcher;if(!s)return e.updatedAt===void 0&&Q(a),Promise.resolve(e.data);let n=s();return e.fetchPromise=n,e.status="fetching",e.lastFetchTime=Date.now(),this.emit(a,e),n.then(i=>{let u=y(e.equalityStrategy,e.equalityFn);e.data=u(e.data,i)?e.data:i,e.status="success",e.isError=!1,e.isSuccess=!0,e.updatedAt=Date.now(),e.fetchPromise=void 0,e.error=void 0,this.emit(a,e);}).catch(i=>{e.error=i,e.status="error",e.isError=!0,e.isSuccess=!1,e.updatedAt=Date.now(),e.fetchPromise=void 0,this.emit(a,e);}),n};this.setQueryData=(a,r)=>{let e=this.ensureState(a),s=e.data,n=typeof r=="function"?r(s):r;y(e.equalityStrategy,e.equalityFn)(s,n)||(e.data=n,e.updatedAt=Date.now(),e.error=void 0,e.status="success",e.isInvalidated=!1,e.isError=!1,e.isSuccess=!0,this.emit(a,e));};this.getQueryData=(a,r)=>{let e=this.ensureState(a,r);return this.handleMountLogic(a,e),d(e).data};this.getQueryState=(a,r)=>{let e=this.ensureState(a,r);this.handleMountLogic(a,e);let s=d(e),n=e.lastReturnedState;return !n||!l(n,s,"shallow")?(e.lastReturnedState=s,s):n};this.invalidateQuery=a=>{let r=this.ensureState(a);r.isInvalidated=!0,this.emit(a,r),this.fetchQuery(a);};this.subscribeQuery=(a,r,e)=>{let s=c(a),n=this.ensureState(a,e);return this.subs.has(s)||this.subs.set(s,new Set),this.subs.get(s).add(r),this.handleMountLogic(a,n),()=>{this.subs.get(s).delete(r);}};}ensureState(a,r={}){this.hasQueriesBeenUsed=!0;let e=c(a),s=this.cache.get(e);if(s){let n=s.fromPersisterCache?{...s,...this.defaultConfig,...r}:{...this.defaultConfig,...s,...r};Object.assign(s,n),s.enabled=n.enabled!==!1,s.persist=n.persist!==!1,s.fromPersisterCache=!1,this.cache.set(e,s);}else {let n={...this.defaultConfig,...r},i=p(n,()=>this.fetchQuery(a));this.cache.set(e,i);}return this.persister?.sync(this.cache),this.cache.get(e)}emit(a,r){let e=c(a);this.cache.set(e,r),this.persister?.sync(this.cache);let s=this.subs.get(e);if(!s)return;let n=d(r);for(let i of Array.from(s))i(n);}handleMountLogic(a,r){let e=r.lastFetchTime&&Date.now()-r.lastFetchTime<this.throttleTime;if(r.status==="fetching"||!r.enabled||e||!r.fetcher)return;let s=Date.now(),n=r.updatedAt==null||s-(r.updatedAt||0)>r.staleTime||r.isInvalidated,i=!1;r.updatedAt==null?i=!0:(r.refetchOnSubscribe==="always"&&(i=!0),r.refetchOnSubscribe==="stale"&&(i=n)),i&&this.fetchQuery(a);}},T=h;var o=new T,b=o.registerFetcher,O=o.fetchQuery,K=o.setQueryData,w=o.getQueryData,x=o.getQueryState,E=o.invalidateQuery,A=o.subscribeQuery,q=o.setDefaultConfig,I=o.dangerClearCache;
3
+ function c(a){return Array.isArray(a)?a.map(String).join("#"):String(a)}function l(a,t,r="shallow"){if(a===t)return !0;if(a==null||t==null)return a===t;if(typeof a!="object"||typeof t!="object")return !1;try{let e=a,s=t;if(Array.isArray(e)&&Array.isArray(s)){if(e.length!==s.length)return !1;for(let u=0;u<e.length;u++)if(r==="deep"){if(!l(e[u],s[u],r))return !1}else if(e[u]!==s[u])return !1;return !0}if(Array.isArray(e)||Array.isArray(s))return !1;let n=Object.keys(e),i=Object.keys(s);if(n.length!==i.length)return !1;for(let u=0;u<n.length;u++){let y=n[u];if(r==="deep"){if(!l(e[y],s[y],r))return !1}else if(e[y]!==s[y])return !1}return !0}catch{return !1}}function f(a,t){return t||((r,e)=>l(r,e,a||"shallow"))}function p(a,t,r){return {key:a,status:"idle",updatedAt:void 0,staleTime:t?.staleTime??0,isInvalidated:!1,fetcher:t?.fetcher,equalityFn:f(t?.equalityStrategy,t?.equalityFn),equalityStrategy:t?.equalityStrategy??"shallow",placeholderData:t?.placeholderData,usePreviousDataOnError:t?.usePreviousDataOnError??!1,usePlaceholderOnError:t?.usePlaceholderOnError??!1,refetchOnSubscribe:t?.refetchOnSubscribe??"stale",enabled:t?.enabled!==!1,refetch:r||(()=>Promise.resolve(void 0)),isSuccess:!1,isError:!1,lastReturnedState:void 0,persist:t?.persist!==!1}}function d(a){let t=Date.now(),r=a.updatedAt!==void 0&&t-a.updatedAt>a.staleTime||a.isInvalidated,e,s=!1;switch(a.status){case"error":a.usePreviousDataOnError&&a.data!==void 0?e=a.data:a.usePlaceholderOnError&&a.placeholderData!==void 0&&(e=a.placeholderData,s=!0);break;case"fetching":a.data!==void 0?(e=a.data,s=!1):a.placeholderData&&(e=a.placeholderData,s=!0);break;case"success":case"idle":e=a.data??a.placeholderData,s=a.data?!1:!!a.placeholderData;break}return {data:e,error:a.error,status:a.status,updatedAt:a.updatedAt,isStale:r,isPlaceholderData:s,isLoading:a.status==="fetching"&&!a.updatedAt,isFetching:a.status==="fetching",isError:a.isError,isSuccess:a.isSuccess,refetch:a.refetch}}function Q(a){console.warn(`[qortex] No fetcher or data for key "${c(a)}". Register a fetcher or set initial data.`);}var h=class{constructor(){this.cache=new Map;this.subs=new Map;this.defaultConfig={};this.throttleTime=500;this.persister=null;this.hasQueriesBeenUsed=!1;this.dangerClearCache=()=>{this.cache.clear(),this.subs.clear(),this.persister?.clear();};this.setDefaultConfig=({throttleTime:t,persister:r,...e})=>{this.defaultConfig={...this.defaultConfig,...e},t!==void 0&&(this.throttleTime=t),r&&(this.persister=r,this.persister?.load(this.cache,this.hasQueriesBeenUsed));};this.registerFetcher=(t,r)=>{let e=this.ensureState(t,r);this.handleMountLogic(t,e);};this.fetchQuery=(t,r)=>{let e=this.ensureState(t,r);if(e.fetchPromise)return e.fetchPromise;let s=e.fetcher;if(!s)return e.updatedAt===void 0&&Q(t),Promise.resolve(e.data);let n=s();return e.fetchPromise=n,e.status="fetching",e.lastFetchTime=Date.now(),this.emit(t,e),n.then(i=>{let u=f(e.equalityStrategy,e.equalityFn);e.data=u(e.data,i)?e.data:i,e.status="success",e.isError=!1,e.isSuccess=!0,e.updatedAt=Date.now(),e.fetchPromise=void 0,e.error=void 0,this.emit(t,e);}).catch(i=>{e.error=i,e.status="error",e.isError=!0,e.isSuccess=!1,e.updatedAt=Date.now(),e.fetchPromise=void 0,this.emit(t,e);}),n};this.setQueryData=(t,r)=>{let e=this.ensureState(t),s=e.data,n=typeof r=="function"?r(s):r;f(e.equalityStrategy,e.equalityFn)(s,n)||(e.data=n,e.updatedAt=Date.now(),e.error=void 0,e.status="success",e.isInvalidated=!1,e.isError=!1,e.isSuccess=!0,this.emit(t,e));};this.getQueryData=(t,r)=>{let e=this.ensureState(t,r);return this.handleMountLogic(t,e),d(e).data};this.getQueryState=(t,r)=>{let e=this.ensureState(t,r);this.handleMountLogic(t,e);let s=d(e),n=e.lastReturnedState;return !n||!l(n,s,"shallow")?(e.lastReturnedState=s,s):n};this.invalidateQuery=t=>{let r=this.findMatchingStates(t);for(let e of r)e.isInvalidated=!0,this.emit(e.key,e),this.fetchQuery(e.key);};this.subscribeQuery=(t,r,e)=>{let s=c(t),n=this.ensureState(t,e);return this.subs.has(s)||this.subs.set(s,new Set),this.subs.get(s).add(r),this.handleMountLogic(t,n),()=>{this.subs.get(s).delete(r);}};}ensureState(t,r={}){this.hasQueriesBeenUsed=!0;let e=c(t),s=this.cache.get(e);if(s){let n=s.fromPersisterCache?{...s,...this.defaultConfig,...r}:{...this.defaultConfig,...s,...r};Object.assign(s,n),s.enabled=n.enabled!==!1,s.persist=n.persist!==!1,s.fromPersisterCache=!1,this.cache.set(e,s);}else {let n={...this.defaultConfig,...r},i=p(e,n,()=>this.fetchQuery(t));this.cache.set(e,i);}return this.persister?.sync(this.cache),this.cache.get(e)}emit(t,r){let e=c(t);this.cache.set(e,r),this.persister?.sync(this.cache);let s=this.subs.get(e);if(!s)return;let n=d(r);for(let i of Array.from(s))i(n);}findMatchingStates(t){let r=c(t);return [...this.cache.values()].filter(e=>e.key.startsWith(r+"#")||e.key===r)}handleMountLogic(t,r){let e=r.lastFetchTime&&Date.now()-r.lastFetchTime<this.throttleTime;if(r.status==="fetching"||!r.enabled||e||!r.fetcher)return;let s=Date.now(),n=r.updatedAt==null||s-(r.updatedAt||0)>r.staleTime||r.isInvalidated,i=!1;r.updatedAt==null?i=!0:(r.refetchOnSubscribe==="always"&&(i=!0),r.refetchOnSubscribe==="stale"&&(i=n)),i&&this.fetchQuery(t);}},T=h;var o=new T,b=o.registerFetcher,K=o.fetchQuery,O=o.setQueryData,w=o.getQueryData,x=o.getQueryState,E=o.invalidateQuery,A=o.subscribeQuery,I=o.setDefaultConfig,q=o.dangerClearCache;
4
4
 
5
5
  exports.QueryManagerCore = h;
6
- exports.dangerClearCache = I;
7
- exports.fetchQuery = O;
6
+ exports.dangerClearCache = q;
7
+ exports.fetchQuery = K;
8
8
  exports.getQueryData = w;
9
9
  exports.getQueryState = x;
10
10
  exports.invalidateQuery = E;
11
11
  exports.queryManager = o;
12
12
  exports.registerFetcher = b;
13
13
  exports.serializeKey = c;
14
- exports.setDefaultConfig = q;
15
- exports.setQueryData = K;
14
+ exports.setDefaultConfig = I;
15
+ exports.setQueryData = O;
16
16
  exports.subscribeQuery = A;
package/index.mjs CHANGED
@@ -1,3 +1,3 @@
1
- function c(t){return Array.isArray(t)?t.join("-"):String(t)}function l(t,a,r="shallow"){if(t===a)return !0;if(t==null||a==null)return t===a;if(typeof t!="object"||typeof a!="object")return !1;try{let e=t,s=a;if(Array.isArray(e)&&Array.isArray(s)){if(e.length!==s.length)return !1;for(let u=0;u<e.length;u++)if(r==="deep"){if(!l(e[u],s[u],r))return !1}else if(e[u]!==s[u])return !1;return !0}if(Array.isArray(e)||Array.isArray(s))return !1;let n=Object.keys(e),i=Object.keys(s);if(n.length!==i.length)return !1;for(let u=0;u<n.length;u++){let f=n[u];if(r==="deep"){if(!l(e[f],s[f],r))return !1}else if(e[f]!==s[f])return !1}return !0}catch{return !1}}function y(t,a){return a||((r,e)=>l(r,e,t||"shallow"))}function p(t,a){return {status:"idle",updatedAt:void 0,staleTime:t?.staleTime??0,isInvalidated:!1,fetcher:t?.fetcher,equalityFn:y(t?.equalityStrategy,t?.equalityFn),equalityStrategy:t?.equalityStrategy??"shallow",placeholderData:t?.placeholderData,usePreviousDataOnError:t?.usePreviousDataOnError??!1,usePlaceholderOnError:t?.usePlaceholderOnError??!1,refetchOnSubscribe:t?.refetchOnSubscribe??"stale",enabled:t?.enabled!==!1,refetch:a||(()=>Promise.resolve(void 0)),isSuccess:!1,isError:!1,lastReturnedState:void 0,persist:t?.persist!==!1}}function d(t){let a=Date.now(),r=t.updatedAt!==void 0&&a-t.updatedAt>t.staleTime||t.isInvalidated,e,s=!1;switch(t.status){case"error":t.usePreviousDataOnError&&t.data!==void 0?e=t.data:t.usePlaceholderOnError&&t.placeholderData!==void 0&&(e=t.placeholderData,s=!0);break;case"fetching":t.data!==void 0?(e=t.data,s=!1):t.placeholderData&&(e=t.placeholderData,s=!0);break;case"success":case"idle":e=t.data??t.placeholderData,s=t.data?!1:!!t.placeholderData;break}return {data:e,error:t.error,status:t.status,updatedAt:t.updatedAt,isStale:r,isPlaceholderData:s,isLoading:t.status==="fetching"&&!t.updatedAt,isFetching:t.status==="fetching",isError:t.isError,isSuccess:t.isSuccess,refetch:t.refetch}}function Q(t){console.warn(`[qortex] No fetcher or data for key "${c(t)}". Register a fetcher or set initial data.`);}var h=class{constructor(){this.cache=new Map;this.subs=new Map;this.defaultConfig={};this.throttleTime=500;this.persister=null;this.hasQueriesBeenUsed=!1;this.dangerClearCache=()=>{this.cache.clear(),this.subs.clear(),this.persister?.clear();};this.setDefaultConfig=({throttleTime:a,persister:r,...e})=>{this.defaultConfig={...this.defaultConfig,...e},a!==void 0&&(this.throttleTime=a),r&&(this.persister=r,this.persister?.load(this.cache,this.hasQueriesBeenUsed));};this.registerFetcher=(a,r)=>{let e=this.ensureState(a,r);this.handleMountLogic(a,e);};this.fetchQuery=(a,r)=>{let e=this.ensureState(a,r);if(e.fetchPromise)return e.fetchPromise;let s=e.fetcher;if(!s)return e.updatedAt===void 0&&Q(a),Promise.resolve(e.data);let n=s();return e.fetchPromise=n,e.status="fetching",e.lastFetchTime=Date.now(),this.emit(a,e),n.then(i=>{let u=y(e.equalityStrategy,e.equalityFn);e.data=u(e.data,i)?e.data:i,e.status="success",e.isError=!1,e.isSuccess=!0,e.updatedAt=Date.now(),e.fetchPromise=void 0,e.error=void 0,this.emit(a,e);}).catch(i=>{e.error=i,e.status="error",e.isError=!0,e.isSuccess=!1,e.updatedAt=Date.now(),e.fetchPromise=void 0,this.emit(a,e);}),n};this.setQueryData=(a,r)=>{let e=this.ensureState(a),s=e.data,n=typeof r=="function"?r(s):r;y(e.equalityStrategy,e.equalityFn)(s,n)||(e.data=n,e.updatedAt=Date.now(),e.error=void 0,e.status="success",e.isInvalidated=!1,e.isError=!1,e.isSuccess=!0,this.emit(a,e));};this.getQueryData=(a,r)=>{let e=this.ensureState(a,r);return this.handleMountLogic(a,e),d(e).data};this.getQueryState=(a,r)=>{let e=this.ensureState(a,r);this.handleMountLogic(a,e);let s=d(e),n=e.lastReturnedState;return !n||!l(n,s,"shallow")?(e.lastReturnedState=s,s):n};this.invalidateQuery=a=>{let r=this.ensureState(a);r.isInvalidated=!0,this.emit(a,r),this.fetchQuery(a);};this.subscribeQuery=(a,r,e)=>{let s=c(a),n=this.ensureState(a,e);return this.subs.has(s)||this.subs.set(s,new Set),this.subs.get(s).add(r),this.handleMountLogic(a,n),()=>{this.subs.get(s).delete(r);}};}ensureState(a,r={}){this.hasQueriesBeenUsed=!0;let e=c(a),s=this.cache.get(e);if(s){let n=s.fromPersisterCache?{...s,...this.defaultConfig,...r}:{...this.defaultConfig,...s,...r};Object.assign(s,n),s.enabled=n.enabled!==!1,s.persist=n.persist!==!1,s.fromPersisterCache=!1,this.cache.set(e,s);}else {let n={...this.defaultConfig,...r},i=p(n,()=>this.fetchQuery(a));this.cache.set(e,i);}return this.persister?.sync(this.cache),this.cache.get(e)}emit(a,r){let e=c(a);this.cache.set(e,r),this.persister?.sync(this.cache);let s=this.subs.get(e);if(!s)return;let n=d(r);for(let i of Array.from(s))i(n);}handleMountLogic(a,r){let e=r.lastFetchTime&&Date.now()-r.lastFetchTime<this.throttleTime;if(r.status==="fetching"||!r.enabled||e||!r.fetcher)return;let s=Date.now(),n=r.updatedAt==null||s-(r.updatedAt||0)>r.staleTime||r.isInvalidated,i=!1;r.updatedAt==null?i=!0:(r.refetchOnSubscribe==="always"&&(i=!0),r.refetchOnSubscribe==="stale"&&(i=n)),i&&this.fetchQuery(a);}},T=h;var o=new T,b=o.registerFetcher,O=o.fetchQuery,K=o.setQueryData,w=o.getQueryData,x=o.getQueryState,E=o.invalidateQuery,A=o.subscribeQuery,q=o.setDefaultConfig,I=o.dangerClearCache;
1
+ function c(a){return Array.isArray(a)?a.map(String).join("#"):String(a)}function l(a,t,r="shallow"){if(a===t)return !0;if(a==null||t==null)return a===t;if(typeof a!="object"||typeof t!="object")return !1;try{let e=a,s=t;if(Array.isArray(e)&&Array.isArray(s)){if(e.length!==s.length)return !1;for(let u=0;u<e.length;u++)if(r==="deep"){if(!l(e[u],s[u],r))return !1}else if(e[u]!==s[u])return !1;return !0}if(Array.isArray(e)||Array.isArray(s))return !1;let n=Object.keys(e),i=Object.keys(s);if(n.length!==i.length)return !1;for(let u=0;u<n.length;u++){let y=n[u];if(r==="deep"){if(!l(e[y],s[y],r))return !1}else if(e[y]!==s[y])return !1}return !0}catch{return !1}}function f(a,t){return t||((r,e)=>l(r,e,a||"shallow"))}function p(a,t,r){return {key:a,status:"idle",updatedAt:void 0,staleTime:t?.staleTime??0,isInvalidated:!1,fetcher:t?.fetcher,equalityFn:f(t?.equalityStrategy,t?.equalityFn),equalityStrategy:t?.equalityStrategy??"shallow",placeholderData:t?.placeholderData,usePreviousDataOnError:t?.usePreviousDataOnError??!1,usePlaceholderOnError:t?.usePlaceholderOnError??!1,refetchOnSubscribe:t?.refetchOnSubscribe??"stale",enabled:t?.enabled!==!1,refetch:r||(()=>Promise.resolve(void 0)),isSuccess:!1,isError:!1,lastReturnedState:void 0,persist:t?.persist!==!1}}function d(a){let t=Date.now(),r=a.updatedAt!==void 0&&t-a.updatedAt>a.staleTime||a.isInvalidated,e,s=!1;switch(a.status){case"error":a.usePreviousDataOnError&&a.data!==void 0?e=a.data:a.usePlaceholderOnError&&a.placeholderData!==void 0&&(e=a.placeholderData,s=!0);break;case"fetching":a.data!==void 0?(e=a.data,s=!1):a.placeholderData&&(e=a.placeholderData,s=!0);break;case"success":case"idle":e=a.data??a.placeholderData,s=a.data?!1:!!a.placeholderData;break}return {data:e,error:a.error,status:a.status,updatedAt:a.updatedAt,isStale:r,isPlaceholderData:s,isLoading:a.status==="fetching"&&!a.updatedAt,isFetching:a.status==="fetching",isError:a.isError,isSuccess:a.isSuccess,refetch:a.refetch}}function Q(a){console.warn(`[qortex] No fetcher or data for key "${c(a)}". Register a fetcher or set initial data.`);}var h=class{constructor(){this.cache=new Map;this.subs=new Map;this.defaultConfig={};this.throttleTime=500;this.persister=null;this.hasQueriesBeenUsed=!1;this.dangerClearCache=()=>{this.cache.clear(),this.subs.clear(),this.persister?.clear();};this.setDefaultConfig=({throttleTime:t,persister:r,...e})=>{this.defaultConfig={...this.defaultConfig,...e},t!==void 0&&(this.throttleTime=t),r&&(this.persister=r,this.persister?.load(this.cache,this.hasQueriesBeenUsed));};this.registerFetcher=(t,r)=>{let e=this.ensureState(t,r);this.handleMountLogic(t,e);};this.fetchQuery=(t,r)=>{let e=this.ensureState(t,r);if(e.fetchPromise)return e.fetchPromise;let s=e.fetcher;if(!s)return e.updatedAt===void 0&&Q(t),Promise.resolve(e.data);let n=s();return e.fetchPromise=n,e.status="fetching",e.lastFetchTime=Date.now(),this.emit(t,e),n.then(i=>{let u=f(e.equalityStrategy,e.equalityFn);e.data=u(e.data,i)?e.data:i,e.status="success",e.isError=!1,e.isSuccess=!0,e.updatedAt=Date.now(),e.fetchPromise=void 0,e.error=void 0,this.emit(t,e);}).catch(i=>{e.error=i,e.status="error",e.isError=!0,e.isSuccess=!1,e.updatedAt=Date.now(),e.fetchPromise=void 0,this.emit(t,e);}),n};this.setQueryData=(t,r)=>{let e=this.ensureState(t),s=e.data,n=typeof r=="function"?r(s):r;f(e.equalityStrategy,e.equalityFn)(s,n)||(e.data=n,e.updatedAt=Date.now(),e.error=void 0,e.status="success",e.isInvalidated=!1,e.isError=!1,e.isSuccess=!0,this.emit(t,e));};this.getQueryData=(t,r)=>{let e=this.ensureState(t,r);return this.handleMountLogic(t,e),d(e).data};this.getQueryState=(t,r)=>{let e=this.ensureState(t,r);this.handleMountLogic(t,e);let s=d(e),n=e.lastReturnedState;return !n||!l(n,s,"shallow")?(e.lastReturnedState=s,s):n};this.invalidateQuery=t=>{let r=this.findMatchingStates(t);for(let e of r)e.isInvalidated=!0,this.emit(e.key,e),this.fetchQuery(e.key);};this.subscribeQuery=(t,r,e)=>{let s=c(t),n=this.ensureState(t,e);return this.subs.has(s)||this.subs.set(s,new Set),this.subs.get(s).add(r),this.handleMountLogic(t,n),()=>{this.subs.get(s).delete(r);}};}ensureState(t,r={}){this.hasQueriesBeenUsed=!0;let e=c(t),s=this.cache.get(e);if(s){let n=s.fromPersisterCache?{...s,...this.defaultConfig,...r}:{...this.defaultConfig,...s,...r};Object.assign(s,n),s.enabled=n.enabled!==!1,s.persist=n.persist!==!1,s.fromPersisterCache=!1,this.cache.set(e,s);}else {let n={...this.defaultConfig,...r},i=p(e,n,()=>this.fetchQuery(t));this.cache.set(e,i);}return this.persister?.sync(this.cache),this.cache.get(e)}emit(t,r){let e=c(t);this.cache.set(e,r),this.persister?.sync(this.cache);let s=this.subs.get(e);if(!s)return;let n=d(r);for(let i of Array.from(s))i(n);}findMatchingStates(t){let r=c(t);return [...this.cache.values()].filter(e=>e.key.startsWith(r+"#")||e.key===r)}handleMountLogic(t,r){let e=r.lastFetchTime&&Date.now()-r.lastFetchTime<this.throttleTime;if(r.status==="fetching"||!r.enabled||e||!r.fetcher)return;let s=Date.now(),n=r.updatedAt==null||s-(r.updatedAt||0)>r.staleTime||r.isInvalidated,i=!1;r.updatedAt==null?i=!0:(r.refetchOnSubscribe==="always"&&(i=!0),r.refetchOnSubscribe==="stale"&&(i=n)),i&&this.fetchQuery(t);}},T=h;var o=new T,b=o.registerFetcher,K=o.fetchQuery,O=o.setQueryData,w=o.getQueryData,x=o.getQueryState,E=o.invalidateQuery,A=o.subscribeQuery,I=o.setDefaultConfig,q=o.dangerClearCache;
2
2
 
3
- export { h as QueryManagerCore, I as dangerClearCache, O as fetchQuery, w as getQueryData, x as getQueryState, E as invalidateQuery, o as queryManager, b as registerFetcher, c as serializeKey, q as setDefaultConfig, K as setQueryData, A as subscribeQuery };
3
+ export { h as QueryManagerCore, q as dangerClearCache, K as fetchQuery, w as getQueryData, x as getQueryState, E as invalidateQuery, o as queryManager, b as registerFetcher, c as serializeKey, I as setDefaultConfig, O as setQueryData, A as subscribeQuery };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qortex-core",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Framework-agnostic query cache & fetch registry (MFE friendly).",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -28,7 +28,7 @@
28
28
  "persister.js",
29
29
  "persister.mjs",
30
30
  "persister.d.ts",
31
- "types-15a3228d.d.ts",
31
+ "types-45e8205b.d.ts",
32
32
  "README.md"
33
33
  ],
34
34
  "repository": {
package/persister.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { P as PersisterConfig, B as BasePersister } from './types-15a3228d.js';
2
- export { e as Persister } from './types-15a3228d.js';
1
+ import { P as PersisterConfig, B as BasePersister } from './types-45e8205b.js';
2
+ export { e as Persister } from './types-45e8205b.js';
3
3
 
4
4
  /**
5
5
  * Creates a persister instance for data persistence.
package/persister.js CHANGED
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
- function g(e){return typeof e=="string"&&["idle","fetching","success","error"].includes(e)}function h(e){return typeof e=="string"&&["shallow","deep"].includes(e)}function b(e){return e===!1||typeof e=="string"&&["always","stale"].includes(e)}function P(e){return !(!e||typeof e!="object"||!g(e.status)||typeof e.staleTime!="number"||e.staleTime<0||typeof e.isInvalidated!="boolean"||!h(e.equalityStrategy)||!b(e.refetchOnSubscribe))}function l(e){if(!e||typeof e!="object"||!e.queries||typeof e.queries!="object")return !1;for(let[t,r]of Object.entries(e.queries))if(typeof t!="string"||!P(r))return !1;return !(typeof e.burstKey!="string"||typeof e.timestamp!="number"||e.timestamp<0)}function u(e){let{fetcher:t,equalityFn:r,fetchPromise:s,refetch:i,fromPersisterCache:o,...n}=e;return n}function f(e,t){return {...t??{},...e,fromPersisterCache:!t}}function y(e){try{let t=JSON.parse(e);return l(t)?t:null}catch{return null}}var c=()=>{console.warn("[Qortex] No storage found, persister will not be able to persist data");},p=()=>{console.warn("[Qortex] Persister is being set after queries have been used. This may cause data inconsistency. It is recommended to set the persister before any query usage.");};var a=class{constructor(t,r){this.syncTimeout=null;this.debounceTime=100;this.burstKey="0.3.3";this.storageKey="qortex";this.storage=t,this.burstKey=r?.burstKey??this.burstKey,this.storageKey=r?.prefix??this.storageKey,this.debounceTime=r?.debounceTime??this.debounceTime;}save(t){try{let r={queries:{},burstKey:this.burstKey,timestamp:Date.now()};for(let[i,o]of Object.entries(t))r.queries[i]=o;let s=JSON.stringify(r);this.storage.setItem(this.storageKey,s);}catch(r){console.warn("[Qortex] Failed to persist state:",r);}}load(t,r){r&&p();try{let s=this.storage.getItem(this.storageKey);if(!s)return;let i=y(s);if(!i){console.warn("[Qortex] Invalid persisted state format, clearing cache"),this.clear();return}if(i.burstKey!==this.burstKey){console.warn("[Qortex] Burst key mismatch, clearing cache"),this.clear();return}for(let[o,n]of Object.entries(i.queries)){let S=n,m=t.get(o),d=f(S,m);t.set(o,d);}}catch(s){console.warn("[Qortex] Failed to load persisted state:",s),this.clear();}}clear(){try{this.storage.removeItem(this.storageKey);}catch(t){console.warn("[Qortex] Failed to clear persisted data:",t);}}sync(t){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>{let r={};for(let[s,i]of t.entries())i.persist!==!1&&(r[s]=u(i));this.save(r);},this.debounceTime);}};var Q=e=>{switch(e){case"local":if(typeof localStorage<"u")return window.localStorage;break;case"session":if(typeof sessionStorage<"u")return window.sessionStorage;break;default:throw new Error(`Invalid storage type: ${e}`)}},w=(e,t)=>{let r=Q(e);if(!r){c();return}return new a(r,t)};
3
+ function g(e){return typeof e=="string"&&["idle","fetching","success","error"].includes(e)}function h(e){return typeof e=="string"&&["shallow","deep"].includes(e)}function b(e){return e===!1||typeof e=="string"&&["always","stale"].includes(e)}function P(e){return !(!e||typeof e!="object"||!g(e.status)||typeof e.staleTime!="number"||e.staleTime<0||typeof e.isInvalidated!="boolean"||!h(e.equalityStrategy)||!b(e.refetchOnSubscribe))}function l(e){if(!e||typeof e!="object"||!e.queries||typeof e.queries!="object")return !1;for(let[t,r]of Object.entries(e.queries))if(typeof t!="string"||!P(r))return !1;return !(typeof e.burstKey!="string"||typeof e.timestamp!="number"||e.timestamp<0)}function u(e){let{fetcher:t,equalityFn:r,fetchPromise:s,refetch:i,fromPersisterCache:o,...n}=e;return n}function f(e,t){return {...t??{},...e,fromPersisterCache:!t}}function y(e){try{let t=JSON.parse(e);return l(t)?t:null}catch{return null}}var c=()=>{console.warn("[Qortex] No storage found, persister will not be able to persist data");},p=()=>{console.warn("[Qortex] Persister is being set after queries have been used. This may cause data inconsistency. It is recommended to set the persister before any query usage.");};var a=class{constructor(t,r){this.syncTimeout=null;this.debounceTime=100;this.burstKey="0.3.4";this.storageKey="qortex";this.storage=t,this.burstKey=r?.burstKey??this.burstKey,this.storageKey=r?.prefix??this.storageKey,this.debounceTime=r?.debounceTime??this.debounceTime;}save(t){try{let r={queries:{},burstKey:this.burstKey,timestamp:Date.now()};for(let[i,o]of Object.entries(t))r.queries[i]=o;let s=JSON.stringify(r);this.storage.setItem(this.storageKey,s);}catch(r){console.warn("[Qortex] Failed to persist state:",r);}}load(t,r){r&&p();try{let s=this.storage.getItem(this.storageKey);if(!s)return;let i=y(s);if(!i){console.warn("[Qortex] Invalid persisted state format, clearing cache"),this.clear();return}if(i.burstKey!==this.burstKey){console.warn("[Qortex] Burst key mismatch, clearing cache"),this.clear();return}for(let[o,n]of Object.entries(i.queries)){let S=n,m=t.get(o),d=f(S,m);t.set(o,d);}}catch(s){console.warn("[Qortex] Failed to load persisted state:",s),this.clear();}}clear(){try{this.storage.removeItem(this.storageKey);}catch(t){console.warn("[Qortex] Failed to clear persisted data:",t);}}sync(t){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>{let r={};for(let[s,i]of t.entries())i.persist!==!1&&(r[s]=u(i));this.save(r);},this.debounceTime);}};var Q=e=>{switch(e){case"local":if(typeof localStorage<"u")return window.localStorage;break;case"session":if(typeof sessionStorage<"u")return window.sessionStorage;break;default:throw new Error(`Invalid storage type: ${e}`)}},w=(e,t)=>{let r=Q(e);if(!r){c();return}return new a(r,t)};
4
4
 
5
5
  exports.createPersister = w;
package/persister.mjs CHANGED
@@ -1,3 +1,3 @@
1
- function g(e){return typeof e=="string"&&["idle","fetching","success","error"].includes(e)}function h(e){return typeof e=="string"&&["shallow","deep"].includes(e)}function b(e){return e===!1||typeof e=="string"&&["always","stale"].includes(e)}function P(e){return !(!e||typeof e!="object"||!g(e.status)||typeof e.staleTime!="number"||e.staleTime<0||typeof e.isInvalidated!="boolean"||!h(e.equalityStrategy)||!b(e.refetchOnSubscribe))}function l(e){if(!e||typeof e!="object"||!e.queries||typeof e.queries!="object")return !1;for(let[t,r]of Object.entries(e.queries))if(typeof t!="string"||!P(r))return !1;return !(typeof e.burstKey!="string"||typeof e.timestamp!="number"||e.timestamp<0)}function u(e){let{fetcher:t,equalityFn:r,fetchPromise:s,refetch:i,fromPersisterCache:o,...n}=e;return n}function f(e,t){return {...t??{},...e,fromPersisterCache:!t}}function y(e){try{let t=JSON.parse(e);return l(t)?t:null}catch{return null}}var c=()=>{console.warn("[Qortex] No storage found, persister will not be able to persist data");},p=()=>{console.warn("[Qortex] Persister is being set after queries have been used. This may cause data inconsistency. It is recommended to set the persister before any query usage.");};var a=class{constructor(t,r){this.syncTimeout=null;this.debounceTime=100;this.burstKey="0.3.3";this.storageKey="qortex";this.storage=t,this.burstKey=r?.burstKey??this.burstKey,this.storageKey=r?.prefix??this.storageKey,this.debounceTime=r?.debounceTime??this.debounceTime;}save(t){try{let r={queries:{},burstKey:this.burstKey,timestamp:Date.now()};for(let[i,o]of Object.entries(t))r.queries[i]=o;let s=JSON.stringify(r);this.storage.setItem(this.storageKey,s);}catch(r){console.warn("[Qortex] Failed to persist state:",r);}}load(t,r){r&&p();try{let s=this.storage.getItem(this.storageKey);if(!s)return;let i=y(s);if(!i){console.warn("[Qortex] Invalid persisted state format, clearing cache"),this.clear();return}if(i.burstKey!==this.burstKey){console.warn("[Qortex] Burst key mismatch, clearing cache"),this.clear();return}for(let[o,n]of Object.entries(i.queries)){let S=n,m=t.get(o),d=f(S,m);t.set(o,d);}}catch(s){console.warn("[Qortex] Failed to load persisted state:",s),this.clear();}}clear(){try{this.storage.removeItem(this.storageKey);}catch(t){console.warn("[Qortex] Failed to clear persisted data:",t);}}sync(t){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>{let r={};for(let[s,i]of t.entries())i.persist!==!1&&(r[s]=u(i));this.save(r);},this.debounceTime);}};var Q=e=>{switch(e){case"local":if(typeof localStorage<"u")return window.localStorage;break;case"session":if(typeof sessionStorage<"u")return window.sessionStorage;break;default:throw new Error(`Invalid storage type: ${e}`)}},w=(e,t)=>{let r=Q(e);if(!r){c();return}return new a(r,t)};
1
+ function g(e){return typeof e=="string"&&["idle","fetching","success","error"].includes(e)}function h(e){return typeof e=="string"&&["shallow","deep"].includes(e)}function b(e){return e===!1||typeof e=="string"&&["always","stale"].includes(e)}function P(e){return !(!e||typeof e!="object"||!g(e.status)||typeof e.staleTime!="number"||e.staleTime<0||typeof e.isInvalidated!="boolean"||!h(e.equalityStrategy)||!b(e.refetchOnSubscribe))}function l(e){if(!e||typeof e!="object"||!e.queries||typeof e.queries!="object")return !1;for(let[t,r]of Object.entries(e.queries))if(typeof t!="string"||!P(r))return !1;return !(typeof e.burstKey!="string"||typeof e.timestamp!="number"||e.timestamp<0)}function u(e){let{fetcher:t,equalityFn:r,fetchPromise:s,refetch:i,fromPersisterCache:o,...n}=e;return n}function f(e,t){return {...t??{},...e,fromPersisterCache:!t}}function y(e){try{let t=JSON.parse(e);return l(t)?t:null}catch{return null}}var c=()=>{console.warn("[Qortex] No storage found, persister will not be able to persist data");},p=()=>{console.warn("[Qortex] Persister is being set after queries have been used. This may cause data inconsistency. It is recommended to set the persister before any query usage.");};var a=class{constructor(t,r){this.syncTimeout=null;this.debounceTime=100;this.burstKey="0.3.4";this.storageKey="qortex";this.storage=t,this.burstKey=r?.burstKey??this.burstKey,this.storageKey=r?.prefix??this.storageKey,this.debounceTime=r?.debounceTime??this.debounceTime;}save(t){try{let r={queries:{},burstKey:this.burstKey,timestamp:Date.now()};for(let[i,o]of Object.entries(t))r.queries[i]=o;let s=JSON.stringify(r);this.storage.setItem(this.storageKey,s);}catch(r){console.warn("[Qortex] Failed to persist state:",r);}}load(t,r){r&&p();try{let s=this.storage.getItem(this.storageKey);if(!s)return;let i=y(s);if(!i){console.warn("[Qortex] Invalid persisted state format, clearing cache"),this.clear();return}if(i.burstKey!==this.burstKey){console.warn("[Qortex] Burst key mismatch, clearing cache"),this.clear();return}for(let[o,n]of Object.entries(i.queries)){let S=n,m=t.get(o),d=f(S,m);t.set(o,d);}}catch(s){console.warn("[Qortex] Failed to load persisted state:",s),this.clear();}}clear(){try{this.storage.removeItem(this.storageKey);}catch(t){console.warn("[Qortex] Failed to clear persisted data:",t);}}sync(t){this.syncTimeout&&clearTimeout(this.syncTimeout),this.syncTimeout=setTimeout(()=>{let r={};for(let[s,i]of t.entries())i.persist!==!1&&(r[s]=u(i));this.save(r);},this.debounceTime);}};var Q=e=>{switch(e){case"local":if(typeof localStorage<"u")return window.localStorage;break;case"session":if(typeof sessionStorage<"u")return window.sessionStorage;break;default:throw new Error(`Invalid storage type: ${e}`)}},w=(e,t)=>{let r=Q(e);if(!r){c();return}return new a(r,t)};
2
2
 
3
3
  export { w as createPersister };
@@ -4,6 +4,7 @@
4
4
  * NOT EXPORTED - internal implementation detail
5
5
  */
6
6
  type QueryStateInternal<T = any, E = unknown> = {
7
+ key: string;
7
8
  data?: T;
8
9
  error?: E;
9
10
  status: QueryStatus;
@@ -73,7 +74,8 @@ type Persister = BasePersister;
73
74
  * Query key can be a string or readonly array of strings/numbers
74
75
  * Using readonly to prevent accidental mutations
75
76
  */
76
- type QueryKey = string | readonly (string | number)[];
77
+ type Key = string | number | boolean | null | undefined;
78
+ type QueryKey = Key | readonly Key[];
77
79
  /** Function that fetches data, must be async */
78
80
  type Fetcher<T = any> = () => Promise<T>;
79
81
  /** Function that compares two values for equality */