Merge pull request #89 from DjDeveloperr/slash

Remove unnecessary asyncs from RESTManager
This commit is contained in:
DjDeveloper 2021-01-21 20:17:25 +05:30 committed by GitHub
commit 3dde9688ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 11 deletions

View File

@ -173,11 +173,11 @@ export class RESTManager {
if (options?.userAgent !== undefined) this.userAgent = options.userAgent if (options?.userAgent !== undefined) this.userAgent = options.userAgent
if (options?.canary !== undefined) this.canary = options.canary if (options?.canary !== undefined) this.canary = options.canary
if (options?.client !== undefined) this.client = options.client if (options?.client !== undefined) this.client = options.client
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.handleRateLimits() this.handleRateLimits()
} }
private async checkQueues(): Promise<void> { /** Checks the queues of buckets, if empty, delete entry */
private checkQueues(): void {
Object.entries(this.queues).forEach(([key, value]) => { Object.entries(this.queues).forEach(([key, value]) => {
if (value.length === 0) { if (value.length === 0) {
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
@ -186,11 +186,10 @@ export class RESTManager {
}) })
} }
/** Adds a Request to Queue */
private queue(request: QueuedItem): void { private queue(request: QueuedItem): void {
const route = request.url.substring( const route = request.url.substring(
// eslint seriously? Number(baseEndpoints.DISCORD_API_URL.length) + 1
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
baseEndpoints.DISCORD_API_URL.length + 1
) )
const parts = route.split('/') const parts = route.split('/')
parts.shift() parts.shift()
@ -243,10 +242,8 @@ export class RESTManager {
} }
if (Object.keys(this.queues).length !== 0) { if (Object.keys(this.queues).length !== 0) {
// await delay(100)
// eslint-disable-next-line @typescript-eslint/no-floating-promises // eslint-disable-next-line @typescript-eslint/no-floating-promises
this.processQueue() this.processQueue()
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.checkQueues() this.checkQueues()
} else this.processing = false } else this.processing = false
} }
@ -293,7 +290,7 @@ export class RESTManager {
return data return data
} }
private async isRateLimited(url: string): Promise<number | false> { private isRateLimited(url: string): number | false {
const global = this.rateLimits.get('global') const global = this.rateLimits.get('global')
const rateLimited = this.rateLimits.get(url) const rateLimited = this.rateLimits.get(url)
const now = Date.now() const now = Date.now()
@ -308,6 +305,7 @@ export class RESTManager {
return false return false
} }
/** Processes headers of the Response */
private processHeaders( private processHeaders(
url: string, url: string,
headers: Headers headers: Headers
@ -361,12 +359,13 @@ export class RESTManager {
return rateLimited ? bucket : undefined return rateLimited ? bucket : undefined
} }
private async handleStatusCode( /** Handles status code of response and acts as required */
private handleStatusCode(
response: Response, response: Response,
body: any, body: any,
data: { [key: string]: any }, data: { [key: string]: any },
reject: CallableFunction reject: CallableFunction
): Promise<void> { ): void {
const status = response.status const status = response.status
// We have hit ratelimit - this should not happen // We have hit ratelimit - this should not happen
@ -555,10 +554,13 @@ export class RESTManager {
}) })
} }
private async handleRateLimits(): Promise<void> { /** Checks for RateLimits times and deletes if already over */
private handleRateLimits(): void {
const now = Date.now() const now = Date.now()
this.rateLimits.forEach((value, key) => { this.rateLimits.forEach((value, key) => {
// Ratelimit has not ended
if (value.resetAt > now) return if (value.resetAt > now) return
// It ended, so delete
this.rateLimits.delete(key) this.rateLimits.delete(key)
if (key === 'global') this.globalRateLimit = false if (key === 'global') this.globalRateLimit = false
}) })