From a266b21e21a676d978482f947dba34fc2e60291a Mon Sep 17 00:00:00 2001 From: Luigi Pinca Date: Thu, 7 May 2020 10:56:02 +0200 Subject: [PATCH] Fix nits --- doc/ws.md | 13 ++++++------- lib/event-target.js | 47 +++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/doc/ws.md b/doc/ws.md index 40bb56a71..6317f9be7 100644 --- a/doc/ws.md +++ b/doc/ws.md @@ -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) @@ -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: @@ -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. diff --git a/lib/event-target.js b/lib/event-target.js index edac08e63..d4bb4bbf4 100644 --- a/lib/event-target.js +++ b/lib/event-target.js @@ -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) { @@ -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); } },