mirror of
https://github.com/danbulant/discord.js
synced 2026-05-25 04:52:22 +00:00
Overhaul sharding broadcast/send
This commit is contained in:
parent
737ad8e92b
commit
b795ed8109
3 changed files with 20 additions and 13 deletions
File diff suppressed because one or more lines are too long
|
|
@ -28,6 +28,20 @@ class Shard {
|
|||
*/
|
||||
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to the shard's process.
|
||||
* @param {*} message Message to send to the shard
|
||||
* @returns {Promise<Shard>}
|
||||
*/
|
||||
send(message) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const sent = this.process.send(message, err => {
|
||||
if (err) reject(err); else resolve(this);
|
||||
});
|
||||
if (!sent) throw new Error('Failed to send message to shard\'s process.');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Shard;
|
||||
|
|
|
|||
|
|
@ -75,21 +75,14 @@ class ShardingManager extends EventEmitter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Sends a message to a shard by ID.
|
||||
* @param {number} id ID of the shard to send the message to
|
||||
* @param {*} message Message to send to the shard
|
||||
*/
|
||||
send(id, message) {
|
||||
if (!this.shards.has(id)) throw new Error('Shard ID is invalid!');
|
||||
this.shards.get(id).process.send(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast a message to all shards.
|
||||
* Send a message to all shards.
|
||||
* @param {*} message Message to be sent to the shards
|
||||
* @returns {Promise<Shard[]>}
|
||||
*/
|
||||
broadcast(message) {
|
||||
for (const shard of this.shards.values()) shard.process.send(message);
|
||||
const promises = [];
|
||||
for (const shard of this.shards.values()) promises.push(shard.send(message));
|
||||
return Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue