@tanstack/query-core 5.86.0 → 5.87.1

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.
Files changed (136) hide show
  1. package/build/legacy/{hydration-BpLOP9Dw.d.ts → hydration-BYonJkjc.d.ts} +1 -1
  2. package/build/legacy/{hydration-BC7iBQD-.d.cts → hydration-_hO-y142.d.cts} +1 -1
  3. package/build/legacy/hydration.d.cts +1 -1
  4. package/build/legacy/hydration.d.ts +1 -1
  5. package/build/legacy/index.cjs +14 -12
  6. package/build/legacy/index.cjs.map +1 -1
  7. package/build/legacy/index.d.cts +5 -4
  8. package/build/legacy/index.d.ts +5 -4
  9. package/build/legacy/index.js +26 -23
  10. package/build/legacy/index.js.map +1 -1
  11. package/build/legacy/infiniteQueryBehavior.d.cts +1 -1
  12. package/build/legacy/infiniteQueryBehavior.d.ts +1 -1
  13. package/build/legacy/infiniteQueryObserver.d.cts +1 -1
  14. package/build/legacy/infiniteQueryObserver.d.ts +1 -1
  15. package/build/legacy/mutation.d.cts +1 -1
  16. package/build/legacy/mutation.d.ts +1 -1
  17. package/build/legacy/mutationCache.d.cts +1 -1
  18. package/build/legacy/mutationCache.d.ts +1 -1
  19. package/build/legacy/mutationObserver.d.cts +1 -1
  20. package/build/legacy/mutationObserver.d.ts +1 -1
  21. package/build/legacy/notifyManager.cjs +2 -1
  22. package/build/legacy/notifyManager.cjs.map +1 -1
  23. package/build/legacy/notifyManager.js +2 -1
  24. package/build/legacy/notifyManager.js.map +1 -1
  25. package/build/legacy/queriesObserver.d.cts +1 -1
  26. package/build/legacy/queriesObserver.d.ts +1 -1
  27. package/build/legacy/query.cjs +10 -0
  28. package/build/legacy/query.cjs.map +1 -1
  29. package/build/legacy/query.d.cts +1 -1
  30. package/build/legacy/query.d.ts +1 -1
  31. package/build/legacy/query.js +10 -0
  32. package/build/legacy/query.js.map +1 -1
  33. package/build/legacy/queryCache.d.cts +1 -1
  34. package/build/legacy/queryCache.d.ts +1 -1
  35. package/build/legacy/queryClient.d.cts +1 -1
  36. package/build/legacy/queryClient.d.ts +1 -1
  37. package/build/legacy/queryObserver.cjs +5 -4
  38. package/build/legacy/queryObserver.cjs.map +1 -1
  39. package/build/legacy/queryObserver.d.cts +1 -1
  40. package/build/legacy/queryObserver.d.ts +1 -1
  41. package/build/legacy/queryObserver.js +5 -4
  42. package/build/legacy/queryObserver.js.map +1 -1
  43. package/build/legacy/removable.cjs +3 -2
  44. package/build/legacy/removable.cjs.map +1 -1
  45. package/build/legacy/removable.js +3 -2
  46. package/build/legacy/removable.js.map +1 -1
  47. package/build/legacy/retryer.d.cts +1 -1
  48. package/build/legacy/retryer.d.ts +1 -1
  49. package/build/legacy/streamedQuery.d.cts +1 -1
  50. package/build/legacy/streamedQuery.d.ts +1 -1
  51. package/build/legacy/timeoutManager.cjs +110 -0
  52. package/build/legacy/timeoutManager.cjs.map +1 -0
  53. package/build/legacy/timeoutManager.d.cts +58 -0
  54. package/build/legacy/timeoutManager.d.ts +58 -0
  55. package/build/legacy/timeoutManager.js +81 -0
  56. package/build/legacy/timeoutManager.js.map +1 -0
  57. package/build/legacy/types.d.cts +1 -1
  58. package/build/legacy/types.d.ts +1 -1
  59. package/build/legacy/utils.cjs +2 -1
  60. package/build/legacy/utils.cjs.map +1 -1
  61. package/build/legacy/utils.d.cts +1 -1
  62. package/build/legacy/utils.d.ts +1 -1
  63. package/build/legacy/utils.js +2 -1
  64. package/build/legacy/utils.js.map +1 -1
  65. package/build/modern/{hydration-BpLOP9Dw.d.ts → hydration-BYonJkjc.d.ts} +1 -1
  66. package/build/modern/{hydration-BC7iBQD-.d.cts → hydration-_hO-y142.d.cts} +1 -1
  67. package/build/modern/hydration.d.cts +1 -1
  68. package/build/modern/hydration.d.ts +1 -1
  69. package/build/modern/index.cjs +14 -12
  70. package/build/modern/index.cjs.map +1 -1
  71. package/build/modern/index.d.cts +5 -4
  72. package/build/modern/index.d.ts +5 -4
  73. package/build/modern/index.js +26 -23
  74. package/build/modern/index.js.map +1 -1
  75. package/build/modern/infiniteQueryBehavior.d.cts +1 -1
  76. package/build/modern/infiniteQueryBehavior.d.ts +1 -1
  77. package/build/modern/infiniteQueryObserver.d.cts +1 -1
  78. package/build/modern/infiniteQueryObserver.d.ts +1 -1
  79. package/build/modern/mutation.d.cts +1 -1
  80. package/build/modern/mutation.d.ts +1 -1
  81. package/build/modern/mutationCache.d.cts +1 -1
  82. package/build/modern/mutationCache.d.ts +1 -1
  83. package/build/modern/mutationObserver.d.cts +1 -1
  84. package/build/modern/mutationObserver.d.ts +1 -1
  85. package/build/modern/notifyManager.cjs +2 -1
  86. package/build/modern/notifyManager.cjs.map +1 -1
  87. package/build/modern/notifyManager.js +2 -1
  88. package/build/modern/notifyManager.js.map +1 -1
  89. package/build/modern/queriesObserver.d.cts +1 -1
  90. package/build/modern/queriesObserver.d.ts +1 -1
  91. package/build/modern/query.cjs +10 -0
  92. package/build/modern/query.cjs.map +1 -1
  93. package/build/modern/query.d.cts +1 -1
  94. package/build/modern/query.d.ts +1 -1
  95. package/build/modern/query.js +10 -0
  96. package/build/modern/query.js.map +1 -1
  97. package/build/modern/queryCache.d.cts +1 -1
  98. package/build/modern/queryCache.d.ts +1 -1
  99. package/build/modern/queryClient.d.cts +1 -1
  100. package/build/modern/queryClient.d.ts +1 -1
  101. package/build/modern/queryObserver.cjs +5 -4
  102. package/build/modern/queryObserver.cjs.map +1 -1
  103. package/build/modern/queryObserver.d.cts +1 -1
  104. package/build/modern/queryObserver.d.ts +1 -1
  105. package/build/modern/queryObserver.js +5 -4
  106. package/build/modern/queryObserver.js.map +1 -1
  107. package/build/modern/removable.cjs +3 -2
  108. package/build/modern/removable.cjs.map +1 -1
  109. package/build/modern/removable.js +3 -2
  110. package/build/modern/removable.js.map +1 -1
  111. package/build/modern/retryer.d.cts +1 -1
  112. package/build/modern/retryer.d.ts +1 -1
  113. package/build/modern/streamedQuery.d.cts +1 -1
  114. package/build/modern/streamedQuery.d.ts +1 -1
  115. package/build/modern/timeoutManager.cjs +98 -0
  116. package/build/modern/timeoutManager.cjs.map +1 -0
  117. package/build/modern/timeoutManager.d.cts +58 -0
  118. package/build/modern/timeoutManager.d.ts +58 -0
  119. package/build/modern/timeoutManager.js +70 -0
  120. package/build/modern/timeoutManager.js.map +1 -0
  121. package/build/modern/types.d.cts +1 -1
  122. package/build/modern/types.d.ts +1 -1
  123. package/build/modern/utils.cjs +2 -1
  124. package/build/modern/utils.cjs.map +1 -1
  125. package/build/modern/utils.d.cts +1 -1
  126. package/build/modern/utils.d.ts +1 -1
  127. package/build/modern/utils.js +2 -1
  128. package/build/modern/utils.js.map +1 -1
  129. package/package.json +1 -1
  130. package/src/index.ts +32 -27
  131. package/src/notifyManager.ts +3 -1
  132. package/src/query.ts +12 -0
  133. package/src/queryObserver.ts +8 -6
  134. package/src/removable.ts +5 -3
  135. package/src/timeoutManager.ts +135 -0
  136. package/src/utils.ts +2 -1
