From 69cae02381fc8f1f3e0b33cbda587f4705acdf1d Mon Sep 17 00:00:00 2001 From: eight Date: Sun, 7 Oct 2018 21:40:29 +0800 Subject: [PATCH] Drop getStyles --- background/background.js | 16 ++++------------ background/storage.js | 13 ------------- background/style-manager.js | 8 +++++++- manage/config-dialog.js | 2 +- 4 files changed, 12 insertions(+), 27 deletions(-) diff --git a/background/background.js b/background/background.js index 5598d7ac..10338ac1 100644 --- a/background/background.js +++ b/background/background.js @@ -337,22 +337,14 @@ function updateIcon({tab, styles}) { stylesReceived(styles); return; } - getTabRealURL(tab) - .then(url => getStyles({matchUrl: url, asHash: true, omitCode: true})) - .then(stylesReceived); - - function getLength(styles) { - const keys = Object.keys(styles || {}); - // Using "included" because styles may not include the psuedo-section added by the - // filterStylesInternal function - return String(keys.filter(key => styles[key][0] ? styles[key][0].included : false).length); - } + styleManager.countStylesByUrl(tab.url, {enabled: true}) + .then(count => stylesReceived({length: count})); function stylesReceived(styles) { - const disableAll = 'disableAll' in styles ? styles.disableAll : prefs.get('disableAll'); + const disableAll = prefs.get('disableAll'); const postfix = disableAll ? 'x' : !styles.length ? 'w' : ''; const color = prefs.get(disableAll ? 'badgeDisabled' : 'badgeNormal'); - const text = prefs.get('show-badge') && styles.length ? getLength(styles) : ''; + const text = prefs.get('show-badge') && styles.length ? String(styles.length) : ''; const iconset = ['', 'light/'][prefs.get('iconset')] || ''; let tabIcon = tabIcons.get(tab.id); if (!tabIcon) tabIcons.set(tab.id, (tabIcon = {})); diff --git a/background/storage.js b/background/storage.js index 61ad39f3..59c74e06 100644 --- a/background/storage.js +++ b/background/storage.js @@ -29,19 +29,6 @@ var cachedStyles = { }, }; -function getStyles(options) { - if (cachedStyles.list) { - return Promise.resolve(filterStyles(options)); - } - if (cachedStyles.mutex.inProgress) { - return new Promise(resolve => { - cachedStyles.mutex.onDone.push({options, resolve}); - }); - } - cachedStyles.mutex.inProgress = true; -} - - function filterStyles({ enabled = null, id = null, diff --git a/background/style-manager.js b/background/style-manager.js index bcd4af6b..5ca04594 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -23,7 +23,8 @@ const styleManager = (() => { toggleStyle, getAllStyles, // used by import-export getStylesInfoByUrl, // used by popup - countStyles + countStyles, + countStylesByUrl, // used by icon badge }); function getAllStyles() { @@ -251,6 +252,11 @@ const styleManager = (() => { .map(k => getStyleWithNoCode(styles.get(Number(k)).data)); } + function countStylesByUrl(url, filter) { + const sections = getSectionsByUrl(url, filter); + return Object.keys(sections).length; + } + function getSectionsByUrl(url, filter) { let cache = cachedStyleForUrl.get(url); if (!cache) { diff --git a/manage/config-dialog.js b/manage/config-dialog.js index 4e0cbab0..1f603b7b 100644 --- a/manage/config-dialog.js +++ b/manage/config-dialog.js @@ -117,7 +117,7 @@ function configDialog(style) { return; } if (!bgStyle) { - API.getStyles({id: style.id, omitCode: !BG}) + API.getStylesInfo({id: style.id}) .then(([bgStyle]) => save({anyChangeIsDirty}, bgStyle || {})); return; }