UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 3.5 kB
import{_ as e}from"../../../../chunks/tslib.es6.js";import t from"../../../../core/Accessor.js";import r from"../../../../core/Evented.js";import s from"../../../../core/Logger.js";import{isNone as o,unwrapOr as i,isSome as n}from"../../../../core/maybe.js";import{isPromiseLike as u,isAbortError as a}from"../../../../core/promiseUtils.js";import{property as h}from"../../../../core/accessorSupport/decorators/property.js";import"../../../../core/accessorSupport/ensureType.js";import"../../../../core/arrayUtils.js";import{subclass as c}from"../../../../core/accessorSupport/decorators/subclass.js";import{RenderRequestType as p}from"./basicInterfaces.js";import{ContentObjectType as d}from"./ContentObjectType.js";import{TextureTechnique as l}from"./TextureTechnique.js";import{TextureTechniqueConfiguration as m}from"./TextureTechniqueConfiguration.js";import{assert as _}from"./Util.js";import{TaskPriority as f}from"../../../support/Scheduler.js";let g=class extends t{constructor(e,t,s){super({}),this._stage=e,this._techniqueRepository=t,this._rctx=s,this._textures=new Map,this._loadingCount=0,this._frameUpdates=new Map,this.events=new r,this._frameTask=e.view.resourceController.scheduler.registerTask(f.TEXTURE_UNLOAD)}normalizeCtorArgs(){return{}}destroy(){this._frameTask.remove(),this._stage.forEachOfType(d.Texture,(e=>e.unload()))}get updating(){return this._loadingCount>0||this._frameTask.updating}get textureTechnique(){return o(this._textureTechnique)&&(this._textureTechnique=this._techniqueRepository.acquire(l,new m)),this._textureTechnique}acquire(e){const t=this._textures.get(e);return t?(t.ref(),i(t.loadingPromise,t)):this._createNewRef(e)}update(){let e=!1;this._frameUpdates.forEach((t=>{const r=t.texture.frameUpdate(this._rctx,this.textureTechnique,t.previousToken);r>=0&&r!==t.previousToken&&(t.previousToken=r,e=!0)})),e&&this.events.emit("changed",p.BACKGROUND)}_createNewRef(e){const t=this._stage.getObject(e);if(o(t))return _(void 0!==t),null;const r=t.events.on("unloaded",(()=>{r.remove(),this._onTextureUnloaded(e)})),i=new T(e,(()=>{this._frameTask.schedule((()=>{i.isUnreferenced&&t.unload()}))}));return this._textures.set(e,i),i.ref(),n(t.glTexture)?(this._updateGLTexture(i,t.glTexture),t.requiresFrameUpdates&&this._frameUpdates.set(e,{texture:t,previousToken:-1}),i):(this._loadingCount++,i.loadingPromise=this._stage.schedule((()=>{const r=t.load(this._rctx,(()=>this.textureTechnique)),o=r=>(this._loadingCount--,i.loadingPromise=null,this._updateGLTexture(i,r),t.requiresFrameUpdates&&this._frameUpdates.set(e,{texture:t,previousToken:-1}),i),n=e=>(this._loadingCount--,i.loadingPromise=null,a(e)||s.getLogger(this.declaredClass).error(e),null);return u(r)?r.then(o,n):o(r)})),i.loadingPromise)}_updateGLTexture(e,t){e.glTexture=t,this.events.emit("changed",p.UPDATE)}_onTextureUnloaded(e){this._textures.delete(e),this._frameUpdates.delete(e)}};e([h()],g.prototype,"_loadingCount",void 0),e([h()],g.prototype,"_frameTask",void 0),e([h()],g.prototype,"updating",null),g=e([c("esri.views.3d.webgl-engine.lib.TextureRepository")],g);class T{constructor(e,t){this.id=e,this._release=t,this._refCount=0}get isUnreferenced(){return 0===this._refCount}ref(){++this._refCount}release(){--this._refCount,this._refCount>0||(0!==this._refCount?(s.getLogger("esri.views.3d.webgl-engine.lib.TextureRepository.RefCountedTextureImpl").error("Cannot dereference texture that has no references!"),this._refCount=0):this._release())}}export{g as TextureRepository};