From a9df98c2f67b8afec94e5dfcc622d894255b0911 Mon Sep 17 00:00:00 2001 From: Nils Maier Date: Sun, 25 Aug 2019 20:33:54 +0200 Subject: [PATCH] Overhault fast filter matching Closes #11 --- lib/api.ts | 2 +- lib/filters.ts | 43 +++++++++++-------------------------------- lib/select.ts | 2 +- 3 files changed, 13 insertions(+), 34 deletions(-) diff --git a/lib/api.ts b/lib/api.ts index 8f3be75..f9945d2 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -18,7 +18,7 @@ const MAX_BATCH = 10000; export const API = new class { 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) { diff --git a/lib/filters.ts b/lib/filters.ts index e71611c..e409de4 100644 --- a/lib/filters.ts +++ b/lib/filters.ts @@ -6,11 +6,11 @@ import uuid from "./uuid"; import "./objectoverlay"; import { storage, i18n } from "./browser"; import { EventEmitter } from "./events"; -import { Prefs } from "./prefs"; import { TYPE_LINK, TYPE_MEDIA, TYPE_ALL } from "./constants"; // eslint-disable-next-line no-unused-vars import { Overlayable } from "./objectoverlay"; import * as DEFAULT_FILTERS from "../data/filters.json"; +import { FASTFILTER } from "./recentlist"; const REG_ESCAPE = /[{}()[\]\\^$.]/g; const REG_FNMATCH = /[*?]/; @@ -351,8 +351,6 @@ class Filters extends EventEmitter { private filters: Filter[]; - private fastFilter: string | null; - ignoreNext: boolean; private readonly typeMatchers: Map; @@ -362,10 +360,8 @@ class Filters extends EventEmitter { this.typeMatchers = new Map(); this.loaded = false; this.filters = []; - this.fastFilter = null; this.ignoreNext = false; this.regenerate(); - Prefs.on("fast-filter", this.updateFastFilter.bind(this)); storage.onChanged.addListener(async (changes: any) => { if (this.ignoreNext) { this.ignoreNext = false; @@ -438,21 +434,12 @@ class Filters extends EventEmitter { return new FastFilter(this, value); } - getFastFilter() { - if (!this.fastFilter) { - throw new Error("Nothing stored"); + async getFastFilter() { + await FASTFILTER.init(); + if (!FASTFILTER.current) { + return null; } - return new FastFilter(this, this.fastFilter); - } - - 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(); + return new FastFilter(this, FASTFILTER.current); } regenerate() { @@ -480,17 +467,6 @@ class Filters extends EventEmitter { 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_LINK, new Matcher(links)); this.typeMatchers.set(TYPE_MEDIA, new Matcher(media)); @@ -534,14 +510,17 @@ class Filters extends EventEmitter { defaultFilters[filter]); this.filters.push(new Filter(this, filter, current)); } - this.fastFilter = await Prefs.get("fast-filter", null); this.loaded = true; this.regenerate(); } - filterItemsByType(items: any[], type: number) { + async filterItemsByType(items: any[], type: number) { const matcher = this.typeMatchers.get(type); + const fast = await this.getFastFilter(); return items.filter(function(item) { + if (fast && fast.matchItem(item)) { + return true; + } return matcher && matcher.matchItem(item); }); } diff --git a/lib/select.ts b/lib/select.ts index 88fe10c..af76b9b 100644 --- a/lib/select.ts +++ b/lib/select.ts @@ -88,7 +88,7 @@ export async function select(links: any[], media: any[]) { let fast: any = null; let onlyFast: false; try { - fast = fm.getFastFilter(); + fast = await fm.getFastFilter(); } catch (ex) { // ignored