From e9dc9c47a9e03e35bb792347c19a64b0bda6b156 Mon Sep 17 00:00:00 2001 From: Lyubo Marinov Date: Thu, 27 Apr 2017 15:21:01 -0500 Subject: [PATCH] Coding style For lack of a better word/phrase, I'm calling all changes coding style. I'm targeting readability through naming and syntax. --- .jshintignore | 2 +- modules/API/API.js | 33 +++--- modules/API/constants.js | 3 +- modules/API/external/external_api.js | 16 ++- modules/remotecontrol/Receiver.js | 26 ++--- modules/remotecontrol/RemoteControl.js | 10 +- .../transport/PostMessageTransportBackend.js | 100 ++++++++++-------- modules/transport/Transport.js | 41 ++++--- modules/transport/index.js | 3 +- 9 files changed, 119 insertions(+), 115 deletions(-) diff --git a/.jshintignore b/.jshintignore index f37de4d03..bf8675b36 100644 --- a/.jshintignore +++ b/.jshintignore @@ -8,9 +8,9 @@ node_modules/ # The following are checked by ESLint with the maximum configuration which # supersedes JSHint. flow-typed/ -react/ modules/API/ modules/transport/ +react/ # The following are checked by ESLint with the minimum configuration which does # not supersede JSHint but take advantage of advanced language features such as diff --git a/modules/API/API.js b/modules/API/API.js index 0c1edd2bc..18c7f3e67 100644 --- a/modules/API/API.js +++ b/modules/API/API.js @@ -37,9 +37,7 @@ function initCommands() { 'email': APP.conference.changeLocalEmail, 'avatar-url': APP.conference.changeLocalAvatarUrl }; - transport.on('event', event => { - const { name, data } = event; - + transport.on('event', ({ data, name }) => { if (name && commands[name]) { commands[name](...data); @@ -101,15 +99,18 @@ class API { * module. * @returns {void} */ - init(options = {}) { - if (!shouldBeEnabled() && !options.forceEnable) { + init({ forceEnable } = {}) { + if (!shouldBeEnabled() && !forceEnable) { return; } /** * Current status (enabled/disabled) of API. + * + * @private + * @type {boolean} */ - this.enabled = true; + this._enabled = true; APP.conference.addListener( JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED, @@ -126,10 +127,10 @@ class API { * @returns {void} */ _sendEvent(name, data = {}) { - if (this.enabled) { + if (this._enabled) { transport.sendEvent({ - name, - data + data, + name }); } } @@ -151,17 +152,15 @@ class API { * @param {Object} options - Object with the message properties. * @returns {void} */ - notifyReceivedChatMessage(options = {}) { - const { id, nick, body, ts } = options; - + notifyReceivedChatMessage({ body, id, nick, ts } = {}) { if (APP.conference.isLocalId(id)) { return; } this._sendEvent('incoming-message', { from: id, - nick, message: body, + nick, stamp: ts }); } @@ -198,8 +197,8 @@ class API { */ notifyDisplayNameChanged(id, displayname) { this._sendEvent('display-name-change', { - id, - displayname + displayname, + id }); } @@ -241,8 +240,8 @@ class API { * @returns {void} */ dispose() { - if (this.enabled) { - this.enabled = false; + if (this._enabled) { + this._enabled = false; APP.conference.removeListener( JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED, onDesktopSharingEnabledChanged); diff --git a/modules/API/constants.js b/modules/API/constants.js index 8ecb608bd..e59fbabe0 100644 --- a/modules/API/constants.js +++ b/modules/API/constants.js @@ -3,5 +3,4 @@ declare var getConfigParamsFromUrl: Function; /** * JitsiMeetExternalAPI id - unique for a webpage. */ -export const API_ID - = getConfigParamsFromUrl().jitsi_meet_external_api_id; +export const API_ID = getConfigParamsFromUrl().jitsi_meet_external_api_id; diff --git a/modules/API/external/external_api.js b/modules/API/external/external_api.js index 23e1ac13d..24ec9b7c9 100644 --- a/modules/API/external/external_api.js +++ b/modules/API/external/external_api.js @@ -81,8 +81,8 @@ function configToURLParamsArray(config = {}) { for (const key in config) { // eslint-disable-line guard-for-in try { - params.push(`${key}=${ - encodeURIComponent(JSON.stringify(config[key]))}`); + params.push( + `${key}=${encodeURIComponent(JSON.stringify(config[key]))}`); } catch (e) { console.warn(`Error encoding ${key}: ${e}`); } @@ -233,14 +233,10 @@ class JitsiMeetExternalAPI extends EventEmitter { */ _setupListeners() { - this._transport.on('event', event => { - const { name, data } = event; - + this._transport.on('event', ({ data, name }) => { if (name === 'participant-joined') { changeParticipantNumber(this, 1); - } - - if (name === 'participant-left') { + } else if (name === 'participant-left') { changeParticipantNumber(this, -1); } @@ -364,8 +360,8 @@ class JitsiMeetExternalAPI extends EventEmitter { return; } this._transport.sendEvent({ - name: commands[name], - data: args + data: args, + name: commands[name] }); } diff --git a/modules/remotecontrol/Receiver.js b/modules/remotecontrol/Receiver.js index 5339158da..b6e31150a 100644 --- a/modules/remotecontrol/Receiver.js +++ b/modules/remotecontrol/Receiver.js @@ -1,4 +1,5 @@ -/* global APP, JitsiMeetJS, interfaceConfig, config */ +/* global APP, config, interfaceConfig, JitsiMeetJS */ + import * as JitsiMeetConferenceEvents from '../../ConferenceEvents'; import { DISCO_REMOTE_CONTROL_FEATURE, @@ -10,8 +11,8 @@ import { transport } from '../transport'; import RemoteControlParticipant from "./RemoteControlParticipant"; -const logger = require("jitsi-meet-logger").getLogger(__filename); const ConferenceEvents = JitsiMeetJS.events.conference; +const logger = require("jitsi-meet-logger").getLogger(__filename); /** * This class represents the receiver party for a remote controller session. @@ -33,9 +34,9 @@ export default class Receiver extends RemoteControlParticipant { this._hangupListener = this._onHangup.bind(this); // We expect here that even if we receive the supported event earlier // it will be cached and we'll receive it. - transport.on('event', data => { - if(data.name === REMOTE_CONTROL_EVENT_TYPE) { - this._onRemoteControlAPIEvent(data.event); + transport.on('event', ({ event, name }) => { + if(name === REMOTE_CONTROL_EVENT_TYPE) { + this._onRemoteControlAPIEvent(event); return true; } @@ -193,7 +194,7 @@ export default class Receiver extends RemoteControlParticipant { } this._sendRemoteControlEvent(userId, { type: EVENT_TYPES.permissions, - action: action + action }); } @@ -204,13 +205,12 @@ export default class Receiver extends RemoteControlParticipant { */ _onRemoteControlAPIEvent(event) { switch(event.type) { - case EVENT_TYPES.supported: - this._onRemoteControlSupported(); - break; - case EVENT_TYPES.permissions: - this._onRemoteControlPermissionsEvent( - event.userId, event.action); - break; + case EVENT_TYPES.supported: + this._onRemoteControlSupported(); + break; + case EVENT_TYPES.permissions: + this._onRemoteControlPermissionsEvent(event.userId, event.action); + break; } } diff --git a/modules/remotecontrol/RemoteControl.js b/modules/remotecontrol/RemoteControl.js index 4208b2603..466662471 100644 --- a/modules/remotecontrol/RemoteControl.js +++ b/modules/remotecontrol/RemoteControl.js @@ -25,8 +25,9 @@ class RemoteControl { * enabled or not. */ init() { - if(config.disableRemoteControl || this.initialized - || !APP.conference.isDesktopSharingEnabled) { + if(config.disableRemoteControl + || this.initialized + || !APP.conference.isDesktopSharingEnabled) { return; } logger.log("Initializing remote control."); @@ -42,8 +43,9 @@ class RemoteControl { * the user supports remote control and with false if not. */ checkUserRemoteControlSupport(user) { - return user.getFeatures().then(features => - features.has(DISCO_REMOTE_CONTROL_FEATURE), () => false + return user.getFeatures().then( + features => features.has(DISCO_REMOTE_CONTROL_FEATURE), + () => false ); } } diff --git a/modules/transport/PostMessageTransportBackend.js b/modules/transport/PostMessageTransportBackend.js index e2e03bd4d..f79c96211 100644 --- a/modules/transport/PostMessageTransportBackend.js +++ b/modules/transport/PostMessageTransportBackend.js @@ -1,5 +1,14 @@ import Postis from 'postis'; +/** + * The default options for postis. + * + * @type {Object} + */ +const DEFAULT_POSTIS_OPTIONS = { + window: window.opener || window.parent +}; + /** * The list of methods of incomming postis messages that we have to support for * backward compatability for the users that are directly sending messages to @@ -7,9 +16,18 @@ import Postis from 'postis'; * * @type {string[]} */ -const legacyIncomingMethods = [ 'display-name', 'toggle-audio', 'toggle-video', - 'toggle-film-strip', 'toggle-chat', 'toggle-contact-list', - 'toggle-share-screen', 'video-hangup', 'email', 'avatar-url' ]; +const LEGACY_INCOMING_METHODS = [ + 'avatar-url', + 'display-name', + 'email', + 'toggle-audio', + 'toggle-chat', + 'toggle-contact-list', + 'toggle-film-strip', + 'toggle-share-screen', + 'toggle-video', + 'video-hangup' +]; /** * The list of methods of outgoing postis messages that we have to support for @@ -18,10 +36,16 @@ const legacyIncomingMethods = [ 'display-name', 'toggle-audio', 'toggle-video', * * @type {string[]} */ -const legacyOutgoingMethods = [ 'display-name-change', 'incoming-message', - 'outgoing-message', 'participant-joined', 'participant-left', - 'video-ready-to-close', 'video-conference-joined', - 'video-conference-left' ]; +const LEGACY_OUTGOING_METHODS = [ + 'display-name-change', + 'incoming-message', + 'outgoing-message', + 'participant-joined', + 'participant-left', + 'video-conference-joined', + 'video-conference-left', + 'video-ready-to-close' +]; /** * The postis method used for all messages. @@ -30,15 +54,6 @@ const legacyOutgoingMethods = [ 'display-name-change', 'incoming-message', */ const POSTIS_METHOD_NAME = 'data'; -/** - * The default options for postis. - * - * @type {Object} - */ -const defaultPostisOptions = { - window: window.opener || window.parent -}; - /** * Implements message transport using the postMessage API. */ @@ -49,27 +64,30 @@ export default class PostMessageTransportBackend { * @param {Object} options - Optional parameters for configuration of the * transport. */ - constructor(options = {}) { - const postisOptions = Object.assign({}, defaultPostisOptions, - options.postisOptions); + constructor({ enableLegacyFormat, postisOptions } = {}) { + this.postis = Postis({ + ...DEFAULT_POSTIS_OPTIONS, + ...postisOptions + }); - this.postis = Postis(postisOptions); - - this._enableLegacyFormat = options.enableLegacyFormat; + this._enableLegacyFormat = enableLegacyFormat; if (!this._enableLegacyFormat) { // backward compatability - legacyIncomingMethods.forEach(method => - this.postis.listen(method, - params => this._onPostisDataReceived(method, params))); + LEGACY_INCOMING_METHODS.forEach(method => + this.postis.listen( + method, + params => this._legacyDataReceiveCallback(method, params))); } - this.postis.listen(POSTIS_METHOD_NAME, data => - this._dataReceivedCallBack(data)); - - this._dataReceivedCallBack = () => { - // do nothing until real callback is set; + this._receiveCallback = () => { + // Do nothing until a callback is set by the consumer of + // PostMessageTransportBackend via setReceiveCallback. }; + + this.postis.listen( + POSTIS_METHOD_NAME, + data => this._receiveCallback(data)); } /** @@ -79,15 +97,13 @@ export default class PostMessageTransportBackend { * @param {Any} params - The params property from postis data object. * @returns {void} */ - _onPostisDataReceived(method, params = {}) { - const newData = { + _legacyDataReceiveCallback(method, params = {}) { + this._receiveCallback({ data: { name: method, data: params } - }; - - this._dataReceivedCallBack(newData); + }); } /** @@ -96,13 +112,11 @@ export default class PostMessageTransportBackend { * @param {Object} data - The data to be sent. * @returns {void} */ - _sendLegacyData(data) { - const method = data.name; - - if (method && legacyOutgoingMethods.indexOf(method) !== -1) { + _sendLegacyData({ data, name }) { + if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) { this.postis.send({ - method, - params: data.data + method: name, + params: data }); } } @@ -143,7 +157,7 @@ export default class PostMessageTransportBackend { * @param {Function} callback - The new callback. * @returns {void} */ - setDataReceivedCallback(callback) { - this._dataReceivedCallBack = callback; + setReceiveCallback(callback) { + this._receiveCallback = callback; } } diff --git a/modules/transport/Transport.js b/modules/transport/Transport.js index 2fa0ae3d9..52d6cca6b 100644 --- a/modules/transport/Transport.js +++ b/modules/transport/Transport.js @@ -14,9 +14,7 @@ export default class Transport { * @param {Object} options - Optional parameters for configuration of the * transport. */ - constructor(options = {}) { - const { transport } = options; - + constructor({ transport } = {}) { this._requestID = 0; this._responseHandlers = new Map(); @@ -66,9 +64,9 @@ export default class Transport { this.emit('request', data.data, (result, error) => { this._transport.send({ type: MESSAGE_TYPE_RESPONSE, - result, error, - id: data.id + id: data.id, + result }); }); } else { @@ -97,22 +95,19 @@ export default class Transport { */ emit(eventName, ...args) { const listenersForEvent = this._listeners.get(eventName); - - if (!listenersForEvent || listenersForEvent.size === 0) { - this._unprocessedMessages.add(args); - - return false; - } - let isProcessed = false; - listenersForEvent.forEach(listener => { - isProcessed = listener(...args) || isProcessed; - }); + if (listenersForEvent && listenersForEvent.size) { + listenersForEvent.forEach(listener => { + isProcessed = listener(...args) || isProcessed; + }); + } if (!isProcessed) { this._unprocessedMessages.add(args); } + + return isProcessed; } /** @@ -146,7 +141,7 @@ export default class Transport { /** * Removes all listeners, or those of the specified eventName. * - * @param {string} [eventName] - The name of the event. + * @param {string} eventName - The name of the event. * @returns {Transport} References to the instance of Transport class, so * that calls can be chained. */ @@ -204,13 +199,13 @@ export default class Transport { if (!this._transport) { return Promise.reject(new Error('No transport defined!')); } + this._requestID++; + const id = this._requestID; return new Promise((resolve, reject) => { - this._responseHandlers.set(this._requestID, response => { - const { result, error } = response; - + this._responseHandlers.set(this._requestID, ({ error, result }) => { if (result) { resolve(result); } else if (error) { @@ -221,9 +216,9 @@ export default class Transport { }); this._transport.send({ - id, type: MESSAGE_TYPE_REQUEST, - data + data, + id }); }); } @@ -236,8 +231,8 @@ export default class Transport { */ setTransport(transport) { this._disposeTransport(); + this._transport = transport; - this._transport.setDataReceivedCallback( - this._onDataReceived.bind(this)); + this._transport.setReceiveCallback(this._onDataReceived.bind(this)); } } diff --git a/modules/transport/index.js b/modules/transport/index.js index d83160e03..788031f63 100644 --- a/modules/transport/index.js +++ b/modules/transport/index.js @@ -12,8 +12,7 @@ import PostMessageTransportBackend from './PostMessageTransportBackend'; const postisOptions = {}; if (typeof API_ID === 'number') { - postisOptions.scope - = `jitsi_meet_external_api_${API_ID}`; + postisOptions.scope = `jitsi_meet_external_api_${API_ID}`; } export const transport = new Transport({