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

View File

@ -25,18 +25,17 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) {
);
}
const FirefoxCom = (function FirefoxComClosure() {
return {
class FirefoxCom {
/**
* 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
* 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 {string} [data] - The data to send.
* @param {Object|string} [data] - The data to send.
* @returns {*} The response.
*/
requestSync(action, data) {
static requestSync(action, data) {
const request = document.createTextNode("");
document.documentElement.appendChild(request);
@ -51,17 +50,15 @@ const FirefoxCom = (function FirefoxComClosure() {
request.remove();
return response;
},
}
/**
* Creates an event that the extension is listening for and will
* asynchronously respond by calling the callback.
* Creates an event that the extension is listening for and will, optionally,
* asynchronously respond to.
* @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.
* @param {Object|string} [data] - The data to send.
*/
request(action, data, callback) {
static request(action, data, callback = null) {
const request = document.createTextNode("");
if (callback) {
request.addEventListener(
@ -84,10 +81,9 @@ const FirefoxCom = (function FirefoxComClosure() {
sync: false,
responseExpected: !!callback,
});
return request.dispatchEvent(sender);
},
};
})();
request.dispatchEvent(sender);
}
}
class DownloadManager {
downloadUrl(url, filename) {