Browse Source

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 years ago
parent
commit
b49c1c6ba2

+ 8
- 1
modules/API/API.js View File

1
 import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
1
 import * as JitsiMeetConferenceEvents from '../../ConferenceEvents';
2
-import { transport } from '../transport';
2
+import { getJitsiMeetTransport } from '../transport';
3
 
3
 
4
 import { API_ID } from './constants';
4
 import { API_ID } from './constants';
5
 
5
 
18
  */
18
  */
19
 let initialScreenSharingState = false;
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
  * Initializes supported commands.
29
  * Initializes supported commands.
23
  *
30
  *

+ 3
- 1
modules/API/constants.js View File

3
 /**
3
 /**
4
  * JitsiMeetExternalAPI id - unique for a webpage.
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 View File

1
 import EventEmitter from 'events';
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
 const logger = require('jitsi-meet-logger').getLogger(__filename);
8
 const logger = require('jitsi-meet-logger').getLogger(__filename);
8
 
9
 
184
         this._createIFrame(Math.max(height, MIN_HEIGHT),
185
         this._createIFrame(Math.max(height, MIN_HEIGHT),
185
             Math.max(width, MIN_WIDTH));
186
             Math.max(width, MIN_WIDTH));
186
         this._transport = new Transport({
187
         this._transport = new Transport({
187
-            transport: new PostMessageTransportBackend({
188
+            backend: new PostMessageTransportBackend({
188
                 postisOptions: {
189
                 postisOptions: {
189
                     scope: `jitsi_meet_external_api_${id}`,
190
                     scope: `jitsi_meet_external_api_${id}`,
190
                     window: this.frame.contentWindow
191
                     window: this.frame.contentWindow

+ 8
- 1
modules/remotecontrol/Receiver.js View File

7
     PERMISSIONS_ACTIONS,
7
     PERMISSIONS_ACTIONS,
8
     REMOTE_CONTROL_EVENT_TYPE
8
     REMOTE_CONTROL_EVENT_TYPE
9
 } from "../../service/remotecontrol/Constants";
9
 } from "../../service/remotecontrol/Constants";
10
-import { transport } from '../transport';
10
+import { getJitsiMeetTransport } from '../transport';
11
 
11
 
12
 import RemoteControlParticipant from "./RemoteControlParticipant";
12
 import RemoteControlParticipant from "./RemoteControlParticipant";
13
 
13
 
14
 const ConferenceEvents = JitsiMeetJS.events.conference;
14
 const ConferenceEvents = JitsiMeetJS.events.conference;
15
 const logger = require("jitsi-meet-logger").getLogger(__filename);
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
  * This class represents the receiver party for a remote controller session.
25
  * This class represents the receiver party for a remote controller session.
19
  * It handles "remote-control-event" events and sends them to the
26
  * It handles "remote-control-event" events and sends them to the

+ 15
- 7
modules/transport/PostMessageTransportBackend.js View File

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
  * Jitsi Meet (without using external_api.js)
15
  * Jitsi Meet (without using external_api.js)
16
  *
16
  *
17
  * @type {string[]}
17
  * @type {string[]}
31
 
31
 
32
 /**
32
 /**
33
  * The list of methods of outgoing postis messages that we have to support for
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
  * postis messages send by Jitsi Meet(without using external_api.js).
35
  * postis messages send by Jitsi Meet(without using external_api.js).
36
  *
36
  *
37
  * @type {string[]}
37
  * @type {string[]}
70
             ...postisOptions
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
         this._enableLegacyFormat = enableLegacyFormat;
81
         this._enableLegacyFormat = enableLegacyFormat;
74
 
82
 
75
-        if (!this._enableLegacyFormat) {
76
-            // backward compatability
83
+        if (this._enableLegacyFormat) {
84
+            // backward compatibility
77
             LEGACY_INCOMING_METHODS.forEach(method =>
85
             LEGACY_INCOMING_METHODS.forEach(method =>
78
                 this.postis.listen(
86
                 this.postis.listen(
79
                     method,
87
                     method,
91
     }
99
     }
92
 
100
 
93
     /**
101
     /**
94
-     * Handles incomming legacy postis data.
102
+     * Handles incoming legacy postis data.
95
      *
103
      *
96
      * @param {string} method - The method property from postis data object.
104
      * @param {string} method - The method property from postis data object.
97
      * @param {Any} params - The params property from postis data object.
105
      * @param {Any} params - The params property from postis data object.
142
             params: data
150
             params: data
143
         });
151
         });
144
 
152
 
145
-        if (!this._enableLegacyFormat) {
153
+        if (this._enableLegacyFormat) {
146
             // For the legacy use case we don't need any new fields defined in
154
             // For the legacy use case we don't need any new fields defined in
147
             // Transport class. That's why we are passing only the original
155
             // Transport class. That's why we are passing only the original
148
             // object passed by the consumer of the Transport class which is
156
             // object passed by the consumer of the Transport class which is

+ 58
- 31
modules/transport/Transport.js View File

5
 } from './constants';
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
 export default class Transport {
11
 export default class Transport {
11
     /**
12
     /**
12
      * Creates new instance.
13
      * Creates new instance.
13
      *
14
      *
14
      * @param {Object} options - Optional parameters for configuration of the
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
         this._requestID = 0;
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
         this._responseHandlers = new Map();
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
         this._listeners = new Map();
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
         this._unprocessedMessages = new Set();
50
         this._unprocessedMessages = new Set();
25
 
51
 
52
+        /**
53
+         * Alias.
54
+         */
26
         this.addListener = this.on;
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
      * @returns {void}
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
      * @param {Object} data - The data.
