ref(external_api): To use transport module

This commit is contained in:
hristoterezov 2017-04-17 22:17:23 -05:00
parent 54388b6a0a
commit 3e055c1201
3 changed files with 66 additions and 47 deletions

View File

@ -1,5 +1,8 @@
import EventEmitter from 'events'; import EventEmitter from 'events';
import postisInit from 'postis';
import PostMessageTransportBackend
from '../../transport/PostMessageTransportBackend';
import Transport from '../../transport/Transport';
const logger = require('jitsi-meet-logger').getLogger(__filename); const logger = require('jitsi-meet-logger').getLogger(__filename);
@ -25,14 +28,14 @@ const commands = {
* events expected by jitsi-meet * events expected by jitsi-meet
*/ */
const events = { const events = {
displayNameChange: 'display-name-change', 'display-name-change': 'displayNameChange',
incomingMessage: 'incoming-message', 'incoming-message': 'incomingMessage',
outgoingMessage: 'outgoing-message', 'outgoing-message': 'outgoingMessage',
participantJoined: 'participant-joined', 'participant-joined': 'participantJoined',
participantLeft: 'participant-left', 'participant-left': 'participantLeft',
readyToClose: 'video-ready-to-close', 'video-ready-to-close': 'readyToClose',
videoConferenceJoined: 'video-conference-joined', 'video-conference-joined': 'videoConferenceJoined',
videoConferenceLeft: 'video-conference-left' 'video-conference-left': 'videoConferenceLeft'
}; };
/** /**
@ -180,9 +183,13 @@ class JitsiMeetExternalAPI extends EventEmitter {
}); });
this._createIFrame(Math.max(height, MIN_HEIGHT), this._createIFrame(Math.max(height, MIN_HEIGHT),
Math.max(width, MIN_WIDTH)); Math.max(width, MIN_WIDTH));
this.postis = postisInit({ this._transport = new Transport({
transport: new PostMessageTransportBackend({
postisOptions: {
scope: `jitsi_meet_external_api_${id}`, scope: `jitsi_meet_external_api_${id}`,
window: this.frame.contentWindow window: this.frame.contentWindow
}
})
}); });
this.numberOfParticipants = 1; this.numberOfParticipants = 1;
this._setupListeners(); this._setupListeners();
@ -225,17 +232,28 @@ class JitsiMeetExternalAPI extends EventEmitter {
* @private * @private
*/ */
_setupListeners() { _setupListeners() {
this.postis.listen('participant-joined',
changeParticipantNumber.bind(null, this, 1));
this.postis.listen('participant-left',
changeParticipantNumber.bind(null, this, -1));
for (const eventName in events) { // eslint-disable-line guard-for-in this._transport.on('event', event => {
const postisMethod = events[eventName]; const { name, data } = event;
this.postis.listen(postisMethod, if (name === 'participant-joined') {
(...args) => this.emit(eventName, ...args)); changeParticipantNumber(this, 1);
} }
if (name === 'participant-left') {
changeParticipantNumber(this, -1);
}
const eventName = events[name];
if (eventName) {
this.emit(eventName, data);
return true;
}
return false;
});
} }
/** /**
@ -319,10 +337,7 @@ class JitsiMeetExternalAPI extends EventEmitter {
* @returns {void} * @returns {void}
*/ */
dispose() { dispose() {
if (this.postis) { this._transport.dispose();
this.postis.destroy();
this.postis = null;
}
this.removeAllListeners(); this.removeAllListeners();
if (this.iframeHolder) { if (this.iframeHolder) {
this.iframeHolder.parentNode.removeChild(this.iframeHolder); this.iframeHolder.parentNode.removeChild(this.iframeHolder);
@ -348,16 +363,9 @@ class JitsiMeetExternalAPI extends EventEmitter {
return; return;
} }
this._transport.sendEvent({
if (!this.postis) { name: commands[name],
logger.error('Cannot execute command using disposed instance.'); data: args
return;
}
this.postis.send({
method: commands[name],
params: args
}); });
} }

View File

@ -50,14 +50,19 @@ export default class PostMessageTransportBackend {
* transport. * transport.
*/ */
constructor(options = {}) { constructor(options = {}) {
const postisOptions = Object.assign({}, defaultPostisOptions, options); const postisOptions = Object.assign({}, defaultPostisOptions,
options.postisOptions);
this.postis = Postis(postisOptions); this.postis = Postis(postisOptions);
this._enableLegacyFormat = options.enableLegacyFormat;
if (!this._enableLegacyFormat) {
// backward compatability // backward compatability
legacyIncomingMethods.forEach(method => legacyIncomingMethods.forEach(method =>
this.postis.listen(method, this.postis.listen(method,
params => this._onPostisDataReceived(method, params))); params => this._onPostisDataReceived(method, params)));
}
this.postis.listen(POSTIS_METHOD_NAME, data => this.postis.listen(POSTIS_METHOD_NAME, data =>
this._dataReceivedCallBack(data)); this._dataReceivedCallBack(data));
@ -123,11 +128,14 @@ export default class PostMessageTransportBackend {
params: data params: data
}); });
if (!this._enableLegacyFormat) {
// For the legacy use case we don't need any new fields defined in // For the legacy use case we don't need any new fields defined in
// Transport class. That's why we are passing only the original object // Transport class. That's why we are passing only the original
// passed by the consumer of the Transport class which is data.data. // object passed by the consumer of the Transport class which is
// data.data.
this._sendLegacyData(data.data); this._sendLegacyData(data.data);
} }
}
/** /**
* Sets the callback for receiving data. * Sets the callback for receiving data.

View File

@ -9,15 +9,18 @@ import PostMessageTransportBackend from './PostMessageTransportBackend';
* *
* @type {Object} * @type {Object}
*/ */
const postMessageOptions = {}; const postisOptions = {};
if (typeof API_ID === 'number') { if (typeof API_ID === 'number') {
postMessageOptions.scope postisOptions.scope
= `jitsi_meet_external_api_${API_ID}`; = `jitsi_meet_external_api_${API_ID}`;
} }
export const transport = new Transport({ export const transport = new Transport({
transport: new PostMessageTransportBackend(postMessageOptions) transport: new PostMessageTransportBackend({
enableLegacyFormat: true,
postisOptions
})
}); });
/** /**