forked from inshared/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremovable.ts
37 lines (30 loc) · 882 Bytes
/
removable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { isServer, isValidTimeout } from './utils'
export abstract class Removable {
cacheTime!: number
private gcTimeout?: ReturnType<typeof setTimeout>
destroy(): void {
this.clearGcTimeout()
}
protected scheduleGc(): void {
this.clearGcTimeout()
if (isValidTimeout(this.cacheTime)) {
this.gcTimeout = setTimeout(() => {
this.optionalRemove()
}, this.cacheTime)
}
}
protected updateCacheTime(newCacheTime: number | undefined): void {
// Default to 5 minutes (Infinity for server-side) if no cache time is set
this.cacheTime = Math.max(
this.cacheTime || 0,
newCacheTime ?? (isServer ? Infinity : 5 * 60 * 1000),
)
}
protected clearGcTimeout() {
if (this.gcTimeout) {
clearTimeout(this.gcTimeout)
this.gcTimeout = undefined
}
}
protected abstract optionalRemove(): void
}