File tree Expand file tree Collapse file tree 2 files changed +28
-0
lines changed
Expand file tree Collapse file tree 2 files changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,17 @@ export abstract class ClientProxy<T extends ServerProxy> extends EventEmitter {
5353 }
5454 }
5555
56+ /**
57+ * Remove an event listener.
58+ */
59+ public off ( event : string , cb : ( ...args : any [ ] ) => void ) : this {
60+ // Fill it here because the fill we're using to provide EventEmitter for the
61+ // browser doesn't appear to include `off`.
62+ this . removeListener ( event , cb ) ;
63+
64+ return this ;
65+ }
66+
5667 protected get proxy ( ) : T {
5768 if ( ! this . _proxy ) {
5869 throw new Error ( "not initialized" ) ;
Original file line number Diff line number Diff line change @@ -38,6 +38,23 @@ describe("net", () => {
3838 expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
3939 } ) ;
4040
41+ it ( "should remove event listener" , async ( ) => {
42+ const socket = new net . Socket ( ) ;
43+
44+ const fn1 = jest . fn ( ) ;
45+ const fn2 = jest . fn ( ) ;
46+
47+ socket . on ( "error" , fn1 ) ;
48+ socket . on ( "error" , fn2 ) ;
49+ socket . off ( "error" , fn1 ) ;
50+
51+ socket . connect ( "/tmp/t/e/s/t/d/o/e/s/n/o/t/e/x/i/s/t" ) ;
52+
53+ await new Promise ( ( r ) : nativeNet . Socket => socket . on ( "close" , r ) ) ;
54+ expect ( fn1 ) . toHaveBeenCalledTimes ( 0 ) ;
55+ expect ( fn2 ) . toHaveBeenCalledTimes ( 1 ) ;
56+ } ) ;
57+
4158 it ( "should connect" , async ( ) => {
4259 await new Promise ( ( resolve ) : void => {
4360 const socket = net . createConnection ( socketPath , ( ) => {
You can’t perform that action at this time.
0 commit comments