/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{removeMaybe as o}from"../../../../core/maybe.js";import{throwIfAborted as t,onAbort as r}from"../../../../core/promiseUtils.js";import{DefaultLoadingContext as e}from"../../glTF/DefaultLoadingContext.js";import{load as s}from"./wosrLoader.js";class a{constructor(o){this._gltfLoading=new Map,this._wosrLoading=new Map,this._gltfCache=o("gltf-resources",()=>{}),this._wosrCache=o("wosr-resources",()=>{})}destroy(){this._gltfLoading.forEach(o=>o.abortController.abort()),this._wosrLoading.forEach(o=>o.abortController.abort()),this._gltfCache.destroy(),this._wosrCache.destroy()}loadGLTF(o,t,r,s){const a=`gltf:${o},pbr:${r},em:${s}`,i=this._gltfCache.get(a);return i?Promise.resolve(i):n(this._gltfLoading,this._gltfCache,a,async t=>{const{loadGLTF:a}=await import("../../glTF/loader.js");return a(new e,o,t,r,s)},t)}loadWOSR(o,t){const r=`wosr:${o}:${t.disableTextures}`,e=this._wosrCache.get(r);return e?Promise.resolve(e):n(this._wosrLoading,this._wosrCache,r,t=>s(o,t),t)}}async function n(e,s,a,n,l){t(l);const c=r(l,()=>i(e,a));let f=e.get(a);if(f)f.refCount++;else{const o=new AbortController;f={refCount:1,abortController:o,promise:n({...l,signal:o.signal})},e.set(a,f)}try{const o=await f.promise;return s.put(a,o),e.delete(a),t(l),o}finally{o(c)}}function i(o,t){const r=o.get(t);if(null!=r){if(--r.refCount>0)return}o.delete(t),null!=r&&r.abortController.abort()}export{a as ObjectResourceCache};
