Bläddra i källkod

fix(transport): Code style issues and enableLegacyFormat param bug

Improves naming.
Fixing typos.
enableLegacyFormat param was working like disableLegacyFormat.
Improves the structure of transport/index.js
master
hristoterezov 8 år sedan
förälder
incheckning
b49c1c6ba2

+ 8
- 1
modules/API/API.js Visa fil

@@ -1,5 +1,5 @@
1 1
 import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
2
-import { transport } from '../transport';
2
+import { getJitsiMeetTransport } from '../transport';
3 3
 
4 4
 import { API_ID } from './constants';
5 5
 
@@ -18,6 +18,13 @@ let commands = {};
18 18
  */
19 19
 let initialScreenSharingState = false;
20 20
 
21
+/**
22
+ * The transport instance used for communication with external apps.
23
+ *
24
+ * @type {Transport}
25
+ */
26
+const transport = getJitsiMeetTransport();
27
+
21 28
 /**
22 29
  * Initializes supported commands.
23 30
  *

+ 3
- 1
modules/API/constants.js Visa fil

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

+ 5
- 4
modules/API/external/external_api.js Visa fil

@@ -1,8 +1,9 @@
1 1
 import EventEmitter from 'events';
2 2
 
3
-import PostMessageTransportBackend
4
-    from '../../transport/PostMessageTransportBackend';
5
-import Transport from '../../transport/Transport';
3
+import {
4
+    PostMessageTransportBackend,
5
+    Transport
6
+} from '../../transport';
6 7
 
7 8
 const logger = require('jitsi-meet-logger').getLogger(__filename);
8 9
 
@@ -184,7 +185,7 @@ class JitsiMeetExternalAPI extends EventEmitter {
184 185
         this._createIFrame(Math.max(height, MIN_HEIGHT),
185 186
             Math.max(width, MIN_WIDTH));
186 187
         this._transport = new Transport({
187
-            transport: new PostMessageTransportBackend({
188
+            backend: new PostMessageTransportBackend({
188 189
                 postisOptions: {
189 190
                     scope: `jitsi_meet_external_api_${id}`,
190 191
                     window: this.frame.contentWindow

+ 8
- 1
modules/remotecontrol/Receiver.js Visa fil

@@ -7,13 +7,20 @@ import {
7 7
     PERMISSIONS_ACTIONS,
8 8
     REMOTE_CONTROL_EVENT_TYPE
9 9
 } from "../../service/remotecontrol/Constants";
10
-import { transport } from '../transport';
10
+import { getJitsiMeetTransport } from '../transport';
11 11
 
12 12
 import RemoteControlParticipant from "./RemoteControlParticipant";
13 13
 
14 14
 const ConferenceEvents = JitsiMeetJS.events.conference;
15 15
 const logger = require("jitsi-meet-logger").getLogger(__filename);
16 16
 
17
+/**
18
+ * The transport instance used for communication with external apps.
19
+ *
20
+ * @type {Transport}
21
+ */
22
+const transport = getJitsiMeetTransport();
23
+
17 24
 /**
18 25
  * This class represents the receiver party for a remote controller session.
19 26
  * It handles "remote-control-event" events and sends them to the

+ 15
- 7
modules/transport/PostMessageTransportBackend.js Visa fil

@@ -10,8 +10,8 @@ const DEFAULT_POSTIS_OPTIONS = {
10 10
 };
11 11
 
12 12
 /**
13
- * The list of methods of incomming postis messages that we have to support for
14
- * backward compatability for the users that are directly sending messages to
13
+ * The list of methods of incoming postis messages that we have to support for
14
+ * backward compatibility for the users that are directly sending messages to
15 15
  * Jitsi Meet (without using external_api.js)
16 16
  *
17 17
  * @type {string[]}
@@ -31,7 +31,7 @@ const LEGACY_INCOMING_METHODS = [
31 31
 
32 32
 /**
33 33
  * The list of methods of outgoing postis messages that we have to support for
34
- * backward compatability for the users that are directly listening to the
34
+ * backward compatibility for the users that are directly listening to the
35 35
  * postis messages send by Jitsi Meet(without using external_api.js).
36 36
  *
37 37
  * @type {string[]}
@@ -70,10 +70,18 @@ export default class PostMessageTransportBackend {
70 70
             ...postisOptions
71 71
         });
72 72
 
73
+        /**
74
+         * If true PostMessageTransportBackend will process and send data using
75
+         * the legacy format and in the same time the current format. Otherwise
76
+         * all data received that is using the legacy format will be ignored and
77
+         * no data with the legacy format will be sent.
78
+         *
79
+         * @type {boolean}
80
+         */
73 81
         this._enableLegacyFormat = enableLegacyFormat;
