mirror of
https://github.com/danbulant/discord.js
synced 2026-05-27 22:11:53 +00:00
Use a custom promisified setTimeout
This commit is contained in:
parent
acf82f32c3
commit
26b28813a8
3 changed files with 15 additions and 5 deletions
|
|
@ -3,7 +3,6 @@ const EventEmitter = require('events');
|
|||
const path = require('path');
|
||||
const Util = require('../util/Util');
|
||||
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
|
||||
|
|
@ -122,7 +121,7 @@ class Shard extends EventEmitter {
|
|||
this.process.removeListener('exit', this._exitListener);
|
||||
this.process.kill();
|
||||
this._handleExit(false);
|
||||
if (delay > 0) await delayFor(delay);
|
||||
if (delay > 0) await Util.delayFor(delay);
|
||||
return this.spawn(waitForReady);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ const Shard = require('./Shard');
|
|||
const Collection = require('../util/Collection');
|
||||
const Util = require('../util/Util');
|
||||
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.
|
||||
|
|
@ -132,7 +131,7 @@ class ShardingManager extends EventEmitter {
|
|||
const promises = [];
|
||||
const shard = this.createShard();
|
||||
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
|
||||
}
|
||||
|
||||
|
|
@ -192,7 +191,7 @@ class ShardingManager extends EventEmitter {
|
|||
let s = 0;
|
||||
for (const shard of this.shards) {
|
||||
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
|
||||
}
|
||||
return this.shards;
|
||||
|
|
|
|||
|
|
@ -366,6 +366,18 @@ class Util {
|
|||
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue