Skip to content

Commit

Permalink
add off method to remove the listener
Browse files Browse the repository at this point in the history
To have consistency with the `on` method
  • Loading branch information
Atrue committed Feb 9, 2023
1 parent 1db5c42 commit 619701d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ const mockServer = new Server('ws://localhost:8080');
mockServer.on('connection', socket => {
socket.on('message', () => {});
socket.on('close', () => {});
socket.on('error', () => {});

socket.send('message');
socket.close();
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ declare module 'mock-socket' {
close: () => void;
error: (err: Error) => void;
message: (message: string | Blob | ArrayBuffer | ArrayBufferView) => void;
open: () => void;
}

//
Expand Down Expand Up @@ -56,6 +55,7 @@ declare module 'mock-socket' {
target: WebSocket;
close(options?: CloseOptions): void;
on<K extends keyof WebSocketCallbackMap>(type: K, callback: WebSocketCallbackMap[K]): void;
off<K extends keyof WebSocketCallbackMap>(type: K, callback: WebSocketCallbackMap[K]): void;
}

class Server extends EventTarget {
Expand All @@ -68,6 +68,7 @@ declare module 'mock-socket' {
restoreWebsocket(): void;

on(type: string, callback: (socket: Client) => void): void;
off(type: string, callback: (socket: Client) => void): void;
close(options?: CloseOptions): void;
emit(event: string, data: any, options?: EmitOptions): void;

Expand Down
9 changes: 7 additions & 2 deletions src/helpers/proxy-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ export default function proxyFactory(target) {
};
}

const toSocketName = type => (type === 'message' ? `server::${type}` : type);
if (prop === 'on') {
return function onWrapper(type, cb) {
type = type === 'message' ? `server::${type}` : type;
target.addEventListener(type, cb);
target.addEventListener(toSocketName(type), cb);
};
}
if (prop === 'off') {
return function offWrapper(type, cb) {
target.removeEventListener(toSocketName(type), cb);
};
}

Expand Down
7 changes: 7 additions & 0 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ class Server extends EventTarget {
this.addEventListener(type, callback);
}

/*
* Remove event listener
*/
off(type, callback) {
this.removeEventListener(type, callback);
}

/*
* Closes the connection and triggers the onclose method of all listening
* websockets. After that it removes itself from the urlMap so another server
Expand Down
14 changes: 11 additions & 3 deletions tests/unit/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ test('that after creating a server it is added to the network bridge', t => {
t.deepEqual(networkBridge.urlMap, {}, 'the urlMap was cleared after the close call');
});

test('that callback functions can be added to the listeners object', t => {
test('that callback functions can be added and removed from the listeners object', t => {
const myServer = new Server('ws://not-real/');

myServer.on('message', () => {});
myServer.on('close', () => {});
const onMessage = () => {};
const onError = () => {};
myServer.on('message', onMessage);
myServer.on('close', onError);

t.is(myServer.listeners.message.length, 1);
t.is(myServer.listeners.close.length, 1);

myServer.off('message', onMessage);
myServer.off('close', onError);

t.is(myServer.listeners.message.length, 0);
t.is(myServer.listeners.close.length, 0);

myServer.close();
});

Expand Down

0 comments on commit 619701d

Please sign in to comment.