UNPKG

@juzi/wechaty-puppet-whatsapp

Version:
51 lines 1.64 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RequestPool = void 0; const error_type_js_1 = require("../exception/error-type.js"); const whatsapp_error_js_1 = __importDefault(require("../exception/whatsapp-error.js")); 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 (0, whatsapp_error_js_1.default)(error_type_js_1.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 = {}; } } exports.RequestPool = RequestPool; //# sourceMappingURL=request-pool.js.map