Skip to content

Commit

Permalink
fix: avoid infinite hangs on async messages (revise)
Browse files Browse the repository at this point in the history
cherry-pick from commit 999a690
  • Loading branch information
ACTCD committed Jul 12, 2024
1 parent c4d196d commit 9211ea2
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/ext/background/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,9 @@ async function nativeChecks() {
* Make sure not to return `undefined` or `rejection`, otherwise the reply may never be delivered
* @see {@link https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessage#listener}
* @type {Parameters<typeof browser.runtime.onMessage.addListener>[0]}
* @returns {Promise<{status: "pending"|"fulfilled"|"rejected", result: any}>}
* @returns {Promise<{status: "pending"|"fulfilled"|"rejected", result?: any}>}
*/
async function handleMessage(message, sender) {
/** @type {Promise} */
let promise;
switch (message.name) {
case "REQ_USERSCRIPTS": {
// get the page url from the content script that sent request
Expand All @@ -331,27 +329,42 @@ async function handleMessage(message, sender) {
} catch (error) {
console.error(error);
// @ts-ignore -- ignore for now and will reconstruct this in the future.
return { error };
return { error: String(error) };
}
}
case "API_CLOSE_TAB": {
promise = browser.tabs.remove(message.tabId || sender.tab.id);
break;
try {
await browser.tabs.remove(message.tabId || sender.tab.id);
return { status: "fulfilled" };
} catch (error) {
console.error(message, sender, error);
return { status: "rejected", result: String(error) };
}
}
case "API_OPEN_TAB": {
promise = browser.tabs.create({
active: message.active,
index: sender.tab.index + 1,
url: message.url,
});
break;
try {
const tab = await browser.tabs.create({
active: message.active,
index: sender.tab.index + 1,
url: message.url,
});
return { status: "fulfilled", result: tab };
} catch (error) {
console.error(message, sender, error);
return { status: "rejected", result: String(error) };
}
}
case "API_ADD_STYLE": {
promise = browser.tabs.insertCSS(sender.tab.id, {
code: message.css,
cssOrigin: "user",
});
break;
try {
await browser.tabs.insertCSS(sender.tab.id, {
code: message.css,
cssOrigin: "user",
});
return { status: "fulfilled" };
} catch (error) {
console.error(message, sender, error);
return { status: "rejected", result: String(error) };
}
}
case "API_GET_TAB": {
if (typeof sender.tab === "undefined") {
Expand All @@ -365,17 +378,17 @@ async function handleMessage(message, sender) {
return { status: "fulfilled", result: tabObj };
} catch (error) {
console.error("failed to parse tab data for getTab", error);
return { status: "rejected", result: error };
return { status: "rejected", result: String(error) };
}
}
case "API_SAVE_TAB": {
if (sender.tab != null && sender.tab.id) {
const key = `tab-${sender.tab.id}`;
sessionStorage.setItem(key, JSON.stringify(message.tabObj));
return { status: "fulfilled", result: undefined };
return { status: "fulfilled" };
} else {
const error = "unable to save tab, empty tab id";
return { status: "rejected", result: error };
return { status: "rejected", result: String(error) };
}
}
case "API_SET_CLIPBOARD": {
Expand Down Expand Up @@ -481,7 +494,7 @@ async function handleMessage(message, sender) {
port.postMessage({ name: "onloadend", event });
};
}
break;
return { status: "fulfilled" };
}
case "REFRESH_SESSION_RULES": {
setSessionRules();
Expand All @@ -492,13 +505,6 @@ async function handleMessage(message, sender) {
break;
}
}
try {
const result = await promise;
return { status: "fulfilled", result };
} catch (error) {
console.error(message, sender, error);
return { status: "rejected", result: error };
}
}
browser.runtime.onInstalled.addListener(async () => {
nativeChecks();
Expand Down

0 comments on commit 9211ea2

Please sign in to comment.