All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.build.esm.cache.js.map Maven / Gradle / Ivy

The newest version!
{"version":3,"file":"cache.js","sources":["../../src/cache.ts"],"sourcesContent":["/**\n * Creates a cache that evicts keys in fifo order\n * @param size {Number}\n */\nexport function makeFifoCache(\n  size: number,\n): {\n  get: (key: Key) => Value | undefined;\n  add: (key: Key, value: Value) => void;\n  delete: (key: Key) => boolean;\n  clear: () => void;\n  size: () => number;\n} {\n  // Maintain a fifo queue of keys, we cannot rely on Object.keys as the browser may not support it.\n  let evictionOrder: Key[] = [];\n  let cache: Record = {};\n\n  return {\n    add(key: Key, value: Value) {\n      while (evictionOrder.length >= size) {\n        // shift is O(n) but this is small size and only happens if we are\n        // exceeding the cache size so it should be fine.\n        const evictCandidate = evictionOrder.shift();\n\n        if (evictCandidate !== undefined) {\n          // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n          delete cache[evictCandidate];\n        }\n      }\n\n      // in case we have a collision, delete the old key.\n      if (cache[key]) {\n        this.delete(key);\n      }\n\n      evictionOrder.push(key);\n      cache[key] = value;\n    },\n    clear() {\n      cache = {};\n      evictionOrder = [];\n    },\n    get(key: Key): Value | undefined {\n      return cache[key];\n    },\n    size() {\n      return evictionOrder.length;\n    },\n    // Delete cache key and return true if it existed, false otherwise.\n    delete(key: Key): boolean {\n      if (!cache[key]) {\n        return false;\n      }\n\n      // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n      delete cache[key];\n\n      for (let i = 0; i < evictionOrder.length; i++) {\n        if (evictionOrder[i] === key) {\n          evictionOrder.splice(i, 1);\n          break;\n        }\n      }\n\n      return true;\n    },\n  };\n}\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;AACO,SAAS,aAAa;AAC7B,EAAE,IAAI;AACN;;AAMA,CAAE;AACF;AACA,EAAE,IAAI,aAAa,GAAU,EAAE;AAC/B,EAAE,IAAI,KAAK,GAA0B,EAAE;;AAEvC,EAAE,OAAO;AACT,IAAI,GAAG,CA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