@@ -45,22 +45,10 @@ class Agent extends DispatcherBase {
4545 }
4646
4747 this [ kOnConnect ] = ( origin , targets ) => {
48- const result = this [ kClients ] . get ( origin )
49- if ( result ) {
50- result . count += 1
51- }
5248 this . emit ( 'connect' , origin , [ this , ...targets ] )
5349 }
5450
5551 this [ kOnDisconnect ] = ( origin , targets , err ) => {
56- const result = this [ kClients ] . get ( origin )
57- if ( result ) {
58- result . count -= 1
59- if ( result . count <= 0 ) {
60- this [ kClients ] . delete ( origin )
61- result . dispatcher . destroy ( )
62- }
63- }
6452 this . emit ( 'disconnect' , origin , [ this , ...targets ] , err )
6553 }
6654
@@ -91,8 +79,24 @@ class Agent extends DispatcherBase {
9179 if ( ! dispatcher ) {
9280 dispatcher = this [ kFactory ] ( opts . origin , this [ kOptions ] )
9381 . on ( 'drain' , this [ kOnDrain ] )
94- . on ( 'connect' , this [ kOnConnect ] )
95- . on ( 'disconnect' , this [ kOnDisconnect ] )
82+ . on ( 'connect' , ( origin , targets ) => {
83+ const result = this [ kClients ] . get ( key )
84+ if ( result ) {
85+ result . count += 1
86+ }
87+ this [ kOnConnect ] ( origin , targets )
88+ } )
89+ . on ( 'disconnect' , ( origin , targets , err ) => {
90+ const result = this [ kClients ] . get ( key )
91+ if ( result ) {
92+ result . count -= 1
93+ if ( result . count <= 0 ) {
94+ this [ kClients ] . delete ( key )
95+ result . dispatcher . destroy ( )
96+ }
97+ }
98+ this [ kOnDisconnect ] ( origin , targets , err )
99+ } )
96100 . on ( 'connectionError' , this [ kOnConnectionError ] )
97101
98102 this [ kClients ] . set ( key , { count : 0 , dispatcher } )
0 commit comments