mirror of
https://github.com/danbulant/discord.js
synced 2026-05-25 21:12:06 +00:00
make shardmanager better (#731)
* add respawn thing for shards, and make it easier to recieve messages from shards * run docs
This commit is contained in:
parent
4bf6ad30f3
commit
fc9d049cc1
3 changed files with 15 additions and 4 deletions
File diff suppressed because one or more lines are too long
|
|
@ -27,6 +27,14 @@ class Shard {
|
|||
* @type {ChildProcess}
|
||||
*/
|
||||
this.process = childProcess.fork(path.resolve(this.manager.file), [id, this.manager.shards.size]);
|
||||
|
||||
this.process.once('exit', () => {
|
||||
if (this.manager.respawn) this.manager.createShard(this.id);
|
||||
});
|
||||
|
||||
this.process.on('message', message => {
|
||||
this.manager.emit('message', this, message);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -15,8 +15,9 @@ class ShardingManager extends EventEmitter {
|
|||
/**
|
||||
* @param {string} file Path to your shard script file
|
||||
* @param {number} [totalShards=1] Number of shards to default to spawning
|
||||
* @param {boolean} [respawn=true] Respawn a shard when it dies
|
||||
*/
|
||||
constructor(file, totalShards) {
|
||||
constructor(file, totalShards, respawn = true) {
|
||||
super();
|
||||
|
||||
/**
|
||||
|
|
@ -39,6 +40,8 @@ class ShardingManager extends EventEmitter {
|
|||
}
|
||||
if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
|
||||
|
||||
this.respawn = respawn;
|
||||
|
||||
/**
|
||||
* A collection of shards that this manager has spawned
|
||||
* @type {Collection<number, Shard>}
|
||||
|
|
@ -48,9 +51,9 @@ class ShardingManager extends EventEmitter {
|
|||
|
||||
/**
|
||||
* Spawns a single shard.
|
||||
* @param {number} id The ID of the shard to spawn. THIS IS NOT NEEDED IN ANY NORMAL CASE!
|
||||
*/
|
||||
createShard() {
|
||||
const id = this.shards.size;
|
||||
createShard(id = this.shards.size) {
|
||||
const shard = new Shard(this, id);
|
||||
this.shards.set(id, shard);
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in a new issue