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

removable.ts

39 lines971 Bytes

1import { timeoutManager } from './timeoutManager'
2import { isServer, isValidTimeout } from './utils'
3import type { ManagedTimerId } from './timeoutManager'
4​
5export abstract class Removable {
6 gcTime!: number
7 #gcTimeout?: ManagedTimerId
8​
9 destroy(): void {
10 this.clearGcTimeout()
11 }
12​
13 protected scheduleGc(): void {
14 this.clearGcTimeout()
15​
16 if (isValidTimeout(this.gcTime)) {
17 this.#gcTimeout = timeoutManager.setTimeout(() => {
18 this.optionalRemove()
19 }, this.gcTime)
20 }
21 }
22​
23 protected updateGcTime(newGcTime: number | undefined): void {
24 // Default to 5 minutes (Infinity for server-side) if no gcTime is set
25 this.gcTime = Math.max(
26 this.gcTime || 0,
27 newGcTime ?? (isServer ? Infinity : 5 * 60 * 1000),
28 )
29 }
30​
31 protected clearGcTimeout() {
32 if (this.#gcTimeout) {
33 timeoutManager.clearTimeout(this.#gcTimeout)
34 this.#gcTimeout = undefined
35 }
36 }
37​
38 protected abstract optionalRemove(): void