Przeglądaj źródła

Coding style

For lack of a better word/phrase, I'm calling all changes coding style.
I'm targeting readability through naming and syntax.
master
Lyubo Marinov 8 lat temu
rodzic
commit
e9dc9c47a9

+ 1
- 1
.jshintignore Wyświetl plik

@@ -8,9 +8,9 @@ node_modules/
8 8
 # The following are checked by ESLint with the maximum configuration which
9 9
 # supersedes JSHint.
10 10
 flow-typed/
11
-react/
12 11
 modules/API/
13 12
 modules/transport/
13
+react/
14 14
 
15 15
 # The following are checked by ESLint with the minimum configuration which does
16 16
 # not supersede JSHint but take advantage of advanced language features such as

+ 16
- 17
modules/API/API.js Wyświetl plik

@@ -37,9 +37,7 @@ function initCommands() {
37 37
         'email': APP.conference.changeLocalEmail,
38 38
         'avatar-url': APP.conference.changeLocalAvatarUrl
39 39
     };
40
-    transport.on('event', event => {
41
-        const { name, data } = event;
42
-
40
+    transport.on('event', ({ data, name }) => {
43 41
         if (name && commands[name]) {
44 42
             commands[name](...data);
45 43
 
@@ -101,15 +99,18 @@ class API {
101 99
      * module.
102 100
      * @returns {void}
103 101
      */
104
-    init(options = {}) {
105
-        if (!shouldBeEnabled() && !options.forceEnable) {
102
+    init({ forceEnable } = {}) {
103
+        if (!shouldBeEnabled() && !forceEnable) {
106 104
             return;
107 105
         }
108 106
 
109 107
         /**
110 108
          * Current status (enabled/disabled) of API.
109
+         *
110
+         * @private
111
+         * @type {boolean}
111 112
          */
112
-        this.enabled = true;
113
+        this._enabled = true;
113 114
 
114 115
         APP.conference.addListener(
115 116
             JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
@@ -126,10 +127,10 @@ class API {
126 127
      * @returns {void}
127 128
      */
128 129
     _sendEvent(name, data = {}) {
129
-        if (this.enabled) {
130
+        if (this._enabled) {
130 131
             transport.sendEvent({
131
-                name,
132
-                data
132
+                data,
133
+                name
133 134
             });
134 135
         }
135 136
     }
@@ -151,17 +152,15 @@ class API {
151 152
      * @param {Object} options - Object with the message properties.
152 153
      * @returns {void}
153 154
      */
154
-    notifyReceivedChatMessage(options = {}) {
155
-        const { id, nick, body, ts } = options;
156
-
155
+    notifyReceivedChatMessage({ body, id, nick, ts } = {}) {
157 156
         if (APP.conference.isLocalId(id)) {
158 157
             return;
159 158
         }
160 159
 
161 160
         this._sendEvent('incoming-message', {
162 161
             from: id,
163
-            nick,
164 162
             message: body,
163
+            nick,
165 164
             stamp: ts
166 165
         });
167 166
     }
@@ -198,8 +197,8 @@ class API {
198 197
      */
199 198
     notifyDisplayNameChanged(id, displayname) {
200 199
         this._sendEvent('display-name-change', {
201
-            id,
202
-            displayname
200
+            displayname,
201
+            id
203 202
         });
204 203
     }
205 204
 
@@ -241,8 +240,8 @@ class API {
241 240
      * @returns {void}
242 241
      */
243 242
     dispose() {
244
-        if (this.enabled) {
245
-            this.enabled = false;
243
+        if (this._enabled) {
244
+            this._enabled = false;
246 245
             APP.conference.removeListener(
247 246
                 JitsiMeetConferenceEvents.DESKTOP_SHARING_ENABLED_CHANGED,
248 247
                 onDesktopSharingEnabledChanged);

+ 1
- 2
modules/API/constants.js Wyświetl plik

@@ -3,5 +3,4 @@ declare var getConfigParamsFromUrl: Function;
3 3
 /**
4 4
  * JitsiMeetExternalAPI id - unique for a webpage.
5 5
  */
6
-export const API_ID
7
-    = getConfigParamsFromUrl().jitsi_meet_external_api_id;
6
+export const API_ID = getConfigParamsFromUrl().jitsi_meet_external_api_id;

+ 6
- 10
modules/API/external/external_api.js Wyświetl plik

@@ -81,8 +81,8 @@ function configToURLParamsArray(config = {}) {
81 81
 
82 82
     for (const key in config) { // eslint-disable-line guard-for-in
83 83
         try {
84
-            params.push(`${key}=${
85
-                encodeURIComponent(JSON.stringify(config[key]))}`);
84
+            params.push(
85
+                `${key}=${encodeURIComponent(JSON.stringify(config[key]))}`);
86 86
         } catch (e) {
87 87
             console.warn(`Error encoding ${key}: ${e}`);
88 88
         }
@@ -233,14 +233,10 @@ class JitsiMeetExternalAPI extends EventEmitter {
233 233
      */
234 234
     _setupListeners() {
235 235
 
236
-        this._transport.on('event', event => {
237
-            const { name, data } = event;
238
-
236
+        this._transport.on('event', ({ data, name }) => {
239 237
             if (name === 'participant-joined') {
240 238
                 changeParticipantNumber(this, 1);
241
-            }
242
-
243
-            if (name === 'participant-left') {
239
+            } else if (name === 'participant-left') {
244 240
                 changeParticipantNumber(this, -1);
245 241
             }
246 242
 
@@ -364,8 +360,8 @@ class JitsiMeetExternalAPI extends EventEmitter {
364 360
             return;
365 361
         }
366 362
         this._transport.sendEvent({
367
-            name: commands[name],
368
-            data: args
363
+            data: args,
364
+            name: commands[name]
369 365
         });
370 366
     }
371 367
 

+ 13
- 13
modules/remotecontrol/Receiver.js Wyświetl plik

@@ -1,4 +1,5 @@
1
-/* global APP, JitsiMeetJS, interfaceConfig, config */
1
+/* global APP, config, interfaceConfig, JitsiMeetJS */
2
+
2 3
 import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
3 4
 import {
4 5
     DISCO_REMOTE_CONTROL_FEATURE,
@@ -10,8 +11,8 @@ import { transport } from '../transport';
10 11
 
11 12
 import RemoteControlParticipant from "./RemoteControlParticipant";
12 13
 
13
-const logger = require("jitsi-meet-logger").getLogger(__filename);
14 14
 const ConferenceEvents = JitsiMeetJS.events.conference;
15
+const logger = require("jitsi-meet-logger").getLogger(__filename);
15 16
 
16 17
 /**
17 18
  * This class represents the receiver party for a remote controller session.
@@ -33,9 +34,9 @@ export default class Receiver extends RemoteControlParticipant {
33 34
         this._hangupListener = this._onHangup.bind(this);
34 35
         // We expect here that even if we receive the supported event earlier
35 36
         // it will be cached and we'll receive it.
36
-        transport.on('event', data => {
37
-            if(data.name === REMOTE_CONTROL_EVENT_TYPE) {
38
-                this._onRemoteControlAPIEvent(data.event);
37
+        transport.on('event', ({ event, name }) => {
38
+            if(name === REMOTE_CONTROL_EVENT_TYPE) {
39
+                this._onRemoteControlAPIEvent(event);
39 40
 
40 41
                 return true;
41 42
             }
@@ -193,7 +194,7 @@ export default class Receiver extends RemoteControlParticipant {
193 194
         }
194 195
         this._sendRemoteControlEvent(userId, {
195 196
             type: EVENT_TYPES.permissions,
196
-            action: action
197
+            action
197 198
         });
198 199
     }
199 200
 
@@ -204,13 +205,12 @@ export default class Receiver extends RemoteControlParticipant {
204 205
      */
205 206
     _onRemoteControlAPIEvent(event) {
206 207
         switch(event.type) {
207
-            case EVENT_TYPES.supported:
208
-                this._onRemoteControlSupported();
209
-                break;
210
-            case EVENT_TYPES.permissions:
211
-                this._onRemoteControlPermissionsEvent(
212
-                    event.userId, event.action);
213
-                break;
208
+        case EVENT_TYPES.supported:
209
+            this._onRemoteControlSupported();
210
+            break;
211
+        case EVENT_TYPES.permissions:
212
+            this._onRemoteControlPermissionsEvent(event.userId, event.action);
213
+            break;
214 214
         }
215 215
     }
216 216
 

+ 6
- 4
modules/remotecontrol/RemoteControl.js Wyświetl plik

@@ -25,8 +25,9 @@ class RemoteControl {
25 25
      * enabled or not.
26 26
      */
27 27
     init() {
28
-        if(config.disableRemoteControl || this.initialized
29
-            || !APP.conference.isDesktopSharingEnabled) {
28
+        if(config.disableRemoteControl
29
+                || this.initialized
30
+                || !APP.conference.isDesktopSharingEnabled) {
30 31
             return;
31 32
         }
32 33
         logger.log("Initializing remote control.");
@@ -42,8 +43,9 @@ class RemoteControl {
42 43
      * the user supports remote control and with false if not.
43 44
      */
44 45
     checkUserRemoteControlSupport(user) {
45
-        return user.getFeatures().then(features =>
46
-            features.has(DISCO_REMOTE_CONTROL_FEATURE), () => false
46
+        return user.getFeatures().then(
47
+            features => features.has(DISCO_REMOTE_CONTROL_FEATURE),
48
+            () => false
47 49
         );
48 50
     }
49 51
 }

+ 57
- 43
modules/transport/PostMessageTransportBackend.js Wyświetl plik

@@ -1,5 +1,14 @@
1 1
 import Postis from 'postis';
2 2
 
3
+/**
4
+ * The default options for postis.
5
+ *
6
+ * @type {Object}
7
+ */
8
+const DEFAULT_POSTIS_OPTIONS = {
9
+    window: window.opener || window.parent
10
+};
11
+
3 12
 /**
4 13
  * The list of methods of incomming postis messages that we have to support for
5 14
  * backward compatability for the users that are directly sending messages to
@@ -7,9 +16,18 @@ import Postis from 'postis';
7 16
  *
8 17
  * @type {string[]}
9 18
  */
10
-const legacyIncomingMethods = [ 'display-name', 'toggle-audio', 'toggle-video',
11
-    'toggle-film-strip', 'toggle-chat', 'toggle-contact-list',
12
-    'toggle-share-screen', 'video-hangup', 'email', 'avatar-url' ];
19
+const LEGACY_INCOMING_METHODS = [
20
+    'avatar-url',
21
+    'display-name',
22
+    'email',
23
+    'toggle-audio',
24
+    'toggle-chat',
25
+    'toggle-contact-list',
26
+    'toggle-film-strip',
27
+    'toggle-share-screen',
28
+    'toggle-video',
29
+    'video-hangup'
30
+];
13 31
 
14 32
 /**
15 33
  * 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',
18 36
  *
19 37
  * @type {string[]}
20 38
  */
21
-const legacyOutgoingMethods = [ 'display-name-change', 'incoming-message',
22
-    'outgoing-message', 'participant-joined', 'participant-left',
23
-    'video-ready-to-close', 'video-conference-joined',
24
-    'video-conference-left' ];
39
+const LEGACY_OUTGOING_METHODS = [
40
+    'display-name-change',
41
+    'incoming-message',
42
+    'outgoing-message',
43
+    'participant-joined',
44
+    'participant-left',
45
+    'video-conference-joined',
46
+    'video-conference-left',
47
+    'video-ready-to-close'
48
+];
25 49
 
26 50
 /**
27 51
  * The postis method used for all messages.
@@ -30,15 +54,6 @@ const legacyOutgoingMethods = [ 'display-name-change', 'incoming-message',
30 54
  */
31 55
 const POSTIS_METHOD_NAME = 'data';
32 56
 
33
-/**
34
- * The default options for postis.
35
- *
36
- * @type {Object}
37
- */
38
-const defaultPostisOptions = {
39
-    window: window.opener || window.parent
40
-};
41
-
42 57
 /**
43 58
  * Implements message transport using the postMessage API.
44 59
  */
@@ -49,27 +64,30 @@ export default class PostMessageTransportBackend {
49 64
      * @param {Object} options - Optional parameters for configuration of the
50 65
      * transport.
51 66
      */
52
-    constructor(options = {}) {
53
-        const postisOptions = Object.assign({}, defaultPostisOptions,
54
-            options.postisOptions);
55
-
56
-        this.postis = Postis(postisOptions);
67
+    constructor({ enableLegacyFormat, postisOptions } = {}) {
68
+        this.postis = Postis({
69
+            ...DEFAULT_POSTIS_OPTIONS,
70
+            ...postisOptions
71
+        });
57 72
 
58
-        this._enableLegacyFormat = options.enableLegacyFormat;
73
+        this._enableLegacyFormat = enableLegacyFormat;
59 74
 
60 75
         if (!this._enableLegacyFormat) {
61 76
             // backward compatability
62
-            legacyIncomingMethods.forEach(method =>
63
-                this.postis.listen(method,
64
-                    params => this._onPostisDataReceived(method, params)));
77
+            LEGACY_INCOMING_METHODS.forEach(method =>
78
+                this.postis.listen(
79
+                    method,
80
+                    params => this._legacyDataReceiveCallback(method, params)));
65 81
         }
66 82
 
67
-        this.postis.listen(POSTIS_METHOD_NAME, data =>
68
-            this._dataReceivedCallBack(data));
69
-
70
-        this._dataReceivedCallBack = () => {
71
-            // do nothing until real callback is set;
83
+        this._receiveCallback = () => {
84
+            // Do nothing until a callback is set by the consumer of
85
+            // PostMessageTransportBackend via setReceiveCallback.
72 86
         };
87
+
88
+        this.postis.listen(
89
+            POSTIS_METHOD_NAME,
90
+            data => this._receiveCallback(data));
73 91
     }
74 92
 
75 93
     /**
@@ -79,15 +97,13 @@ export default class PostMessageTransportBackend {
79 97
      * @param {Any} params - The params property from postis data object.
80 98
      * @returns {void}
81 99
      */
82
-    _onPostisDataReceived(method, params = {}) {
83
-        const newData = {
100
+    _legacyDataReceiveCallback(method, params = {}) {
101
+        this._receiveCallback({
84 102
             data: {
85 103
                 name: method,
86 104
                 data: params
87 105
             }
88
-        };
89
-
90
-        this._dataReceivedCallBack(newData);
106
+        });
91 107
     }
92 108
 
93 109
     /**
@@ -96,13 +112,11 @@ export default class PostMessageTransportBackend {
96 112
      * @param {Object} data - The data to be sent.
97 113
      * @returns {void}
98 114
      */
99
-    _sendLegacyData(data) {
100
-        const method = data.name;
101
-
102
-        if (method && legacyOutgoingMethods.indexOf(method) !== -1) {
115
+    _sendLegacyData({ data, name }) {
116
+        if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
103 117
             this.postis.send({
104
-                method,
105
-                params: data.data
118
+                method: name,
119
+                params: data
106 120
             });
107 121
         }
108 122
     }
@@ -143,7 +157,7 @@ export default class PostMessageTransportBackend {
143 157
      * @param {Function} callback - The new callback.
144 158
      * @returns {void}
145 159
      */
146
-    setDataReceivedCallback(callback) {
147
-        this._dataReceivedCallBack = callback;
160
+    setReceiveCallback(callback) {
161
+        this._receiveCallback = callback;
148 162
     }
149 163
 }

+ 18
- 23
modules/transport/Transport.js Wyświetl plik

@@ -14,9 +14,7 @@ export default class Transport {
14 14
      * @param {Object} options - Optional parameters for configuration of the
15 15
      * transport.
16 16
      */
17
-    constructor(options = {}) {
18
-        const { transport } = options;
19
-
17
+    constructor({ transport } = {}) {
20 18
         this._requestID = 0;
21 19
 
22 20
         this._responseHandlers = new Map();
@@ -66,9 +64,9 @@ export default class Transport {
66 64
             this.emit('request', data.data, (result, error) => {
67 65
                 this._transport.send({
68 66
                     type: MESSAGE_TYPE_RESPONSE,
69
-                    result,
70 67
                     error,
71
-                    id: data.id
68
+                    id: data.id,
69
+                    result
72 70
                 });
73 71
             });
74 72
         } else {
@@ -97,22 +95,19 @@ export default class Transport {
97 95
      */
98 96
     emit(eventName, ...args) {
99 97
         const listenersForEvent = this._listeners.get(eventName);
100
-
101
-        if (!listenersForEvent || listenersForEvent.size === 0) {
102
-            this._unprocessedMessages.add(args);
103
-
104
-            return false;
105
-        }
106
-
107 98
         let isProcessed = false;
108 99
 
109
-        listenersForEvent.forEach(listener => {
110
-            isProcessed = listener(...args) || isProcessed;
111
-        });
100
+        if (listenersForEvent && listenersForEvent.size) {
101
+            listenersForEvent.forEach(listener => {
102
+                isProcessed = listener(...args) || isProcessed;
103
+            });
104
+        }
112 105
 
113 106
         if (!isProcessed) {
114 107
             this._unprocessedMessages.add(args);
115 108
         }
109
+
110
+        return isProcessed;
116 111
     }
117 112
 
118 113
     /**
@@ -146,7 +141,7 @@ export default class Transport {
146 141
     /**
147 142
      * Removes all listeners, or those of the specified eventName.
148 143
      *
149
-     * @param {string} [eventName] -  The name of the event.
144
+     * @param {string} eventName - The name of the event.
150 145
      * @returns {Transport} References to the instance of Transport class, so
151 146
      * that calls can be chained.
152 147
      */
@@ -204,13 +199,13 @@ export default class Transport {
204 199
         if (!this._transport) {
205 200
             return Promise.reject(new Error('No transport defined!'));
206 201
         }
202
+
207 203
         this._requestID++;
204
+
208 205
         const id = this._requestID;
209 206
 
210 207
         return new Promise((resolve, reject) => {
211
-            this._responseHandlers.set(this._requestID, response => {
212
-                const { result, error } = response;
213
-
208
+            this._responseHandlers.set(this._requestID, ({ error, result }) => {
214 209
                 if (result) {
215 210
                     resolve(result);
216 211
                 } else if (error) {
@@ -221,9 +216,9 @@ export default class Transport {
221 216
             });
222 217
 
223 218
             this._transport.send({
224
-                id,
225 219
                 type: MESSAGE_TYPE_REQUEST,
226
-                data
220
+                data,
221
+                id
227 222
             });
228 223
         });
229 224
     }
@@ -236,8 +231,8 @@ export default class Transport {
236 231
      */
237 232
     setTransport(transport) {
238 233
         this._disposeTransport();
234
+
239 235
         this._transport = transport;
240
-        this._transport.setDataReceivedCallback(
241
-            this._onDataReceived.bind(this));
236
+        this._transport.setReceiveCallback(this._onDataReceived.bind(this));
242 237
     }
243 238
 }

+ 1
- 2
modules/transport/index.js Wyświetl plik

@@ -12,8 +12,7 @@ import PostMessageTransportBackend from './PostMessageTransportBackend';
12 12
 const postisOptions = {};
13 13
 
14 14
 if (typeof API_ID === 'number') {
15
-    postisOptions.scope
16
-        = `jitsi_meet_external_api_${API_ID}`;
15
+    postisOptions.scope = `jitsi_meet_external_api_${API_ID}`;
17 16
 }
18 17
 
19 18
 export const transport = new Transport({

Ładowanie…
Anuluj
Zapisz