diff --git a/packages/system/src/NostrSystem.ts b/packages/system/src/NostrSystem.ts index 4ecc1e33..c5bb7758 100644 --- a/packages/system/src/NostrSystem.ts +++ b/packages/system/src/NostrSystem.ts @@ -118,9 +118,13 @@ export class NostrSystem extends ExternalStore implements System return this.Queries.get(id); } - Query(type: { new (): T }, req: RequestBuilder): Query { + Query(type: { new(): T }, req: RequestBuilder): Query { const existing = this.Queries.get(req.id); if (existing) { + // if same instance, just return query + if (existing.fromInstance === req.instance) { + return existing; + } const filters = !req.options?.skipDiff ? req.buildDiff(this.#relayCache, existing.flatFilters) : req.build(this.#relayCache); @@ -137,7 +141,7 @@ export class NostrSystem extends ExternalStore implements System const store = new type(); const filters = req.build(this.#relayCache); - const q = new Query(req.id, store, req.options?.leaveOpen); + const q = new Query(req.id, req.instance, store, req.options?.leaveOpen); this.Queries.set(req.id, q); for (const subQ of filters) { this.SendQuery(q, subQ); diff --git a/packages/system/src/Query.ts b/packages/system/src/Query.ts index f56d7baa..20ce8070 100644 --- a/packages/system/src/Query.ts +++ b/packages/system/src/Query.ts @@ -111,6 +111,11 @@ export class Query implements QueryBase { */ id: string; + /** + * RequestBuilder instance + */ + fromInstance: string; + /** * Which relays this query has already been executed on */ @@ -138,9 +143,10 @@ export class Query implements QueryBase { #log = debug("Query"); - constructor(id: string, feed: NoteStore, leaveOpen?: boolean) { + constructor(id: string, instance: string, feed: NoteStore, leaveOpen?: boolean) { this.id = id; this.#feed = feed; + this.fromInstance = instance; this.#leaveOpen = leaveOpen ?? false; this.#checkTraces(); } diff --git a/packages/system/src/RequestBuilder.ts b/packages/system/src/RequestBuilder.ts index 129c0816..b93dc648 100644 --- a/packages/system/src/RequestBuilder.ts +++ b/packages/system/src/RequestBuilder.ts @@ -5,6 +5,7 @@ import { RelayCache, splitAllByWriteRelays, splitByWriteRelays } from "./GossipM import { mergeSimilar } from "./RequestMerger"; import { FlatReqFilter, expandFilter } from "./RequestExpander"; import debug from "debug"; +import { v4 as uuid } from "uuid"; /** * Which strategy is used when building REQ filters @@ -50,11 +51,13 @@ export interface RequestBuilderOptions { */ export class RequestBuilder { id: string; + instance: string; #builders: Array; #options?: RequestBuilderOptions; #log = debug("RequestBuilder"); constructor(id: string) { + this.instance = uuid(); this.id = id; this.#builders = []; } diff --git a/packages/system/tests/Query.test.ts b/packages/system/tests/Query.test.ts index 3c026319..11a7f139 100644 --- a/packages/system/tests/Query.test.ts +++ b/packages/system/tests/Query.test.ts @@ -10,7 +10,7 @@ window.crypto.getRandomValues = getRandomValues as any; describe("query", () => { test("progress", () => { - const q = new Query("test", new FlatNoteStore()); + const q = new Query("test", "", new FlatNoteStore()); const opt = { read: true, write: true, @@ -66,7 +66,7 @@ describe("query", () => { }); it("should merge all sub-query filters", () => { - const q = new Query("test", new FlatNoteStore()); + const q = new Query("test", "", new FlatNoteStore()); const c0 = new Connection("wss://test.com", { read: true, write: true }); q.sendToRelay(c0, { filters: [