Skip to content

Commit

Permalink
refactor(sharding): use switch statement (#8928)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristancamejo authored Dec 15, 2022
1 parent 627511d commit 6540914
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 45 deletions.
37 changes: 23 additions & 14 deletions packages/discord.js/src/sharding/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down
68 changes: 37 additions & 31 deletions packages/discord.js/src/sharding/ShardClientUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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;
}
});
}
Expand Down

0 comments on commit 6540914

Please sign in to comment.