@tanstack/query-core 5.91.2 → 5.94.4
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/package.json +1 -1
- package/build/legacy/_tsup-dts-rollup.d.cts +0 -2229
- package/build/legacy/_tsup-dts-rollup.d.ts +0 -2229
- package/build/legacy/chunk-PXG64RU4.js +0 -25
- package/build/legacy/chunk-PXG64RU4.js.map +0 -1
- package/build/legacy/environmentManager.cjs +0 -48
- package/build/legacy/environmentManager.cjs.map +0 -1
- package/build/legacy/environmentManager.d.cts +0 -2
- package/build/legacy/environmentManager.d.ts +0 -2
- package/build/legacy/environmentManager.js +0 -25
- package/build/legacy/environmentManager.js.map +0 -1
- package/build/legacy/focusManager.cjs +0 -107
- package/build/legacy/focusManager.cjs.map +0 -1
- package/build/legacy/focusManager.d.cts +0 -2
- package/build/legacy/focusManager.d.ts +0 -2
- package/build/legacy/focusManager.js +0 -80
- package/build/legacy/focusManager.js.map +0 -1
- package/build/legacy/hydration.cjs +0 -178
- package/build/legacy/hydration.cjs.map +0 -1
- package/build/legacy/hydration.d.cts +0 -7
- package/build/legacy/hydration.d.ts +0 -7
- package/build/legacy/hydration.js +0 -152
- package/build/legacy/hydration.js.map +0 -1
- package/build/legacy/index.cjs +0 -113
- package/build/legacy/index.cjs.map +0 -1
- package/build/legacy/index.d.cts +0 -142
- package/build/legacy/index.d.ts +0 -142
- package/build/legacy/index.js +0 -75
- package/build/legacy/index.js.map +0 -1
- package/build/legacy/infiniteQueryBehavior.cjs +0 -146
- package/build/legacy/infiniteQueryBehavior.cjs.map +0 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +0 -3
- package/build/legacy/infiniteQueryBehavior.d.ts +0 -3
- package/build/legacy/infiniteQueryBehavior.js +0 -126
- package/build/legacy/infiniteQueryBehavior.js.map +0 -1
- package/build/legacy/infiniteQueryObserver.cjs +0 -93
- package/build/legacy/infiniteQueryObserver.cjs.map +0 -1
- package/build/legacy/infiniteQueryObserver.d.cts +0 -1
- package/build/legacy/infiniteQueryObserver.d.ts +0 -1
- package/build/legacy/infiniteQueryObserver.js +0 -74
- package/build/legacy/infiniteQueryObserver.js.map +0 -1
- package/build/legacy/mutation.cjs +0 -333
- package/build/legacy/mutation.cjs.map +0 -1
- package/build/legacy/mutation.d.cts +0 -4
- package/build/legacy/mutation.d.ts +0 -4
- package/build/legacy/mutation.js +0 -306
- package/build/legacy/mutation.js.map +0 -1
- package/build/legacy/mutationCache.cjs +0 -170
- package/build/legacy/mutationCache.cjs.map +0 -1
- package/build/legacy/mutationCache.d.cts +0 -2
- package/build/legacy/mutationCache.d.ts +0 -2
- package/build/legacy/mutationCache.js +0 -137
- package/build/legacy/mutationCache.js.map +0 -1
- package/build/legacy/mutationObserver.cjs +0 -190
- package/build/legacy/mutationObserver.cjs.map +0 -1
- package/build/legacy/mutationObserver.d.cts +0 -1
- package/build/legacy/mutationObserver.d.ts +0 -1
- package/build/legacy/mutationObserver.js +0 -164
- package/build/legacy/mutationObserver.js.map +0 -1
- package/build/legacy/notifyManager.cjs +0 -113
- package/build/legacy/notifyManager.cjs.map +0 -1
- package/build/legacy/notifyManager.d.cts +0 -3
- package/build/legacy/notifyManager.d.ts +0 -3
- package/build/legacy/notifyManager.js +0 -88
- package/build/legacy/notifyManager.js.map +0 -1
- package/build/legacy/onlineManager.cjs +0 -96
- package/build/legacy/onlineManager.cjs.map +0 -1
- package/build/legacy/onlineManager.d.cts +0 -2
- package/build/legacy/onlineManager.d.ts +0 -2
- package/build/legacy/onlineManager.js +0 -69
- package/build/legacy/onlineManager.js.map +0 -1
- package/build/legacy/queriesObserver.cjs +0 -260
- package/build/legacy/queriesObserver.cjs.map +0 -1
- package/build/legacy/queriesObserver.d.cts +0 -2
- package/build/legacy/queriesObserver.d.ts +0 -2
- package/build/legacy/queriesObserver.js +0 -234
- package/build/legacy/queriesObserver.js.map +0 -1
- package/build/legacy/query.cjs +0 -487
- package/build/legacy/query.cjs.map +0 -1
- package/build/legacy/query.d.cts +0 -10
- package/build/legacy/query.d.ts +0 -10
- package/build/legacy/query.js +0 -468
- package/build/legacy/query.js.map +0 -1
- package/build/legacy/queryCache.cjs +0 -131
- package/build/legacy/queryCache.cjs.map +0 -1
- package/build/legacy/queryCache.d.cts +0 -3
- package/build/legacy/queryCache.d.ts +0 -3
- package/build/legacy/queryCache.js +0 -105
- package/build/legacy/queryCache.js.map +0 -1
- package/build/legacy/queryClient.cjs +0 -349
- package/build/legacy/queryClient.cjs.map +0 -1
- package/build/legacy/queryClient.d.cts +0 -1
- package/build/legacy/queryClient.d.ts +0 -1
- package/build/legacy/queryClient.js +0 -324
- package/build/legacy/queryClient.js.map +0 -1
- package/build/legacy/queryObserver.cjs +0 -516
- package/build/legacy/queryObserver.cjs.map +0 -1
- package/build/legacy/queryObserver.d.cts +0 -1
- package/build/legacy/queryObserver.d.ts +0 -1
- package/build/legacy/queryObserver.js +0 -498
- package/build/legacy/queryObserver.js.map +0 -1
- package/build/legacy/removable.cjs +0 -70
- package/build/legacy/removable.cjs.map +0 -1
- package/build/legacy/removable.d.cts +0 -1
- package/build/legacy/removable.d.ts +0 -1
- package/build/legacy/removable.js +0 -44
- package/build/legacy/removable.js.map +0 -1
- package/build/legacy/retryer.cjs +0 -166
- package/build/legacy/retryer.cjs.map +0 -1
- package/build/legacy/retryer.d.cts +0 -7
- package/build/legacy/retryer.d.ts +0 -7
- package/build/legacy/retryer.js +0 -140
- package/build/legacy/retryer.js.map +0 -1
- package/build/legacy/streamedQuery.cjs +0 -80
- package/build/legacy/streamedQuery.cjs.map +0 -1
- package/build/legacy/streamedQuery.d.cts +0 -1
- package/build/legacy/streamedQuery.d.ts +0 -1
- package/build/legacy/streamedQuery.js +0 -57
- package/build/legacy/streamedQuery.js.map +0 -1
- package/build/legacy/subscribable.cjs +0 -51
- package/build/legacy/subscribable.cjs.map +0 -1
- package/build/legacy/subscribable.d.cts +0 -1
- package/build/legacy/subscribable.d.ts +0 -1
- package/build/legacy/subscribable.js +0 -28
- package/build/legacy/subscribable.js.map +0 -1
- package/build/legacy/thenable.cjs +0 -76
- package/build/legacy/thenable.cjs.map +0 -1
- package/build/legacy/thenable.d.cts +0 -6
- package/build/legacy/thenable.d.ts +0 -6
- package/build/legacy/thenable.js +0 -52
- package/build/legacy/thenable.js.map +0 -1
- package/build/legacy/timeoutManager.cjs +0 -110
- package/build/legacy/timeoutManager.cjs.map +0 -1
- package/build/legacy/timeoutManager.d.cts +0 -7
- package/build/legacy/timeoutManager.d.ts +0 -7
- package/build/legacy/timeoutManager.js +0 -81
- package/build/legacy/timeoutManager.js.map +0 -1
- package/build/legacy/types.cjs +0 -37
- package/build/legacy/types.cjs.map +0 -1
- package/build/legacy/types.d.cts +0 -95
- package/build/legacy/types.d.ts +0 -95
- package/build/legacy/types.js +0 -12
- package/build/legacy/types.js.map +0 -1
- package/build/legacy/utils.cjs +0 -330
- package/build/legacy/utils.cjs.map +0 -1
- package/build/legacy/utils.d.cts +0 -30
- package/build/legacy/utils.d.ts +0 -30
- package/build/legacy/utils.js +0 -283
- package/build/legacy/utils.js.map +0 -1
- package/build/modern/_tsup-dts-rollup.d.cts +0 -2229
- package/build/modern/_tsup-dts-rollup.d.ts +0 -2229
- package/build/modern/environmentManager.cjs +0 -48
- package/build/modern/environmentManager.cjs.map +0 -1
- package/build/modern/environmentManager.d.cts +0 -2
- package/build/modern/environmentManager.d.ts +0 -2
- package/build/modern/environmentManager.js +0 -23
- package/build/modern/environmentManager.js.map +0 -1
- package/build/modern/focusManager.cjs +0 -93
- package/build/modern/focusManager.cjs.map +0 -1
- package/build/modern/focusManager.d.cts +0 -2
- package/build/modern/focusManager.d.ts +0 -2
- package/build/modern/focusManager.js +0 -67
- package/build/modern/focusManager.js.map +0 -1
- package/build/modern/hydration.cjs +0 -173
- package/build/modern/hydration.cjs.map +0 -1
- package/build/modern/hydration.d.cts +0 -7
- package/build/modern/hydration.d.ts +0 -7
- package/build/modern/hydration.js +0 -145
- package/build/modern/hydration.js.map +0 -1
- package/build/modern/index.cjs +0 -113
- package/build/modern/index.cjs.map +0 -1
- package/build/modern/index.d.cts +0 -142
- package/build/modern/index.d.ts +0 -142
- package/build/modern/index.js +0 -73
- package/build/modern/index.js.map +0 -1
- package/build/modern/infiniteQueryBehavior.cjs +0 -142
- package/build/modern/infiniteQueryBehavior.cjs.map +0 -1
- package/build/modern/infiniteQueryBehavior.d.cts +0 -3
- package/build/modern/infiniteQueryBehavior.d.ts +0 -3
- package/build/modern/infiniteQueryBehavior.js +0 -120
- package/build/modern/infiniteQueryBehavior.js.map +0 -1
- package/build/modern/infiniteQueryObserver.cjs +0 -92
- package/build/modern/infiniteQueryObserver.cjs.map +0 -1
- package/build/modern/infiniteQueryObserver.d.cts +0 -1
- package/build/modern/infiniteQueryObserver.d.ts +0 -1
- package/build/modern/infiniteQueryObserver.js +0 -71
- package/build/modern/infiniteQueryObserver.js.map +0 -1
- package/build/modern/mutation.cjs +0 -307
- package/build/modern/mutation.cjs.map +0 -1
- package/build/modern/mutation.d.cts +0 -4
- package/build/modern/mutation.d.ts +0 -4
- package/build/modern/mutation.js +0 -281
- package/build/modern/mutation.js.map +0 -1
- package/build/modern/mutationCache.cjs +0 -149
- package/build/modern/mutationCache.cjs.map +0 -1
- package/build/modern/mutationCache.d.cts +0 -2
- package/build/modern/mutationCache.d.ts +0 -2
- package/build/modern/mutationCache.js +0 -124
- package/build/modern/mutationCache.js.map +0 -1
- package/build/modern/mutationObserver.cjs +0 -165
- package/build/modern/mutationObserver.cjs.map +0 -1
- package/build/modern/mutationObserver.d.cts +0 -1
- package/build/modern/mutationObserver.d.ts +0 -1
- package/build/modern/mutationObserver.js +0 -140
- package/build/modern/mutationObserver.js.map +0 -1
- package/build/modern/notifyManager.cjs +0 -113
- package/build/modern/notifyManager.cjs.map +0 -1
- package/build/modern/notifyManager.d.cts +0 -3
- package/build/modern/notifyManager.d.ts +0 -3
- package/build/modern/notifyManager.js +0 -86
- package/build/modern/notifyManager.js.map +0 -1
- package/build/modern/onlineManager.cjs +0 -83
- package/build/modern/onlineManager.cjs.map +0 -1
- package/build/modern/onlineManager.d.cts +0 -2
- package/build/modern/onlineManager.d.ts +0 -2
- package/build/modern/onlineManager.js +0 -57
- package/build/modern/onlineManager.js.map +0 -1
- package/build/modern/queriesObserver.cjs +0 -237
- package/build/modern/queriesObserver.cjs.map +0 -1
- package/build/modern/queriesObserver.d.cts +0 -2
- package/build/modern/queriesObserver.d.ts +0 -2
- package/build/modern/queriesObserver.js +0 -212
- package/build/modern/queriesObserver.js.map +0 -1
- package/build/modern/query.cjs +0 -461
- package/build/modern/query.cjs.map +0 -1
- package/build/modern/query.d.cts +0 -10
- package/build/modern/query.d.ts +0 -10
- package/build/modern/query.js +0 -443
- package/build/modern/query.js.map +0 -1
- package/build/modern/queryCache.cjs +0 -122
- package/build/modern/queryCache.cjs.map +0 -1
- package/build/modern/queryCache.d.cts +0 -3
- package/build/modern/queryCache.d.ts +0 -3
- package/build/modern/queryCache.js +0 -97
- package/build/modern/queryCache.js.map +0 -1
- package/build/modern/queryClient.cjs +0 -322
- package/build/modern/queryClient.cjs.map +0 -1
- package/build/modern/queryClient.d.cts +0 -1
- package/build/modern/queryClient.d.ts +0 -1
- package/build/modern/queryClient.js +0 -305
- package/build/modern/queryClient.js.map +0 -1
- package/build/modern/queryObserver.cjs +0 -489
- package/build/modern/queryObserver.cjs.map +0 -1
- package/build/modern/queryObserver.d.cts +0 -1
- package/build/modern/queryObserver.d.ts +0 -1
- package/build/modern/queryObserver.js +0 -472
- package/build/modern/queryObserver.js.map +0 -1
- package/build/modern/removable.cjs +0 -59
- package/build/modern/removable.cjs.map +0 -1
- package/build/modern/removable.d.cts +0 -1
- package/build/modern/removable.d.ts +0 -1
- package/build/modern/removable.js +0 -34
- package/build/modern/removable.js.map +0 -1
- package/build/modern/retryer.cjs +0 -162
- package/build/modern/retryer.cjs.map +0 -1
- package/build/modern/retryer.d.cts +0 -7
- package/build/modern/retryer.d.ts +0 -7
- package/build/modern/retryer.js +0 -134
- package/build/modern/retryer.js.map +0 -1
- package/build/modern/streamedQuery.cjs +0 -80
- package/build/modern/streamedQuery.cjs.map +0 -1
- package/build/modern/streamedQuery.d.cts +0 -1
- package/build/modern/streamedQuery.d.ts +0 -1
- package/build/modern/streamedQuery.js +0 -55
- package/build/modern/streamedQuery.js.map +0 -1
- package/build/modern/subscribable.cjs +0 -51
- package/build/modern/subscribable.cjs.map +0 -1
- package/build/modern/subscribable.d.cts +0 -1
- package/build/modern/subscribable.d.ts +0 -1
- package/build/modern/subscribable.js +0 -26
- package/build/modern/subscribable.js.map +0 -1
- package/build/modern/thenable.cjs +0 -75
- package/build/modern/thenable.cjs.map +0 -1
- package/build/modern/thenable.d.cts +0 -6
- package/build/modern/thenable.d.ts +0 -6
- package/build/modern/thenable.js +0 -49
- package/build/modern/thenable.js.map +0 -1
- package/build/modern/timeoutManager.cjs +0 -98
- package/build/modern/timeoutManager.cjs.map +0 -1
- package/build/modern/timeoutManager.d.cts +0 -7
- package/build/modern/timeoutManager.d.ts +0 -7
- package/build/modern/timeoutManager.js +0 -70
- package/build/modern/timeoutManager.js.map +0 -1
- package/build/modern/types.cjs +0 -37
- package/build/modern/types.cjs.map +0 -1
- package/build/modern/types.d.cts +0 -95
- package/build/modern/types.d.ts +0 -95
- package/build/modern/types.js +0 -10
- package/build/modern/types.js.map +0 -1
- package/build/modern/utils.cjs +0 -330
- package/build/modern/utils.cjs.map +0 -1
- package/build/modern/utils.d.cts +0 -30
- package/build/modern/utils.d.ts +0 -30
- package/build/modern/utils.js +0 -281
- package/build/modern/utils.js.map +0 -1
package/build/modern/query.js
DELETED
|
@@ -1,443 +0,0 @@
|
|
|
1
|
-
// src/query.ts
|
|
2
|
-
import {
|
|
3
|
-
ensureQueryFn,
|
|
4
|
-
noop,
|
|
5
|
-
replaceData,
|
|
6
|
-
resolveEnabled,
|
|
7
|
-
resolveStaleTime,
|
|
8
|
-
skipToken,
|
|
9
|
-
timeUntilStale
|
|
10
|
-
} from "./utils.js";
|
|
11
|
-
import { notifyManager } from "./notifyManager.js";
|
|
12
|
-
import { CancelledError, canFetch, createRetryer } from "./retryer.js";
|
|
13
|
-
import { Removable } from "./removable.js";
|
|
14
|
-
var Query = class extends Removable {
|
|
15
|
-
#initialState;
|
|
16
|
-
#revertState;
|
|
17
|
-
#cache;
|
|
18
|
-
#client;
|
|
19
|
-
#retryer;
|
|
20
|
-
#defaultOptions;
|
|
21
|
-
#abortSignalConsumed;
|
|
22
|
-
constructor(config) {
|
|
23
|
-
super();
|
|
24
|
-
this.#abortSignalConsumed = false;
|
|
25
|
-
this.#defaultOptions = config.defaultOptions;
|
|
26
|
-
this.setOptions(config.options);
|
|
27
|
-
this.observers = [];
|
|
28
|
-
this.#client = config.client;
|
|
29
|
-
this.#cache = this.#client.getQueryCache();
|
|
30
|
-
this.queryKey = config.queryKey;
|
|
31
|
-
this.queryHash = config.queryHash;
|
|
32
|
-
this.#initialState = getDefaultState(this.options);
|
|
33
|
-
this.state = config.state ?? this.#initialState;
|
|
34
|
-
this.scheduleGc();
|
|
35
|
-
}
|
|
36
|
-
get meta() {
|
|
37
|
-
return this.options.meta;
|
|
38
|
-
}
|
|
39
|
-
get promise() {
|
|
40
|
-
return this.#retryer?.promise;
|
|
41
|
-
}
|
|
42
|
-
setOptions(options) {
|
|
43
|
-
this.options = { ...this.#defaultOptions, ...options };
|
|
44
|
-
this.updateGcTime(this.options.gcTime);
|
|
45
|
-
if (this.state && this.state.data === void 0) {
|
|
46
|
-
const defaultState = getDefaultState(this.options);
|
|
47
|
-
if (defaultState.data !== void 0) {
|
|
48
|
-
this.setState(
|
|
49
|
-
successState(defaultState.data, defaultState.dataUpdatedAt)
|
|
50
|
-
);
|
|
51
|
-
this.#initialState = defaultState;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
optionalRemove() {
|
|
56
|
-
if (!this.observers.length && this.state.fetchStatus === "idle") {
|
|
57
|
-
this.#cache.remove(this);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
setData(newData, options) {
|
|
61
|
-
const data = replaceData(this.state.data, newData, this.options);
|
|
62
|
-
this.#dispatch({
|
|
63
|
-
data,
|
|
64
|
-
type: "success",
|
|
65
|
-
dataUpdatedAt: options?.updatedAt,
|
|
66
|
-
manual: options?.manual
|
|
67
|
-
});
|
|
68
|
-
return data;
|
|
69
|
-
}
|
|
70
|
-
setState(state, setStateOptions) {
|
|
71
|
-
this.#dispatch({ type: "setState", state, setStateOptions });
|
|
72
|
-
}
|
|
73
|
-
cancel(options) {
|
|
74
|
-
const promise = this.#retryer?.promise;
|
|
75
|
-
this.#retryer?.cancel(options);
|
|
76
|
-
return promise ? promise.then(noop).catch(noop) : Promise.resolve();
|
|
77
|
-
}
|
|
78
|
-
destroy() {
|
|
79
|
-
super.destroy();
|
|
80
|
-
this.cancel({ silent: true });
|
|
81
|
-
}
|
|
82
|
-
get resetState() {
|
|
83
|
-
return this.#initialState;
|
|
84
|
-
}
|
|
85
|
-
reset() {
|
|
86
|
-
this.destroy();
|
|
87
|
-
this.setState(this.resetState);
|
|
88
|
-
}
|
|
89
|
-
isActive() {
|
|
90
|
-
return this.observers.some(
|
|
91
|
-
(observer) => resolveEnabled(observer.options.enabled, this) !== false
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
isDisabled() {
|
|
95
|
-
if (this.getObserversCount() > 0) {
|
|
96
|
-
return !this.isActive();
|
|
97
|
-
}
|
|
98
|
-
return this.options.queryFn === skipToken || !this.isFetched();
|
|
99
|
-
}
|
|
100
|
-
isFetched() {
|
|
101
|
-
return this.state.dataUpdateCount + this.state.errorUpdateCount > 0;
|
|
102
|
-
}
|
|
103
|
-
isStatic() {
|
|
104
|
-
if (this.getObserversCount() > 0) {
|
|
105
|
-
return this.observers.some(
|
|
106
|
-
(observer) => resolveStaleTime(observer.options.staleTime, this) === "static"
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
isStale() {
|
|
112
|
-
if (this.getObserversCount() > 0) {
|
|
113
|
-
return this.observers.some(
|
|
114
|
-
(observer) => observer.getCurrentResult().isStale
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
return this.state.data === void 0 || this.state.isInvalidated;
|
|
118
|
-
}
|
|
119
|
-
isStaleByTime(staleTime = 0) {
|
|
120
|
-
if (this.state.data === void 0) {
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
if (staleTime === "static") {
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
if (this.state.isInvalidated) {
|
|
127
|
-
return true;
|
|
128
|
-
}
|
|
129
|
-
return !timeUntilStale(this.state.dataUpdatedAt, staleTime);
|
|
130
|
-
}
|
|
131
|
-
onFocus() {
|
|
132
|
-
const observer = this.observers.find((x) => x.shouldFetchOnWindowFocus());
|
|
133
|
-
observer?.refetch({ cancelRefetch: false });
|
|
134
|
-
this.#retryer?.continue();
|
|
135
|
-
}
|
|
136
|
-
onOnline() {
|
|
137
|
-
const observer = this.observers.find((x) => x.shouldFetchOnReconnect());
|
|
138
|
-
observer?.refetch({ cancelRefetch: false });
|
|
139
|
-
this.#retryer?.continue();
|
|
140
|
-
}
|
|
141
|
-
addObserver(observer) {
|
|
142
|
-
if (!this.observers.includes(observer)) {
|
|
143
|
-
this.observers.push(observer);
|
|
144
|
-
this.clearGcTimeout();
|
|
145
|
-
this.#cache.notify({ type: "observerAdded", query: this, observer });
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
removeObserver(observer) {
|
|
149
|
-
if (this.observers.includes(observer)) {
|
|
150
|
-
this.observers = this.observers.filter((x) => x !== observer);
|
|
151
|
-
if (!this.observers.length) {
|
|
152
|
-
if (this.#retryer) {
|
|
153
|
-
if (this.#abortSignalConsumed || this.#isInitialPausedFetch()) {
|
|
154
|
-
this.#retryer.cancel({ revert: true });
|
|
155
|
-
} else {
|
|
156
|
-
this.#retryer.cancelRetry();
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
this.scheduleGc();
|
|
160
|
-
}
|
|
161
|
-
this.#cache.notify({ type: "observerRemoved", query: this, observer });
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
getObserversCount() {
|
|
165
|
-
return this.observers.length;
|
|
166
|
-
}
|
|
167
|
-
#isInitialPausedFetch() {
|
|
168
|
-
return this.state.fetchStatus === "paused" && this.state.status === "pending";
|
|
169
|
-
}
|
|
170
|
-
invalidate() {
|
|
171
|
-
if (!this.state.isInvalidated) {
|
|
172
|
-
this.#dispatch({ type: "invalidate" });
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
async fetch(options, fetchOptions) {
|
|
176
|
-
if (this.state.fetchStatus !== "idle" && // If the promise in the retryer is already rejected, we have to definitely
|
|
177
|
-
// re-start the fetch; there is a chance that the query is still in a
|
|
178
|
-
// pending state when that happens
|
|
179
|
-
this.#retryer?.status() !== "rejected") {
|
|
180
|
-
if (this.state.data !== void 0 && fetchOptions?.cancelRefetch) {
|
|
181
|
-
this.cancel({ silent: true });
|
|
182
|
-
} else if (this.#retryer) {
|
|
183
|
-
this.#retryer.continueRetry();
|
|
184
|
-
return this.#retryer.promise;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
if (options) {
|
|
188
|
-
this.setOptions(options);
|
|
189
|
-
}
|
|
190
|
-
if (!this.options.queryFn) {
|
|
191
|
-
const observer = this.observers.find((x) => x.options.queryFn);
|
|
192
|
-
if (observer) {
|
|
193
|
-
this.setOptions(observer.options);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
if (process.env.NODE_ENV !== "production") {
|
|
197
|
-
if (!Array.isArray(this.options.queryKey)) {
|
|
198
|
-
console.error(
|
|
199
|
-
`As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']`
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
const abortController = new AbortController();
|
|
204
|
-
const addSignalProperty = (object) => {
|
|
205
|
-
Object.defineProperty(object, "signal", {
|
|
206
|
-
enumerable: true,
|
|
207
|
-
get: () => {
|
|
208
|
-
this.#abortSignalConsumed = true;
|
|
209
|
-
return abortController.signal;
|
|
210
|
-
}
|
|
211
|
-
});
|
|
212
|
-
};
|
|
213
|
-
const fetchFn = () => {
|
|
214
|
-
const queryFn = ensureQueryFn(this.options, fetchOptions);
|
|
215
|
-
const createQueryFnContext = () => {
|
|
216
|
-
const queryFnContext2 = {
|
|
217
|
-
client: this.#client,
|
|
218
|
-
queryKey: this.queryKey,
|
|
219
|
-
meta: this.meta
|
|
220
|
-
};
|
|
221
|
-
addSignalProperty(queryFnContext2);
|
|
222
|
-
return queryFnContext2;
|
|
223
|
-
};
|
|
224
|
-
const queryFnContext = createQueryFnContext();
|
|
225
|
-
this.#abortSignalConsumed = false;
|
|
226
|
-
if (this.options.persister) {
|
|
227
|
-
return this.options.persister(
|
|
228
|
-
queryFn,
|
|
229
|
-
queryFnContext,
|
|
230
|
-
this
|
|
231
|
-
);
|
|
232
|
-
}
|
|
233
|
-
return queryFn(queryFnContext);
|
|
234
|
-
};
|
|
235
|
-
const createFetchContext = () => {
|
|
236
|
-
const context2 = {
|
|
237
|
-
fetchOptions,
|
|
238
|
-
options: this.options,
|
|
239
|
-
queryKey: this.queryKey,
|
|
240
|
-
client: this.#client,
|
|
241
|
-
state: this.state,
|
|
242
|
-
fetchFn
|
|
243
|
-
};
|
|
244
|
-
addSignalProperty(context2);
|
|
245
|
-
return context2;
|
|
246
|
-
};
|
|
247
|
-
const context = createFetchContext();
|
|
248
|
-
this.options.behavior?.onFetch(context, this);
|
|
249
|
-
this.#revertState = this.state;
|
|
250
|
-
if (this.state.fetchStatus === "idle" || this.state.fetchMeta !== context.fetchOptions?.meta) {
|
|
251
|
-
this.#dispatch({ type: "fetch", meta: context.fetchOptions?.meta });
|
|
252
|
-
}
|
|
253
|
-
this.#retryer = createRetryer({
|
|
254
|
-
initialPromise: fetchOptions?.initialPromise,
|
|
255
|
-
fn: context.fetchFn,
|
|
256
|
-
onCancel: (error) => {
|
|
257
|
-
if (error instanceof CancelledError && error.revert) {
|
|
258
|
-
this.setState({
|
|
259
|
-
...this.#revertState,
|
|
260
|
-
fetchStatus: "idle"
|
|
261
|
-
});
|
|
262
|
-
}
|
|
263
|
-
abortController.abort();
|
|
264
|
-
},
|
|
265
|
-
onFail: (failureCount, error) => {
|
|
266
|
-
this.#dispatch({ type: "failed", failureCount, error });
|
|
267
|
-
},
|
|
268
|
-
onPause: () => {
|
|
269
|
-
this.#dispatch({ type: "pause" });
|
|
270
|
-
},
|
|
271
|
-
onContinue: () => {
|
|
272
|
-
this.#dispatch({ type: "continue" });
|
|
273
|
-
},
|
|
274
|
-
retry: context.options.retry,
|
|
275
|
-
retryDelay: context.options.retryDelay,
|
|
276
|
-
networkMode: context.options.networkMode,
|
|
277
|
-
canRun: () => true
|
|
278
|
-
});
|
|
279
|
-
try {
|
|
280
|
-
const data = await this.#retryer.start();
|
|
281
|
-
if (data === void 0) {
|
|
282
|
-
if (process.env.NODE_ENV !== "production") {
|
|
283
|
-
console.error(
|
|
284
|
-
`Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
throw new Error(`${this.queryHash} data is undefined`);
|
|
288
|
-
}
|
|
289
|
-
this.setData(data);
|
|
290
|
-
this.#cache.config.onSuccess?.(data, this);
|
|
291
|
-
this.#cache.config.onSettled?.(
|
|
292
|
-
data,
|
|
293
|
-
this.state.error,
|
|
294
|
-
this
|
|
295
|
-
);
|
|
296
|
-
return data;
|
|
297
|
-
} catch (error) {
|
|
298
|
-
if (error instanceof CancelledError) {
|
|
299
|
-
if (error.silent) {
|
|
300
|
-
return this.#retryer.promise;
|
|
301
|
-
} else if (error.revert) {
|
|
302
|
-
if (this.state.data === void 0) {
|
|
303
|
-
throw error;
|
|
304
|
-
}
|
|
305
|
-
return this.state.data;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
this.#dispatch({
|
|
309
|
-
type: "error",
|
|
310
|
-
error
|
|
311
|
-
});
|
|
312
|
-
this.#cache.config.onError?.(
|
|
313
|
-
error,
|
|
314
|
-
this
|
|
315
|
-
);
|
|
316
|
-
this.#cache.config.onSettled?.(
|
|
317
|
-
this.state.data,
|
|
318
|
-
error,
|
|
319
|
-
this
|
|
320
|
-
);
|
|
321
|
-
throw error;
|
|
322
|
-
} finally {
|
|
323
|
-
this.scheduleGc();
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
#dispatch(action) {
|
|
327
|
-
const reducer = (state) => {
|
|
328
|
-
switch (action.type) {
|
|
329
|
-
case "failed":
|
|
330
|
-
return {
|
|
331
|
-
...state,
|
|
332
|
-
fetchFailureCount: action.failureCount,
|
|
333
|
-
fetchFailureReason: action.error
|
|
334
|
-
};
|
|
335
|
-
case "pause":
|
|
336
|
-
return {
|
|
337
|
-
...state,
|
|
338
|
-
fetchStatus: "paused"
|
|
339
|
-
};
|
|
340
|
-
case "continue":
|
|
341
|
-
return {
|
|
342
|
-
...state,
|
|
343
|
-
fetchStatus: "fetching"
|
|
344
|
-
};
|
|
345
|
-
case "fetch":
|
|
346
|
-
return {
|
|
347
|
-
...state,
|
|
348
|
-
...fetchState(state.data, this.options),
|
|
349
|
-
fetchMeta: action.meta ?? null
|
|
350
|
-
};
|
|
351
|
-
case "success":
|
|
352
|
-
const newState = {
|
|
353
|
-
...state,
|
|
354
|
-
...successState(action.data, action.dataUpdatedAt),
|
|
355
|
-
dataUpdateCount: state.dataUpdateCount + 1,
|
|
356
|
-
...!action.manual && {
|
|
357
|
-
fetchStatus: "idle",
|
|
358
|
-
fetchFailureCount: 0,
|
|
359
|
-
fetchFailureReason: null
|
|
360
|
-
}
|
|
361
|
-
};
|
|
362
|
-
this.#revertState = action.manual ? newState : void 0;
|
|
363
|
-
return newState;
|
|
364
|
-
case "error":
|
|
365
|
-
const error = action.error;
|
|
366
|
-
return {
|
|
367
|
-
...state,
|
|
368
|
-
error,
|
|
369
|
-
errorUpdateCount: state.errorUpdateCount + 1,
|
|
370
|
-
errorUpdatedAt: Date.now(),
|
|
371
|
-
fetchFailureCount: state.fetchFailureCount + 1,
|
|
372
|
-
fetchFailureReason: error,
|
|
373
|
-
fetchStatus: "idle",
|
|
374
|
-
status: "error",
|
|
375
|
-
// flag existing data as invalidated if we get a background error
|
|
376
|
-
// note that "no data" always means stale so we can set unconditionally here
|
|
377
|
-
isInvalidated: true
|
|
378
|
-
};
|
|
379
|
-
case "invalidate":
|
|
380
|
-
return {
|
|
381
|
-
...state,
|
|
382
|
-
isInvalidated: true
|
|
383
|
-
};
|
|
384
|
-
case "setState":
|
|
385
|
-
return {
|
|
386
|
-
...state,
|
|
387
|
-
...action.state
|
|
388
|
-
};
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
this.state = reducer(this.state);
|
|
392
|
-
notifyManager.batch(() => {
|
|
393
|
-
this.observers.forEach((observer) => {
|
|
394
|
-
observer.onQueryUpdate();
|
|
395
|
-
});
|
|
396
|
-
this.#cache.notify({ query: this, type: "updated", action });
|
|
397
|
-
});
|
|
398
|
-
}
|
|
399
|
-
};
|
|
400
|
-
function fetchState(data, options) {
|
|
401
|
-
return {
|
|
402
|
-
fetchFailureCount: 0,
|
|
403
|
-
fetchFailureReason: null,
|
|
404
|
-
fetchStatus: canFetch(options.networkMode) ? "fetching" : "paused",
|
|
405
|
-
...data === void 0 && {
|
|
406
|
-
error: null,
|
|
407
|
-
status: "pending"
|
|
408
|
-
}
|
|
409
|
-
};
|
|
410
|
-
}
|
|
411
|
-
function successState(data, dataUpdatedAt) {
|
|
412
|
-
return {
|
|
413
|
-
data,
|
|
414
|
-
dataUpdatedAt: dataUpdatedAt ?? Date.now(),
|
|
415
|
-
error: null,
|
|
416
|
-
isInvalidated: false,
|
|
417
|
-
status: "success"
|
|
418
|
-
};
|
|
419
|
-
}
|
|
420
|
-
function getDefaultState(options) {
|
|
421
|
-
const data = typeof options.initialData === "function" ? options.initialData() : options.initialData;
|
|
422
|
-
const hasData = data !== void 0;
|
|
423
|
-
const initialDataUpdatedAt = hasData ? typeof options.initialDataUpdatedAt === "function" ? options.initialDataUpdatedAt() : options.initialDataUpdatedAt : 0;
|
|
424
|
-
return {
|
|
425
|
-
data,
|
|
426
|
-
dataUpdateCount: 0,
|
|
427
|
-
dataUpdatedAt: hasData ? initialDataUpdatedAt ?? Date.now() : 0,
|
|
428
|
-
error: null,
|
|
429
|
-
errorUpdateCount: 0,
|
|
430
|
-
errorUpdatedAt: 0,
|
|
431
|
-
fetchFailureCount: 0,
|
|
432
|
-
fetchFailureReason: null,
|
|
433
|
-
fetchMeta: null,
|
|
434
|
-
isInvalidated: false,
|
|
435
|
-
status: hasData ? "success" : "pending",
|
|
436
|
-
fetchStatus: "idle"
|
|
437
|
-
};
|
|
438
|
-
}
|
|
439
|
-
export {
|
|
440
|
-
Query,
|
|
441
|
-
fetchState
|
|
442
|
-
};
|
|
443
|
-
//# sourceMappingURL=query.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/query.ts"],"sourcesContent":["import {\n ensureQueryFn,\n noop,\n replaceData,\n resolveEnabled,\n resolveStaleTime,\n skipToken,\n timeUntilStale,\n} from './utils'\nimport { notifyManager } from './notifyManager'\nimport { CancelledError, canFetch, createRetryer } from './retryer'\nimport { Removable } from './removable'\nimport type { QueryCache } from './queryCache'\nimport type { QueryClient } from './queryClient'\nimport type {\n CancelOptions,\n DefaultError,\n FetchStatus,\n InitialDataFunction,\n OmitKeyof,\n QueryFunctionContext,\n QueryKey,\n QueryMeta,\n QueryOptions,\n QueryStatus,\n SetDataOptions,\n StaleTime,\n} from './types'\nimport type { QueryObserver } from './queryObserver'\nimport type { Retryer } from './retryer'\n\n// TYPES\n\ninterface QueryConfig<\n TQueryFnData,\n TError,\n TData,\n TQueryKey extends QueryKey = QueryKey,\n> {\n client: QueryClient\n queryKey: TQueryKey\n queryHash: string\n options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>\n defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>\n state?: QueryState<TData, TError>\n}\n\nexport interface QueryState<TData = unknown, TError = DefaultError> {\n data: TData | undefined\n dataUpdateCount: number\n dataUpdatedAt: number\n error: TError | null\n errorUpdateCount: number\n errorUpdatedAt: number\n fetchFailureCount: number\n fetchFailureReason: TError | null\n fetchMeta: FetchMeta | null\n isInvalidated: boolean\n status: QueryStatus\n fetchStatus: FetchStatus\n}\n\nexport interface FetchContext<\n TQueryFnData,\n TError,\n TData,\n TQueryKey extends QueryKey = QueryKey,\n> {\n fetchFn: () => unknown | Promise<unknown>\n fetchOptions?: FetchOptions\n signal: AbortSignal\n options: QueryOptions<TQueryFnData, TError, TData, any>\n client: QueryClient\n queryKey: TQueryKey\n state: QueryState<TData, TError>\n}\n\nexport interface QueryBehavior<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> {\n onFetch: (\n context: FetchContext<TQueryFnData, TError, TData, TQueryKey>,\n query: Query,\n ) => void\n}\n\nexport type FetchDirection = 'forward' | 'backward'\n\nexport interface FetchMeta {\n fetchMore?: { direction: FetchDirection }\n}\n\nexport interface FetchOptions<TData = unknown> {\n cancelRefetch?: boolean\n meta?: FetchMeta\n initialPromise?: Promise<TData>\n}\n\ninterface FailedAction<TError> {\n type: 'failed'\n failureCount: number\n error: TError\n}\n\ninterface FetchAction {\n type: 'fetch'\n meta?: FetchMeta\n}\n\ninterface SuccessAction<TData> {\n data: TData | undefined\n type: 'success'\n dataUpdatedAt?: number\n manual?: boolean\n}\n\ninterface ErrorAction<TError> {\n type: 'error'\n error: TError\n}\n\ninterface InvalidateAction {\n type: 'invalidate'\n}\n\ninterface PauseAction {\n type: 'pause'\n}\n\ninterface ContinueAction {\n type: 'continue'\n}\n\ninterface SetStateAction<TData, TError> {\n type: 'setState'\n state: Partial<QueryState<TData, TError>>\n setStateOptions?: SetStateOptions\n}\n\nexport type Action<TData, TError> =\n | ContinueAction\n | ErrorAction<TError>\n | FailedAction<TError>\n | FetchAction\n | InvalidateAction\n | PauseAction\n | SetStateAction<TData, TError>\n | SuccessAction<TData>\n\nexport interface SetStateOptions {\n meta?: any\n}\n\n// CLASS\n\nexport class Query<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n> extends Removable {\n queryKey: TQueryKey\n queryHash: string\n options!: QueryOptions<TQueryFnData, TError, TData, TQueryKey>\n state: QueryState<TData, TError>\n\n #initialState: QueryState<TData, TError>\n #revertState?: QueryState<TData, TError>\n #cache: QueryCache\n #client: QueryClient\n #retryer?: Retryer<TData>\n observers: Array<QueryObserver<any, any, any, any, any>>\n #defaultOptions?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>\n #abortSignalConsumed: boolean\n\n constructor(config: QueryConfig<TQueryFnData, TError, TData, TQueryKey>) {\n super()\n\n this.#abortSignalConsumed = false\n this.#defaultOptions = config.defaultOptions\n this.setOptions(config.options)\n this.observers = []\n this.#client = config.client\n this.#cache = this.#client.getQueryCache()\n this.queryKey = config.queryKey\n this.queryHash = config.queryHash\n this.#initialState = getDefaultState(this.options)\n this.state = config.state ?? this.#initialState\n this.scheduleGc()\n }\n get meta(): QueryMeta | undefined {\n return this.options.meta\n }\n\n get promise(): Promise<TData> | undefined {\n return this.#retryer?.promise\n }\n\n setOptions(\n options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n ): void {\n this.options = { ...this.#defaultOptions, ...options }\n\n this.updateGcTime(this.options.gcTime)\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (this.state && this.state.data === undefined) {\n const defaultState = getDefaultState(this.options)\n if (defaultState.data !== undefined) {\n this.setState(\n successState(defaultState.data, defaultState.dataUpdatedAt),\n )\n this.#initialState = defaultState\n }\n }\n }\n\n protected optionalRemove() {\n if (!this.observers.length && this.state.fetchStatus === 'idle') {\n this.#cache.remove(this)\n }\n }\n\n setData(\n newData: TData,\n options?: SetDataOptions & { manual: boolean },\n ): TData {\n const data = replaceData(this.state.data, newData, this.options)\n\n // Set data and mark it as cached\n this.#dispatch({\n data,\n type: 'success',\n dataUpdatedAt: options?.updatedAt,\n manual: options?.manual,\n })\n\n return data\n }\n\n setState(\n state: Partial<QueryState<TData, TError>>,\n setStateOptions?: SetStateOptions,\n ): void {\n this.#dispatch({ type: 'setState', state, setStateOptions })\n }\n\n cancel(options?: CancelOptions): Promise<void> {\n const promise = this.#retryer?.promise\n this.#retryer?.cancel(options)\n return promise ? promise.then(noop).catch(noop) : Promise.resolve()\n }\n\n destroy(): void {\n super.destroy()\n\n this.cancel({ silent: true })\n }\n\n get resetState(): QueryState<TData, TError> {\n return this.#initialState\n }\n\n reset(): void {\n this.destroy()\n this.setState(this.resetState)\n }\n\n isActive(): boolean {\n return this.observers.some(\n (observer) => resolveEnabled(observer.options.enabled, this) !== false,\n )\n }\n\n isDisabled(): boolean {\n if (this.getObserversCount() > 0) {\n return !this.isActive()\n }\n // if a query has no observers, it should still be considered disabled if it never attempted a fetch\n return this.options.queryFn === skipToken || !this.isFetched()\n }\n\n isFetched() {\n return this.state.dataUpdateCount + this.state.errorUpdateCount > 0\n }\n\n isStatic(): boolean {\n if (this.getObserversCount() > 0) {\n return this.observers.some(\n (observer) =>\n resolveStaleTime(observer.options.staleTime, this) === 'static',\n )\n }\n\n return false\n }\n\n isStale(): boolean {\n // check observers first, their `isStale` has the source of truth\n // calculated with `isStaleByTime` and it takes `enabled` into account\n if (this.getObserversCount() > 0) {\n return this.observers.some(\n (observer) => observer.getCurrentResult().isStale,\n )\n }\n\n return this.state.data === undefined || this.state.isInvalidated\n }\n\n isStaleByTime(staleTime: StaleTime = 0): boolean {\n // no data is always stale\n if (this.state.data === undefined) {\n return true\n }\n // static is never stale\n if (staleTime === 'static') {\n return false\n }\n // if the query is invalidated, it is stale\n if (this.state.isInvalidated) {\n return true\n }\n\n return !timeUntilStale(this.state.dataUpdatedAt, staleTime)\n }\n\n onFocus(): void {\n const observer = this.observers.find((x) => x.shouldFetchOnWindowFocus())\n\n observer?.refetch({ cancelRefetch: false })\n\n // Continue fetch if currently paused\n this.#retryer?.continue()\n }\n\n onOnline(): void {\n const observer = this.observers.find((x) => x.shouldFetchOnReconnect())\n\n observer?.refetch({ cancelRefetch: false })\n\n // Continue fetch if currently paused\n this.#retryer?.continue()\n }\n\n addObserver(observer: QueryObserver<any, any, any, any, any>): void {\n if (!this.observers.includes(observer)) {\n this.observers.push(observer)\n\n // Stop the query from being garbage collected\n this.clearGcTimeout()\n\n this.#cache.notify({ type: 'observerAdded', query: this, observer })\n }\n }\n\n removeObserver(observer: QueryObserver<any, any, any, any, any>): void {\n if (this.observers.includes(observer)) {\n this.observers = this.observers.filter((x) => x !== observer)\n\n if (!this.observers.length) {\n // If the transport layer does not support cancellation\n // we'll let the query continue so the result can be cached\n if (this.#retryer) {\n if (this.#abortSignalConsumed || this.#isInitialPausedFetch()) {\n this.#retryer.cancel({ revert: true })\n } else {\n this.#retryer.cancelRetry()\n }\n }\n\n this.scheduleGc()\n }\n\n this.#cache.notify({ type: 'observerRemoved', query: this, observer })\n }\n }\n\n getObserversCount(): number {\n return this.observers.length\n }\n\n #isInitialPausedFetch(): boolean {\n return (\n this.state.fetchStatus === 'paused' && this.state.status === 'pending'\n )\n }\n\n invalidate(): void {\n if (!this.state.isInvalidated) {\n this.#dispatch({ type: 'invalidate' })\n }\n }\n\n async fetch(\n options?: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n fetchOptions?: FetchOptions<TQueryFnData>,\n ): Promise<TData> {\n if (\n this.state.fetchStatus !== 'idle' &&\n // If the promise in the retryer is already rejected, we have to definitely\n // re-start the fetch; there is a chance that the query is still in a\n // pending state when that happens\n this.#retryer?.status() !== 'rejected'\n ) {\n if (this.state.data !== undefined && fetchOptions?.cancelRefetch) {\n // Silently cancel current fetch if the user wants to cancel refetch\n this.cancel({ silent: true })\n } else if (this.#retryer) {\n // make sure that retries that were potentially cancelled due to unmounts can continue\n this.#retryer.continueRetry()\n // Return current promise if we are already fetching\n return this.#retryer.promise\n }\n }\n\n // Update config if passed, otherwise the config from the last execution is used\n if (options) {\n this.setOptions(options)\n }\n\n // Use the options from the first observer with a query function if no function is found.\n // This can happen when the query is hydrated or created with setQueryData.\n if (!this.options.queryFn) {\n const observer = this.observers.find((x) => x.options.queryFn)\n if (observer) {\n this.setOptions(observer.options)\n }\n }\n\n if (process.env.NODE_ENV !== 'production') {\n if (!Array.isArray(this.options.queryKey)) {\n console.error(\n `As of v4, queryKey needs to be an Array. If you are using a string like 'repoData', please change it to an Array, e.g. ['repoData']`,\n )\n }\n }\n\n const abortController = new AbortController()\n\n // Adds an enumerable signal property to the object that\n // which sets abortSignalConsumed to true when the signal\n // is read.\n const addSignalProperty = (object: unknown) => {\n Object.defineProperty(object, 'signal', {\n enumerable: true,\n get: () => {\n this.#abortSignalConsumed = true\n return abortController.signal\n },\n })\n }\n\n // Create fetch function\n const fetchFn = () => {\n const queryFn = ensureQueryFn(this.options, fetchOptions)\n\n // Create query function context\n const createQueryFnContext = (): QueryFunctionContext<TQueryKey> => {\n const queryFnContext: OmitKeyof<\n QueryFunctionContext<TQueryKey>,\n 'signal'\n > = {\n client: this.#client,\n queryKey: this.queryKey,\n meta: this.meta,\n }\n addSignalProperty(queryFnContext)\n return queryFnContext as QueryFunctionContext<TQueryKey>\n }\n\n const queryFnContext = createQueryFnContext()\n\n this.#abortSignalConsumed = false\n if (this.options.persister) {\n return this.options.persister(\n queryFn,\n queryFnContext,\n this as unknown as Query,\n )\n }\n\n return queryFn(queryFnContext)\n }\n\n // Trigger behavior hook\n const createFetchContext = (): FetchContext<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n > => {\n const context: OmitKeyof<\n FetchContext<TQueryFnData, TError, TData, TQueryKey>,\n 'signal'\n > = {\n fetchOptions,\n options: this.options,\n queryKey: this.queryKey,\n client: this.#client,\n state: this.state,\n fetchFn,\n }\n\n addSignalProperty(context)\n return context as FetchContext<TQueryFnData, TError, TData, TQueryKey>\n }\n\n const context = createFetchContext()\n\n this.options.behavior?.onFetch(context, this as unknown as Query)\n\n // Store state in case the current fetch needs to be reverted\n this.#revertState = this.state\n\n // Set to fetching state if not already in it\n if (\n this.state.fetchStatus === 'idle' ||\n this.state.fetchMeta !== context.fetchOptions?.meta\n ) {\n this.#dispatch({ type: 'fetch', meta: context.fetchOptions?.meta })\n }\n\n // Try to fetch the data\n this.#retryer = createRetryer({\n initialPromise: fetchOptions?.initialPromise as\n | Promise<TData>\n | undefined,\n fn: context.fetchFn as () => Promise<TData>,\n onCancel: (error) => {\n if (error instanceof CancelledError && error.revert) {\n this.setState({\n ...this.#revertState,\n fetchStatus: 'idle' as const,\n })\n }\n abortController.abort()\n },\n onFail: (failureCount, error) => {\n this.#dispatch({ type: 'failed', failureCount, error })\n },\n onPause: () => {\n this.#dispatch({ type: 'pause' })\n },\n onContinue: () => {\n this.#dispatch({ type: 'continue' })\n },\n retry: context.options.retry,\n retryDelay: context.options.retryDelay,\n networkMode: context.options.networkMode,\n canRun: () => true,\n })\n\n try {\n const data = await this.#retryer.start()\n // this is more of a runtime guard\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n if (data === undefined) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(\n `Query data cannot be undefined. Please make sure to return a value other than undefined from your query function. Affected query key: ${this.queryHash}`,\n )\n }\n throw new Error(`${this.queryHash} data is undefined`)\n }\n\n this.setData(data)\n\n // Notify cache callback\n this.#cache.config.onSuccess?.(data, this as Query<any, any, any, any>)\n this.#cache.config.onSettled?.(\n data,\n this.state.error as any,\n this as Query<any, any, any, any>,\n )\n return data\n } catch (error) {\n if (error instanceof CancelledError) {\n if (error.silent) {\n // silent cancellation implies a new fetch is going to be started,\n // so we piggyback onto that promise\n return this.#retryer.promise\n } else if (error.revert) {\n // transform error into reverted state data\n // if the initial fetch was cancelled, we have no data, so we have\n // to get reject with a CancelledError\n if (this.state.data === undefined) {\n throw error\n }\n return this.state.data\n }\n }\n this.#dispatch({\n type: 'error',\n error: error as TError,\n })\n\n // Notify cache callback\n this.#cache.config.onError?.(\n error as any,\n this as Query<any, any, any, any>,\n )\n this.#cache.config.onSettled?.(\n this.state.data,\n error as any,\n this as Query<any, any, any, any>,\n )\n\n throw error // rethrow the error for further handling\n } finally {\n // Schedule query gc after fetching\n this.scheduleGc()\n }\n }\n\n #dispatch(action: Action<TData, TError>): void {\n const reducer = (\n state: QueryState<TData, TError>,\n ): QueryState<TData, TError> => {\n switch (action.type) {\n case 'failed':\n return {\n ...state,\n fetchFailureCount: action.failureCount,\n fetchFailureReason: action.error,\n }\n case 'pause':\n return {\n ...state,\n fetchStatus: 'paused',\n }\n case 'continue':\n return {\n ...state,\n fetchStatus: 'fetching',\n }\n case 'fetch':\n return {\n ...state,\n ...fetchState(state.data, this.options),\n fetchMeta: action.meta ?? null,\n }\n case 'success':\n const newState = {\n ...state,\n ...successState(action.data, action.dataUpdatedAt),\n dataUpdateCount: state.dataUpdateCount + 1,\n ...(!action.manual && {\n fetchStatus: 'idle' as const,\n fetchFailureCount: 0,\n fetchFailureReason: null,\n }),\n }\n // If fetching ends successfully, we don't need revertState as a fallback anymore.\n // For manual updates, capture the state to revert to it in case of a cancellation.\n this.#revertState = action.manual ? newState : undefined\n\n return newState\n case 'error':\n const error = action.error\n return {\n ...state,\n error,\n errorUpdateCount: state.errorUpdateCount + 1,\n errorUpdatedAt: Date.now(),\n fetchFailureCount: state.fetchFailureCount + 1,\n fetchFailureReason: error,\n fetchStatus: 'idle',\n status: 'error',\n // flag existing data as invalidated if we get a background error\n // note that \"no data\" always means stale so we can set unconditionally here\n isInvalidated: true,\n }\n case 'invalidate':\n return {\n ...state,\n isInvalidated: true,\n }\n case 'setState':\n return {\n ...state,\n ...action.state,\n }\n }\n }\n\n this.state = reducer(this.state)\n\n notifyManager.batch(() => {\n this.observers.forEach((observer) => {\n observer.onQueryUpdate()\n })\n\n this.#cache.notify({ query: this, type: 'updated', action })\n })\n }\n}\n\nexport function fetchState<\n TQueryFnData,\n TError,\n TData,\n TQueryKey extends QueryKey,\n>(\n data: TData | undefined,\n options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n) {\n return {\n fetchFailureCount: 0,\n fetchFailureReason: null,\n fetchStatus: canFetch(options.networkMode) ? 'fetching' : 'paused',\n ...(data === undefined &&\n ({\n error: null,\n status: 'pending',\n } as const)),\n } as const\n}\n\nfunction successState<TData>(data: TData | undefined, dataUpdatedAt?: number) {\n return {\n data,\n dataUpdatedAt: dataUpdatedAt ?? Date.now(),\n error: null,\n isInvalidated: false,\n status: 'success' as const,\n }\n}\n\nfunction getDefaultState<\n TQueryFnData,\n TError,\n TData,\n TQueryKey extends QueryKey,\n>(\n options: QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n): QueryState<TData, TError> {\n const data =\n typeof options.initialData === 'function'\n ? (options.initialData as InitialDataFunction<TData>)()\n : options.initialData\n\n const hasData = data !== undefined\n\n const initialDataUpdatedAt = hasData\n ? typeof options.initialDataUpdatedAt === 'function'\n ? options.initialDataUpdatedAt()\n : options.initialDataUpdatedAt\n : 0\n\n return {\n data,\n dataUpdateCount: 0,\n dataUpdatedAt: hasData ? (initialDataUpdatedAt ?? Date.now()) : 0,\n error: null,\n errorUpdateCount: 0,\n errorUpdatedAt: 0,\n fetchFailureCount: 0,\n fetchFailureReason: null,\n fetchMeta: null,\n isInvalidated: false,\n status: hasData ? 'success' : 'pending',\n fetchStatus: 'idle',\n }\n}\n"],"mappings":";AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,qBAAqB;AAC9B,SAAS,gBAAgB,UAAU,qBAAqB;AACxD,SAAS,iBAAiB;AAmJnB,IAAM,QAAN,cAKG,UAAU;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;AAAA,IACf,QAAQ,UAAU,YAAY;AAAA,IAC9B,aAAa;AAAA,EACf;AACF;","names":["queryFnContext","context"]}
|
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/queryCache.ts
|
|
21
|
-
var queryCache_exports = {};
|
|
22
|
-
__export(queryCache_exports, {
|
|
23
|
-
QueryCache: () => QueryCache
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(queryCache_exports);
|
|
26
|
-
var import_utils = require("./utils.cjs");
|
|
27
|
-
var import_query = require("./query.cjs");
|
|
28
|
-
var import_notifyManager = require("./notifyManager.cjs");
|
|
29
|
-
var import_subscribable = require("./subscribable.cjs");
|
|
30
|
-
var QueryCache = class extends import_subscribable.Subscribable {
|
|
31
|
-
constructor(config = {}) {
|
|
32
|
-
super();
|
|
33
|
-
this.config = config;
|
|
34
|
-
this.#queries = /* @__PURE__ */ new Map();
|
|
35
|
-
}
|
|
36
|
-
#queries;
|
|
37
|
-
build(client, options, state) {
|
|
38
|
-
const queryKey = options.queryKey;
|
|
39
|
-
const queryHash = options.queryHash ?? (0, import_utils.hashQueryKeyByOptions)(queryKey, options);
|
|
40
|
-
let query = this.get(queryHash);
|
|
41
|
-
if (!query) {
|
|
42
|
-
query = new import_query.Query({
|
|
43
|
-
client,
|
|
44
|
-
queryKey,
|
|
45
|
-
queryHash,
|
|
46
|
-
options: client.defaultQueryOptions(options),
|
|
47
|
-
state,
|
|
48
|
-
defaultOptions: client.getQueryDefaults(queryKey)
|
|
49
|
-
});
|
|
50
|
-
this.add(query);
|
|
51
|
-
}
|
|
52
|
-
return query;
|
|
53
|
-
}
|
|
54
|
-
add(query) {
|
|
55
|
-
if (!this.#queries.has(query.queryHash)) {
|
|
56
|
-
this.#queries.set(query.queryHash, query);
|
|
57
|
-
this.notify({
|
|
58
|
-
type: "added",
|
|
59
|
-
query
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
remove(query) {
|
|
64
|
-
const queryInMap = this.#queries.get(query.queryHash);
|
|
65
|
-
if (queryInMap) {
|
|
66
|
-
query.destroy();
|
|
67
|
-
if (queryInMap === query) {
|
|
68
|
-
this.#queries.delete(query.queryHash);
|
|
69
|
-
}
|
|
70
|
-
this.notify({ type: "removed", query });
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
clear() {
|
|
74
|
-
import_notifyManager.notifyManager.batch(() => {
|
|
75
|
-
this.getAll().forEach((query) => {
|
|
76
|
-
this.remove(query);
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
get(queryHash) {
|
|
81
|
-
return this.#queries.get(queryHash);
|
|
82
|
-
}
|
|
83
|
-
getAll() {
|
|
84
|
-
return [...this.#queries.values()];
|
|
85
|
-
}
|
|
86
|
-
find(filters) {
|
|
87
|
-
const defaultedFilters = { exact: true, ...filters };
|
|
88
|
-
return this.getAll().find(
|
|
89
|
-
(query) => (0, import_utils.matchQuery)(defaultedFilters, query)
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
findAll(filters = {}) {
|
|
93
|
-
const queries = this.getAll();
|
|
94
|
-
return Object.keys(filters).length > 0 ? queries.filter((query) => (0, import_utils.matchQuery)(filters, query)) : queries;
|
|
95
|
-
}
|
|
96
|
-
notify(event) {
|
|
97
|
-
import_notifyManager.notifyManager.batch(() => {
|
|
98
|
-
this.listeners.forEach((listener) => {
|
|
99
|
-
listener(event);
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
onFocus() {
|
|
104
|
-
import_notifyManager.notifyManager.batch(() => {
|
|
105
|
-
this.getAll().forEach((query) => {
|
|
106
|
-
query.onFocus();
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
}
|
|
110
|
-
onOnline() {
|
|
111
|
-
import_notifyManager.notifyManager.batch(() => {
|
|
112
|
-
this.getAll().forEach((query) => {
|
|
113
|
-
query.onOnline();
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
-
0 && (module.exports = {
|
|
120
|
-
QueryCache
|
|
121
|
-
});
|
|
122
|
-
//# sourceMappingURL=queryCache.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queryCache.ts"],"sourcesContent":["import { hashQueryKeyByOptions, matchQuery } from './utils'\nimport { Query } from './query'\nimport { notifyManager } from './notifyManager'\nimport { Subscribable } from './subscribable'\nimport type { QueryFilters } from './utils'\nimport type { Action, QueryState } from './query'\nimport type {\n DefaultError,\n NotifyEvent,\n QueryKey,\n QueryOptions,\n WithRequired,\n} from './types'\nimport type { QueryClient } from './queryClient'\nimport type { QueryObserver } from './queryObserver'\n\n// TYPES\n\ninterface QueryCacheConfig {\n onError?: (\n error: DefaultError,\n query: Query<unknown, unknown, unknown>,\n ) => void\n onSuccess?: (data: unknown, query: Query<unknown, unknown, unknown>) => void\n onSettled?: (\n data: unknown | undefined,\n error: DefaultError | null,\n query: Query<unknown, unknown, unknown>,\n ) => void\n}\n\ninterface NotifyEventQueryAdded extends NotifyEvent {\n type: 'added'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryRemoved extends NotifyEvent {\n type: 'removed'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryUpdated extends NotifyEvent {\n type: 'updated'\n query: Query<any, any, any, any>\n action: Action<any, any>\n}\n\ninterface NotifyEventQueryObserverAdded extends NotifyEvent {\n type: 'observerAdded'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverRemoved extends NotifyEvent {\n type: 'observerRemoved'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverResultsUpdated extends NotifyEvent {\n type: 'observerResultsUpdated'\n query: Query<any, any, any, any>\n}\n\ninterface NotifyEventQueryObserverOptionsUpdated extends NotifyEvent {\n type: 'observerOptionsUpdated'\n query: Query<any, any, any, any>\n observer: QueryObserver<any, any, any, any, any>\n}\n\nexport type QueryCacheNotifyEvent =\n | NotifyEventQueryAdded\n | NotifyEventQueryRemoved\n | NotifyEventQueryUpdated\n | NotifyEventQueryObserverAdded\n | NotifyEventQueryObserverRemoved\n | NotifyEventQueryObserverResultsUpdated\n | NotifyEventQueryObserverOptionsUpdated\n\ntype QueryCacheListener = (event: QueryCacheNotifyEvent) => void\n\nexport interface QueryStore {\n has: (queryHash: string) => boolean\n set: (queryHash: string, query: Query) => void\n get: (queryHash: string) => Query | undefined\n delete: (queryHash: string) => void\n values: () => IterableIterator<Query>\n}\n\n// CLASS\n\nexport class QueryCache extends Subscribable<QueryCacheListener> {\n #queries: QueryStore\n\n constructor(public config: QueryCacheConfig = {}) {\n super()\n this.#queries = new Map<string, Query>()\n }\n\n build<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n client: QueryClient,\n options: WithRequired<\n QueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n 'queryKey'\n >,\n state?: QueryState<TData, TError>,\n ): Query<TQueryFnData, TError, TData, TQueryKey> {\n const queryKey = options.queryKey\n const queryHash =\n options.queryHash ?? hashQueryKeyByOptions(queryKey, options)\n let query = this.get<TQueryFnData, TError, TData, TQueryKey>(queryHash)\n\n if (!query) {\n query = new Query({\n client,\n queryKey,\n queryHash,\n options: client.defaultQueryOptions(options),\n state,\n defaultOptions: client.getQueryDefaults(queryKey),\n })\n this.add(query)\n }\n\n return query\n }\n\n add(query: Query<any, any, any, any>): void {\n if (!this.#queries.has(query.queryHash)) {\n this.#queries.set(query.queryHash, query)\n\n this.notify({\n type: 'added',\n query,\n })\n }\n }\n\n remove(query: Query<any, any, any, any>): void {\n const queryInMap = this.#queries.get(query.queryHash)\n\n if (queryInMap) {\n query.destroy()\n\n if (queryInMap === query) {\n this.#queries.delete(query.queryHash)\n }\n\n this.notify({ type: 'removed', query })\n }\n }\n\n clear(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n this.remove(query)\n })\n })\n }\n\n get<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n >(\n queryHash: string,\n ): Query<TQueryFnData, TError, TData, TQueryKey> | undefined {\n return this.#queries.get(queryHash) as\n | Query<TQueryFnData, TError, TData, TQueryKey>\n | undefined\n }\n\n getAll(): Array<Query> {\n return [...this.#queries.values()]\n }\n\n find<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData>(\n filters: WithRequired<QueryFilters, 'queryKey'>,\n ): Query<TQueryFnData, TError, TData> | undefined {\n const defaultedFilters = { exact: true, ...filters }\n\n return this.getAll().find((query) =>\n matchQuery(defaultedFilters, query),\n ) as Query<TQueryFnData, TError, TData> | undefined\n }\n\n findAll(filters: QueryFilters<any> = {}): Array<Query> {\n const queries = this.getAll()\n return Object.keys(filters).length > 0\n ? queries.filter((query) => matchQuery(filters, query))\n : queries\n }\n\n notify(event: QueryCacheNotifyEvent): void {\n notifyManager.batch(() => {\n this.listeners.forEach((listener) => {\n listener(event)\n })\n })\n }\n\n onFocus(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onFocus()\n })\n })\n }\n\n onOnline(): void {\n notifyManager.batch(() => {\n this.getAll().forEach((query) => {\n query.onOnline()\n })\n })\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAkD;AAClD,mBAAsB;AACtB,2BAA8B;AAC9B,0BAA6B;AAwFtB,IAAM,aAAN,cAAyB,iCAAiC;AAAA,EAG/D,YAAmB,SAA2B,CAAC,GAAG;AAChD,UAAM;AADW;AAEjB,SAAK,WAAW,oBAAI,IAAmB;AAAA,EACzC;AAAA,EALA;AAAA,EAOA,MAME,QACA,SAIA,OAC+C;AAC/C,UAAM,WAAW,QAAQ;AACzB,UAAM,YACJ,QAAQ,iBAAa,oCAAsB,UAAU,OAAO;AAC9D,QAAI,QAAQ,KAAK,IAA4C,SAAS;AAEtE,QAAI,CAAC,OAAO;AACV,cAAQ,IAAI,mBAAM;AAAA,QAChB;AAAA,QACA;AAAA,QACA;AAAA,QACA,SAAS,OAAO,oBAAoB,OAAO;AAAA,QAC3C;AAAA,QACA,gBAAgB,OAAO,iBAAiB,QAAQ;AAAA,MAClD,CAAC;AACD,WAAK,IAAI,KAAK;AAAA,IAChB;AAEA,WAAO;AAAA,EACT;AAAA,EAEA,IAAI,OAAwC;AAC1C,QAAI,CAAC,KAAK,SAAS,IAAI,MAAM,SAAS,GAAG;AACvC,WAAK,SAAS,IAAI,MAAM,WAAW,KAAK;AAExC,WAAK,OAAO;AAAA,QACV,MAAM;AAAA,QACN;AAAA,MACF,CAAC;AAAA,IACH;AAAA,EACF;AAAA,EAEA,OAAO,OAAwC;AAC7C,UAAM,aAAa,KAAK,SAAS,IAAI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