Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: if onInstalled listener fails, "toggle" hotkey won't work #92

Open
WofWca opened this issue Jul 17, 2022 · 0 comments
Open

fix: if onInstalled listener fails, "toggle" hotkey won't work #92

WofWca opened this issue Jul 17, 2022 · 0 comments

Comments

@WofWca
Copy link
Owner

WofWca commented Jul 17, 2022

const currentVersion = chrome.runtime.getManifest().version;
let postInstallDonePromiseResolve: () => void;
// Resolves when it is made sure that all migrations have been run (if there are any) and it is safe to operate the
// storage.
const postInstallDonePromise = new Promise<void>(r => postInstallDonePromiseResolve = r);
// Pretty hacky. Feels like there must be API that allows us to do this. TODO?
browser.storage.local.get('__lastHandledUpdateToVersion').then(({ __lastHandledUpdateToVersion }) => {
if (currentVersion === __lastHandledUpdateToVersion) {
postInstallDonePromiseResolve();
}
});
browser.runtime.onInstalled.addListener(async details => {
if (!['update', 'install'].includes(details.reason)) {
return;
}
// In regard to popup or content scripts – this is pretty much guaranteed to finish running before them because
// popups don't get opened immediately upon installation and in order to get content scripts to work you'd
// need to reload the page.
if (details.reason === 'update') {
const { default: runRequiredMigrations } = await import(
/* webpackExports: ['default'] */
'./migrations/runRequiredMigrations'
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await runRequiredMigrations(details.previousVersion!);
}
await setNewSettingsKeysToDefaults();
browser.storage.local.set({ __lastHandledUpdateToVersion: currentVersion });
postInstallDonePromiseResolve();
});
// Just for top-level `await`. Don't do this for the whole file cause `runtime.onInstalled.addListener` needs to be
// called synchronously (https://developer.chrome.com/docs/extensions/mv2/background_pages/#listeners).
(async () => {
await postInstallDonePromise;

If there was an update of the extension (i.e. browser.runtime.onInstalled.addListener listener gets called), then, if it doesn't finish and then the browser is closed, initBrowserHotkeysListener initIconAndBadgeUpdater will not be called until the next update.

This seems to me like the browser.runtime.onInstalled API is poorly designed, requiring us to make this postInstallDonePromise.
Or could we just remove the onInstalled listener and just execute migrations every time __lastHandledUpdateToVersion is not equal to the version from the manifest?

This is not normal for the listener to fail thought, so it's not very important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant