Skip to content

Commit

Permalink
Fix nits
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed May 7, 2020
1 parent d4a1b87 commit e4b35da
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 37 deletions.
13 changes: 6 additions & 7 deletions doc/ws.md
Expand Up @@ -26,7 +26,7 @@
- [Event: 'pong'](#event-pong)
- [Event: 'unexpected-response'](#event-unexpected-response)
- [Event: 'upgrade'](#event-upgrade)
- [websocket.addEventListener(type, listener)](#websocketaddeventlistenertype-listener)
- [websocket.addEventListener(type, listener[, options])](#websocketaddeventlistenertype-listener-options)
- [websocket.binaryType](#websocketbinarytype)
- [websocket.bufferedAmount](#websocketbufferedamount)
- [websocket.close([code[, reason]])](#websocketclosecode-reason)
Expand Down Expand Up @@ -89,7 +89,7 @@ is provided with a single argument then that is:
- `secure` {Boolean} `true` if `req.connection.authorized` or
`req.connection.encrypted` is set.

The return value (Boolean) of the function determines whether or not to accept
The return value (`Boolean`) of the function determines whether or not to accept
the handshake.

if `verifyClient` is provided with two arguments then those are:
Expand Down Expand Up @@ -340,11 +340,10 @@ handshake. This allows you to read headers from the server, for example

- `type` {String} A string representing the event type to listen for.
- `listener` {Function} The listener to add.
- `options` {object} An options object specifies characteristics about the event
listener. The available options are:
- `once` {Boolean} A Boolean indicating that the listener should be invoked at
most once after being added. If true, the listener would be automatically
removed when invoked.
- `options` {Object}
- `once` {Boolean} A `Boolean` indicating that the listener should be invoked
at most once after being added. If `true`, the listener would be
automatically removed when invoked.

Register an event listener emulating the `EventTarget` interface.

Expand Down
57 changes: 27 additions & 30 deletions lib/event-target.js
Expand Up @@ -109,13 +109,16 @@ const EventTarget = {
/**
* Register an event listener.
*
* @param {String} method A string representing the event type to listen for
* @param {String} type A string representing the event type to listen for
* @param {Function} listener The listener to add
* @param {Object} [options] An options object specifies characteristics
* about the event listener
* @param {Object} options An options object specifies characteristics about
* the event listener
* @param {Boolean} options.once A `Boolean`` indicating that the listener
* should be invoked at most once after being added. If `true`, the
* listener would be automatically removed when invoked.
* @public
*/
addEventListener(method, listener, options = {}) {
addEventListener(type, listener, options) {
if (typeof listener !== 'function') return;

function onMessage(data) {
Expand All @@ -134,44 +137,38 @@ const EventTarget = {
listener.call(this, new OpenEvent(this));
}

const type = options.once ? 'once' : 'on';

switch (method) {
case 'message':
onMessage._listener = listener;
this[type](method, onMessage);
break;
case 'close':
onClose._listener = listener;
this[type](method, onClose);
break;
case 'error':
onError._listener = listener;
this[type](method, onError);
break;
case 'open':
onOpen._listener = listener;
this[type](method, onOpen);
break;
default:
this[type](method, listener);
break;
const method = options && options.once ? 'once' : 'on';

if (type === 'message') {
onMessage._listener = listener;
this[method](type, onMessage);
} else if (type === 'close') {
onClose._listener = listener;
this[method](type, onClose);
} else if (type === 'error') {
onError._listener = listener;
this[method](type, onError);
} else if (type === 'open') {
onOpen._listener = listener;
this[method](type, onOpen);
} else {
this[method](type, listener);
}
},

/**
* Remove an event listener.
*
* @param {String} method A string representing the event type to remove
* @param {String} type A string representing the event type to remove
* @param {Function} listener The listener to remove
* @public
*/
removeEventListener(method, listener) {
const listeners = this.listeners(method);
removeEventListener(type, listener) {
const listeners = this.listeners(type);

for (let i = 0; i < listeners.length; i++) {
if (listeners[i] === listener || listeners[i]._listener === listener) {
this.removeListener(method, listeners[i]);
this.removeListener(type, listeners[i]);
}
}
}
Expand Down

0 comments on commit e4b35da

Please sign in to comment.