Cache/buffer lifecycle
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user