Skip to content

Commit 1f1775c

Browse files
authored
fix(timeout): make sure NodeJs.Timout doesn't leak (#10323)
* fix(timeout): make sure NodeJs.Timout doesn't leak * changeset
1 parent b1188fe commit 1f1775c

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

.changeset/funny-views-build.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': patch
3+
---
4+
5+
fix(timeoutManager): make sure NodeJs.Timout doesn't leak

packages/query-core/src/timeoutManager.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export type TimeoutProvider<TTimerId extends ManagedTimerId = ManagedTimerId> =
2828
readonly clearInterval: (intervalId: TTimerId | undefined) => void
2929
}
3030

31-
export const defaultTimeoutProvider: TimeoutProvider<
32-
ReturnType<typeof setTimeout>
33-
> = {
31+
type SystemTimerId = ReturnType<typeof setTimeout>
32+
33+
export const defaultTimeoutProvider: TimeoutProvider = {
3434
// We need the wrapper function syntax below instead of direct references to
3535
// global setTimeout etc.
3636
//
@@ -42,10 +42,12 @@ export const defaultTimeoutProvider: TimeoutProvider<
4242
// have a hard reference to the original implementation at the time when this
4343
// file was imported.
4444
setTimeout: (callback, delay) => setTimeout(callback, delay),
45-
clearTimeout: (timeoutId) => clearTimeout(timeoutId),
45+
clearTimeout: (timeoutId) =>
46+
clearTimeout(timeoutId as SystemTimerId | undefined),
4647

4748
setInterval: (callback, delay) => setInterval(callback, delay),
48-
clearInterval: (intervalId) => clearInterval(intervalId),
49+
clearInterval: (intervalId) =>
50+
clearInterval(intervalId as SystemTimerId | undefined),
4951
}
5052

5153
/**
@@ -62,7 +64,8 @@ export const defaultTimeoutProvider: TimeoutProvider<
6264
export class TimeoutManager implements Omit<TimeoutProvider, 'name'> {
6365
// We cannot have TimeoutManager<T> as we must instantiate it with a concrete
6466
// 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.
67+
// would need to support the default provider's concrete timer ID, which is
68+
// infeasible across environments.
6669
//
6770
// We settle for type safety for the TimeoutProvider type, and accept that
6871
// this class is unsafe internally to allow for extension.

0 commit comments

Comments
 (0)