ref(api): use Transport modules from js-utils

PostMessageTransportBackend and Transport, along
with the constants file they both use, were
moved int js-utils for jitsi-meet-spot.
This commit is contained in:
Leonard Kim 2019-12-09 14:32:05 -08:00 committed by virtuacoplenny
parent 55983ff62a
commit 9c77ab7f4d
6 changed files with 6 additions and 371 deletions

View File

@ -1,77 +0,0 @@
import Postis from 'postis';
/**
* The default options for postis.
*
* @type {Object}
*/
const DEFAULT_POSTIS_OPTIONS = {
window: window.opener || window.parent
};
/**
* The postis method used for all messages.
*
* @type {string}
*/
const POSTIS_METHOD_NAME = 'message';
/**
* Implements message transport using the postMessage API.
*/
export default class PostMessageTransportBackend {
/**
* Creates new PostMessageTransportBackend instance.
*
* @param {Object} options - Optional parameters for configuration of the
* transport.
*/
constructor({ postisOptions } = {}) {
// eslint-disable-next-line new-cap
this.postis = Postis({
...DEFAULT_POSTIS_OPTIONS,
...postisOptions
});
this._receiveCallback = () => {
// Do nothing until a callback is set by the consumer of
// PostMessageTransportBackend via setReceiveCallback.
};
this.postis.listen(
POSTIS_METHOD_NAME,
message => this._receiveCallback(message));
}
/**
* Disposes the allocated resources.
*
* @returns {void}
*/
dispose() {
this.postis.destroy();
}
/**
* Sends the passed message.
*
* @param {Object} message - The message to be sent.
* @returns {void}
*/
send(message) {
this.postis.send({
method: POSTIS_METHOD_NAME,
params: message
});
}
/**
* Sets the callback for receiving data.
*
* @param {Function} callback - The new callback.
* @returns {void}
*/
setReceiveCallback(callback) {
this._receiveCallback = callback;
}
}

View File

@ -1,267 +0,0 @@
import {
MESSAGE_TYPE_EVENT,
MESSAGE_TYPE_REQUEST,
MESSAGE_TYPE_RESPONSE
} from './constants';
/**
* Stores the currnet transport backend that have to be used. Also implements
* request/response mechanism.
*/
export default class Transport {
/**
* Creates new instance.
*
* @param {Object} options - Optional parameters for configuration of the
* transport backend.
*/
constructor({ backend } = {}) {
/**
* Maps an event name and listener that have been added to the Transport
* instance.
*
* @type {Map<string, Function>}
*/
this._listeners = new Map();
/**
* The request ID counter used for the id property of the request. This
* property is used to match the responses with the request.
*
* @type {number}
*/
this._requestID = 0;
/**
* Maps an IDs of the requests and handlers that will process the
* responses of those requests.
*
* @type {Map<number, Function>}
*/
this._responseHandlers = new Map();
/**
* A set with the events and requests that were received but not
* processed by any listener. They are later passed on every new
* listener until they are processed.
*
* @type {Set<Object>}
*/
this._unprocessedMessages = new Set();
/**
* Alias.
*/
this.addListener = this.on;
if (backend) {
this.setBackend(backend);
}
}
/**
* Disposes the current transport backend.
*
* @returns {void}
*/
_disposeBackend() {
if (this._backend) {
this._backend.dispose();
this._backend = null;
}
}
/**
* Handles incoming messages from the transport backend.
*
* @param {Object} message - The message.
* @returns {void}
*/
_onMessageReceived(message) {
if (message.type === MESSAGE_TYPE_RESPONSE) {
const handler = this._responseHandlers.get(message.id);
if (handler) {
handler(message);
this._responseHandlers.delete(message.id);
}
} else if (message.type === MESSAGE_TYPE_REQUEST) {
this.emit('request', message.data, (result, error) => {
this._backend.send({
type: MESSAGE_TYPE_RESPONSE,
error,
id: message.id,
result
});
});
} else {
this.emit('event', message.data);
}
}
/**
* Disposes the allocated resources.
*
* @returns {void}
*/
dispose() {
this._responseHandlers.clear();
this._unprocessedMessages.clear();
this.removeAllListeners();
this._disposeBackend();
}
/**
* Calls each of the listeners registered for the event named eventName, in
* the order they were registered, passing the supplied arguments to each.
*
* @param {string} eventName - The name of the event.
* @returns {boolean} True if the event has been processed by any listener,
* false otherwise.
*/
emit(eventName, ...args) {
const listenersForEvent = this._listeners.get(eventName);
let isProcessed = false;
if (listenersForEvent && listenersForEvent.size) {
listenersForEvent.forEach(listener => {
isProcessed = listener(...args) || isProcessed;
});
}
if (!isProcessed) {
this._unprocessedMessages.add(args);
}
return isProcessed;
}
/**
* Adds the listener function to the listeners collection for the event
* named eventName.
*
* @param {string} eventName - The name of the event.
* @param {Function} listener - The listener that will be added.
* @returns {Transport} References to the instance of Transport class, so
* that calls can be chained.
*/
on(eventName, listener) {
let listenersForEvent = this._listeners.get(eventName);
if (!listenersForEvent) {
listenersForEvent = new Set();
this._listeners.set(eventName, listenersForEvent);
}
listenersForEvent.add(listener);
this._unprocessedMessages.forEach(args => {
if (listener(...args)) {
this._unprocessedMessages.delete(args);
}
});
return this;
}
/**
* Removes all listeners, or those of the specified eventName.
*
* @param {string} [eventName] - The name of the event. If this parameter is
* not specified all listeners will be removed.
* @returns {Transport} References to the instance of Transport class, so
* that calls can be chained.
*/
removeAllListeners(eventName) {
if (eventName) {
this._listeners.delete(eventName);
} else {
this._listeners.clear();
}
return this;
}
/**
* Removes the listener function from the listeners collection for the event
* named eventName.
*
* @param {string} eventName - The name of the event.
* @param {Function} listener - The listener that will be removed.
* @returns {Transport} References to the instance of Transport class, so
* that calls can be chained.
*/
removeListener(eventName, listener) {
const listenersForEvent = this._listeners.get(eventName);
if (listenersForEvent) {
listenersForEvent.delete(listener);
}
return this;
}
/**
* Sends the passed event.
*
* @param {Object} event - The event to be sent.
* @returns {void}
*/
sendEvent(event = {}) {
if (this._backend) {
this._backend.send({
type: MESSAGE_TYPE_EVENT,
data: event
});
}
}
/**
* Sending request.
*
* @param {Object} request - The request to be sent.
* @returns {Promise}
*/
sendRequest(request) {
if (!this._backend) {
return Promise.reject(new Error('No transport backend defined!'));
}
this._requestID++;
const id = this._requestID;
return new Promise((resolve, reject) => {
this._responseHandlers.set(id, ({ error, result }) => {
if (typeof result !== 'undefined') {
resolve(result);
// eslint-disable-next-line no-negated-condition
} else if (typeof error !== 'undefined') {
reject(error);
} else { // no response
reject(new Error('Unexpected response format!'));
}
});
this._backend.send({
type: MESSAGE_TYPE_REQUEST,
data: request,
id
});
});
}
/**
* Changes the current backend transport.
*
* @param {Object} backend - The new transport backend that will be used.
* @returns {void}
*/
setBackend(backend) {
this._disposeBackend();
this._backend = backend;
this._backend.setReceiveCallback(this._onMessageReceived.bind(this));
}
}

