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

TimeoutManager.md

246 lines5.9 KB

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