Enhance: don't cache enabled state
This commit is contained in:
parent
fbe77a8d15
commit
859afc8ee9
|
@ -26,7 +26,6 @@ const styleManager = (() => {
|
|||
maybeMatch: Set<styleId>,
|
||||
sections: Object<styleId => {
|
||||
id: styleId,
|
||||
enabled: Boolean,
|
||||
code: String
|
||||
}>
|
||||
} */
|
||||
|
@ -104,38 +103,10 @@ const styleManager = (() => {
|
|||
const style = styles.get(id);
|
||||
const data = Object.assign({}, style.data, {enabled});
|
||||
return saveStyle(data)
|
||||
.then(newData => {
|
||||
style.data = newData;
|
||||
for (const url of style.appliesTo) {
|
||||
const cache = cachedStyleForUrl.get(url);
|
||||
if (cache) {
|
||||
cache.sections[newData.id].enabled = newData.enabled;
|
||||
}
|
||||
}
|
||||
const message = {
|
||||
method: 'styleUpdated',
|
||||
reason: 'toggle',
|
||||
codeIsUpdated: false,
|
||||
style: {id, enabled}
|
||||
};
|
||||
// FIXME: is this faster?
|
||||
if (isAllExtensionUrl(style.appliesTo)) {
|
||||
return msg.broadcastExtension(message, 'both');
|
||||
}
|
||||
return msg.broadcast(message);
|
||||
})
|
||||
.then(newData => handleSave(newData, 'toggle', false))
|
||||
.then(() => id);
|
||||
}
|
||||
|
||||
function isAllExtensionUrl(urls) {
|
||||
for (const url of urls) {
|
||||
if (!/^\w+?-extension:\/\//.test(url)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function getStylesInfo(filter) {
|
||||
if (filter && filter.id) {
|
||||
return [getStyleWithNoCode(styles.get(filter.id).data)];
|
||||
|
@ -236,7 +207,7 @@ const styleManager = (() => {
|
|||
};
|
||||
}
|
||||
|
||||
function broadcastStyleUpdated(data, reason, method) {
|
||||
function broadcastStyleUpdated(data, reason, method, codeIsUpdated = true) {
|
||||
const style = styles.get(data.id);
|
||||
const excluded = new Set();
|
||||
const updated = new Set();
|
||||
|
@ -253,7 +224,6 @@ const styleManager = (() => {
|
|||
updated.add(url);
|
||||
cache.sections[data.id] = {
|
||||
id: data.id,
|
||||
enabled: data.enabled,
|
||||
code
|
||||
};
|
||||
}
|
||||
|
@ -265,7 +235,8 @@ const styleManager = (() => {
|
|||
id: data.id,
|
||||
enabled: data.enabled
|
||||
},
|
||||
reason
|
||||
reason,
|
||||
codeIsUpdated
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -285,7 +256,7 @@ const styleManager = (() => {
|
|||
});
|
||||
}
|
||||
|
||||
function handleSave(data, reason) {
|
||||
function handleSave(data, reason, codeIsUpdated) {
|
||||
const style = styles.get(data.id);
|
||||
let method;
|
||||
if (!style) {
|
||||
|
@ -298,7 +269,7 @@ const styleManager = (() => {
|
|||
style.data = data;
|
||||
method = 'styleUpdated';
|
||||
}
|
||||
return broadcastStyleUpdated(data, reason, method)
|
||||
return broadcastStyleUpdated(data, reason, method, codeIsUpdated)
|
||||
.then(() => data);
|
||||
}
|
||||
|
||||
|
@ -343,7 +314,7 @@ const styleManager = (() => {
|
|||
return result;
|
||||
}
|
||||
|
||||
function getSectionsByUrl(url, filter) {
|
||||
function getSectionsByUrl(url, id) {
|
||||
let cache = cachedStyleForUrl.get(url);
|
||||
if (!cache) {
|
||||
cache = {
|
||||
|
@ -359,15 +330,8 @@ const styleManager = (() => {
|
|||
.map(i => styles.get(i))
|
||||
);
|
||||
}
|
||||
if (filter) {
|
||||
const sections = !filter.id ? Object.values(cache.sections) :
|
||||
cache.sections[filter.id] ? [cache.sections[filter.id]] :
|
||||
[];
|
||||
return sections.filter(s => filterMatch(filter, s))
|
||||
.reduce((o, v) => {
|
||||
o[v.id] = v;
|
||||
return o;
|
||||
}, {});
|
||||
if (id) {
|
||||
return {[id]: cache.sections[id]};
|
||||
}
|
||||
return cache.sections;
|
||||
|
||||
|
@ -377,10 +341,8 @@ const styleManager = (() => {
|
|||
if (code) {
|
||||
cache.sections[data.id] = {
|
||||
id: data.id,
|
||||
enabled: data.enabled,
|
||||
code
|
||||
};
|
||||
// FIXME: memory leak
|
||||
appliesTo.add(url);
|
||||
}
|
||||
}
|
||||
|
@ -422,6 +384,9 @@ const styleManager = (() => {
|
|||
if (style.exclusions && style.exclusions.some(e => compileExclusion(e).test(url))) {
|
||||
return 'excluded';
|
||||
}
|
||||
if (!style.enabled) {
|
||||
return 'disabled';
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -38,11 +38,7 @@ API_METHODS.styleViaAPI = !CHROME && (() => {
|
|||
if (id === null && !ignoreUrlCheck && frameStyles.url === url) {
|
||||
return NOP;
|
||||
}
|
||||
const filter = {enabled: true};
|
||||
if (id !== null) {
|
||||
filter.id = id;
|
||||
}
|
||||
return styleManager.getSectionsByUrl(url, filter).then(sections => {
|
||||
return styleManager.getSectionsByUrl(url, id).then(sections => {
|
||||
const tasks = [];
|
||||
for (const section of Object.values(sections)) {
|
||||
const styleId = section.id;
|
||||
|
|
|
@ -41,7 +41,7 @@ const APPLY = (() => {
|
|||
if (!chrome.app && document instanceof XMLDocument) {
|
||||
return API.styleViaAPI({action: 'styleApply'});
|
||||
}
|
||||
return API.getSectionsByUrl(getMatchUrl(), {enabled: true})
|
||||
return API.getSectionsByUrl(getMatchUrl())
|
||||
.then(result => {
|
||||
const styles = Object.values(result);
|
||||
// CSS transition bug workaround: since we insert styles asynchronously,
|
||||
|
@ -182,7 +182,7 @@ const APPLY = (() => {
|
|||
if (request.codeIsUpdated === false) {
|
||||
applyStyleState(request.style);
|
||||
} else if (request.style.enabled) {
|
||||
API.getSectionsByUrl(getMatchUrl(), {id: request.style.id})
|
||||
API.getSectionsByUrl(getMatchUrl(), request.style.id)
|
||||
.then(sections => {
|
||||
if (!sections[request.style.id]) {
|
||||
removeStyle(request.style);
|
||||
|
@ -197,14 +197,14 @@ const APPLY = (() => {
|
|||
|
||||
case 'styleAdded':
|
||||
if (request.style.enabled) {
|
||||
API.getSectionsByUrl(getMatchUrl(), {id: request.style.id})
|
||||
API.getSectionsByUrl(getMatchUrl(), request.style.id)
|
||||
.then(buildSections)
|
||||
.then(applyStyles);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'urlChanged':
|
||||
API.getSectionsByUrl(getMatchUrl(), {enabled: true})
|
||||
API.getSectionsByUrl(getMatchUrl())
|
||||
.then(buildSections)
|
||||
.then(replaceAll);
|
||||
break;
|
||||
|
@ -288,7 +288,7 @@ const APPLY = (() => {
|
|||
addStyleElement(inCache);
|
||||
disabledElements.delete(id);
|
||||
} else {
|
||||
return API.getSectionsByUrl(getMatchUrl(), {id})
|
||||
return API.getSectionsByUrl(getMatchUrl(), id)
|
||||
.then(buildSections)
|
||||
.then(applyStyles);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user