Skip to content

Commit ba64abb

Browse files
committed
Improved the abort handling for stale watched query results when the query/parameters change. This fixes the edge case where an already fetching query would handle a query change and briefly report isFetching being false before becoming true again.
1 parent afca6f5 commit ba64abb

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

.changeset/real-dolls-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': patch
3+
---
4+
5+
Improved the abort handling for stale watched query results when the query/parameters change. This fixes the edge case where an already fetching query would handle a query change and briefly report `isFetching` being false before becoming true again.

packages/common/src/client/watched/processors/AbstractQueryProcessor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export abstract class AbstractQueryProcessor<
8383
* Updates the underlying query.
8484
*/
8585
async updateSettings(settings: Settings) {
86+
this.abortController.abort();
8687
await this.initialized;
8788

8889
if (!this.state.isFetching && this.reportFetching) {
@@ -92,7 +93,7 @@ export abstract class AbstractQueryProcessor<
9293
}
9394

9495
this.options.watchOptions = settings;
95-
this.abortController.abort();
96+
9697
this.abortController = new AbortController();
9798
await this.runWithReporting(() =>
9899
this.linkQuery({
@@ -121,7 +122,6 @@ export abstract class AbstractQueryProcessor<
121122
if (typeof update.data !== 'undefined') {
122123
await this.iterateAsyncListenersWithError(async (l) => l.onData?.(this.state.data));
123124
}
124-
125125
await this.iterateAsyncListenersWithError(async (l) => l.onStateChange?.(this.state));
126126
}
127127

packages/common/src/client/watched/processors/DifferentialQueryProcessor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ export class DifferentialQueryProcessor<RowType>
236236
db.onChangeWithCallback(
237237
{
238238
onChange: async () => {
239-
if (this.closed) {
239+
if (this.closed || abortSignal.aborted) {
240240
return;
241241
}
242242
// This fires for each change of the relevant tables
@@ -256,6 +256,10 @@ export class DifferentialQueryProcessor<RowType>
256256
db: this.options.db
257257
});
258258

259+
if (abortSignal.aborted) {
260+
return;
261+
}
262+
259263
if (this.reportFetching) {
260264
partialStateUpdate.isFetching = false;
261265
}

packages/common/src/client/watched/processors/OnChangeQueryProcessor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class OnChangeQueryProcessor<Data> extends AbstractQueryProcessor<Data, W
5757
db.onChangeWithCallback(
5858
{
5959
onChange: async () => {
60-
if (this.closed) {
60+
if (this.closed || abortSignal.aborted) {
6161
return;
6262
}
6363
// This fires for each change of the relevant tables
@@ -77,6 +77,10 @@ export class OnChangeQueryProcessor<Data> extends AbstractQueryProcessor<Data, W
7777
db: this.options.db
7878
});
7979

80+
if (abortSignal.aborted) {
81+
return;
82+
}
83+
8084
if (this.reportFetching) {
8185
partialStateUpdate.isFetching = false;
8286
}

0 commit comments

Comments
 (0)