retry API on browser startup automatically

This commit is contained in:
tophf 2022-07-25 19:23:38 +03:00
parent 00b732177f
commit 079e7e50f1
2 changed files with 20 additions and 9 deletions

View File

@ -60,7 +60,7 @@
mqDark.onchange(mqDark); mqDark.onchange(mqDark);
// Declare all vars before init() or it'll throw due to "temporal dead zone" of const/let // Declare all vars before init() or it'll throw due to "temporal dead zone" of const/let
const ready = init(); init();
// the popup needs a check as it's not a tab but can be opened in a tab manually for whatever reason // the popup needs a check as it's not a tab but can be opened in a tab manually for whatever reason
if (!isTab) { if (!isTab) {
@ -190,13 +190,6 @@
}); });
break; break;
case 'backgroundReady':
ready.catch(err =>
msg.isIgnorableError(err)
? init()
: console.error(err));
break;
case 'updateCount': case 'updateCount':
updateCount(); updateCount();
break; break;

View File

@ -23,6 +23,8 @@
tab: new Set(), tab: new Set(),
extension: new Set(), extension: new Set(),
}; };
let bgReadySignal;
let bgReadying = new Promise(fn => (bgReadySignal = fn));
// TODO: maybe move into polyfill.js and hook addListener to wrap/unwrap automatically // TODO: maybe move into polyfill.js and hook addListener to wrap/unwrap automatically
chrome.runtime.onMessage.addListener(onRuntimeMessage); chrome.runtime.onMessage.addListener(onRuntimeMessage);
@ -119,6 +121,9 @@
} }
function onRuntimeMessage({data, target}, sender, sendResponse) { function onRuntimeMessage({data, target}, sender, sendResponse) {
if (bgReadying && data && data.method === 'backgroundReady') {
bgReadySignal();
}
const res = msg._execute(TARGETS[target] || TARGETS.all, data, sender); const res = msg._execute(TARGETS[target] || TARGETS.all, data, sender);
if (res instanceof Promise) { if (res instanceof Promise) {
res.then(wrapData, wrapError).then(sendResponse); res.then(wrapData, wrapError).then(sendResponse);
@ -148,6 +153,19 @@
: data; : data;
} }
async function sendRetry(m) {
try {
return await msg.send(m);
} catch (e) {
if (!bgReadying || !msg.isIgnorableError(e)) {
return Promise.reject(e);
}
await bgReadying;
bgReadying = bgReadySignal = null;
return msg.send(m);
}
}
const apiHandler = !msg.isBg && { const apiHandler = !msg.isBg && {
get({path}, name) { get({path}, name) {
const fn = () => {}; const fn = () => {};
@ -161,7 +179,7 @@
let res; let res;
// content scripts, probably private tabs, and our extension tab during Chrome startup // content scripts, probably private tabs, and our extension tab during Chrome startup
if (!bg || !bg.msg || !bg.msg.ready && await bg.bgReady.all && false) { if (!bg || !bg.msg || !bg.msg.ready && await bg.bgReady.all && false) {
res = msg.send(message); res = bgReadying ? sendRetry(message) : msg.send(message);
} else { } else {
res = deepCopy(await bg.msg._execute(TARGETS.extension, message, { res = deepCopy(await bg.msg._execute(TARGETS.extension, message, {
// Using a fake id for our Options frame as we want to fetch styles early // Using a fake id for our Options frame as we want to fetch styles early