@powersync/react 0.0.0-dev-20250922105207 → 0.0.0-dev-20250925184532

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.
@@ -27,20 +27,51 @@ export const useWatchedQuery = (options) => {
27
27
  return watch;
28
28
  }
29
29
  const [watchedQuery, setWatchedQuery] = React.useState(createWatchedQuery);
30
+ const disposePendingUpdateListener = React.useRef(null);
30
31
  React.useEffect(() => {
31
32
  watchedQuery?.close();
32
- setWatchedQuery(createWatchedQuery);
33
+ const newQuery = createWatchedQuery();
34
+ setWatchedQuery(newQuery);
35
+ return () => {
36
+ disposePendingUpdateListener.current?.();
37
+ newQuery?.close();
38
+ };
33
39
  }, [powerSync, active]);
34
- // Indicates that the query will be re-fetched due to a change in the query.
35
- // Used when `isFetching` hasn't been set to true yet due to React execution.
36
- React.useEffect(() => {
37
- if (queryChanged) {
38
- watchedQuery?.updateSettings({
39
- query,
40
- throttleMs: hookOptions.throttleMs,
41
- reportFetching: hookOptions.reportFetching
42
- });
43
- }
44
- }, [queryChanged]);
45
- return useNullableWatchedQuerySubscription(watchedQuery);
40
+ /**
41
+ * Indicates that the query will be re-fetched due to a change in the query.
42
+ * We execute this in-line (not using an effect) since effects are delayed till after the hook returns.
43
+ * The `queryChanged` value should only be true for a single render.
44
+ * The `updateSettings` method is asynchronous, thus it will update the state asynchronously.
45
+ * In the React hooks we'd like to report that we are fetching the data for an updated query
46
+ * as soon as the query has been updated. This prevents a result flow where e.g. the hook:
47
+ * - already returned a result: isLoading, isFetching are both false
48
+ * - the query is updated, but the state is still isFetching=false from the previous state
49
+ * We override the isFetching status while the `updateSettings` method is running (if we report `isFetching`),
50
+ * we override this value just until the `updateSettings` method itself will update the `isFetching` status.
51
+ * We achieve this by registering a `settingsWillUpdate` listener on the `WatchedQuery`. This will fire
52
+ * just before the `isFetching` status is updated.
53
+ */
54
+ if (queryChanged) {
55
+ // Keep track of this pending operation
56
+ watchedQuery?.updateSettings({
57
+ query,
58
+ throttleMs: hookOptions.throttleMs,
59
+ reportFetching: hookOptions.reportFetching
60
+ });
61
+ // This could have been called multiple times, clear any old listeners.
62
+ disposePendingUpdateListener.current?.();
63
+ disposePendingUpdateListener.current = watchedQuery?.registerListener({
64
+ settingsWillUpdate: () => {
65
+ // We'll use the fact that we have a listener at all as an indication
66
+ disposePendingUpdateListener.current?.();
67
+ disposePendingUpdateListener.current = null;
68
+ }
69
+ });
70
+ }
71
+ const shouldReportCurrentlyFetching = (hookOptions.reportFetching ?? true) && !!disposePendingUpdateListener.current;
72
+ const result = useNullableWatchedQuerySubscription(watchedQuery);
73
+ return {
74
+ ...result,
75
+ isFetching: result?.isFetching || shouldReportCurrentlyFetching
76
+ };
46
77
  };
@@ -6,8 +6,12 @@ export type InternalHookOptions<DataType> = {
6
6
  queryChanged: boolean;
7
7
  active: boolean;
8
8
  };
9
- export declare const checkQueryChanged: <T>(query: WatchCompatibleQuery<T>, options: AdditionalOptions) => boolean;
9
+ interface WatchCompatibleQueryWithParams<T> extends WatchCompatibleQuery<T> {
10
+ stringifiedParameters?: string;
11
+ }
12
+ export declare const checkQueryChanged: <T>(query: WatchCompatibleQueryWithParams<T>, options: AdditionalOptions) => boolean;
10
13
  export declare const constructCompatibleQuery: <RowType>(query: string | CompilableQuery<RowType>, parameters: any[], options: AdditionalOptions) => {
11
- parsedQuery: WatchCompatibleQuery<RowType[]>;
14
+ parsedQuery: WatchCompatibleQueryWithParams<RowType[]>;
12
15
  queryChanged: boolean;
13
16
  };
17
+ export {};
@@ -9,7 +9,7 @@ export const checkQueryChanged = (query, options) => {
9
9
  return false; // If compilation fails, we assume the query has changed
10
10
  }
11
11
  const compiled = _compiled;
12
- const stringifiedParams = JSON.stringify(compiled.parameters);
12
+ const stringifiedParams = query.stringifiedParameters ?? JSON.stringify(compiled.parameters);
13
13
  const stringifiedOptions = JSON.stringify(options);
14
14
  const previousQueryRef = React.useRef({ sqlStatement: compiled.sql, stringifiedParams, stringifiedOptions });
15
15
  if (previousQueryRef.current.sqlStatement !== compiled.sql ||
@@ -24,6 +24,7 @@ export const checkQueryChanged = (query, options) => {
24
24
  };
25
25
  export const constructCompatibleQuery = (query, parameters = [], options) => {
26
26
  const powerSync = usePowerSync();
27
+ const stringifiedParameters = React.useMemo(() => JSON.stringify(parameters), [parameters]);
27
28
  const parsedQuery = React.useMemo(() => {
28
29
  if (typeof query == 'string') {
29
30
  return {
@@ -31,7 +32,9 @@ export const constructCompatibleQuery = (query, parameters = [], options) => {
31
32
  sql: query,
32
33
  parameters
33
34
  }),
34
- execute: () => powerSync.getAll(query, parameters)
35
+ execute: () => powerSync.getAll(query, parameters),
36
+ // Setting this is a small optimization that avoids checkQueryChanged recomputing the JSON representation.
37
+ stringifiedParameters
35
38
  };
36
39
  }
37
40
  else {
@@ -45,9 +48,11 @@ export const constructCompatibleQuery = (query, parameters = [], options) => {
45
48
  };
46
49
  },
47
50
  execute: () => query.execute()
51
+ // Note that we can't set stringifiedParameters here because we only know parameters after the query has been
52
+ // compiled.
48
53
  };
49
54
  }
50
- }, [query, powerSync]);
55
+ }, [query, powerSync, stringifiedParameters]);
51
56
  const queryChanged = checkQueryChanged(parsedQuery, options);
52
57
  return {
53
58
  parsedQuery,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powersync/react",
3
- "version": "0.0.0-dev-20250922105207",
3
+ "version": "0.0.0-dev-20250925184532",
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-20250922105207",
27
+ "@powersync/common": "0.0.0-dev-20250925184532",
28
28
  "react": "*"
29
29
  },
30
30
  "devDependencies": {
@@ -35,8 +35,9 @@
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-20250922105207",
39
- "@powersync/web": "0.0.0-dev-20250922105207"
38
+ "@powersync/common": "0.0.0-dev-20250925184532",
39
+ "@powersync/web": "0.0.0-dev-20250925184532",
40
+ "@powersync/drizzle-driver": "0.6.0"
40
41
  },
41
42
  "scripts": {
42
43
  "build": "tsc -b",