qortex-core 0.3.4 → 0.3.6

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 f(s){return Array.isArray(s)?s.map(String).join("#"):String(s)}function d(s,e,r="shallow"){if(s===e)return !0;if(s==null||e==null)return s===e;if(typeof s!="object"||typeof e!="object")return !1;try{let t=r==="deep",n=Array.isArray(s),a=Array.isArray(e);if(n!==a)return !1;if(n){let o=s,l=e;return o.length!==l.length?!1:o.every((T,g)=>t?d(T,l[g],r):T===l[g])}let i=s,u=e,c=Object.keys(i);return c.length!==Object.keys(u).length?!1:c.every(o=>t?d(i[o],u[o],r):i[o]===u[o])}catch{return !1}}function h(s,e){return e||((r,t)=>d(r,t,s||"shallow"))}function S(s,e,r){return {key:s,status:"idle",updatedAt:void 0,staleTime:e?.staleTime??0,isInvalidated:!1,fetcher:e?.fetcher,equalityFn:h(e?.equalityStrategy,e?.equalityFn),equalityStrategy:e?.equalityStrategy??"shallow",placeholderData:e?.placeholderData,usePreviousDataOnError:e?.usePreviousDataOnError??!1,usePlaceholderOnError:e?.usePlaceholderOnError??!1,refetchOnSubscribe:e?.refetchOnSubscribe??"stale",enabled:e?.enabled!==!1,refetch:r||(()=>Promise.resolve(void 0)),isSuccess:!1,isError:!1,lastReturnedState:void 0,persist:e?.persist!==!1}}function p(s){let{status:e,data:r,placeholderData:t,updatedAt:n,staleTime:a,isInvalidated:i}=s,u=n!==void 0&&Date.now()-n>a||i,c,o=!1;return e==="error"?s.usePreviousDataOnError&&r!==void 0?c=r:s.usePlaceholderOnError&&t!==void 0&&(c=t,o=!0):(c=r??t,o=r===void 0&&t!==void 0),{data:c,error:s.error,status:e,updatedAt:n,isStale:u,isPlaceholderData:o,isLoading:e==="fetching"&&!n,isFetching:e==="fetching",isError:s.isError,isSuccess:s.isSuccess,refetch:s.refetch}}function F(s){console.warn(`[qortex] No fetcher or data for key "${f(s)}". Register a fetcher or set initial data.`);}var Q=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:e,persister:r,...t})=>{this.defaultConfig={...this.defaultConfig,...t},e!==void 0&&(this.throttleTime=e),r&&(this.persister=r,this.persister?.load(this.cache,this.hasQueriesBeenUsed));};this.registerFetcher=(e,r)=>{let t=this.ensureState(e,r);this.handleMountLogic(e,t);};this.fetchQuery=(e,r)=>{let t=this.ensureState(e,r);if(t.fetchPromise)return t.fetchPromise;let{fetcher:n}=t;if(!n)return t.updatedAt===void 0&&F(e),Promise.resolve(t.data);let a=n();Object.assign(t,{fetchPromise:a,status:"fetching"}),this.emit(e,t);let i=(u,c,o)=>{let l=h(t.equalityStrategy,t.equalityFn);Object.assign(t,{data:u&&c!==void 0?l(t.data,c)?t.data:c:t.data,error:u?void 0:o,status:u?"success":"error",isError:!u,isSuccess:u,updatedAt:Date.now(),fetchPromise:void 0}),this.emit(e,t);};return a.then(u=>i(!0,u)).catch(u=>i(!1,void 0,u)),a};this.setQueryData=(e,r)=>{let t=this.ensureState(e),n=t.data,a=typeof r=="function"?r(n):r;h(t.equalityStrategy,t.equalityFn)(n,a)||(Object.assign(t,{data:a,updatedAt:Date.now(),error:void 0,status:"success",isInvalidated:!1,isError:!1,isSuccess:!0}),this.emit(e,t));};this.getQueryData=(e,r)=>{let t=this.ensureState(e,r);return this.handleMountLogic(e,t),p(t).data};this.getQueryState=(e,r)=>{let t=this.ensureState(e,r);this.handleMountLogic(e,t);let n=p(t),{lastReturnedState:a}=t;return a&&d(a,n,"shallow")?a:(t.lastReturnedState=n,n)};this.invalidateQuery=e=>{let r=this.findMatchingStates(e);for(let t of r)t.isInvalidated=!0,this.emit(t.key,t),this.fetchQuery(t.key);};this.subscribeQuery=(e,r,t)=>{let n=f(e),a=this.ensureState(e,t),i=this.subs.get(n)??this.subs.set(n,new Set).get(n);return i.add(r),this.handleMountLogic(e,a),()=>i.delete(r)};}ensureState(e,r={}){this.hasQueriesBeenUsed=!0;let t=f(e),n=this.cache.get(t);if(n){let a=n.fromPersisterCache?{...n,...this.defaultConfig,...r}:{...this.defaultConfig,...n,...r};Object.assign(n,a,{enabled:a.enabled!==!1,persist:a.persist!==!1,fromPersisterCache:!1});}else n=S(t,{...this.defaultConfig,...r},()=>this.fetchQuery(e)),this.cache.set(t,n);return this.persister?.sync(this.cache),n}emit(e,r){let t=f(e);this.cache.set(t,r),this.persister?.sync(this.cache);let n=this.subs.get(t);if(!n)return;let a=p(r);queueMicrotask(()=>{for(let i of Array.from(n))i(a);});}findMatchingStates(e){let r=f(e);return [...this.cache.values()].filter(t=>t.key.startsWith(r+"#")||t.key===r)}handleMountLogic(e,r){if(r.status==="fetching"||!r.enabled||!r.fetcher)return;if(!r.updatedAt){this.fetchQuery(e);return}let t=Date.now()-r.updatedAt;if(t<this.throttleTime)return;let{refetchOnSubscribe:n}=r;n==="always"?this.fetchQuery(e):n==="stale"&&(t>r.staleTime||r.isInvalidated)&&this.fetchQuery(e);}},m=Q;var y=new m,E=y.registerFetcher,q=y.fetchQuery,w=y.setQueryData,I=y.getQueryData,P=y.getQueryState,C=y.invalidateQuery,A=y.subscribeQuery,R=y.setDefaultConfig,M=y.dangerClearCache;
4
4
 
