Drop getStyles

This commit is contained in:
eight 2018-10-07 21:40:29 +08:00
parent c5d41529d9
commit 69cae02381
4 changed files with 12 additions and 27 deletions

View File

@ -337,22 +337,14 @@ function updateIcon({tab, styles}) {
stylesReceived(styles); stylesReceived(styles);
return; return;
} }
getTabRealURL(tab) styleManager.countStylesByUrl(tab.url, {enabled: true})
.then(url => getStyles({matchUrl: url, asHash: true, omitCode: true})) .then(count => stylesReceived({length: count}));
.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);
}
function stylesReceived(styles) { 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 postfix = disableAll ? 'x' : !styles.length ? 'w' : '';
const color = prefs.get(disableAll ? 'badgeDisabled' : 'badgeNormal'); 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')] || ''; const iconset = ['', 'light/'][prefs.get('iconset')] || '';
let tabIcon = tabIcons.get(tab.id); let tabIcon = tabIcons.get(tab.id);
if (!tabIcon) tabIcons.set(tab.id, (tabIcon = {})); if (!tabIcon) tabIcons.set(tab.id, (tabIcon = {}));

View File

@ -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({ function filterStyles({
enabled = null, enabled = null,
id = null, id = null,

View File

@ -23,7 +23,8 @@ const styleManager = (() => {
toggleStyle, toggleStyle,
getAllStyles, // used by import-export getAllStyles, // used by import-export
getStylesInfoByUrl, // used by popup getStylesInfoByUrl, // used by popup
countStyles countStyles,
countStylesByUrl, // used by icon badge
}); });
function getAllStyles() { function getAllStyles() {
@ -251,6 +252,11 @@ const styleManager = (() => {
.map(k => getStyleWithNoCode(styles.get(Number(k)).data)); .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) { function getSectionsByUrl(url, filter) {
let cache = cachedStyleForUrl.get(url); let cache = cachedStyleForUrl.get(url);
if (!cache) { if (!cache) {

View File

@ -117,7 +117,7 @@ function configDialog(style) {
return; return;
} }
if (!bgStyle) { if (!bgStyle) {
API.getStyles({id: style.id, omitCode: !BG}) API.getStylesInfo({id: style.id})
.then(([bgStyle]) => save({anyChangeIsDirty}, bgStyle || {})); .then(([bgStyle]) => save({anyChangeIsDirty}, bgStyle || {}));
return; return;
} }