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