@tanstack/query-core 5.86.0 → 5.87.0
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/build/legacy/{hydration-BpLOP9Dw.d.ts → hydration-BYonJkjc.d.ts} +1 -1
- package/build/legacy/{hydration-BC7iBQD-.d.cts → hydration-_hO-y142.d.cts} +1 -1
- package/build/legacy/hydration.d.cts +1 -1
- package/build/legacy/hydration.d.ts +1 -1
- package/build/legacy/index.cjs +14 -12
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +5 -4
- package/build/legacy/index.d.ts +5 -4
- package/build/legacy/index.js +26 -23
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
- package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
- package/build/legacy/infiniteQueryObserver.d.cts +1 -1
- package/build/legacy/infiniteQueryObserver.d.ts +1 -1
- package/build/legacy/mutation.d.cts +1 -1
- package/build/legacy/mutation.d.ts +1 -1
- package/build/legacy/mutationCache.d.cts +1 -1
- package/build/legacy/mutationCache.d.ts +1 -1
- package/build/legacy/mutationObserver.d.cts +1 -1
- package/build/legacy/mutationObserver.d.ts +1 -1
- package/build/legacy/notifyManager.cjs +2 -1
- package/build/legacy/notifyManager.cjs.map +1 -1
- package/build/legacy/notifyManager.js +2 -1
- package/build/legacy/notifyManager.js.map +1 -1
- package/build/legacy/queriesObserver.d.cts +1 -1
- package/build/legacy/queriesObserver.d.ts +1 -1
- package/build/legacy/query.d.cts +1 -1
- package/build/legacy/query.d.ts +1 -1
- package/build/legacy/queryCache.d.cts +1 -1
- package/build/legacy/queryCache.d.ts +1 -1
- package/build/legacy/queryClient.d.cts +1 -1
- package/build/legacy/queryClient.d.ts +1 -1
- package/build/legacy/queryObserver.cjs +5 -4
- package/build/legacy/queryObserver.cjs.map +1 -1
- package/build/legacy/queryObserver.d.cts +1 -1
- package/build/legacy/queryObserver.d.ts +1 -1
- package/build/legacy/queryObserver.js +5 -4
- package/build/legacy/queryObserver.js.map +1 -1
- package/build/legacy/removable.cjs +3 -2
- package/build/legacy/removable.cjs.map +1 -1
- package/build/legacy/removable.js +3 -2
- package/build/legacy/removable.js.map +1 -1
- package/build/legacy/retryer.d.cts +1 -1
- package/build/legacy/retryer.d.ts +1 -1
- package/build/legacy/streamedQuery.d.cts +1 -1
- package/build/legacy/streamedQuery.d.ts +1 -1
- package/build/legacy/timeoutManager.cjs +110 -0
- package/build/legacy/timeoutManager.cjs.map +1 -0
- package/build/legacy/timeoutManager.d.cts +58 -0
- package/build/legacy/timeoutManager.d.ts +58 -0
- package/build/legacy/timeoutManager.js +81 -0
- package/build/legacy/timeoutManager.js.map +1 -0
- package/build/legacy/types.d.cts +1 -1
- package/build/legacy/types.d.ts +1 -1
- package/build/legacy/utils.cjs +2 -1
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts +1 -1
- package/build/legacy/utils.d.ts +1 -1
- package/build/legacy/utils.js +2 -1
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/{hydration-BpLOP9Dw.d.ts → hydration-BYonJkjc.d.ts} +1 -1
- package/build/modern/{hydration-BC7iBQD-.d.cts → hydration-_hO-y142.d.cts} +1 -1
- package/build/modern/hydration.d.cts +1 -1
- package/build/modern/hydration.d.ts +1 -1
- package/build/modern/index.cjs +14 -12
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +5 -4
- package/build/modern/index.d.ts +5 -4
- package/build/modern/index.js +26 -23
- package/build/modern/index.js.map +1 -1
- package/build/modern/infiniteQueryBehavior.d.cts +1 -1
- package/build/modern/infiniteQueryBehavior.d.ts +1 -1
- package/build/modern/infiniteQueryObserver.d.cts +1 -1
- package/build/modern/infiniteQueryObserver.d.ts +1 -1
- package/build/modern/mutation.d.cts +1 -1
- package/build/modern/mutation.d.ts +1 -1
- package/build/modern/mutationCache.d.cts +1 -1
- package/build/modern/mutationCache.d.ts +1 -1
- package/build/modern/mutationObserver.d.cts +1 -1
- package/build/modern/mutationObserver.d.ts +1 -1
- package/build/modern/notifyManager.cjs +2 -1
- package/build/modern/notifyManager.cjs.map +1 -1
- package/build/modern/notifyManager.js +2 -1
- package/build/modern/notifyManager.js.map +1 -1
- package/build/modern/queriesObserver.d.cts +1 -1
- package/build/modern/queriesObserver.d.ts +1 -1
- package/build/modern/query.d.cts +1 -1
- package/build/modern/query.d.ts +1 -1
- package/build/modern/queryCache.d.cts +1 -1
- package/build/modern/queryCache.d.ts +1 -1
- package/build/modern/queryClient.d.cts +1 -1
- package/build/modern/queryClient.d.ts +1 -1
- package/build/modern/queryObserver.cjs +5 -4
- package/build/modern/queryObserver.cjs.map +1 -1
- package/build/modern/queryObserver.d.cts +1 -1
- package/build/modern/queryObserver.d.ts +1 -1
- package/build/modern/queryObserver.js +5 -4
- package/build/modern/queryObserver.js.map +1 -1
- package/build/modern/removable.cjs +3 -2
- package/build/modern/removable.cjs.map +1 -1
- package/build/modern/removable.js +3 -2
- package/build/modern/removable.js.map +1 -1
- package/build/modern/retryer.d.cts +1 -1
- package/build/modern/retryer.d.ts +1 -1
- package/build/modern/streamedQuery.d.cts +1 -1
- package/build/modern/streamedQuery.d.ts +1 -1
- package/build/modern/timeoutManager.cjs +98 -0
- package/build/modern/timeoutManager.cjs.map +1 -0
- package/build/modern/timeoutManager.d.cts +58 -0
- package/build/modern/timeoutManager.d.ts +58 -0
- package/build/modern/timeoutManager.js +70 -0
- package/build/modern/timeoutManager.js.map +1 -0
- package/build/modern/types.d.cts +1 -1
- package/build/modern/types.d.ts +1 -1
- package/build/modern/utils.cjs +2 -1
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts +1 -1
- package/build/modern/utils.d.ts +1 -1
- package/build/modern/utils.js +2 -1
- package/build/modern/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +32 -27
- package/src/notifyManager.ts +3 -1
- package/src/queryObserver.ts +8 -6
- package/src/removable.ts +5 -3
- package/src/timeoutManager.ts +135 -0
- package/src/utils.ts +2 -1
package/src/removable.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
import { timeoutManager } from './timeoutManager'
|
|
1
2
|
import { isServer, isValidTimeout } from './utils'
|
|
3
|
+
import type { ManagedTimerId } from './timeoutManager'
|
|
2
4
|
|
|
3
5
|
export abstract class Removable {
|
|
4
6
|
gcTime!: number
|
|
5
|
-
#gcTimeout?:
|
|
7
|
+
#gcTimeout?: ManagedTimerId
|
|
6
8
|
|
|
7
9
|
destroy(): void {
|
|
8
10
|
this.clearGcTimeout()
|
|
@@ -12,7 +14,7 @@ export abstract class Removable {
|
|
|
12
14
|
this.clearGcTimeout()
|
|
13
15
|
|
|
14
16
|
if (isValidTimeout(this.gcTime)) {
|
|
15
|
-
this.#gcTimeout = setTimeout(() => {
|
|
17
|
+
this.#gcTimeout = timeoutManager.setTimeout(() => {
|
|
16
18
|
this.optionalRemove()
|
|
17
19
|
}, this.gcTime)
|
|
18
20
|
}
|
|
@@ -28,7 +30,7 @@ export abstract class Removable {
|
|
|
28
30
|
|
|
29
31
|
protected clearGcTimeout() {
|
|
30
32
|
if (this.#gcTimeout) {
|
|
31
|
-
clearTimeout(this.#gcTimeout)
|
|
33
|
+
timeoutManager.clearTimeout(this.#gcTimeout)
|
|
32
34
|
this.#gcTimeout = undefined
|
|
33
35
|
}
|
|
34
36
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* {@link TimeoutManager} does not support passing arguments to the callback.
|
|
3
|
+
*
|
|
4
|
+
* `(_: void)` is the argument type inferred by TypeScript's default typings for
|
|
5
|
+
* `setTimeout(cb, number)`.
|
|
6
|
+
* If we don't accept a single void argument, then
|
|
7
|
+
* `new Promise(resolve => timeoutManager.setTimeout(resolve, N))` is a type error.
|
|
8
|
+
*/
|
|
9
|
+
export type TimeoutCallback = (_: void) => void
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Wrapping `setTimeout` is awkward from a typing perspective because platform
|
|
13
|
+
* typings may extend the return type of `setTimeout`. For example, NodeJS
|
|
14
|
+
* typings add `NodeJS.Timeout`; but a non-default `timeoutManager` may not be
|
|
15
|
+
* able to return such a type.
|
|
16
|
+
*/
|
|
17
|
+
export type ManagedTimerId = number | { [Symbol.toPrimitive]: () => number }
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Backend for timer functions.
|
|
21
|
+
*/
|
|
22
|
+
export type TimeoutProvider<TTimerId extends ManagedTimerId = ManagedTimerId> =
|
|
23
|
+
{
|
|
24
|
+
readonly setTimeout: (callback: TimeoutCallback, delay: number) => TTimerId
|
|
25
|
+
readonly clearTimeout: (timeoutId: TTimerId | undefined) => void
|
|
26
|
+
|
|
27
|
+
readonly setInterval: (callback: TimeoutCallback, delay: number) => TTimerId
|
|
28
|
+
readonly clearInterval: (intervalId: TTimerId | undefined) => void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const defaultTimeoutProvider: TimeoutProvider<
|
|
32
|
+
ReturnType<typeof setTimeout>
|
|
33
|
+
> = {
|
|
34
|
+
// We need the wrapper function syntax below instead of direct references to
|
|
35
|
+
// global setTimeout etc.
|
|
36
|
+
//
|
|
37
|
+
// BAD: `setTimeout: setTimeout`
|
|
38
|
+
// GOOD: `setTimeout: (cb, delay) => setTimeout(cb, delay)`
|
|
39
|
+
//
|
|
40
|
+
// If we use direct references here, then anything that wants to spy on or
|
|
41
|
+
// replace the global setTimeout (like tests) won't work since we'll already
|
|
42
|
+
// have a hard reference to the original implementation at the time when this
|
|
43
|
+
// file was imported.
|
|
44
|
+
setTimeout: (callback, delay) => setTimeout(callback, delay),
|
|
45
|
+
clearTimeout: (timeoutId) => clearTimeout(timeoutId),
|
|
46
|
+
|
|
47
|
+
setInterval: (callback, delay) => setInterval(callback, delay),
|
|
48
|
+
clearInterval: (intervalId) => clearInterval(intervalId),
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Allows customization of how timeouts are created.
|
|
53
|
+
*
|
|
54
|
+
* @tanstack/query-core makes liberal use of timeouts to implement `staleTime`
|
|
55
|
+
* and `gcTime`. The default TimeoutManager provider uses the platform's global
|
|
56
|
+
* `setTimeout` implementation, which is known to have scalability issues with
|
|
57
|
+
* thousands of timeouts on the event loop.
|
|
58
|
+
*
|
|
59
|
+
* If you hit this limitation, consider providing a custom TimeoutProvider that
|
|
60
|
+
* coalesces timeouts.
|
|
61
|
+
*/
|
|
62
|
+
export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
|
|
63
|
+
// We cannot have TimeoutManager<T> as we must instantiate it with a concrete
|
|
64
|
+
// type at app boot; and if we leave that type, then any new timer provider
|
|
65
|
+
// would need to support ReturnType<typeof setTimeout>, which is infeasible.
|
|
66
|
+
//
|
|
67
|
+
// We settle for type safety for the TimeoutProvider type, and accept that
|
|
68
|
+
// this class is unsafe internally to allow for extension.
|
|
69
|
+
#provider: TimeoutProvider<any> = defaultTimeoutProvider
|
|
70
|
+
#providerCalled = false
|
|
71
|
+
|
|
72
|
+
setTimeoutProvider<TTimerId extends ManagedTimerId>(
|
|
73
|
+
provider: TimeoutProvider<TTimerId>,
|
|
74
|
+
): void {
|
|
75
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
76
|
+
if (this.#providerCalled && provider !== this.#provider) {
|
|
77
|
+
// After changing providers, `clearTimeout` will not work as expected for
|
|
78
|
+
// timeouts from the previous provider.
|
|
79
|
+
//
|
|
80
|
+
// Since they may allocate the same timeout ID, clearTimeout may cancel an
|
|
81
|
+
// arbitrary different timeout, or unexpected no-op.
|
|
82
|
+
//
|
|
83
|
+
// We could protect against this by mixing the timeout ID bits
|
|
84
|
+
// deterministically with some per-provider bits.
|
|
85
|
+
//
|
|
86
|
+
// We could internally queue `setTimeout` calls to `TimeoutManager` until
|
|
87
|
+
// some API call to set the initial provider.
|
|
88
|
+
console.error(
|
|
89
|
+
`[timeoutManager]: Switching provider after calls to previous provider might result in unexpected behavior.`,
|
|
90
|
+
{ previous: this.#provider, provider },
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
this.#provider = provider
|
|
96
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
97
|
+
this.#providerCalled = false
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId {
|
|
102
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
103
|
+
this.#providerCalled = true
|
|
104
|
+
}
|
|
105
|
+
return this.#provider.setTimeout(callback, delay)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
clearTimeout(timeoutId: ManagedTimerId | undefined): void {
|
|
109
|
+
this.#provider.clearTimeout(timeoutId)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId {
|
|
113
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
114
|
+
this.#providerCalled = true
|
|
115
|
+
}
|
|
116
|
+
return this.#provider.setInterval(callback, delay)
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
clearInterval(intervalId: ManagedTimerId | undefined): void {
|
|
120
|
+
this.#provider.clearInterval(intervalId)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export const timeoutManager = new TimeoutManager()
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* In many cases code wants to delay to the next event loop tick; this is not
|
|
128
|
+
* mediated by {@link timeoutManager}.
|
|
129
|
+
*
|
|
130
|
+
* This function is provided to make auditing the `tanstack/query-core` for
|
|
131
|
+
* incorrect use of system `setTimeout` easier.
|
|
132
|
+
*/
|
|
133
|
+
export function systemSetTimeoutZero(callback: TimeoutCallback): void {
|
|
134
|
+
setTimeout(callback, 0)
|
|
135
|
+
}
|
package/src/utils.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { timeoutManager } from './timeoutManager'
|
|
1
2
|
import type {
|
|
2
3
|
DefaultError,
|
|
3
4
|
Enabled,
|
|
@@ -361,7 +362,7 @@ function hasObjectPrototype(o: any): boolean {
|
|
|
361
362
|
|
|
362
363
|
export function sleep(timeout: number): Promise<void> {
|
|
363
364
|
return new Promise((resolve) => {
|
|
364
|
-
setTimeout(resolve, timeout)
|
|
365
|
+
timeoutManager.setTimeout(resolve, timeout)
|
|
365
366
|
})
|
|
366
367
|
}
|
|
367
368
|
|