From 3aa42c012ec2199048b33d6cad29c8d1b1f6072a Mon Sep 17 00:00:00 2001 From: Folyd Date: Mon, 6 Jun 2022 00:14:02 +0800 Subject: [PATCH] Do not use async callback for onMessage Fix error: "The message port closed before a response was received." --- extension/main.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/extension/main.js b/extension/main.js index 38accad6..c7d9979a 100644 --- a/extension/main.js +++ b/extension/main.js @@ -433,7 +433,9 @@ function getPlatformOs() { await storage.setItem('auto-update-version', `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`); } - chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => { + // DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE: + // https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049 + chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { switch (message.action) { // Stable:* action is exclusive to stable docs event case "stable:add": { @@ -478,24 +480,29 @@ function getPlatformOs() { } // Crate:* action is exclusive to crate event case "crate:check": { - let crates = await CrateDocManager.getCrates(); - sendResponse(crates[message.crateName]); + CrateDocManager.getCrates().then((crates) => { + sendResponse(crates[message.crateName]); + }) break; } case "crate:add": { if (message.searchIndex) { - await CrateDocManager.addCrate(message.crateName, message.crateVersion, message.searchIndex); - await crateDocSearcher.initAllCrateSearcher(); - sendResponse(true); + CrateDocManager.addCrate(message.crateName, message.crateVersion, message.searchIndex).then(() => { + crateDocSearcher.initAllCrateSearcher(); + }).then(() => { + sendResponse(true); + }); } else { sendResponse(false); } break; } case "crate:remove": { - await CrateDocManager.removeCrate(message.crateName); - await crateDocSearcher.initAllCrateSearcher(); - sendResponse(true); + CrateDocManager.removeCrate(message.crateName).then(() => { + crateDocSearcher.initAllCrateSearcher(); + }).then(() => { + sendResponse(true); + }); break; } // Index-update:* action is exclusive to index update event