chromatic-lattice/assets/lru-cache.9506e0ec.js

1 line
4 KiB
JavaScript

import{y as x}from"./yallist.fd762fe7.js";var C=typeof globalThis!="undefined"?globalThis:typeof window!="undefined"?window:typeof global!="undefined"?global:typeof self!="undefined"?self:{};function G(n){return n&&n.__esModule&&Object.prototype.hasOwnProperty.call(n,"default")?n.default:n}function N(n){if(n.__esModule)return n;var t=Object.defineProperty({},"__esModule",{value:!0});return Object.keys(n).forEach(function(e){var r=Object.getOwnPropertyDescriptor(n,e);Object.defineProperty(t,e,r.get?r:{enumerable:!0,get:function(){return n[e]}})}),t}const O=x,u=Symbol("max"),l=Symbol("length"),g=Symbol("lengthCalculator"),b=Symbol("allowStale"),c=Symbol("maxAge"),o=Symbol("dispose"),A=Symbol("noDisposeOnSet"),i=Symbol("lruList"),a=Symbol("cache"),S=Symbol("updateAgeOnGet"),v=()=>1;class _{constructor(t){if(typeof t=="number"&&(t={max:t}),t||(t={}),t.max&&(typeof t.max!="number"||t.max<0))throw new TypeError("max must be a non-negative number");this[u]=t.max||1/0;const e=t.length||v;if(this[g]=typeof e!="function"?v:e,this[b]=t.stale||!1,t.maxAge&&typeof t.maxAge!="number")throw new TypeError("maxAge must be a number");this[c]=t.maxAge||0,this[o]=t.dispose,this[A]=t.noDisposeOnSet||!1,this[S]=t.updateAgeOnGet||!1,this.reset()}set max(t){if(typeof t!="number"||t<0)throw new TypeError("max must be a non-negative number");this[u]=t||1/0,d(this)}get max(){return this[u]}set allowStale(t){this[b]=!!t}get allowStale(){return this[b]}set maxAge(t){if(typeof t!="number")throw new TypeError("maxAge must be a non-negative number");this[c]=t,d(this)}get maxAge(){return this[c]}set lengthCalculator(t){typeof t!="function"&&(t=v),t!==this[g]&&(this[g]=t,this[l]=0,this[i].forEach(e=>{e.length=this[g](e.value,e.key),this[l]+=e.length})),d(this)}get lengthCalculator(){return this[g]}get length(){return this[l]}get itemCount(){return this[i].length}rforEach(t,e){e=e||this;for(let r=this[i].tail;r!==null;){const s=r.prev;E(this,t,r,e),r=s}}forEach(t,e){e=e||this;for(let r=this[i].head;r!==null;){const s=r.next;E(this,t,r,e),r=s}}keys(){return this[i].toArray().map(t=>t.key)}values(){return this[i].toArray().map(t=>t.value)}reset(){this[o]&&this[i]&&this[i].length&&this[i].forEach(t=>this[o](t.key,t.value)),this[a]=new Map,this[i]=new O,this[l]=0}dump(){return this[i].map(t=>w(this,t)?!1:{k:t.key,v:t.value,e:t.now+(t.maxAge||0)}).toArray().filter(t=>t)}dumpLru(){return this[i]}set(t,e,r){if(r=r||this[c],r&&typeof r!="number")throw new TypeError("maxAge must be a number");const s=r?Date.now():0,h=this[g](e,t);if(this[a].has(t)){if(h>this[u])return y(this,this[a].get(t)),!1;const m=this[a].get(t).value;return this[o]&&(this[A]||this[o](t,m.value)),m.now=s,m.maxAge=r,m.value=e,this[l]+=h-m.length,m.length=h,this.get(t),d(this),!0}const f=new T(t,e,h,s,r);return f.length>this[u]?(this[o]&&this[o](t,e),!1):(this[l]+=f.length,this[i].unshift(f),this[a].set(t,this[i].head),d(this),!0)}has(t){if(!this[a].has(t))return!1;const e=this[a].get(t).value;return!w(this,e)}get(t){return p(this,t,!0)}peek(t){return p(this,t,!1)}pop(){const t=this[i].tail;return t?(y(this,t),t.value):null}del(t){y(this,this[a].get(t))}load(t){this.reset();const e=Date.now();for(let r=t.length-1;r>=0;r--){const s=t[r],h=s.e||0;if(h===0)this.set(s.k,s.v);else{const f=h-e;f>0&&this.set(s.k,s.v,f)}}}prune(){this[a].forEach((t,e)=>p(this,e,!1))}}const p=(n,t,e)=>{const r=n[a].get(t);if(r){const s=r.value;if(w(n,s)){if(y(n,r),!n[b])return}else e&&(n[S]&&(r.value.now=Date.now()),n[i].unshiftNode(r));return s.value}},w=(n,t)=>{if(!t||!t.maxAge&&!n[c])return!1;const e=Date.now()-t.now;return t.maxAge?e>t.maxAge:n[c]&&e>n[c]},d=n=>{if(n[l]>n[u])for(let t=n[i].tail;n[l]>n[u]&&t!==null;){const e=t.prev;y(n,t),t=e}},y=(n,t)=>{if(t){const e=t.value;n[o]&&n[o](e.key,e.value),n[l]-=e.length,n[a].delete(e.key),n[i].removeNode(t)}};class T{constructor(t,e,r,s,h){this.key=t,this.value=e,this.length=r,this.now=s,this.maxAge=h||0}}const E=(n,t,e,r)=>{let s=e.value;w(n,s)&&(y(n,e),n[b]||(s=void 0)),s&&t.call(r,s.value,s.key,n)};var j=_;export{G as a,C as c,N as g,j as l};