@powersync/react 0.0.0-dev-20250807074601 → 0.0.0-dev-20250828091657

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,11 +5,8 @@ import { WatchedQuery } from '@powersync/common';
5
5
  * before this component is committed. The promise will release it's listener once the query is no longer loading.
6
6
  * This temporary hold is used to ensure that the query is not disposed in the interim.
7
7
  * Creates a subscription for state change which creates a temporary hold on the query
8
- * @returns a function to release the hold
9
8
  */
10
- export declare const useTemporaryHold: (watchedQuery?: WatchedQuery<unknown>) => {
11
- releaseHold: () => void;
12
- };
9
+ export declare const useTemporaryHold: (watchedQuery?: WatchedQuery<unknown>) => void;
13
10
  /**
14
11
  * React suspense relies on a promise that resolves once the initial data has loaded.
15
12
  * This creates a promise which registers a listener on the watched query.
@@ -1,28 +1,24 @@
1
1
  import React from 'react';
2
- const NO_OP_RELEASE = () => { };
3
2
  /**
4
3
  * The store will dispose this query if it has no subscribers attached to it.
5
4
  * The suspense promise adds a subscriber to the query, but the promise could resolve
6
5
  * before this component is committed. The promise will release it's listener once the query is no longer loading.
7
6
  * This temporary hold is used to ensure that the query is not disposed in the interim.
8
7
  * Creates a subscription for state change which creates a temporary hold on the query
9
- * @returns a function to release the hold
10
8
  */
11
9
  export const useTemporaryHold = (watchedQuery) => {
12
- // Defaults to a no-op. If the provided WatchedQuery is not loading, we don't need a
13
- // temporary hold.
14
- const releaseTemporaryHold = React.useRef(NO_OP_RELEASE);
10
+ const releaseTemporaryHold = React.useRef(undefined);
15
11
  const addedHoldTo = React.useRef(undefined);
16
12
  if (addedHoldTo.current !== watchedQuery) {
13
+ // The query changed, we no longer need the previous hold if present
17
14
  releaseTemporaryHold.current?.();
18
- releaseTemporaryHold.current = NO_OP_RELEASE;
15
+ releaseTemporaryHold.current = undefined;
19
16
  addedHoldTo.current = watchedQuery;
20
17
  if (!watchedQuery || !watchedQuery.state.isLoading) {
21
- // No query to hold or no reason to hold, return a no-op
22
- return {
23
- releaseHold: releaseTemporaryHold.current
24
- };
18
+ // No query to hold or no reason to hold, return
19
+ return;
25
20
  }
21
+ // Create a hold by subscribing
26
22
  const disposeSubscription = watchedQuery.registerListener({
27
23
  onStateChange: (state) => { }
28
24
  });
@@ -54,9 +50,6 @@ export const useTemporaryHold = (watchedQuery) => {
54
50
  // Set a timeout to conditionally remove the temporary hold
55
51
  setTimeout(checkHold, timeoutPollMs);
56
52
  }
57
- return {
58
- releaseHold: releaseTemporaryHold.current
59
- };
60
53
  };
61
54
  /**
62
55
  * React suspense relies on a promise that resolves once the initial data has loaded.
@@ -25,7 +25,7 @@ export const useSingleSuspenseQuery = (query, parameters = [], options = {}) =>
25
25
  const store = getQueryStore(powerSync);
26
26
  // Only use a temporary watched query if we don't have data yet.
27
27
  const watchedQuery = data ? null : store.getQuery(key, parsedQuery, options);
28
- const { releaseHold } = useTemporaryHold(watchedQuery);
28
+ useTemporaryHold(watchedQuery);
29
29
  React.useEffect(() => {
30
30
  // Set the initial yielded data
31
31
  // it should be available once we commit the component
@@ -36,9 +36,6 @@ export const useSingleSuspenseQuery = (query, parameters = [], options = {}) =>
36
36
  setData(watchedQuery.state.data);
37
37
  setError(null);
38
38
  }
39
- if (!watchedQuery?.state.isLoading) {
40
- releaseHold();
41
- }
42
39
  }, []);
43
40
  if (error != null) {
44
41
  // Report errors - this is caught by an error boundary
@@ -22,7 +22,7 @@ import { createSuspendingPromise, useTemporaryHold } from './suspense-utils.js';
22
22
  * }
23
23
  */
24
24
  export const useWatchedQuerySuspenseSubscription = (query) => {
25
- const { releaseHold } = useTemporaryHold(query);
25
+ useTemporaryHold(query);
26
26
  // Force update state function
27
27
  const [, setUpdateCounter] = React.useState(0);
28
28
  React.useEffect(() => {
@@ -34,11 +34,6 @@ export const useWatchedQuerySuspenseSubscription = (query) => {
34
34
  setUpdateCounter((prev) => prev + 1);
35
35
  }
36
36
  });
37
- // This runs on the first iteration before the component is suspended
38
- // We should only release the hold once the component is no longer loading
39
- if (!query.state.isLoading) {
40
- releaseHold();
41
- }
42
37
  return dispose;
43
38
  }, []);
