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