From 0caf72d5bd1bb9b60d38d51b037065a7386fa3d9 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 1 Jan 2021 14:21:47 +0100 Subject: [PATCH] Convert `FirefoxCom` to a class, with `static` methods *Please note:* It's highly recommended to ignore whitespace-only changes when looking at this patch. Besides modernizing this code, by converting it to a standard class, the existing JSDoc comments are updated to actually agree better with the way that this functionality is used now. (The next patch will reduce usage of `FirefoxCom.request` significantly, hence the JSDocs for the optional `callback` is removed to not unnecessarily advertise that functionality.) Finally, the unnecessary/unused `return` statement at the end of `FirefoxCom.request` is also removed. --- web/firefoxcom.js | 112 ++++++++++++++++++++++------------------------ 1 file changed, 54 insertions(+), 58 deletions(-) diff --git a/web/firefoxcom.js b/web/firefoxcom.js index ecb3095d7..6ab1f70cc 100644 --- a/web/firefoxcom.js +++ b/web/firefoxcom.js @@ -25,69 +25,65 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { ); } -const FirefoxCom = (function FirefoxComClosure() { - return { - /** - * Creates an event that the extension is listening for and will - * synchronously respond to. - * NOTE: It is reccomended to use request() instead since one day we may not - * be able to synchronously reply. - * @param {string} action - The action to trigger. - * @param {string} [data] - The data to send. - * @returns {*} The response. - */ - requestSync(action, data) { - const request = document.createTextNode(""); - document.documentElement.appendChild(request); +class FirefoxCom { + /** + * Creates an event that the extension is listening for and will + * synchronously respond to. + * NOTE: It is recommended to use request() instead since one day we may not + * be able to synchronously reply. + * @param {string} action - The action to trigger. + * @param {Object|string} [data] - The data to send. + * @returns {*} The response. + */ + static requestSync(action, data) { + const request = document.createTextNode(""); + document.documentElement.appendChild(request); - const sender = document.createEvent("CustomEvent"); - sender.initCustomEvent("pdf.js.message", true, false, { - action, - data, - sync: true, - }); - request.dispatchEvent(sender); - const response = sender.detail.response; - request.remove(); + const sender = document.createEvent("CustomEvent"); + sender.initCustomEvent("pdf.js.message", true, false, { + action, + data, + sync: true, + }); + request.dispatchEvent(sender); + const response = sender.detail.response; + request.remove(); - return response; - }, + return response; + } - /** - * Creates an event that the extension is listening for and will - * asynchronously respond by calling the callback. - * @param {string} action - The action to trigger. - * @param {string} [data] - The data to send. - * @param {Function} [callback] - Response callback that will be called - * with one data argument. - */ - request(action, data, callback) { - const request = document.createTextNode(""); - if (callback) { - request.addEventListener( - "pdf.js.response", - event => { - const response = event.detail.response; - event.target.remove(); + /** + * Creates an event that the extension is listening for and will, optionally, + * asynchronously respond to. + * @param {string} action - The action to trigger. + * @param {Object|string} [data] - The data to send. + */ + static request(action, data, callback = null) { + const request = document.createTextNode(""); + if (callback) { + request.addEventListener( + "pdf.js.response", + event => { + const response = event.detail.response; + event.target.remove(); - callback(response); - }, - { once: true } - ); - } - document.documentElement.appendChild(request); + callback(response); + }, + { once: true } + ); + } + document.documentElement.appendChild(request); - const sender = document.createEvent("CustomEvent"); - sender.initCustomEvent("pdf.js.message", true, false, { - action, - data, - sync: false, - responseExpected: !!callback, - }); - return request.dispatchEvent(sender); - }, - }; -})(); + const sender = document.createEvent("CustomEvent"); + sender.initCustomEvent("pdf.js.message", true, false, { + action, + data, + sync: false, + responseExpected: !!callback, + }); + request.dispatchEvent(sender); + } +} class DownloadManager { downloadUrl(url, filename) {