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

View File

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