fix: add active query tracking to QueryOnConnect and await its stop()

This commit is contained in:
Levente Kiss 2025-10-31 18:46:10 +13:00 committed by fryorcraken
parent 9793b40275
commit a97e7eceb9
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4

View File

@ -54,6 +54,7 @@ export class QueryOnConnect<
private isRunning: boolean = false; private isRunning: boolean = false;
private abortController?: AbortController; private abortController?: AbortController;
private activeQueryPromise?: Promise<void>;
private boundStoreConnectHandler?: (event: CustomEvent<PeerId>) => void; private boundStoreConnectHandler?: (event: CustomEvent<PeerId>) => void;
private boundHealthHandler?: (event: CustomEvent<HealthStatus>) => void; private boundHealthHandler?: (event: CustomEvent<HealthStatus>) => void;
@ -87,7 +88,7 @@ export class QueryOnConnect<
this.setupEventListeners(); this.setupEventListeners();
} }
public stop(): void { public async stop(): Promise<void> {
if (!this.isRunning) { if (!this.isRunning) {
return; return;
} }
@ -99,6 +100,15 @@ export class QueryOnConnect<
this.abortController = undefined; this.abortController = undefined;
} }
if (this.activeQueryPromise) {
log.info("Waiting for active query to complete...");
try {
await this.activeQueryPromise;
} catch (error) {
log.warn("Active query failed during stop:", error);
}
}
this.unsetEventListeners(); this.unsetEventListeners();
} }
@ -130,7 +140,10 @@ export class QueryOnConnect<
this.lastTimeOffline > this.lastSuccessfulQuery || this.lastTimeOffline > this.lastSuccessfulQuery ||
timeSinceLastQuery > this.forceQueryThresholdMs timeSinceLastQuery > this.forceQueryThresholdMs
) { ) {
await this.query(peerId); this.activeQueryPromise = this.query(peerId).finally(() => {
this.activeQueryPromise = undefined;
});
await this.activeQueryPromise;
} else { } else {
log.info(`no querying`); log.info(`no querying`);
} }