Overhault fast filter matching

Closes #11
This commit is contained in:
Nils Maier 2019-08-25 20:33:54 +02:00
parent ab2b6e40af
commit a9df98c2f6
3 changed files with 13 additions and 34 deletions

View File

@ -18,7 +18,7 @@ const MAX_BATCH = 10000;
export const API = new class { export const API = new class {
async filter(arr: any, type: number) { async filter(arr: any, type: number) {
return (await filters()).filterItemsByType(arr, type); return await (await filters()).filterItemsByType(arr, type);
} }
async queue(items: any, options: any) { async queue(items: any, options: any) {

View File

@ -6,11 +6,11 @@ import uuid from "./uuid";
import "./objectoverlay"; import "./objectoverlay";
import { storage, i18n } from "./browser"; import { storage, i18n } from "./browser";
import { EventEmitter } from "./events"; import { EventEmitter } from "./events";
import { Prefs } from "./prefs";
import { TYPE_LINK, TYPE_MEDIA, TYPE_ALL } from "./constants"; import { TYPE_LINK, TYPE_MEDIA, TYPE_ALL } from "./constants";
// eslint-disable-next-line no-unused-vars // eslint-disable-next-line no-unused-vars
import { Overlayable } from "./objectoverlay"; import { Overlayable } from "./objectoverlay";
import * as DEFAULT_FILTERS from "../data/filters.json"; import * as DEFAULT_FILTERS from "../data/filters.json";
import { FASTFILTER } from "./recentlist";
const REG_ESCAPE = /[{}()[\]\\^$.]/g; const REG_ESCAPE = /[{}()[\]\\^$.]/g;
const REG_FNMATCH = /[*?]/; const REG_FNMATCH = /[*?]/;
@ -351,8 +351,6 @@ class Filters extends EventEmitter {
private filters: Filter[]; private filters: Filter[];
private fastFilter: string | null;
ignoreNext: boolean; ignoreNext: boolean;
private readonly typeMatchers: Map<number, Matcher>; private readonly typeMatchers: Map<number, Matcher>;
@ -362,10 +360,8 @@ class Filters extends EventEmitter {
this.typeMatchers = new Map(); this.typeMatchers = new Map();
this.loaded = false; this.loaded = false;
this.filters = []; this.filters = [];
this.fastFilter = null;
this.ignoreNext = false; this.ignoreNext = false;
this.regenerate(); this.regenerate();
Prefs.on("fast-filter", this.updateFastFilter.bind(this));
storage.onChanged.addListener(async (changes: any) => { storage.onChanged.addListener(async (changes: any) => {
if (this.ignoreNext) { if (this.ignoreNext) {
this.ignoreNext = false; this.ignoreNext = false;
@ -438,21 +434,12 @@ class Filters extends EventEmitter {
return new FastFilter(this, value); return new FastFilter(this, value);
} }
getFastFilter() { async getFastFilter() {
if (!this.fastFilter) { await FASTFILTER.init();
throw new Error("Nothing stored"); if (!FASTFILTER.current) {
return null;
} }
return new FastFilter(this, this.fastFilter); return new FastFilter(this, FASTFILTER.current);
}
async setFastFilter(value: string) {
this.fastFilter = value || "";
await Prefs.set("fast-filter", this.fastFilter);
}
updateFastFilter(pref: any, key: string, value: string) {
this.fastFilter = value || null;
this.regenerate();
} }
regenerate() { regenerate() {
@ -480,17 +467,6 @@ class Filters extends EventEmitter {
console.error("Filter", current.label || "unknown", ex); console.error("Filter", current.label || "unknown", ex);
} }
} }
if (this.fastFilter) {
try {
const fastFilter = new FastFilter(this, this.fastFilter);
all.push(fastFilter);
links.push(fastFilter);
media.push(fastFilter);
}
catch (ex) {
console.error("fast filter", this.fastFilter, "is invalid", ex);
}
}
this.typeMatchers.set(TYPE_ALL, new Matcher(all)); this.typeMatchers.set(TYPE_ALL, new Matcher(all));
this.typeMatchers.set(TYPE_LINK, new Matcher(links)); this.typeMatchers.set(TYPE_LINK, new Matcher(links));
this.typeMatchers.set(TYPE_MEDIA, new Matcher(media)); this.typeMatchers.set(TYPE_MEDIA, new Matcher(media));
@ -534,14 +510,17 @@ class Filters extends EventEmitter {
defaultFilters[filter]); defaultFilters[filter]);
this.filters.push(new Filter(this, filter, current)); this.filters.push(new Filter(this, filter, current));
} }
this.fastFilter = await Prefs.get("fast-filter", null);
this.loaded = true; this.loaded = true;
this.regenerate(); this.regenerate();
} }
filterItemsByType(items: any[], type: number) { async filterItemsByType(items: any[], type: number) {
const matcher = this.typeMatchers.get(type); const matcher = this.typeMatchers.get(type);
const fast = await this.getFastFilter();
return items.filter(function(item) { return items.filter(function(item) {
if (fast && fast.matchItem(item)) {
return true;
}
return matcher && matcher.matchItem(item); return matcher && matcher.matchItem(item);
}); });
} }

View File

@ -88,7 +88,7 @@ export async function select(links: any[], media: any[]) {
let fast: any = null; let fast: any = null;
let onlyFast: false; let onlyFast: false;
try { try {
fast = fm.getFastFilter(); fast = await fm.getFastFilter();
} }
catch (ex) { catch (ex) {
// ignored // ignored