UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 2.05 kB
import e from"../../request.js";import t from"../../core/Error.js";import{isAbortError as r}from"../../core/promiseUtils.js";import{isAnimatedPNG as i,parseApng as s}from"../../views/2d/engine/webgl/animatedFormats/apng.js";import{isAnimatedGIF as n,parseGif as a}from"../../views/2d/engine/webgl/animatedFormats/gif.js";class o{constructor(){this._resourceMap=new Map,this._inFlightResourceMap=new Map,this.geometryEngine=null,this.geometryEnginePromise=null}destroy(){this._inFlightResourceMap.clear(),this._resourceMap.clear()}getResource(e){return this._resourceMap.get(e)??null}async fetchResource(e,t){const r=this._resourceMap.get(e);if(r)return{width:r.width,height:r.height};let i=this._inFlightResourceMap.get(e);return i?i.then((e=>({width:e.width,height:e.height}))):(i=c(e,t),this._inFlightResourceMap.set(e,i),i.then((t=>(this._inFlightResourceMap.delete(e),this._resourceMap.set(e,t),{width:t.width,height:t.height})),(()=>({width:0,height:0}))))}deleteResource(e){this._inFlightResourceMap.delete(e),this._resourceMap.delete(e)}}async function h(i,s){const n=window.URL.createObjectURL(i);try{const{data:t}=await e(n,{...s,responseType:"image"});return t}catch(a){if(!r(a))throw new t("mapview-invalid-resource",`Could not fetch requested resource at ${n}`);throw a}finally{window.URL.revokeObjectURL(n)}}async function c(e,t){const{arrayBuffer:r,mediaType:o}=await u(e,t),c="image/png"===o;if("image/gif"===o&&n(r))return a(r);if(c&&i(r))return s(r,t);return h(new Blob([r],{type:o}),t)}async function u(i,s){let n;const a=";base64,";if(i.includes(a)){const e=i.indexOf(a),t=i.indexOf(a)+a.length,r=i.substring(t),s=atob(r),o=new Uint8Array(s.length);for(let i=0;i<s.length;i++)o[i]=s.charCodeAt(i);n={arrayBuffer:o.buffer,mediaType:i.substring(0,e).replace("data:","")}}else try{const t=await e(i,{responseType:"array-buffer",...s}),r=t.data;n={arrayBuffer:r,mediaType:t.getHeader("Content-Type")}}catch(o){if(!r(o))throw new t("mapview-invalid-resource",`Could not fetch requested resource at ${i}`)}return n}export{o as default};