Skip to content

Commit 7fdae00

Browse files
committed
refactor: reconnection logic to improve readability and maintainability
1 parent 0e3ccf2 commit 7fdae00

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

src/structures/Node.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,8 @@ export class LavalinkNode {
10361036
// Set reconnection state to pending
10371037
this.reconnectionState = ReconnectionState.PENDING;
10381038
this.NodeManager.emit("reconnectinprogress", this);
1039-
if (instaReconnect) {
1039+
1040+
const executeReconnect = () => {
10401041
if (this.reconnectAttempts >= this.options.retryAmount) {
10411042
const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`)
10421043

@@ -1049,21 +1050,14 @@ export class LavalinkNode {
10491050
this.reconnectionState = ReconnectionState.RECONNECTING;
10501051
this.connect();
10511052
this.reconnectAttempts++;
1053+
};
1054+
if (instaReconnect) {
1055+
executeReconnect();
10521056
return;
10531057
}
10541058
this.reconnectTimeout = setTimeout(() => {
10551059
this.reconnectTimeout = null;
1056-
if (this.reconnectAttempts >= this.options.retryAmount) {
1057-
const error = new Error(`Unable to connect after ${this.options.retryAmount} attempts.`)
1058-
this.NodeManager.emit("error", this, error);
1059-
this.reconnectionState = ReconnectionState.IDLE;
1060-
return this.destroy(DestroyReasons.NodeReconnectFail);
1061-
}
1062-
1063-
this.NodeManager.emit("reconnecting", this);
1064-
this.reconnectionState = ReconnectionState.RECONNECTING;
1065-
this.connect();
1066-
this.reconnectAttempts++;
1060+
executeReconnect();
10671061
}, this.options.retryDelay || 1000);
10681062
}
10691063

0 commit comments

Comments
 (0)