Skip to content

Commit 6745f50

Browse files
fix suspending queries
1 parent 7f2c53d commit 6745f50

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

.changeset/beige-jars-whisper.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/react': patch
3+
---
4+
5+
Fixed regression in useSuspendingQuery where `releaseHold is not a function` could be thrown during hot reload or if the WatchedQuery `loading` state resolved before the first re-render.

packages/react/src/hooks/suspense/suspense-utils.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { WatchedQuery } from '@powersync/common';
22
import React from 'react';
33

4+
const NO_OP_RELEASE = () => {};
5+
46
/**
57
* The store will dispose this query if it has no subscribers attached to it.
68
* The suspense promise adds a subscriber to the query, but the promise could resolve
@@ -10,17 +12,20 @@ import React from 'react';
1012
* @returns a function to release the hold
1113
*/
1214
export const useTemporaryHold = (watchedQuery?: WatchedQuery<unknown>) => {
13-
const releaseTemporaryHold = React.useRef<(() => void) | undefined>(undefined);
15+
// Defaults to a no-op. If the provided WatchedQuery is not loading, we don't need a
16+
// temporary hold.
17+
const releaseTemporaryHold = React.useRef<() => void>(NO_OP_RELEASE);
1418
const addedHoldTo = React.useRef<WatchedQuery<unknown> | undefined>(undefined);
1519

1620
if (addedHoldTo.current !== watchedQuery) {
1721
releaseTemporaryHold.current?.();
22+
releaseTemporaryHold.current = NO_OP_RELEASE;
1823
addedHoldTo.current = watchedQuery;
1924

2025
if (!watchedQuery || !watchedQuery.state.isLoading) {
2126
// No query to hold or no reason to hold, return a no-op
2227
return {
23-
releaseHold: () => {}
28+
releaseHold: releaseTemporaryHold.current
2429
};
2530
}
2631

0 commit comments

Comments
 (0)