File tree 2 files changed +28
-0
lines changed
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 {
53
53
}
54
54
}
55
55
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
+
56
67
protected get proxy ( ) : T {
57
68
if ( ! this . _proxy ) {
58
69
throw new Error ( "not initialized" ) ;
Original file line number Diff line number Diff line change @@ -38,6 +38,23 @@ describe("net", () => {
38
38
expect ( fn ) . toHaveBeenCalledTimes ( 1 ) ;
39
39
} ) ;
40
40
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
+
41
58
it ( "should connect" , async ( ) => {
42
59
await new Promise ( ( resolve ) : void => {
43
60
const socket = net . createConnection ( socketPath , ( ) => {
You can’t perform that action at this time.
0 commit comments