UNPKG

@arcgis/core

Version:

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

6 lines (5 loc) 3.58 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.32/esri/copyright.txt for details. */ import t from"../../../core/CircularArray.js";import{clamp as e}from"../../../core/mathUtils.js";import{defaultStreamIdField as s,esriTimestamp as i,internalTimeReceivedField as r}from"../../support/streamLayerUtils.js";const o=1e3;class d{constructor(t,e,i,r,o=128){this._trackIdToObservations=new Map,this._idCounter=0,this._lastPurge=performance.now(),this._addOrUpdated=new Map,this._removed=[],this._maxAge=0,this._timeInfo=i,this._purgeOptions=r,this.store=t,this.objectIdField=e,this.purgeInterval=o,this._useGeneratedIds=this.objectIdField===s}removeById(t){this._removed.push(t)}removeByTrackId(t){const e=this._trackIdToObservations.get(t);if(e)for(const s of e.entries)this._removed.push(s)}add(s){if(this._useGeneratedIds){const t=this._nextId();s.attributes[this.objectIdField]=t,s.objectId=t}else s.objectId=s.attributes[this.objectIdField];const i=s.objectId;if(this._addOrUpdated.set(i,s),this._maxAge=Math.max(this._maxAge,s.attributes[this._timeInfo.startTimeField]),!this._timeInfo.trackIdField)return null==this._trackIdLessObservations&&(this._trackIdLessObservations=new t(1e5)),void this._trackIdLessObservations.enqueue(i);const r=s.attributes[this._timeInfo.trackIdField];if(!this._trackIdToObservations.has(r)){const s=null!=this._purgeOptions?.maxObservations?this._purgeOptions.maxObservations:o,i=e(s,0,o);this._trackIdToObservations.set(r,new t(i))}const d=this._trackIdToObservations.get(r),a=d?.enqueue(i);null!=a&&(this._addOrUpdated.has(a)?this._addOrUpdated.delete(a):this._removed.push(a))}checkForUpdates(){const t=this._getToAdd(),e=this._getToRemove(),s=performance.now(),o=s-this._lastPurge,d=Date.now();o>=this.purgeInterval&&(this._purge(s),this._lastPurge=s);const a=[];if(null!=e)for(const i of e){const t=this.store.removeById(i);null!=t&&a.push(t)}const n=[];if(null!=t){const o=new Set(e??[]);for(const e of t)o.has(e.objectId)||(e.attributes[i]=s,e.attributes[r]=d,this.store.add(e),n.push(e))}return!(!n.length&&!a?.length)&&(this.store.update(n,a),!0)}_getToAdd(){if(!this._addOrUpdated.size)return null;const t=new Array(this._addOrUpdated.size);let e=0;return this._addOrUpdated.forEach((s=>t[e++]=s)),this._addOrUpdated.clear(),t}_getToRemove(){const t=this._removed;return this._removed.length?(this._removed=[],t):null}_nextId(){const t=this._idCounter;return this._idCounter=(this._idCounter+1)%4294967294+1,t}_purge(t){const e=this._purgeOptions;null!=e&&(this._purgeSomeByDisplayCount(e),this._purgeByAge(e),this._purgeByAgeReceived(t,e),this._purgeTracks())}_purgeSomeByDisplayCount(t){if(!t.displayCount)return;let e=this.store.size;if(e>t.displayCount){if(this._timeInfo.trackIdField)for(const s of this._trackIdToObservations.values())if(e>t.displayCount&&s.size){const t=s.dequeue();this._removed.push(t),e--}if(null!=this._trackIdLessObservations){let s=e-t.displayCount;for(;s-- >0;){const t=this._trackIdLessObservations.dequeue();null!=t&&this._removed.push(t)}}}}_purgeByAge(t){const e=this._timeInfo?.startTimeField;if(!t.age||!e)return;const s=60*t.age*1e3,i=this._maxAge-s;this.store.forEach((t=>{t.attributes[e]<i&&this._removed.push(t.objectId)}))}_purgeByAgeReceived(t,e){if(!e.ageReceived)return;const s=t-60*e.ageReceived*1e3;this.store.forEach((t=>{t.attributes[i]<s&&this._removed.push(t.objectId)}))}_purgeTracks(){this._trackIdToObservations.forEach(((t,e)=>{0===t.size&&this._trackIdToObservations.delete(e)}))}}export{d as StreamFeatureManager};