77
      * @param {Object} data - The data.
49
      * @returns {void}
78
      * @returns {void}
56
                 handler(data);
85
                 handler(data);
57
                 this._responseHandlers.delete(data.id);
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
             this.emit('request', data.data, (result, error) => {
89
             this.emit('request', data.data, (result, error) => {
65
-                this._transport.send({
90
+                this._backend.send({
66
                     type: MESSAGE_TYPE_RESPONSE,
91
                     type: MESSAGE_TYPE_RESPONSE,
67
                     error,
92
                     error,
68
                     id: data.id,
93
                     id: data.id,
83
         this._responseHandlers.clear();
108
         this._responseHandlers.clear();
84
         this._unprocessedMessages.clear();
109
         this._unprocessedMessages.clear();
85
         this.removeAllListeners();
110
         this.removeAllListeners();
86
-        this._disposeTransport();
111
+        this._disposeBackend();
87
     }
112
     }
88
 
113
 
89
     /**
114
     /**
91
      * the order they were registered, passing the supplied arguments to each.
116
      * the order they were registered, passing the supplied arguments to each.
92
      *
117
      *
93
      * @param {string} eventName -  The name of the event.
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
     emit(eventName, ...args) {
122
     emit(eventName, ...args) {
97
         const listenersForEvent = this._listeners.get(eventName);
123
         const listenersForEvent = this._listeners.get(eventName);
141
     /**
167
     /**
142
      * Removes all listeners, or those of the specified eventName.
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
      * @returns {Transport} References to the instance of Transport class, so
172
      * @returns {Transport} References to the instance of Transport class, so
146
      * that calls can be chained.
173
      * that calls can be chained.
147
      */
174
      */
181
      * @returns {void}
208
      * @returns {void}
182
      */
209
      */
183
     sendEvent(data = {}) {
210
     sendEvent(data = {}) {
184
-        if (this._transport) {
185
-            this._transport.send({
211
+        if (this._backend) {
212
+            this._backend.send({
186
                 type: MESSAGE_TYPE_EVENT,
213
                 type: MESSAGE_TYPE_EVENT,
187
                 data
214
                 data
188
             });
215
             });
196
      * @returns {Promise}
223
      * @returns {Promise}
197
      */
224
      */
198
     sendRequest(data) {
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
         this._requestID++;
230
         this._requestID++;
215
                 }
242
                 }
216
             });
243
             });
217
 
244
 
218
-            this._transport.send({
245
+            this._backend.send({
219
                 type: MESSAGE_TYPE_REQUEST,
246
                 type: MESSAGE_TYPE_REQUEST,
220
                 data,
247
                 data,
221
                 id
248
                 id
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
      * @returns {void}
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 View File

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
 import { getJitsiMeetGlobalNS } from '../util/helpers';
4
 import { getJitsiMeetGlobalNS } from '../util/helpers';
3
 
5
 
4
 import Transport from './Transport';
6
 import Transport from './Transport';
5
 import PostMessageTransportBackend from './PostMessageTransportBackend';
7
 import PostMessageTransportBackend from './PostMessageTransportBackend';
6
 
8
 
9
+export {
10
+    Transport,
11
+    PostMessageTransportBackend
12
+};
13
+
7
 /**
14
 /**
8
  * Option for the default low level transport.
15
  * Option for the default low level transport.
9
  *
16
  *
15
     postisOptions.scope = `jitsi_meet_external_api_${API_ID}`;
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
  * Sets the transport to passed transport.
51
  * Sets the transport to passed transport.
27
  *
52
  *
28
- * @param {Object} newTransport - The new transport.
53
+ * @param {Object} externalTransportBackend - The new transport.
29
  * @returns {void}
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 View File

84
  * we store everything that needs to be global (for some reason).
84
  * we store everything that needs to be global (for some reason).
85
  */
85
  */
86
 export function getJitsiMeetGlobalNS() {
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 View File

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

Loading…
Cancel
Save