Add a new FirefoxCom.requestAsync
method, to simplify the code in web/firefoxcom.js
There's a fair number of cases where `FirefoxCom.request`-calls are manually wrapped in a Promise to make it asynchronous. We can reduce the amount of boilerplate code in these cases by introducing a new `FirefoxCom.requestAsync` method instead. Furthermore, a couple of `FirefoxCom.request`-calls in the `DownloadManager` are also changed to be asynchronous rather than using callback-functions. With this patch, we're thus able to replace a lot of *direct* usages of `FirefoxCom.request` with the new `FirefoxCom.requestAsync` method instead.
This commit is contained in:
parent
0caf72d5bd
commit
a49b3e04d5
@ -29,8 +29,8 @@ class FirefoxCom {
|
|||||||
/**
|
/**
|
||||||
* 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 recommended to use request() instead since one day we may not
|
* NOTE: It is recommended to use requestAsync() instead since one day we may
|
||||||
* be able to synchronously reply.
|
* not be able to synchronously reply.
|
||||||
* @param {string} action - The action to trigger.
|
* @param {string} action - The action to trigger.
|
||||||
* @param {Object|string} [data] - The data to send.
|
* @param {Object|string} [data] - The data to send.
|
||||||
* @returns {*} The response.
|
* @returns {*} The response.
|
||||||
@ -52,6 +52,19 @@ class FirefoxCom {
|
|||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an event that the extension is listening for and will
|
||||||
|
* asynchronously respond to.
|
||||||
|
* @param {string} action - The action to trigger.
|
||||||
|
* @param {Object|string} [data] - The data to send.
|
||||||
|
* @returns {Promise<any>} A promise that is resolved with the response data.
|
||||||
|
*/
|
||||||
|
static requestAsync(action, data) {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
this.request(action, data, resolve);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an event that the extension is listening for and will, optionally,
|
* Creates an event that the extension is listening for and will, optionally,
|
||||||
* asynchronously respond to.
|
* asynchronously respond to.
|
||||||
@ -97,58 +110,42 @@ class DownloadManager {
|
|||||||
const blobUrl = URL.createObjectURL(
|
const blobUrl = URL.createObjectURL(
|
||||||
new Blob([data], { type: contentType })
|
new Blob([data], { type: contentType })
|
||||||
);
|
);
|
||||||
const onResponse = err => {
|
|
||||||
URL.revokeObjectURL(blobUrl);
|
|
||||||
};
|
|
||||||
|
|
||||||
FirefoxCom.request(
|
FirefoxCom.requestAsync("download", {
|
||||||
"download",
|
blobUrl,
|
||||||
{
|
originalUrl: blobUrl,
|
||||||
blobUrl,
|
filename,
|
||||||
originalUrl: blobUrl,
|
isAttachment: true,
|
||||||
filename,
|
}).then(error => {
|
||||||
isAttachment: true,
|
URL.revokeObjectURL(blobUrl);
|
||||||
},
|
});
|
||||||
onResponse
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
download(blob, url, filename, sourceEventType = "download") {
|
download(blob, url, filename, sourceEventType = "download") {
|
||||||
const blobUrl = URL.createObjectURL(blob);
|
const blobUrl = URL.createObjectURL(blob);
|
||||||
const onResponse = err => {
|
|
||||||
if (err && this.onerror) {
|
FirefoxCom.requestAsync("download", {
|
||||||
this.onerror(err);
|
blobUrl,
|
||||||
|
originalUrl: url,
|
||||||
|
filename,
|
||||||
|
sourceEventType,
|
||||||
|
}).then(error => {
|
||||||
|
if (error && this.onerror) {
|
||||||
|
this.onerror(error);
|
||||||
}
|
}
|
||||||
URL.revokeObjectURL(blobUrl);
|
URL.revokeObjectURL(blobUrl);
|
||||||
};
|
});
|
||||||
|
|
||||||
FirefoxCom.request(
|
|
||||||
"download",
|
|
||||||
{
|
|
||||||
blobUrl,
|
|
||||||
originalUrl: url,
|
|
||||||
filename,
|
|
||||||
sourceEventType,
|
|
||||||
},
|
|
||||||
onResponse
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class FirefoxPreferences extends BasePreferences {
|
class FirefoxPreferences extends BasePreferences {
|
||||||
async _writeToStorage(prefObj) {
|
async _writeToStorage(prefObj) {
|
||||||
return new Promise(function (resolve) {
|
return FirefoxCom.requestAsync("setPreferences", prefObj);
|
||||||
FirefoxCom.request("setPreferences", prefObj, resolve);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async _readFromStorage(prefObj) {
|
async _readFromStorage(prefObj) {
|
||||||
return new Promise(function (resolve) {
|
const prefStr = await FirefoxCom.requestAsync("getPreferences", prefObj);
|
||||||
FirefoxCom.request("getPreferences", prefObj, function (prefStr) {
|
return JSON.parse(prefStr);
|
||||||
const readPrefs = JSON.parse(prefStr);
|
|
||||||
resolve(readPrefs);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,13 +251,10 @@ class FirefoxComDataRangeTransport extends PDFDataRangeTransport {
|
|||||||
|
|
||||||
class FirefoxScripting {
|
class FirefoxScripting {
|
||||||
static async createSandbox(data) {
|
static async createSandbox(data) {
|
||||||
return new Promise(resolve => {
|
const success = await FirefoxCom.requestAsync("createSandbox", data);
|
||||||
FirefoxCom.request("createSandbox", data, resolve);
|
if (!success) {
|
||||||
}).then(success => {
|
throw new Error("Cannot create sandbox.");
|
||||||
if (!success) {
|
}
|
||||||
throw new Error("Cannot create sandbox.");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async dispatchEventInSandbox(event) {
|
static async dispatchEventInSandbox(event) {
|
||||||
@ -343,9 +337,7 @@ class FirefoxExternalServices extends DefaultExternalServices {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static async fallback(data) {
|
static async fallback(data) {
|
||||||
return new Promise(resolve => {
|
return FirefoxCom.requestAsync("fallback", data);
|
||||||
FirefoxCom.request("fallback", data, resolve);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static reportTelemetry(data) {
|
static reportTelemetry(data) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user