Skip to content

Commit

Permalink
Do not use async callback for onMessage
Browse files Browse the repository at this point in the history
Fix error: "The message port closed before a response was received."
  • Loading branch information
Folyd committed Jun 5, 2022
1 parent 5ba0944 commit 3aa42c0
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions extension/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3aa42c0

Please sign in to comment.