View File

@ -1,20 +0,0 @@
/**
* The message type for events.
*
* @type {string}
*/
export const MESSAGE_TYPE_EVENT = 'event';
/**
* The message type for requests.
*
* @type {string}
*/
export const MESSAGE_TYPE_REQUEST = 'request';
/**
* The message type for responses.
*
* @type {string}
*/
export const MESSAGE_TYPE_RESPONSE = 'response';

View File

@ -3,8 +3,7 @@
import { API_ID } from '../API/constants';
import { getJitsiMeetGlobalNS } from '../../react/features/base/util';
import PostMessageTransportBackend from './PostMessageTransportBackend';
import Transport from './Transport';
import { PostMessageTransportBackend, Transport } from 'js-utils/transport';
export {
PostMessageTransportBackend,

7
package-lock.json generated
View File

@ -10670,11 +10670,12 @@
"integrity": "sha1-mGbfOVECEw449/mWvOtlRDIJwls="
},
"js-utils": {
"version": "github:jitsi/js-utils#192b1c996e8c05530eb1f19e82a31069c3021e31",
"from": "github:jitsi/js-utils#192b1c996e8c05530eb1f19e82a31069c3021e31",
"version": "github:jitsi/js-utils#964294793a7dbb85b2a38c87abe5ae46c4acf0af",
"from": "github:jitsi/js-utils#964294793a7dbb85b2a38c87abe5ae46c4acf0af",
"requires": {
"bowser": "1.9.1",
"js-md5": "0.7.3"
"js-md5": "0.7.3",
"postis": "2.2.0"
},
"dependencies": {
"js-md5": {

View File

@ -53,7 +53,7 @@
"jquery-contextmenu": "2.4.5",
"jquery-i18next": "1.2.1",
"js-md5": "0.6.1",
"js-utils": "github:jitsi/js-utils#192b1c996e8c05530eb1f19e82a31069c3021e31",
"js-utils": "github:jitsi/js-utils#964294793a7dbb85b2a38c87abe5ae46c4acf0af",
"jsrsasign": "8.0.12",
"jwt-decode": "2.2.0",
"lib-jitsi-meet": "github:jitsi/lib-jitsi-meet#4e7034ee6a0d0e68487c583c981f4e07ab73926c",
@ -61,7 +61,6 @@
"lodash": "4.17.13",
"moment": "2.19.4",
"moment-duration-format": "2.2.2",
"postis": "2.2.0",
"react": "16.9",
"react-dom": "16.9",
"react-emoji-render": "1.0.0",