Skip to content

Commit

Permalink
Add types for once/off/removeListener (#43170)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikeee committed Mar 18, 2020
1 parent ab406a4 commit 4bf2352
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 18 additions & 0 deletions types/ws/index.d.ts
Expand Up @@ -238,11 +238,29 @@ declare namespace WebSocket {
on(event: 'close' | 'listening', cb: (this: Server) => void): this;
on(event: string | symbol, listener: (this: Server, ...args: any[]) => void): this;

once(event: 'connection', cb: (this: Server, socket: WebSocket, request: http.IncomingMessage) => void): this;
once(event: 'error', cb: (this: Server, error: Error) => void): this;
once(event: 'headers', cb: (this: Server, headers: string[], request: http.IncomingMessage) => void): this;
once(event: 'close' | 'listening', cb: (this: Server) => void): this;
once(event: string | symbol, listener: (...args: any[]) => void): this;

off(event: 'connection', cb: (this: Server, socket: WebSocket, request: http.IncomingMessage) => void): this;
off(event: 'error', cb: (this: Server, error: Error) => void): this;
off(event: 'headers', cb: (this: Server, headers: string[], request: http.IncomingMessage) => void): this;
off(event: 'close' | 'listening', cb: (this: Server) => void): this;
off(event: string | symbol, listener: (this: Server, ...args: any[]) => void): this;

addListener(event: 'connection', cb: (client: WebSocket) => void): this;
addListener(event: 'error', cb: (err: Error) => void): this;
addListener(event: 'headers', cb: (headers: string[], request: http.IncomingMessage) => void): this;
addListener(event: 'close' | 'listening', cb: () => void): this;
addListener(event: string | symbol, listener: (...args: any[]) => void): this;

removeListener(event: 'connection', cb: (client: WebSocket) => void): this;
removeListener(event: 'error', cb: (err: Error) => void): this;
removeListener(event: 'headers', cb: (headers: string[], request: http.IncomingMessage) => void): this;
removeListener(event: 'close' | 'listening', cb: () => void): this;
removeListener(event: string | symbol, listener: (...args: any[]) => void): this;
}

// WebSocket stream
Expand Down
8 changes: 5 additions & 3 deletions types/ws/ws-tests.ts
Expand Up @@ -27,9 +27,11 @@ import * as url from 'url';
ws.send('something', (error?: Error) => {});
ws.send('something', {}, (error?: Error) => {});
});

wss.on('upgrade', (res) => {
console.log(`response: ${Object.keys(res)}`);
wss.once('connection', (ws, req) => {
ws.send('something');
});
wss.off('connection', (ws, req) => {
ws.send('something');
});
}

Expand Down

0 comments on commit 4bf2352

Please sign in to comment.