qortex-core 0.3.4 → 0.3.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/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-45e8205b.js';
2
- export { E as EqualityFn, c as EqualityStrategy, d as QueryStatus, R as RefetchOnSubscribeOptions } from './types-45e8205b.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-6c88db66.js';
2
+ export { E as EqualityFn, c as EqualityStrategy, d as QueryStatus, R as RefetchOnSubscribeOptions } from './types-6c88db66.js';
3
3
 
4
4
  /**
5
5
  * Core query manager that handles caching, fetching, and state management
@@ -53,7 +53,7 @@ declare class QueryManagerCore {
53
53
  * @param config.equalityFn - Default equality function. Default: `Object.is`
54
54
  * @param config.usePreviousDataOnError - Default behavior for previous data on error. Default: `false`
55
55
  * @param config.usePlaceholderOnError - Default behavior for placeholder data on error. Default: `false`
56
- * @param config.throttleTime - Time in ms to throttle fetch requests. Default: `500`
56
+ * @param config.throttleTime - Time in ms to throttle fetch requests. Default: `100`
57
57
  * @param config.persister - Persister instance for data persistence
58
58
  *
59
59
  * These defaults will be merged with individual query options. Useful for setting global behavior
package/index.js CHANGED
@@ -1,16 +1,16 @@
1
1
  'use strict';
2
2
 
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;
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=100;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",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);queueMicrotask(()=>{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=Date.now(),s=r.updatedAt&&e-r.updatedAt<this.throttleTime;if(r.status==="fetching"||!r.enabled||s||!r.fetcher)return;let n=r.updatedAt==null||e-(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);}},S=h;var o=new S,D=o.registerFetcher,K=o.fetchQuery,O=o.setQueryData,A=o.getQueryData,x=o.getQueryState,E=o.invalidateQuery,w=o.subscribeQuery,q=o.setDefaultConfig,I=o.dangerClearCache;
4
4
 
5
5
  exports.QueryManagerCore = h;
6
- exports.dangerClearCache = q;
6
+ exports.dangerClearCache = I;
7
7
  exports.fetchQuery = K;
8
- exports.getQueryData = w;
8
+ exports.getQueryData = A;
9
9
  exports.getQueryState = x;
10
10
  exports.invalidateQuery = E;
11
11
  exports.queryManager = o;
12
- exports.registerFetcher = b;
12
+ exports.registerFetcher = D;
13
13
  exports.serializeKey = c;
14
- exports.setDefaultConfig = I;
14
+ exports.setDefaultConfig = q;
15
15
  exports.setQueryData = O;
16
- exports.subscribeQuery = A;
16
+ exports.subscribeQuery = w;
package/index.mjs CHANGED
@@ -1,3 +1,3 @@
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;
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=100;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",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);queueMicrotask(()=>{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=Date.now(),s=r.updatedAt&&e-r.updatedAt<this.throttleTime;if(r.status==="fetching"||!r.enabled||s||!r.fetcher)return;let n=r.updatedAt==null||e-(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);}},S=h;var o=new S,D=o.registerFetcher,K=o.fetchQuery,O=o.setQueryData,A=o.getQueryData,x=o.getQueryState,E=o.invalidateQuery,w=o.subscribeQuery,q=o.setDefaultConfig,I=o.dangerClearCache;
2
2
 
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 };
3
+ export { h as QueryManagerCore, I as dangerClearCache, K as fetchQuery, A as getQueryData, x as getQueryState, E as invalidateQuery, o as queryManager, D as registerFetcher, c as serializeKey, q as setDefaultConfig, O as setQueryData, w as subscribeQuery };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qortex-core",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
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-45e8205b.d.ts",
31
+ "types-6c88db66.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-45e8205b.js';
2
- export { e as Persister } from './types-45e8205b.js';
1
+ import { P as PersisterConfig, B as BasePersister } from './types-6c88db66.js';
2
+ export { e as Persister } from './types-6c88db66.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.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)};
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.5";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.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)};
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.5";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 };
@@ -19,7 +19,6 @@ type QueryStateInternal<T = any, E = unknown> = {
19
19
  usePlaceholderOnError?: boolean;
20
20
  refetchOnSubscribe: "always" | "stale" | false;
21
21
  enabled: boolean;
22
- lastFetchTime?: number;
23
22
  fetchPromise?: Promise<T> | T;
24
23
  refetch?: () => Promise<T>;
25
24
  isSuccess: boolean;