44
39
  if (query.state.error != null) {
@@ -8,25 +8,25 @@ export function useQuery(query, parameters = [], options = {}) {
8
8
  return { isLoading: false, isFetching: false, data: [], error: new Error('PowerSync not configured.') };
9
9
  }
10
10
  const { parsedQuery, queryChanged } = constructCompatibleQuery(query, parameters, options);
11
- switch (options?.runQueryOnce) {
12
- case true:
13
- return useSingleQuery({
14
- query: parsedQuery,
15
- powerSync,
16
- queryChanged
17
- });
18
- default:
19
- return useWatchedQuery({
20
- query: parsedQuery,
21
- powerSync,
22
- queryChanged,
23
- options: {
24
- reportFetching: options.reportFetching,
25
- // Maintains backwards compatibility with previous versions
26
- // Differentiation is opt-in by default
27
- // We emit new data for each table change by default.
28
- rowComparator: options.rowComparator
29
- }
30
- });
31
- }
11
+ const runOnce = options?.runQueryOnce == true;
12
+ const single = useSingleQuery({
13
+ query: parsedQuery,
14
+ powerSync,
15
+ queryChanged,
16
+ active: runOnce
17
+ });
18
+ const watched = useWatchedQuery({
19
+ query: parsedQuery,
20
+ powerSync,
21
+ queryChanged,
22
+ options: {
23
+ reportFetching: options.reportFetching,
24
+ // Maintains backwards compatibility with previous versions
25
+ // Differentiation is opt-in by default
26
+ // We emit new data for each table change by default.
27
+ rowComparator: options.rowComparator
28
+ },
29
+ active: !runOnce
30
+ });
31
+ return runOnce ? single : watched;
32
32
  }
@@ -1,3 +1,6 @@
1
1
  import { QueryResult } from './watch-types.js';
2
2
  import { InternalHookOptions } from './watch-utils.js';
3
+ /**
4
+ * @internal not exported from `index.ts`
5
+ */
3
6
  export declare const useSingleQuery: <RowType = any>(options: InternalHookOptions<RowType[]>) => QueryResult<RowType>;
@@ -1,6 +1,9 @@
1
1
  import React from 'react';
