UNPKG

status-sharding

Version:

Welcome to Status Sharding! This package is designed to provide an efficient and flexible solution for sharding Discord bots, allowing you to scale your bot across multiple processes or workers.

51 lines (50 loc) 1.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PromiseHandler = void 0; const types_1 = require("../types"); /** Handles promises by storing them in a map and resolving them when the response is received. */ class PromiseHandler { instance; /** List of promises and their unique identifiers. */ nonces = new Map(); /** Creates an instance of PromiseHandler. */ constructor(instance) { this.instance = instance; } /** Resolves the promise with the data received. */ resolve(message) { const promise = this.nonces.get(message._nonce); if (!promise) return this.instance._debug(`Received a message with an unknown nonce: ${message._nonce}`); if (promise.timeout) clearTimeout(promise.timeout); this.nonces.delete(message._nonce); if (message._type !== types_1.MessageTypes.ClientEvalResponseError) promise.resolve(message.data); else { const data = message.data; const error = new Error(data.message + '\n' + data.stack); error.cause = data.script; error.stack = data.stack; error.name = data.name; promise.reject(error); console.error('An error occurred while evaluating the script:', data); } } /** Creates a promise and stores it in the map. */ async create(nonce, timeout) { return await new Promise((resolve, reject) => { if (!timeout) this.nonces.set(nonce, { resolve, reject }); else this.nonces.set(nonce, { resolve, reject, timeout: setTimeout(() => { this.nonces.delete(nonce); reject(new Error('Promise timed out.')); }, timeout), }); }); } } exports.PromiseHandler = PromiseHandler;