@doegis/core
Version:
DOE GIS API
3 lines (1 loc) • 9.82 kB
JavaScript
import{_ as e}from"../../../../chunks/tslib.es6.js";import t from"../../../../request.js";import s from"../../../../core/Accessor.js";import{remove as i,splitIntoChunks as r}from"../../../../core/arrayUtils.js";import{resultOrAbort as a,result as n}from"../../../../core/asyncUtils.js";import{estimateStringByteSize as o,estimateNumberByteSize as c}from"../../../../core/byteSizeEstimations.js";import d from"../../../../core/Logger.js";import{isSome as h,unwrap as l,destroyMaybe as u,abortMaybe as m,isNone as g,get as p}from"../../../../core/maybe.js";import{whenOrAbort as _}from"../../../../core/promiseUtils.js";import{watch as y,initial as b}from"../../../../core/reactiveUtils.js";import{property as f}from"../../../../core/accessorSupport/decorators/property.js";import"../../../../core/accessorSupport/ensureType.js";import{subclass as O}from"../../../../core/accessorSupport/decorators/subclass.js";import{formatNumber as j}from"../../../../intl/number.js";import{getMaximumQuerySize as I,queryAllJSON as C}from"../../../../layers/support/featureQueryAll.js";import{enableDirect3DObjectFeatureLayerDisplay as v,sceneLayerEditingEnabled as L}from"../../../../support/featureFlags.js";import E from"../FeatureLayerView3D.js";let A=class extends s{constructor(e){super(e),this._warnMaximumChangedObjectsExceeded=!1,this._interactiveEditingSessions=null,this._maximumNumberOfEditOVerrides=S,this._original3DOFLDefinitionExpression=null,this._associatedLayerView=null,this._changedObjectIds=new Set,this._pendingFetchChangedObjectIds=null,this._pendingFetchAbortController=new AbortController}initialize(){this._memCache=this.memoryController.newCache(`${this.layer.uid}-attribute-overrides`),this._pendingFetchChangedObjectIds=this._fetchChangedObjectIds(this._pendingFetchAbortController?.signal),this._pendingFetchChangedObjectIds.then((()=>{this._pendingFetchAbortController=null,this._pendingFetchChangedObjectIds=null})),this.is3DOFL&&v()&&h(this._associatedLayer)&&this._associatedLayer.load().then((e=>{this.destroyed||(this._original3DOFLDefinitionExpression=e.definitionExpression,this.addHandles(y((()=>this._definitionExpression),(t=>e.definitionExpression=t),b)),this._associatedLayerView=new E({layer:this._associatedLayer,view:this.view}))}))}destroy(){this.is3DOFL&&v()&&h(this._associatedLayerView)&&h(this._associatedLayer)&&(this._associatedLayer.definitionExpression=l(this._original3DOFLDefinitionExpression)),this._set("layer",null),this._memCache=u(this._memCache),this._pendingFetchAbortController=m(this._pendingFetchAbortController),this._pendingFetchChangedObjectIds=null}get is3DOFL(){return L()&&h(this._associatedLayer)&&h(this._associatedLayer.infoFor3D)}get geometryChangedObjectIds(){return this.is3DOFL?[...this._changedObjectIds]:[]}get _associatedLayer(){return null==this.layer?null:this.layer.associatedLayer}get _definitionExpression(){const e=this.geometryChangedObjectIds;return 0===e.length?"1 = 0":`OBJECTID IN (${e.join(",")})`}get updating(){if(!this.is3DOFL)return!1;if(!v())return!1;return!h(this._associatedLayerView)||h(this._associatedLayerView)&&this._associatedLayerView.updating}get isEmpty(){return null==this._pendingFetchChangedObjectIds&&0===this._changedObjectIds.size}createInteractiveEditSession(e){this._changedObjectIds.add(e),this.notifyChange("_changedObjectIds"),g(this._interactiveEditingSessions)&&(this._interactiveEditingSessions=[]);const t=this._interactiveEditingSessions,s=new F(e,{rollback:()=>{i(t,s),0===t.length&&(this._interactiveEditingSessions=null)},commit:t=>{for(const[s,i]of t)this.updateAttributeValue(e,s,i)}});return t.unshift(s),s}async apply(e,t,s){if(g(t))return;const{loadedAttributes:i,attributeData:r}=t;if(g(i)||0===i.length||g(r))return;if(this._pendingFetchChangedObjectIds&&await _(this._pendingFetchChangedObjectIds,s),0===this._changedObjectIds.size)return;const a={loadedAttributes:i,attributeData:r},n=this._getOverridesFromCache(e,a,this._changedObjectIds),{objectIds:o,fieldNames:c}=n;if(0===o.length||0===c.length)return;const d=await this._queryOverridesFromAssociatedLayer(o,c,s);g(d)||this._processOverridesFromAssociatedLayer(e,d,c,a)}updateGeometry(e){this._changedObjectIds.add(e),this.notifyChange("_changedObjectIds")}updateAttributeValue(e,t,s){this._changedObjectIds.add(e),this._cacheAttributeValue(e,t,s),this.notifyChange("_changedObjectIds")}_cacheAttributeValue(e,t,s){this._memCache.put(this._getAttributeCacheKey(e,t),s,this._memCacheAttributeValueSize(s))}_getOverridesFromCache(e,{loadedAttributes:t,attributeData:s},i){const r=new Set,a=new Array;for(const o of t)a[o.index]=s[o.name];const n=new Set;for(let o=0;o<e.length;o++){const s=e[o];if(i.has(s))for(const e of t){const t=this._fromCache(s,e.index);void 0===t?(r.add(s),n.add(e.name)):a[e.index][o]=t}}return{objectIds:Array.from(r),fieldNames:Array.from(n)}}_fromCache(e,t){const s=this._fromInteractiveEditingSession(e,t);if(void 0!==s)return s;const i=this._getAttributeCacheKey(e,t);return this._memCache.get(i)}_fromInteractiveEditingSession(e,t){if(!g(this._interactiveEditingSessions))for(const s of this._interactiveEditingSessions){if(s.objectId!==e)continue;const i=s.get(t);if(void 0!==i)return i}}_getAttributeCacheKey(e,t){return`${e}-${t}`}async _queryOverridesFromAssociatedLayer(e,t,s){if(0===e.length||0===t.length)return null;e=e.sort(((e,t)=>e-t)),this._warnMaximumChangedObjectsExceeded&&(this._warnMaximumChangedObjectsExceeded=!1,this._logMaximumObjectsExceededWarning());const i=this.layer.associatedLayer;if(g(i))return null;const n=i.createQuery();n.where="1=1",n.returnGeometry=!1,n.outFields=[i.objectIdField,...t],n.cacheHint=!0,n.objectIds=e;const o=I(i),c=e.length>o?r(e,o).map((e=>{const t=n.clone();return t.objectIds=e,a(C(i,t,{signal:s}))})):[a(C(i,n,{signal:s}))];return(await Promise.all(c)).reduce(((e,t)=>e.concat(t.ok?t.value.features:[])),[])}_logMaximumObjectsExceededWarning(){let e=`The number of edited objects that are not yet cached in the scene service exceeds the maximum limit. Attribute changes will only be available for the first ${j(this._maximumNumberOfEditOVerrides)} objects. Please consider re-caching the scene service`;const t=this.layer.portalItem;t&&t.loaded?e+=` (${t.portal.url}/home/item.html?id=${t.id}#settings)`:e+=` (${this.layer.parsedUrl.path})`,d.getLogger("esri.views.3d.layers.i3s.I3SOverrides").warn("#queryOverrides()",this.layer.title,`${e}.`)}_processOverridesFromAssociatedLayer(e,t,s,{loadedAttributes:i,attributeData:r}){const a=this.layer.associatedLayer;if(g(a))return;const n=a.objectIdField,o=s.map((e=>r[e])),c=new Map(i.map((e=>[e.name,e.index]))),d=s.map((e=>c.get(e))),h=new Map(Array.from(e,((e,t)=>[e,t])));for(const l of t){const e=l.attributes[n];for(let t=0;t<s.length;t++){const i=d[t],r=h.get(e),a=l.attributes[s[t]];o[t][r]=a,this._cacheAttributeValue(e,i,a)}}}_memCacheAttributeValueSize(e){return"string"==typeof e?o(e):c()}async _fetchChangedObjectIds(e){const s=this.layer;if(await s.load({signal:e}),this._changedObjectIds.clear(),g(s.associatedLayer)||!s.associatedLayer.capabilities?.operations?.supportsChangeTracking)return;const i=this._getFetchChangedObjectIdsServerGen();if(g(i))return;const r=s.associatedLayer.layerId,a=h(s.associatedLayer.infoFor3D),o=await n(t(`${s.associatedLayer.url}/extractChanges`,{method:"post",query:{f:"json",returnIdsOnly:!0,layers:`[${r}]`,returnUpdates:!0,returnDeletes:a,returnInserts:a,layerServerGens:JSON.stringify([{id:r,serverGen:i}])},timeout:w,signal:e}));if(o.ok&&o.value.data?.edits&&1===o.value.data.edits.length){const e=o.value.data.edits[0],t=p(e,"objectIds","adds"),s=p(e,"objectIds","updates"),i=p(e,"objectIds","deletes");let r=[];h(t)&&(r=r.concat(t)),h(s)&&(r=r.concat(s)),h(i)&&(r=r.concat(i));const a=Math.min(this._maximumNumberOfEditOVerrides,r.length);a<r.length&&(this._warnMaximumChangedObjectsExceeded=!0);const n=r.sort(((e,t)=>e-t));for(let o=0;o<a;o++)this._changedObjectIds.add(n[o])}this.notifyChange("_changedObjectIds")}_getFetchChangedObjectIdsServerGen(){const e=this.layer;if(h(e.serviceUpdateTimeStamp)&&h(e.serviceUpdateTimeStamp.lastUpdate))return e.serviceUpdateTimeStamp.lastUpdate;const t=e.associatedLayer;return h(t)&&h(t.serverGens)&&h(t.serverGens.minServerGen)?t.serverGens.minServerGen:null}get test(){const e=Array.from(this._changedObjectIds),t=this._pendingFetchChangedObjectIds,s=this;return{changedObjectIds:e,pendingFetchChangedObjectIds:t,get maximumNumberOfEditOVerrides(){return s._maximumNumberOfEditOVerrides},set maximumNumberOfEditOVerrides(e){s._maximumNumberOfEditOVerrides=e}}}};e([f({constructOnly:!0})],A.prototype,"view",void 0),e([f({constructOnly:!0})],A.prototype,"layer",void 0),e([f({readOnly:!0})],A.prototype,"is3DOFL",null),e([f({readOnly:!0})],A.prototype,"geometryChangedObjectIds",null),e([f()],A.prototype,"_associatedLayer",null),e([f()],A.prototype,"_associatedLayerView",void 0),e([f({constructOnly:!0})],A.prototype,"memoryController",void 0),e([f()],A.prototype,"_changedObjectIds",void 0),e([f()],A.prototype,"_pendingFetchChangedObjectIds",void 0),e([f()],A.prototype,"_definitionExpression",null),e([f()],A.prototype,"updating",null),e([f()],A.prototype,"isEmpty",null),A=e([O("esri.views.3d.layers.i3s.I3SOverrides")],A);class F{constructor(e,t){this.objectId=e,this._options=t,this._updates=new Map,this._state=x.ACTIVE}get(e){return this._updates.get(e)}set(e,t){this._isActive&&this._updates.set(e,t)}rollback(){this._isActive&&(this._state=x.ROLLED_BACK,this._options.rollback())}commit(){this._isActive&&(this._state=x.COMMITTED,this._options.commit(this._updates),this._updates.clear())}get _isActive(){return this._state===x.ACTIVE}}var x;!function(e){e[e.ACTIVE=0]="ACTIVE",e[e.COMMITTED=1]="COMMITTED",e[e.ROLLED_BACK=2]="ROLLED_BACK"}(x||(x={}));const w=1e4,S=5e4;export{A as I3SOverrides};