UNPKG

@juzi/wechaty-puppet-whatsapp

Version:
44 lines 1.29 kB
import { WA_ERROR_TYPE } from '../exception/error-type.js'; import WAError from '../exception/whatsapp-error.js'; export class RequestPool { static _instance; poolMap = {}; constructor() { } static get Instance() { if (!this._instance) { this._instance = new RequestPool(); } return this._instance; } hasRequest(id) { return !!this.poolMap[id]; } pushRequest(id, timeout) { const callback = new Promise((resolve, reject) => { if (!this.poolMap[id]) { this.poolMap[id] = []; } this.poolMap[id].push(resolve); if (timeout) { setTimeout(reject, timeout); } }).catch(() => { delete this.poolMap[id]; throw WAError(WA_ERROR_TYPE.ERR_REQUEST_TIMEOUT, `TIMEOUT when processing request :${id}`); }); return callback; } resolveRequest(id) { const callbacks = this.poolMap[id]; if (!callbacks || callbacks.length === 0) { return false; } callbacks.forEach(cb => cb()); delete this.poolMap[id]; return true; } clearPool() { this.poolMap = {}; } } //# sourceMappingURL=request-pool.js.map