package/src/index.ts CHANGED
@@ -1,50 +1,55 @@
1
1
  /* istanbul ignore file */
2
2
 
3
- export { CancelledError } from './retryer'
4
- export { QueryCache } from './queryCache'
5
- export type { QueryCacheNotifyEvent } from './queryCache'
6
- export { QueryClient } from './queryClient'
7
- export { QueryObserver } from './queryObserver'
8
- export { QueriesObserver } from './queriesObserver'
3
+ export { focusManager } from './focusManager'
4
+ export {
5
+ defaultShouldDehydrateMutation,
6
+ defaultShouldDehydrateQuery,
7
+ dehydrate,
8
+ hydrate,
9
+ } from './hydration'
9
10
  export { InfiniteQueryObserver } from './infiniteQueryObserver'
10
11
  export { MutationCache } from './mutationCache'
11
12
  export type { MutationCacheNotifyEvent } from './mutationCache'
12
13
  export { MutationObserver } from './mutationObserver'
13
- export { notifyManager, defaultScheduler } from './notifyManager'
14
- export { focusManager } from './focusManager'
14
+ export { defaultScheduler, notifyManager } from './notifyManager'
15
15
  export { onlineManager } from './onlineManager'
16
+ export { QueriesObserver } from './queriesObserver'
17
+ export { QueryCache } from './queryCache'
18
+ export type { QueryCacheNotifyEvent } from './queryCache'
19
+ export { QueryClient } from './queryClient'
20
+ export { QueryObserver } from './queryObserver'
21
+ export { CancelledError, isCancelledError } from './retryer'
22
+ export {
23
+ timeoutManager,
24
+ type ManagedTimerId,
25
+ type TimeoutCallback,
26
+ type TimeoutProvider,
27
+ } from './timeoutManager'
16
28
  export {
17
29
  hashKey,
18
- partialMatchKey,
19
- replaceEqualDeep,
20
30
  isServer,
21
- matchQuery,
22
- matchMutation,
23
31
  keepPreviousData,
24
- skipToken,
32
+ matchMutation,
33
+ matchQuery,
25
34
  noop,
35
+ partialMatchKey,
36
+ replaceEqualDeep,
26
37
  shouldThrowError,
38
+ skipToken,
27
39
  } from './utils'
