fixup! reduce badge update rate during page load

This commit is contained in:
tophf 2020-02-24 18:40:46 +03:00
parent ac5e907d72
commit a58d26c2e5
2 changed files with 11 additions and 9 deletions

View File

@ -28,13 +28,14 @@ const iconManager = (() => {
}); });
Object.assign(API_METHODS, { Object.assign(API_METHODS, {
/** @param {(number|string)[]} styleIds */ /** @param {(number|string)[]} styleIds
updateIconBadge(styleIds) { * @param {boolean} [lazyBadge=false] preventing flicker during page load */
updateIconBadge(styleIds, {lazyBadge} = {}) {
// FIXME: in some cases, we only have to redraw the badge. is it worth a optimization? // FIXME: in some cases, we only have to redraw the badge. is it worth a optimization?
const {frameId, tab: {id: tabId}} = this.sender; const {frameId, tab: {id: tabId}} = this.sender;
const value = styleIds.length ? styleIds.map(Number) : undefined; const value = styleIds.length ? styleIds.map(Number) : undefined;
tabManager.set(tabId, 'styleIds', frameId, value); tabManager.set(tabId, 'styleIds', frameId, value);
debounce(refreshStaleBadges, frameId ? 250 : 0); debounce(refreshStaleBadges, frameId && lazyBadge ? 250 : 0);
staleBadges.add(tabId); staleBadges.add(tabId);
if (!frameId) refreshIcon(tabId, true); if (!frameId) refreshIcon(tabId, true);
}, },
@ -52,7 +53,7 @@ const iconManager = (() => {
function onPortDisconnected({sender}) { function onPortDisconnected({sender}) {
if (tabManager.get(sender.tab.id, 'styleIds')) { if (tabManager.get(sender.tab.id, 'styleIds')) {
API_METHODS.updateIconBadge.call({sender}, []); API_METHODS.updateIconBadge.call({sender}, [], {lazyBadge: true});
} }
} }

View File

@ -19,6 +19,7 @@ self.INJECTED !== 1 && (() => {
const initializing = init(); const initializing = init();
/** @type chrome.runtime.Port */ /** @type chrome.runtime.Port */
let port; let port;
let lazyBadge = IS_FRAME;
// 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 (!IS_TAB) { if (!IS_TAB) {
@ -166,18 +167,18 @@ self.INJECTED !== 1 && (() => {
function updateCount() { function updateCount() {
if (!IS_TAB) return; if (!IS_TAB) return;
if (STYLE_VIA_API) {
API.styleViaAPI({method: 'updateCount'}).catch(msg.ignoreError);
} else {
API.updateIconBadge(styleInjector.list.map(style => style.id)).catch(msg.ignoreError);
}
if (IS_FRAME) { if (IS_FRAME) {
if (!port && styleInjector.list.length) { if (!port && styleInjector.list.length) {
port = chrome.runtime.connect({name: 'iframe'}); port = chrome.runtime.connect({name: 'iframe'});
} else if (port && !styleInjector.list.length) { } else if (port && !styleInjector.list.length) {
port.disconnect(); port.disconnect();
} }
if (lazyBadge && performance.now() > 1000) lazyBadge = false;
} }
(STYLE_VIA_API ?
API.styleViaAPI({method: 'updateCount'}) :
API.updateIconBadge(styleInjector.list.map(style => style.id), {lazyBadge})
).catch(msg.ignoreError);
} }
function orphanCheck() { function orphanCheck() {