rsnext/packages/next/compiled/lru-cache/index.js

1 line
9.3 KiB
JavaScript
Raw Normal View History

(()=>{"use strict";var t={717:(t,e,i)=>{const s=i(112);const n=Symbol("max");const l=Symbol("length");const r=Symbol("lengthCalculator");const h=Symbol("allowStale");const a=Symbol("maxAge");const o=Symbol("dispose");const u=Symbol("noDisposeOnSet");const f=Symbol("lruList");const p=Symbol("cache");const v=Symbol("updateAgeOnGet");const naiveLength=()=>1;class LRUCache{constructor(t){if(typeof t==="number")t={max:t};if(!t)t={};if(t.max&&(typeof t.max!=="number"||t.max<0))throw new TypeError("max must be a non-negative number");const e=this[n]=t.max||Infinity;const i=t.length||naiveLength;this[r]=typeof i!=="function"?naiveLength:i;this[h]=t.stale||false;if(t.maxAge&&typeof t.maxAge!=="number")throw new TypeError("maxAge must be a number");this[a]=t.maxAge||0;this[o]=t.dispose;this[u]=t.noDisposeOnSet||false;this[v]=t.updateAgeOnGet||false;this.reset()}set max(t){if(typeof t!=="number"||t<0)throw new TypeError("max must be a non-negative number");this[n]=t||Infinity;trim(this)}get max(){return this[n]}set allowStale(t){this[h]=!!t}get allowStale(){return this[h]}set maxAge(t){if(typeof t!=="number")throw new TypeError("maxAge must be a non-negative number");this[a]=t;trim(this)}get maxAge(){return this[a]}set lengthCalculator(t){if(typeof t!=="function")t=naiveLength;if(t!==this[r]){this[r]=t;this[l]=0;this[f].forEach((t=>{t.length=this[r](t.value,t.key);this[l]+=t.length}))}trim(this)}get lengthCalculator(){return this[r]}get length(){return this[l]}get itemCount(){return this[f].length}rforEach(t,e){e=e||this;for(let i=this[f].tail;i!==null;){const s=i.prev;forEachStep(this,t,i,e);i=s}}forEach(t,e){e=e||this;for(let i=this[f].head;i!==null;){const s=i.next;forEachStep(this,t,i,e);i=s}}keys(){return this[f].toArray().map((t=>t.key))}values(){return this[f].toArray().map((t=>t.value))}reset(){if(this[o]&&this[f]&&this[f].length){this[f].forEach((t=>this[o](t.key,t.value)))}this[p]=new Map;this[f]=new s;this[l]=0}dump(){return this[f].map((t=>isStale(this,t)?false:{k:t.key,v:t.value,e:t.now+(t.maxAge||0)})).toArray().filter((t=>t))}dumpLru(){return this[f]}set(t,e,i){i=i||this[a];if(i&&typeof i!=="number")throw new TypeError("maxAge must be a number");const s=i?Date.now():0;const h=this[r](e,t);if(this[p].has(t)){if(h>this[n]){del(this,this[p].get(t));return false}const r=this[p].get(t);const a=r.value;if(this[o]){if(!this[u])this[o](t,a.value)}a.now=s;a.maxAge=i;a.value=e;this[l]+=h-a.length;a.length=h;this.get(t);trim(this);return true}const v=new Entry(t,e,h,s,i);if(v.length>this[n]){if(this[o])this[o](t,e);return false}this[l]+=v.length;this[f].unshift(v);this[p].set(t,this[f].head);trim(this);return true}has(t){if(!this[p].has(t))return false;const e=this[p].get(t).value;return!isStale(this,e)}get(t){return get(this,t,true)}peek(t){return get(this,t,false)}pop(){const t=this[f].tail;if(!t)return null;del(this,t);return t.value}del(t){del(this,this[p].get(t))}load(t){this.reset();const e=Date.now();for(let i=t.length-1;i>=0;i--){const s=t[i];const n=s.e||0;if(n===0)this.set(s.k,s.v);else{const t=n-e;if(t>0){this.set(s.k,s.v,t)}}}}prune(){this[p].forEach(((t,e)=>get(this,e,false)))}}const get=(t,e,i)=>{const s=t[p].get(e);if(s){const e=s.value;if(isStale(t,e)){del(t,s);if(!t[h])return undefined}else{if(i){if(t[v])s.value.now=Date.now();t[f].unshiftNode(s)}}return e.value}};const isStale=(t,e)=>{if(!e||!e.maxAge&&!t[a])return false;const i=Date.now()-e.now;return e.maxAge?i>e.maxAge:t[a]&&i>t[a]};const trim=t=>{if(t[l]>t[n]){for(let e=t[f].tail;t[l]>t[n]&&e!==null;){const i=e.prev;del(t,e);e=i}}};const del=(t,e)=>{if(e){const i=e.value;if(t[o])t[o](i.key,i.value);t[l]-=i.length;t[p].delete(i.key);t[f].removeNode(e)}};class Entry{constructor(t,e,i,s,n){this.key=t;this.value=e;this.length=i;this.now=s;this.maxAge=n||0}}const forEachStep=(t,e,i,s)=>{let n=i.value;if(isStale(t,n)){del(t,i);if(!t[h])n=undefined}if(n)e.call(s,n.value,n.key,t)};t.exports=LRUCache},598:t=>{t.exports=function(t){t.prototype[Symbol.iterator]=function*(){for(let t=this.head;t;t=t.next){yield t.value}}}},112:(t,e,i)=>{t.exports=Yallist;Yallist.Node