Cache enabled state

This commit is contained in:
eight 2018-10-06 15:11:01 +08:00
parent 1f18b13a92
commit e594b8ccb1
2 changed files with 43 additions and 95 deletions

View File

@ -49,7 +49,7 @@ const styleManager = (() => {
if ([...style.appliesTo].every(isExtensionUrl)) {
return msg.broadcastExtension(message);
}
return msg.broadcast(message);
return msg.broadcast(message, tab => style.appliesTo.has(tab.url));
})
.then(() => id);
}
@ -120,13 +120,15 @@ const styleManager = (() => {
.then(() => {
for (const url of style.appliesTo) {
const cache = cachedStyleForUrl.get(url);
if (cache) {
delete cache[id];
}
}
styles.delete(id);
return msg.broadcast({
method: 'styleDeleted',
style: {id}
});
}, tab => style.appliesTo.has(tab.url));
})
.then(() => id);
}
@ -187,7 +189,13 @@ const styleManager = (() => {
}
} else {
updated.set(url, code);
cache[newData.id] = code;
if (cache) {
cache[newData.id] = {
id: newData.id,
enabled: newData.enabled,
code
};
}
}
}
style.appliesTo = new Set(updated.keys());
@ -218,7 +226,11 @@ const styleManager = (() => {
}
const cache = cachedStyleForUrl.get(tab.url);
if (cache) {
cache[data.id] = code;
cache[data.id] = {
id: data.id,
enabled: data.enabled,
code
};
}
appliesTo.add(tab.url);
return {
@ -260,22 +272,26 @@ const styleManager = (() => {
}
function getSectionsByUrl(url, filterId) {
let result = cachedStyleForUrl.get(url);
if (!result) {
result = {};
let cache = cachedStyleForUrl.get(url);
if (!cache) {
cache = {};
for (const {appliesTo, data} of styles.values()) {
const code = getAppliedCode(url, data);
if (code) {
result[data.id] = code;
cache[data.id] = {
id: data.id,
enabled: data.enabled,
sections: code
};
appliesTo.add(url);
}
}
cachedStyleForUrl.set(url, result);
cachedStyleForUrl.set(url, cache);
}
if (filterId) {
return {[filterId]: result[filterId]};
return {[filterId]: cache[filterId]};
}
return result;
return cache;
}
function getAppliedCode(url, data) {
@ -336,7 +352,9 @@ const styleManager = (() => {
(!section.urlPrefixes || !section.urlPrefixes.length) &&
(!section.urls || !section.urls.length) &&
(!section.domains || !section.domains.length)
)
) {
return true;
}
return false;
}
@ -378,89 +396,12 @@ const styleManager = (() => {
function getDomain(url) {
// FIXME: use a naive regexp
return url.match(/\w+:\/\//);
return url.match(/^[\w-]+:\/\/(?:[\w:-]+@)?([^:/#]+)/)[1];
}
function getUrlNoHash(url) {
return url.split('#')[0];
}
// function cleanData(method, data) {
// if (
// (method === 'styleUpdated' || method === 'styleAdded') &&
// (data.sections || data.sourceCode)
// ) {
// apply/popup/manage use only meta for these two methods,
// editor may need the full code but can fetch it directly,
// so we send just the meta to avoid spamming lots of tabs with huge styles
// return getStyleWithNoCode(data);
// }
// return data;
// }
function isExtensionStyle(id) {
// TODO
// const style = styles.get(id);
// if (!style)
return false;
}
// function emitChanges(method, data) {
// const pendingPrivilage = runtimeSendMessage({method, cleanData(method, data)});
// const affectsAll = !msg.affects || msg.affects.all;
// const affectsOwnOriginOnly =
// !affectsAll && (msg.affects.editor || msg.affects.manager);
// const affectsTabs = affectsAll || affectsOwnOriginOnly;
// const affectsIcon = affectsAll || msg.affects.icon;
// const affectsPopup = affectsAll || msg.affects.popup;
// const affectsSelf = affectsPopup || msg.prefs;
// notify all open extension pages and popups
// if (affectsSelf) {
// msg.tabId = undefined;
// sendMessage(msg, ignoreChromeError);
// }
// notify tabs
// if (affectsTabs || affectsIcon) {
// const notifyTab = tab => {
// if (!styleUpdated
// && (affectsTabs || URLS.optionsUI.includes(tab.url))
// own pages are already notified via sendMessage
// && !(affectsSelf && tab.url.startsWith(URLS.ownOrigin))
// skip lazy-loaded aka unloaded tabs that seem to start loading on message in FF
// && (!FIREFOX || tab.width)) {
// msg.tabId = tab.id;
// sendMessage(msg, ignoreChromeError);
// }
// if (affectsIcon) {
// eslint-disable-next-line no-use-before-define
// debounce(API.updateIcon, 0, {tab});
// }
// };
// list all tabs including chrome-extension:// which can be ours
// Promise.all([
// queryTabs(isExtensionStyle(data.id) ? {url: URLS.ownOrigin + '*'} : {}),
// getActiveTab(),
// ]).then(([tabs, activeTab]) => {
// const activeTabId = activeTab && activeTab.id;
// for (const tab of tabs) {
// invokeOrPostpone(tab.id === activeTabId, notifyTab, tab);
// }
// });
// }
// notify self: the message no longer is sent to the origin in new Chrome
// if (typeof onRuntimeMessage !== 'undefined') {
// onRuntimeMessage(originalMessage);
// }
// notify apply.js on own pages
// if (typeof applyOnMessage !== 'undefined') {
// applyOnMessage(originalMessage);
// }
// propagate saved style state/code efficiently
// if (styleUpdated) {
// msg.refreshOwnTabs = false;
// API.refreshAllTabs(msg);
// }
// }
})();
function notifyAllTabs() {}

View File

@ -8,7 +8,8 @@ const msg = (() => {
isBg = true;
window._msg = {
id: 1,
storage: new Map()
storage: new Map(),
handler: null
};
}
const runtimeSend = promisify(chrome.runtime.sendMessage.bind(chrome.runtime));
@ -59,8 +60,11 @@ const msg = (() => {
}
function sendBg(data) {
if (bg === undefined) {
// always wrap doSend in promise
return preparing.then(doSend);
}
return withPromiseError(doSend);
function doSend() {
if (bg) {
@ -147,11 +151,14 @@ const msg = (() => {
if (handler) {
return;
}
bg._msg.handler = handler = {
handler = {
both: [],
tab: [],
extension: []
};
if (isBg) {
bg._msg.handler = handler;
}
chrome.runtime.onMessage.addListener(handleMessage);
}