UNPKG

@arcgis/core

Version:

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

3 lines (2 loc) 5.64 kB
/* COPYRIGHT Esri - https://js.arcgis.com/5.0.8/LICENSE.txt */ import{__decorate as e}from"tslib";import t from"../../../../Graphic.js";import s from"../../../../core/Accessor.js";import{makeHandle as i}from"../../../../core/handleUtils.js";import{destroyMaybe as r}from"../../../../core/maybe.js";import{EsriPromiseMixin as o}from"../../../../core/Promise.js";import{watch as a,initial as n}from"../../../../core/reactiveUtils.js";import{property as c,subclass as d}from"../../../../core/accessorSupport/decorators.js";import{featureGeometryTypeKebabDictionary as h}from"../../../../geometry/support/typeUtils.js";import{StreamFeatureManager as p}from"../../../../layers/graphics/data/StreamFeatureManager.js";import{createConnection as l}from"../../../../layers/graphics/sources/connections/createConnection.js";import{EventedSet as u}from"../../support/EventedSet.js";import{TaskPriority as m}from"../../../support/Scheduler.js";import{Yield as y}from"../../../support/Yield.js";const g=2500;let f=class extends t{constructor(e){super(e)}getObjectId(){return this.objectId}};e([c({type:Number,json:{read:!0}})],f.prototype,"objectId",void 0),f=e([d("esri.views.3d.layers.graphics.StreamController.StreamGraphic")],f);class _{constructor(e){this.onUpdate=e,this._idToGraphic=new Map}destroy(){this._idToGraphic.clear()}add(e){this._idToGraphic.set(e.objectId,e)}get(e){return this._idToGraphic.get(e)}forEach(e){this._idToGraphic.forEach(e)}removeById(e){const t=this._idToGraphic.get(e);return t?(t.sourceLayer=t.layer=t.origin=null,this._idToGraphic.delete(e),t):null}update(e,t){this.onUpdate(e,t)}get size(){return this._idToGraphic.size}}const v=Symbol("startup-watches"),w=Symbol("update-interval");let R=class extends(o(s)){constructor(){super(...arguments),this.isPaused=!1,this.graphics=new u,this._updateInfo={websocket:0,client:0},this._updateRequested=!1,this._lastUpdateRateTime=0,this._outSpatialReference=null}initialize(){this.addResolvingPromise(this.layer.when(()=>this._startup()))}destroy(){this.clear()}clear(){this._shutdown(),this.graphics.clear()}get updating(){return!this.connection||this.readyToRun}get readyToRun(){return!this.isPaused&&null!=this.connection&&this._updateRequested}get _graphicOrigin(){return this.layer.graphicOrigin}runTask(){if(this._updateRequested=!1,this._emitUpdateRate(),!this.featuresManager.checkForUpdates())return y}_emitUpdateRate(){const e=performance.now(),t=e-this._lastUpdateRateTime;if(t<g)return;this._lastUpdateRateTime=e;const s=Math.round(this._updateInfo.client/(t/1e3)),i=Math.round(this._updateInfo.websocket/(t/1e3));this._updateInfo.client=0,this._updateInfo.websocket=0,this.layerView.emit("update-rate",{client:s,websocket:i})}_shutdown(){this.connection=r(this.connection),this.store=r(this.store),this.removeAllHandles()}_startup(){const{layer:e,layerView:t}=this,{spatialReference:s,definitionExpression:r,geometryDefinition:o,timeInfo:c,purgeOptions:d,maxReconnectionAttempts:u,maxReconnectionInterval:y,customParameters:g}=e,f=e.geometryType?h.toJSON(e.geometryType):null,R=s,I=t.view.spatialReference,b={type:"object-id",fieldName:e.objectIdField};this.clear(),this._set("connection",l(e.parsedUrl,R,I,f,r,o,u,y,g??void 0)),this._outSpatialReference=I.toJSON(),this.store=new _(this._onUpdate.bind(this)),this.featuresManager=new p(this.store,b,c.toJSON(),d),this.removeHandles(v),this.removeHandles(w),this.addHandles([e.on("send-message-to-socket",e=>this.connection.sendMessageToSocket(e)),e.on("send-message-to-client",e=>this.connection.sendMessageToClient(e)),this.connection.on("data-received",e=>this._onFeature(e)),this.connection.on("message-received",e=>this._onWebSocketMessage(e)),a(()=>[e.definitionExpression,e.geometryDefinition,e.purgeOptions],()=>this._startup()),a(()=>this.isPaused?null:this.layer.updateInterval,e=>{if(this.removeHandles(w),null==e)return;const t=setInterval(()=>{this._updateRequested=!0},e);this.addHandles(i(()=>clearInterval(t)),w)},n),this.layerView.view.resourceController.scheduler.registerTask(m.STREAM_CONTROLLER,this)],v),this._updateRequested=!0}_onWebSocketMessage(e){if(this.layerView.emit("message-received",e),"type"in e)switch(e.type){case"delete":if(e.objectIds)for(const t of e.objectIds)this.featuresManager.removeById(t);if(e.trackIds)for(const t of e.trackIds)this.featuresManager.removeByTrackId(t);break;case"clear":this.store.forEach(e=>this.featuresManager.removeById(e.objectId))}}_onFeature(e){this._updateInfo.websocket++,this.layerView.hasEventListener("data-received")&&this.layerView.emit("data-received",{attributes:e.attributes,centroid:e.centroid,geometry:e.geometry});try{null==e.geometry||e.geometry.spatialReference||(e.geometry.spatialReference=this._outSpatialReference);const t=f.fromJSON(e);t.sourceLayer=t.layer=this.layer,t.origin=this._graphicOrigin,this.featuresManager.add(t)}catch{}}_onUpdate(e,t){null!=t&&this.graphics.removeMany(t),null!=e&&(this._updateInfo.client+=e.length,this.graphics.addMany(e))}pauseStream(){this.isPaused=!0}resumeStream(){this.isPaused=!1}disconnect(){this._shutdown()}connect(){null==this.connection&&this._startup()}clearGraphics(){this.graphics.clear()}};e([c()],R.prototype,"isPaused",void 0),e([c({constructOnly:!0})],R.prototype,"layer",void 0),e([c({constructOnly:!0})],R.prototype,"layerView",void 0),e([c()],R.prototype,"connection",void 0),e([c({readOnly:!0})],R.prototype,"updating",null),e([c()],R.prototype,"readyToRun",null),e([c()],R.prototype,"_updateRequested",void 0),e([c()],R.prototype,"_graphicOrigin",null),R=e([d("esri.views.3d.layers.graphics.StreamController")],R);export{R as StreamController,f as StreamGraphic};