mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
make sharding manager constructor better (#794)
* add color stuff to support popular color libraries * make this better * fix docs
This commit is contained in:
committed by
Schuyler Cebulskie
parent
96355a4968
commit
0de3d1bfc4
File diff suppressed because one or more lines are too long
@@ -10,8 +10,9 @@ class Shard {
|
||||
/**
|
||||
* @param {ShardingManager} manager The sharding manager
|
||||
* @param {number} id The ID of this shard
|
||||
* @param {array} [spawnArgs=] Optional command line arguments to pass to the shard
|
||||
*/
|
||||
constructor(manager, id) {
|
||||
constructor(manager, id, spawnArgs = []) {
|
||||
/**
|
||||
* Manager that created the shard
|
||||
* @type {ShardingManager}
|
||||
@@ -28,7 +29,7 @@ class Shard {
|
||||
* Process of the shard
|
||||
* @type {ChildProcess}
|
||||
*/
|
||||
this.process = childProcess.fork(path.resolve(this.manager.file), [], {
|
||||
this.process = childProcess.fork(path.resolve(this.manager.file), spawnArgs, {
|
||||
env: {
|
||||
SHARD_ID: this.id,
|
||||
SHARD_COUNT: this.manager.totalShards,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
const mergeDefault = require('../util/MergeDefault');
|
||||
const Shard = require('./Shard');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
@@ -14,12 +14,13 @@ const Collection = require('../util/Collection');
|
||||
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
|
||||
* @param {Object} options The options for the sharding manager.
|
||||
*/
|
||||
constructor(file, totalShards = 1, respawn = true) {
|
||||
constructor(file, options = {}) {
|
||||
super();
|
||||
|
||||
options = mergeDefault({ totalShards: 1, respawn: true, spawnArgs: [] }, options);
|
||||
|
||||
/**
|
||||
* Path to the shard script file
|
||||
* @type {string}
|
||||
@@ -34,18 +35,26 @@ class ShardingManager extends EventEmitter {
|
||||
* Amount of shards that this manager is going to spawn
|
||||
* @type {number}
|
||||
*/
|
||||
this.totalShards = totalShards;
|
||||
if (typeof totalShards !== 'number' || isNaN(totalShards)) {
|
||||
if (typeof options.totalShards !== 'number' || isNaN(options.totalShards)) {
|
||||
throw new TypeError('Amount of shards must be a number.');
|
||||
}
|
||||
if (totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
|
||||
if (totalShards !== Math.floor(totalShards)) throw new RangeError('Amount of shards must be an integer.');
|
||||
if (options.totalShards < 1) throw new RangeError('Amount of shards must be at least 1.');
|
||||
if (options.totalShards !== Math.floor(options.totalShards)) {
|
||||
throw new RangeError('Amount of shards must be an integer.');
|
||||
}
|
||||
this.totalShards = options.totalShards;
|
||||
|
||||
/**
|
||||
* Whether shards should automatically respawn upon exiting
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.respawn = respawn;
|
||||
this.respawn = options.respawn;
|
||||
|
||||
/**
|
||||
* An array of arguments to pass to shards.
|
||||
* @type {array}
|
||||
*/
|
||||
this.spawnArgs = options.spawnArgs;
|
||||
|
||||
/**
|
||||
* A collection of shards that this manager has spawned
|
||||
@@ -60,7 +69,7 @@ class ShardingManager extends EventEmitter {
|
||||
* @returns {Promise<Shard>}
|
||||
*/
|
||||
createShard(id = this.shards.size) {
|
||||
const shard = new Shard(this, id);
|
||||
const shard = new Shard(this, id, this.spawnArgs);
|
||||
this.shards.set(id, shard);
|
||||
/**
|
||||
* Emitted upon launching a shard
|
||||
|
||||
Reference in New Issue
Block a user