Open History to see the last commit
1import { isServer, isValidTimeout } from './utils'
2β
3export abstract class Removable {
4 gcTime!: number
5 #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.gcTime)) {
15 this.#gcTimeout = setTimeout(() => {
16 this.optionalRemove()
17 }, this.gcTime)
18 }
19 }
20β
21 protected updateGcTime(newGcTime: number | undefined): void {
22 // Default to 5 minutes (Infinity for server-side) if no gcTime is set
23 this.gcTime = Math.max(
24 this.gcTime || 0,
25 newGcTime ?? (isServer ? Infinity : 5 * 60 * 1000),
26 )
27 }
28β
29 protected clearGcTimeout() {
30 if (this.#gcTimeout) {
31 clearTimeout(this.#gcTimeout)
32 this.#gcTimeout = undefined
33 }
34 }
35β
36 protected abstract optionalRemove(): void
37}