28
- export type { MutationFilters, QueryFilters, Updater, SkipToken } from './utils'
29
- export { isCancelledError } from './retryer'
30
- export {
31
- dehydrate,
32
- hydrate,
33
- defaultShouldDehydrateQuery,
34
- defaultShouldDehydrateMutation,
35
- } from './hydration'
40
+ export type { MutationFilters, QueryFilters, SkipToken, Updater } from './utils'
36
41
 
37
42
  export { streamedQuery as experimental_streamedQuery } from './streamedQuery'
38
43
 
39
44
  // Types
40
- export * from './types'
41
- export type { QueryState } from './query'
42
- export { Query } from './query'
43
- export type { MutationState } from './mutation'
44
- export { Mutation } from './mutation'
45
45
  export type {
46
- DehydrateOptions,
47
46
  DehydratedState,
47
+ DehydrateOptions,
48
48
  HydrateOptions,
49
49
  } from './hydration'
50
+ export { Mutation } from './mutation'
51
+ export type { MutationState } from './mutation'
50
52
  export type { QueriesObserverOptions } from './queriesObserver'
53
+ export { Query } from './query'
54
+ export type { QueryState } from './query'
55
+ export * from './types'
@@ -1,5 +1,7 @@
1
1
  // TYPES
2
2
 
3
+ import { systemSetTimeoutZero } from './timeoutManager'
4
+
3
5
  type NotifyCallback = () => void
