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

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

There is a newer version: 8.38.0
Show 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,CAAA;AAC/B,EAAE,IAAI,KAAK,GAA0B,EAAE,CAAA;AACvC;AACA,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