@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 7.15 kB
JavaScript
import e from"../../../../../../core/Error.js";import t from"../../../../../../core/Logger.js";import{isAbortError as r}from"../../../../../../core/promiseUtils.js";import{errorPointSymbolJSON as s,errorPolylineSymbolJSON as i,errorPolygonSymbolJSON as a}from"../../../../../../symbols/support/defaultsJSON.js";import{WGLSymbologyType as o,WGLGeometryType as l}from"../../enums.js";import{createMaterialKey as m}from"../../materialKey/MaterialKey.js";import c from"./WGLDynamicFillTemplate.js";import n from"./WGLDynamicLineTemplate.js";import h from"./WGLDynamicMarkerTemplate.js";import p from"./WGLDynamicTextTemplate.js";import u from"./WGLFillTemplate.js";import _ from"./WGLLineTemplate.js";import f from"./WGLMarkerTemplate.js";import M from"./WGLTextTemplate.js";import{Lock as y,withLock as T}from"../../util/Lock.js";import{ok as I}from"../../util/Result.js";import{codepoints as g}from"../../../../layers/features/textUtils.js";import{cimLayerToRasterizationInfo as d}from"../../../../layers/support/cimSymbolUtils.js";const S=t.getLogger("esri.views.2d.engine.webgl.mesh.templates.WGLTemplateStore"),w={sortKey:null,templates:new Array},k={isOutline:!1,placement:null,symbologyType:o.DEFAULT,vvFlags:0},C={...s,hash:JSON.stringify(s),materialKey:m(l.MARKER,k)},L={...i,hash:JSON.stringify(i),materialKey:m(l.LINE,k)},b={...a,hash:JSON.stringify(a),materialKey:m(l.FILL,k)};function E(e,t){const r=e.length;return e.push(null),t.then((t=>e[r]=t)),e}function G(e){return null!=e&&!!(1&e)}function j(e){return"worker:port-closed"===e.name}class x{constructor(e,t){this._idCounter=1,this._templateIdCounter=1,this._idToTemplateGroup=new Map,this._symbolToTemplate=new Map,this._fetchQueue=[],this._idToResolver=new Map,this._cimTemplateCache=new Map,this._cimAnalyses=[],this._lock=new y,this._fetchResource=e,this._tileInfo=t}get _markerError(){return this._errorTemplates.marker[0]}get _fillError(){return this._errorTemplates.fill[0]}get _lineError(){return this._errorTemplates.line[0]}get _textError(){return this._errorTemplates.line[0]}createTemplateGroup(e,t,r=null){this._initErrorTemplates();const s=e.hash,i=this._symbolToTemplate.get(s);if(null!=i)return i;const a=new Array,o={sortKey:r,templates:a};t&&this._createMeshTemplates(a,t,!0),this._createMeshTemplates(a,e,!1);const l=this._createGroupId("expanded-cim"===e.type&&F(e));return this._idToTemplateGroup.set(l,o),this._symbolToTemplate.set(s,l),l}getTemplateGroup(e){return this._idToTemplateGroup.get(e)??w}getDynamicTemplateGroup(e){return this._idToTemplateGroup.has(e)?(G(e)||S.error("mapview-template-store",`Id ${e} does not refer to a dynamic template`),this._idToTemplateGroup.get(e)):w}getMosaicItem(e,t){const r=this._createTemplateId(),s=new Promise((e=>this._idToResolver.set(r,e)));return this._fetchQueue.push({symbol:e,id:r,glyphIds:t}),s}finalize(e){return this._fetchQueue.length||this._lock.isHeld()?T(this._lock,this._fetchAllQueuedResources.bind(this),e):Promise.resolve()}_initErrorTemplates(){this._errorTemplates||(this._errorTemplates={fill:this._createMeshTemplates([],b,!1),marker:this._createMeshTemplates([],C,!1),line:this._createMeshTemplates([],L,!1)})}_fetchAllQueuedResources(t){if(!this._fetchQueue.length)return Promise.resolve();const s=this._fetchQueue,i=this._cimAnalyses;return this._fetchQueue=[],this._cimAnalyses=[],Promise.all(i).then((()=>this._fetchResource(s,t).then((e=>{for(const{id:t,mosaicItem:r}of e){this._idToResolver.get(t)(r),this._idToResolver.delete(t)}})))).catch((t=>{r(t)?this._fetchQueue=this._fetchQueue.concat(s):j(t)||S.error(new e("mapview-template-store","Unable to fetch requested texture resources",t))}))}_createGroupId(e){return this._idCounter++<<1|(e?1:0)}_createTemplateId(){return this._templateIdCounter++}async _createSMS(e){const{spriteMosaicItem:t}=await this.getMosaicItem(e);return I(t,S)?f.fromSimpleMarker(e,t):this._markerError}async _createPMS(e){const{spriteMosaicItem:t}=await this.getMosaicItem(e);return I(t,S)?f.fromPictureMarker(e,t):this._markerError}async _createSFS(e,t){const{spriteMosaicItem:r}=await this.getMosaicItem(e);return I(r,S)?u.fromSimpleFill(e,r,t):this._fillError}async _createPFS(e,t){const{spriteMosaicItem:r}=await this.getMosaicItem(e);return I(r,S)?u.fromPictureFill(e,r,t):this._fillError}async _createSLS(e,t){const{spriteMosaicItem:r}=await this.getMosaicItem(e);return I(r,S)?_.fromSimpleLine(e,r):this._lineError}async _createLMS(e){const{spriteMosaicItem:t}=await this.getMosaicItem(e);return I(t,S)?f.fromLineSymbolMarker(e,t):this._markerError}async _createTS(e){const{glyphMosaicItems:t}=await this.getMosaicItem(e);return M.fromText(e,t??[])}async _createCIMText(e){const{glyphMosaicItems:t}=await this.getMosaicItem(d(e),g(e.text));return I(t,S)?M.fromCIMText(e,t,this._tileInfo):this._textError}async _createCIMFill(e){const{spriteMosaicItem:t}=await this.getMosaicItem(d(e));return I(t,S)?u.fromCIMFill(e,t,this._tileInfo):this._fillError}async _createCIMLine(e){const{spriteMosaicItem:t}=await this.getMosaicItem(d(e));return I(t,S)?_.fromCIMLine(e,t,this._tileInfo):this._lineError}async _createCIMMarker(e){const{spriteMosaicItem:t}=await this.getMosaicItem(d(e));return I(t,S)?f.fromCIMMarker(e,t,this._tileInfo):this._markerError}async _createCIM(e){const t=e.templateHash;let r=this._cimTemplateCache.get(t);if(null!=r)return r;switch(e.type){case"marker":r=await this._createCIMMarker(e);break;case"line":r=await this._createCIMLine(e);break;case"fill":r=await this._createCIMFill(e);break;case"text":r=await this._createCIMText(e)}return this._cimTemplateCache.set(t,r),r}async _createDynamicCIM(e){const t=e.templateHash;let r=this._cimTemplateCache.get(t);if(null!=r)return r;switch(e.type){case"marker":r=h.fromCIMMarker(e,this._tileInfo);break;case"line":r=n.fromCIMLine(e,this._tileInfo);break;case"fill":r=c.fromCIMFill(e,this._tileInfo);break;case"text":r=p.fromCIMText(e,this._tileInfo)}return this._cimTemplateCache.set(t,r),r}_createPrimitiveMeshTemplates(e,t,r){switch(t.type){case"esriSMS":return E(e,this._createSMS(t));case"esriPMS":return E(e,this._createPMS(t));case"esriSFS":return E(e,this._createSFS(t,r));case"line-marker":return E(e,this._createLMS(t));case"esriPFS":return E(e,this._createPFS(t,r));case"esriSLS":return E(e,this._createSLS(t,!1));case"esriTS":return E(e,this._createTS(t));default:return S.error("Unable to create mesh template for unknown symbol type {: $ }{symbol.type}"),e}}_createMeshTemplates(e,t,r){if(t.type.includes("3d"))return S.error("3D symbols are not supported with MapView"),e;if("expanded-cim"===t.type){for(const r of t.layers)"function"==typeof r.materialHash?E(e,this._createDynamicCIM(r)):E(e,this._createCIM(r));return e}if("composite-symbol"===t.type){for(const s of t.layers)this._createPrimitiveMeshTemplates(e,s,r);return e}return"cim"===t.type||"label"===t.type||"web-style"===t.type?e:this._createPrimitiveMeshTemplates(e,t,r)}}const F=e=>{if(!e.layers)return!1;for(const t of e.layers)if("function"==typeof t.materialHash)return!0;return!1};export{x as WGLTemplateStore,C as errorPointSchema2D,b as errorPolygonSchema2D,L as errorPolylineSchema2D,G as isDynamicId};