From 9c77ab7f4d530650d19c4531659cec0c35636b28 Mon Sep 17 00:00:00 2001 From: Leonard Kim Date: Mon, 9 Dec 2019 14:32:05 -0800 Subject: [PATCH] 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. --- .../transport/PostMessageTransportBackend.js | 77 ----- modules/transport/Transport.js | 267 ------------------ modules/transport/constants.js | 20 -- modules/transport/index.js | 3 +- package-lock.json | 7 +- package.json | 3 +- 6 files changed, 6 insertions(+), 371 deletions(-) delete mode 100644 modules/transport/PostMessageTransportBackend.js delete mode 100644 modules/transport/Transport.js delete mode 100644 modules/transport/constants.js diff --git a/modules/transport/PostMessageTransportBackend.js b/modules/transport/PostMessageTransportBackend.js deleted file mode 100644 index f1435952b..000000000 --- a/modules/transport/PostMessageTransportBackend.js +++ /dev/null @@ -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; - } -} diff --git a/modules/transport/Transport.js b/modules/transport/Transport.js deleted file mode 100644 index 9f6cebbca..000000000 --- a/modules/transport/Transport.js +++ /dev/null @@ -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} - */ - 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} - */ - 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} - */ - 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)); - } -} diff --git a/modules/transport/constants.js b/modules/transport/constants.js deleted file mode 100644 index 54fb87cf0..000000000 --- a/modules/transport/constants.js +++ /dev/null @@ -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'; diff --git a/modules/transport/index.js b/modules/transport/index.js index fc67c1f40..b6032876d 100644 --- a/modules/transport/index.js +++ b/modules/transport/index.js @@ -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, diff --git a/package-lock.json b/package-lock.json index e9b7b43ed..b816719ff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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": { diff --git a/package.json b/package.json index 36eacfd20..e24319ca9 100644 --- a/package.json +++ b/package.json @@ -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",