Skip to content

Commit 28282a7

Browse files
committed
Provide off on event listeners
1 parent bb2bdcc commit 28282a7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/protocol/src/common/proxy.ts

+11
Original file line numberDiff line numberDiff 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");

packages/protocol/test/net.test.ts

+17
Original file line numberDiff line numberDiff 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, () => {

0 commit comments

Comments
 (0)