3 onDispose?: ([key, value]: [K, V | undefined]) => void;
6 function createLRU<K, V>({ max, onDispose }: Args<K, V>): Map<K, V> {
7 const map = new Map<K, V>();
9 const getOldestKey = () => map.keys().next().value;
15 forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any) {
16 return map.forEach(callbackfn, thisArg);
18 clear: () => map.clear(),
19 has: (key: K) => map.has(key),
20 delete: (key: K) => map.delete(key),
21 entries: () => map.entries(),
22 keys: () => map.keys(),
23 values: () => map.values(),
24 [Symbol.iterator]: () => map[Symbol.iterator](),
25 get [Symbol.toStringTag]() {
26 return map[Symbol.toStringTag];
28 set: (key: K, value: V) => {
31 } else if (map.size === max) {
32 const keyToDispose = getOldestKey();
33 const valueToDispose = map.get(keyToDispose);
34 map.delete(keyToDispose);
36 onDispose([keyToDispose, valueToDispose]);
43 const item = map.get(key);
54 export default createLRU;