refactor: new node features (#5132)

Co-authored-by: Antonio Román <kyradiscord@gmail.com>
This commit is contained in:
Sugden
2021-06-30 21:40:33 +01:00
committed by GitHub
parent f108746c15
commit 1e8f01253e
68 changed files with 305 additions and 360 deletions

View File

@@ -40,7 +40,7 @@ class Shard extends EventEmitter {
* Arguments for the shard's process (only when {@link ShardingManager#mode} is `process`)
* @type {string[]}
*/
this.args = manager.shardArgs || [];
this.args = manager.shardArgs ?? [];
/**
* Arguments for the shard's process executable (only when {@link ShardingManager#mode} is `process`)
@@ -127,14 +127,16 @@ class Shard extends EventEmitter {
this._evals.clear();
this._fetches.clear();
const child = this.process ?? this.worker;
/**
* Emitted upon the creation of the shard's child process/worker.
* @event Shard#spawn
* @param {ChildProcess|Worker} process Child process/worker that was created
*/
this.emit('spawn', this.process || this.worker);
this.emit('spawn', child);
if (timeout === -1 || timeout === Infinity) return this.process || this.worker;
if (timeout === -1 || timeout === Infinity) return child;
await new Promise((resolve, reject) => {
const cleanup = () => {
clearTimeout(spawnTimeoutTimer);
@@ -168,7 +170,7 @@ class Shard extends EventEmitter {
this.once('disconnect', onDisconnect);
this.once('death', onDeath);
});
return this.process || this.worker;
return child;
}
/**
@@ -242,10 +244,10 @@ class Shard extends EventEmitter {
if (this._fetches.has(prop)) return this._fetches.get(prop);
const promise = new Promise((resolve, reject) => {
const child = this.process || this.worker;
const child = this.process ?? this.worker;
const listener = message => {
if (!message || message._fetchProp !== prop) return;
if (message?._fetchProp !== prop) return;
child.removeListener('message', listener);
this._fetches.delete(prop);
resolve(message._result);
@@ -276,10 +278,10 @@ class Shard extends EventEmitter {
if (this._evals.has(script)) return this._evals.get(script);
const promise = new Promise((resolve, reject) => {
const child = this.process || this.worker;
const child = this.process ?? this.worker;
const listener = message => {
if (!message || message._eval !== script) return;
if (message?._eval !== script) return;
child.removeListener('message', listener);
this._evals.delete(script);
if (!message._error) resolve(message._result);
@@ -388,7 +390,7 @@ class Shard extends EventEmitter {
* @event Shard#death
* @param {ChildProcess|Worker} process Child process/worker that exited
*/
this.emit('death', this.process || this.worker);
this.emit('death', this.process ?? this.worker);
this.ready = false;
this.process = null;

View File

@@ -109,10 +109,10 @@ class ShardClientUtil {
*/
fetchClientValues(prop, shard) {
return new Promise((resolve, reject) => {
const parent = this.parentPort || process;
const parent = this.parentPort ?? process;
const listener = message => {
if (!message || message._sFetchProp !== prop || message._sFetchPropShard !== shard) return;
if (message?._sFetchProp !== prop || message._sFetchPropShard !== shard) return;
parent.removeListener('message', listener);
if (!message._error) resolve(message._result);
else reject(Util.makeError(message._error));
@@ -139,7 +139,7 @@ class ShardClientUtil {
*/
broadcastEval(script, options = {}) {
return new Promise((resolve, reject) => {
const parent = this.parentPort || process;
const parent = this.parentPort ?? process;
if (typeof script !== 'function') {
reject(new TypeError('SHARDING_INVALID_EVAL_BROADCAST'));
return;
@@ -147,7 +147,7 @@ class ShardClientUtil {
script = `(${script})(this, ${JSON.stringify(options.context)})`;
const listener = message => {
if (!message || message._sEval !== script || message._sEvalShard !== options.shard) return;
if (message?._sEval !== script || message._sEvalShard !== options.shard) return;
parent.removeListener('message', listener);
if (!message._error) resolve(message._result);
else reject(Util.makeError(message._error));

View File

@@ -70,7 +70,7 @@ class ShardingManager extends EventEmitter {
* List of shards this sharding manager spawns
* @type {string|number[]}
*/
this.shardList = options.shardList || 'auto';
this.shardList = options.shardList ?? 'auto';
if (this.shardList !== 'auto') {
if (!Array.isArray(this.shardList)) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shardList', 'an array.');
@@ -132,7 +132,7 @@ class ShardingManager extends EventEmitter {
* Token to use for obtaining the automatic shard count, and passing to shards
* @type {?string}
*/
this.token = options.token ? options.token.replace(/^Bot\s*/i, '') : null;
this.token = options.token?.replace(/^Bot\s*/i, '') ?? null;
/**
* A collection of shards that this manager has spawned