mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
Overhaul sharding broadcast/send
This commit is contained in:
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]);
|
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;
|
module.exports = Shard;
|
||||||
|
|||||||
@@ -75,21 +75,14 @@ class ShardingManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends a message to a shard by ID.
|
* Send a message to all shards.
|
||||||
* @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.
|
|
||||||
* @param {*} message Message to be sent to the shards
|
* @param {*} message Message to be sent to the shards
|
||||||
|
* @returns {Promise<Shard[]>}
|
||||||
*/
|
*/
|
||||||
broadcast(message) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user