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 1520x 20x   75x 63x     63x 53x 53x     20x    
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;
  };
}