UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 6.96 kB
import{_ as e}from"../../../../../chunks/tslib.es6.js";import"../../../../../core/has.js";import{isSome as t}from"../../../../../core/maybe.js";import{property as s}from"../../../../../core/accessorSupport/decorators/property.js";import"../../../../../core/accessorSupport/ensureType.js";import"../../../../../core/arrayUtils.js";import{subclass as r}from"../../../../../core/accessorSupport/decorators/subclass.js";import{r as i}from"../../../../../chunks/rbush.js";import{convertFromFeature as o,quantizeOptimizedGeometry as n,quantizeX as a,quantizeY as c}from"../../../../../layers/graphics/featureConversionUtils.js";import d from"../../../../../layers/graphics/OptimizedGeometry.js";import{StreamFeatureManager as h}from"../../../../../layers/graphics/data/StreamFeatureManager.js";import{createConnection as u}from"../../../../../layers/graphics/sources/connections/createConnection.js";import{DataTileSource as p}from"./DataTileSource.js";import{FeatureSetReaderJSON as l}from"../support/FeatureSetReaderJSON.js";import{UpdateToken as m}from"../support/UpdateToken.js";const _=2500;function y(e,s){const r=e.weakClone();if(t(e.geometry)){const t=a(s,e.geometry.coords[0]),i=c(s,e.geometry.coords[1]);r.geometry=new d([],[t,i])}return r}function g(e){return"esriGeometryPoint"===e?y:(t,s)=>{const r=t.weakClone(),i=new d,o=!1,a=!1,c=n(i,t.geometry,o,a,e,s,!1,!1);return r.geometry=c,r}}function f(e){return"esriGeometryPoint"===e?e=>t(e.geometry)?{minX:e.geometry.coords[0],minY:e.geometry.coords[1],maxX:e.geometry.coords[0],maxY:e.geometry.coords[1]}:{minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}:e=>{let s=1/0,r=1/0,i=-1/0,o=-1/0;return t(e.geometry)&&e.geometry.forEachVertex(((e,t)=>{s=Math.min(s,e),r=Math.min(r,t),i=Math.max(i,e),o=Math.max(o,t)})),{minX:s,minY:r,maxX:i,maxY:o}}}function b(e,t){const s=i(9,f(t));return s.load(e),s}function v(e,t){return e.search({minX:t.bounds[0],minY:t.bounds[1],maxX:t.bounds[2],maxY:t.bounds[3]})}class I{constructor(e,t){this.onUpdate=e,this._geometryType=t,this._objectIdToFeature=new Map,this._index=null}get _features(){const e=[];return this._objectIdToFeature.forEach((t=>e.push(t))),e}add(e){this._objectIdToFeature.set(e.objectId,e),this._index=null}get(e){return this._objectIdToFeature.has(e)?this._objectIdToFeature.get(e):null}forEach(e){this._objectIdToFeature.forEach(e)}search(e){return this._index||(this._index=b(this._features,this._geometryType)),v(this._index,e)}clear(){this._index=null,this._objectIdToFeature.clear()}removeById(e){const t=this._objectIdToFeature.get(e);return t?(this._objectIdToFeature.delete(e),this._index=null,t):null}update(e,t){this.onUpdate(e,t)}get size(){return this._objectIdToFeature.size}}let T=class extends p{constructor(e){super(e),this.type="stream",this._updateIntervalId=0,this._level=0,this._updateInfo={websocket:0,client:0},this._isPaused=!1,this._inUpdate=!1;const{outSR:t}=e,{geometryType:s,objectIdField:r,timeInfo:i,purgeOptions:o,source:n,spatialReference:a,serviceFilter:c,maxReconnectionAttempts:d,maxReconnectionInterval:p,updateInterval:l,customParameters:m,enabledEventTypes:y}=e.serviceInfo,f=new I(this._onUpdate.bind(this),s),b=new h(f,r,i,o),v=u(n,a,t,s,c,d,p,m??{});this._store=f,this._manager=b,this._connection=v,this._quantize=g(s),this._enabledEventTypes=new Set(y),this._handles=[this._connection.on("data-received",(e=>this._onFeature(e))),this._connection.on("message-received",(e=>this._onWebSocketMessage(e)))],this._initUpdateInterval=()=>{let t=performance.now();this._updateIntervalId=setInterval((()=>{const s=performance.now(),r=s-t;if(r>_){t=s;const e=Math.round(this._updateInfo.client/(r/1e3)),i=Math.round(this._updateInfo.websocket/(r/1e3));this._updateInfo.client=0,this._updateInfo.websocket=0,this.events.emit("updateRate",{client:e,websocket:i})}e.canAcceptRequest()&&!this._inUpdate&&this._manager.checkForUpdates()}),l)},this._initUpdateInterval()}destroy(){super.destroy(),this._clearUpdateInterval(),this._handles.forEach((e=>e.remove())),this._connection.destroy()}_fetchDataTile(){}get connectionStatus(){return this._isPaused?"paused":this._connection?.connectionStatus}get errorString(){return this._connection?.errorString}updateCustomParameters(e){this._connection.updateCustomParameters(e)}pauseStream(){this._isPaused||(this._isPaused=!0,this._clearUpdateInterval())}resumeStream(){this._isPaused&&(this._isPaused=!1,this._initUpdateInterval())}sendMessageToSocket(e){this._connection.sendMessageToSocket(e)}sendMessageToClient(e){this._connection.sendMessageToClient(e)}enableEvent(e,t){t?this._enabledEventTypes.add(e):this._enabledEventTypes.delete(e)}get updating(){return!1}subscribe(e,t){super.subscribe(e,t);const s=this._subscriptions.get(e.id);this._level=e.level;const r=this._getTileFeatures(e);this._onMessage({type:"append",id:e.key.id,addOrUpdate:r,end:!0}),s.didSend=!0}unsubscribe(e){super.unsubscribe(e)}*readers(e){const t=this._subscriptions.get(e),{tile:s}=t;yield this._getTileFeatures(s)}createTileQuery(e){throw new Error("Service does not support tile queries")}async resend(){this._subscriptions.forEach((e=>{const{tile:t}=e,s={type:"append",id:t.id,addOrUpdate:this._getTileFeatures(t),end:!0};this._onMessage(s)}))}_getTileFeatures(e){const t=this._store.search(e).map((t=>this._quantize(t,e.transform)));return l.fromOptimizedFeatures(t,this._serviceInfo,e.transform)}_onWebSocketMessage(e){if(this._enabledEventTypes.has("message-received")&&this.events.emit("message-received",e),"type"in e)switch(e.type){case"delete":if(e.objectIds)for(const t of e.objectIds)this._manager.removeById(t);if(e.trackIds)for(const t of e.trackIds)this._manager.removeByTrackId(t);break;case"clear":this._store.forEach((e=>this._manager.removeById(e.objectId)))}}_onFeature(e){this._updateInfo.websocket++;try{this._enabledEventTypes.has("data-received")&&this.events.emit("data-received",e);const t=o(e,this._serviceInfo.geometryType,!1,!1,this._serviceInfo.objectIdField);this._manager.add(t)}catch(t){}}_clearUpdateInterval(){clearInterval(this._updateIntervalId),this._updateIntervalId=0}async _onUpdate(e,s){this._inUpdate=!0;try{t(e)&&(this._updateInfo.client+=e.length),this._subscriptions.forEach(((e,t)=>{e.didSend&&e.tile.level===this._level&&this._onMessage({type:"append",id:t,addOrUpdate:null,clear:!0,end:!1})}));const s=[];this._subscriptions.forEach(((e,t)=>{if(!e.didSend||e.tile.level!==this._level)return;const r=e.tile,i={type:"append",id:t,addOrUpdate:this._getTileFeatures(r),remove:[],end:!1,status:m.empty()};e.requests.stream.enqueue(i),s.push(this._onMessage(i))})),await Promise.all(s),this._subscriptions.forEach(((e,t)=>{e.didSend&&e.tile.level===this._level&&this._onMessage({type:"append",id:t,addOrUpdate:null,end:!0})}))}catch{}this._inUpdate=!1}};e([s()],T.prototype,"_isPaused",void 0),e([s()],T.prototype,"connectionStatus",null),e([s()],T.prototype,"errorString",null),T=e([r("esri.views.2d.layers.features.sources")],T);export{T as StreamSource};