2
+ /**
3
+ * @internal not exported from `index.ts`
4
+ */
2
5
  export const useSingleQuery = (options) => {
3
- const { query, powerSync, queryChanged } = options;
6
+ const { query, powerSync, queryChanged, active } = options;
4
7
  const [output, setOutputState] = React.useState({
5
8
  isLoading: true,
6
9
  isFetching: true,
@@ -38,13 +41,16 @@ export const useSingleQuery = (options) => {
38
41
  }
39
42
  }, [queryChanged, query]);
40
43
  // Trigger initial query execution
44
+ // @ts-ignore: Complains about not all code paths returning a value
41
45
  React.useEffect(() => {
42
- const abortController = new AbortController();
43
- runQuery(abortController.signal);
44
- return () => {
45
- abortController.abort();
46
- };
47
- }, [powerSync, queryChanged]);
46
+ if (active) {
47
+ const abortController = new AbortController();
48
+ runQuery(abortController.signal);
49
+ return () => {
50
+ abortController.abort();
51
+ };
52
+ }
53
+ }, [powerSync, active, queryChanged]);
48
54
  return {
49
55
  ...output,
50
56
  refresh: runQuery
@@ -1,5 +1,5 @@
1
1
  import React from 'react';
2
- import { useWatchedQuerySubscription } from './useWatchedQuerySubscription.js';
2
+ import { useNullableWatchedQuerySubscription } from './useWatchedQuerySubscription.js';
3
3
  /**
4
4
  * @internal This is not exported from the index.ts
5
5
  *
@@ -9,8 +9,11 @@ import { useWatchedQuerySubscription } from './useWatchedQuerySubscription.js';
9
9
  * not affect the internal implementation in this case.
10
10
  */
11
11
  export const useWatchedQuery = (options) => {
12
- const { query, powerSync, queryChanged, options: hookOptions } = options;
13
- const createWatchedQuery = React.useCallback(() => {
12
+ const { query, powerSync, queryChanged, options: hookOptions, active } = options;
13
+ function createWatchedQuery() {
14
+ if (!active) {
15
+ return null;
16
+ }
14
17
  const watch = hookOptions.rowComparator
15
18
  ? powerSync.customQuery(query).differentialWatch({
16
19
  rowComparator: hookOptions.rowComparator,
@@ -22,22 +25,22 @@ export const useWatchedQuery = (options) => {
22
25
  throttleMs: hookOptions.throttleMs
23
26
  });
24
27
  return watch;
25
- }, []);
28
+ }
26
29
  const [watchedQuery, setWatchedQuery] = React.useState(createWatchedQuery);
27
30
  React.useEffect(() => {
28
- watchedQuery.close();
31
+ watchedQuery?.close();
29
32
  setWatchedQuery(createWatchedQuery);
30
- }, [powerSync]);
33
+ }, [powerSync, active]);
31
34
  // Indicates that the query will be re-fetched due to a change in the query.
32
35
  // Used when `isFetching` hasn't been set to true yet due to React execution.
33
36
  React.useEffect(() => {
34
37
  if (queryChanged) {
35
- watchedQuery.updateSettings({
38
+ watchedQuery?.updateSettings({
36
39
  query,
37
40
  throttleMs: hookOptions.throttleMs,
38
41
  reportFetching: hookOptions.reportFetching
39
42
  });
40
43
  }
41
44
  }, [queryChanged]);
42
- return useWatchedQuerySubscription(watchedQuery);
45
+ return useNullableWatchedQuerySubscription(watchedQuery);
43
46
  };
@@ -14,3 +14,7 @@ import { WatchedQuery } from '@powersync/common';
14
14
  *
15
15
  */
16
16
  export declare const useWatchedQuerySubscription: <ResultType = unknown, Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>>(query: Query) => Query["state"];
17
+ /**
18
+ * @internal
19
+ */
20
+ export declare const useNullableWatchedQuerySubscription: <ResultType = unknown, Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>>(query: Query | null) => Query["state"] | undefined;
@@ -14,16 +14,23 @@ import React from 'react';
14
14
  *
15
15
  */
16
16
  export const useWatchedQuerySubscription = (query) => {
17
- const [output, setOutputState] = React.useState(query.state);
17
+ return useNullableWatchedQuerySubscription(query);
18
+ };
19
+ /**
20
+ * @internal
21
+ */
22
+ export const useNullableWatchedQuerySubscription = (query) => {
23
+ const [output, setOutputState] = React.useState(query?.state);
24
+ // @ts-ignore: Complains about not all code paths returning a value
18
25
  React.useEffect(() => {
19
- const dispose = query.registerListener({
20
- onStateChange: (state) => {
21
- setOutputState({ ...state });
22
- }
23
- });
24
- return () => {
25
- dispose();
26
- };
26
+ if (query) {
27
+ setOutputState(query.state);
28
+ return query.registerListener({
29
+ onStateChange: (state) => {
30
+ setOutputState({ ...state });
31
+ }
32
+ });
33
+ }
27
34
  }, [query]);
28
35
  return output;
29
36
  };
@@ -4,6 +4,7 @@ export type InternalHookOptions<DataType> = {
4
4
  query: WatchCompatibleQuery<DataType>;
5
5
  powerSync: AbstractPowerSyncDatabase;
6
6
  queryChanged: boolean;
7
+ active: boolean;
7
8
  };
8
9
  export declare const checkQueryChanged: <T>(query: WatchCompatibleQuery<T>, options: AdditionalOptions) => boolean;
9
10
  export declare const constructCompatibleQuery: <RowType>(query: string | CompilableQuery<RowType>, parameters: any[], options: AdditionalOptions) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/react",
3
- "version": "0.0.0-dev-20250807074601",
3
+ "version": "0.0.0-dev-20250828091657",
4
4
  "publishConfig": {
5
5
  "registry": "https://registry.npmjs.org/",
6
6
  "access": "public"
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "homepage": "https://docs.powersync.com",
26
26
  "peerDependencies": {
27
- "@powersync/common": "0.0.0-dev-20250807074601",
27
+ "@powersync/common": "0.0.0-dev-20250828091657",
28
28
  "react": "*"
29
29
  },
30
30
  "devDependencies": {
@@ -35,8 +35,8 @@
35
35
  "react": "18.3.1",
36
36
  "react-dom": "18.3.1",
37
37
  "react-error-boundary": "^4.1.0",
38
- "@powersync/common": "0.0.0-dev-20250807074601",
39
- "@powersync/web": "0.0.0-dev-20250807074601"
38
+ "@powersync/common": "0.0.0-dev-20250828091657",
39
+ "@powersync/web": "0.0.0-dev-20250828091657"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "tsc -b",