mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 12:33:30 +01:00
Use a custom promisified setTimeout
This commit is contained in:
@@ -3,7 +3,6 @@ const EventEmitter = require('events');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
const { Error } = require('../errors');
|
const { Error } = require('../errors');
|
||||||
const delayFor = require('util').promisify(setTimeout);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A self-contained shard created by the {@link ShardingManager}. Each one has a {@link ChildProcess} that contains
|
* A self-contained shard created by the {@link ShardingManager}. Each one has a {@link ChildProcess} that contains
|
||||||
@@ -122,7 +121,7 @@ class Shard extends EventEmitter {
|
|||||||
this.process.removeListener('exit', this._exitListener);
|
this.process.removeListener('exit', this._exitListener);
|
||||||
this.process.kill();
|
this.process.kill();
|
||||||
this._handleExit(false);
|
this._handleExit(false);
|
||||||
if (delay > 0) await delayFor(delay);
|
if (delay > 0) await Util.delayFor(delay);
|
||||||
return this.spawn(waitForReady);
|
return this.spawn(waitForReady);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ const Shard = require('./Shard');
|
|||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
const { Error, TypeError, RangeError } = require('../errors');
|
const { Error, TypeError, RangeError } = require('../errors');
|
||||||
const delayFor = require('util').promisify(setTimeout);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a utility class that makes multi-process sharding of a bot an easy and painless experience.
|
* This is a utility class that makes multi-process sharding of a bot an easy and painless experience.
|
||||||
@@ -132,7 +131,7 @@ class ShardingManager extends EventEmitter {
|
|||||||
const promises = [];
|
const promises = [];
|
||||||
const shard = this.createShard();
|
const shard = this.createShard();
|
||||||
promises.push(shard.spawn(waitForReady));
|
promises.push(shard.spawn(waitForReady));
|
||||||
if (delay > 0 && s !== amount) promises.push(delayFor(delay));
|
if (delay > 0 && s !== amount) promises.push(Util.delayFor(delay));
|
||||||
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +191,7 @@ class ShardingManager extends EventEmitter {
|
|||||||
let s = 0;
|
let s = 0;
|
||||||
for (const shard of this.shards) {
|
for (const shard of this.shards) {
|
||||||
const promises = [shard.respawn(respawnDelay, waitForReady)];
|
const promises = [shard.respawn(respawnDelay, waitForReady)];
|
||||||
if (++s < this.shards.size && shardDelay > 0) promises.push(delayFor(shardDelay));
|
if (++s < this.shards.size && shardDelay > 0) promises.push(Util.delayFor(shardDelay));
|
||||||
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
await Promise.all(promises); // eslint-disable-line no-await-in-loop
|
||||||
}
|
}
|
||||||
return this.shards;
|
return this.shards;
|
||||||
|
|||||||
@@ -366,6 +366,18 @@ class Util {
|
|||||||
|
|
||||||
return dec;
|
return dec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a Promise that resolves after a specified duration.
|
||||||
|
* @param {number} ms How long to wait before resolving (in milliseconds)
|
||||||
|
* @returns {Promise<void>}
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
static delayFor(ms) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
setTimeout(resolve, ms);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Util;
|
module.exports = Util;
|
||||||
|
|||||||
Reference in New Issue
Block a user