Update CacheManager.ts

cache-manager
Steve Ruiz 2024-05-11 09:57:37 +01:00
rodzic ef181cbd98
commit 1cb4423563
1 zmienionych plików z 34 dodań i 12 usunięć

Wyświetl plik

@ -9,34 +9,56 @@ type CacheItem<T extends object, Q> = {
export class CacheManager {
constructor(public editor: Editor) {}
/**
* The manager's cache items.
*/
private items = new Map<string, CacheItem<any, any>>()
/**
* Get a cache by name.
*/
private get<T extends object, Q>(name: string): CacheItem<T, Q> {
return this.items.get(name)!
}
/**
* Get whether a cache exists.
*/
has(name: string) {
return this.items.has(name)
}
/**
* Create a new cache.
*
* @param name - The name of the cache.
* @param fn - The function to use to create a new cached value for a given input.
*/
createCache<T extends object, Q>(name: string, fn: (input: T) => Q) {
this.items.set(name, { cache: new WeakCache<T, Q>(), fn })
return this.get<T, Q>(name)
}
has(name: string) {
return this.items.has(name)
}
get<T extends object, Q>(name: string): CacheItem<T, Q> {
return this.items.get(name)!
}
/**
* Get the value from a cache. If the cache does not exist, it will be created using the function provided when the cache was created.
*
* @param name - The name of the cache.
* @param input - The input to the cache function.
*/
getValue<T extends object, Q>(name: string, input: T): Q {
const item = this.get<T, Q>(name)
if (!item) throw Error(`Cache ${name} not found`)
return item.cache.get(input, item.fn)
}
/**
* Clear all values from a cache.
*
* @param name - The name of the cache.
*/
clear(name: string) {
const item = this.items.get(name)
if (!item) throw Error(`Cache ${name} not found`)
item.cache.clear()
}
clearAll() {
this.items.clear()
}
}