Skip to content

Commit

Permalink
refactor(Util): replace Util.delayFor with tp.setTimeout (#7082)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet committed Dec 8, 2021
1 parent 23513d1 commit 25b8491
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/client/websocket/WebSocketManager.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

const EventEmitter = require('node:events');
const { setTimeout: sleep } = require('node:timers/promises');
const { Collection } = require('@discordjs/collection');
const { RPCErrorCodes } = require('discord-api-types/v9');
const WebSocketShard = require('./WebSocketShard');
const PacketHandlers = require('./handlers');
const { Error } = require('../../errors');
const { Events, ShardEvents, Status, WSCodes, WSEvents } = require('../../util/Constants');
const Util = require('../../util/Util');

const BeforeReadyWhitelist = [
WSEvents.READY,
Expand Down Expand Up @@ -258,7 +258,7 @@ class WebSocketManager extends EventEmitter {
// If we have more shards, add a 5s delay
if (this.shardQueue.size) {
this.debug(`Shard Queue Size: ${this.shardQueue.size}; continuing in 5 seconds...`);
await Util.delayFor(5_000);
await sleep(5_000);
return this.createShards();
}

Expand All @@ -279,7 +279,7 @@ class WebSocketManager extends EventEmitter {
this.debug(`Couldn't reconnect or fetch information about the gateway. ${error}`);
if (error.httpStatus !== 401) {
this.debug(`Possible network error occurred. Retrying in 5s...`);
await Util.delayFor(5_000);
await sleep(5_000);
this.reconnecting = false;
return this.reconnect();
}
Expand Down
6 changes: 3 additions & 3 deletions src/rest/RequestHandler.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

const { setTimeout: sleep } = require('node:timers/promises');
const { AsyncQueue } = require('@sapphire/async-queue');
const DiscordAPIError = require('./DiscordAPIError');
const HTTPError = require('./HTTPError');
const RateLimitError = require('./RateLimitError');
const {
Events: { DEBUG, RATE_LIMIT, INVALID_REQUEST_WARNING, API_RESPONSE, API_REQUEST },
} = require('../util/Constants');
const Util = require('../util/Util');

function parseResponse(res) {
if (res.headers.get('content-type').startsWith('application/json')) return res.json();
Expand Down Expand Up @@ -145,7 +145,7 @@ class RequestHandler {
}
delayPromise = this.manager.globalDelay;
} else {
delayPromise = Util.delayFor(timeout);
delayPromise = sleep(timeout);
}

// Determine whether a RateLimitError should be thrown
Expand Down Expand Up @@ -333,7 +333,7 @@ class RequestHandler {

// If caused by a sublimit, wait it out here so other requests on the route can be handled
if (sublimitTimeout) {
await Util.delayFor(sublimitTimeout);
await sleep(sublimitTimeout);
}
return this.execute(request);
}
Expand Down
3 changes: 2 additions & 1 deletion src/sharding/Shard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const EventEmitter = require('node:events');
const path = require('node:path');
const { setTimeout: sleep } = require('node:timers/promises');
const { Error } = require('../errors');
const Util = require('../util/Util');
let childProcess = null;
Expand Down Expand Up @@ -201,7 +202,7 @@ class Shard extends EventEmitter {
*/
async respawn({ delay = 500, timeout = 30_000 } = {}) {
this.kill();
if (delay > 0) await Util.delayFor(delay);
if (delay > 0) await sleep(delay);
return this.spawn(timeout);
}

Expand Down
5 changes: 3 additions & 2 deletions src/sharding/ShardingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const EventEmitter = require('node:events');
const fs = require('node:fs');
const path = require('node:path');
const { setTimeout: sleep } = require('node:timers/promises');
const { Collection } = require('@discordjs/collection');
const Shard = require('./Shard');
const { Error, TypeError, RangeError } = require('../errors');
Expand Down Expand Up @@ -214,7 +215,7 @@ class ShardingManager extends EventEmitter {
const promises = [];
const shard = this.createShard(shardId);
promises.push(shard.spawn(timeout));
if (delay > 0 && this.shards.size !== this.shardList.length) promises.push(Util.delayFor(delay));
if (delay > 0 && this.shards.size !== this.shardList.length) promises.push(sleep(delay));
await Promise.all(promises); // eslint-disable-line no-await-in-loop
}

Expand Down Expand Up @@ -306,7 +307,7 @@ class ShardingManager extends EventEmitter {
let s = 0;
for (const shard of this.shards.values()) {
const promises = [shard.respawn({ respawnDelay, timeout })];
if (++s < this.shards.size && shardDelay > 0) promises.push(Util.delayFor(shardDelay));
if (++s < this.shards.size && shardDelay > 0) promises.push(sleep(shardDelay));
await Promise.all(promises); // eslint-disable-line no-await-in-loop
}
return this.shards;
Expand Down
12 changes: 0 additions & 12 deletions src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,18 +587,6 @@ class Util extends null {
return text.replaceAll('```', '`\u200b``');
}

/**
* 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);
});
}

/**
* Creates a sweep filter that sweeps archived threads
* @param {number} [lifetime=14400] How long a thread has to be archived to be valid for sweeping
Expand Down
1 change: 0 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,6 @@ export class Util extends null {
/** @deprecated Use {@link MessageOptions.allowedMentions} to control mentions in a message instead. */
public static removeMentions(str: string): string;
public static cloneObject(obj: unknown): unknown;
public static delayFor(ms: number): Promise<void>;
public static discordSort<K, V extends { rawPosition: number; id: Snowflake }>(
collection: Collection<K, V>,
): Collection<K, V>;
Expand Down

0 comments on commit 25b8491

Please sign in to comment.