Coding style

For lack of a better word/phrase, I'm calling all changes coding style.
I'm targeting readability through naming and syntax.
This commit is contained in:
Lyubo Marinov 2017-04-27 15:21:01 -05:00 committed by hristoterezov
parent 3e055c1201
commit e9dc9c47a9
9 changed files with 119 additions and 115 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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]
});
}

View File

@ -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;
}
}

View File

@ -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
);
}
}

View File

@ -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;
}
}

View File

@ -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));
}
}

View File

@ -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({