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,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) {