/* COPYRIGHT Esri - https://js.arcgis.com/5.1.0/LICENSE.txt */
import{__addDisposableResource as r,__disposeResources as t,__decorate as o}from"tslib";import e from"./Accessor.js";import{abortMaybe as s,removeMaybe as n}from"./maybe.js";import{onAbort as i,throwIfAbortError as l,eachAlways as a}from"./promiseUtils.js";import{property as u}from"./accessorSupport/decorators/property.js";import{subclass as c}from"./accessorSupport/decorators/subclass.js";function p(r,t,o){return a(r.map((r,e)=>t.apply(o,[r,e])))}async function h(r,t,o){return(await a(r.map((r,e)=>t.apply(o,[r,e])))).map(r=>r.value)}function m(r){return{ok:!0,value:r}}function _(r){return{ok:!1,error:r}}function f(r){return null!=r&&!0===r.ok?r.value:null}function y(r){return null!=r&&!1===r.ok?r.error:null}async function b(r){if(null==r)return{ok:!1,error:new Error("no promise provided")};try{return m(await r)}catch(t){return _(t)}}async function d(r){try{return m(await r)}catch(t){return l(t),_(t)}}function v(r){if(!0===r.ok)return r.value;throw r.error}function w(r,t){return new k(r,t)}let k=class extends e{get value(){return f(this._result)}get error(){return y(this._result)}get finished(){return null!=this._result}constructor(r,t){super({}),this._result=null,this._abortHandle=null,this.abort=()=>{this._abortController=s(this._abortController)},this.remove=this.abort,this._abortController=new AbortController;const{signal:o}=this._abortController;this.promise=r(o),this.promise.then(r=>{this._result=m(r),this._cleanup()},r=>{this._result=_(r),this._cleanup()}),this._abortHandle=i(t,this.abort)}normalizeCtorArgs(){return{}}destroy(){this.abort()}_cleanup(){this._abortHandle=n(this._abortHandle),this._abortController=null}};o([u()],k.prototype,"value",null),o([u()],k.prototype,"error",null),o([u()],k.prototype,"finished",null),o([u()],k.prototype,"promise",void 0),o([u()],k.prototype,"_result",void 0),k=o([c("esri.core.asyncUtils.ReactiveTask")],k);class C{constructor(){this._sequence=Promise.resolve()}async acquire(){let r;this._sequence=new Promise(t=>{r=this._sequence.then(()=>t,()=>t)});const t=await r;return{[Symbol.dispose]:t}}async run(o){const e={stack:[],error:void 0,hasError:!1};try{r(e,await this.acquire(),!1);return await o()}catch(s){e.error=s,e.hasError=!0}finally{t(e)}}}export{C as AsyncMutex,v as assertResult,w as createTask,p as forEach,_ as makeResultError,m as makeResultOk,h as map,b as result,y as resultError,d as resultOrAbort,f as resultValue};
