|
| 1 | +import {AsyncCacheMap, CacheMap, CacheMapInput, KeyPrefix} from './types'; |
| 2 | + |
| 3 | +const supportsDeleteSpreadCache = new WeakMap<Function, boolean>(); |
| 4 | + |
| 5 | +function supportsDeleteSpreadUncached<TKey, TValue>( |
| 6 | + cacheMap: CacheMapInput<TKey, TValue>, |
| 7 | +): boolean { |
| 8 | + return /^[^={]*\.\.\./.test(cacheMap.delete.toString()); |
| 9 | +} |
| 10 | + |
| 11 | +function supportsDeleteSpread<TKey, TValue>( |
| 12 | + cacheMap: CacheMapInput<TKey, TValue>, |
| 13 | +): boolean { |
| 14 | + if (cacheMap.constructor === Map || cacheMap.constructor === WeakMap) { |
| 15 | + return false; |
| 16 | + } |
| 17 | + if (cacheMap.constructor === Object || cacheMap.constructor === Function) { |
| 18 | + return supportsDeleteSpreadUncached(cacheMap); |
| 19 | + } |
| 20 | + |
| 21 | + const cached = supportsDeleteSpreadCache.get(cacheMap.constructor); |
| 22 | + if (cached !== undefined) return cached; |
| 23 | + |
| 24 | + const freshValue = supportsDeleteSpreadUncached(cacheMap); |
| 25 | + supportsDeleteSpreadCache.set(cacheMap.constructor, freshValue); |
| 26 | + return freshValue; |
| 27 | +} |
| 28 | + |
| 29 | +class CacheMapImplementation<TKey, TResult, TMappedKey> |
| 30 | + implements CacheMap<TKey, TResult> |
| 31 | +{ |
| 32 | + private readonly _map: CacheMapInput<TMappedKey, TResult>; |
| 33 | + private readonly _mapKey: (key: TKey) => TMappedKey; |
| 34 | + private readonly _supportsDeleteSpread: boolean; |
| 35 | + constructor( |
| 36 | + map: CacheMapInput<TMappedKey, TResult>, |
| 37 | + mapKey: (key: TKey) => TMappedKey, |
| 38 | + ) { |
| 39 | + this._map = map; |
| 40 | + this._mapKey = mapKey; |
| 41 | + this._supportsDeleteSpread = supportsDeleteSpread(map); |
| 42 | + } |
| 43 | + |
| 44 | + get size() { |
| 45 | + return this._map.size; |
| 46 | + } |
| 47 | + get(key: TKey): TResult | undefined { |
| 48 | + const cacheKey = this._mapKey(key); |
| 49 | + return this._map.get(cacheKey); |
| 50 | + } |
| 51 | + set(key: TKey, value: TResult): void { |
| 52 | + const cacheKey = this._mapKey(key); |
| 53 | + this._map.set(cacheKey, value); |
| 54 | + } |
| 55 | + deletePrefix(prefix: KeyPrefix<TKey>): void { |
| 56 | + if (this._map.deletePrefix) { |
| 57 | + this._map.deletePrefix(prefix as any); |
| 58 | + } else if (this._map.keys && typeof prefix === 'string') { |
| 59 | + for (const key of this._map.keys()) { |
| 60 | + const k: unknown = key; |
| 61 | + if (typeof k !== 'string') { |
| 62 | + throw new Error( |
| 63 | + `This cache contains non-string keys so you cannot use deletePrefix.`, |
| 64 | + ); |
| 65 | + } |
| 66 | + if (k.startsWith(prefix)) { |
| 67 | + this._map.delete(key); |
| 68 | + } |
| 69 | + } |
| 70 | + } else { |
| 71 | + throw new Error(`This cache does not support deletePrefix.`); |
| 72 | + } |
| 73 | + } |
| 74 | + delete(...keys: TKey[]): void { |
| 75 | + if (!this._supportsDeleteSpread || keys.length < 2) { |
| 76 | + for (const key of keys) { |
| 77 | + const cacheKey = this._mapKey(key); |
| 78 | + this._map.delete(cacheKey); |
| 79 | + } |
| 80 | + } else { |
| 81 | + const cacheKeys = keys.map(this._mapKey); |
| 82 | + this._map.delete(...cacheKeys); |
| 83 | + } |
| 84 | + } |
| 85 | + clear(): void { |
| 86 | + if (!this._map.clear) { |
| 87 | + throw new Error(`This cache does not support clearing`); |
| 88 | + } |
| 89 | + this._map.clear(); |
| 90 | + } |
| 91 | +} |
| 92 | +export function createCacheMap<TKey, TValue, TMappedKey = TKey>( |
| 93 | + map: CacheMapInput<TMappedKey, TValue>, |
| 94 | + mapKey: (key: TKey) => TMappedKey, |
| 95 | +): CacheMap<TKey, TValue> { |
| 96 | + return new CacheMapImplementation(map, mapKey); |
| 97 | +} |
| 98 | + |
| 99 | +class AsyncCacheMapImplementation<TKey, TResult, TMappedKey> |
| 100 | + extends CacheMapImplementation<TKey, Promise<TResult>, TMappedKey> |
| 101 | + implements AsyncCacheMap<TKey, TResult> |
| 102 | +{ |
| 103 | + set(key: TKey, value: TResult | Promise<TResult>): void { |
| 104 | + super.set(key, Promise.resolve(value)); |
| 105 | + } |
| 106 | +} |
| 107 | +export function createAsyncCacheMap<TKey, TValue, TMappedKey = TKey>( |
| 108 | + map: CacheMapInput<TMappedKey, Promise<TValue>>, |
| 109 | + mapKey: (key: TKey) => TMappedKey, |
| 110 | +): AsyncCacheMap<TKey, TValue> { |
| 111 | + return new AsyncCacheMapImplementation(map, mapKey); |
| 112 | +} |
0 commit comments