/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import e from"./Logger.js";import{NoPriority as t}from"./MemCache.js";const s=()=>e.getLogger("esri/core/MemCachePool");class r{constructor(e,t){this._cache=e(t,(e,t,r)=>{switch(t){case 0:return e.forEach(e=>e.dispose()),0;case 1:{const t=e.shift();return t?(r-=Math.round(t.usedMemory),t.dispose()):r>0&&(s().warn("Encountered empty MemCachePool with non-zero memory."),r=0),r}}})}hitrate(){return this._cache.hitRate}destroy(){this._cache.destroy()}clear(){this._cache.clear()}getSize(e){return this._cache.getSize(e)}pop(e){const t=this._cache.peek(e);if(!t)return;const s=t.pop();return t.length>0?s&&(t.usedMemory=this._cache.getSize(e)-Math.round(s.usedMemory),this._cache.updateSize(e)):this._cache.pop(e),s}put(e,s,r=t){const o=this._cache.peek(e);if(!o){const t=new c(s);return void this._cache.put(e,t,r)}o.push(s),o.usedMemory=this._cache.getSize(e)+Math.round(s.usedMemory),this._cache.updateSize(e)}}class c extends Array{constructor(e){super(),this.item=e,this.usedMemory=e.usedMemory,this.push(e)}}export{r as MemCachePool};
