0
Fork 0
mirror of https://github.com/penpot/penpot-exporter-figma-plugin.git synced 2024-12-21 21:23:06 -05:00
penpot-exporter-figma-plugin/plugin-src/Cache.ts
Jordi Sala Morales 4edb964a96
Cache google fonts traversal (#176)
* wip

* revert

* extract as a class

* fix

* fix package.jsonm
2024-06-18 14:02:13 +02:00

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;
}
}