Fix: various

This commit is contained in:
eight 2018-10-13 15:58:31 +08:00
parent 8b4ab47d89
commit 7e0eddeb8f
2 changed files with 36 additions and 13 deletions

View File

@ -30,7 +30,16 @@ const styleManager = (() => {
code: String
}>
} */
const cachedStyleForUrl = createCache();
const cachedStyleForUrl = createCache({
onDeleted: (url, cache) => {
for (const section of Object.values(cache.sections)) {
const style = styles.get(section.id);
if (style) {
style.appliesTo.delete(url);
}
}
}
});
const BAD_MATCHER = {test: () => false};
const compileRe = createCompiler(text => `^(${text})$`);
@ -109,18 +118,22 @@ const styleManager = (() => {
codeIsUpdated: false,
style: {id, enabled}
};
if ([...style.appliesTo].every(isExtensionUrl)) {
// FIXME: is this faster?
if (isAllExtensionUrl(style.appliesTo)) {
return msg.broadcastExtension(message, 'both');
}
// FIXME: this won't work with iframes
// return msg.broadcast(message, tab => style.appliesTo.has(tab.url));
return msg.broadcast(message);
})
.then(() => id);
}
function isExtensionUrl(url) {
return /^\w+?-extension:\/\//.test(url);
function isAllExtensionUrl(urls) {
for (const url of urls) {
if (!/^\w+?-extension:\/\//.test(url)) {
return false;
}
}
return true;
}
function getStylesInfo(filter) {
@ -223,7 +236,7 @@ const styleManager = (() => {
};
}
function broadcastStyleUpdated(data, reason, method = 'styleUpdated') {
function broadcastStyleUpdated(data, reason, method) {
const style = styles.get(data.id);
const excluded = new Set();
const updated = new Set();
@ -289,9 +302,10 @@ const styleManager = (() => {
.then(() => data);
}
// get styles matching a URL, including sloppy regexps and excluded items.
function getStylesInfoByUrl(url) {
// FIXME: do we want to cache this? Who would like to rapidly using popup
// or searching the DB with the same URL?
// FIXME: do we want to cache this? Who would like to open popup rapidly
// or search the DB with the same URL?
const result = [];
for (const style of styles.values()) {
let excluded = false;
@ -351,8 +365,10 @@ const styleManager = (() => {
// return {[filter.id]: cache.sections[filter.id]};
// }
if (filter) {
return Object.values(cache.sections)
.filter(s => filterMatch(filter, s))
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;

View File

@ -2,7 +2,7 @@
'use strict';
// create a FIFO limit-size map.
function createCache(size = 1000) {
function createCache({size = 1000, onDeleted} = {}) {
const map = new Map();
const buffer = Array(size);
let index = 0;
@ -37,6 +37,9 @@ function createCache(size = 1000) {
if (map.size === size) {
// full
map.delete(buffer[lastIndex].id);
if (onDeleted) {
onDeleted(buffer[lastIndex].id, buffer[lastIndex].data);
}
lastIndex = (lastIndex + 1) % size;
}
const item = {id, data, index};
@ -48,13 +51,17 @@ function createCache(size = 1000) {
function delete_(id) {
const item = map.get(id);
if (!item) {
return;
return false;
}
map.delete(item.id);
const lastItem = buffer[lastIndex];
lastItem.index = item.index;
buffer[item.index] = lastItem;
lastIndex = (lastIndex + 1) % size;
if (onDeleted) {
onDeleted(item.id, item.data);
}
return true;
}
function clear() {