UNPKG

@allmaps/stdlib

Version:

Allmaps Standard Library

69 lines (68 loc) 2.51 kB
export function getPropertyFromCacheOrComputation(cache, key, computation, checkUse = () => true, checkStore = () => true) { if (cache.has(key) && checkUse(cache.get(key))) { return cache.get(key); } else { const result = computation(); if (checkStore(result)) { cache.set(key, result); } return result; } } export function getPropertyFromDoubleCacheOrComputation(cache, key0, key1, computation, checkUse = () => true, checkStore = () => true) { if (cache.get(key0)?.has(key1) && checkUse(cache.get(key0)?.get(key1))) { return cache.get(key0)?.get(key1); } else { const result = computation(); if (checkStore(result)) { if (!cache.get(key0)) { cache.set(key0, new Map()); } cache.get(key0)?.set(key1, result); } return result; } } export function getPropertyFromTripleCacheOrComputation(cache, key0, key1, key2, computation, checkUse = () => true, checkStore = () => true) { if (cache.get(key0)?.get(key1)?.has(key2) && checkUse(cache.get(key0)?.get(key1)?.get(key2))) { return cache.get(key0)?.get(key1)?.get(key2); } else { const result = computation(); if (checkStore(result)) { if (!cache.get(key0)) { cache.set(key0, new Map()); } if (!cache.get(key0)?.get(key1)) { cache.get(key0)?.set(key1, new Map()); } cache.get(key0)?.get(key1)?.set(key2, result); } return result; } } export function getPropertyFromQuadrupleCacheOrComputation(cache, key0, key1, key2, key3, computation, checkUse = () => true, checkStore = () => true) { if (cache.get(key0)?.get(key1)?.get(key2)?.has(key3) && checkUse(cache.get(key0)?.get(key1)?.get(key2)?.get(key3))) { return cache.get(key0)?.get(key1)?.get(key2)?.get(key3); } else { const result = computation(); if (checkStore(result)) { if (!cache.get(key0)) { cache.set(key0, new Map()); } if (!cache.get(key0)?.get(key1)) { cache.get(key0)?.set(key1, new Map()); } if (!cache.get(key0)?.get(key1)?.get(key2)) { cache.get(key0)?.get(key1)?.set(key2, new Map()); } cache.get(key0)?.get(key1)?.get(key2)?.set(key3, result); } return result; } }