All files / services/utils cachedThunk.ts

88.89% Statements 8/9
50% Branches 1/2
100% Functions 2/2
88.89% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 1517x 17x   64x 52x     52x 46x 46x     17x    
const cache = new WeakMap<() => any, any>();
 
export function createCachedThunk<ThunkResult>(
  thunk: () => ThunkResult,
): () =>I ThunkResult {
  return () => {
    if (cache.has(thunk)) {
      return cache.get(thunk);
    }
    const result = thunk();
    cache.set(thunk, result);
    return result;
  };
}