Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-1644: Fix Unchecked runtime.lastError in message handler #362

Merged
merged 7 commits into from Apr 2, 2019
fix lint errors
  • Loading branch information
christophertino committed Mar 29, 2019
commit a021659c65aa14fb6f25b816e06428a7a53f3a67
@@ -17,6 +17,18 @@ import { log } from '../../../src/utils/common';
const { onMessage } = chrome.runtime;
const IS_EDGE = (globals.BROWSER_INFO.name === 'edge');

/**
* Default callback handler for sendMessage. Allows us to handle
* 'Unchecked runtime.lastError: The message port closed before a response was received' errors.
* This occurs when the `chrome.runtime.onmessage` handler returns `false` with no `callback()`
* but `chrome.runtime.sendMessage` has been passed a default callback.
*/
const defaultCallback = function () {
if (chrome.runtime.lastError) {
log('defaultCallback error:', chrome.runtime.lastError);
}
};

/**
* Send a message to the handlers in src/background wrapped in a
* promise. This should be used for messages that require a callback.
@@ -83,7 +95,7 @@ export function sendMessageInPromise(name, message, origin = '') {
* @return {Object} response
* @todo runtime.sendMessage does not return any value.
*/
export function sendMessage(name, message, origin = '', callback = _defaultCallback()) {
export function sendMessage(name, message, origin = '', callback = defaultCallback()) {
log('Panel sendMessage: sending to background', name);
// @EDGE chrome.runtime.sendMessage(message) works, but the `callback` of
// chrome.runtime.sendMessage(message, callback) fails to
@@ -106,7 +118,7 @@ export function sendMessage(name, message, origin = '', callback = _defaultCallb
* @return {Object} response
* @todo runtime.sendMessage does not return any value.
*/
export function sendRewardMessage(name, message, callback = _defaultCallback()) {
export function sendRewardMessage(name, message, callback = defaultCallback()) {
log('Panel sendRewardMessage: sending to background', name);
return chrome.runtime.sendMessage({
name,
@@ -136,16 +148,3 @@ export function openSupportPage() {
sendMessage('account.openSupportPage');
window.close();
}


/**
* Default callback handler for sendMessage. Allows us to handle
* 'Unchecked runtime.lastError: The message port closed before a response was received' errors.
* This occurs when the `chrome.runtime.onmessage` handler returns `false` with no `callback()`
* but `chrome.runtime.sendMessage` has been passed a default callback.
*/
function _defaultCallback() {
if (chrome.runtime.lastError) {
log('defaultCallback error:', chrome.runtime.lastError);
}
}
ProTip! Use n and p to navigate between commits in a pull request.