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);
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 = {}));

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

View File

@ -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) {

View File

@ -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;
}