Skip to content

Commit 803f274

Browse files
committed
Also passing relevant signal to fetchTables and when fetching data from the watched query part of the hook.
1 parent 48dc2cb commit 803f274

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

packages/react/src/hooks/useQuery.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type QueryResult<T> = {
2020
/**
2121
* Function used to run the query again.
2222
*/
23-
refresh?: () => Promise<void>;
23+
refresh?: (signal?: AbortSignal) => Promise<void>;
2424
};
2525

2626
/**
@@ -112,9 +112,14 @@ export const useQuery = <T = any>(
112112
}
113113
};
114114

115-
const fetchTables = async () => {
115+
const fetchTables = async (signal?: AbortSignal) => {
116116
try {
117117
const tables = await powerSync.resolveTables(sqlStatement, memoizedParams, memoizedOptions);
118+
119+
if (signal?.aborted) {
120+
return;
121+
}
122+
118123
setTables(tables);
119124
} catch (e) {
120125
console.error('Failed to fetch tables:', e);
@@ -125,7 +130,7 @@ export const useQuery = <T = any>(
125130
React.useEffect(() => {
126131
const abortController = new AbortController();
127132
const updateData = async () => {
128-
await fetchTables();
133+
await fetchTables(abortController.signal);
129134
await fetchData(abortController.signal);
130135
};
131136

@@ -150,7 +155,7 @@ export const useQuery = <T = any>(
150155
powerSync.onChangeWithCallback(
151156
{
152157
onChange: async () => {
153-
await fetchData();
158+
await fetchData(abortController.current.signal);
154159
},
155160
onError(e) {
156161
handleError(e);

0 commit comments

Comments
 (0)