wait for real prefs on bg startup

+ convert msg.sendXXX to async
This commit is contained in:
tophf 2022-02-19 08:46:24 +03:00
parent cc3c85be58
commit 9d64e9ba54
4 changed files with 14 additions and 17 deletions

View File

@ -222,6 +222,6 @@ Promise.all([
require(['/background/context-menus']), require(['/background/context-menus']),
]).then(() => { ]).then(() => {
bgReady._resolveAll(); bgReady._resolveAll();
msg.isBgReady = true; msg.ready = true;
msg.broadcast({method: 'backgroundReady'}); msg.broadcast({method: 'backgroundReady'});
}); });

View File

@ -5,7 +5,7 @@
* Common stuff that's loaded first so it's immediately available to all background scripts * Common stuff that's loaded first so it's immediately available to all background scripts
*/ */
const bgReady = {}; window.bgReady = {}; /* global bgReady */
bgReady.styles = new Promise(r => (bgReady._resolveStyles = r)); bgReady.styles = new Promise(r => (bgReady._resolveStyles = r));
bgReady.all = new Promise(r => (bgReady._resolveAll = r)); bgReady.all = new Promise(r => (bgReady._resolveAll = r));

View File

@ -75,7 +75,7 @@ const styleMan = (() => {
}; };
uuidIndex.addCustomId(orderWrap, {set: setOrder}); uuidIndex.addCustomId(orderWrap, {set: setOrder});
/** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */ /** @type {Promise|boolean} will be `true` to avoid wasting a microtask tick on each `await` */
let ready = init(); let ready = Promise.all([init(), prefs.ready]);
chrome.runtime.onConnect.addListener(port => { chrome.runtime.onConnect.addListener(port => {
if (port.name === 'livePreview') { if (port.name === 'livePreview') {

View File

@ -16,6 +16,8 @@
]; ];
const ERR_NO_RECEIVER = 'Receiving end does not exist'; const ERR_NO_RECEIVER = 'Receiving end does not exist';
const ERR_PORT_CLOSED = 'The message port closed before'; const ERR_PORT_CLOSED = 'The message port closed before';
const NULL_RESPONSE = {error: {message: ERR_NO_RECEIVER}};
const STACK = 'Callstack before invoking msg.';
const handler = { const handler = {
both: new Set(), both: new Set(),
tab: new Set(), tab: new Set(),
@ -76,14 +78,14 @@
} }
}, },
send(data, target = 'extension') { async send(data, target = 'extension') {
return browser.runtime.sendMessage({data, target}) const err = new Error(`${STACK}send:`); // Saving callstack prior to `await`
.then(unwrapResponseFactory('send')); return unwrap(err, await browser.runtime.sendMessage({data, target}));
}, },
sendTab(tabId, data, options, target = 'tab') { async sendTab(tabId, data, options, target = 'tab') {
return browser.tabs.sendMessage(tabId, {data, target}, options) const err = new Error(`${STACK}sendTab:`); // Saving callstack prior to `await`
.then(unwrapResponseFactory('sendTab')); return unwrap(err, await browser.tabs.sendMessage(tabId, {data, target}, options));
}, },
_execute(types, ...args) { _execute(types, ...args) {
@ -113,7 +115,7 @@
function getExtBg() { function getExtBg() {
const fn = chrome.extension.getBackgroundPage; const fn = chrome.extension.getBackgroundPage;
const bg = fn && fn(); const bg = fn && fn();
return bg === window || bg && bg.msg && bg.msg.isBgReady ? bg : null; return bg === window || bg && (bg.msg || {}).ready ? bg : null;
} }
function onRuntimeMessage({data, target}, sender, sendResponse) { function onRuntimeMessage({data, target}, sender, sendResponse) {
@ -138,12 +140,7 @@
}; };
} }
function unwrapResponseFactory(name) { function unwrap(localErr, {data, error} = NULL_RESPONSE) {
// Saving the local callstack before making an async call
return unwrapResponse.bind(null, new Error(`Callstack before invoking msg.${name}:`));
}
function unwrapResponse(localErr, {data, error} = {error: {message: ERR_NO_RECEIVER}}) {
return error return error
? Promise.reject(Object.assign(localErr, error, error.stack && { ? Promise.reject(Object.assign(localErr, error, error.stack && {
stack: `${error.stack}\n${localErr.stack}`, stack: `${error.stack}\n${localErr.stack}`,
@ -163,7 +160,7 @@
const message = {method: 'invokeAPI', path, args}; const message = {method: 'invokeAPI', path, args};
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) { if (!bg || !bg.msg || !bg.msg.ready && await bg.bgReady.all && false) {
res = msg.send(message); res = msg.send(message);
} else { } else {
res = deepMerge(await bg.msg._execute(TARGETS.extension, message, { res = deepMerge(await bg.msg._execute(TARGETS.extension, message, {