UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 5.22 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import{_ as e}from"../../../chunks/tslib.es6.js";import t from"../../../Graphic.js";import s from"../../../core/Accessor.js";import{destroyMaybe as r}from"../../../core/maybe.js";import o from"../../../core/Promise.js";import{watch as i}from"../../../core/reactiveUtils.js";import{property as a}from"../../../core/accessorSupport/decorators/property.js";import"../../../core/has.js";import"../../../core/Logger.js";import"../../../core/RandomLCG.js";import{subclass as n}from"../../../core/accessorSupport/decorators/subclass.js";import{featureGeometryTypeKebabDictionary as c}from"../../../geometry/support/typeUtils.js";import{StreamFeatureManager as h}from"../data/StreamFeatureManager.js";import{createConnection as d}from"../sources/connections/createConnection.js";import{EventedSet as l}from"../../../views/3d/support/EventedSet.js";const p=2500;let u=class extends t{constructor(e){super(e)}getObjectId(){return this.objectId}};e([a({type:Number,json:{read:!0}})],u.prototype,"objectId",void 0),u=e([n("esri.layers.graphics.controllers.StreamController.StreamGraphic")],u);class m{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=null,this._idToGraphic.delete(e),t):null}update(e,t){this.onUpdate(e,t)}get size(){return this._idToGraphic.size}}let y=class extends(o.EsriPromiseMixin(s)){constructor(){super(...arguments),this.isPaused=!1,this.graphics=new l,this._updateInfo={websocket:0,client:0},this._updateIntervalId=null,this._outSpatialReference=null}initialize(){this.addResolvingPromise(this.layer.when((()=>this._startup())))}destroy(){this.clear()}_clearInterval(){null!==this._updateIntervalId&&(clearInterval(this._updateIntervalId),this._updateIntervalId=null)}clear(){this._shutdown(),this.graphics.clear()}get updating(){return!this.connection||"connected"===this.connection.connectionStatus}_shutdown(){this._clearInterval(),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,objectIdField:a,timeInfo:n,purgeOptions:l,maxReconnectionAttempts:p,maxReconnectionInterval:u,customParameters:y}=e,f=e.geometryType?c.toJSON(e.geometryType):null,g=s,_=t.view.spatialReference;this.clear(),this._set("connection",d(e.parsedUrl,g,_,f,r,o,p,u,y??void 0)),this._outSpatialReference=_.toJSON(),this.store=new m(this._onUpdate.bind(this)),this.featuresManager=new h(this.store,a,n.toJSON(),l);const I="startup-watches";this.removeHandles(I),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))),i((()=>[e.definitionExpression,e.geometryDefinition,e.purgeOptions]),(()=>this._startup()))],I),this.isPaused||this._initUpdateInterval()}_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=u.fromJSON(e);t.sourceLayer=t.layer=this.layer,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))}_initUpdateInterval(){this._clearInterval();const{updateInterval:e}=this.layer;let t=performance.now();this._updateIntervalId=setInterval((()=>{const e=performance.now(),s=e-t;if(s>p){t=e;const r=Math.round(this._updateInfo.client/(s/1e3)),o=Math.round(this._updateInfo.websocket/(s/1e3));this._updateInfo.client=0,this._updateInfo.websocket=0,this.layerView.emit("update-rate",{client:r,websocket:o})}this.featuresManager.checkForUpdates()}),e)}pauseStream(){this.isPaused=!0,this._clearInterval()}resumeStream(){this.isPaused=!1,this._initUpdateInterval()}disconnect(){this._shutdown()}connect(){null==this.connection&&this._startup()}clearGraphics(){this.graphics.clear()}};e([a()],y.prototype,"isPaused",void 0),e([a({constructOnly:!0})],y.prototype,"layer",void 0),e([a({constructOnly:!0})],y.prototype,"layerView",void 0),e([a()],y.prototype,"connection",void 0),e([a({readOnly:!0})],y.prototype,"updating",null),y=e([n("esri.layers.graphics.controllers.StreamController")],y);export{y as StreamController,u as StreamGraphic};