Browse Source

ref(external_api): To use transport module

master
hristoterezov 8 years ago
parent
commit
3e055c1201

+ 43
- 35
modules/API/external/external_api.js View File

@@ -1,5 +1,8 @@
1 1
 import EventEmitter from 'events';
2
-import postisInit from 'postis';
2
+
3
+import PostMessageTransportBackend
4
+    from '../../transport/PostMessageTransportBackend';
5
+import Transport from '../../transport/Transport';
3 6
 
4 7
 const logger = require('jitsi-meet-logger').getLogger(__filename);
5 8
 
@@ -25,14 +28,14 @@ const commands = {
25 28
  * events expected by jitsi-meet
26 29
  */
27 30
 const events = {
28
-    displayNameChange: 'display-name-change',
29
-    incomingMessage: 'incoming-message',
30
-    outgoingMessage: 'outgoing-message',
31
-    participantJoined: 'participant-joined',
32
-    participantLeft: 'participant-left',
33
-    readyToClose: 'video-ready-to-close',
34
-    videoConferenceJoined: 'video-conference-joined',
35
-    videoConferenceLeft: 'video-conference-left'
31
+    'display-name-change': 'displayNameChange',
32
+    'incoming-message': 'incomingMessage',
33
+    'outgoing-message': 'outgoingMessage',
34
+    'participant-joined': 'participantJoined',
35
+    'participant-left': 'participantLeft',
36
+    'video-ready-to-close': 'readyToClose',
37
+    'video-conference-joined': 'videoConferenceJoined',
38
+    'video-conference-left': 'videoConferenceLeft'
36 39
 };
37 40
 
38 41
 /**
@@ -180,9 +183,13 @@ class JitsiMeetExternalAPI extends EventEmitter {
180 183
         });
181 184
         this._createIFrame(Math.max(height, MIN_HEIGHT),
182 185
             Math.max(width, MIN_WIDTH));
183
-        this.postis = postisInit({
184
-            scope: `jitsi_meet_external_api_${id}`,
185
-            window: this.frame.contentWindow
186
+        this._transport = new Transport({
187
+            transport: new PostMessageTransportBackend({
188
+                postisOptions: {
189
+                    scope: `jitsi_meet_external_api_${id}`,
190
+                    window: this.frame.contentWindow
191
+                }
192
+            })
186 193
         });
187 194
         this.numberOfParticipants = 1;
188 195
         this._setupListeners();
@@ -225,17 +232,28 @@ class JitsiMeetExternalAPI extends EventEmitter {
225 232
      * @private
226 233
      */
227 234
     _setupListeners() {
228
-        this.postis.listen('participant-joined',
229
-            changeParticipantNumber.bind(null, this, 1));
230
-        this.postis.listen('participant-left',
231
-            changeParticipantNumber.bind(null, this, -1));
232 235
 
233
-        for (const eventName in events) { // eslint-disable-line guard-for-in
234
-            const postisMethod = events[eventName];
236
+        this._transport.on('event', event => {
237
+            const { name, data } = event;
235 238
 
236
-            this.postis.listen(postisMethod,
237
-                (...args) => this.emit(eventName, ...args));
238
-        }
239
+            if (name === 'participant-joined') {
240
+                changeParticipantNumber(this, 1);
241
+            }
242
+
243
+            if (name === 'participant-left') {
244
+                changeParticipantNumber(this, -1);
245
+            }
246
+
247
+            const eventName = events[name];
248
+
249
+            if (eventName) {
250
+                this.emit(eventName, data);
251
+
252
+                return true;
253
+            }
254
+
255
+            return false;
256
+        });
239 257
     }
240 258
 
241 259
     /**
@@ -319,10 +337,7 @@ class JitsiMeetExternalAPI extends EventEmitter {
319 337
      * @returns {void}
320 338
      */
321 339
     dispose() {
322
-        if (this.postis) {
323
-            this.postis.destroy();
324
-            this.postis = null;
325
-        }
340
+        this._transport.dispose();
326 341
         this.removeAllListeners();
327 342
         if (this.iframeHolder) {
328 343
             this.iframeHolder.parentNode.removeChild(this.iframeHolder);
@@ -348,16 +363,9 @@ class JitsiMeetExternalAPI extends EventEmitter {
348 363
 
349 364
             return;
350 365
         }
351
-
352
-        if (!this.postis) {
353
-            logger.error('Cannot execute command using disposed instance.');
354
-
355
-            return;
356
-        }
357
-
358
-        this.postis.send({
359
-            method: commands[name],
360
-            params: args
366
+        this._transport.sendEvent({
367
+            name: commands[name],
368
+            data: args
361 369
         });
362 370
     }
363 371
 

+ 17
- 9
modules/transport/PostMessageTransportBackend.js View File

@@ -50,14 +50,19 @@ export default class PostMessageTransportBackend {
50 50
      * transport.
51 51
      */
52 52
     constructor(options = {}) {
53
-        const postisOptions = Object.assign({}, defaultPostisOptions, options);
53
+        const postisOptions = Object.assign({}, defaultPostisOptions,
54
+            options.postisOptions);
54 55
 
55 56
         this.postis = Postis(postisOptions);
56 57
 
57
-        // backward compatability
58
-        legacyIncomingMethods.forEach(method =>
59
-            this.postis.listen(method,
60
-                params => this._onPostisDataReceived(method, params)));
58
+        this._enableLegacyFormat = options.enableLegacyFormat;
59
+
60
+        if (!this._enableLegacyFormat) {
61
+            // backward compatability
62
+            legacyIncomingMethods.forEach(method =>
63
+                this.postis.listen(method,
64
+                    params => this._onPostisDataReceived(method, params)));
65
+        }
61 66
 
62 67
         this.postis.listen(POSTIS_METHOD_NAME, data =>
63 68
             this._dataReceivedCallBack(data));
@@ -123,10 +128,13 @@ export default class PostMessageTransportBackend {
123 128
             params: data
124 129
         });
125 130
 
126
-        // For the legacy use case we don't need any new fields defined in
127
-        // Transport class. That's why we are passing only the original object
128
-        // passed by the consumer of the Transport class which is data.data.
129
-        this._sendLegacyData(data.data);
131
+        if (!this._enableLegacyFormat) {
132
+            // For the legacy use case we don't need any new fields defined in
133
+            // Transport class. That's why we are passing only the original
134
+            // object passed by the consumer of the Transport class which is
135
+            // data.data.
136
+            this._sendLegacyData(data.data);
137
+        }
130 138
     }
131 139
 
132 140
     /**

+ 6
- 3
modules/transport/index.js View File

@@ -9,15 +9,18 @@ import PostMessageTransportBackend from './PostMessageTransportBackend';
9 9
  *
10 10
  * @type {Object}
11 11
  */
12
-const postMessageOptions = {};
12
+const postisOptions = {};
13 13
 
14 14
 if (typeof API_ID === 'number') {
15
-    postMessageOptions.scope
15
+    postisOptions.scope
16 16
         = `jitsi_meet_external_api_${API_ID}`;
17 17
 }
18 18
 
19 19
 export const transport = new Transport({
20
-    transport: new PostMessageTransportBackend(postMessageOptions)
20
+    transport: new PostMessageTransportBackend({
21
+        enableLegacyFormat: true,
22
+        postisOptions
23
+    })
21 24
 });
22 25
 
23 26
 /**

Loading…
Cancel
Save