All files / source/CaffeineMc WorkingCache.js

100% Statements 13/13
87.5% Branches 7/8
100% Functions 8/8
100% Lines 13/13

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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48  7x 7x 7x                   7x 7x 7x 7x   30x         60x       543x       435x         28x   543x                  
"use strict";
let Caf = require("caffeine-script-runtime");
Caf.defMod(module, () => {
  return Caf.importInvoke(
    ["currentSecond"],
    [global, require("./StandardImport")],
    (currentSecond) => {
      let workingCache,
        cacheExpiresIn,
        workingCacheLastResetAt,
        resetWorkingCache,
        cacheRead,
        cacheWrite;
      workingCache = {};
      cacheExpiresIn = 1;
      workingCacheLastResetAt = currentSecond() - cacheExpiresIn * 10;
      return {
        checkWorkingCacheExpiration: function () {
          return currentSecond() - workingCacheLastResetAt > cacheExpiresIn
            ? resetWorkingCache()
            : undefined;
        },
        resetWorkingCache: (resetWorkingCache = function () {
          return (workingCache = {});
        }),
        cacheRead: (cacheRead = function (key, p) {
          let base;
          return Caf.exists((base = workingCache[key])) && base[p];
        }),
        cacheWrite: (cacheWrite = function (key, p, v) {
          let temp;
          return (((temp = workingCache[key]) != null
            ? temp
            : (workingCache[key] = {}))[p] = v);
        }),
        cacheable: function (key, f) {
          return (p) => {
            let temp;
            return (temp = cacheRead(key, p)) != null
              ? temp
              : cacheWrite(key, p, f(p));
          };
        },
      };
    }
  );
});