mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01: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:
File diff suppressed because one or more lines are too long
@@ -27,6 +27,14 @@ class Shard {
|
|||||||
* @type {ChildProcess}
|
* @type {ChildProcess}
|
||||||
*/
|
*/
|
||||||
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]);
|
||||||
|
|
||||||
|
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 {string} file Path to your shard script file
|
||||||
* @param {number} [totalShards=1] Number of shards to default to spawning
|
* @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();
|
super();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -39,6 +40,8 @@ class ShardingManager extends EventEmitter {
|
|||||||
}
|
}
|
||||||
if (this.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
|
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
|
* A collection of shards that this manager has spawned
|
||||||
* @type {Collection<number, Shard>}
|
* @type {Collection<number, Shard>}
|
||||||
@@ -48,9 +51,9 @@ class ShardingManager extends EventEmitter {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns a single shard.
|
* Spawns a single shard.
|
||||||
|
* @param {number} id The ID of the shard to spawn. THIS IS NOT NEEDED IN ANY NORMAL CASE!
|
||||||
*/
|
*/
|
||||||
createShard() {
|
createShard(id = this.shards.size) {
|
||||||
const id = this.shards.size;
|
|
||||||
const shard = new Shard(this, id);
|
const shard = new Shard(this, id);
|
||||||
this.shards.set(id, shard);
|
this.shards.set(id, shard);
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user