@qiaopeng/tanstack-query-plus 0.5.8 → 0.5.10
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/README.md +155 -95
- package/dist/{types/base.d.ts → base-CHnwqfyz.d.cts} +12 -14
- package/dist/base-CHnwqfyz.d.ts +52 -0
- package/dist/chunk-52ZO6Y67.cjs +1121 -0
- package/dist/chunk-52ZO6Y67.cjs.map +1 -0
- package/dist/chunk-5J6OXSLW.cjs +36 -0
- package/dist/chunk-5J6OXSLW.cjs.map +1 -0
- package/dist/chunk-6MAYHLTE.cjs +310 -0
- package/dist/chunk-6MAYHLTE.cjs.map +1 -0
- package/dist/chunk-ADS2QTMP.js +144 -0
- package/dist/chunk-ADS2QTMP.js.map +1 -0
- package/dist/chunk-APXNNHBD.cjs +374 -0
- package/dist/chunk-APXNNHBD.cjs.map +1 -0
- package/dist/chunk-AXMWOGNX.js +134 -0
- package/dist/chunk-AXMWOGNX.js.map +1 -0
- package/dist/chunk-B4KO3K3E.cjs +521 -0
- package/dist/chunk-B4KO3K3E.cjs.map +1 -0
- package/dist/chunk-BK3OTIBD.cjs +15 -0
- package/dist/chunk-BK3OTIBD.cjs.map +1 -0
- package/dist/chunk-BYAOQALW.js +13 -0
- package/dist/chunk-BYAOQALW.js.map +1 -0
- package/dist/chunk-CRTVS7VI.cjs +162 -0
- package/dist/chunk-CRTVS7VI.cjs.map +1 -0
- package/dist/chunk-EXITP7QO.js +288 -0
- package/dist/chunk-EXITP7QO.js.map +1 -0
- package/dist/chunk-GMO3PRZZ.js +565 -0
- package/dist/chunk-GMO3PRZZ.js.map +1 -0
- package/dist/chunk-HRO2DWKZ.js +12 -0
- package/dist/chunk-HRO2DWKZ.js.map +1 -0
- package/dist/chunk-JHDKUQSB.js +1069 -0
- package/dist/chunk-JHDKUQSB.js.map +1 -0
- package/dist/chunk-JN2Y6RSY.js +23 -0
- package/dist/chunk-JN2Y6RSY.js.map +1 -0
- package/dist/chunk-JRJSKRZW.cjs +29 -0
- package/dist/chunk-JRJSKRZW.cjs.map +1 -0
- package/dist/chunk-KC62H4VJ.js +123 -0
- package/dist/chunk-KC62H4VJ.js.map +1 -0
- package/dist/chunk-LHEHSLD5.js +31 -0
- package/dist/chunk-LHEHSLD5.js.map +1 -0
- package/dist/chunk-N4264P7N.cjs +156 -0
- package/dist/chunk-N4264P7N.cjs.map +1 -0
- package/dist/chunk-NF5QYPYC.cjs +133 -0
- package/dist/chunk-NF5QYPYC.cjs.map +1 -0
- package/dist/chunk-OFLCHKKE.cjs +28 -0
- package/dist/chunk-OFLCHKKE.cjs.map +1 -0
- package/dist/chunk-PCNSWVA5.cjs +602 -0
- package/dist/chunk-PCNSWVA5.cjs.map +1 -0
- package/dist/chunk-STOMAA2X.js +85 -0
- package/dist/chunk-STOMAA2X.js.map +1 -0
- package/dist/chunk-UVF5S6LX.cjs +15 -0
- package/dist/chunk-UVF5S6LX.cjs.map +1 -0
- package/dist/chunk-WEIXCDCA.cjs +90 -0
- package/dist/chunk-WEIXCDCA.cjs.map +1 -0
- package/dist/chunk-X3ZTSLBQ.js +355 -0
- package/dist/chunk-X3ZTSLBQ.js.map +1 -0
- package/dist/chunk-YEV73J4J.js +504 -0
- package/dist/chunk-YEV73J4J.js.map +1 -0
- package/dist/chunk-YW5PNTRU.cjs +14 -0
- package/dist/chunk-YW5PNTRU.cjs.map +1 -0
- package/dist/chunk-ZNXSWUIS.js +12 -0
- package/dist/chunk-ZNXSWUIS.js.map +1 -0
- package/dist/chunk-ZUEMBY4W.js +21 -0
- package/dist/chunk-ZUEMBY4W.js.map +1 -0
- package/dist/components/index.cjs +60 -0
- package/dist/components/index.cjs.map +1 -0
- package/dist/components/index.d.cts +43 -0
- package/dist/components/index.d.ts +43 -4
- package/dist/components/index.js +3 -4
- package/dist/components/index.js.map +1 -0
- package/dist/core/devtools.cjs +25 -0
- package/dist/core/devtools.cjs.map +1 -0
- package/dist/core/devtools.d.cts +17 -0
- package/dist/core/devtools.d.ts +9 -7
- package/dist/core/devtools.js +4 -16
- package/dist/core/devtools.js.map +1 -0
- package/dist/core/index.cjs +220 -0
- package/dist/core/index.cjs.map +1 -0
- package/dist/core/index.d.cts +196 -0
- package/dist/core/index.d.ts +196 -9
- package/dist/core/index.js +7 -8
- package/dist/core/index.js.map +1 -0
- package/dist/features/index.cjs +76 -0
- package/dist/features/index.cjs.map +1 -0
- package/dist/features/index.d.cts +86 -0
- package/dist/features/index.d.ts +86 -4
- package/dist/features/index.js +7 -3
- package/dist/features/index.js.map +1 -0
- package/dist/hooks/index.cjs +209 -0
- package/dist/hooks/index.cjs.map +1 -0
- package/dist/hooks/index.d.cts +377 -0
- package/dist/hooks/index.d.ts +377 -10
- package/dist/hooks/index.js +8 -9
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useInViewPrefetch.cjs +33 -0
- package/dist/hooks/useInViewPrefetch.cjs.map +1 -0
- package/dist/hooks/useInViewPrefetch.d.cts +12 -0
- package/dist/hooks/useInViewPrefetch.d.ts +6 -4
- package/dist/hooks/useInViewPrefetch.js +30 -20
- package/dist/hooks/useInViewPrefetch.js.map +1 -0
- package/dist/index.cjs +811 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +36 -0
- package/dist/index.d.ts +36 -8
- package/dist/index.js +89 -7
- package/dist/index.js.map +1 -0
- package/dist/{types/offline.d.ts → offline-DVPtgoAS.d.ts} +15 -13
- package/dist/offline-xxeA_-6V.d.cts +99 -0
- package/dist/persistence-MRtkmhqq.d.cts +216 -0
- package/dist/persistence-tIrEb0pR.d.ts +216 -0
- package/dist/react-query/index.cjs +52 -0
- package/dist/react-query/index.cjs.map +1 -0
- package/dist/react-query/index.d.cts +1 -0
- package/dist/react-query/index.d.ts +1 -3
- package/dist/react-query/index.js +3 -1
- package/dist/react-query/index.js.map +1 -0
- package/dist/types/index.cjs +43 -0
- package/dist/types/index.cjs.map +1 -0
- package/dist/types/index.d.cts +157 -0
- package/dist/types/index.d.ts +116 -12
- package/dist/types/index.js +6 -8
- package/dist/types/index.js.map +1 -0
- package/dist/utils/index.cjs +234 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +272 -0
- package/dist/utils/index.d.ts +272 -10
- package/dist/utils/index.js +9 -9
- package/dist/utils/index.js.map +1 -0
- package/package.json +13 -3
- package/dist/PersistQueryClientProvider.d.ts +0 -22
- package/dist/PersistQueryClientProvider.d.ts.map +0 -1
- package/dist/PersistQueryClientProvider.js +0 -47
- package/dist/components/LoadingFallback.d.ts +0 -16
- package/dist/components/LoadingFallback.d.ts.map +0 -1
- package/dist/components/LoadingFallback.js +0 -27
- package/dist/components/QueryErrorBoundary.d.ts +0 -12
- package/dist/components/QueryErrorBoundary.d.ts.map +0 -1
- package/dist/components/QueryErrorBoundary.js +0 -9
- package/dist/components/SuspenseWrapper.d.ts +0 -14
- package/dist/components/SuspenseWrapper.d.ts.map +0 -1
- package/dist/components/SuspenseWrapper.js +0 -9
- package/dist/components/index.d.ts.map +0 -1
- package/dist/components/internal/ErrorBoundary.d.ts +0 -27
- package/dist/components/internal/ErrorBoundary.d.ts.map +0 -1
- package/dist/components/internal/ErrorBoundary.js +0 -37
- package/dist/core/config.d.ts +0 -80
- package/dist/core/config.d.ts.map +0 -1
- package/dist/core/config.js +0 -321
- package/dist/core/devtools.d.ts.map +0 -1
- package/dist/core/env.d.ts +0 -4
- package/dist/core/env.d.ts.map +0 -1
- package/dist/core/env.js +0 -26
- package/dist/core/focusManager.d.ts +0 -33
- package/dist/core/focusManager.d.ts.map +0 -1
- package/dist/core/focusManager.js +0 -122
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/keys.d.ts +0 -59
- package/dist/core/keys.d.ts.map +0 -1
- package/dist/core/keys.js +0 -107
- package/dist/core/queryOptions.d.ts +0 -19
- package/dist/core/queryOptions.d.ts.map +0 -1
- package/dist/core/queryOptions.js +0 -44
- package/dist/features/index.d.ts.map +0 -1
- package/dist/features/offline.d.ts +0 -48
- package/dist/features/offline.d.ts.map +0 -1
- package/dist/features/offline.js +0 -269
- package/dist/features/persistence.d.ts +0 -36
- package/dist/features/persistence.d.ts.map +0 -1
- package/dist/features/persistence.js +0 -186
- package/dist/hooks/batchQueries.d.ts +0 -129
- package/dist/hooks/batchQueries.d.ts.map +0 -1
- package/dist/hooks/batchQueries.js +0 -301
- package/dist/hooks/index.d.ts.map +0 -1
- package/dist/hooks/useDataGuardMutation.d.ts +0 -39
- package/dist/hooks/useDataGuardMutation.d.ts.map +0 -1
- package/dist/hooks/useDataGuardMutation.js +0 -151
- package/dist/hooks/useDataGuardQuery.d.ts +0 -28
- package/dist/hooks/useDataGuardQuery.d.ts.map +0 -1
- package/dist/hooks/useDataGuardQuery.js +0 -71
- package/dist/hooks/useFocusManager.d.ts +0 -41
- package/dist/hooks/useFocusManager.d.ts.map +0 -1
- package/dist/hooks/useFocusManager.js +0 -109
- package/dist/hooks/useInViewPrefetch.d.ts.map +0 -1
- package/dist/hooks/useInfiniteQuery.d.ts +0 -33
- package/dist/hooks/useInfiniteQuery.d.ts.map +0 -1
- package/dist/hooks/useInfiniteQuery.js +0 -61
- package/dist/hooks/useMutation.d.ts +0 -25
- package/dist/hooks/useMutation.d.ts.map +0 -1
- package/dist/hooks/useMutation.js +0 -180
- package/dist/hooks/usePrefetch.d.ts +0 -54
- package/dist/hooks/usePrefetch.d.ts.map +0 -1
- package/dist/hooks/usePrefetch.js +0 -229
- package/dist/hooks/useQuery.d.ts +0 -21
- package/dist/hooks/useQuery.d.ts.map +0 -1
- package/dist/hooks/useQuery.js +0 -46
- package/dist/hooks/useSuspenseQuery.d.ts +0 -11
- package/dist/hooks/useSuspenseQuery.d.ts.map +0 -1
- package/dist/hooks/useSuspenseQuery.js +0 -19
- package/dist/index.d.ts.map +0 -1
- package/dist/react-query/index.d.ts.map +0 -1
- package/dist/types/base.d.ts.map +0 -1
- package/dist/types/base.js +0 -26
- package/dist/types/dataGuard.d.ts +0 -69
- package/dist/types/dataGuard.d.ts.map +0 -1
- package/dist/types/dataGuard.js +0 -10
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/infinite.d.ts +0 -39
- package/dist/types/infinite.d.ts.map +0 -1
- package/dist/types/infinite.js +0 -1
- package/dist/types/offline.d.ts.map +0 -1
- package/dist/types/offline.js +0 -8
- package/dist/types/optimistic.d.ts +0 -126
- package/dist/types/optimistic.d.ts.map +0 -1
- package/dist/types/optimistic.js +0 -7
- package/dist/types/persistence.d.ts +0 -9
- package/dist/types/persistence.d.ts.map +0 -1
- package/dist/types/persistence.js +0 -1
- package/dist/types/selectors.d.ts +0 -11
- package/dist/types/selectors.d.ts.map +0 -1
- package/dist/types/selectors.js +0 -1
- package/dist/types/suspense.d.ts +0 -67
- package/dist/types/suspense.d.ts.map +0 -1
- package/dist/types/suspense.js +0 -1
- package/dist/utils/dataGuard.d.ts +0 -37
- package/dist/utils/dataGuard.d.ts.map +0 -1
- package/dist/utils/dataGuard.js +0 -257
- package/dist/utils/fieldMapper.d.ts +0 -9
- package/dist/utils/fieldMapper.d.ts.map +0 -1
- package/dist/utils/fieldMapper.js +0 -27
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/network.d.ts +0 -9
- package/dist/utils/network.d.ts.map +0 -1
- package/dist/utils/network.js +0 -43
- package/dist/utils/optimisticUtils.d.ts +0 -45
- package/dist/utils/optimisticUtils.d.ts.map +0 -1
- package/dist/utils/optimisticUtils.js +0 -116
- package/dist/utils/placeholderData.d.ts +0 -3
- package/dist/utils/placeholderData.d.ts.map +0 -1
- package/dist/utils/placeholderData.js +0 -28
- package/dist/utils/prefetchManager.d.ts +0 -111
- package/dist/utils/prefetchManager.d.ts.map +0 -1
- package/dist/utils/prefetchManager.js +0 -246
- package/dist/utils/queryKey.d.ts +0 -24
- package/dist/utils/queryKey.d.ts.map +0 -1
- package/dist/utils/queryKey.js +0 -77
- package/dist/utils/selectors.d.ts +0 -30
- package/dist/utils/selectors.d.ts.map +0 -1
- package/dist/utils/selectors.js +0 -18
- package/dist/utils/storage.d.ts +0 -7
- package/dist/utils/storage.d.ts.map +0 -1
- package/dist/utils/storage.js +0 -84
|
@@ -0,0 +1,1121 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkYW5PNTRU_cjs = require('./chunk-YW5PNTRU.cjs');
|
|
4
|
+
var chunkAPXNNHBD_cjs = require('./chunk-APXNNHBD.cjs');
|
|
5
|
+
var chunkNF5QYPYC_cjs = require('./chunk-NF5QYPYC.cjs');
|
|
6
|
+
var chunk6MAYHLTE_cjs = require('./chunk-6MAYHLTE.cjs');
|
|
7
|
+
var chunkOFLCHKKE_cjs = require('./chunk-OFLCHKKE.cjs');
|
|
8
|
+
var reactQuery = require('@tanstack/react-query');
|
|
9
|
+
var react = require('react');
|
|
10
|
+
|
|
11
|
+
function createErrorAggregate(results, queries) {
|
|
12
|
+
const errors = [];
|
|
13
|
+
const errorsByType = /* @__PURE__ */ new Map();
|
|
14
|
+
results.forEach((result, index) => {
|
|
15
|
+
if (result.isError && result.error) {
|
|
16
|
+
const error = result.error;
|
|
17
|
+
const queryKey = queries[index]?.queryKey;
|
|
18
|
+
errors.push({ index, error, queryKey });
|
|
19
|
+
const errorType = error instanceof Error ? error.constructor.name : "Unknown";
|
|
20
|
+
if (!errorsByType.has(errorType)) {
|
|
21
|
+
errorsByType.set(errorType, []);
|
|
22
|
+
}
|
|
23
|
+
errorsByType.get(errorType).push(error);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const firstError = errors.length > 0 ? errors[0].error : null;
|
|
27
|
+
const lastError = errors.length > 0 ? errors[errors.length - 1].error : null;
|
|
28
|
+
const errorSummary = errors.length === 0 ? "No errors" : errors.length === 1 ? `1 error: ${firstError instanceof Error ? firstError.message : String(firstError)}` : `${errors.length} errors: ${Array.from(errorsByType.entries()).map(([type, errs]) => `${type}(${errs.length})`).join(", ")}`;
|
|
29
|
+
return { totalErrors: errors.length, errors, errorsByType, firstError, lastError, errorSummary };
|
|
30
|
+
}
|
|
31
|
+
function createOperationReport(results, queries, startTime, retryCount = 0) {
|
|
32
|
+
const total = results.length;
|
|
33
|
+
const successResults = [];
|
|
34
|
+
const failureErrors = [];
|
|
35
|
+
results.forEach((result, index) => {
|
|
36
|
+
if (result.isSuccess && result.data !== void 0) {
|
|
37
|
+
successResults.push({ index, data: result.data });
|
|
38
|
+
} else if (result.isError && result.error) {
|
|
39
|
+
failureErrors.push({ index, error: result.error, queryKey: queries[index]?.queryKey });
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
const successful = successResults.length;
|
|
43
|
+
const failed = failureErrors.length;
|
|
44
|
+
const isFullSuccess = successful === total && failed === 0;
|
|
45
|
+
const isFullFailure = failed === total && successful === 0;
|
|
46
|
+
const isPartialSuccess = successful > 0 && failed > 0;
|
|
47
|
+
return { total, successful, failed, successResults, failureErrors, isPartialSuccess, isFullSuccess, isFullFailure, duration: Date.now() - startTime, retryCount };
|
|
48
|
+
}
|
|
49
|
+
async function executeBatchOperationWithRetry(operation, retryConfig) {
|
|
50
|
+
const maxRetries = retryConfig?.maxRetries ?? 0;
|
|
51
|
+
const retryDelay = retryConfig?.retryDelay ?? 1e3;
|
|
52
|
+
const shouldRetry = retryConfig?.shouldRetry;
|
|
53
|
+
let retryCount = 0;
|
|
54
|
+
let results = await operation();
|
|
55
|
+
if (maxRetries === 0) {
|
|
56
|
+
return { results, retryCount };
|
|
57
|
+
}
|
|
58
|
+
while (retryCount < maxRetries) {
|
|
59
|
+
const hasFailures = results.some((r) => r.status === "rejected");
|
|
60
|
+
if (!hasFailures) break;
|
|
61
|
+
if (shouldRetry) {
|
|
62
|
+
const firstError = results.find((r) => r.status === "rejected");
|
|
63
|
+
if (firstError && firstError.status === "rejected") {
|
|
64
|
+
const error = firstError.reason instanceof Error ? firstError.reason : new Error(String(firstError.reason));
|
|
65
|
+
if (!shouldRetry(error, retryCount + 1)) {
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const delay = typeof retryDelay === "function" ? retryDelay(retryCount) : retryDelay;
|
|
71
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
72
|
+
retryCount++;
|
|
73
|
+
results = await operation();
|
|
74
|
+
}
|
|
75
|
+
return { results, retryCount };
|
|
76
|
+
}
|
|
77
|
+
function calculateBatchStats(results) {
|
|
78
|
+
const total = results.length;
|
|
79
|
+
const loading = results.filter((result) => result.isLoading).length;
|
|
80
|
+
const success = results.filter((result) => result.isSuccess).length;
|
|
81
|
+
const error = results.filter((result) => result.isError).length;
|
|
82
|
+
const stale = results.filter((result) => result.isStale).length;
|
|
83
|
+
return { total, loading, success, error, stale, successRate: total > 0 ? success / total * 100 : 0, errorRate: total > 0 ? error / total * 100 : 0 };
|
|
84
|
+
}
|
|
85
|
+
function useEnhancedQueries(queries, config = {}) {
|
|
86
|
+
const queryClient = reactQuery.useQueryClient();
|
|
87
|
+
const { results, stats } = reactQuery.useQueries({ queries, combine: (results2) => ({ results: results2, stats: calculateBatchStats(results2) }) });
|
|
88
|
+
const retryCountRef = react.useRef(0);
|
|
89
|
+
const operations = {
|
|
90
|
+
_queryClient: queryClient,
|
|
91
|
+
_queries: queries,
|
|
92
|
+
_results: results,
|
|
93
|
+
refetchAll: async () => {
|
|
94
|
+
const operation = () => {
|
|
95
|
+
const promises = queries.map((query) => queryClient.refetchQueries({ queryKey: query.queryKey, exact: true }));
|
|
96
|
+
return Promise.allSettled(promises);
|
|
97
|
+
};
|
|
98
|
+
const { results: settledResults, retryCount } = await executeBatchOperationWithRetry(operation, config.retryConfig);
|
|
99
|
+
retryCountRef.current = retryCount;
|
|
100
|
+
if (config.enablePartialSuccess) {
|
|
101
|
+
const report = operations.getOperationReport();
|
|
102
|
+
if (report.isPartialSuccess && config.onPartialSuccess) {
|
|
103
|
+
config.onPartialSuccess(report);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return settledResults;
|
|
107
|
+
},
|
|
108
|
+
invalidateAll: async () => {
|
|
109
|
+
const operation = () => {
|
|
110
|
+
const promises = queries.map((query) => queryClient.invalidateQueries({ queryKey: query.queryKey, exact: true }));
|
|
111
|
+
return Promise.allSettled(promises);
|
|
112
|
+
};
|
|
113
|
+
const { results: settledResults, retryCount } = await executeBatchOperationWithRetry(operation, config.retryConfig);
|
|
114
|
+
retryCountRef.current = retryCount;
|
|
115
|
+
return settledResults;
|
|
116
|
+
},
|
|
117
|
+
cancelAll: async () => {
|
|
118
|
+
const promises = queries.map((query) => queryClient.cancelQueries({ queryKey: query.queryKey, exact: true }));
|
|
119
|
+
return Promise.allSettled(promises);
|
|
120
|
+
},
|
|
121
|
+
resetAll: async () => {
|
|
122
|
+
const operation = () => {
|
|
123
|
+
const promises = queries.map((query) => queryClient.resetQueries({ queryKey: query.queryKey, exact: true }));
|
|
124
|
+
return Promise.allSettled(promises);
|
|
125
|
+
};
|
|
126
|
+
const { results: settledResults, retryCount } = await executeBatchOperationWithRetry(operation, config.retryConfig);
|
|
127
|
+
retryCountRef.current = retryCount;
|
|
128
|
+
return settledResults;
|
|
129
|
+
},
|
|
130
|
+
removeAll: () => {
|
|
131
|
+
queries.forEach((query) => {
|
|
132
|
+
queryClient.removeQueries({ queryKey: query.queryKey, exact: true });
|
|
133
|
+
});
|
|
134
|
+
},
|
|
135
|
+
retryFailed: async () => {
|
|
136
|
+
const failedIndices = results.map((result, index) => result.isError ? index : -1).filter((index) => index !== -1);
|
|
137
|
+
if (failedIndices.length === 0) {
|
|
138
|
+
return createOperationReport(results, queries, 0, 0);
|
|
139
|
+
}
|
|
140
|
+
const retryPromises = failedIndices.map((index) => {
|
|
141
|
+
const query = queries[index];
|
|
142
|
+
return queryClient.refetchQueries({ queryKey: query.queryKey, exact: true });
|
|
143
|
+
});
|
|
144
|
+
await Promise.allSettled(retryPromises);
|
|
145
|
+
retryCountRef.current++;
|
|
146
|
+
return operations.getOperationReport();
|
|
147
|
+
},
|
|
148
|
+
getErrorAggregate: () => {
|
|
149
|
+
return createErrorAggregate(results, queries);
|
|
150
|
+
},
|
|
151
|
+
getOperationReport: () => {
|
|
152
|
+
return createOperationReport(results, queries, 0, retryCountRef.current);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
return { data: results, stats, operations, config };
|
|
156
|
+
}
|
|
157
|
+
function useEnhancedSuspenseQueries(queries, config = {}) {
|
|
158
|
+
const queryClient = reactQuery.useQueryClient();
|
|
159
|
+
const { results, stats } = reactQuery.useSuspenseQueries({ queries, combine: (results2) => ({ results: results2, stats: calculateBatchStats(results2) }) });
|
|
160
|
+
const retryCountRef = react.useRef(0);
|
|
161
|
+
const operations = {
|
|
162
|
+
_queryClient: queryClient,
|
|
163
|
+
_queries: queries,
|
|
164
|
+
_results: results,
|
|
165
|
+
refetchAll: async () => {
|
|
166
|
+
const operation = () => {
|
|
167
|
+
const promises = queries.map((query) => queryClient.refetchQueries({ queryKey: query.queryKey, exact: true }));
|
|
168
|
+
return Promise.allSettled(promises);
|
|
169
|
+
};
|
|
170
|
+
const { results: settledResults, retryCount } = await executeBatchOperationWithRetry(operation, config.retryConfig);
|
|
171
|
+
retryCountRef.current = retryCount;
|
|
172
|
+
if (config.enablePartialSuccess) {
|
|
173
|
+
const report = operations.getOperationReport();
|
|
174
|
+
if (report.isPartialSuccess && config.onPartialSuccess) {
|
|
175
|
+
config.onPartialSuccess(report);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return settledResults;
|
|
179
|
+
},
|
|
180
|
+
invalidateAll: async () => {
|
|
181
|
+
const operation = () => {
|
|
182
|
+
const promises = queries.map((query) => queryClient.invalidateQueries({ queryKey: query.queryKey, exact: true }));
|
|
183
|
+
return Promise.allSettled(promises);
|
|
184
|
+
};
|
|
185
|
+
const { results: settledResults, retryCount } = await executeBatchOperationWithRetry(operation, config.retryConfig);
|
|
186
|
+
retryCountRef.current = retryCount;
|
|
187
|
+
return settledResults;
|
|
188
|
+
},
|
|
189
|
+
cancelAll: async () => {
|
|
190
|
+
const promises = queries.map((query) => queryClient.cancelQueries({ queryKey: query.queryKey, exact: true }));
|
|
191
|
+
return Promise.allSettled(promises);
|
|
192
|
+
},
|
|
193
|
+
resetAll: async () => {
|
|
194
|
+
const operation = () => {
|
|
195
|
+
const promises = queries.map((query) => queryClient.resetQueries({ queryKey: query.queryKey, exact: true }));
|
|
196
|
+
return Promise.allSettled(promises);
|
|
197
|
+
};
|
|
198
|
+
const { results: settledResults, retryCount } = await executeBatchOperationWithRetry(operation, config.retryConfig);
|
|
199
|
+
retryCountRef.current = retryCount;
|
|
200
|
+
return settledResults;
|
|
201
|
+
},
|
|
202
|
+
removeAll: () => {
|
|
203
|
+
queries.forEach((query) => {
|
|
204
|
+
queryClient.removeQueries({ queryKey: query.queryKey, exact: true });
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
retryFailed: async () => {
|
|
208
|
+
const failedIndices = results.map((result, index) => result.isError ? index : -1).filter((index) => index !== -1);
|
|
209
|
+
if (failedIndices.length === 0) {
|
|
210
|
+
return createOperationReport(results, queries, 0, 0);
|
|
211
|
+
}
|
|
212
|
+
const retryPromises = failedIndices.map((index) => {
|
|
213
|
+
const query = queries[index];
|
|
214
|
+
return queryClient.refetchQueries({ queryKey: query.queryKey, exact: true });
|
|
215
|
+
});
|
|
216
|
+
await Promise.allSettled(retryPromises);
|
|
217
|
+
retryCountRef.current++;
|
|
218
|
+
return operations.getOperationReport();
|
|
219
|
+
},
|
|
220
|
+
getErrorAggregate: () => {
|
|
221
|
+
return createErrorAggregate(results, queries);
|
|
222
|
+
},
|
|
223
|
+
getOperationReport: () => {
|
|
224
|
+
return createOperationReport(results, queries, 0, retryCountRef.current);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
return { data: results, stats, operations, config };
|
|
228
|
+
}
|
|
229
|
+
function createBatchQueryConfig(config = {}) {
|
|
230
|
+
return { enableStats: true, enableBatchOperations: true, autoRefreshInterval: void 0, onBatchSuccess: void 0, onBatchError: void 0, onBatchSettled: void 0, enablePartialSuccess: false, onPartialSuccess: void 0, retryConfig: void 0, ...config };
|
|
231
|
+
}
|
|
232
|
+
var batchQueryUtils = {
|
|
233
|
+
isAllLoading: (results) => results.every((result) => result.isLoading),
|
|
234
|
+
isAllSuccess: (results) => results.every((result) => result.isSuccess),
|
|
235
|
+
hasError: (results) => results.some((result) => result.isError),
|
|
236
|
+
hasStale: (results) => results.some((result) => result.isStale),
|
|
237
|
+
getAllErrors: (results) => results.filter((result) => result.isError).map((result) => result.error).filter((error) => error instanceof Error),
|
|
238
|
+
getAllData: (results) => results.filter((result) => result.isSuccess).map((result) => result.data),
|
|
239
|
+
isAllPending: (results) => results.every((result) => result.isPending),
|
|
240
|
+
isAnyFetching: (results) => results.some((result) => result.isFetching),
|
|
241
|
+
getSuccessData: (results) => results.filter((result) => result.isSuccess && result.data !== void 0).map((result) => result.data),
|
|
242
|
+
getFirstError: (results) => {
|
|
243
|
+
const errorResult = results.find((result) => result.isError);
|
|
244
|
+
return errorResult?.error instanceof Error ? errorResult.error : null;
|
|
245
|
+
},
|
|
246
|
+
createErrorAggregate: (results, queries) => createErrorAggregate(results, queries),
|
|
247
|
+
createOperationReport: (results, queries, startTime, retryCount = 0) => createOperationReport(results, queries, startTime, retryCount)
|
|
248
|
+
};
|
|
249
|
+
function useCombinedQueries(options) {
|
|
250
|
+
return reactQuery.useQueries(options);
|
|
251
|
+
}
|
|
252
|
+
function useDynamicBatchQueries(options) {
|
|
253
|
+
const { items, queryKeyPrefix, queryFn, enabled = true, staleTime, gcTime, config = {} } = options;
|
|
254
|
+
const queries = react.useMemo(() => {
|
|
255
|
+
if (!enabled || items.length === 0) return [];
|
|
256
|
+
return items.map((item) => ({ queryKey: [...queryKeyPrefix, item], queryFn: () => queryFn(item), staleTime, gcTime, enabled }));
|
|
257
|
+
}, [items, queryKeyPrefix, queryFn, enabled, staleTime, gcTime]);
|
|
258
|
+
return useEnhancedQueries(queries, config);
|
|
259
|
+
}
|
|
260
|
+
function useDependentBatchQueries(options) {
|
|
261
|
+
const { primaryQuery, dependentQueries, config = {} } = options;
|
|
262
|
+
const primaryResult = reactQuery.useQuery(primaryQuery);
|
|
263
|
+
const queries = react.useMemo(() => {
|
|
264
|
+
try {
|
|
265
|
+
if (!primaryResult.data) {
|
|
266
|
+
return [];
|
|
267
|
+
}
|
|
268
|
+
return dependentQueries(primaryResult.data);
|
|
269
|
+
} catch {
|
|
270
|
+
return [];
|
|
271
|
+
}
|
|
272
|
+
}, [primaryResult.data, dependentQueries]);
|
|
273
|
+
const enhancedQueriesResult = useEnhancedQueries(queries, config);
|
|
274
|
+
return { primaryResult, results: enhancedQueriesResult.data, stats: enhancedQueriesResult.stats, operations: enhancedQueriesResult.operations };
|
|
275
|
+
}
|
|
276
|
+
function useAutoRefreshBatchQueries(options) {
|
|
277
|
+
const { queries, refreshInterval = 3e4, enabled = true, config = {} } = options;
|
|
278
|
+
const result = useEnhancedQueries(queries, config);
|
|
279
|
+
react.useEffect(() => {
|
|
280
|
+
if (!enabled || !refreshInterval || !result.operations) return;
|
|
281
|
+
const intervalId = setInterval(() => {
|
|
282
|
+
result.operations?.refetchAll();
|
|
283
|
+
}, refreshInterval);
|
|
284
|
+
return () => clearInterval(intervalId);
|
|
285
|
+
}, [enabled, refreshInterval, result.operations]);
|
|
286
|
+
return result;
|
|
287
|
+
}
|
|
288
|
+
function useDashboardQueries(queriesMap) {
|
|
289
|
+
const queries = react.useMemo(() => {
|
|
290
|
+
return Object.values(queriesMap);
|
|
291
|
+
}, [queriesMap]);
|
|
292
|
+
const { results, combinedData, stats } = reactQuery.useQueries({ queries, combine: (results2) => {
|
|
293
|
+
const keys = Object.keys(queriesMap);
|
|
294
|
+
const data = {};
|
|
295
|
+
keys.forEach((key, index) => {
|
|
296
|
+
data[key] = results2[index].data;
|
|
297
|
+
});
|
|
298
|
+
return { results: results2, combinedData: data, stats: calculateBatchStats(results2) };
|
|
299
|
+
} });
|
|
300
|
+
const isLoading = results.some((r) => r.isLoading);
|
|
301
|
+
const isError = results.some((r) => r.isError);
|
|
302
|
+
const isSuccess = results.every((r) => r.isSuccess);
|
|
303
|
+
return { data: combinedData, results, stats, isLoading, isError, isSuccess };
|
|
304
|
+
}
|
|
305
|
+
function usePaginatedBatchQueries(options) {
|
|
306
|
+
const { pageNumbers, queryKeyPrefix, queryFn, staleTime, config } = options;
|
|
307
|
+
return useDynamicBatchQueries({ items: pageNumbers, queryKeyPrefix, queryFn, staleTime, config });
|
|
308
|
+
}
|
|
309
|
+
function useConditionalBatchQueries(queries) {
|
|
310
|
+
const enabledQueries = react.useMemo(() => {
|
|
311
|
+
return queries.filter((q) => q.enabled !== false);
|
|
312
|
+
}, [queries]);
|
|
313
|
+
return useEnhancedQueries(enabledQueries);
|
|
314
|
+
}
|
|
315
|
+
function useRetryBatchQueries(options) {
|
|
316
|
+
const { queries, retry, retryDelay, config } = options;
|
|
317
|
+
const queriesWithRetry = react.useMemo(() => {
|
|
318
|
+
return queries.map((query) => ({ ...query, retry: query.retry ?? retry, retryDelay: query.retryDelay ?? retryDelay }));
|
|
319
|
+
}, [queries, retry, retryDelay]);
|
|
320
|
+
return useEnhancedQueries(queriesWithRetry, config);
|
|
321
|
+
}
|
|
322
|
+
function useBatchQueryPerformance(results) {
|
|
323
|
+
return react.useMemo(() => {
|
|
324
|
+
const stats = calculateBatchStats(results);
|
|
325
|
+
const fetchTimes = results.filter((r) => r.dataUpdatedAt > 0).map((r) => r.dataUpdatedAt);
|
|
326
|
+
const avgFetchTime = fetchTimes.length > 0 ? fetchTimes.reduce((a, b) => a + b, 0) / fetchTimes.length : 0;
|
|
327
|
+
return { ...stats, avgFetchTime, totalQueries: results.length, activeQueries: results.filter((r) => r.isFetching).length, cachedQueries: results.filter((r) => !r.isStale).length };
|
|
328
|
+
}, [results]);
|
|
329
|
+
}
|
|
330
|
+
function useFocusState() {
|
|
331
|
+
const [focused, setFocused] = react.useState(chunkNF5QYPYC_cjs.focusManager.isFocused());
|
|
332
|
+
react.useEffect(() => {
|
|
333
|
+
const unsubscribe = chunkNF5QYPYC_cjs.focusManager.subscribe((isFocused) => {
|
|
334
|
+
setFocused(isFocused);
|
|
335
|
+
});
|
|
336
|
+
return unsubscribe;
|
|
337
|
+
}, []);
|
|
338
|
+
return focused;
|
|
339
|
+
}
|
|
340
|
+
function useFocusRefetch(options = {}) {
|
|
341
|
+
const queryClient = reactQuery.useQueryClient();
|
|
342
|
+
const { enabled = true, minInterval = 5e3, queryKeys = [] } = options;
|
|
343
|
+
const smartManager = chunkNF5QYPYC_cjs.getSmartFocusManager();
|
|
344
|
+
const queryKeysJson = JSON.stringify(queryKeys);
|
|
345
|
+
const stableQueryKeys = react.useMemo(() => queryKeys, [queryKeysJson]);
|
|
346
|
+
react.useEffect(() => {
|
|
347
|
+
if (!enabled || stableQueryKeys.length === 0) return;
|
|
348
|
+
const unsubscribe = chunkNF5QYPYC_cjs.focusManager.subscribe((isFocused) => {
|
|
349
|
+
if (!isFocused) return;
|
|
350
|
+
stableQueryKeys.forEach((queryKey) => {
|
|
351
|
+
if (smartManager.shouldRefetch(queryKey, minInterval)) {
|
|
352
|
+
queryClient.invalidateQueries({ queryKey });
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
return unsubscribe;
|
|
357
|
+
}, [queryClient, enabled, minInterval, stableQueryKeys, smartManager]);
|
|
358
|
+
}
|
|
359
|
+
function usePauseFocus(options = {}) {
|
|
360
|
+
const { autoPause = false, pauseWhen = false } = options;
|
|
361
|
+
react.useEffect(() => {
|
|
362
|
+
if (autoPause || pauseWhen) {
|
|
363
|
+
chunkNF5QYPYC_cjs.pauseFocusManager();
|
|
364
|
+
return () => {
|
|
365
|
+
chunkNF5QYPYC_cjs.resumeFocusManager();
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
}, [autoPause, pauseWhen]);
|
|
369
|
+
const pause = react.useCallback(() => {
|
|
370
|
+
chunkNF5QYPYC_cjs.pauseFocusManager();
|
|
371
|
+
}, []);
|
|
372
|
+
const resume = react.useCallback(() => {
|
|
373
|
+
chunkNF5QYPYC_cjs.resumeFocusManager();
|
|
374
|
+
}, []);
|
|
375
|
+
return { pause, resume };
|
|
376
|
+
}
|
|
377
|
+
function useSmartFocusManager() {
|
|
378
|
+
const manager = chunkNF5QYPYC_cjs.getSmartFocusManager();
|
|
379
|
+
const pause = react.useCallback(() => {
|
|
380
|
+
manager.pause();
|
|
381
|
+
}, [manager]);
|
|
382
|
+
const resume = react.useCallback(() => {
|
|
383
|
+
manager.resume();
|
|
384
|
+
}, [manager]);
|
|
385
|
+
const getStats = react.useCallback(() => {
|
|
386
|
+
return manager.getStats();
|
|
387
|
+
}, [manager]);
|
|
388
|
+
return { pause, resume, getStats, stats: manager.getStats() };
|
|
389
|
+
}
|
|
390
|
+
function useConditionalFocusRefetch(queryKey, condition, options = {}) {
|
|
391
|
+
const queryClient = reactQuery.useQueryClient();
|
|
392
|
+
const { minInterval = 5e3, enabled = true } = options;
|
|
393
|
+
const smartManager = chunkNF5QYPYC_cjs.getSmartFocusManager();
|
|
394
|
+
const queryKeyJson = JSON.stringify(queryKey);
|
|
395
|
+
const stableQueryKey = react.useMemo(() => queryKey, [queryKeyJson]);
|
|
396
|
+
const conditionRef = react.useRef(condition);
|
|
397
|
+
react.useEffect(() => {
|
|
398
|
+
conditionRef.current = condition;
|
|
399
|
+
}, [condition]);
|
|
400
|
+
react.useEffect(() => {
|
|
401
|
+
if (!enabled) return;
|
|
402
|
+
const unsubscribe = chunkNF5QYPYC_cjs.focusManager.subscribe((isFocused) => {
|
|
403
|
+
if (!isFocused || !conditionRef.current()) return;
|
|
404
|
+
if (smartManager.shouldRefetch(stableQueryKey, minInterval)) {
|
|
405
|
+
queryClient.invalidateQueries({ queryKey: stableQueryKey });
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
return unsubscribe;
|
|
409
|
+
}, [queryClient, stableQueryKey, minInterval, enabled, smartManager]);
|
|
410
|
+
}
|
|
411
|
+
function useFocusCallback(callback, options = {}) {
|
|
412
|
+
const { minInterval = 0, enabled = true, queryKey } = options;
|
|
413
|
+
const lastCallTime = react.useRef(0);
|
|
414
|
+
const smartManager = chunkNF5QYPYC_cjs.getSmartFocusManager();
|
|
415
|
+
const queryKeyJson = JSON.stringify(queryKey);
|
|
416
|
+
const stableQueryKey = react.useMemo(() => queryKey, [queryKeyJson]);
|
|
417
|
+
const callbackRef = react.useRef(callback);
|
|
418
|
+
react.useEffect(() => {
|
|
419
|
+
callbackRef.current = callback;
|
|
420
|
+
}, [callback]);
|
|
421
|
+
react.useEffect(() => {
|
|
422
|
+
if (!enabled) return;
|
|
423
|
+
const unsubscribe = chunkNF5QYPYC_cjs.focusManager.subscribe((isFocused) => {
|
|
424
|
+
if (!isFocused) return;
|
|
425
|
+
if (stableQueryKey !== void 0) {
|
|
426
|
+
if (smartManager.shouldRefetch(stableQueryKey, minInterval)) {
|
|
427
|
+
callbackRef.current();
|
|
428
|
+
}
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const now = Date.now();
|
|
432
|
+
if (minInterval > 0 && now - lastCallTime.current < minInterval) return;
|
|
433
|
+
lastCallTime.current = now;
|
|
434
|
+
callbackRef.current();
|
|
435
|
+
});
|
|
436
|
+
return unsubscribe;
|
|
437
|
+
}, [minInterval, enabled, stableQueryKey, smartManager]);
|
|
438
|
+
}
|
|
439
|
+
function usePageVisibility() {
|
|
440
|
+
const [isVisible, setIsVisible] = react.useState(typeof document !== "undefined" ? !document.hidden : true);
|
|
441
|
+
react.useEffect(() => {
|
|
442
|
+
const unsubscribe = chunkNF5QYPYC_cjs.focusManager.subscribe((isFocused) => {
|
|
443
|
+
setIsVisible(isFocused);
|
|
444
|
+
});
|
|
445
|
+
return unsubscribe;
|
|
446
|
+
}, []);
|
|
447
|
+
return isVisible;
|
|
448
|
+
}
|
|
449
|
+
function useEnhancedInfiniteQuery(options) {
|
|
450
|
+
return reactQuery.useInfiniteQuery(options);
|
|
451
|
+
}
|
|
452
|
+
function createInfiniteQueryOptions(config) {
|
|
453
|
+
return reactQuery.infiniteQueryOptions({ ...config, maxPages: config.maxPages });
|
|
454
|
+
}
|
|
455
|
+
function createCursorPaginationOptions(config) {
|
|
456
|
+
return createInfiniteQueryOptions({
|
|
457
|
+
queryKey: config.queryKey,
|
|
458
|
+
queryFn: ({ pageParam }) => config.queryFn(pageParam),
|
|
459
|
+
initialPageParam: config.initialCursor ?? null,
|
|
460
|
+
getNextPageParam: (lastPage) => lastPage.cursor ?? null,
|
|
461
|
+
getPreviousPageParam: () => null,
|
|
462
|
+
staleTime: config.staleTime,
|
|
463
|
+
gcTime: config.gcTime,
|
|
464
|
+
maxPages: config.maxPages
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
function createOffsetPaginationOptions(config) {
|
|
468
|
+
const limit = config.limit ?? 20;
|
|
469
|
+
return createInfiniteQueryOptions({
|
|
470
|
+
queryKey: [...config.queryKey, limit],
|
|
471
|
+
queryFn: ({ pageParam }) => config.queryFn(pageParam, limit),
|
|
472
|
+
initialPageParam: 0,
|
|
473
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
474
|
+
if (lastPage.hasMore === false) return void 0;
|
|
475
|
+
const currentOffset = allPages.length * limit;
|
|
476
|
+
if (currentOffset >= lastPage.total) return void 0;
|
|
477
|
+
return currentOffset;
|
|
478
|
+
},
|
|
479
|
+
getPreviousPageParam: (_firstPage, allPages) => {
|
|
480
|
+
if (allPages.length <= 1) return void 0;
|
|
481
|
+
return (allPages.length - 2) * limit;
|
|
482
|
+
},
|
|
483
|
+
staleTime: config.staleTime,
|
|
484
|
+
gcTime: config.gcTime,
|
|
485
|
+
maxPages: config.maxPages
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
function createPageNumberPaginationOptions(config) {
|
|
489
|
+
return createInfiniteQueryOptions({
|
|
490
|
+
queryKey: config.queryKey,
|
|
491
|
+
queryFn: ({ pageParam }) => config.queryFn(pageParam),
|
|
492
|
+
initialPageParam: 1,
|
|
493
|
+
getNextPageParam: (lastPage, allPages) => {
|
|
494
|
+
const nextPage = allPages.length + 1;
|
|
495
|
+
if (nextPage > lastPage.totalPages) return void 0;
|
|
496
|
+
return nextPage;
|
|
497
|
+
},
|
|
498
|
+
getPreviousPageParam: (_firstPage, allPages) => {
|
|
499
|
+
if (allPages.length <= 1) return void 0;
|
|
500
|
+
return allPages.length - 1;
|
|
501
|
+
},
|
|
502
|
+
staleTime: config.staleTime,
|
|
503
|
+
gcTime: config.gcTime,
|
|
504
|
+
maxPages: config.maxPages
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
function useMutation(options) {
|
|
508
|
+
const queryClient = reactQuery.useQueryClient();
|
|
509
|
+
const { optimistic, onMutate, onError, onSuccess, onSettled, ...restOptions } = options;
|
|
510
|
+
const mutationConfig = {
|
|
511
|
+
...restOptions,
|
|
512
|
+
retry: restOptions.retry ?? chunk6MAYHLTE_cjs.DEFAULT_MUTATION_CONFIG.retry,
|
|
513
|
+
retryDelay: restOptions.retryDelay ?? chunk6MAYHLTE_cjs.DEFAULT_MUTATION_CONFIG.retryDelay,
|
|
514
|
+
gcTime: restOptions.gcTime ?? chunk6MAYHLTE_cjs.DEFAULT_MUTATION_CONFIG.gcTime
|
|
515
|
+
};
|
|
516
|
+
if (!optimistic) {
|
|
517
|
+
if (onMutate) {
|
|
518
|
+
mutationConfig.onMutate = onMutate;
|
|
519
|
+
}
|
|
520
|
+
if (onError) {
|
|
521
|
+
mutationConfig.onError = onError;
|
|
522
|
+
}
|
|
523
|
+
if (onSuccess) {
|
|
524
|
+
mutationConfig.onSuccess = onSuccess;
|
|
525
|
+
}
|
|
526
|
+
if (onSettled) {
|
|
527
|
+
mutationConfig.onSettled = onSettled;
|
|
528
|
+
}
|
|
529
|
+
} else {
|
|
530
|
+
mutationConfig.onMutate = async (variables) => {
|
|
531
|
+
if (optimistic.enabled === false) {
|
|
532
|
+
const mutateCallback = onMutate;
|
|
533
|
+
const userContext = onMutate ? await mutateCallback(variables) : void 0;
|
|
534
|
+
return { userContext };
|
|
535
|
+
}
|
|
536
|
+
try {
|
|
537
|
+
const familyKey = optimistic.familyKey ?? chunkAPXNNHBD_cjs.deriveFamilyKey(optimistic.queryKey);
|
|
538
|
+
await queryClient.cancelQueries({ queryKey: familyKey });
|
|
539
|
+
const previousData = queryClient.getQueriesData({ queryKey: familyKey });
|
|
540
|
+
let mappedVariables = variables;
|
|
541
|
+
if (optimistic.fieldMapping && typeof variables === "object" && variables !== null) {
|
|
542
|
+
mappedVariables = { ...variables };
|
|
543
|
+
const sourceObj = variables;
|
|
544
|
+
const targetObj = mappedVariables;
|
|
545
|
+
Object.entries(optimistic.fieldMapping).forEach(([sourceField, targetField]) => {
|
|
546
|
+
if (sourceField in sourceObj) {
|
|
547
|
+
targetObj[targetField] = sourceObj[sourceField];
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
queryClient.setQueriesData(
|
|
552
|
+
{ queryKey: familyKey },
|
|
553
|
+
(oldData) => {
|
|
554
|
+
if (!oldData) return oldData;
|
|
555
|
+
return optimistic.updater(oldData, mappedVariables);
|
|
556
|
+
}
|
|
557
|
+
);
|
|
558
|
+
const mutateCallback = onMutate;
|
|
559
|
+
const userContext = onMutate ? await mutateCallback(variables) : void 0;
|
|
560
|
+
return { previousData, userContext };
|
|
561
|
+
} catch (error) {
|
|
562
|
+
return { userContext: void 0 };
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
mutationConfig.onError = (error, variables, context) => {
|
|
566
|
+
if (context?.previousData) {
|
|
567
|
+
context.previousData.forEach(([queryKey, data]) => {
|
|
568
|
+
queryClient.setQueryData(queryKey, data);
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
if (optimistic.rollback && context?.previousData) {
|
|
572
|
+
try {
|
|
573
|
+
const firstSnapshot = context.previousData[0]?.[1];
|
|
574
|
+
if (firstSnapshot) {
|
|
575
|
+
optimistic.rollback(firstSnapshot, error);
|
|
576
|
+
}
|
|
577
|
+
} catch {
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
if (onError) {
|
|
581
|
+
const errorCallback = onError;
|
|
582
|
+
errorCallback(error, variables, context?.userContext);
|
|
583
|
+
}
|
|
584
|
+
};
|
|
585
|
+
mutationConfig.onSuccess = (data, variables, context) => {
|
|
586
|
+
const scope = optimistic.invalidateScope ?? (chunkAPXNNHBD_cjs.isListFamilyKey(optimistic.queryKey) ? "family" : "exact");
|
|
587
|
+
const invalidations = [];
|
|
588
|
+
if (scope !== "none") {
|
|
589
|
+
if (scope === "family") {
|
|
590
|
+
const familyKey = optimistic.familyKey ?? chunkAPXNNHBD_cjs.deriveFamilyKey(optimistic.queryKey);
|
|
591
|
+
invalidations.push({ queryKey: familyKey });
|
|
592
|
+
} else {
|
|
593
|
+
invalidations.push({ queryKey: optimistic.queryKey });
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
if (Array.isArray(optimistic.relatedKeys) && optimistic.relatedKeys.length > 0) {
|
|
597
|
+
optimistic.relatedKeys.forEach((k) => invalidations.push({ queryKey: k }));
|
|
598
|
+
}
|
|
599
|
+
if (Array.isArray(optimistic.invalidates) && optimistic.invalidates.length > 0) {
|
|
600
|
+
optimistic.invalidates.forEach((k) => invalidations.push({ queryKey: k }));
|
|
601
|
+
}
|
|
602
|
+
if (invalidations.length > 0) {
|
|
603
|
+
const seen = /* @__PURE__ */ new Set();
|
|
604
|
+
const tasks = invalidations.map((cfg) => ({ ...cfg, exact: false })).filter((cfg) => {
|
|
605
|
+
const key = JSON.stringify(cfg.queryKey);
|
|
606
|
+
if (seen.has(key)) return false;
|
|
607
|
+
seen.add(key);
|
|
608
|
+
return true;
|
|
609
|
+
});
|
|
610
|
+
tasks.forEach((task) => queryClient.invalidateQueries(task));
|
|
611
|
+
}
|
|
612
|
+
if (onSuccess) {
|
|
613
|
+
const successCallback = onSuccess;
|
|
614
|
+
successCallback(data, variables, context?.userContext);
|
|
615
|
+
}
|
|
616
|
+
};
|
|
617
|
+
mutationConfig.onSettled = (data, error, variables, context) => {
|
|
618
|
+
if (onSettled) {
|
|
619
|
+
const settledCallback = onSettled;
|
|
620
|
+
settledCallback(data, error, variables, context?.userContext);
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
return reactQuery.useMutation(mutationConfig);
|
|
625
|
+
}
|
|
626
|
+
function setupMutationDefaults(queryClient, config) {
|
|
627
|
+
Object.entries(config).forEach(([key, options]) => {
|
|
628
|
+
queryClient.setMutationDefaults([key], options);
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
function useListMutation(mutationFn, queryKey, options) {
|
|
632
|
+
const queryClient = reactQuery.useQueryClient();
|
|
633
|
+
return reactQuery.useMutation({
|
|
634
|
+
mutationFn,
|
|
635
|
+
onSettled: () => {
|
|
636
|
+
const familyKey = chunkAPXNNHBD_cjs.deriveFamilyKey(queryKey);
|
|
637
|
+
queryClient.invalidateQueries({ queryKey: familyKey, exact: false });
|
|
638
|
+
},
|
|
639
|
+
...options,
|
|
640
|
+
mutationKey: options?.mutationKey
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
function isMissingOrStale(queryClient, queryKey, staleTime) {
|
|
644
|
+
const state = queryClient.getQueryState(queryKey);
|
|
645
|
+
if (!state || state.data === void 0) return true;
|
|
646
|
+
const updatedAt = state.dataUpdatedAt ?? 0;
|
|
647
|
+
return Date.now() - updatedAt > staleTime;
|
|
648
|
+
}
|
|
649
|
+
function useHoverPrefetch(queryKey, queryFn, options = {}) {
|
|
650
|
+
const queryClient = reactQuery.useQueryClient();
|
|
651
|
+
const timeoutRef = react.useRef(void 0);
|
|
652
|
+
const { hoverDelay = 200, enabled = true, staleTime = chunk6MAYHLTE_cjs.DEFAULT_STALE_TIME, minInterval = 1e3 } = options;
|
|
653
|
+
const queryFnRef = react.useRef(queryFn);
|
|
654
|
+
react.useEffect(() => {
|
|
655
|
+
queryFnRef.current = queryFn;
|
|
656
|
+
}, [queryFn]);
|
|
657
|
+
const lastPrefetchRef = react.useRef(/* @__PURE__ */ new Map());
|
|
658
|
+
const prefetch = react.useCallback(() => {
|
|
659
|
+
if (!enabled) return;
|
|
660
|
+
const keyStr = JSON.stringify(queryKey);
|
|
661
|
+
const last = lastPrefetchRef.current.get(keyStr) ?? 0;
|
|
662
|
+
if (Date.now() - last < minInterval) return;
|
|
663
|
+
if (!isMissingOrStale(queryClient, queryKey, staleTime)) return;
|
|
664
|
+
queryClient.prefetchQuery({ queryKey, queryFn: queryFnRef.current, staleTime });
|
|
665
|
+
lastPrefetchRef.current.set(keyStr, Date.now());
|
|
666
|
+
}, [queryClient, queryKey, enabled, staleTime]);
|
|
667
|
+
const handleMouseEnter = react.useCallback(() => {
|
|
668
|
+
if (!enabled) return;
|
|
669
|
+
if (timeoutRef.current) {
|
|
670
|
+
clearTimeout(timeoutRef.current);
|
|
671
|
+
}
|
|
672
|
+
timeoutRef.current = setTimeout(prefetch, hoverDelay);
|
|
673
|
+
}, [prefetch, hoverDelay, enabled]);
|
|
674
|
+
const handleMouseLeave = react.useCallback(() => {
|
|
675
|
+
if (timeoutRef.current) {
|
|
676
|
+
clearTimeout(timeoutRef.current);
|
|
677
|
+
}
|
|
678
|
+
}, []);
|
|
679
|
+
react.useEffect(() => () => {
|
|
680
|
+
if (timeoutRef.current) {
|
|
681
|
+
clearTimeout(timeoutRef.current);
|
|
682
|
+
}
|
|
683
|
+
}, []);
|
|
684
|
+
return { onMouseEnter: handleMouseEnter, onMouseLeave: handleMouseLeave, onFocus: prefetch };
|
|
685
|
+
}
|
|
686
|
+
function useRoutePrefetch() {
|
|
687
|
+
const queryClient = reactQuery.useQueryClient();
|
|
688
|
+
const lastPrefetchRef = react.useRef(/* @__PURE__ */ new Map());
|
|
689
|
+
return react.useCallback((queryKey, queryFn, options) => {
|
|
690
|
+
const { enabled = true, staleTime = chunk6MAYHLTE_cjs.DEFAULT_STALE_TIME, minInterval = 1e3 } = options || {};
|
|
691
|
+
if (!enabled) return;
|
|
692
|
+
const keyStr = JSON.stringify(queryKey);
|
|
693
|
+
const last = lastPrefetchRef.current.get(keyStr) ?? 0;
|
|
694
|
+
if (Date.now() - last < minInterval) return;
|
|
695
|
+
if (!isMissingOrStale(queryClient, queryKey, staleTime)) return;
|
|
696
|
+
queryClient.prefetchQuery({ queryKey, queryFn, staleTime });
|
|
697
|
+
lastPrefetchRef.current.set(keyStr, Date.now());
|
|
698
|
+
}, [queryClient]);
|
|
699
|
+
}
|
|
700
|
+
function useBatchPrefetch() {
|
|
701
|
+
const queryClient = reactQuery.useQueryClient();
|
|
702
|
+
return react.useCallback((queries) => {
|
|
703
|
+
queries.forEach(({ queryKey, queryFn, staleTime = chunk6MAYHLTE_cjs.DEFAULT_STALE_TIME }) => {
|
|
704
|
+
if (isMissingOrStale(queryClient, queryKey, staleTime)) {
|
|
705
|
+
queryClient.prefetchQuery({ queryKey, queryFn, staleTime });
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
}, [queryClient]);
|
|
709
|
+
}
|
|
710
|
+
function useSmartPrefetch() {
|
|
711
|
+
const queryClient = reactQuery.useQueryClient();
|
|
712
|
+
const prefetchedRef = react.useRef(/* @__PURE__ */ new Set());
|
|
713
|
+
const lastPrefetchRef = react.useRef(/* @__PURE__ */ new Map());
|
|
714
|
+
const shouldPrefetchQuery = react.useCallback((queryKey) => {
|
|
715
|
+
const key = JSON.stringify(queryKey);
|
|
716
|
+
if (prefetchedRef.current.has(key)) return false;
|
|
717
|
+
if (chunkAPXNNHBD_cjs.isSlowNetwork()) return false;
|
|
718
|
+
return true;
|
|
719
|
+
}, []);
|
|
720
|
+
const prefetch = react.useCallback((queryKey, queryFn, options) => {
|
|
721
|
+
if (!shouldPrefetchQuery(queryKey)) return;
|
|
722
|
+
const { staleTime = chunk6MAYHLTE_cjs.DEFAULT_STALE_TIME, minInterval = 1e3 } = options || {};
|
|
723
|
+
const keyStr = JSON.stringify(queryKey);
|
|
724
|
+
const last = lastPrefetchRef.current.get(keyStr) ?? 0;
|
|
725
|
+
if (Date.now() - last < minInterval) return;
|
|
726
|
+
if (!isMissingOrStale(queryClient, queryKey, staleTime)) return;
|
|
727
|
+
prefetchedRef.current.add(keyStr);
|
|
728
|
+
queryClient.prefetchQuery({ queryKey, queryFn, staleTime });
|
|
729
|
+
lastPrefetchRef.current.set(keyStr, Date.now());
|
|
730
|
+
}, [queryClient, shouldPrefetchQuery]);
|
|
731
|
+
const clearPrefetchHistory = react.useCallback(() => {
|
|
732
|
+
prefetchedRef.current.clear();
|
|
733
|
+
}, []);
|
|
734
|
+
return { prefetch, shouldPrefetch: !chunkAPXNNHBD_cjs.isSlowNetwork(), clearPrefetchHistory };
|
|
735
|
+
}
|
|
736
|
+
function useStableQueryKey(queryKey) {
|
|
737
|
+
const keyRef = react.useRef(queryKey);
|
|
738
|
+
const keyStr = JSON.stringify(queryKey);
|
|
739
|
+
const prevKeyStr = react.useRef(keyStr);
|
|
740
|
+
if (prevKeyStr.current !== keyStr) {
|
|
741
|
+
keyRef.current = queryKey;
|
|
742
|
+
prevKeyStr.current = keyStr;
|
|
743
|
+
}
|
|
744
|
+
return keyRef.current;
|
|
745
|
+
}
|
|
746
|
+
function useConditionalPrefetch(queryKey, queryFn, condition, options) {
|
|
747
|
+
const queryClient = reactQuery.useQueryClient();
|
|
748
|
+
const { staleTime = chunk6MAYHLTE_cjs.DEFAULT_STALE_TIME, delay = 0 } = options || {};
|
|
749
|
+
const queryFnRef = react.useRef(queryFn);
|
|
750
|
+
react.useEffect(() => {
|
|
751
|
+
queryFnRef.current = queryFn;
|
|
752
|
+
}, [queryFn]);
|
|
753
|
+
const stableQueryKey = useStableQueryKey(queryKey);
|
|
754
|
+
react.useEffect(() => {
|
|
755
|
+
if (!condition) return;
|
|
756
|
+
const currentQueryKey = stableQueryKey;
|
|
757
|
+
const timeoutId = setTimeout(() => {
|
|
758
|
+
if (isMissingOrStale(queryClient, currentQueryKey, staleTime)) {
|
|
759
|
+
queryClient.prefetchQuery({ queryKey: currentQueryKey, queryFn: queryFnRef.current, staleTime });
|
|
760
|
+
}
|
|
761
|
+
}, delay);
|
|
762
|
+
return () => clearTimeout(timeoutId);
|
|
763
|
+
}, [condition, queryClient, staleTime, delay, stableQueryKey]);
|
|
764
|
+
}
|
|
765
|
+
function useIdlePrefetch(queryKey, queryFn, options) {
|
|
766
|
+
const queryClient = reactQuery.useQueryClient();
|
|
767
|
+
const { staleTime = chunk6MAYHLTE_cjs.DEFAULT_STALE_TIME, enabled = true, timeout = 1e3 } = options || {};
|
|
768
|
+
const queryFnRef = react.useRef(queryFn);
|
|
769
|
+
react.useEffect(() => {
|
|
770
|
+
queryFnRef.current = queryFn;
|
|
771
|
+
}, [queryFn]);
|
|
772
|
+
const stableQueryKey = useStableQueryKey(queryKey);
|
|
773
|
+
react.useEffect(() => {
|
|
774
|
+
if (!enabled) return;
|
|
775
|
+
const currentQueryKey = stableQueryKey;
|
|
776
|
+
if (typeof window === "undefined" || !("requestIdleCallback" in window)) {
|
|
777
|
+
const timeoutId = setTimeout(() => {
|
|
778
|
+
if (isMissingOrStale(queryClient, currentQueryKey, staleTime)) {
|
|
779
|
+
queryClient.prefetchQuery({ queryKey: currentQueryKey, queryFn: queryFnRef.current, staleTime });
|
|
780
|
+
}
|
|
781
|
+
}, timeout);
|
|
782
|
+
return () => clearTimeout(timeoutId);
|
|
783
|
+
}
|
|
784
|
+
const idleCallbackId = window.requestIdleCallback(() => {
|
|
785
|
+
if (isMissingOrStale(queryClient, currentQueryKey, staleTime)) {
|
|
786
|
+
queryClient.prefetchQuery({ queryKey: currentQueryKey, queryFn: queryFnRef.current, staleTime });
|
|
787
|
+
}
|
|
788
|
+
}, { timeout });
|
|
789
|
+
return () => window.cancelIdleCallback(idleCallbackId);
|
|
790
|
+
}, [queryClient, staleTime, enabled, timeout, stableQueryKey]);
|
|
791
|
+
}
|
|
792
|
+
function usePeriodicPrefetch(queryKey, queryFn, options) {
|
|
793
|
+
const queryClient = reactQuery.useQueryClient();
|
|
794
|
+
const { staleTime = chunk6MAYHLTE_cjs.DEFAULT_STALE_TIME, enabled = true, interval = 6e4 } = options || {};
|
|
795
|
+
const queryFnRef = react.useRef(queryFn);
|
|
796
|
+
react.useEffect(() => {
|
|
797
|
+
queryFnRef.current = queryFn;
|
|
798
|
+
}, [queryFn]);
|
|
799
|
+
const stableQueryKey = useStableQueryKey(queryKey);
|
|
800
|
+
react.useEffect(() => {
|
|
801
|
+
if (!enabled) return;
|
|
802
|
+
const currentQueryKey = stableQueryKey;
|
|
803
|
+
const prefetchData = () => {
|
|
804
|
+
if (isMissingOrStale(queryClient, currentQueryKey, staleTime)) {
|
|
805
|
+
queryClient.prefetchQuery({ queryKey: currentQueryKey, queryFn: queryFnRef.current, staleTime });
|
|
806
|
+
}
|
|
807
|
+
};
|
|
808
|
+
prefetchData();
|
|
809
|
+
const intervalId = setInterval(prefetchData, interval);
|
|
810
|
+
return () => clearInterval(intervalId);
|
|
811
|
+
}, [queryClient, staleTime, enabled, interval, stableQueryKey]);
|
|
812
|
+
}
|
|
813
|
+
function usePredictivePrefetch() {
|
|
814
|
+
const queryClient = reactQuery.useQueryClient();
|
|
815
|
+
const interactionHistoryRef = react.useRef([]);
|
|
816
|
+
const maxHistorySize = 50;
|
|
817
|
+
const recordInteraction = react.useCallback((action, target) => {
|
|
818
|
+
interactionHistoryRef.current.push({ action, target, timestamp: Date.now() });
|
|
819
|
+
if (interactionHistoryRef.current.length > maxHistorySize) {
|
|
820
|
+
interactionHistoryRef.current.shift();
|
|
821
|
+
}
|
|
822
|
+
}, []);
|
|
823
|
+
const getPredictions = react.useCallback(() => {
|
|
824
|
+
const history = interactionHistoryRef.current;
|
|
825
|
+
if (history.length < 3) return [];
|
|
826
|
+
const targetCounts = /* @__PURE__ */ new Map();
|
|
827
|
+
history.forEach(({ target }) => {
|
|
828
|
+
targetCounts.set(target, (targetCounts.get(target) || 0) + 1);
|
|
829
|
+
});
|
|
830
|
+
return Array.from(targetCounts.entries()).sort((a, b) => b[1] - a[1]).slice(0, 5).map(([target]) => target);
|
|
831
|
+
}, []);
|
|
832
|
+
const prefetchPredicted = react.useCallback((getQueryConfig) => {
|
|
833
|
+
const predictions = getPredictions();
|
|
834
|
+
predictions.forEach((target) => {
|
|
835
|
+
const { queryKey, queryFn } = getQueryConfig(target);
|
|
836
|
+
queryClient.prefetchQuery({ queryKey, queryFn });
|
|
837
|
+
});
|
|
838
|
+
}, [queryClient, getPredictions]);
|
|
839
|
+
const clearHistory = react.useCallback(() => {
|
|
840
|
+
interactionHistoryRef.current = [];
|
|
841
|
+
}, []);
|
|
842
|
+
return { recordInteraction, getPredictions, prefetchPredicted, clearHistory };
|
|
843
|
+
}
|
|
844
|
+
function usePriorityPrefetch() {
|
|
845
|
+
const queryClient = reactQuery.useQueryClient();
|
|
846
|
+
const tasksRef = react.useRef([]);
|
|
847
|
+
const [taskCount, setTaskCount] = react.useState(0);
|
|
848
|
+
const addPrefetchTask = react.useCallback((queryKey, queryFn, priority = "medium") => {
|
|
849
|
+
tasksRef.current.push({ queryKey, queryFn, priority, timestamp: Date.now() });
|
|
850
|
+
setTaskCount(tasksRef.current.length);
|
|
851
|
+
}, []);
|
|
852
|
+
const processTasks = react.useCallback(async () => {
|
|
853
|
+
const priorityOrder = { high: 0, medium: 1, low: 2 };
|
|
854
|
+
const sortedTasks = [...tasksRef.current].sort((a, b) => {
|
|
855
|
+
const priorityDiff = priorityOrder[a.priority] - priorityOrder[b.priority];
|
|
856
|
+
if (priorityDiff !== 0) return priorityDiff;
|
|
857
|
+
return a.timestamp - b.timestamp;
|
|
858
|
+
});
|
|
859
|
+
for (const task of sortedTasks) {
|
|
860
|
+
await queryClient.prefetchQuery({ queryKey: task.queryKey, queryFn: task.queryFn });
|
|
861
|
+
}
|
|
862
|
+
tasksRef.current = [];
|
|
863
|
+
setTaskCount(0);
|
|
864
|
+
}, [queryClient]);
|
|
865
|
+
const clearTasks = react.useCallback(() => {
|
|
866
|
+
tasksRef.current = [];
|
|
867
|
+
setTaskCount(0);
|
|
868
|
+
}, []);
|
|
869
|
+
return { addPrefetchTask, processTasks, clearTasks, taskCount };
|
|
870
|
+
}
|
|
871
|
+
function useEnhancedQuery(options) {
|
|
872
|
+
const {
|
|
873
|
+
logErrors = chunkOFLCHKKE_cjs.isDev,
|
|
874
|
+
onSlowQuery,
|
|
875
|
+
slowQueryThreshold = 3e3,
|
|
876
|
+
trackPerformance = false,
|
|
877
|
+
...queryOptions
|
|
878
|
+
} = options;
|
|
879
|
+
const startTimeRef = react.useRef(0);
|
|
880
|
+
const refetchCountRef = react.useRef(0);
|
|
881
|
+
const lastDurationRef = react.useRef(null);
|
|
882
|
+
const wasFetchingRef = react.useRef(false);
|
|
883
|
+
const result = reactQuery.useQuery(queryOptions);
|
|
884
|
+
react.useEffect(() => {
|
|
885
|
+
if (trackPerformance || onSlowQuery) {
|
|
886
|
+
if (result.isFetching && !wasFetchingRef.current) {
|
|
887
|
+
startTimeRef.current = Date.now();
|
|
888
|
+
} else if (!result.isFetching && wasFetchingRef.current && startTimeRef.current > 0) {
|
|
889
|
+
const duration = Date.now() - startTimeRef.current;
|
|
890
|
+
lastDurationRef.current = duration;
|
|
891
|
+
if (onSlowQuery && duration > slowQueryThreshold) {
|
|
892
|
+
onSlowQuery(duration, queryOptions.queryKey);
|
|
893
|
+
}
|
|
894
|
+
startTimeRef.current = 0;
|
|
895
|
+
}
|
|
896
|
+
wasFetchingRef.current = result.isFetching;
|
|
897
|
+
}
|
|
898
|
+
}, [result.isFetching, onSlowQuery, slowQueryThreshold, trackPerformance, queryOptions.queryKey]);
|
|
899
|
+
react.useEffect(() => {
|
|
900
|
+
if (logErrors && result.isError && result.error) {
|
|
901
|
+
console.error(`[useEnhancedQuery Error] ${JSON.stringify(queryOptions.queryKey)}:`, result.error);
|
|
902
|
+
}
|
|
903
|
+
}, [result.isError, result.error, logErrors, queryOptions.queryKey]);
|
|
904
|
+
react.useEffect(() => {
|
|
905
|
+
if (result.isRefetching) {
|
|
906
|
+
refetchCountRef.current++;
|
|
907
|
+
}
|
|
908
|
+
}, [result.isRefetching]);
|
|
909
|
+
return {
|
|
910
|
+
...result,
|
|
911
|
+
refetchCount: refetchCountRef.current,
|
|
912
|
+
lastQueryDuration: lastDurationRef.current
|
|
913
|
+
};
|
|
914
|
+
}
|
|
915
|
+
function createSuspenseQuery(getQueryKey, queryFn, options) {
|
|
916
|
+
return (variables) => {
|
|
917
|
+
const queryKey = getQueryKey(variables);
|
|
918
|
+
return reactQuery.useSuspenseQuery({ queryKey, queryFn, ...options });
|
|
919
|
+
};
|
|
920
|
+
}
|
|
921
|
+
function createSuspenseInfiniteQuery(getQueryKey, queryFn, options) {
|
|
922
|
+
return (variables) => {
|
|
923
|
+
const queryKey = getQueryKey(variables);
|
|
924
|
+
return reactQuery.useSuspenseInfiniteQuery({ queryKey, queryFn, ...options });
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
function useEnhancedSuspenseQuery(options) {
|
|
928
|
+
return reactQuery.useSuspenseQuery(options);
|
|
929
|
+
}
|
|
930
|
+
function useEnhancedSuspenseInfiniteQuery(options) {
|
|
931
|
+
return reactQuery.useSuspenseInfiniteQuery(options);
|
|
932
|
+
}
|
|
933
|
+
function useDataGuardQueryConfig(queryKey, fetchFn, options) {
|
|
934
|
+
const queryClient = reactQuery.useQueryClient();
|
|
935
|
+
return {
|
|
936
|
+
queryKey,
|
|
937
|
+
queryFn: async () => {
|
|
938
|
+
const newData = await fetchFn();
|
|
939
|
+
const cached = queryClient.getQueryData(queryKey);
|
|
940
|
+
const { shouldReject, reason, strategy, guardDetails } = chunkAPXNNHBD_cjs.applyDataGuard(
|
|
941
|
+
newData,
|
|
942
|
+
cached,
|
|
943
|
+
queryKey,
|
|
944
|
+
options
|
|
945
|
+
);
|
|
946
|
+
options?.onDataGuardApplied?.({
|
|
947
|
+
strategy,
|
|
948
|
+
passed: !shouldReject,
|
|
949
|
+
details: guardDetails
|
|
950
|
+
});
|
|
951
|
+
if (shouldReject) {
|
|
952
|
+
if (chunkOFLCHKKE_cjs.isDev) {
|
|
953
|
+
console.warn("[Data Guard] \u62D2\u7EDD\u65E7\u6570\u636E", {
|
|
954
|
+
reason,
|
|
955
|
+
strategy,
|
|
956
|
+
queryKey,
|
|
957
|
+
guardDetails
|
|
958
|
+
});
|
|
959
|
+
}
|
|
960
|
+
options?.onStaleDataDetected?.({
|
|
961
|
+
reason,
|
|
962
|
+
strategy,
|
|
963
|
+
queryKey,
|
|
964
|
+
cached,
|
|
965
|
+
rejected: newData
|
|
966
|
+
});
|
|
967
|
+
return cached;
|
|
968
|
+
}
|
|
969
|
+
if (chunkOFLCHKKE_cjs.isDev) {
|
|
970
|
+
console.log("[Data Guard] \u63A5\u53D7\u65B0\u6570\u636E", {
|
|
971
|
+
strategy,
|
|
972
|
+
guardDetails
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
return chunkAPXNNHBD_cjs.addHashToData(newData);
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
function useDataGuardMutation(mutationFn, queryKey, options) {
|
|
980
|
+
const queryClient = reactQuery.useQueryClient();
|
|
981
|
+
const { onConflict, onSuccess, optimistic, ...restOptions } = options || {};
|
|
982
|
+
const enhancedMutationFn = async (data) => {
|
|
983
|
+
try {
|
|
984
|
+
const result = await mutationFn(data);
|
|
985
|
+
return result;
|
|
986
|
+
} catch (error) {
|
|
987
|
+
if (error && typeof error === "object" && (error.status === 409 || error.code === "CONFLICT" || error.name === "ConflictError")) {
|
|
988
|
+
onConflict?.(error);
|
|
989
|
+
throw new chunkYW5PNTRU_cjs.ConflictError(error);
|
|
990
|
+
}
|
|
991
|
+
throw error;
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
const enhancedOptimistic = optimistic ? {
|
|
995
|
+
...optimistic,
|
|
996
|
+
updater: (old, variables) => {
|
|
997
|
+
const updated = optimistic.updater(old, variables);
|
|
998
|
+
if (!updated) return updated;
|
|
999
|
+
if (variables.id === void 0 || variables.id === null) {
|
|
1000
|
+
return updated;
|
|
1001
|
+
}
|
|
1002
|
+
const withMark = chunkAPXNNHBD_cjs.markRecentlyUpdated(updated, variables.id);
|
|
1003
|
+
if (old && old.version !== void 0) {
|
|
1004
|
+
withMark.version = old.version + 1;
|
|
1005
|
+
}
|
|
1006
|
+
if (old && old.updatedAt !== void 0) {
|
|
1007
|
+
withMark.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1008
|
+
}
|
|
1009
|
+
if (old && old._hash !== void 0) {
|
|
1010
|
+
withMark._hash = chunkAPXNNHBD_cjs.hashObject(withMark.items);
|
|
1011
|
+
}
|
|
1012
|
+
return withMark;
|
|
1013
|
+
}
|
|
1014
|
+
} : void 0;
|
|
1015
|
+
const enhancedOnSuccess = (data, variables, onMutateResult, context) => {
|
|
1016
|
+
try {
|
|
1017
|
+
const familyKey = Array.isArray(queryKey) ? queryKey.slice(0, -1) : [queryKey];
|
|
1018
|
+
chunkAPXNNHBD_cjs.updateFamilyMetadata(queryClient, familyKey, data);
|
|
1019
|
+
const cleanupDelay = 5e3;
|
|
1020
|
+
setTimeout(() => {
|
|
1021
|
+
try {
|
|
1022
|
+
if (variables.id === void 0 || variables.id === null) {
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
const cache = queryClient.getQueryCache();
|
|
1026
|
+
const queries = cache.findAll({
|
|
1027
|
+
predicate: (q) => chunkAPXNNHBD_cjs.startsWithKeyPrefix(q.queryKey, familyKey)
|
|
1028
|
+
});
|
|
1029
|
+
queries.forEach((q) => {
|
|
1030
|
+
const old = queryClient.getQueryData(q.queryKey);
|
|
1031
|
+
if (old?._recentlyUpdatedIds) {
|
|
1032
|
+
const cleared = chunkAPXNNHBD_cjs.clearRecentlyUpdated(old, variables.id);
|
|
1033
|
+
queryClient.setQueryData(q.queryKey, cleared);
|
|
1034
|
+
}
|
|
1035
|
+
});
|
|
1036
|
+
} catch (error) {
|
|
1037
|
+
}
|
|
1038
|
+
}, cleanupDelay);
|
|
1039
|
+
} catch (error) {
|
|
1040
|
+
}
|
|
1041
|
+
onSuccess?.(data, variables, onMutateResult, context);
|
|
1042
|
+
};
|
|
1043
|
+
return useMutation({
|
|
1044
|
+
...restOptions,
|
|
1045
|
+
mutationFn: enhancedMutationFn,
|
|
1046
|
+
optimistic: enhancedOptimistic,
|
|
1047
|
+
// 类型复杂,暂时保留
|
|
1048
|
+
onSuccess: enhancedOnSuccess,
|
|
1049
|
+
onError: (error, variables, onMutateResult, context) => {
|
|
1050
|
+
if (error instanceof chunkYW5PNTRU_cjs.ConflictError) {
|
|
1051
|
+
const familyKey = Array.isArray(queryKey) ? queryKey.slice(0, -1) : [queryKey];
|
|
1052
|
+
queryClient.invalidateQueries({ queryKey: familyKey });
|
|
1053
|
+
}
|
|
1054
|
+
restOptions.onError?.(error, variables, onMutateResult, context);
|
|
1055
|
+
}
|
|
1056
|
+
});
|
|
1057
|
+
}
|
|
1058
|
+
function useMutationState(options = {}) {
|
|
1059
|
+
return reactQuery.useMutationState(options);
|
|
1060
|
+
}
|
|
1061
|
+
function useIsFetching(filters) {
|
|
1062
|
+
return reactQuery.useIsFetching(filters);
|
|
1063
|
+
}
|
|
1064
|
+
function useIsMutating(filters) {
|
|
1065
|
+
return reactQuery.useIsMutating(filters);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
Object.defineProperty(exports, "skipToken", {
|
|
1069
|
+
enumerable: true,
|
|
1070
|
+
get: function () { return reactQuery.skipToken; }
|
|
1071
|
+
});
|
|
1072
|
+
exports.batchQueryUtils = batchQueryUtils;
|
|
1073
|
+
exports.calculateBatchStats = calculateBatchStats;
|
|
1074
|
+
exports.createBatchQueryConfig = createBatchQueryConfig;
|
|
1075
|
+
exports.createCursorPaginationOptions = createCursorPaginationOptions;
|
|
1076
|
+
exports.createInfiniteQueryOptions = createInfiniteQueryOptions;
|
|
1077
|
+
exports.createOffsetPaginationOptions = createOffsetPaginationOptions;
|
|
1078
|
+
exports.createPageNumberPaginationOptions = createPageNumberPaginationOptions;
|
|
1079
|
+
exports.createSuspenseInfiniteQuery = createSuspenseInfiniteQuery;
|
|
1080
|
+
exports.createSuspenseQuery = createSuspenseQuery;
|
|
1081
|
+
exports.setupMutationDefaults = setupMutationDefaults;
|
|
1082
|
+
exports.useAutoRefreshBatchQueries = useAutoRefreshBatchQueries;
|
|
1083
|
+
exports.useBatchPrefetch = useBatchPrefetch;
|
|
1084
|
+
exports.useBatchQueryPerformance = useBatchQueryPerformance;
|
|
1085
|
+
exports.useCombinedQueries = useCombinedQueries;
|
|
1086
|
+
exports.useConditionalBatchQueries = useConditionalBatchQueries;
|
|
1087
|
+
exports.useConditionalFocusRefetch = useConditionalFocusRefetch;
|
|
1088
|
+
exports.useConditionalPrefetch = useConditionalPrefetch;
|
|
1089
|
+
exports.useDashboardQueries = useDashboardQueries;
|
|
1090
|
+
exports.useDataGuardMutation = useDataGuardMutation;
|
|
1091
|
+
exports.useDataGuardQueryConfig = useDataGuardQueryConfig;
|
|
1092
|
+
exports.useDependentBatchQueries = useDependentBatchQueries;
|
|
1093
|
+
exports.useDynamicBatchQueries = useDynamicBatchQueries;
|
|
1094
|
+
exports.useEnhancedInfiniteQuery = useEnhancedInfiniteQuery;
|
|
1095
|
+
exports.useEnhancedQueries = useEnhancedQueries;
|
|
1096
|
+
exports.useEnhancedQuery = useEnhancedQuery;
|
|
1097
|
+
exports.useEnhancedSuspenseInfiniteQuery = useEnhancedSuspenseInfiniteQuery;
|
|
1098
|
+
exports.useEnhancedSuspenseQueries = useEnhancedSuspenseQueries;
|
|
1099
|
+
exports.useEnhancedSuspenseQuery = useEnhancedSuspenseQuery;
|
|
1100
|
+
exports.useFocusCallback = useFocusCallback;
|
|
1101
|
+
exports.useFocusRefetch = useFocusRefetch;
|
|
1102
|
+
exports.useFocusState = useFocusState;
|
|
1103
|
+
exports.useHoverPrefetch = useHoverPrefetch;
|
|
1104
|
+
exports.useIdlePrefetch = useIdlePrefetch;
|
|
1105
|
+
exports.useIsFetching = useIsFetching;
|
|
1106
|
+
exports.useIsMutating = useIsMutating;
|
|
1107
|
+
exports.useListMutation = useListMutation;
|
|
1108
|
+
exports.useMutation = useMutation;
|
|
1109
|
+
exports.useMutationState = useMutationState;
|
|
1110
|
+
exports.usePageVisibility = usePageVisibility;
|
|
1111
|
+
exports.usePaginatedBatchQueries = usePaginatedBatchQueries;
|
|
1112
|
+
exports.usePauseFocus = usePauseFocus;
|
|
1113
|
+
exports.usePeriodicPrefetch = usePeriodicPrefetch;
|
|
1114
|
+
exports.usePredictivePrefetch = usePredictivePrefetch;
|
|
1115
|
+
exports.usePriorityPrefetch = usePriorityPrefetch;
|
|
1116
|
+
exports.useRetryBatchQueries = useRetryBatchQueries;
|
|
1117
|
+
exports.useRoutePrefetch = useRoutePrefetch;
|
|
1118
|
+
exports.useSmartFocusManager = useSmartFocusManager;
|
|
1119
|
+
exports.useSmartPrefetch = useSmartPrefetch;
|
|
1120
|
+
//# sourceMappingURL=chunk-52ZO6Y67.cjs.map
|
|
1121
|
+
//# sourceMappingURL=chunk-52ZO6Y67.cjs.map
|