/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{isPromiseLike as t}from"../../core/promiseUtils.js";import{when as e,watch as s}from"../../core/reactiveUtils.js";import{addFrameTask as a}from"../../core/scheduling.js";import{signal as i}from"../../core/signal.js";import r from"./PaddedViewState.js";import{newScheduler as d}from"../support/Scheduler.js";class h{constructor(s){this.view=s,this._updating=i(!1),this._stationaryHandle=null,this._frameTaskHandle=null,this._updateParameters=null,this._updateRequested=!1,this._scheduler=d(),this._schedulerHandle=e(()=>this._scheduler.updating,()=>this.requestFrame()),this.stationary=!0,this.prepare=()=>{this._updateParameters&&(this._updateParameters.state=this.view.state,this._updateParameters.stationary=this.view.stationary,this._updateParameters.pixelRatio=window.devicePixelRatio,this._updateParameters.renderingOptions=this.view.renderingOptions,this._updateParameters.targetState.copy(this.view.state),null==this.view.animation?.target||t(this.view.animation.target)||(this._updateParameters.targetState.viewpoint=this.view.animation.target))},this.update=t=>{if(this._updateRequested=!1,this.view?.destroyed)return void(this._updating.value=!1);const{allLayerViews:e,graphicsView:s,state:{id:a}}=this.view;e?.forEach(this._updateLayerView,this),null!=s&&(s.lastUpdateId!==a&&(s.viewChange(),s.lastUpdateId=a),s.updateRequested&&s.processUpdate(this._updateParameters)),this.view.graphicsTileStore?.setViewState(this._updateParameters.state),this.view.animation?this._scheduler.state=0:this.view.interacting?this._scheduler.state=1:this._scheduler.state=2;const i=this._scheduler.frame(t);this._updating.value=this._updateRequested,i||this._updateRequested||2!==this._scheduler.state||this._frameTaskHandle?.pause()}}destroy(){this.stop(),this._schedulerHandle.remove(),this._scheduler.destroy()}get scheduler(){return this._scheduler}get updating(){return this._updating.value}start(){if(this._frameTaskHandle)return;const t=this.view;this.stationary=t.stationary,this._updateParameters={state:t.state,targetState:new r,pixelRatio:window.devicePixelRatio,stationary:this.stationary,renderingOptions:t.renderingOptions},this._stationaryHandle=s(()=>t.stationary,t=>{this.stationary=t,this.requestUpdate()}),this._frameTaskHandle=a(this),this.requestUpdate()}stop(){this._frameTaskHandle&&(this._updateRequested=!1,this._stationaryHandle?.remove(),this._frameTaskHandle.remove(),this._updateParameters=this._stationaryHandle=this._frameTaskHandle=null,this.stationary=!0)}requestUpdate(){this._updateRequested||(this._updateRequested=!0,this._updating.value=!0,this.requestFrame())}requestFrame(){this._frameTaskHandle&&this._frameTaskHandle.resume()}_updateLayerView(t){if(!t.attached)return void(t.spatialReferenceSupported&&this.requestUpdate());const e=this.view.state,s=t.lastUpdateId;null!=s&&(this.stationary||t.moving)||(t.moving=!0),s!==e.id&&t.viewChange(),this.stationary&&t.moving&&(t.moving=!1,t.moveEnd()),t.lastUpdateId=e.id,t.updateRequested&&t.processUpdate(this._updateParameters),"layerViews"in t&&t.layerViews?.forEach(this._updateLayerView,this)}}export{h as default};
