check filter match on the worker side

This commit is contained in:
Martti Malmi 2024-01-03 17:19:22 +02:00
parent 395848fd8c
commit 1199418d0e

View File

@ -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<Tag>;
private saveQueue: SaveQueueEntry[] = [];
private seenEvents = new Set<string>(); // LRU set maybe?
private seenFilters = new Set<string>();
private subscribedEventIds = new Set<string>();
private subscribedAuthors = new Set<string>();
private subscribedTags = new Set<string>();
@ -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);
});
}
}