5
- exports.QueryManagerCore = h;
6
- exports.dangerClearCache = q;
7
- exports.fetchQuery = K;
8
- exports.getQueryData = w;
9
- exports.getQueryState = x;
10
- exports.invalidateQuery = E;
11
- exports.queryManager = o;
12
- exports.registerFetcher = b;
13
- exports.serializeKey = c;
14
- exports.setDefaultConfig = I;
15
- exports.setQueryData = O;
5
+ exports.QueryManagerCore = Q;
6
+ exports.dangerClearCache = M;
7
+ exports.fetchQuery = q;
8
+ exports.getQueryData = I;
9
+ exports.getQueryState = P;
10
+ exports.invalidateQuery = C;
11
+ exports.queryManager = y;
12
+ exports.registerFetcher = E;
13
+ exports.serializeKey = f;
14
+ exports.setDefaultConfig = R;
15
+ exports.setQueryData = w;
16
16
  exports.subscribeQuery = A;
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 f(s){return Array.isArray(s)?s.map(String).join("#"):String(s)}function d(s,e,r="shallow"){if(s===e)return !0;if(s==null||e==null)return s===e;if(typeof s!="object"||typeof e!="object")return !1;try{let t=r==="deep",n=Array.isArray(s),a=Array.isArray(e);if(n!==a)return !1;if(n){let o=s,l=e;return o.length!==l.length?!1:o.every((T,g)=>t?d(T,l[g],r):T===l[g])}let i=s,u=e,c=Object.keys(i);return c.length!==Object.keys(u).length?!1:c.every(o=>t?d(i[o],u[o],r):i[o]===u[o])}catch{return !1}}function h(s,e){return e||((r,t)=>d(r,t,s||"shallow"))}function S(s,e,r){return {key:s,status:"idle",updatedAt:void 0,staleTime:e?.staleTime??0,isInvalidated:!1,fetcher:e?.fetcher,equalityFn:h(e?.equalityStrategy,e?.equalityFn),equalityStrategy:e?.equalityStrategy??"shallow",placeholderData:e?.placeholderData,usePreviousDataOnError:e?.usePreviousDataOnError??!1,usePlaceholderOnError:e?.usePlaceholderOnError??!1,refetchOnSubscribe:e?.refetchOnSubscribe??"stale",enabled:e?.enabled!==!1,refetch:r||(()=>Promise.resolve(void 0)),isSuccess:!1,isError:!1,lastReturnedState:void 0,persist:e?.persist!==!1}}function p(s){let{status:e,data:r,placeholderData:t,updatedAt:n,staleTime:a,isInvalidated:i}=s,u=n!==void 0&&Date.now()-n>a||i,c,o=!1;return e==="error"?s.usePreviousDataOnError&&r!==void 0?c=r:s.usePlaceholderOnError&&t!==void 0&&(c=t,o=!0):(c=r??t,o=r===void 0&&t!==void 0),{data:c,error:s.error,status:e,updatedAt:n,isStale:u,isPlaceholderData:o,isLoading:e==="fetching"&&!n,isFetching:e==="fetching",isError:s.isError,isSuccess:s.isSuccess,refetch:s.refetch}}function F(s){console.warn(`[qortex] No fetcher or data for key "${f(s)}". Register a fetcher or set initial data.`);}var Q=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:e,persister:r,...t})=>{this.defaultConfig={...this.defaultConfig,...t},e!==void 0&&(this.throttleTime=e),r&&(this.persister=r,this.persister?.load(this.cache,this.hasQueriesBeenUsed));};this.registerFetcher=(e,r)=>{let t=this.ensureState(e,r);this.handleMountLogic(e,t);};this.fetchQuery=(e,r)=>{let t=this.ensureState(e,r);if(t.fetchPromise)return t.fetchPromise;let{fetcher:n}=t;if(!n)return t.updatedAt===void 0&&F(e),Promise.resolve(t.data);let a=n();Object.assign(t,{fetchPromise:a,status:"fetching"}),this.emit(e,t);let i=(u,c,o)=>{let l=h(t.equalityStrategy,t.equalityFn);Object.assign(t,{data:u&&c!==void 0?l(t.data,c)?t.data:c:t.data,error:u?void 0:o,status:u?"success":"error",isError:!u,isSuccess:u,updatedAt:Date.now(),fetchPromise:void 0}),this.emit(e,t);};return a.then(u=>i(!0,u)).catch(u=>i(!1,void 0,u)),a};this.setQueryData=(e,r)=>{let t=this.ensureState(e),n=t.data,a=typeof r=="function"?r(n):r;h(t.equalityStrategy,t.equalityFn)(n,a)||(Object.assign(t,{data:a,updatedAt:Date.now(),error:void 0,status:"success",isInvalidated:!1,isError:!1,isSuccess:!0}),this.emit(e,t));};this.getQueryData=(e,r)=>{let t=this.ensureState(e,r);return this.handleMountLogic(e,t),p(t).data};this.getQueryState=(e,r)=>{let t=this.ensureState(e,r);this.handleMountLogic(e,t);let n=p(t),{lastReturnedState:a}=t;return a&&d(a,n,"shallow")?a:(t.lastReturnedState=n,n)};this.invalidateQuery=e=>{let r=this.findMatchingStates(e);for(let t of r)t.isInvalidated=!0,this.emit(t.key,t),this.fetchQuery(t.key);};this.subscribeQuery=(e,r,t)=>{let n=f(e),a=this.ensureState(e,t),i=this.subs.get(n)??this.subs.set(n,new Set).get(n);return i.add(r),this.handleMountLogic(e,a),()=>i.delete(r)};}ensureState(e,r={}){this.hasQueriesBeenUsed=!0;let t=f(e),n=this.cache.get(t);if(n){let a=n.fromPersisterCache?{...n,...this.defaultConfig,...r}:{...this.defaultConfig,...n,...r};Object.assign(n,a,{enabled:a.enabled!==!1,persist:a.persist!==!1,fromPersisterCache:!1});}else n=S(t,{...this.defaultConfig,...r},()=>this.fetchQuery(e)),this.cache.set(t,n);return this.persister?.sync(this.cache),n}emit(e,r){let t=f(e);this.cache.set(t,r),this.persister?.sync(this.cache);let n=this.subs.get(t);if(!n)return;let a=p(r);queueMicrotask(()=>{for(let i of Array.from(n))i(a);});}findMatchingStates(e){let r=f(e);return [...this.cache.values()].filter(t=>t.key.startsWith(r+"#")||t.key===r)}handleMountLogic(e,r){if(r.status==="fetching"||!r.enabled||!r.fetcher)return;if(!r.updatedAt){this.fetchQuery(e);return}let t=Date.now()-r.updatedAt;if(t<this.throttleTime)return;let{refetchOnSubscribe:n}=r;n==="always"?this.fetchQuery(e):n==="stale"&&(t>r.staleTime||r.isInvalidated)&&this.fetchQuery(e);}},m=Q;var y=new m,E=y.registerFetcher,q=y.fetchQuery,w=y.setQueryData,I=y.getQueryData,P=y.getQueryState,C=y.invalidateQuery,A=y.subscribeQuery,R=y.setDefaultConfig,M=y.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 { Q as QueryManagerCore, M as dangerClearCache, q as fetchQuery, I as getQueryData, P as getQueryState, C as invalidateQuery, y as queryManager, E as registerFetcher, f as serializeKey, R as setDefaultConfig, w as setQueryData, A 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.6",
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.6";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.6";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;