mirror of
https://github.com/penpot/penpot-exporter-figma-plugin.git
synced 2024-12-21 21:23:06 -05:00
4edb964a96
* wip * revert * extract as a class * fix * fix package.jsonm
26 lines
693 B
TypeScript
26 lines
693 B
TypeScript
import { LRUCache } from 'lru-cache';
|
|
|
|
const empty: unique symbol = Symbol('noValue');
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
export class Cache<K extends {}, V extends {}> {
|
|
private cache: LRUCache<K, V | typeof empty>;
|
|
|
|
public constructor(options: LRUCache.Options<K, V | typeof empty, unknown>) {
|
|
this.cache = new LRUCache(options);
|
|
}
|
|
|
|
public get(key: K, calculate: () => V | undefined): V | undefined {
|
|
if (this.cache.has(key)) {
|
|
const cacheItem = this.cache.get(key);
|
|
|
|
return cacheItem === empty ? undefined : cacheItem;
|
|
}
|
|
|
|
const calculated = calculate();
|
|
|
|
this.cache.set(key, calculated ?? empty);
|
|
|
|
return calculated;
|
|
}
|
|
}
|