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 +3 -3
- package/index.js +12 -12
- package/index.mjs +2 -2
- package/package.json +2 -2
- package/persister.d.ts +2 -2
- package/persister.js +1 -1
- package/persister.mjs +1 -1
- package/{types-45e8205b.d.ts → types-6c88db66.d.ts} +0 -1
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-
|
|
2
|
-
export { E as EqualityFn, c as EqualityStrategy, d as QueryStatus, R as RefetchOnSubscribeOptions } from './types-
|
|
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: `
|
|
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
|
|
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 =
|
|
6
|
-
exports.dangerClearCache =
|
|
7
|
-
exports.fetchQuery =
|
|
8
|
-
exports.getQueryData =
|
|
9
|
-
exports.getQueryState =
|
|
10
|
-
exports.invalidateQuery =
|
|
11
|
-
exports.queryManager =
|
|
12
|
-
exports.registerFetcher =
|
|
13
|
-
exports.serializeKey =
|
|
14
|
-
exports.setDefaultConfig =
|
|
15
|
-
exports.setQueryData =
|
|
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
|
|
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 {
|
|
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.
|
|
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-
|
|
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-
|
|
2
|
-
export { e as Persister } from './types-
|
|
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.
|
|
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.
|
|
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;
|