qortex-core 0.2.9 → 0.3.0-beta.2
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 +7 -83
- package/index.js +2 -2
- package/index.mjs +2 -2
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -1,84 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* Using readonly to prevent accidental mutations
|
|
4
|
-
*/
|
|
5
|
-
type QueryKey = string | readonly (string | number)[];
|
|
6
|
-
/** Function that fetches data, must be async */
|
|
7
|
-
type Fetcher<T = any> = () => Promise<T>;
|
|
8
|
-
/** Function that compares two values for equality */
|
|
9
|
-
type EqualityFn<T = any> = (a: T | undefined, b: T | undefined) => boolean;
|
|
10
|
-
/** Strategy for equality comparison */
|
|
11
|
-
type EqualityStrategy = 'shallow' | 'deep';
|
|
12
|
-
/**
|
|
13
|
-
* Infers the return type of a fetcher function
|
|
14
|
-
*
|
|
15
|
-
* This utility type extracts the return type from a fetcher function,
|
|
16
|
-
* handling both synchronous and asynchronous fetchers.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```typescript
|
|
20
|
-
* const fetchUser = async (id: string): Promise<User> => { ... };
|
|
21
|
-
* type UserType = InferFetcherResult<typeof fetchUser>; // Promise<User>
|
|
22
|
-
*
|
|
23
|
-
* const fetchConfig = (): Config => { ... };
|
|
24
|
-
* type ConfigType = InferFetcherResult<typeof fetchConfig>; // Config
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @template F - The fetcher function type
|
|
28
|
-
* @returns The inferred return type of the fetcher, or `any` if inference fails
|
|
29
|
-
*/
|
|
30
|
-
type InferFetcherResult<F> = F extends Fetcher<infer R> ? R : any;
|
|
31
|
-
/**
|
|
32
|
-
* Query status types for better type safety
|
|
33
|
-
*/
|
|
34
|
-
type QueryStatus = "idle" | "fetching" | "success" | "error";
|
|
35
|
-
/**
|
|
36
|
-
* Comprehensive options for all query operations
|
|
37
|
-
* Improved with better type constraints
|
|
38
|
-
*/
|
|
39
|
-
type QueryOptions<T = any> = {
|
|
40
|
-
enabled?: boolean;
|
|
41
|
-
refetchOnSubscribe?: "always" | "stale" | false;
|
|
42
|
-
fetcher?: Fetcher<T>;
|
|
43
|
-
equalityFn?: EqualityFn<T>;
|
|
44
|
-
equalityStrategy?: EqualityStrategy;
|
|
45
|
-
staleTime?: number;
|
|
46
|
-
signal?: AbortSignal;
|
|
47
|
-
placeholderData?: T;
|
|
48
|
-
usePreviousDataOnError?: boolean;
|
|
49
|
-
usePlaceholderOnError?: boolean;
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Default configuration options that can be set globally
|
|
53
|
-
* Includes throttleTime which is not part of regular QueryOptions
|
|
54
|
-
*/
|
|
55
|
-
type DefaultConfig = {
|
|
56
|
-
enabled?: boolean;
|
|
57
|
-
refetchOnSubscribe?: "always" | "stale" | false;
|
|
58
|
-
staleTime?: number;
|
|
59
|
-
usePreviousDataOnError?: boolean;
|
|
60
|
-
usePlaceholderOnError?: boolean;
|
|
61
|
-
equalityFn?: EqualityFn<any>;
|
|
62
|
-
equalityStrategy?: EqualityStrategy;
|
|
63
|
-
throttleTime?: number;
|
|
64
|
-
};
|
|
65
|
-
/**
|
|
66
|
-
* Public query state returned by getQueryState
|
|
67
|
-
* Improved with stricter error typing and better generic constraints
|
|
68
|
-
*/
|
|
69
|
-
type QueryState<T = any, E = unknown> = {
|
|
70
|
-
data?: T;
|
|
71
|
-
error?: E;
|
|
72
|
-
status: QueryStatus;
|
|
73
|
-
updatedAt?: number;
|
|
74
|
-
isStale: boolean;
|
|
75
|
-
isPlaceholderData: boolean;
|
|
76
|
-
isLoading: boolean;
|
|
77
|
-
isFetching: boolean;
|
|
78
|
-
isError: boolean;
|
|
79
|
-
isSuccess: boolean;
|
|
80
|
-
refetch: () => Promise<T>;
|
|
81
|
-
};
|
|
1
|
+
import { D as DefaultConfig, Q as QueryKey, a as QueryOptions, F as Fetcher, I as InferFetcherResult, b as QueryState } from './types-e61a968e.js';
|
|
2
|
+
export { E as EqualityFn, c as EqualityStrategy, d as QueryStatus, R as RefetchOnSubscribeOptions } from './types-e61a968e.js';
|
|
82
3
|
|
|
83
4
|
/**
|
|
84
5
|
* Core query manager that handles caching, fetching, and state management
|
|
@@ -89,6 +10,8 @@ declare class QueryManagerCore {
|
|
|
89
10
|
private subs;
|
|
90
11
|
private defaultConfig;
|
|
91
12
|
private throttleTime;
|
|
13
|
+
private persister;
|
|
14
|
+
private hasQueriesBeenUsed;
|
|
92
15
|
/**
|
|
93
16
|
* ⚠️ DANGER: Clear all cached data and subscriptions
|
|
94
17
|
*
|
|
@@ -96,6 +19,7 @@ declare class QueryManagerCore {
|
|
|
96
19
|
* - All cached query data
|
|
97
20
|
* - All active subscriptions
|
|
98
21
|
* - All state references
|
|
22
|
+
* - All persisted data
|
|
99
23
|
*
|
|
100
24
|
* @warning This should ONLY be used in testing environments or when you need to completely reset the query manager state. Using this in production will cause all active queries to lose their data and subscriptions to break.
|
|
101
25
|
*
|
|
@@ -114,7 +38,7 @@ declare class QueryManagerCore {
|
|
|
114
38
|
/**
|
|
115
39
|
* Set default configuration for all queries
|
|
116
40
|
*/
|
|
117
|
-
setDefaultConfig({ throttleTime, ...config }: DefaultConfig): void;
|
|
41
|
+
setDefaultConfig({ throttleTime, persister, ...config }: DefaultConfig): void;
|
|
118
42
|
/**
|
|
119
43
|
* Ensures a query state exists in cache, creating it if necessary
|
|
120
44
|
* User-friendly with any fallback for better developer experience
|
|
@@ -201,4 +125,4 @@ declare const dangerClearCache: QueryManagerCore["dangerClearCache"];
|
|
|
201
125
|
*/
|
|
202
126
|
declare function serializeKey(key: QueryKey): string;
|
|
203
127
|
|
|
204
|
-
export { DefaultConfig,
|
|
128
|
+
export { DefaultConfig, Fetcher, InferFetcherResult, QueryKey, QueryManagerCore, QueryOptions, QueryState, _queryManager, dangerClearCache, fetchQuery, getQueryData, getQueryState, invalidateQuery, registerFetcher, serializeKey, setDefaultConfig, setQueryData, subscribeQuery };
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
function c(t){return Array.isArray(t)?t.join(","):String(t)}function
|
|
3
|
+
function c(t){return Array.isArray(t)?t.join(","):String(t)}function l(t,r,a="shallow"){if(t===r)return !0;if(t==null||r==null)return t===r;if(typeof t!="object"||typeof r!="object")return !1;try{let e=t,s=r;if(Array.isArray(e)&&Array.isArray(s)){if(e.length!==s.length)return !1;for(let o=0;o<e.length;o++)if(a==="deep"){if(!l(e[o],s[o],a))return !1}else if(e[o]!==s[o])return !1;return !0}if(Array.isArray(e)||Array.isArray(s))return !1;let n=Object.keys(e),u=Object.keys(s);if(n.length!==u.length)return !1;for(let o=0;o<n.length;o++){let y=n[o];if(a==="deep"){if(!l(e[y],s[y],a))return !1}else if(e[y]!==s[y])return !1}return !0}catch{return !1}}function g(t,r){return r||((a,e)=>l(a,e,t||"shallow"))}function h(t,r){return {status:"idle",updatedAt:void 0,staleTime:t?.staleTime??0,isInvalidated:!1,fetcher:t?.fetcher,equalityFn:g(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}}function d(t){let r=Date.now(),a=t.updatedAt!==void 0&&r-t.updatedAt>t.staleTime||t.isInvalidated,e,s=!1;switch(t.status){case"error":t.usePreviousDataOnError&&t.data!==void 0?e=t.data:t.usePlaceholderOnError&&t.placeholderData!==void 0&&(e=t.placeholderData,s=!0);break;case"fetching":t.data!==void 0?(e=t.data,s=!1):t.placeholderData&&(e=t.placeholderData,s=!0);break;case"success":case"idle":e=t.data??t.placeholderData,s=t.data?!1:!!t.placeholderData;break}return {data:e,error:t.error,status:t.status,updatedAt:t.updatedAt,isStale:a,isPlaceholderData:s,isLoading:t.status==="fetching"&&!t.updatedAt,isFetching:t.status==="fetching",isError:t.isError,isSuccess:t.isSuccess,refetch:t.refetch}}function Q(t){console.warn(`[qortex] No fetcher or data for key "${c(t)}". Register a fetcher or set initial data.`);}var f=class{constructor(){this.cache=new Map;this.subs=new Map;this.defaultConfig={};this.throttleTime=50;this.persister=null;this.hasQueriesBeenUsed=!1;}dangerClearCache(){this.cache.clear(),this.subs.clear(),this.persister?.clear();}setDefaultConfig({throttleTime:r,persister:a,...e}){this.defaultConfig={...this.defaultConfig,...e},r!==void 0&&(this.throttleTime=r),a&&(this.persister=a,this.persister?.load(this.cache,this.hasQueriesBeenUsed));}ensureState(r,a={}){this.hasQueriesBeenUsed=!0;let e=c(r),s=this.cache.get(e);if(s){let n={...this.defaultConfig,...s,...a};Object.assign(s,n),s.enabled=n.enabled!==!1,this.cache.set(e,s);}else {let n={...this.defaultConfig,...a},u=h(n,()=>this.fetchQuery(r));this.cache.set(e,u);}return this.cache.get(e)}emit(r,a){let e=c(r);this.cache.set(e,a),this.persister?.sync(this.cache);let s=this.subs.get(e);if(!s)return;let n=d(a);for(let u of Array.from(s))u(n);}registerFetcher(r,a){let e=this.ensureState(r,a);this.handleMountLogic(r,e);}fetchQuery(r,a){let e=this.ensureState(r,a);if(e.fetchPromise)return e.fetchPromise;let s=e.fetcher;if(!s)return e.updatedAt===void 0&&Q(r),Promise.resolve(e.data);let n=s();return e.fetchPromise=n,e.status="fetching",e.lastFetchTime=Date.now(),this.emit(r,e),n.then(u=>{e.data=e.equalityFn(e.data,u)?e.data:u,e.status="success",e.isError=!1,e.isSuccess=!0,e.updatedAt=Date.now(),e.fetchPromise=void 0,e.error=void 0,this.emit(r,e);}).catch(u=>{e.error=u,e.status="error",e.isError=!0,e.isSuccess=!1,e.updatedAt=Date.now(),e.fetchPromise=void 0,this.emit(r,e);}),n}setQueryData(r,a){let e=this.ensureState(r),s=e.data;e.equalityFn(s,a)||(e.data=a,e.updatedAt=Date.now(),e.error=void 0,e.status="success",e.isInvalidated=!1,e.isError=!1,e.isSuccess=!0,this.emit(r,e));}getQueryData(r,a){let e=this.ensureState(r,a);return this.handleMountLogic(r,e),d(e).data}getQueryState(r,a){let e=this.ensureState(r,a);this.handleMountLogic(r,e);let s=d(e),n=e.lastReturnedState;return !n||!l(n,s,"shallow")?(e.lastReturnedState=s,s):n}invalidateQuery(r){let a=this.ensureState(r);a.isInvalidated=!0,this.emit(r,a),this.fetchQuery(r);}subscribeQuery(r,a,e){let s=c(r),n=this.ensureState(r,e);return this.subs.has(s)||this.subs.set(s,new Set),this.subs.get(s).add(a),this.handleMountLogic(r,n),()=>{this.subs.get(s).delete(a);}}handleMountLogic(r,a){let e=a.lastFetchTime&&Date.now()-a.lastFetchTime<this.throttleTime;if(a.status==="fetching"||!a.enabled||e||!a.fetcher)return;let s=Date.now(),n=a.updatedAt==null||s-(a.updatedAt||0)>a.staleTime||a.isInvalidated,u=!1;a.updatedAt==null?u=!0:(a.refetchOnSubscribe==="always"&&(u=!0),a.refetchOnSubscribe==="stale"&&(u=n)),u&&this.fetchQuery(r);}},p=f;var i=new p,D=i.registerFetcher.bind(i),O=i.fetchQuery.bind(i),K=i.setQueryData.bind(i),C=i.getQueryData.bind(i),w=i.getQueryState.bind(i),x=i.invalidateQuery.bind(i),A=i.subscribeQuery.bind(i),E=i.setDefaultConfig.bind(i),I=i.dangerClearCache.bind(i);
|
|
4
4
|
|
|
5
5
|
exports.QueryManagerCore = f;
|
|
6
6
|
exports._queryManager = i;
|
|
@@ -9,7 +9,7 @@ exports.fetchQuery = O;
|
|
|
9
9
|
exports.getQueryData = C;
|
|
10
10
|
exports.getQueryState = w;
|
|
11
11
|
exports.invalidateQuery = x;
|
|
12
|
-
exports.registerFetcher =
|
|
12
|
+
exports.registerFetcher = D;
|
|
13
13
|
exports.serializeKey = c;
|
|
14
14
|
exports.setDefaultConfig = E;
|
|
15
15
|
exports.setQueryData = K;
|
package/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
function c(t){return Array.isArray(t)?t.join(","):String(t)}function
|
|
1
|
+
function c(t){return Array.isArray(t)?t.join(","):String(t)}function l(t,r,a="shallow"){if(t===r)return !0;if(t==null||r==null)return t===r;if(typeof t!="object"||typeof r!="object")return !1;try{let e=t,s=r;if(Array.isArray(e)&&Array.isArray(s)){if(e.length!==s.length)return !1;for(let o=0;o<e.length;o++)if(a==="deep"){if(!l(e[o],s[o],a))return !1}else if(e[o]!==s[o])return !1;return !0}if(Array.isArray(e)||Array.isArray(s))return !1;let n=Object.keys(e),u=Object.keys(s);if(n.length!==u.length)return !1;for(let o=0;o<n.length;o++){let y=n[o];if(a==="deep"){if(!l(e[y],s[y],a))return !1}else if(e[y]!==s[y])return !1}return !0}catch{return !1}}function g(t,r){return r||((a,e)=>l(a,e,t||"shallow"))}function h(t,r){return {status:"idle",updatedAt:void 0,staleTime:t?.staleTime??0,isInvalidated:!1,fetcher:t?.fetcher,equalityFn:g(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}}function d(t){let r=Date.now(),a=t.updatedAt!==void 0&&r-t.updatedAt>t.staleTime||t.isInvalidated,e,s=!1;switch(t.status){case"error":t.usePreviousDataOnError&&t.data!==void 0?e=t.data:t.usePlaceholderOnError&&t.placeholderData!==void 0&&(e=t.placeholderData,s=!0);break;case"fetching":t.data!==void 0?(e=t.data,s=!1):t.placeholderData&&(e=t.placeholderData,s=!0);break;case"success":case"idle":e=t.data??t.placeholderData,s=t.data?!1:!!t.placeholderData;break}return {data:e,error:t.error,status:t.status,updatedAt:t.updatedAt,isStale:a,isPlaceholderData:s,isLoading:t.status==="fetching"&&!t.updatedAt,isFetching:t.status==="fetching",isError:t.isError,isSuccess:t.isSuccess,refetch:t.refetch}}function Q(t){console.warn(`[qortex] No fetcher or data for key "${c(t)}". Register a fetcher or set initial data.`);}var f=class{constructor(){this.cache=new Map;this.subs=new Map;this.defaultConfig={};this.throttleTime=50;this.persister=null;this.hasQueriesBeenUsed=!1;}dangerClearCache(){this.cache.clear(),this.subs.clear(),this.persister?.clear();}setDefaultConfig({throttleTime:r,persister:a,...e}){this.defaultConfig={...this.defaultConfig,...e},r!==void 0&&(this.throttleTime=r),a&&(this.persister=a,this.persister?.load(this.cache,this.hasQueriesBeenUsed));}ensureState(r,a={}){this.hasQueriesBeenUsed=!0;let e=c(r),s=this.cache.get(e);if(s){let n={...this.defaultConfig,...s,...a};Object.assign(s,n),s.enabled=n.enabled!==!1,this.cache.set(e,s);}else {let n={...this.defaultConfig,...a},u=h(n,()=>this.fetchQuery(r));this.cache.set(e,u);}return this.cache.get(e)}emit(r,a){let e=c(r);this.cache.set(e,a),this.persister?.sync(this.cache);let s=this.subs.get(e);if(!s)return;let n=d(a);for(let u of Array.from(s))u(n);}registerFetcher(r,a){let e=this.ensureState(r,a);this.handleMountLogic(r,e);}fetchQuery(r,a){let e=this.ensureState(r,a);if(e.fetchPromise)return e.fetchPromise;let s=e.fetcher;if(!s)return e.updatedAt===void 0&&Q(r),Promise.resolve(e.data);let n=s();return e.fetchPromise=n,e.status="fetching",e.lastFetchTime=Date.now(),this.emit(r,e),n.then(u=>{e.data=e.equalityFn(e.data,u)?e.data:u,e.status="success",e.isError=!1,e.isSuccess=!0,e.updatedAt=Date.now(),e.fetchPromise=void 0,e.error=void 0,this.emit(r,e);}).catch(u=>{e.error=u,e.status="error",e.isError=!0,e.isSuccess=!1,e.updatedAt=Date.now(),e.fetchPromise=void 0,this.emit(r,e);}),n}setQueryData(r,a){let e=this.ensureState(r),s=e.data;e.equalityFn(s,a)||(e.data=a,e.updatedAt=Date.now(),e.error=void 0,e.status="success",e.isInvalidated=!1,e.isError=!1,e.isSuccess=!0,this.emit(r,e));}getQueryData(r,a){let e=this.ensureState(r,a);return this.handleMountLogic(r,e),d(e).data}getQueryState(r,a){let e=this.ensureState(r,a);this.handleMountLogic(r,e);let s=d(e),n=e.lastReturnedState;return !n||!l(n,s,"shallow")?(e.lastReturnedState=s,s):n}invalidateQuery(r){let a=this.ensureState(r);a.isInvalidated=!0,this.emit(r,a),this.fetchQuery(r);}subscribeQuery(r,a,e){let s=c(r),n=this.ensureState(r,e);return this.subs.has(s)||this.subs.set(s,new Set),this.subs.get(s).add(a),this.handleMountLogic(r,n),()=>{this.subs.get(s).delete(a);}}handleMountLogic(r,a){let e=a.lastFetchTime&&Date.now()-a.lastFetchTime<this.throttleTime;if(a.status==="fetching"||!a.enabled||e||!a.fetcher)return;let s=Date.now(),n=a.updatedAt==null||s-(a.updatedAt||0)>a.staleTime||a.isInvalidated,u=!1;a.updatedAt==null?u=!0:(a.refetchOnSubscribe==="always"&&(u=!0),a.refetchOnSubscribe==="stale"&&(u=n)),u&&this.fetchQuery(r);}},p=f;var i=new p,D=i.registerFetcher.bind(i),O=i.fetchQuery.bind(i),K=i.setQueryData.bind(i),C=i.getQueryData.bind(i),w=i.getQueryState.bind(i),x=i.invalidateQuery.bind(i),A=i.subscribeQuery.bind(i),E=i.setDefaultConfig.bind(i),I=i.dangerClearCache.bind(i);
|
|
2
2
|
|
|
3
|
-
export { f as QueryManagerCore, i as _queryManager, I as dangerClearCache, O as fetchQuery, C as getQueryData, w as getQueryState, x as invalidateQuery,
|
|
3
|
+
export { f as QueryManagerCore, i as _queryManager, I as dangerClearCache, O as fetchQuery, C as getQueryData, w as getQueryState, x as invalidateQuery, D as registerFetcher, c as serializeKey, E as setDefaultConfig, K as setQueryData, A as subscribeQuery };
|