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.

72 lines 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.IPCBrokerClient = exports.IPCBrokerManager = void 0; const clusterManager_1 = require("../core/clusterManager"); const clusterClient_1 = require("../core/clusterClient"); /** The IPC Broker Abstract class. */ class IPCBrokerAbstract { instance; /** The listeners of the IPC Broker. */ listeners = new Map(); /** Creates an instance of IPCBrokerAbstract. */ constructor(instance) { this.instance = instance; } /** Listens to a specific channel. */ listen(channelName, callback) { const listeners = this.listeners.get(channelName) ?? []; listeners.push(callback); this.listeners.set(channelName, listeners); } /** Handles the message received, and executes the callback. (Not meant to be used by the user.) */ handleMessage(message) { if (!message._data || !message.broker) return; const listeners = this.listeners.get(message.broker); if (!listeners) return; for (const listener of listeners) { listener(message._data); } } } /** IPC Broker Manager class. */ class IPCBrokerManager extends IPCBrokerAbstract { /** Sends a message to a specific channel. */ async send(channelName, message, clusterId) { if (this.instance instanceof clusterManager_1.ClusterManager) { if (clusterId === undefined) { for (const cluster of this.instance.clusters.values()) { cluster.thread?.send({ _data: message, broker: channelName, }); } } else { const cluster = this.instance.clusters.get(clusterId); if (!cluster) return Promise.reject(new Error('BROKER_INVALID_CLUSTER_ID | Invalid cluster id provided.')); return cluster.thread?.send({ _data: message, broker: channelName, }); } } } } exports.IPCBrokerManager = IPCBrokerManager; /** IPC Broker Client class. */ class IPCBrokerClient extends IPCBrokerAbstract { /** Sends a message to a specific channel. */ async send(channelName, message) { if (this.instance instanceof clusterClient_1.ClusterClient) { return this.instance.process?.send({ _data: message, broker: channelName, }); } } } exports.IPCBrokerClient = IPCBrokerClient; //# sourceMappingURL=broker.js.map