74 82
 
75
-        if (!this._enableLegacyFormat) {
76
-            // backward compatability
83
+        if (this._enableLegacyFormat) {
84
+            // backward compatibility
77 85
             LEGACY_INCOMING_METHODS.forEach(method =>
78 86
                 this.postis.listen(
79 87
                     method,
@@ -91,7 +99,7 @@ export default class PostMessageTransportBackend {
91 99
     }
92 100
 
93 101
     /**
94
-     * Handles incomming legacy postis data.
102
+     * Handles incoming legacy postis data.
95 103
      *
96 104
      * @param {string} method - The method property from postis data object.
97 105
      * @param {Any} params - The params property from postis data object.
@@ -142,7 +150,7 @@ export default class PostMessageTransportBackend {
142 150
             params: data
143 151
         });
144 152
 
145
-        if (!this._enableLegacyFormat) {
153
+        if (this._enableLegacyFormat) {
146 154
             // For the legacy use case we don't need any new fields defined in
147 155
             // Transport class. That's why we are passing only the original
148 156
             // object passed by the consumer of the Transport class which is

+ 58
- 31
modules/transport/Transport.js Visa fil

@@ -5,45 +5,74 @@ import {
5 5
 } from './constants';
6 6
 
7 7
 /**
8
- * Stores the currnet transport that have to be used.
8
+ * Stores the currnet transport backend that have to be used. Also implements
9
+ * request/response mechanism.
9 10
  */
10 11
 export default class Transport {
11 12
     /**
12 13
      * Creates new instance.
13 14
      *
14 15
      * @param {Object} options - Optional parameters for configuration of the
15
-     * transport.
16
+     * transport backend.
16 17
      */
17
-    constructor({ transport } = {}) {
18
+    constructor({ backend } = {}) {
19
+        /**
20
+         * The request ID counter used for the id property of the request. This
21
+         * property is used to match the responses with the request.
22
+         *
23
+         * @type {number}
24
+         */
18 25
         this._requestID = 0;
19 26
 
27
+        /**
28
+         * Maps an IDs of the requests and handlers that will process the
29
+         * responses of those requests.
30
+         *
31
+         * @type {Map<number, Function>}
32
+         */
20 33
         this._responseHandlers = new Map();
21 34
 
35
+        /**
36
+         * Maps an event name and listener that have been added to the Transport
37
+         * instance.
38
+         *
39
+         * @type {Map<string, Function>}
40
+         */
22 41
         this._listeners = new Map();
23 42
 
43
+        /**
44
+         * A set with the events and requests that were received but not
45
+         * processed by any listener. They are later passed on every new
46
+         * listener until they are processed.
47
+         *
48
+         * @type {Set<Object>}
49
+         */
24 50
         this._unprocessedMessages = new Set();
25 51
 
52
+        /**
53
+         * Alias.
54
+         */
26 55
         this.addListener = this.on;
27 56
 
28
-        if (transport) {
29
-            this.setTransport(transport);
57
+        if (backend) {
58
+            this.setBackend(backend);
30 59
         }
31 60
     }
32 61
 
33 62
     /**
34
-     * Disposes the current transport.
63
+     * Disposes the current transport backend.
35 64
      *
36 65
      * @returns {void}
37 66
      */
38
-    _disposeTransport() {
39
-        if (this._transport) {
40
-            this._transport.dispose();
41
-            this._transport = null;
67
+    _disposeBackend() {
68
+        if (this._backend) {
69
+            this._backend.dispose();
70
+            this._backend = null;
42 71
         }
43 72
     }
44 73
 
45 74
     /**
46
-     * Handles incomming data from the transport.
75
+     * Handles incoming data from the transport backend.
47 76
      *
48 77
      * @param {Object} data - The data.
49 78
      * @returns {void}
@@ -56,13 +85,9 @@ export default class Transport {
56 85
                 handler(data);
57 86
                 this._responseHandlers.delete(data.id);
58 87
             }
59
-
60
-            return;
61
-        }
62
-
63
-        if (data.type === MESSAGE_TYPE_REQUEST) {
88
+        } else if (data.type === MESSAGE_TYPE_REQUEST) {
64 89
             this.emit('request', data.data, (result, error) => {
65
-                this._transport.send({
90
+                this._backend.send({
66 91
                     type: MESSAGE_TYPE_RESPONSE,
67 92
                     error,
68 93
                     id: data.id,
@@ -83,7 +108,7 @@ export default class Transport {
83 108
         this._responseHandlers.clear();
84 109
         this._unprocessedMessages.clear();
85 110
         this.removeAllListeners();
86
-        this._disposeTransport();
111
+        this._disposeBackend();
87 112
     }
88 113
 
89 114
     /**
@@ -91,7 +116,8 @@ export default class Transport {
91 116
      * the order they were registered, passing the supplied arguments to each.
92 117
      *
93 118
      * @param {string} eventName -  The name of the event.
94
-     * @returns {boolean} True if the event had listeners, false otherwise.
119
+     * @returns {boolean} True if the event has been processed by any listener,
120
+     * false otherwise.
95 121
      */
96 122
     emit(eventName, ...args) {
97 123
         const listenersForEvent = this._listeners.get(eventName);
@@ -141,7 +167,8 @@ export default class Transport {
141 167
     /**
142 168
      * Removes all listeners, or those of the specified eventName.
143 169
      *
144
-     * @param {string} eventName - The name of the event.
170
+     * @param {string} [eventName] - The name of the event. If this parameter is
171
+     * not specified all listeners will be removed.
145 172
      * @returns {Transport} References to the instance of Transport class, so
146 173
      * that calls can be chained.
147 174
      */
@@ -181,8 +208,8 @@ export default class Transport {
181 208
      * @returns {void}
182 209
      */
183 210
     sendEvent(data = {}) {
184
-        if (this._transport) {
185
-            this._transport.send({
211
+        if (this._backend) {
212
+            this._backend.send({
186 213
                 type: MESSAGE_TYPE_EVENT,
187 214
                 data
188 215
             });
@@ -196,8 +223,8 @@ export default class Transport {
196 223
      * @returns {Promise}
197 224
      */
198 225
     sendRequest(data) {
199
-        if (!this._transport) {
200
-            return Promise.reject(new Error('No transport defined!'));
226
+        if (!this._backend) {
227
+            return Promise.reject(new Error('No transport backend defined!'));
201 228
         }
202 229
 
203 230
         this._requestID++;
@@ -215,7 +242,7 @@ export default class Transport {
215 242
                 }
216 243
             });
217 244
 
218
-            this._transport.send({
245
+            this._backend.send({
219 246
                 type: MESSAGE_TYPE_REQUEST,
220 247
                 data,
221 248
                 id
@@ -224,15 +251,15 @@ export default class Transport {
224 251
     }
225 252
 
226 253
     /**
227
-     * Changes the current transport.
254
+     * Changes the current backend transport.
228 255
      *
229
-     * @param {Object} transport - The new transport that will be used.
256
+     * @param {Object} backend - The new transport backend that will be used.
230 257
      * @returns {void}
231 258
      */
232
-    setTransport(transport) {
233
-        this._disposeTransport();
259
+    setBackend(backend) {
260
+        this._disposeBackend();
234 261
 
235
-        this._transport = transport;
236
-        this._transport.setReceiveCallback(this._onDataReceived.bind(this));
262
+        this._backend = backend;
263
+        this._backend.setReceiveCallback(this._onDataReceived.bind(this));
237 264
     }
238 265
 }

+ 36
- 10
modules/transport/index.js Visa fil

@@ -1,9 +1,16 @@
1
-import { API_ID } from '../API';
1
+// FIXME: change to '../API' when we update to webpack2. If we do this now all
2
+// files from API modules will be included in external_api.js.
3
+import { API_ID } from '../API/constants';
2 4
 import { getJitsiMeetGlobalNS } from '../util/helpers';
3 5
 
4 6
 import Transport from './Transport';
5 7
 import PostMessageTransportBackend from './PostMessageTransportBackend';
6 8
 
9
+export {
10
+    Transport,
11
+    PostMessageTransportBackend
12
+};
13
+
7 14
 /**
8 15
  * Option for the default low level transport.
9 16
  *
@@ -15,19 +22,38 @@ if (typeof API_ID === 'number') {
15 22
     postisOptions.scope = `jitsi_meet_external_api_${API_ID}`;
16 23
 }
17 24
 
18
-export const transport = new Transport({
19
-    transport: new PostMessageTransportBackend({
20
-        enableLegacyFormat: true,
21
-        postisOptions
22
-    })
23
-});
25
+/**
26
+ * The instance of Transport class that will be used by Jitsi Meet.
27
+ *
28
+ * @type {Transport}
29
+ */
30
+let transport;
31
+
32
+/**
33
+ * Returns the instance of Transport class that will be used by Jitsi Meet.
34
+ *
35
+ * @returns {Transport}
36
+ */
37
+export function getJitsiMeetTransport() {
38
+    if (!transport) {
39
+        transport = new Transport({
40
+            backend: new PostMessageTransportBackend({
41
+                enableLegacyFormat: true,
42
+                postisOptions
43
+            })
44
+        });
45
+    }
46
+
47
+    return transport;
48
+}
24 49
 
25 50
 /**
26 51
  * Sets the transport to passed transport.
27 52
  *
28
- * @param {Object} newTransport - The new transport.
53
+ * @param {Object} externalTransportBackend - The new transport.
29 54
  * @returns {void}
30 55
  */
31
-getJitsiMeetGlobalNS().useNewExternalTransport = function(newTransport) {
32
-    transport.setTransport(newTransport);
56
+getJitsiMeetGlobalNS().setExternalTransportBackend = function(
57
+    externalTransportBackend) {
58
+    transport.setBackend(externalTransportBackend);
33 59
 };

+ 6
- 3
modules/util/helpers.js Visa fil

@@ -84,8 +84,11 @@ export function debounce(fn, wait = 0, options = {}) {
84 84
  * we store everything that needs to be global (for some reason).
85 85
  */
86 86
 export function getJitsiMeetGlobalNS() {
87
-    if(!window.JitsiMeetGlobalNS) {
88
-        window.JitsiMeetGlobalNS = { };
87
+    if(!window.JitsiMeetJS) {
88
+        window.JitsiMeetJS = { };
89 89
     }
90
-    return window.JitsiMeetGlobalNS;
90
+    if(!window.JitsiMeetJS.app) {
91
+        window.JitsiMeetJS.app = { };
92
+    }
93
+    return window.JitsiMeetJS.app;
91 94
 }

+ 2
- 2
react/index.web.js Visa fil

@@ -3,7 +3,7 @@
3 3
 import React from 'react';
4 4
 import ReactDOM from 'react-dom';
5 5
 
6
-import { transport } from '../modules/transport';
6
+import { getJitsiMeetTransport } from '../modules/transport';
7 7
 
8 8
 import config from './config';
9 9
 import { App } from './features/app';
@@ -36,5 +36,5 @@ window.addEventListener('beforeunload', () => {
36 36
         APP.logCollectorStarted = false;
37 37
     }
38 38
     APP.API.dispose();
39
-    transport.dispose();
39
+    getJitsiMeetTransport().dispose();
40 40
 });

Laddar…
Avbryt
Spara