@@ -1272,12 +1272,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
1272
1272
1273
1273
// destroy old socket before allocating a new one
1274
1274
if ( this . isOurSocket && this . conn ) {
1275
- // The 'close' listener is called when the socket is destroyed, which
1276
- // initiates another reconnect attempt. This reconnect attempt disconnects
1277
- // the new connection created by this control flow after the `retryDelay`.
1278
- // To avoid an endless reconnect loop, we need to remove the 'close'
1279
- // listener here before destroying the socket.
1280
- this . conn . removeAllListeners ( 'close' ) ;
1275
+ this . unbindListeners ( ) ;
1281
1276
this . conn . destroy ( ) ;
1282
1277
this . conn = undefined ;
1283
1278
}
@@ -1431,6 +1426,14 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
1431
1426
} ) ;
1432
1427
}
1433
1428
1429
+ private unbindListeners ( ) {
1430
+ (
1431
+ [ 'data' , 'end' , 'close' , 'timeout' , 'error' ] as ( keyof IrcConnectionEventsMap ) [ ]
1432
+ ) . forEach ( evtType => {
1433
+ this . conn ?. removeAllListeners ( evtType ) ;
1434
+ } ) ;
1435
+ }
1436
+
1434
1437
private reconnect ( retryCount : number ) {
1435
1438
if ( ! this . isOurSocket ) {
1436
1439
// Cannot reconnect if the socket is not ours.
@@ -1461,11 +1464,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
1461
1464
*/
1462
1465
public destroy ( ) {
1463
1466
util . log ( 'Destroying connection' ) ;
1464
- (
1465
- [ 'data' , 'end' , 'close' , 'timeout' , 'error' ] as ( keyof IrcConnectionEventsMap ) [ ]
1466
- ) . forEach ( evtType => {
1467
- this . conn ?. removeAllListeners ( evtType ) ;
1468
- } ) ;
1467
+ this . unbindListeners ( ) ;
1469
1468
if ( this . isOurSocket ) {
1470
1469
this . disconnect ( ) ;
1471
1470
}
0 commit comments