(function(i,e){typeof exports=="object"&&typeof module<"u"?e(exports):typeof define=="function"&&define.amd?define(["exports"],e):(i=typeof globalThis<"u"?globalThis:i||self,e(i["async-resource-semaphore"]={}))})(this,function(i){"use strict";var n=Object.defineProperty;var c=(i,e,a)=>e in i?n(i,e,{enumerable:!0,configurable:!0,writable:!0,value:a}):i[e]=a;var o=(i,e,a)=>(c(i,typeof e!="symbol"?e+"":e,a),a);class e{constructor(t=1){o(this,"limitDefault");o(this,"limits");this.limitDefault=t,this.limits=new Map}getLimit(t){return this.limits.get(t)??this.limitDefault}setLimit(t,s){return this.limits.set(t,Math.min(Math.max(s,0),this.limitDefault))}}const a="semaphore-default";class m{constructor(t=new e){o(this,"semaphoreDataStorage");this.semaphoreDataStorage=t}decreaseLimit(t){this.semaphoreDataStorage.setLimit(t,this.semaphoreDataStorage.getLimit(t)-1)}increaseLimit(t){this.semaphoreDataStorage.setLimit(t,this.semaphoreDataStorage.getLimit(t)+1)}async take(t=a){await new Promise(s=>{const r=()=>{if(this.semaphoreDataStorage.getLimit(t)>0){this.decreaseLimit(t),s();return}setTimeout(r)};r()})}give(t=a){this.increaseLimit(t)}async run(t,s=a){await this.take(s);try{return await t()}finally{this.give(s)}}}i.AsyncSemaphore=m,i.SemaphoreDataMemeoryStorage=e,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});