From 1199418d0eadcb13c4948ca3d0455c898278ac18 Mon Sep 17 00:00:00 2001 From: Martti Malmi Date: Wed, 3 Jan 2024 17:19:22 +0200 Subject: [PATCH] check filter match on the worker side --- packages/app/src/Cache/IndexedDB.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/app/src/Cache/IndexedDB.ts b/packages/app/src/Cache/IndexedDB.ts index 5e0b902d..be8f1129 100644 --- a/packages/app/src/Cache/IndexedDB.ts +++ b/packages/app/src/Cache/IndexedDB.ts @@ -1,6 +1,7 @@ import Dexie, { Table } from "dexie"; import { TaggedNostrEvent, ReqFilter as Filter } from "@snort/system"; import * as Comlink from "comlink"; +import { eventMatchesFilter } from "@snort/system/src/request-matcher"; type Tag = { id: string; @@ -16,7 +17,6 @@ class IndexedDB extends Dexie { tags!: Table; private saveQueue: SaveQueueEntry[] = []; private seenEvents = new Set(); // LRU set maybe? - private seenFilters = new Set(); private subscribedEventIds = new Set(); private subscribedAuthors = new Set(); private subscribedTags = new Set(); @@ -137,12 +137,18 @@ class IndexedDB extends Dexie { // make sure only 1 argument is passed const cb = e => callback(e); + const filterCb = e => { + if (eventMatchesFilter(e, filter)) { + callback(e); + } + }; + if (filter["#p"] && Array.isArray(filter["#p"])) { for (const eventId of filter["#p"]) { this.subscribedTags.add("p|" + eventId); } - await this.subscribeToTags(cb); + await this.subscribeToTags(filterCb); return; } @@ -151,7 +157,7 @@ class IndexedDB extends Dexie { this.subscribedTags.add("e|" + eventId); } - await this.subscribeToTags(cb); + await this.subscribeToTags(filterCb); return; } @@ -160,7 +166,7 @@ class IndexedDB extends Dexie { this.subscribedTags.add("d|" + eventId); } - await this.subscribeToTags(cb); + await this.subscribeToTags(filterCb); return; } @@ -172,7 +178,7 @@ class IndexedDB extends Dexie { if (filter.authors?.length) { filter.authors.forEach(author => this.subscribedAuthors.add(author)); - await this.subscribeToAuthors(cb); + await this.subscribeToAuthors(filterCb); return; } @@ -190,7 +196,7 @@ class IndexedDB extends Dexie { // TODO test that the sort is actually working await query.each(e => { this.seenEvents.add(e.id); - cb(e); + filterCb(e); }); } }