Skip to content

Commit

Permalink
onInstalled and onStartup run late
Browse files Browse the repository at this point in the history
  • Loading branch information
tophf committed Dec 15, 2024
1 parent 7281e0a commit cb66577
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
18 changes: 13 additions & 5 deletions src/background/icon-manager.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {kDisableAll, kInstall} from '/js/consts';
import {kDisableAll} from '/js/consts';
import {subscribe, __values as __prefs} from '/js/prefs';
import {CHROME, FIREFOX, MOBILE, VIVALDI} from '/js/ua';
import {debounce} from '/js/util';
import {ignoreChromeError, MF_ICON_EXT, MF_ICON_PATH} from '/js/util-webext';
import * as colorScheme from './color-scheme';
import {bgBusy, bgPreInit} from './common';
import {bgBusy, bgInit} from './common';
import {removePreloadedStyles} from './style-via-webrequest';
import * as tabMan from './tab-manager';

Expand All @@ -27,9 +27,17 @@ const kShowBadge = 'show-badge';
// https://github.com/openstyles/stylus/issues/335
let hasCanvas = FIREFOX_ANDROID ? false : null;

bgInit.push(initIcons);

bgBusy.then(() => {
const runNow = !__.MV3 || bgPreInit.includes(kInstall);
export async function refreshIconsWhenReady() {
if (bgBusy) {
bgInit[bgInit.indexOf(initIcons)] = 0;
await bgBusy;
}
initIcons(true);
}

function initIcons(runNow = !__.MV3) {
colorScheme.onChange(() => {
if (__prefs[kIconset] === -1) {
debounce(refreshGlobalIcon);
Expand All @@ -47,7 +55,7 @@ bgBusy.then(() => {
kDisableAll,
kIconset,
], () => debounce(refreshAllIcons), runNow);
});
}

tabMan.onUnload.add((tabId, frameId, port) => {
if (frameId && tabMan.getStyleIds(tabId)) {
Expand Down
8 changes: 6 additions & 2 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {bgBusy, bgInit, bgPreInit, stateDB} from './common';
import reinjectContentScripts from './content-scripts';
import initContextMenus from './context-menus';
import download from './download';
import {updateIconBadge} from './icon-manager';
import {refreshIconsWhenReady, updateIconBadge} from './icon-manager';
import prefsApi from './prefs-api';
import setClientData from './set-client-data';
import * as styleMan from './style-manager';
Expand Down Expand Up @@ -103,11 +103,15 @@ chrome.runtime.onInstalled.addListener(({reason, previousVersion}) => {
stateDB.clear(),
DNR.getDynamicRules().then(rules => updateDynamicRules(undefined, getRuleIds(rules))),
DNR.getSessionRules().then(rules => updateSessionRules(undefined, getRuleIds(rules))),
kInstall,
);
refreshIconsWhenReady();
}
});

if (__.MV3) {
chrome.runtime.onStartup.addListener(refreshIconsWhenReady);
}

onMessage(async (m, sender) => {
if (m.method === 'invokeAPI') {
if (bgBusy) await bgBusy;
Expand Down

0 comments on commit cb66577

Please sign in to comment.