4
6
 
5
7
  type NotifyFunction = (callback: () => void) => void
@@ -10,7 +12,7 @@ type BatchCallsCallback<T extends Array<unknown>> = (...args: T) => void
10
12
 
11
13
  type ScheduleFunction = (callback: () => void) => void
12
14
 
13
- export const defaultScheduler: ScheduleFunction = (cb) => setTimeout(cb, 0)
15
+ export const defaultScheduler: ScheduleFunction = systemSetTimeoutZero
14
16
 
15
17
  export function createNotifyManager() {
16
18
  let queue: Array<NotifyCallback> = []
package/src/query.ts CHANGED
@@ -205,6 +205,18 @@ export class Query<
205
205
  this.options = { ...this.#defaultOptions, ...options }
206
206
 
207
207
  this.updateGcTime(this.options.gcTime)
208
+
209
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
210
+ if (this.state && this.state.data === undefined) {
211
+ const defaultState = getDefaultState(this.options)
212
+ if (defaultState.data !== undefined) {
213
+ this.setData(defaultState.data, {
214
+ updatedAt: defaultState.dataUpdatedAt,
215
+ manual: true,
216
+ })
217
+ this.#initialState = defaultState
218
+ }
219
+ }
208
220
  }
209
221
 
210
222
  protected optionalRemove() {
@@ -13,6 +13,8 @@ import {
13
13
  shallowEqualObjects,
14
14
  timeUntilStale,
15
15
  } from './utils'
16
+ import { timeoutManager } from './timeoutManager'
17
+ import type { ManagedTimerId } from './timeoutManager'
16
18
  import type { FetchOptions, Query, QueryState } from './query'
17
19
  import type { QueryClient } from './queryClient'
18
20
  import type { PendingThenable, Thenable } from './thenable'
@@ -62,8 +64,8 @@ export class QueryObserver<
62
64
  // This property keeps track of the last query with defined data.
63
65
  // It will be used to pass the previous data and query to the placeholder function between renders.
64
66
  #lastQueryWithDefinedData?: Query<TQueryFnData, TError, TQueryData, TQueryKey>
65
- #staleTimeoutId?: ReturnType<typeof setTimeout>
66
- #refetchIntervalId?: ReturnType<typeof setInterval>
67
+ #staleTimeoutId?: ManagedTimerId
68
+ #refetchIntervalId?: ManagedTimerId
67
69
  #currentRefetchInterval?: number | false
68
70
  #trackedProps = new Set<keyof QueryObserverResult>()
69
71
 
@@ -365,7 +367,7 @@ export class QueryObserver<
365
367
  // To mitigate this issue we always add 1 ms to the timeout.
366
368
  const timeout = time + 1
367
369
 
368
- this.#staleTimeoutId = setTimeout(() => {
370
+ this.#staleTimeoutId = timeoutManager.setTimeout(() => {
369
371
  if (!this.#currentResult.isStale) {
370
372
  this.updateResult()
371
373
  }
@@ -394,7 +396,7 @@ export class QueryObserver<
394
396
  return
395
397
  }
396
398
 
397
- this.#refetchIntervalId = setInterval(() => {
399
+ this.#refetchIntervalId = timeoutManager.setInterval(() => {
398
400
  if (
399
401
  this.options.refetchIntervalInBackground ||
400
402
  focusManager.isFocused()
@@ -411,14 +413,14 @@ export class QueryObserver<
411
413
 
412
414
  #clearStaleTimeout(): void {
413
415
  if (this.#staleTimeoutId) {
414
- clearTimeout(this.#staleTimeoutId)
416
+ timeoutManager.clearTimeout(this.#staleTimeoutId)
415
417
  this.#staleTimeoutId = undefined
416
418
  }
417
419
  }
418
420
 
419
421
  #clearRefetchInterval(): void {
420
422
  if (this.#refetchIntervalId) {
421
- clearInterval(this.#refetchIntervalId)
423
+ timeoutManager.clearInterval(this.#refetchIntervalId)
422
424
  this.#refetchIntervalId = undefined
423
425
  }
424
426
  }
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?: ReturnType<typeof setTimeout>
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