chore: Improve "fallback to no callback" wrapper.

According to review comment https://github.com/mozilla/webextension-polyfill/pull/59/files#r142001949
This commit is contained in:
Gerben 2018-03-13 00:13:17 +01:00 committed by Luca Greco
parent 917ed413f6
commit 596f47bcc8
1 changed files with 10 additions and 18 deletions

View File

@ -132,22 +132,6 @@ if (typeof browser === "undefined") {
}
return new Promise((resolve, reject) => {
function callAPIWithNoCallback() {
try {
target[name](...args);
// Update the API method metadata, so that the next API calls will not try to
// use the unsupported callback anymore.
metadata.fallbackToNoCallback = false;
metadata.noCallback = true;
resolve();
} catch (error) {
// Catch API parameters validation errors and rejects the promise.
reject(error);
}
}
if (metadata.fallbackToNoCallback) {
// This API method has currently no callback on Chrome, but it return a promise on Firefox,
// and so the polyfill will try to call it with a callback first, and it will fallback
@ -158,10 +142,18 @@ if (typeof browser === "undefined") {
console.warn(`${name} API method doesn't seem to support the callback parameter, ` +
"falling back to call it without a callback: ", cbError);
callAPIWithNoCallback();
target[name](...args);
// Update the API method metadata, so that the next API calls will not try to
// use the unsupported callback anymore.
metadata.fallbackToNoCallback = false;
metadata.noCallback = true;
resolve();
}
} else if (metadata.noCallback) {
callAPIWithNoCallback();
target[name](...args);
resolve();
} else {
target[name](...args, makeCallback({resolve, reject}, metadata));
}