1 | {"version":3,"file":"cache.cjs","sources":["../cache.js"],"sourcesContent":["/* eslint-env browser */\n\n/**\n * An implementation of a map which has keys that expire.\n *\n * @module cache\n */\n\nimport * as list from './list.js'\nimport * as map from './map.js'\nimport * as time from './time.js'\n\n/**\n * @template K, V\n *\n * @implements {list.ListNode}\n */\nclass Entry {\n /**\n * @param {K} key\n * @param {V | Promise<V>} val\n */\n constructor (key, val) {\n /**\n * @type {this | null}\n */\n this.prev = null\n /**\n * @type {this | null}\n */\n this.next = null\n this.created = time.getUnixTime()\n this.val = val\n this.key = key\n }\n}\n\n/**\n * @template K, V\n */\nexport class Cache {\n /**\n * @param {number} timeout\n */\n constructor (timeout) {\n this.timeout = timeout\n /**\n * @type list.List<Entry<K, V>>\n */\n this._q = list.create()\n /**\n * @type {Map<K, Entry<K, V>>}\n */\n this._map = map.create()\n }\n}\n\n/**\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @return {number} Returns the current timestamp\n */\nexport const removeStale = cache => {\n const now = time.getUnixTime()\n const q = cache._q\n while (q.start && now - q.start.created > cache.timeout) {\n cache._map.delete(q.start.key)\n list.popFront(q)\n }\n return now\n}\n\n/**\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @param {K} key\n * @param {V} value\n */\nexport const set = (cache, key, value) => {\n const now = removeStale(cache)\n const q = cache._q\n const n = cache._map.get(key)\n if (n) {\n list.removeNode(q, n)\n list.pushEnd(q, n)\n n.created = now\n n.val = value\n } else {\n const node = new Entry(key, value)\n list.pushEnd(q, node)\n cache._map.set(key, node)\n }\n}\n\n/**\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @param {K} key\n * @return {Entry<K, V> | undefined}\n */\nconst getNode = (cache, key) => {\n removeStale(cache)\n const n = cache._map.get(key)\n if (n) {\n return n\n }\n}\n\n/**\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @param {K} key\n * @return {V | undefined}\n */\nexport const get = (cache, key) => {\n const n = getNode(cache, key)\n return n && !(n.val instanceof Promise) ? n.val : undefined\n}\n\n/**\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @param {K} key\n */\nexport const refreshTimeout = (cache, key) => {\n const now = time.getUnixTime()\n const q = cache._q\n const n = cache._map.get(key)\n if (n) {\n list.removeNode(q, n)\n list.pushEnd(q, n)\n n.created = now\n }\n}\n\n/**\n * Works well in conjunktion with setIfUndefined which has an async init function.\n * Using getAsync & setIfUndefined ensures that the init function is only called once.\n *\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @param {K} key\n * @return {V | Promise<V> | undefined}\n */\nexport const getAsync = (cache, key) => {\n const n = getNode(cache, key)\n return n ? n.val : undefined\n}\n\n/**\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @param {K} key\n */\nexport const remove = (cache, key) => {\n const n = cache._map.get(key)\n if (n) {\n list.removeNode(cache._q, n)\n cache._map.delete(key)\n return n.val && !(n.val instanceof Promise) ? n.val : undefined\n }\n}\n\n/**\n * @template K, V\n *\n * @param {Cache<K, V>} cache\n * @param {K} key\n * @param {function():Promise<V>} init\n * @param {boolean} removeNull Optional argument that automatically removes values that resolve to null/undefined from the cache.\n * @return {Promise<V> | V}\n */\nexport const setIfUndefined = (cache, key, init, removeNull = false) => {\n removeStale(cache)\n const q = cache._q\n const n = cache._map.get(key)\n if (n) {\n return n.val\n } else {\n const p = init()\n const node = new Entry(key, p)\n list.pushEnd(q, node)\n cache._map.set(key, node)\n p.then(v => {\n if (p === node.val) {\n node.val = v\n }\n if (removeNull && v == null) {\n remove(cache, key)\n }\n })\n return p\n }\n}\n\n/**\n * @param {number} timeout\n */\nexport const create = timeout => new Cache(timeout)\n"],"names":["time.getUnixTime","list.create","map.create","list.popFront","list.removeNode","list.pushEnd"],"mappings":";;;;;;;;;;;;;;;AAAA;AAWA;AACA;AACA;AACA;AACA;AACA;AACA,MAAM,KAAK,CAAC;AACZ;AACA;AACA;AACA;AACA,EAAE,WAAW,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE;AACzB;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