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.
This commit is contained in:
Jonas Jenwald 2021-01-01 14:21:47 +01:00
parent 04321546ce
commit 0caf72d5bd

@ -25,18 +25,17 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
); );
} }
const FirefoxCom = (function FirefoxComClosure() { class FirefoxCom {
return {
/** /**
* Creates an event that the extension is listening for and will * Creates an event that the extension is listening for and will
* synchronously respond to. * synchronously respond to.
* NOTE: It is reccomended to use request() instead since one day we may not * NOTE: It is recommended to use request() instead since one day we may not
* be able to synchronously reply. * be able to synchronously reply.
* @param {string} action - The action to trigger. * @param {string} action - The action to trigger.
* @param {string} [data] - The data to send. * @param {Object|string} [data] - The data to send.
* @returns {*} The response. * @returns {*} The response.
*/ */
requestSync(action, data) { static requestSync(action, data) {
const request = document.createTextNode(""); const request = document.createTextNode("");
document.documentElement.appendChild(request); document.documentElement.appendChild(request);
@ -51,17 +50,15 @@ const FirefoxCom = (function FirefoxComClosure() {
request.remove(); request.remove();
return response; return response;
}, }
/** /**
* Creates an event that the extension is listening for and will * Creates an event that the extension is listening for and will, optionally,
* asynchronously respond by calling the callback. * asynchronously respond to.
* @param {string} action - The action to trigger. * @param {string} action - The action to trigger.
* @param {string} [data] - The data to send. * @param {Object|string} [data] - The data to send.
* @param {Function} [callback] - Response callback that will be called
* with one data argument.
*/ */
request(action, data, callback) { static request(action, data, callback = null) {
const request = document.createTextNode(""); const request = document.createTextNode("");
if (callback) { if (callback) {
request.addEventListener( request.addEventListener(
@ -84,10 +81,9 @@ const FirefoxCom = (function FirefoxComClosure() {
sync: false, sync: false,
responseExpected: !!callback, responseExpected: !!callback,
}); });
return request.dispatchEvent(sender); request.dispatchEvent(sender);
}, }
}; }
})();
class DownloadManager { class DownloadManager {
downloadUrl(url, filename) { downloadUrl(url, filename) {