"use strict";var SingletonTaskStatus;Object.defineProperty(exports,"__esModule",{value:!0}),exports.SingletonTask=exports.SingletonTaskStatus=void 0,function(t){t[t.WAITING=0]="WAITING",t[t.PENDING=1]="PENDING",t[t.RESOLVED=2]="RESOLVED",t[t.REJECTED=3]="REJECTED"}(SingletonTaskStatus||(exports.SingletonTaskStatus=SingletonTaskStatus={}));class SingletonTask{constructor(t,s="",e=6e4){this.executor=t,this.key=s,this.timeout=e,this.status=SingletonTaskStatus.WAITING,this.queue=[]}async _run(){this.status=SingletonTaskStatus.PENDING,this.timer=setTimeout(()=>{this._reject("timeout")},this.timeout);try{const t=await this.executor();return this._resolve(t),t}catch(t){throw this._reject(t),t}}_emit(){const{queue:t}=this;this.queue=[];for(const s of t)this.status===SingletonTaskStatus.REJECTED?s.reject(this.rejectedReason):s.resolve(this.result)}get rejected(){return this.status===SingletonTaskStatus.REJECTED}_resolve(t){this.status=SingletonTaskStatus.RESOLVED,this.result=t,clearTimeout(this.timer),this._emit()}_reject(t){this.status=SingletonTaskStatus.REJECTED,this.rejectedReason=t,clearTimeout(this.timer),this._emit()}async getResult(t=!1,s=!1){if(this.status===SingletonTaskStatus.WAITING||this.status===SingletonTaskStatus.REJECTED&&t||this.status===SingletonTaskStatus.RESOLVED&&s)return this._run();if(this.status===SingletonTaskStatus.PENDING)return new Promise((t,s)=>{this.queue.push({resolve:t,reject:s})});if(this.status===SingletonTaskStatus.REJECTED)throw this.rejectedReason;return this.result}getResultCache(){return this.result}}exports.SingletonTask=SingletonTask;