Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

TimeoutManager.md

248 lines6.0 KB

1---
2id: TimeoutManager
3title: TimeoutManager
4redirect_from:
5 - reference/timeoutManager
6---
7​
8Defined in: [packages/query-core/src/timeoutManager.ts:70](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L70)
9​
10Allows customization of how timeouts are created.
11​
12@tanstack/query-core makes liberal use of timeouts to implement `staleTime`
13and `gcTime`. The default TimeoutManager provider uses the platform's global
14`setTimeout` implementation, which is known to have scalability issues with
15thousands of timeouts on the event loop.
16​
17If you hit this limitation, consider providing a custom TimeoutProvider that
18coalesces timeouts.
19​
20## Implements
21​
22- `Omit`\<[`TimeoutProvider`](../type-aliases/TimeoutProvider.md), `"name"`\>
23​
24## Methods
25​
26### clearInterval()
27​
28```ts
29clearInterval(intervalId: ManagedTimerId | undefined): void;
30```
31​
32Defined in: [packages/query-core/src/timeoutManager.ts:224](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L224)
33​
34`clearInterval` can be used to cancel an interval, like the global
35`clearInterval` function. It should be called with an interval ID
36returned by `setInterval`.
37​
38#### Parameters
39​
40##### intervalId
41​
42[`ManagedTimerId`](../type-aliases/ManagedTimerId.md) | `undefined`
43​
44#### Returns
45​
46`void`
47​
48#### Example
49​
50```ts
51import { timeoutManager } from '@tanstack/query-core'
52​
53const intervalId = timeoutManager.setInterval(
54 () => console.log('ran at:', new Date()),
55 1000,
57​
58timeoutManager.clearInterval(intervalId)
59```
60​
61#### Implementation of
62​
63[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`clearInterval`](../type-aliases/TimeoutProvider.md#clearinterval)
64​
65***
66​
67### clearTimeout()
68​
69```ts
70clearTimeout(timeoutId: ManagedTimerId | undefined): void;
71```
72​
73Defined in: [packages/query-core/src/timeoutManager.ts:179](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L179)
74​
75`clearTimeout` cancels a timeout callback scheduled with `setTimeout`,
76like the global `clearTimeout` function. It should be called with a
77timer ID returned by `setTimeout`.
78​
79#### Parameters
80​
81##### timeoutId
82​
83[`ManagedTimerId`](../type-aliases/ManagedTimerId.md) | `undefined`
84​
85#### Returns
86​
87`void`
88​
89#### Example
90​
91```ts
92import { timeoutManager } from '@tanstack/query-core'
93​
94const timeoutId = timeoutManager.setTimeout(
95 () => console.log('ran at:', new Date()),
96 1000,
98​
99timeoutManager.clearTimeout(timeoutId)
100```
101​
102#### Implementation of
103​
104[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`clearTimeout`](../type-aliases/TimeoutProvider.md#cleartimeout)
105​
106***
107​
108### setInterval()
109​
110```ts
111setInterval(callback: TimeoutCallback, delay: number): ManagedTimerId;
112```
113​
114Defined in: [packages/query-core/src/timeoutManager.ts:200](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L200)
115​
116`setInterval` schedules a callback to be called approximately every
117`delay` milliseconds, like the global `setInterval` function.
118​
119Like `setTimeout`, it returns a timer ID, which may be a number or an
120object that can be coerced to a number via `Symbol.toPrimitive`.
121​
122#### Parameters
123​
124##### callback
125​
126[`TimeoutCallback`](../type-aliases/TimeoutCallback.md)
127​
128##### delay
129​
130`number`
131​
132#### Returns
133​
134[`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
135​
136#### Example
137​
138```ts
139import { timeoutManager } from '@tanstack/query-core'
140​
141const intervalId = timeoutManager.setInterval(
142 () => console.log('ran at:', new Date()),
143 1000,
145```
146​
147#### Implementation of
148​
149[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`setInterval`](../type-aliases/TimeoutProvider.md#setinterval)
150​
151***
152​
153### setTimeout()
154​
155```ts
156setTimeout(callback: TimeoutCallback, delay: number): ManagedTimerId;
157```
158​
159Defined in: [packages/query-core/src/timeoutManager.ts:155](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L155)
160​
161`setTimeout` schedules a callback to run after approximately `delay`
162milliseconds, like the global `setTimeout` function. The callback can be
163canceled with `clearTimeout`.
164​
165It returns a timer ID, which may be a number or an object that can be
166coerced to a number via `Symbol.toPrimitive`.
167​
168#### Parameters
169​
170##### callback
171​
172[`TimeoutCallback`](../type-aliases/TimeoutCallback.md)
173​
174##### delay
175​
176`number`
177​
178#### Returns
179​
180[`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
181​
182#### Example
183​
184```ts
185import { timeoutManager } from '@tanstack/query-core'
186​
187const timeoutId = timeoutManager.setTimeout(
188 () => console.log('ran at:', new Date()),
189 1000,
191​
192const timeoutIdNumber: number = Number(timeoutId)
193```
194​
195#### Implementation of
196​
197[`TimeoutProvider`](../type-aliases/TimeoutProvider.md).[`setTimeout`](../type-aliases/TimeoutProvider.md#settimeout)
198​
199***
200​
201### setTimeoutProvider()
202​
203```ts
204setTimeoutProvider<TTimerId>(provider: TimeoutProvider<TTimerId>): void;
205```
206​
207Defined in: [packages/query-core/src/timeoutManager.ts:106](https://github.com/TanStack/query/blob/main/packages/query-core/src/timeoutManager.ts#L106)
208​
209`setTimeoutProvider` can be used to set a custom implementation of the
210`setTimeout`, `clearTimeout`, `setInterval`, `clearInterval` functions,
211called a `TimeoutProvider`.
212​
213This may be useful if you notice event loop performance issues with
214thousands of queries. A custom TimeoutProvider could also support timer
215delays longer than the global `setTimeout` maximum delay value of about
21624 days.
217​
218It is important to call `setTimeoutProvider` before creating a
219QueryClient or queries, so that the same provider is used consistently
220for all timers in the application, since different TimeoutProviders
221cannot cancel each others' timers.
222​
223#### Type Parameters
224​
225##### TTimerId
226​
227`TTimerId` *extends* [`ManagedTimerId`](../type-aliases/ManagedTimerId.md)
228​
229#### Parameters
230​
231##### provider
232​
233[`TimeoutProvider`](../type-aliases/TimeoutProvider.md)\<`TTimerId`\>
234​
235#### Returns
236​
237`void`
238​
239#### Example
240​
241```ts
242import { timeoutManager, QueryClient } from '@tanstack/query-core'
243import { CustomTimeoutProvider } from './CustomTimeoutProvider'
244​
245timeoutManager.setTimeoutProvider(new CustomTimeoutProvider())
246​
247export const queryClient = new QueryClient()
248```