/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{createResolver as e,throwIfAborted as t,onAbortOrThrow as s}from"../../../../core/promiseUtils.js";import{textureUploadManagerChunkSize as o,textureUploadManagerBudget as u}from"./definitions.js";class r{constructor(e){this._queue=[],this._refreshable=e}destroy(){this._queue=[]}enqueueTextureUpdate(u,r){const n=e(),h=u,i=o,a=Math.ceil(h.height/i);t(r);for(let e=0;e<a;e++){const t=e*i,s=e===a-1,o=s?h.height-i*e:i;this._queue.push({type:"chunk",request:u,resolver:n,chunk:e,chunkOffset:t,destHeight:o,chunkIsLast:s,options:r})}return s(r,e=>n.reject(e)),n.promise}upload(){const e=performance.now();let t=0;for(;this._queue.length;){const s=this._queue.shift();if(s){if(null!=s.options.signal&&s.options.signal.aborted)continue;switch(s.type){case"chunk":this._uploadChunk(s);break;case"no-chunk":this._uploadNoChunk(s)}++t;const o=performance.now()-e;if(o+o/t>=u)break}}this._queue.length&&this._refreshable.requestRender()}_uploadChunk(e){const{request:t,resolver:s,chunkOffset:o,chunkIsLast:u,destHeight:r}=e,{data:n,texture:h,width:i}=t;null!=n&&(h.updateData(0,0,o,i,r,n,o),u&&s.resolve())}_uploadNoChunk(e){const{request:t,resolver:s}=e,{data:o,texture:u}=t;u.setData(o),s.resolve()}}export{r as TextureUploadManager};
