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 c8e941c commit a266b21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 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
47 changes: 22 additions & 25 deletions lib/event-target.js
Expand Up @@ -111,11 +111,14 @@ const EventTarget = {
*
* @param {String} method 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(method, listener, options) {
if (typeof listener !== 'function') return;

function onMessage(data) {
Expand All @@ -134,28 +137,22 @@ 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 type = options && options.once ? 'once' : 'on';

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

Expand Down

0 comments on commit a266b21

Please sign in to comment.