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