@powersync/react 1.7.0 → 1.7.2
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.
- package/lib/hooks/watched/useQuery.js +21 -21
- package/lib/hooks/watched/useSingleQuery.d.ts +3 -0
- package/lib/hooks/watched/useSingleQuery.js +13 -7
- package/lib/hooks/watched/useWatchedQuery.js +11 -8
- package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +4 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js +16 -9
- package/lib/hooks/watched/watch-utils.d.ts +1 -0
- package/lib/hooks/watched/watch-utils.js +1 -1
- package/package.json +5 -4
|
@@ -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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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) => {
|
|
@@ -47,7 +47,7 @@ export const constructCompatibleQuery = (query, parameters = [], options) => {
|
|
|
47
47
|
execute: () => query.execute()
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
}, [query, powerSync]);
|
|
50
|
+
}, [query, powerSync, ...parameters]);
|
|
51
51
|
const queryChanged = checkQueryChanged(parsedQuery, options);
|
|
52
52
|
return {
|
|
53
53
|
parsedQuery,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/react",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.2",
|
|
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": "^1.
|
|
27
|
+
"@powersync/common": "^1.37.0",
|
|
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": "1.
|
|
39
|
-
"@powersync/web": "1.
|
|
38
|
+
"@powersync/common": "1.37.0",
|
|
39
|
+
"@powersync/web": "1.26.0",
|
|
40
|
+
"@powersync/drizzle-driver": "0.5.0"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
42
43
|
"build": "tsc -b",
|