UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 4.87 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import{_ as e}from"../../chunks/tslib.es6.js";import t from"../../request.js";import i from"../../core/Accessor.js";import{ByteSizeUnit as r}from"../../core/ByteSizeUnit.js";import s from"../../core/Error.js";import{makeHandle as l}from"../../core/handleUtils.js";import"../../core/has.js";import{LRUCache as o}from"../../core/LRUCache.js";import a from"../../core/PooledArray.js";import{isAbortError as n,isAborted as c,onAbort as h,createAbortError as p}from"../../core/promiseUtils.js";import{watch as m,initial as f}from"../../core/reactiveUtils.js";import{waitTicks as u}from"../../core/scheduling.js";import{objectToQuery as v}from"../../core/urlUtils.js";import{property as y}from"../../core/accessorSupport/decorators/property.js";import"../../core/Logger.js";import"../../core/RandomLCG.js";import{subclass as d}from"../../core/accessorSupport/decorators/subclass.js";import{TileKey as _}from"./TileKey.js";import{Tilemap as b,tilemapDefinitionId as g}from"./Tilemap.js";var j;function w(e,t,i){return new s("tile-map:tile-unavailable","Tile is not available",{level:e,row:t,col:i})}let T=j=class extends i{constructor(e){super(e),this._pendingTilemapRequests={},this.request=t,this.size=32,this._prefetchingEnabled=!0}initialize(){this._tilemapCache=new o(2*r.MEGABYTES),this.addHandles(m((()=>{const{layer:e}=this;return[e?.parsedUrl,e?.tileServers,e?.apiKey,e?.customParameters]}),(()=>this._initializeTilemapDefinition()),f))}get effectiveMinLOD(){return this.minLOD??this.layer.tileInfo.lods[0].level}get effectiveMaxLOD(){return this.maxLOD??this.layer.tileInfo.lods[this.layer.tileInfo.lods.length-1].level}getAvailability(e,t,i){if(!this.layer.tileInfo.lodAt(e)||e<this.effectiveMinLOD||e>this.effectiveMaxLOD)return"unavailable";const r=this._tilemapFromCache(e,t,i,this._tmpTilemapDefinition);return r?r.getAvailability(t,i):"unknown"}fetchAvailability(e,t,i,r){return!this.layer.tileInfo.lodAt(e)||e<this.effectiveMinLOD||e>this.effectiveMaxLOD?Promise.reject(w(e,t,i)):this._fetchTilemap(e,t,i,r).catch((e=>e)).then((r=>{if(r instanceof b){const s=r.getAvailability(t,i);if("unavailable"===s)throw w(e,t,i);return s}if(n(r))throw r;return"unknown"}))}fetchAvailabilityUpsample(e,t,i,r,s){r.level=e,r.row=t,r.col=i;const l=this.layer.tileInfo;l.updateTileInfo(r);const o=this.fetchAvailability(e,t,i,s).catch((e=>{if(n(e))throw e;if(l.upsampleTile(r))return this.fetchAvailabilityUpsample(r.level,r.row,r.col,r,s);throw e}));return this._fetchAvailabilityUpsamplePrefetch(r.id,e,t,i,s,o),o}async _fetchAvailabilityUpsamplePrefetch(e,t,i,r,s,o){if(!this._prefetchingEnabled||null==e)return;const a=`prefetch-${e}`;if(this.hasHandles(a))return;const n=new AbortController;o.then((()=>n.abort()),(()=>n.abort()));let h=!1;const p=l((()=>{h||(h=!0,n.abort())}));if(this.addHandles(p,a),await u(10,n.signal).catch((()=>{})),h||(h=!0,this.removeHandles(a)),c(n))return;const m=new _(e,t,i,r),f={...s,signal:n.signal},v=this.layer.tileInfo;for(let l=0;j._prefetches.length<j._maxPrefetch&&v.upsampleTile(m);++l){const e=this.fetchAvailability(m.level,m.row,m.col,f);j._prefetches.push(e);const t=()=>{j._prefetches.removeUnordered(e)};e.then(t,t)}}_fetchTilemap(e,t,i,r){if(!this.layer.tileInfo.lodAt(e)||e<this.effectiveMinLOD||e>this.effectiveMaxLOD)return Promise.reject(new s("tilemap-cache:level-unavailable",`Level ${e} is unavailable in the service`));const l=this._tmpTilemapDefinition,o=this._tilemapFromCache(e,t,i,l);if(o)return Promise.resolve(o);const a=r?.signal;return r={...r,signal:null},new Promise(((e,t)=>{h(a,(()=>t(p())));const i=g(l);let s=this._pendingTilemapRequests[i];if(!s){s=b.fromDefinition(l,r).then((e=>(this._tilemapCache.put(i,e,e.byteSize),e)));const e=()=>{delete this._pendingTilemapRequests[i]};this._pendingTilemapRequests[i]=s,s.then(e,e)}s.then(e,t)}))}_initializeTilemapDefinition(){if(!this.layer.parsedUrl)return;const{parsedUrl:e,apiKey:t,customParameters:i}=this.layer;this._tilemapCache.clear(),this._tmpTilemapDefinition={service:{url:e.path,query:v({...e.query,...i,token:t??e.query?.token}),tileServers:this.layer.tileServers,request:this.request},width:this.size,height:this.size,level:0,row:0,col:0}}_tilemapFromCache(e,t,i,r){r.level=e,r.row=t-t%this.size,r.col=i-i%this.size;const s=g(r);return this._tilemapCache.get(s)}get test(){}};T._maxPrefetch=4,T._prefetches=new a({initialSize:j._maxPrefetch}),e([y({constructOnly:!0})],T.prototype,"layer",void 0),e([y({constructOnly:!0})],T.prototype,"minLOD",void 0),e([y({constructOnly:!0})],T.prototype,"maxLOD",void 0),e([y({constructOnly:!0})],T.prototype,"request",void 0),e([y({constructOnly:!0})],T.prototype,"size",void 0),T=j=e([d("esri.layers.support.TilemapCache")],T);export{T as TilemapCache};