Cache/buffer lifecycle

This commit is contained in:
Daniel Imms
2026-05-02 05:44:13 -07:00
parent 52d4b2719c
commit 6287c6ddfe
5 changed files with 84 additions and 42 deletions
+22 -5
View File
@@ -4,15 +4,18 @@
*/
import type { IBufferLineStringCache, IBufferLineStringCacheEntry } from 'common/buffer/BufferLine';
import { disposableTimeout } from 'common/Async';
import type { IDisposable } from 'common/Lifecycle';
export class BufferLineStringCache implements IBufferLineStringCache {
const STRING_CACHE_CLEAR_DELAY_MS = 50;
export class BufferLineStringCache implements IBufferLineStringCache, IDisposable {
public generation: number = 0;
public readonly entries: Set<IBufferLineStringCacheEntry> = new Set();
constructor(private readonly _onCacheUsed: () => void = () => { }) { }
private _clearTimeout: IDisposable | undefined;
public touch(): void {
this._onCacheUsed();
this._scheduleClear();
}
public allocateEntry(): IBufferLineStringCacheEntry {
@@ -22,11 +25,13 @@ export class BufferLineStringCache implements IBufferLineStringCache {
generation: this.generation
};
this.entries.add(entry);
this._onCacheUsed();
this._scheduleClear();
return entry;
}
public clear(): void {
this._clearTimeout?.dispose();
this._clearTimeout = undefined;
this.generation++;
for (const entry of this.entries) {
entry.value = undefined;
@@ -34,4 +39,16 @@ export class BufferLineStringCache implements IBufferLineStringCache {
}
this.entries.clear();
}
public dispose(): void {
this.clear();
}
private _scheduleClear(): void {
this._clearTimeout?.dispose();
this._clearTimeout = disposableTimeout(() => {
this._clearTimeout = undefined;
this.clear();
}, STRING_CACHE_CLEAR_DELAY_MS);
}
}