refactor: Make the `wrapAPIs` function take the `chrome` object as a parameter (#139)

This commit is contained in:
ExE Boss 2018-08-27 15:50:40 +02:00 committed by Luca Greco
parent cf135db59e
commit 7b46d25db2
1 changed files with 8 additions and 8 deletions

View File

@ -15,7 +15,7 @@ if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object.
// contents of a function until the first time it's called, and since it will // contents of a function until the first time it's called, and since it will
// never actually need to be called, this allows the polyfill to be included // never actually need to be called, this allows the polyfill to be included
// in Firefox nearly for free. // in Firefox nearly for free.
const wrapAPIs = () => { const wrapAPIs = extensionAPIs => {
// NOTE: apiMetadata is associated to the content of the api-metadata.json file // NOTE: apiMetadata is associated to the content of the api-metadata.json file
// at build time by replacing the following "include" with the content of the // at build time by replacing the following "include" with the content of the
// JSON file. // JSON file.
@ -90,8 +90,8 @@ if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object.
*/ */
const makeCallback = (promise, metadata) => { const makeCallback = (promise, metadata) => {
return (...callbackArgs) => { return (...callbackArgs) => {
if (chrome.runtime.lastError) { if (extensionAPIs.runtime.lastError) {
promise.reject(chrome.runtime.lastError); promise.reject(extensionAPIs.runtime.lastError);
} else if (metadata.singleCallbackArg || callbackArgs.length <= 1) { } else if (metadata.singleCallbackArg || callbackArgs.length <= 1) {
promise.resolve(callbackArgs[0]); promise.resolve(callbackArgs[0]);
} else { } else {
@ -441,14 +441,14 @@ if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object.
}); });
const wrappedSendMessageCallback = ({reject, resolve}, reply) => { const wrappedSendMessageCallback = ({reject, resolve}, reply) => {
if (chrome.runtime.lastError) { if (extensionAPIs.runtime.lastError) {
// Detect when none of the listeners replied to the sendMessage call and resolve // Detect when none of the listeners replied to the sendMessage call and resolve
// the promise to undefined as in Firefox. // the promise to undefined as in Firefox.
// See https://github.com/mozilla/webextension-polyfill/issues/130 // See https://github.com/mozilla/webextension-polyfill/issues/130
if (chrome.runtime.lastError.message === CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE) { if (extensionAPIs.runtime.lastError.message === CHROME_SEND_MESSAGE_CALLBACK_NO_RESPONSE_MESSAGE) {
resolve(); resolve();
} else { } else {
reject(chrome.runtime.lastError); reject(extensionAPIs.runtime.lastError);
} }
} else if (reply && reply.__mozWebExtensionPolyfillReject__) { } else if (reply && reply.__mozWebExtensionPolyfillReject__) {
// Convert back the JSON representation of the error into // Convert back the JSON representation of the error into
@ -504,12 +504,12 @@ if (typeof browser === "undefined" || Object.getPrototypeOf(browser) !== Object.
}, },
}; };
return wrapObject(chrome, staticWrappers, apiMetadata); return wrapObject(extensionAPIs, staticWrappers, apiMetadata);
}; };
// The build process adds a UMD wrapper around this file, which makes the // The build process adds a UMD wrapper around this file, which makes the
// `module` variable available. // `module` variable available.
module.exports = wrapAPIs(); // eslint-disable-line no-undef module.exports = wrapAPIs(chrome); // eslint-disable-line no-undef
} else { } else {
module.exports = browser; // eslint-disable-line no-undef module.exports = browser; // eslint-disable-line no-undef
} }