@powersync/react 0.0.0-dev-20250722092404 → 0.0.0-dev-20250728083821
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/README.md +1 -121
- package/lib/QueryStore.d.ts +6 -5
- package/lib/QueryStore.js +6 -32
- package/lib/WatchedQuery.d.ts +36 -0
- package/lib/WatchedQuery.js +148 -0
- package/lib/hooks/{deprecated/usePowerSyncQuery.js → usePowerSyncQuery.js} +1 -1
- package/lib/hooks/{deprecated/usePowerSyncStatus.js → usePowerSyncStatus.js} +1 -1
- package/lib/hooks/{deprecated/usePowerSyncWatchedQuery.js → usePowerSyncWatchedQuery.js} +1 -1
- package/lib/hooks/useQuery.d.ts +34 -0
- package/lib/hooks/useQuery.js +126 -0
- package/lib/hooks/useStatus.js +2 -2
- package/lib/hooks/useSuspenseQuery.d.ts +25 -0
- package/lib/hooks/useSuspenseQuery.js +74 -0
- package/lib/index.d.ts +5 -9
- package/lib/index.js +5 -7
- package/package.json +3 -6
- package/lib/hooks/suspense/SuspenseQueryResult.d.ts +0 -3
- package/lib/hooks/suspense/SuspenseQueryResult.js +0 -1
- package/lib/hooks/suspense/suspense-utils.d.ts +0 -19
- package/lib/hooks/suspense/suspense-utils.js +0 -78
- package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +0 -10
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +0 -75
- package/lib/hooks/suspense/useSuspenseQuery.d.ts +0 -53
- package/lib/hooks/suspense/useSuspenseQuery.js +0 -10
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.d.ts +0 -23
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +0 -56
- package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +0 -6
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +0 -25
- package/lib/hooks/watched/useQuery.d.ts +0 -38
- package/lib/hooks/watched/useQuery.js +0 -32
- package/lib/hooks/watched/useSingleQuery.d.ts +0 -3
- package/lib/hooks/watched/useSingleQuery.js +0 -52
- package/lib/hooks/watched/useWatchedQuery.d.ts +0 -13
- package/lib/hooks/watched/useWatchedQuery.js +0 -43
- package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +0 -16
- package/lib/hooks/watched/useWatchedQuerySubscription.js +0 -29
- package/lib/hooks/watched/watch-types.d.ts +0 -62
- package/lib/hooks/watched/watch-types.js +0 -1
- package/lib/hooks/watched/watch-utils.d.ts +0 -12
- package/lib/hooks/watched/watch-utils.js +0 -56
- /package/lib/hooks/{deprecated/usePowerSyncQuery.d.ts → usePowerSyncQuery.d.ts} +0 -0
- /package/lib/hooks/{deprecated/usePowerSyncStatus.d.ts → usePowerSyncStatus.d.ts} +0 -0
- /package/lib/hooks/{deprecated/usePowerSyncWatchedQuery.d.ts → usePowerSyncWatchedQuery.d.ts} +0 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { generateQueryKey, getQueryStore } from '../QueryStore';
|
|
3
|
+
import { usePowerSync } from './PowerSyncContext';
|
|
4
|
+
import { parseQuery } from '@powersync/common';
|
|
5
|
+
/**
|
|
6
|
+
* A hook to access the results of a watched query that suspends until the initial result has loaded.
|
|
7
|
+
* @example
|
|
8
|
+
* export const ContentComponent = () => {
|
|
9
|
+
* const { data: lists } = useSuspenseQuery('SELECT * from lists');
|
|
10
|
+
*
|
|
11
|
+
* return <View>
|
|
12
|
+
* {lists.map((l) => (
|
|
13
|
+
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
14
|
+
* ))}
|
|
15
|
+
* </View>;
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* export const DisplayComponent = () => {
|
|
19
|
+
* return (
|
|
20
|
+
* <Suspense fallback={<div>Loading content...</div>}>
|
|
21
|
+
* <ContentComponent />
|
|
22
|
+
* </Suspense>
|
|
23
|
+
* );
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
export const useSuspenseQuery = (query, parameters = [], options = {}) => {
|
|
27
|
+
const powerSync = usePowerSync();
|
|
28
|
+
if (!powerSync) {
|
|
29
|
+
throw new Error('PowerSync not configured.');
|
|
30
|
+
}
|
|
31
|
+
let parsedQuery;
|
|
32
|
+
try {
|
|
33
|
+
parsedQuery = parseQuery(query, parameters);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
throw new Error('Failed to parse query: ' + error.message);
|
|
37
|
+
}
|
|
38
|
+
const key = generateQueryKey(parsedQuery.sqlStatement, parsedQuery.parameters, options);
|
|
39
|
+
// When the component is suspended, all state is discarded. We don't get
|
|
40
|
+
// any notification of that. So checkoutQuery reserves a temporary hold
|
|
41
|
+
// on the query.
|
|
42
|
+
// Once the component "commits", we exchange that for a permanent hold.
|
|
43
|
+
const store = getQueryStore(powerSync);
|
|
44
|
+
const q = store.getQuery(key, { rawQuery: query, sqlStatement: parsedQuery.sqlStatement, queryParameters: parsedQuery.parameters }, options);
|
|
45
|
+
const addedHoldTo = React.useRef(undefined);
|
|
46
|
+
const releaseTemporaryHold = React.useRef(undefined);
|
|
47
|
+
if (addedHoldTo.current !== q) {
|
|
48
|
+
releaseTemporaryHold.current?.();
|
|
49
|
+
releaseTemporaryHold.current = q.addTemporaryHold();
|
|
50
|
+
addedHoldTo.current = q;
|
|
51
|
+
}
|
|
52
|
+
const [_counter, setUpdateCounter] = React.useState(0);
|
|
53
|
+
React.useEffect(() => {
|
|
54
|
+
const dispose = q.registerListener({
|
|
55
|
+
onUpdate: () => {
|
|
56
|
+
setUpdateCounter((counter) => {
|
|
57
|
+
return counter + 1;
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
releaseTemporaryHold.current?.();
|
|
62
|
+
releaseTemporaryHold.current = undefined;
|
|
63
|
+
return dispose;
|
|
64
|
+
}, []);
|
|
65
|
+
if (q.currentError != null) {
|
|
66
|
+
throw q.currentError;
|
|
67
|
+
}
|
|
68
|
+
else if (q.currentData != null) {
|
|
69
|
+
return { data: q.currentData, refresh: () => q.fetchData() };
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
throw q.readyPromise;
|
|
73
|
+
}
|
|
74
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery';
|
|
2
|
-
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus';
|
|
3
|
-
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery';
|
|
4
1
|
export * from './hooks/PowerSyncContext';
|
|
5
|
-
export {
|
|
6
|
-
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery';
|
|
7
|
-
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription';
|
|
2
|
+
export { usePowerSyncQuery } from './hooks/usePowerSyncQuery';
|
|
8
3
|
export { useStatus } from './hooks/useStatus';
|
|
9
|
-
export { useQuery } from './hooks/
|
|
10
|
-
export {
|
|
11
|
-
export {
|
|
4
|
+
export { useQuery } from './hooks/useQuery';
|
|
5
|
+
export { useSuspenseQuery } from './hooks/useSuspenseQuery';
|
|
6
|
+
export { usePowerSyncWatchedQuery } from './hooks/usePowerSyncWatchedQuery';
|
|
7
|
+
export { usePowerSyncStatus } from './hooks/usePowerSyncStatus';
|
package/lib/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery';
|
|
2
|
-
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus';
|
|
3
|
-
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery';
|
|
4
1
|
export * from './hooks/PowerSyncContext';
|
|
5
|
-
export {
|
|
6
|
-
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription';
|
|
2
|
+
export { usePowerSyncQuery } from './hooks/usePowerSyncQuery';
|
|
7
3
|
export { useStatus } from './hooks/useStatus';
|
|
8
|
-
export { useQuery } from './hooks/
|
|
9
|
-
export {
|
|
4
|
+
export { useQuery } from './hooks/useQuery';
|
|
5
|
+
export { useSuspenseQuery } from './hooks/useSuspenseQuery';
|
|
6
|
+
export { usePowerSyncWatchedQuery } from './hooks/usePowerSyncWatchedQuery';
|
|
7
|
+
export { usePowerSyncStatus } from './hooks/usePowerSyncStatus';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/react",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20250728083821",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -22,19 +22,16 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://docs.powersync.com",
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@powersync/common": "0.0.0-dev-
|
|
25
|
+
"@powersync/common": "0.0.0-dev-20250728083821",
|
|
26
26
|
"react": "*"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@testing-library/react": "^15.0.2",
|
|
30
30
|
"@types/react": "^18.3.1",
|
|
31
|
-
"chart.js": "^4.5.0",
|
|
32
31
|
"jsdom": "^24.0.0",
|
|
33
32
|
"react": "18.3.1",
|
|
34
|
-
"react-dom": "18.3.1",
|
|
35
33
|
"react-error-boundary": "^4.1.0",
|
|
36
|
-
"@powersync/common": "0.0.0-dev-
|
|
37
|
-
"@powersync/web": "0.0.0-dev-20250722092404"
|
|
34
|
+
"@powersync/common": "0.0.0-dev-20250728083821"
|
|
38
35
|
},
|
|
39
36
|
"scripts": {
|
|
40
37
|
"build": "tsc -b",
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { WatchedQuery } from '@powersync/common';
|
|
2
|
-
/**
|
|
3
|
-
* The store will dispose this query if it has no subscribers attached to it.
|
|
4
|
-
* The suspense promise adds a subscriber to the query, but the promise could resolve
|
|
5
|
-
* before this component is committed. The promise will release it's listener once the query is no longer loading.
|
|
6
|
-
* This temporary hold is used to ensure that the query is not disposed in the interim.
|
|
7
|
-
* Creates a subscription for state change which creates a temporary hold on the query
|
|
8
|
-
* @returns a function to release the hold
|
|
9
|
-
*/
|
|
10
|
-
export declare const useTemporaryHold: (watchedQuery?: WatchedQuery<unknown>) => {
|
|
11
|
-
releaseHold: () => void;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
15
|
-
* This creates a promise which registers a listener on the watched query.
|
|
16
|
-
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
17
|
-
* while the component is suspended.
|
|
18
|
-
*/
|
|
19
|
-
export declare const createSuspendingPromise: (query: WatchedQuery<unknown>) => Promise<void>;
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
/**
|
|
3
|
-
* The store will dispose this query if it has no subscribers attached to it.
|
|
4
|
-
* The suspense promise adds a subscriber to the query, but the promise could resolve
|
|
5
|
-
* before this component is committed. The promise will release it's listener once the query is no longer loading.
|
|
6
|
-
* This temporary hold is used to ensure that the query is not disposed in the interim.
|
|
7
|
-
* Creates a subscription for state change which creates a temporary hold on the query
|
|
8
|
-
* @returns a function to release the hold
|
|
9
|
-
*/
|
|
10
|
-
export const useTemporaryHold = (watchedQuery) => {
|
|
11
|
-
const releaseTemporaryHold = React.useRef(undefined);
|
|
12
|
-
const addedHoldTo = React.useRef(undefined);
|
|
13
|
-
if (addedHoldTo.current !== watchedQuery) {
|
|
14
|
-
releaseTemporaryHold.current?.();
|
|
15
|
-
addedHoldTo.current = watchedQuery;
|
|
16
|
-
if (!watchedQuery || !watchedQuery.state.isLoading) {
|
|
17
|
-
// No query to hold or no reason to hold, return a no-op
|
|
18
|
-
return {
|
|
19
|
-
releaseHold: () => { }
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
const disposeSubscription = watchedQuery.registerListener({
|
|
23
|
-
onStateChange: (state) => { }
|
|
24
|
-
});
|
|
25
|
-
let timeout;
|
|
26
|
-
const disposeClosedListener = watchedQuery.registerListener({
|
|
27
|
-
closed: () => {
|
|
28
|
-
if (timeout) {
|
|
29
|
-
clearTimeout(timeout);
|
|
30
|
-
}
|
|
31
|
-
disposeClosedListener();
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
const releaseHold = () => {
|
|
35
|
-
disposeSubscription();
|
|
36
|
-
disposeClosedListener();
|
|
37
|
-
};
|
|
38
|
-
releaseTemporaryHold.current = releaseHold;
|
|
39
|
-
const timeoutPollMs = 5_000;
|
|
40
|
-
const checkHold = () => {
|
|
41
|
-
if (watchedQuery.closed || !watchedQuery.state.isLoading || watchedQuery.state.error) {
|
|
42
|
-
// No need to keep a temporary hold on this query
|
|
43
|
-
releaseHold();
|
|
44
|
-
}
|
|
45
|
-
else {
|
|
46
|
-
// Need to keep the hold, check again after timeout
|
|
47
|
-
setTimeout(checkHold, timeoutPollMs);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
// Set a timeout to conditionally remove the temporary hold
|
|
51
|
-
setTimeout(checkHold, timeoutPollMs);
|
|
52
|
-
}
|
|
53
|
-
return {
|
|
54
|
-
releaseHold: releaseTemporaryHold.current
|
|
55
|
-
};
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
59
|
-
* This creates a promise which registers a listener on the watched query.
|
|
60
|
-
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
61
|
-
* while the component is suspended.
|
|
62
|
-
*/
|
|
63
|
-
export const createSuspendingPromise = (query) => {
|
|
64
|
-
return new Promise((resolve) => {
|
|
65
|
-
// The listener here will dispose itself once the loading is done
|
|
66
|
-
// This decreases the number of listeners on the query
|
|
67
|
-
// even if the component is unmounted
|
|
68
|
-
const dispose = query.registerListener({
|
|
69
|
-
onStateChange: (state) => {
|
|
70
|
-
// Returns to the hook if loading is completed or if loading resulted in an error
|
|
71
|
-
if (!state.isLoading || state.error) {
|
|
72
|
-
resolve();
|
|
73
|
-
dispose();
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
});
|
|
78
|
-
};
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { CompilableQuery } from '@powersync/common';
|
|
2
|
-
import { AdditionalOptions } from '../watched/watch-types';
|
|
3
|
-
import { SuspenseQueryResult } from './SuspenseQueryResult';
|
|
4
|
-
/**
|
|
5
|
-
* Use a query which is not watched, but suspends until the initial result has loaded.
|
|
6
|
-
* Internally this uses a WatchedQuery during suspense for state management. The watched
|
|
7
|
-
* query is potentially disposed, if there are no subscribers attached to it, after the initial load.
|
|
8
|
-
* The query can be refreshed by calling the `refresh` function after initial load.
|
|
9
|
-
*/
|
|
10
|
-
export declare const useSingleSuspenseQuery: <T = any>(query: string | CompilableQuery<T>, parameters?: any[], options?: AdditionalOptions) => SuspenseQueryResult<T>;
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { generateQueryKey, getQueryStore } from '../../QueryStore';
|
|
3
|
-
import { usePowerSync } from '../PowerSyncContext';
|
|
4
|
-
import { constructCompatibleQuery } from '../watched/watch-utils';
|
|
5
|
-
import { createSuspendingPromise, useTemporaryHold } from './suspense-utils';
|
|
6
|
-
/**
|
|
7
|
-
* Use a query which is not watched, but suspends until the initial result has loaded.
|
|
8
|
-
* Internally this uses a WatchedQuery during suspense for state management. The watched
|
|
9
|
-
* query is potentially disposed, if there are no subscribers attached to it, after the initial load.
|
|
10
|
-
* The query can be refreshed by calling the `refresh` function after initial load.
|
|
11
|
-
*/
|
|
12
|
-
export const useSingleSuspenseQuery = (query, parameters = [], options = {}) => {
|
|
13
|
-
const powerSync = usePowerSync();
|
|
14
|
-
if (!powerSync) {
|
|
15
|
-
throw new Error('PowerSync not configured.');
|
|
16
|
-
}
|
|
17
|
-
// Manually track data for single queries
|
|
18
|
-
const [data, setData] = React.useState(null);
|
|
19
|
-
const [error, setError] = React.useState(null);
|
|
20
|
-
// Note, we don't need to check if the query changed since we fetch the WatchedQuery
|
|
21
|
-
// from the store given these query params
|
|
22
|
-
const { parsedQuery } = constructCompatibleQuery(query, parameters, options);
|
|
23
|
-
const { sql: parsedSql, parameters: parsedParameters } = parsedQuery.compile();
|
|
24
|
-
const key = generateQueryKey(parsedSql, parsedParameters, options);
|
|
25
|
-
const store = getQueryStore(powerSync);
|
|
26
|
-
// Only use a temporary watched query if we don't have data yet.
|
|
27
|
-
const watchedQuery = data ? null : store.getQuery(key, parsedQuery, options);
|
|
28
|
-
const { releaseHold } = useTemporaryHold(watchedQuery);
|
|
29
|
-
React.useEffect(() => {
|
|
30
|
-
// Set the initial yielded data
|
|
31
|
-
// it should be available once we commit the component
|
|
32
|
-
if (watchedQuery?.state.error) {
|
|
33
|
-
setError(watchedQuery.state.error);
|
|
34
|
-
}
|
|
35
|
-
else if (watchedQuery?.state.isLoading === false) {
|
|
36
|
-
setData(watchedQuery.state.data);
|
|
37
|
-
setError(null);
|
|
38
|
-
}
|
|
39
|
-
if (!watchedQuery?.state.isLoading) {
|
|
40
|
-
releaseHold();
|
|
41
|
-
}
|
|
42
|
-
}, []);
|
|
43
|
-
if (error != null) {
|
|
44
|
-
// Report errors - this is caught by an error boundary
|
|
45
|
-
throw error;
|
|
46
|
-
}
|
|
47
|
-
else if (data || watchedQuery?.state.isLoading === false) {
|
|
48
|
-
// Happy path data return
|
|
49
|
-
return {
|
|
50
|
-
data: data ?? watchedQuery?.state.data ?? [],
|
|
51
|
-
refresh: async (signal) => {
|
|
52
|
-
try {
|
|
53
|
-
const compiledQuery = parsedQuery.compile();
|
|
54
|
-
const result = await parsedQuery.execute({
|
|
55
|
-
sql: compiledQuery.sql,
|
|
56
|
-
parameters: [...compiledQuery.parameters],
|
|
57
|
-
db: powerSync
|
|
58
|
-
});
|
|
59
|
-
if (signal.aborted) {
|
|
60
|
-
return; // Abort if the signal is already aborted
|
|
61
|
-
}
|
|
62
|
-
setData(result);
|
|
63
|
-
setError(null);
|
|
64
|
-
}
|
|
65
|
-
catch (e) {
|
|
66
|
-
setError(e);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
72
|
-
// Notify suspense is required
|
|
73
|
-
throw createSuspendingPromise(watchedQuery);
|
|
74
|
-
}
|
|
75
|
-
};
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { CompilableQuery } from '@powersync/common';
|
|
2
|
-
import { AdditionalOptions, DifferentialHookOptions } from '../watched/watch-types';
|
|
3
|
-
import { ReadonlySuspenseQueryResult, SuspenseQueryResult } from './SuspenseQueryResult';
|
|
4
|
-
/**
|
|
5
|
-
* A hook to access the results of a watched query that suspends until the initial result has loaded.
|
|
6
|
-
* @example
|
|
7
|
-
* export const ContentComponent = () => {
|
|
8
|
-
* // The lists array here will be a new Array reference whenever a change to the
|
|
9
|
-
* // lists table is made.
|
|
10
|
-
* const { data: lists } = useSuspenseQuery('SELECT * from lists');
|
|
11
|
-
*
|
|
12
|
-
* return <View>
|
|
13
|
-
* {lists.map((l) => (
|
|
14
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
15
|
-
* ))}
|
|
16
|
-
* </View>;
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* export const DisplayComponent = () => {
|
|
20
|
-
* return (
|
|
21
|
-
* <Suspense fallback={<div>Loading content...</div>}>
|
|
22
|
-
* <ContentComponent />
|
|
23
|
-
* </Suspense>
|
|
24
|
-
* );
|
|
25
|
-
* }
|
|
26
|
-
*
|
|
27
|
-
* export const DiffContentComponent = () => {
|
|
28
|
-
* // A differential query will emit results when a change to the result set occurs.
|
|
29
|
-
* // The internal array object references are maintained for unchanged rows.
|
|
30
|
-
* // The returned lists array is read only when a `comparator` is provided.
|
|
31
|
-
* const { data: lists } = useSuspenseQuery('SELECT * from lists', [], {
|
|
32
|
-
* comparator: {
|
|
33
|
-
* keyBy: (item) => item.id,
|
|
34
|
-
* compareBy: (item) => JSON.stringify(item)
|
|
35
|
-
* }
|
|
36
|
-
* });
|
|
37
|
-
* return <View>
|
|
38
|
-
* {lists.map((l) => (
|
|
39
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
40
|
-
* ))}
|
|
41
|
-
* </View>;
|
|
42
|
-
* }
|
|
43
|
-
*
|
|
44
|
-
* export const DisplayComponent = () => {
|
|
45
|
-
* return (
|
|
46
|
-
* <Suspense fallback={<div>Loading content...</div>}>
|
|
47
|
-
* <ContentComponent />
|
|
48
|
-
* </Suspense>
|
|
49
|
-
* );
|
|
50
|
-
* }
|
|
51
|
-
*/
|
|
52
|
-
export declare function useSuspenseQuery<RowType = any>(query: string | CompilableQuery<RowType>, parameters?: any[], options?: AdditionalOptions): SuspenseQueryResult<RowType>;
|
|
53
|
-
export declare function useSuspenseQuery<RowType = any>(query: string | CompilableQuery<RowType>, paramerers?: any[], options?: DifferentialHookOptions<RowType>): ReadonlySuspenseQueryResult<RowType>;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { useSingleSuspenseQuery } from './useSingleSuspenseQuery';
|
|
2
|
-
import { useWatchedSuspenseQuery } from './useWatchedSuspenseQuery';
|
|
3
|
-
export function useSuspenseQuery(query, parameters = [], options = {}) {
|
|
4
|
-
switch (options?.runQueryOnce) {
|
|
5
|
-
case true:
|
|
6
|
-
return useSingleSuspenseQuery(query, parameters, options);
|
|
7
|
-
default:
|
|
8
|
-
return useWatchedSuspenseQuery(query, parameters, options);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { WatchedQuery } from '@powersync/common';
|
|
2
|
-
/**
|
|
3
|
-
* A hook to access and subscribe to the results of an existing {@link WatchedQuery}.
|
|
4
|
-
* @example
|
|
5
|
-
* export const ContentComponent = () => {
|
|
6
|
-
* const { data: lists } = useWatchedQuerySuspenseSubscription(listsQuery);
|
|
7
|
-
*
|
|
8
|
-
* return <View>
|
|
9
|
-
* {lists.map((l) => (
|
|
10
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
11
|
-
* ))}
|
|
12
|
-
* </View>;
|
|
13
|
-
* }
|
|
14
|
-
*
|
|
15
|
-
* export const DisplayComponent = () => {
|
|
16
|
-
* return (
|
|
17
|
-
* <Suspense fallback={<div>Loading content...</div>}>
|
|
18
|
-
* <ContentComponent />
|
|
19
|
-
* </Suspense>
|
|
20
|
-
* );
|
|
21
|
-
* }
|
|
22
|
-
*/
|
|
23
|
-
export declare const useWatchedQuerySuspenseSubscription: <ResultType = unknown, Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>>(query: Query) => Query["state"];
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { createSuspendingPromise, useTemporaryHold } from './suspense-utils';
|
|
3
|
-
/**
|
|
4
|
-
* A hook to access and subscribe to the results of an existing {@link WatchedQuery}.
|
|
5
|
-
* @example
|
|
6
|
-
* export const ContentComponent = () => {
|
|
7
|
-
* const { data: lists } = useWatchedQuerySuspenseSubscription(listsQuery);
|
|
8
|
-
*
|
|
9
|
-
* return <View>
|
|
10
|
-
* {lists.map((l) => (
|
|
11
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
12
|
-
* ))}
|
|
13
|
-
* </View>;
|
|
14
|
-
* }
|
|
15
|
-
*
|
|
16
|
-
* export const DisplayComponent = () => {
|
|
17
|
-
* return (
|
|
18
|
-
* <Suspense fallback={<div>Loading content...</div>}>
|
|
19
|
-
* <ContentComponent />
|
|
20
|
-
* </Suspense>
|
|
21
|
-
* );
|
|
22
|
-
* }
|
|
23
|
-
*/
|
|
24
|
-
export const useWatchedQuerySuspenseSubscription = (query) => {
|
|
25
|
-
const { releaseHold } = useTemporaryHold(query);
|
|
26
|
-
// Force update state function
|
|
27
|
-
const [, setUpdateCounter] = React.useState(0);
|
|
28
|
-
React.useEffect(() => {
|
|
29
|
-
// This runs when the component came out of suspense
|
|
30
|
-
// This add a permanent hold since a listener has been added to the query
|
|
31
|
-
const dispose = query.registerListener({
|
|
32
|
-
onStateChange() {
|
|
33
|
-
// Trigger rerender
|
|
34
|
-
setUpdateCounter((prev) => prev + 1);
|
|
35
|
-
}
|
|
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
|
-
return dispose;
|
|
43
|
-
}, []);
|
|
44
|
-
if (query.state.error != null) {
|
|
45
|
-
// Report errors - this is caught by an error boundary
|
|
46
|
-
throw query.state.error;
|
|
47
|
-
}
|
|
48
|
-
else if (!query.state.isLoading) {
|
|
49
|
-
// Happy path data return
|
|
50
|
-
return query.state;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
// Notify suspense is required
|
|
54
|
-
throw createSuspendingPromise(query);
|
|
55
|
-
}
|
|
56
|
-
};
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { CompilableQuery } from '@powersync/common';
|
|
2
|
-
import { AdditionalOptions } from '../watched/watch-types';
|
|
3
|
-
/**
|
|
4
|
-
* @internal This is not exported in the index.ts
|
|
5
|
-
*/
|
|
6
|
-
export declare const useWatchedSuspenseQuery: <T = any>(query: string | CompilableQuery<T>, parameters?: any[], options?: AdditionalOptions) => import("@powersync/common").WatchedQueryState<readonly Readonly<T>[]>;
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { generateQueryKey, getQueryStore } from '../../QueryStore';
|
|
2
|
-
import { usePowerSync } from '../PowerSyncContext';
|
|
3
|
-
import { constructCompatibleQuery } from '../watched/watch-utils';
|
|
4
|
-
import { useWatchedQuerySuspenseSubscription } from './useWatchedQuerySuspenseSubscription';
|
|
5
|
-
/**
|
|
6
|
-
* @internal This is not exported in the index.ts
|
|
7
|
-
*/
|
|
8
|
-
export const useWatchedSuspenseQuery = (query, parameters = [], options = {}) => {
|
|
9
|
-
const powerSync = usePowerSync();
|
|
10
|
-
if (!powerSync) {
|
|
11
|
-
throw new Error('PowerSync not configured.');
|
|
12
|
-
}
|
|
13
|
-
// Note, we don't need to check if the query changed since we fetch the WatchedQuery
|
|
14
|
-
// from the store given these query params
|
|
15
|
-
const { parsedQuery } = constructCompatibleQuery(query, parameters, options);
|
|
16
|
-
const { sql: parsedSql, parameters: parsedParameters } = parsedQuery.compile();
|
|
17
|
-
const key = generateQueryKey(parsedSql, parsedParameters, options);
|
|
18
|
-
// When the component is suspended, all state is discarded. We don't get
|
|
19
|
-
// any notification of that. So checkoutQuery reserves a temporary hold
|
|
20
|
-
// on the query.
|
|
21
|
-
// Once the component "commits", we exchange that for a permanent hold.
|
|
22
|
-
const store = getQueryStore(powerSync);
|
|
23
|
-
const watchedQuery = store.getQuery(key, parsedQuery, options);
|
|
24
|
-
return useWatchedQuerySuspenseSubscription(watchedQuery);
|
|
25
|
-
};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { type CompilableQuery } from '@powersync/common';
|
|
2
|
-
import { AdditionalOptions, DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types';
|
|
3
|
-
/**
|
|
4
|
-
* A hook to access the results of a watched query.
|
|
5
|
-
* @example
|
|
6
|
-
*
|
|
7
|
-
* export const Component = () => {
|
|
8
|
-
* // The lists array here will be a new Array reference whenever a change to the
|
|
9
|
-
* // lists table is made.
|
|
10
|
-
* const { data: lists } = useQuery('SELECT * from lists');
|
|
11
|
-
*
|
|
12
|
-
* return <View>
|
|
13
|
-
* {lists.map((l) => (
|
|
14
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
15
|
-
* ))}
|
|
16
|
-
* </View>
|
|
17
|
-
* }
|
|
18
|
-
*
|
|
19
|
-
* export const DiffComponent = () => {
|
|
20
|
-
* // A differential query will emit results when a change to the result set occurs.
|
|
21
|
-
* // The internal array object references are maintained for unchanged rows.
|
|
22
|
-
* // The returned lists array is read only when a `comparator` is provided.
|
|
23
|
-
* const { data: lists } = useQuery('SELECT * from lists', [], {
|
|
24
|
-
* comparator: {
|
|
25
|
-
* keyBy: (item) => item.id,
|
|
26
|
-
* compareBy: (item) => JSON.stringify(item)
|
|
27
|
-
* }
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* return <View>
|
|
31
|
-
* {lists.map((l) => (
|
|
32
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
33
|
-
* ))}
|
|
34
|
-
* </View>
|
|
35
|
-
* }
|
|
36
|
-
*/
|
|
37
|
-
export declare function useQuery<RowType = any>(query: string | CompilableQuery<RowType>, parameters?: any[], options?: AdditionalOptions): QueryResult<RowType>;
|
|
38
|
-
export declare function useQuery<RowType = any>(query: string | CompilableQuery<RowType>, parameters?: any[], options?: DifferentialHookOptions<RowType>): ReadonlyQueryResult<RowType>;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { usePowerSync } from '../PowerSyncContext';
|
|
2
|
-
import { useSingleQuery } from './useSingleQuery';
|
|
3
|
-
import { useWatchedQuery } from './useWatchedQuery';
|
|
4
|
-
import { constructCompatibleQuery } from './watch-utils';
|
|
5
|
-
export function useQuery(query, parameters = [], options = {}) {
|
|
6
|
-
const powerSync = usePowerSync();
|
|
7
|
-
if (!powerSync) {
|
|
8
|
-
return { isLoading: false, isFetching: false, data: [], error: new Error('PowerSync not configured.') };
|
|
9
|
-
}
|
|
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
|
-
comparator: options.comparator
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
export const useSingleQuery = (options) => {
|
|
3
|
-
const { query, powerSync, queryChanged } = options;
|
|
4
|
-
const [output, setOutputState] = React.useState({
|
|
5
|
-
isLoading: true,
|
|
6
|
-
isFetching: true,
|
|
7
|
-
data: [],
|
|
8
|
-
error: undefined
|
|
9
|
-
});
|
|
10
|
-
const runQuery = React.useCallback(async (signal) => {
|
|
11
|
-
setOutputState((prev) => ({ ...prev, isLoading: true, isFetching: true, error: undefined }));
|
|
12
|
-
try {
|
|
13
|
-
const compiledQuery = query.compile();
|
|
14
|
-
const result = await query.execute({
|
|
15
|
-
sql: compiledQuery.sql,
|
|
16
|
-
parameters: [...compiledQuery.parameters],
|
|
17
|
-
db: powerSync
|
|
18
|
-
});
|
|
19
|
-
if (signal.aborted) {
|
|
20
|
-
return;
|
|
21
|
-
}
|
|
22
|
-
setOutputState((prev) => ({
|
|
23
|
-
...prev,
|
|
24
|
-
isLoading: false,
|
|
25
|
-
isFetching: false,
|
|
26
|
-
data: result,
|
|
27
|
-
error: undefined
|
|
28
|
-
}));
|
|
29
|
-
}
|
|
30
|
-
catch (error) {
|
|
31
|
-
setOutputState((prev) => ({
|
|
32
|
-
...prev,
|
|
33
|
-
isLoading: false,
|
|
34
|
-
isFetching: false,
|
|
35
|
-
data: [],
|
|
36
|
-
error
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
}, [queryChanged, query]);
|
|
40
|
-
// Trigger initial query execution
|
|
41
|
-
React.useEffect(() => {
|
|
42
|
-
const abortController = new AbortController();
|
|
43
|
-
runQuery(abortController.signal);
|
|
44
|
-
return () => {
|
|
45
|
-
abortController.abort();
|
|
46
|
-
};
|
|
47
|
-
}, [powerSync, queryChanged]);
|
|
48
|
-
return {
|
|
49
|
-
...output,
|
|
50
|
-
refresh: runQuery
|
|
51
|
-
};
|
|
52
|
-
};
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types';
|
|
2
|
-
import { InternalHookOptions } from './watch-utils';
|
|
3
|
-
/**
|
|
4
|
-
* @internal This is not exported from the index.ts
|
|
5
|
-
*
|
|
6
|
-
* When a differential query is used the return type is readonly. This is required
|
|
7
|
-
* since the implementation requires a stable ref.
|
|
8
|
-
* For legacy compatibility we allow mutating when a standard query is used. Mutations should
|
|
9
|
-
* not affect the internal implementation in this case.
|
|
10
|
-
*/
|
|
11
|
-
export declare const useWatchedQuery: <RowType = unknown>(options: InternalHookOptions<RowType[]> & {
|
|
12
|
-
options: DifferentialHookOptions<RowType>;
|
|
13
|
-
}) => QueryResult<RowType> | ReadonlyQueryResult<RowType>;
|