/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../chunks/_rollupPluginBabelHelpers","../../chunks/tslib.es6","../Accessor","../Handles","../maybe","../reactiveUtils","../scheduling","../accessorSupport/decorators/property","../accessorSupport/decorators/subclass"],(function(e,t,n,s,a,i,d,r,h,o){"use strict";e.WatchUpdatingTracking=function(e){function n(){var t;return(t=e.apply(this,arguments)||this).updating=!1,t.handleId=0,t.handles=new a,t.scheduleHandleId=0,t.pendingPromises=new Set,t}t._inheritsLoose(n,e);var s=n.prototype;return s.destroy=function(){this.removeAll(),this.handles.destroy()},s.add=function(e,t,n={}){return this._installWatch(e,t,n,d.watch)},s.addWhen=function(e,t,n={}){return this._installWatch(e,t,n,d.when)},s.addOnCollectionChange=function(e,t,{initial:n=!1,final:s=!1}={}){const a=++this.handleId;return this.handles.add([d.on(e,"after-changes",this._createSyncUpdatingCallback(),d.sync),d.on(e,"change",t,{onListenerAdd:n?e=>t({added:e.toArray(),removed:[]}):void 0,onListenerRemove:s?e=>t({added:[],removed:e.toArray()}):void 0})],a),{remove:()=>this.handles.remove(a)}},s.addPromise=function(e){if(i.isNone(e))return e;const t=++this.handleId;this.handles.add({remove:()=>{this.pendingPromises.delete(e)&&(0!==this.pendingPromises.size||this.handles.has(c)||this._set("updating",!1))}},t),this.pendingPromises.add(e),this._set("updating",!0);const n=()=>this.handles.remove(t);return e.then(n,n),e},s.removeAll=function(){this.pendingPromises.clear(),this.handles.removeAll(),this._set("updating",!1)},s._installWatch=function(e,t,n={},s){const a=++this.handleId;n.sync||this._installSyncUpdatingWatch(e,a);const i=s(e,t,n);return this.handles.add(i,a),{remove:()=>this.handles.remove(a)}},s._installSyncUpdatingWatch=function(e,t){const n=this._createSyncUpdatingCallback(),s=d.watch(e,n,{sync:!0,equals:()=>!1});return this.handles.add(s,t),s},s._createSyncUpdatingCallback=function(){return()=>{this.handles.remove(c),++this.scheduleHandleId;const e=this.scheduleHandleId;this._get("updating")||this._set("updating",!0),this.handles.add(r.schedule((()=>{e===this.scheduleHandleId&&(this._set("updating",this.pendingPromises.size>0),this.handles.remove(c))})),c)}},n}(s),n.__decorate([h.property({readOnly:!0})],e.WatchUpdatingTracking.prototype,"updating",void 0),e.WatchUpdatingTracking=n.__decorate([o.subclass("esri.core.support.WatchUpdatingTracking")],e.WatchUpdatingTracking);const c=-42;var l;e.WatchFlags=void 0,(l=e.WatchFlags||(e.WatchFlags={}))[l.NONE=0]="NONE",l[l.SYNC=1]="SYNC",l[l.INIT=2]="INIT",Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
