/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{isSome as r}from"../../core/arrayUtils.js";import{removeHandles as t}from"../../core/handleUtils.js";import{createResolver as e,onAbort as a,throwIfAborted as n,createAbortError as o}from"../../core/promiseUtils.js";import{signal as i}from"../../core/signal.js";class s{constructor(){this._currentOperationSignal=i(null)}get currentOperationType(){return this._currentOperationSignal.value?.type??null}destroy(){this.stop()}async start(i,s,l){const p=this._currentOperationSignal.value;p?.stop();const c=new AbortController,u=l?.signal,h=AbortSignal.any([c.signal,u].filter(r)),m=e();let g=!1;const y=()=>{g||(g=!0,m.reject(o()))},v={type:i,resolve:r=>{g||(g=!0,m.resolve(r))},reject:y,promise:m.promise,stop:()=>c.abort(),handles:[]};this._currentOperationSignal.value=v,v.handles.push(a(u,()=>{c.abort()}),a(h,()=>{t(v.handles),g||y()}));try{return p&&(await p.promise.catch(()=>{}),n(h)),await s(v,h),await m.promise}finally{t(v.handles),this._currentOperationSignal.value===v&&(this._currentOperationSignal.value=null)}}stop(){this._currentOperationSignal.value?.stop()}}export{s as ExclusiveOperationManager};
