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

removable.ts

35 lines847 Bytes

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