From f9e07f4f43c8bdecc3a334351315ca0439b9c0ce Mon Sep 17 00:00:00 2001 From: ayntee Date: Fri, 25 Dec 2020 20:13:06 +0400 Subject: [PATCH 1/2] refactor(gateway): use switch-case to avoid repetitive code --- src/gateway/index.ts | 94 +++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 44 deletions(-) diff --git a/src/gateway/index.ts b/src/gateway/index.ts index a8349a4..85beb62 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -182,50 +182,56 @@ export class Gateway extends EventEmitter { this.emit('close', event.code, event.reason) this.debug(`Connection Closed with code: ${event.code}`) - if (event.code === GatewayCloseCodes.UNKNOWN_ERROR) { - this.debug('API has encountered Unknown Error. Reconnecting...') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect() - } else if (event.code === GatewayCloseCodes.UNKNOWN_OPCODE) { - throw new Error("Unknown OP Code was sent. This shouldn't happen!") - } else if (event.code === GatewayCloseCodes.DECODE_ERROR) { - throw new Error("Invalid Payload was sent. This shouldn't happen!") - } else if (event.code === GatewayCloseCodes.NOT_AUTHENTICATED) { - throw new Error('Not Authorized: Payload was sent before Identifying.') - } else if (event.code === GatewayCloseCodes.AUTHENTICATION_FAILED) { - throw new Error('Invalid Token provided!') - } else if (event.code === GatewayCloseCodes.INVALID_SEQ) { - this.debug('Invalid Seq was sent. Reconnecting.') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect() - } else if (event.code === GatewayCloseCodes.RATE_LIMITED) { - throw new Error("You're ratelimited. Calm down.") - } else if (event.code === GatewayCloseCodes.SESSION_TIMED_OUT) { - this.debug('Session Timeout. Reconnecting.') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect(true) - } else if (event.code === GatewayCloseCodes.INVALID_SHARD) { - this.debug('Invalid Shard was sent. Reconnecting.') - // eslint-disable-next-line @typescript-eslint/no-floating-promises - this.reconnect() - } else if (event.code === GatewayCloseCodes.SHARDING_REQUIRED) { - throw new Error("Couldn't connect. Sharding is required!") - } else if (event.code === GatewayCloseCodes.INVALID_API_VERSION) { - throw new Error("Invalid API Version was used. This shouldn't happen!") - } else if (event.code === GatewayCloseCodes.INVALID_INTENTS) { - throw new Error('Invalid Intents') - } else if (event.code === GatewayCloseCodes.DISALLOWED_INTENTS) { - throw new Error("Given Intents aren't allowed") - } else { - this.debug( - 'Unknown Close code, probably connection error. Reconnecting in 5s.' - ) - if (this.timedIdentify !== null) { - clearTimeout(this.timedIdentify) - this.debug('Timed Identify found. Cleared timeout.') - } - await delay(5000) - await this.reconnect(true) + switch (event.code) { + case GatewayCloseCodes.UNKNOWN_ERROR: + this.debug('API has encountered Unknown Error. Reconnecting...') + // eslint-disable-next-line @typescript-eslint/no-floating-promises + this.reconnect() + break + case GatewayCloseCodes.UNKNOWN_OPCODE: + throw new Error("Unknown OP Code was sent. This shouldn't happen!") + case GatewayCloseCodes.DECODE_ERROR: + throw new Error("Invalid Payload was sent. This shouldn't happen!") + case GatewayCloseCodes.NOT_AUTHENTICATED: + throw new Error('Not Authorized: Payload was sent before Identifying.') + case GatewayCloseCodes.AUTHENTICATION_FAILED: + throw new Error('Invalid Token provided!') + case GatewayCloseCodes.INVALID_SEQ: + this.debug('Invalid Seq was sent. Reconnecting.') + // eslint-disable-next-line @typescript-eslint/no-floating-promises + this.reconnect() + break + case GatewayCloseCodes.RATE_LIMITED: + throw new Error("You're ratelimited. Calm down.") + case GatewayCloseCodes.SESSION_TIMED_OUT: + this.debug('Session Timeout. Reconnecting.') + // eslint-disable-next-line @typescript-eslint/no-floating-promises + this.reconnect(true) + break + case GatewayCloseCodes.INVALID_SHARD: + this.debug('Invalid Shard was sent. Reconnecting.') + // eslint-disable-next-line @typescript-eslint/no-floating-promises + this.reconnect() + break + case GatewayCloseCodes.SHARDING_REQUIRED: + throw new Error("Couldn't connect. Sharding is required!") + case GatewayCloseCodes.INVALID_API_VERSION: + throw new Error("Invalid API Version was used. This shouldn't happen!") + case GatewayCloseCodes.INVALID_INTENTS: + throw new Error('Invalid Intents') + case GatewayCloseCodes.DISALLOWED_INTENTS: + throw new Error("Given Intents aren't allowed") + default: + this.debug( + 'Unknown Close code, probably connection error. Reconnecting in 5s.' + ) + if (this.timedIdentify !== null) { + clearTimeout(this.timedIdentify) + this.debug('Timed Identify found. Cleared timeout.') + } + await delay(5000) + await this.reconnect(true) + break } } From 264d6b49fbf2000b9112d6b27fd7faac0f3013bc Mon Sep 17 00:00:00 2001 From: ayntee Date: Fri, 25 Dec 2020 20:18:35 +0400 Subject: [PATCH 2/2] refactor: destructure code and reason from event --- src/gateway/index.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/gateway/index.ts b/src/gateway/index.ts index 85beb62..0477174 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -177,12 +177,12 @@ export class Gateway extends EventEmitter { } } - private async onclose(event: CloseEvent): Promise { - if (event.reason === RECONNECT_REASON) return - this.emit('close', event.code, event.reason) - this.debug(`Connection Closed with code: ${event.code}`) + private async onclose({ reason, code }: CloseEvent): Promise { + if (reason === RECONNECT_REASON) return + this.emit('close', code, reason) + this.debug(`Connection Closed with code: ${code}`) - switch (event.code) { + switch (code) { case GatewayCloseCodes.UNKNOWN_ERROR: this.debug('API has encountered Unknown Error. Reconnecting...') // eslint-disable-next-line @typescript-eslint/no-floating-promises