diff --git a/packages/discord.js/src/sharding/Shard.js b/packages/discord.js/src/sharding/Shard.js index 322fd40bfabb..e7d2bea43ab7 100644 --- a/packages/discord.js/src/sharding/Shard.js +++ b/packages/discord.js/src/sharding/Shard.js @@ -21,8 +21,14 @@ class Shard extends EventEmitter { constructor(manager, id) { super(); - if (manager.mode === 'process') childProcess = require('node:child_process'); - else if (manager.mode === 'worker') Worker = require('node:worker_threads').Worker; + switch (manager.mode) { + case 'process': + childProcess = require('node:child_process'); + break; + case 'worker': + Worker = require('node:worker_threads').Worker; + break; + } /** * Manager that created the shard @@ -112,18 +118,21 @@ class Shard extends EventEmitter { this._exitListener = this._handleExit.bind(this, undefined, timeout); - if (this.manager.mode === 'process') { - this.process = childProcess - .fork(path.resolve(this.manager.file), this.args, { - env: this.env, - execArgv: this.execArgv, - }) - .on('message', this._handleMessage.bind(this)) - .on('exit', this._exitListener); - } else if (this.manager.mode === 'worker') { - this.worker = new Worker(path.resolve(this.manager.file), { workerData: this.env }) - .on('message', this._handleMessage.bind(this)) - .on('exit', this._exitListener); + switch (this.manager.mode) { + case 'process': + this.process = childProcess + .fork(path.resolve(this.manager.file), this.args, { + env: this.env, + execArgv: this.execArgv, + }) + .on('message', this._handleMessage.bind(this)) + .on('exit', this._exitListener); + break; + case 'worker': + this.worker = new Worker(path.resolve(this.manager.file), { workerData: this.env }) + .on('message', this._handleMessage.bind(this)) + .on('exit', this._exitListener); + break; } this._evals.clear(); diff --git a/packages/discord.js/src/sharding/ShardClientUtil.js b/packages/discord.js/src/sharding/ShardClientUtil.js index 64bc5a540c65..0dcdbd7318e6 100644 --- a/packages/discord.js/src/sharding/ShardClientUtil.js +++ b/packages/discord.js/src/sharding/ShardClientUtil.js @@ -29,29 +29,32 @@ class ShardClientUtil { */ this.parentPort = null; - if (mode === 'process') { - process.on('message', this._handleMessage.bind(this)); - client.on('ready', () => { - process.send({ _ready: true }); - }); - client.on('disconnect', () => { - process.send({ _disconnect: true }); - }); - client.on('reconnecting', () => { - process.send({ _reconnecting: true }); - }); - } else if (mode === 'worker') { - this.parentPort = require('node:worker_threads').parentPort; - this.parentPort.on('message', this._handleMessage.bind(this)); - client.on('ready', () => { - this.parentPort.postMessage({ _ready: true }); - }); - client.on('disconnect', () => { - this.parentPort.postMessage({ _disconnect: true }); - }); - client.on('reconnecting', () => { - this.parentPort.postMessage({ _reconnecting: true }); - }); + switch (mode) { + case 'process': + process.on('message', this._handleMessage.bind(this)); + client.on('ready', () => { + process.send({ _ready: true }); + }); + client.on('disconnect', () => { + process.send({ _disconnect: true }); + }); + client.on('reconnecting', () => { + process.send({ _reconnecting: true }); + }); + break; + case 'worker': + this.parentPort = require('node:worker_threads').parentPort; + this.parentPort.on('message', this._handleMessage.bind(this)); + client.on('ready', () => { + this.parentPort.postMessage({ _ready: true }); + }); + client.on('disconnect', () => { + this.parentPort.postMessage({ _disconnect: true }); + }); + client.on('reconnecting', () => { + this.parentPort.postMessage({ _reconnecting: true }); + }); + break; } } @@ -81,14 +84,17 @@ class ShardClientUtil { */ send(message) { return new Promise((resolve, reject) => { - if (this.mode === 'process') { - process.send(message, err => { - if (err) reject(err); - else resolve(); - }); - } else if (this.mode === 'worker') { - this.parentPort.postMessage(message); - resolve(); + switch (this.mode) { + case 'process': + process.send(message, err => { + if (err) reject(err); + else resolve(); + }); + break; + case 'worker': + this.parentPort.postMessage(message); + resolve(); + break; } }); }