From 859afc8ee9c2d964e1cb9c9dbac7c1613cefef64 Mon Sep 17 00:00:00 2001 From: eight Date: Sat, 13 Oct 2018 16:39:54 +0800 Subject: [PATCH] Enhance: don't cache enabled state --- background/style-manager.js | 59 ++++++++----------------------------- background/style-via-api.js | 6 +--- content/apply.js | 10 +++---- 3 files changed, 18 insertions(+), 57 deletions(-) diff --git a/background/style-manager.js b/background/style-manager.js index 4bef80a9..ec3bbd43 100644 --- a/background/style-manager.js +++ b/background/style-manager.js @@ -26,7 +26,6 @@ const styleManager = (() => { maybeMatch: Set, sections: Object { 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; } diff --git a/background/style-via-api.js b/background/style-via-api.js index 72c1138e..954f2e7b 100644 --- a/background/style-via-api.js +++ b/background/style-via-api.js @@ -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; diff --git a/content/apply.js b/content/apply.js index e8d780b0..c3fd540c 100644 --- a/content/apply.js +++ b/content/apply.js @@ -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); }