/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import e from"../ArrayPool.js";import{makeHandle as t}from"../handleUtils.js";import{equals as r,equalsShallow as l}from"../lang.js";import{ReentrantObjectPool as o}from"../ReentrantObjectPool.js";import{schedule as i}from"../scheduling.js";import{someSet as n}from"../SetUtils.js";import{generateUID as s}from"../uid.js";import{valueOf as u}from"./get.js";import{Lifecycle as c}from"./Lifecycle.js";import{reaction as a,reactionDeferred as d}from"./trackingUtils.js";import{parse as f}from"./utils.js";class h{constructor(){this.uid=s(),this.removed=!1,this.type=null,this.oldValue=null,this.callback=null,this.getValue=null,this.target=null,this.path=null,this.equals=null}static{this.pool=new o(()=>new h)}static acquireUntracked(e,t,l,o,i){return this.pool.acquire(0,e,t,l,o,i,r)}static acquireTracked(e,t,r,l){return this.pool.acquire(1,e,t,r,null,null,l)}notify(e,t){0===this.type?this.callback.call(this.target,e,t,this.path,this.target):this.callback.call(null,e,t,void 0,void 0)}acquire(e,t,r,l,o,i,n){this.uid=s(),this.removed=!1,this.type=e,this.oldValue=t,this.callback=r,this.getValue=l,this.target=o,this.path=i,this.equals=n}release(){this.target=this.path=this.oldValue=this.callback=this.getValue=null,this.uid=s(),this.removed=!0}}const m=new e,p=new Set;let v;function _(e){p.delete(e),p.add(e),g()}function g(){v||(v=i(k))}function j(e){if(e.removed)return;const t=e.oldValue,r=e.getValue();e.equals(t,r)||(e.oldValue=r,e.notify(r,t))}function q(e){for(const t of p.values())t.target===e&&(t.removed=!0)}function k(){let e=10;for(;v&&e--;){v=null;const e=y(),t=m.acquire();for(const r of e){const e=r.uid;j(r),e===r.uid&&r.removed&&t.push(r)}for(const r of p)r.removed&&(t.push(r),p.delete(r));for(const r of t)h.pool.release(r);m.release(t),m.release(e),V.forEach(e=>e())}}function y(){const e=m.acquire();e.length=p.size;let t=0;for(const r of p)e[t]=r,++t;return p.clear(),e}const V=new Set;function E(e){return V.add(e),t(()=>V.delete(e))}function b(e,r,l){let o=f(e,r,l,(e,r,l)=>{let i,n,s=d(()=>u(e,r),(t,s)=>{e.__accessor__?.lifecycle===c.DESTROYED||i&&i.uid!==n?o.remove():(i||(i=h.acquireUntracked(t,l,s,e,r),n=i.uid),_(i))});return t(()=>{s.remove(),i&&(i.uid!==n||i.removed||(i.removed=!0,_(i)),i=null),o=s=null})});return o}function D(e,t,l){const o=f(e,t,l,(e,t,l)=>{let i=!1;return a(()=>u(e,t),(n,s)=>{e.__accessor__.lifecycle!==c.DESTROYED?i||(i=!0,r(s,n)||l.call(e,n,s,t,e),i=!1):o.remove()})});return o}function S(e,r,l,o=!1){return e.__accessor__&&e.__accessor__.lifecycle!==c.DESTROYED?o?D(e,r,l):b(e,r,l):t()}function w(e,r,l){let o,i,n=d(e,(e,t)=>{o&&o.uid!==i?n.remove():(o||(o=h.acquireTracked(e,r,t,l),i=o.uid),_(o))});return t(()=>{n.remove(),o&&(o.uid!==i||o.removed||(o.removed=!0,_(o)),o=null),n=null})}function T(e,t,r){let l=!1;return a(e,(e,o)=>{l||(l=!0,r(o,e)||t(e,o),l=!1)})}function U(e,t,r=!1,o=l){return r?T(e,t,o):w(e,t,o)}function O(e){return n(p,t=>t.oldValue===e)}export{E as afterDispatch,k as dispatch,O as isValueInUse,q as removeTarget,g as scheduleDispatch,S as watch,U as watchTracked};
