|
1 | 1 | import { WatchedQuery } from '@powersync/common'; |
2 | 2 | import React from 'react'; |
3 | 3 |
|
4 | | -const NO_OP_RELEASE = () => {}; |
5 | | - |
6 | 4 | /** |
7 | 5 | * The store will dispose this query if it has no subscribers attached to it. |
8 | 6 | * The suspense promise adds a subscriber to the query, but the promise could resolve |
9 | 7 | * before this component is committed. The promise will release it's listener once the query is no longer loading. |
10 | 8 | * This temporary hold is used to ensure that the query is not disposed in the interim. |
11 | 9 | * Creates a subscription for state change which creates a temporary hold on the query |
12 | | - * @returns a function to release the hold |
13 | 10 | */ |
14 | 11 | export const useTemporaryHold = (watchedQuery?: WatchedQuery<unknown>) => { |
15 | 12 | // Defaults to a no-op. If the provided WatchedQuery is not loading, we don't need a |
16 | 13 | // temporary hold. |
17 | | - const releaseTemporaryHold = React.useRef<() => void>(NO_OP_RELEASE); |
| 14 | + const releaseTemporaryHold = React.useRef<() => void | undefined>(undefined); |
18 | 15 | const addedHoldTo = React.useRef<WatchedQuery<unknown> | undefined>(undefined); |
19 | 16 |
|
20 | 17 | if (addedHoldTo.current !== watchedQuery) { |
21 | 18 | // The query changed, we no longer need the previous hold if present |
22 | 19 | releaseTemporaryHold.current?.(); |
23 | | - releaseTemporaryHold.current = NO_OP_RELEASE; |
| 20 | + releaseTemporaryHold.current = undefined; |
24 | 21 | addedHoldTo.current = watchedQuery; |
25 | 22 |
|
26 | 23 | if (!watchedQuery || !watchedQuery.state.isLoading) { |
27 | | - // No query to hold or no reason to hold, return a no-op |
28 | | - return { |
29 | | - releaseHold: releaseTemporaryHold.current |
30 | | - }; |
| 24 | + // No query to hold or no reason to hold, return |
| 25 | + return; |
31 | 26 | } |
32 | 27 |
|
33 | 28 | // Create a hold by subscribing |
@@ -67,10 +62,6 @@ export const useTemporaryHold = (watchedQuery?: WatchedQuery<unknown>) => { |
67 | 62 | // Set a timeout to conditionally remove the temporary hold |
68 | 63 | setTimeout(checkHold, timeoutPollMs); |
69 | 64 | } |
70 | | - |
71 | | - return { |
72 | | - releaseHold: releaseTemporaryHold.current |
73 | | - }; |
74 | 65 | }; |
75 | 66 |
|
76 | 67 | /** |
|
0 commit comments