continue-custom/core/util/LruCache.ts
Nate Sesti 851e825af6
✨ use and cache imports for autocomplete (#1456)
* ✨ use and cache imports for autocomplete

* fix tsc
2024-06-21 14:02:29 -07:00

30 lines
807 B
TypeScript

export class PrecalculatedLruCache<V> {
private items: [string, V][] = [];
constructor(
private readonly calculateValue: (key: string) => Promise<V>,
private readonly N: number,
) {}
async initKey(key: string) {
// Maintain LRU
const index = this.items.findIndex((item) => item[0] === key);
if (index < 0) {
// Calculate info for new file
const value: V = await this.calculateValue(key);
this.items.push([key, value]);
if (this.items.length > this.N) {
this.items.shift();
}
} else {
// Move to end of array, since it was recently used
const [item] = this.items.splice(index, 1);
this.items.push(item);
}
}
get(key: string): V | undefined {
return this.items.find((item) => item[0] === key)?.[1];
}
}