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