@@ -219,12 +219,12 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
219219 if ( opt . channelPrefixes ) {
220220 this . state . supportedState . channel . types = opt . channelPrefixes ;
221221 }
222- this . state . capabilities . once ( 'serverCapabilitesReady' , ( ) => {
222+ this . state . capabilities . on ( 'serverCapabilitesReady' , ( ) => {
223223 this . onCapsList ( ) ;
224224 // Flush on capabilities modified
225225 this . state . flush ?.( ) ;
226226 } )
227- this . state . capabilities . once ( 'userCapabilitesReady' , ( ) => {
227+ this . state . capabilities . on ( 'userCapabilitesReady' , ( ) => {
228228 this . onCapsConfirmed ( ) ;
229229 // Flush on capabilities modified
230230 this . state . flush ?.( ) ;
@@ -630,9 +630,8 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
630630
631631 // finding what channels a user is in
632632 this . state . chans . forEach ( ( nickChannel , channame ) => {
633- const chanUser = message . nick && nickChannel . users . get ( message . nick ) ;
634- if ( message . nick && chanUser ) {
635- nickChannel . users . set ( message . args [ 0 ] , chanUser ) ;
633+ if ( message . nick && nickChannel . users . has ( message . nick ) ) {
634+ nickChannel . users . set ( message . args [ 0 ] , nickChannel . users . get ( message . nick ) ! ) ;
636635 nickChannel . users . delete ( message . nick ) ;
637636 channelsForNick . push ( channame ) ;
638637 }
@@ -684,25 +683,29 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
684683 }
685684 }
686685 if ( knownPrefixes . length > 0 ) {
687- channel . users . set ( match [ 2 ] , knownPrefixes ) ;
686+ channel . tmpUsers . set ( match [ 2 ] , knownPrefixes ) ;
688687 }
689688 else {
690689 // recombine just in case this server allows weird chars in the nick.
691690 // We know it isn't a mode char.
692- channel . users . set ( match [ 1 ] + match [ 2 ] , '' ) ;
691+ channel . tmpUsers . set ( match [ 1 ] + match [ 2 ] , '' ) ;
693692 }
694693 }
695694 } ) ;
696- // If the channel user list was modified, flush.
697- if ( users . length ) {
698- this . state . flush ?.( )
699- }
700695 }
701696
702697 private onReplyNameEnd ( message : Message ) {
703698 this . _casemap ( message , 1 ) ;
704699 const channel = this . chanData ( message . args [ 1 ] ) ;
705700 if ( channel ) {
701+ channel . users . clear ( ) ;
702+ channel . tmpUsers . forEach ( ( modes , user ) => {
703+ channel . users . set ( user , modes ) ;
704+ } ) ;
705+ channel . tmpUsers . clear ( ) ;
706+
707+ this . state . flush ?.( ) ;
708+
706709 this . emit ( 'names' , message . args [ 1 ] , channel . users ) ;
707710 this . _send ( 'MODE' , message . args [ 1 ] ) ;
708711 }
@@ -1166,6 +1169,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
11661169 key : key ,
11671170 serverName : name ,
11681171 users : new Map ( ) ,
1172+ tmpUsers : new Map ( ) ,
11691173 mode : '' ,
11701174 modeParams : new Map ( ) ,
11711175 } ) ;
@@ -1272,6 +1276,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
12721276
12731277 // destroy old socket before allocating a new one
12741278 if ( this . isOurSocket && this . conn ) {
1279+ this . unbindListeners ( ) ;
12751280 this . conn . destroy ( ) ;
12761281 this . conn = undefined ;
12771282 }
@@ -1425,6 +1430,14 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
14251430 } ) ;
14261431 }
14271432
1433+ private unbindListeners ( ) {
1434+ (
1435+ [ 'data' , 'end' , 'close' , 'timeout' , 'error' ] as ( keyof IrcConnectionEventsMap ) [ ]
1436+ ) . forEach ( evtType => {
1437+ this . conn ?. removeAllListeners ( evtType ) ;
1438+ } ) ;
1439+ }
1440+
14281441 private reconnect ( retryCount : number ) {
14291442 if ( ! this . isOurSocket ) {
14301443 // Cannot reconnect if the socket is not ours.
@@ -1455,11 +1468,7 @@ export class Client extends (EventEmitter as unknown as new () => TypedEmitter<C
14551468 */
14561469 public destroy ( ) {
14571470 util . log ( 'Destroying connection' ) ;
1458- (
1459- [ 'data' , 'end' , 'close' , 'timeout' , 'error' ] as ( keyof IrcConnectionEventsMap ) [ ]
1460- ) . forEach ( evtType => {
1461- this . conn ?. removeAllListeners ( evtType ) ;
1462- } ) ;
1471+ this . unbindListeners ( ) ;
14631472 if ( this . isOurSocket ) {
14641473 this . disconnect ( ) ;
14651474 }
0 commit comments