@powersync/react 0.0.0-dev-20260827080125 → 0.0.0-dev-20261015114518
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 +15 -0
- package/lib/hooks/PowerSyncContext.d.ts +4 -2
- package/lib/hooks/PowerSyncContext.js +7 -1
- package/lib/hooks/PowerSyncContext.js.map +1 -1
- package/lib/hooks/streams.d.ts +4 -3
- package/lib/hooks/streams.js +11 -10
- package/lib/hooks/streams.js.map +1 -1
- package/lib/hooks/suspense/SuspenseQueryCache.d.ts +46 -0
- package/lib/hooks/suspense/SuspenseQueryCache.js +231 -0
- package/lib/hooks/suspense/SuspenseQueryCache.js.map +1 -0
- package/lib/hooks/suspense/useSuspenseQuery.d.ts +3 -3
- package/lib/hooks/suspense/useSuspenseQuery.js +28 -7
- package/lib/hooks/suspense/useSuspenseQuery.js.map +1 -1
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +60 -24
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js.map +1 -1
- package/lib/hooks/useStatus.d.ts +7 -1
- package/lib/hooks/useStatus.js +11 -6
- package/lib/hooks/useStatus.js.map +1 -1
- package/lib/hooks/watched/QueryRunner.d.ts +90 -0
- package/lib/hooks/watched/QueryRunner.js +366 -0
- package/lib/hooks/watched/QueryRunner.js.map +1 -0
- package/lib/hooks/watched/useQuery.js +14 -40
- package/lib/hooks/watched/useQuery.js.map +1 -1
- package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +1 -1
- package/lib/hooks/watched/useWatchedQuerySubscription.js.map +1 -1
- package/lib/hooks/watched/watch-types.d.ts +39 -3
- package/lib/hooks/watched/watch-utils.d.ts +3 -15
- package/lib/hooks/watched/watch-utils.js +5 -31
- package/lib/hooks/watched/watch-utils.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/package.json +11 -9
- package/src/hooks/PowerSyncContext.ts +9 -3
- package/src/hooks/streams.ts +14 -20
- package/src/hooks/suspense/SuspenseQueryCache.ts +253 -0
- package/src/hooks/suspense/useSuspenseQuery.ts +40 -13
- package/src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts +65 -25
- package/src/hooks/useStatus.ts +21 -7
- package/src/hooks/watched/QueryRunner.ts +484 -0
- package/src/hooks/watched/useQuery.ts +15 -41
- package/src/hooks/watched/useWatchedQuerySubscription.ts +2 -2
- package/src/hooks/watched/watch-types.ts +42 -3
- package/src/hooks/watched/watch-utils.ts +8 -54
- package/src/index.ts +8 -2
- package/lib/QueryStore.d.ts +0 -10
- package/lib/QueryStore.js +0 -63
- package/lib/QueryStore.js.map +0 -1
- package/lib/hooks/suspense/suspense-utils.d.ts +0 -16
- package/lib/hooks/suspense/suspense-utils.js +0 -76
- package/lib/hooks/suspense/suspense-utils.js.map +0 -1
- package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +0 -10
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +0 -73
- package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +0 -1
- package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +0 -6
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +0 -26
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +0 -1
- package/lib/hooks/watched/useSingleQuery.d.ts +0 -6
- package/lib/hooks/watched/useSingleQuery.js +0 -59
- package/lib/hooks/watched/useSingleQuery.js.map +0 -1
- package/lib/hooks/watched/useWatchedQuery.d.ts +0 -13
- package/lib/hooks/watched/useWatchedQuery.js +0 -81
- package/lib/hooks/watched/useWatchedQuery.js.map +0 -1
- package/src/QueryStore.ts +0 -81
- package/src/hooks/suspense/suspense-utils.ts +0 -86
- package/src/hooks/suspense/useSingleSuspenseQuery.ts +0 -81
- package/src/hooks/suspense/useWatchedSuspenseQuery.ts +0 -36
- package/src/hooks/watched/useSingleQuery.ts +0 -67
- package/src/hooks/watched/useWatchedQuery.ts +0 -93
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { type CompilableQuery } from '@powersync/common';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
2
|
+
import { useContext, useEffect, useRef, useSyncExternalStore } from 'react';
|
|
3
|
+
import { PowerSyncContext } from '../PowerSyncContext.js';
|
|
4
|
+
import { useAllSyncStreamsHaveSynced } from '../streams.js';
|
|
5
|
+
import { QueryRunner, resolveConfig } from './QueryRunner.js';
|
|
5
6
|
import { AdditionalOptions, DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
6
7
|
import { constructCompatibleQuery } from './watch-utils.js';
|
|
7
|
-
import { useAllSyncStreamsHaveSynced } from '../streams.js';
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* A hook to access the results of a watched query.
|
|
@@ -57,43 +57,17 @@ export function useQuery<RowType = any>(
|
|
|
57
57
|
parameters: any[] = [],
|
|
58
58
|
options: AdditionalOptions & DifferentialHookOptions<RowType> = {}
|
|
59
59
|
) {
|
|
60
|
-
const powerSync =
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
..._loadingState,
|
|
64
|
-
isLoading: false,
|
|
65
|
-
error: new Error('PowerSync not configured.')
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
const { parsedQuery, queryChanged } = constructCompatibleQuery(query, parameters, options);
|
|
69
|
-
const streamsHaveSynced = useAllSyncStreamsHaveSynced(powerSync, options?.streams);
|
|
70
|
-
const runOnce = options?.runQueryOnce == true;
|
|
71
|
-
const single = useSingleQuery<RowType>({
|
|
72
|
-
query: parsedQuery,
|
|
73
|
-
powerSync,
|
|
74
|
-
queryChanged,
|
|
75
|
-
active: runOnce && streamsHaveSynced
|
|
76
|
-
});
|
|
77
|
-
const watched = useWatchedQuery<RowType>({
|
|
78
|
-
query: parsedQuery,
|
|
79
|
-
powerSync,
|
|
80
|
-
queryChanged,
|
|
81
|
-
options: {
|
|
82
|
-
reportFetching: options.reportFetching,
|
|
83
|
-
throttleMs: options.throttleMs,
|
|
84
|
-
// Maintains backwards compatibility with previous versions
|
|
85
|
-
// Differentiation is opt-in by default
|
|
86
|
-
// We emit new data for each table change by default.
|
|
87
|
-
rowComparator: options.rowComparator
|
|
88
|
-
},
|
|
89
|
-
active: !runOnce && streamsHaveSynced
|
|
90
|
-
});
|
|
60
|
+
const powerSync = useContext(PowerSyncContext);
|
|
61
|
+
const parsedQuery = constructCompatibleQuery(query, parameters);
|
|
62
|
+
const streamsHaveSynced = useAllSyncStreamsHaveSynced(null, options.streams);
|
|
91
63
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
64
|
+
const runnerRef = useRef<QueryRunner<RowType> | null>(null);
|
|
65
|
+
const runner = (runnerRef.current ??= new QueryRunner<RowType>());
|
|
66
|
+
const config = resolveConfig(powerSync, parsedQuery, options, streamsHaveSynced);
|
|
95
67
|
|
|
96
|
-
|
|
97
|
-
}
|
|
68
|
+
const snapshot = useSyncExternalStore(runner.subscribe, runner.getSnapshot, runner.getSnapshot);
|
|
98
69
|
|
|
99
|
-
|
|
70
|
+
// Apply the inputs once this render has been committed, so that discarded renders don't start running queries.
|
|
71
|
+
useEffect(() => runner.configure(config));
|
|
72
|
+
return runner.resultFor(config, snapshot);
|
|
73
|
+
}
|
|
@@ -21,7 +21,7 @@ export const useWatchedQuerySubscription = <
|
|
|
21
21
|
>(
|
|
22
22
|
query: Query
|
|
23
23
|
): Query['state'] => {
|
|
24
|
-
return useNullableWatchedQuerySubscription(query)
|
|
24
|
+
return useNullableWatchedQuerySubscription(query)!;
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
/**
|
|
@@ -31,7 +31,7 @@ export const useNullableWatchedQuerySubscription = <
|
|
|
31
31
|
ResultType = unknown,
|
|
32
32
|
Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>
|
|
33
33
|
>(
|
|
34
|
-
query:
|
|
34
|
+
query: WatchedQuery<ResultType> | null
|
|
35
35
|
): Query['state'] | undefined => {
|
|
36
36
|
const [output, setOutputState] = React.useState(query?.state);
|
|
37
37
|
|
|
@@ -7,9 +7,9 @@ export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
|
|
|
7
7
|
*
|
|
8
8
|
* When set, `useQuery` will subscribe to those streams (and automatically handle unsubscribing from them, too).
|
|
9
9
|
*
|
|
10
|
-
* If {@link QuerySyncStreamOptions} is set on a stream, `useQuery` will remain in a loading state until
|
|
11
|
-
* is marked as {@link SyncSubscriptionDescription.hasSynced}. This ensures the query is not missing rows
|
|
12
|
-
* been downloaded.
|
|
10
|
+
* If {@link QuerySyncStreamOptions.waitForStream} is set on a stream, `useQuery` will remain in a loading state until
|
|
11
|
+
* that stream is marked as {@link SyncSubscriptionDescription.hasSynced}. This ensures the query is not missing rows
|
|
12
|
+
* that haven't been downloaded.
|
|
13
13
|
* Note however that after an initial sync, the query will not block itself while new rows are downloading. Instead,
|
|
14
14
|
* consistent sync snapshots will be made available as they've been processed by PowerSync.
|
|
15
15
|
*/
|
|
@@ -56,6 +56,35 @@ export interface DifferentialHookOptions<RowType> extends HookWatchOptions {
|
|
|
56
56
|
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
type WithoutSyncStreams<T> = Omit<T, 'streams'> & {
|
|
60
|
+
/**
|
|
61
|
+
* @deprecated Sync streams are not supported by `useSuspenseQuery` and are ignored. Use `useQuery` or
|
|
62
|
+
* `useSyncStream` to subscribe to streams instead.
|
|
63
|
+
*/
|
|
64
|
+
streams?: QuerySyncStreamOptions[];
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Options for `useSuspenseQuery`.
|
|
69
|
+
*/
|
|
70
|
+
export type SuspenseQueryOptions = WithoutSyncStreams<AdditionalOptions>;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Options for `useSuspenseQuery` with a `rowComparator`.
|
|
74
|
+
*/
|
|
75
|
+
export type DifferentialSuspenseQueryOptions<RowType> = WithoutSyncStreams<DifferentialHookOptions<RowType>>;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Why a `useQuery` hook is currently fetching.
|
|
79
|
+
*
|
|
80
|
+
* - `'initial'`: the first load of this query, `isLoading` is true as well.
|
|
81
|
+
* - `refresh`: `QueryResult.refresh` was called.
|
|
82
|
+
* - `'tables-changed'`: a table the query depends on changed and the same query is re-running. `data` is still current.
|
|
83
|
+
* - `'settings-changed'`: the SQL, parameters or options changed. `data` still holds the previous query's rows until the
|
|
84
|
+
* updated query has produced its first result.
|
|
85
|
+
*/
|
|
86
|
+
export type FetchReason = 'initial' | 'refresh' | 'tables-changed' | 'settings-changed';
|
|
87
|
+
|
|
59
88
|
export type ReadonlyQueryResult<RowType> = {
|
|
60
89
|
readonly data: ReadonlyArray<Readonly<RowType>>;
|
|
61
90
|
/**
|
|
@@ -67,6 +96,11 @@ export type ReadonlyQueryResult<RowType> = {
|
|
|
67
96
|
*/
|
|
68
97
|
readonly isFetching: boolean;
|
|
69
98
|
readonly error: Error | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* Why the query is running while `isFetching` is true, `undefined` otherwise. `'settings-changed'` means the
|
|
101
|
+
* SQL, parameters or options changed and `data` still holds the previous query's rows until the new result arrives.
|
|
102
|
+
*/
|
|
103
|
+
readonly fetchReason?: FetchReason;
|
|
70
104
|
/**
|
|
71
105
|
* Function used to run the query again.
|
|
72
106
|
*/
|
|
@@ -84,6 +118,11 @@ export type QueryResult<RowType> = {
|
|
|
84
118
|
*/
|
|
85
119
|
isFetching: boolean;
|
|
86
120
|
error: Error | undefined;
|
|
121
|
+
/**
|
|
122
|
+
* Why the query is running while `isFetching` is true, `undefined` otherwise. `'settings-changed'` means the
|
|
123
|
+
* SQL, parameters or options changed and `data` still holds the previous query's rows until the new result arrives.
|
|
124
|
+
*/
|
|
125
|
+
fetchReason?: FetchReason;
|
|
87
126
|
/**
|
|
88
127
|
* Function used to run the query again.
|
|
89
128
|
*/
|
|
@@ -1,54 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CompilableQuery, WatchCompatibleQuery } from '@powersync/common';
|
|
2
2
|
import React from 'react';
|
|
3
|
-
import {
|
|
4
|
-
import { AdditionalOptions } from './watch-types.js';
|
|
3
|
+
import { usePowerSyncOrNull } from '../PowerSyncContext.js';
|
|
5
4
|
|
|
6
|
-
export
|
|
7
|
-
query: WatchCompatibleQuery<DataType>;
|
|
8
|
-
powerSync: AbstractPowerSyncDatabase;
|
|
9
|
-
queryChanged: boolean;
|
|
10
|
-
active: boolean;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
interface WatchCompatibleQueryWithParams<T> extends WatchCompatibleQuery<T> {
|
|
5
|
+
export interface WatchCompatibleQueryWithParams<T> extends WatchCompatibleQuery<T> {
|
|
14
6
|
stringifiedParameters?: string;
|
|
15
7
|
}
|
|
16
8
|
|
|
17
|
-
export const
|
|
18
|
-
|
|
19
|
-
try {
|
|
20
|
-
_compiled = query.compile();
|
|
21
|
-
} catch (error) {
|
|
22
|
-
return false; // If compilation fails, we assume the query has changed
|
|
23
|
-
}
|
|
24
|
-
const compiled = _compiled!;
|
|
25
|
-
|
|
26
|
-
const stringifiedParams = query.stringifiedParameters ?? JSON.stringify(compiled.parameters);
|
|
27
|
-
const stringifiedOptions = JSON.stringify(options);
|
|
28
|
-
|
|
29
|
-
const previousQueryRef = React.useRef({ sqlStatement: compiled.sql, stringifiedParams, stringifiedOptions });
|
|
30
|
-
|
|
31
|
-
if (
|
|
32
|
-
previousQueryRef.current.sqlStatement !== compiled.sql ||
|
|
33
|
-
previousQueryRef.current.stringifiedParams != stringifiedParams ||
|
|
34
|
-
previousQueryRef.current.stringifiedOptions != stringifiedOptions
|
|
35
|
-
) {
|
|
36
|
-
previousQueryRef.current.sqlStatement = compiled.sql;
|
|
37
|
-
previousQueryRef.current.stringifiedParams = stringifiedParams;
|
|
38
|
-
previousQueryRef.current.stringifiedOptions = stringifiedOptions;
|
|
39
|
-
|
|
40
|
-
return true;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return false;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export const constructCompatibleQuery = <RowType>(
|
|
47
|
-
query: string | CompilableQuery<RowType>,
|
|
48
|
-
parameters: any[] = [],
|
|
49
|
-
options: AdditionalOptions
|
|
50
|
-
) => {
|
|
51
|
-
const powerSync = usePowerSync()!;
|
|
9
|
+
export const constructCompatibleQuery = <RowType>(query: string | CompilableQuery<RowType>, parameters: any[] = []) => {
|
|
10
|
+
const powerSync = usePowerSyncOrNull();
|
|
52
11
|
const stringifiedParameters = React.useMemo(() => JSON.stringify(parameters), [parameters]);
|
|
53
12
|
|
|
54
13
|
const parsedQuery = React.useMemo<WatchCompatibleQueryWithParams<RowType[]>>(() => {
|
|
@@ -58,8 +17,8 @@ export const constructCompatibleQuery = <RowType>(
|
|
|
58
17
|
sql: query,
|
|
59
18
|
parameters
|
|
60
19
|
}),
|
|
61
|
-
execute: () => powerSync
|
|
62
|
-
// Setting this is a small optimization that avoids
|
|
20
|
+
execute: () => powerSync!.getAll(query, parameters),
|
|
21
|
+
// Setting this is a small optimization that avoids QueryRunner recomputing the JSON representation.
|
|
63
22
|
stringifiedParameters
|
|
64
23
|
};
|
|
65
24
|
} else {
|
|
@@ -79,10 +38,5 @@ export const constructCompatibleQuery = <RowType>(
|
|
|
79
38
|
}
|
|
80
39
|
}, [query, powerSync, stringifiedParameters]);
|
|
81
40
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return {
|
|
85
|
-
parsedQuery,
|
|
86
|
-
queryChanged
|
|
87
|
-
};
|
|
41
|
+
return parsedQuery;
|
|
88
42
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { usePowerSync, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
2
2
|
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
3
3
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
4
4
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
@@ -6,4 +6,10 @@ export { useAllSyncStreamsHaveSynced, useSyncStream, UseSyncStreamOptions } from
|
|
|
6
6
|
export { useStatus } from './hooks/useStatus.js';
|
|
7
7
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
8
8
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
9
|
-
export {
|
|
9
|
+
export {
|
|
10
|
+
AdditionalOptions,
|
|
11
|
+
FetchReason,
|
|
12
|
+
DifferentialSuspenseQueryOptions,
|
|
13
|
+
QuerySyncStreamOptions,
|
|
14
|
+
SuspenseQueryOptions
|
|
15
|
+
} from './hooks/watched/watch-types.js';
|
package/lib/QueryStore.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { AbstractPowerSyncDatabase, WatchCompatibleQuery, WatchedQuery } from '@powersync/common';
|
|
2
|
-
import { DifferentialHookOptions } from './hooks/watched/watch-types.js';
|
|
3
|
-
export declare function generateQueryKey(sqlStatement: string, parameters: ReadonlyArray<unknown>, options: DifferentialHookOptions<unknown>): string;
|
|
4
|
-
export declare class QueryStore {
|
|
5
|
-
private db;
|
|
6
|
-
cache: Map<string, WatchedQuery<unknown, import("@powersync/common").WatchedQueryOptions, import("@powersync/common").WatchedQueryListener<unknown>>>;
|
|
7
|
-
constructor(db: AbstractPowerSyncDatabase);
|
|
8
|
-
getQuery<RowType>(key: string, query: WatchCompatibleQuery<RowType[]>, options: DifferentialHookOptions<RowType>): import("@powersync/common").DifferentialWatchedQuery<RowType>;
|
|
9
|
-
}
|
|
10
|
-
export declare function getQueryStore(db: AbstractPowerSyncDatabase): QueryStore;
|
package/lib/QueryStore.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { WatchedQueryListenerEvent } from '@powersync/common';
|
|
2
|
-
export function generateQueryKey(sqlStatement, parameters, options) {
|
|
3
|
-
return `${sqlStatement} -- ${JSON.stringify(parameters)} -- ${JSON.stringify(options)}`;
|
|
4
|
-
}
|
|
5
|
-
export class QueryStore {
|
|
6
|
-
db;
|
|
7
|
-
cache = new Map();
|
|
8
|
-
constructor(db) {
|
|
9
|
-
this.db = db;
|
|
10
|
-
}
|
|
11
|
-
getQuery(key, query, options) {
|
|
12
|
-
if (this.cache.has(key)) {
|
|
13
|
-
return this.cache.get(key);
|
|
14
|
-
}
|
|
15
|
-
const watch = options.rowComparator
|
|
16
|
-
? this.db.customQuery(query).differentialWatch({
|
|
17
|
-
rowComparator: options.rowComparator,
|
|
18
|
-
reportFetching: options.reportFetching,
|
|
19
|
-
throttleMs: options.throttleMs
|
|
20
|
-
})
|
|
21
|
-
: this.db.customQuery(query).watch({
|
|
22
|
-
reportFetching: options.reportFetching,
|
|
23
|
-
throttleMs: options.throttleMs
|
|
24
|
-
});
|
|
25
|
-
this.cache.set(key, watch);
|
|
26
|
-
const disposer = watch.registerListener({
|
|
27
|
-
closed: () => {
|
|
28
|
-
this.cache.delete(key);
|
|
29
|
-
disposer?.();
|
|
30
|
-
}
|
|
31
|
-
});
|
|
32
|
-
watch.listenerMeta.registerListener({
|
|
33
|
-
listenersChanged: (counts) => {
|
|
34
|
-
// Dispose this query if there are no subscribers present
|
|
35
|
-
// We don't use the total here since we don't want to consider `onclose` listeners
|
|
36
|
-
const relevantCounts = [
|
|
37
|
-
WatchedQueryListenerEvent.ON_DATA,
|
|
38
|
-
WatchedQueryListenerEvent.ON_STATE_CHANGE,
|
|
39
|
-
WatchedQueryListenerEvent.ON_ERROR
|
|
40
|
-
].reduce((sum, event) => {
|
|
41
|
-
return sum + (counts[event] || 0);
|
|
42
|
-
}, 0);
|
|
43
|
-
if (relevantCounts == 0) {
|
|
44
|
-
watch.close();
|
|
45
|
-
this.cache.delete(key);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
return watch;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
let queryStores = undefined;
|
|
53
|
-
export function getQueryStore(db) {
|
|
54
|
-
queryStores ||= new WeakMap();
|
|
55
|
-
const existing = queryStores.get(db);
|
|
56
|
-
if (existing) {
|
|
57
|
-
return existing;
|
|
58
|
-
}
|
|
59
|
-
const store = new QueryStore(db);
|
|
60
|
-
queryStores.set(db, store);
|
|
61
|
-
return store;
|
|
62
|
-
}
|
|
63
|
-
//# sourceMappingURL=QueryStore.js.map
|
package/lib/QueryStore.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"QueryStore.js","sourceRoot":"","sources":["../src/QueryStore.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,yBAAyB,EAC1B,MAAM,mBAAmB,CAAC;AAG3B,MAAM,UAAU,gBAAgB,CAC9B,YAAoB,EACpB,UAAkC,EAClC,OAAyC;IAEzC,OAAO,GAAG,YAAY,OAAO,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;AAC1F,CAAC;AAED,MAAM,OAAO,UAAU;IAGD;IAFpB,KAAK,GAAG,IAAI,GAAG,EAAiC,CAAC;IAEjD,YAAoB,EAA6B;QAA7B,OAAE,GAAF,EAAE,CAA2B;IAAG,CAAC;IAErD,QAAQ,CAAU,GAAW,EAAE,KAAsC,EAAE,OAAyC;QAC9G,IAAI,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YACxB,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAA4B,CAAC;QACxD,CAAC;QAED,MAAM,KAAK,GAAG,OAAO,CAAC,aAAa;YACjC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,iBAAiB,CAAC;gBAC3C,aAAa,EAAE,OAAO,CAAC,aAAa;g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CAAC,EAA6B;IACzD,WAAW,KAAK,IAAI,OAAO,EAAE,CAAC;IAC9B,MAAM,QAAQ,GAAG,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACrC,IAAI,QAAQ,EAAE,CAAC;QACb,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAC;IACjC,WAAW,CAAC,GAAG,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;IAC3B,OAAO,KAAK,CAAC;AACf,CAAC"}
|
|
@@ -1,16 +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
|
-
*/
|
|
9
|
-
export declare const useTemporaryHold: (watchedQuery?: WatchedQuery<unknown>) => void;
|
|
10
|
-
/**
|
|
11
|
-
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
12
|
-
* This creates a promise which registers a listener on the watched query.
|
|
13
|
-
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
14
|
-
* while the component is suspended.
|
|
15
|
-
*/
|
|
16
|
-
export declare const createSuspendingPromise: (query: WatchedQuery<unknown>) => Promise<void>;
|
|
@@ -1,76 +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
|
-
*/
|
|
9
|
-
export const useTemporaryHold = (watchedQuery) => {
|
|
10
|
-
const releaseTemporaryHold = React.useRef(undefined);
|
|
11
|
-
const addedHoldTo = React.useRef(undefined);
|
|
12
|
-
if (addedHoldTo.current !== watchedQuery) {
|
|
13
|
-
// The query changed, we no longer need the previous hold if present
|
|
14
|
-
releaseTemporaryHold.current?.();
|
|
15
|
-
releaseTemporaryHold.current = undefined;
|
|
16
|
-
addedHoldTo.current = watchedQuery;
|
|
17
|
-
if (!watchedQuery || !watchedQuery.state.isLoading) {
|
|
18
|
-
// No query to hold or no reason to hold, return
|
|
19
|
-
return;
|
|
20
|
-
}
|
|
21
|
-
// Create a hold by subscribing
|
|
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
|
-
};
|
|
54
|
-
/**
|
|
55
|
-
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
56
|
-
* This creates a promise which registers a listener on the watched query.
|
|
57
|
-
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
58
|
-
* while the component is suspended.
|
|
59
|
-
*/
|
|
60
|
-
export const createSuspendingPromise = (query) => {
|
|
61
|
-
return new Promise((resolve) => {
|
|
62
|
-
// The listener here will dispose itself once the loading is done
|
|
63
|
-
// This decreases the number of listeners on the query
|
|
64
|
-
// even if the component is unmounted
|
|
65
|
-
const dispose = query.registerListener({
|
|
66
|
-
onStateChange: (state) => {
|
|
67
|
-
// Returns to the hook if loading is completed or if loading resulted in an error
|
|
68
|
-
if (!state.isLoading || state.error) {
|
|
69
|
-
resolve();
|
|
70
|
-
dispose();
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
};
|
|
76
|
-
//# sourceMappingURL=suspense-utils.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"suspense-utils.js","sourceRoot":"","sources":["../../../src/hooks/suspense/suspense-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,YAAoC,EAAE,EAAE;IACvE,MAAM,oBAAoB,GAAG,KAAK,CAAC,MAAM,CAAyB,SAAS,CAAC,CAAC;IAC7E,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAAoC,SAAS,CAAC,CAAC;IAE/E,IAAI,WAAW,CAAC,OAAO,KAAK,YAAY,EAAE,CAAC;QACzC,oEAAoE;QACpE,oBAAoB,CAAC,OAAO,EAAE,EAAE,CAAC;QACjC,oBAAoB,CAAC,OAAO,GAAG,SAAS,CAAC;QACzC,WAAW,CAAC,OAAO,GAAG,YAAY,CAAC;QAEnC,IAAI,CAAC,YAAY,IAAI,CAAC,YAAY,CAAC,KAAK,CA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M,OAAO,GAAG,KAAK,CAAC,gBAAgB,CAAC;YACrC,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE;gBACvB,iFAAiF;gBACjF,IAAI,CAAC,KAAK,CAAC,SAAS,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;oBACpC,OAAO,EAAE,CAAC;oBACV,OAAO,EAAE,CAAC;gBACZ,CAAC;YACH,CAAC;SACF,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { CompilableQuery } from '@powersync/common';
|
|
2
|
-
import { AdditionalOptions } from '../watched/watch-types.js';
|
|
3
|
-
import { SuspenseQueryResult } from './SuspenseQueryResult.js';
|
|
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,73 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { generateQueryKey, getQueryStore } from '../../QueryStore.js';
|
|
3
|
-
import { usePowerSync } from '../PowerSyncContext.js';
|
|
4
|
-
import { constructCompatibleQuery } from '../watched/watch-utils.js';
|
|
5
|
-
import { createSuspendingPromise, useTemporaryHold } from './suspense-utils.js';
|
|
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
|
-
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
|
-
}, []);
|
|
40
|
-
if (error != null) {
|
|
41
|
-
// Report errors - this is caught by an error boundary
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
44
|
-
else if (data || watchedQuery?.state.isLoading === false) {
|
|
45
|
-
// Happy path data return
|
|
46
|
-
return {
|
|
47
|
-
data: data ?? watchedQuery?.state.data ?? [],
|
|
48
|
-
refresh: async (signal) => {
|
|
49
|
-
try {
|
|
50
|
-
const compiledQuery = parsedQuery.compile();
|
|
51
|
-
const result = await parsedQuery.execute({
|
|
52
|
-
sql: compiledQuery.sql,
|
|
53
|
-
parameters: [...compiledQuery.parameters],
|
|
54
|
-
db: powerSync
|
|
55
|
-
});
|
|
56
|
-
if (signal.aborted) {
|
|
57
|
-
return; // Abort if the signal is already aborted
|
|
58
|
-
}
|
|
59
|
-
setData(result);
|
|
60
|
-
setError(null);
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
63
|
-
setError(e);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
else {
|
|
69
|
-
// Notify suspense is required
|
|
70
|
-
throw createSuspendingPromise(watchedQuery);
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
//# sourceMappingURL=useSingleSuspenseQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSingleSuspenseQuery.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useSingleSuspenseQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGhF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CACpC,KAAkC,EAClC,aAAoB,EAAE,EACtB,UAA6B,EAAE,EACP,EAAE;IAC1B,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC/C,CAAC;IAED,yCAAyC;IACzC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAa,IAAI,CAAC,CAAC;IACzD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,KAAK,CA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AAI,YAAY,EAAE,KAAK,CAAC,IAAI,IAAI,EAAE;YAC5C,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;gBACxB,IAAI,CAAC;oBACH,MAAM,aAAa,GAAG,WAAW,CAAC,OAAO,EAAE,CAAC;oBAC5C,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,OAAO,CAAC;wBACvC,GAAG,EAAE,aAAa,CAAC,GAAG;wBACtB,UAAU,EAAE,CAAC,GAAG,aAAa,CAAC,UAAU,CAAC;wBACzC,EAAE,EAAE,SAAS;qBACd,CAAC,CAAC;oBACH,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;wBACnB,OAAO,CAAC,yCAAyC;oBACnD,CAAC;oBACD,OAAO,CAAC,MAAM,CAAC,CAAC;oBAChB,QAAQ,CAAC,IAAI,CAAC,CAAC;gBACjB,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBACX,QAAQ,CAAC,CAAC,CAAC,CAAC;gBACd,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,8BAA8B;QAC9B,MAAM,uBAAuB,CAAC,YAAa,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { CompilableQuery } from '@powersync/common';
|
|
2
|
-
import { AdditionalOptions } from '../watched/watch-types.js';
|
|
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,26 +0,0 @@
|
|
|
1
|
-
import { generateQueryKey, getQueryStore } from '../../QueryStore.js';
|
|
2
|
-
import { usePowerSync } from '../PowerSyncContext.js';
|
|
3
|
-
import { constructCompatibleQuery } from '../watched/watch-utils.js';
|
|
4
|
-
import { useWatchedQuerySuspenseSubscription } from './useWatchedQuerySuspenseSubscription.js';
|
|
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
|
-
};
|
|
26
|
-
//# sourceMappingURL=useWatchedSuspenseQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useWatchedSuspenseQuery.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useWatchedSuspenseQuery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,mCAAmC,EAAE,MAAM,0CAA0C,CAAC;AAE/F;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,KAAkC,EAClC,aAAoB,EAAE,EACtB,UAA6B,EAAE,EAC/B,EAAE;IACF,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC/C,CAAC;IAED,oFAAoF;IACpF,0CAA0C;IAC1C,MAAM,EAAE,WAAW,EAAE,GAAG,wBAAwB,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAC7E,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,UAAU,EAAE,gBAAgB,EAAE,GAAG,WAAW,CAAC,OAAO,EAAE,CAAC;IAE/E,MAAM,GAAG,GAAG,gBAAgB,CAAC,SAAS,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAEnE,wEAAwE;IACxE,uEAAuE;IACvE,gBAAgB;IAChB,uEAAuE;IACvE,MAAM,KAAK,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IACvC,MAAM,YAAY,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;IAE/D,OAAO,mCAAmC,CAAC,YAAY,CAAC,CAAC;AAC3D,CAAC,CAAC"}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { QueryResult } from './watch-types.js';
|
|
2
|
-
import { InternalHookOptions } from './watch-utils.js';
|
|
3
|
-
/**
|
|
4
|
-
* @internal not exported from `index.ts`
|
|
5
|
-
*/
|
|
6
|
-
export declare const useSingleQuery: <RowType = any>(options: InternalHookOptions<RowType[]>) => QueryResult<RowType>;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
/**
|
|
3
|
-
* @internal not exported from `index.ts`
|
|
4
|
-
*/
|
|
5
|
-
export const useSingleQuery = (options) => {
|
|
6
|
-
const { query, powerSync, queryChanged, active } = options;
|
|
7
|
-
const [output, setOutputState] = React.useState({
|
|
8
|
-
isLoading: true,
|
|
9
|
-
isFetching: true,
|
|
10
|
-
data: [],
|
|
11
|
-
error: undefined
|
|
12
|
-
});
|
|
13
|
-
const runQuery = React.useCallback(async (signal) => {
|
|
14
|
-
setOutputState((prev) => ({ ...prev, isLoading: true, isFetching: true, error: undefined }));
|
|
15
|
-
try {
|
|
16
|
-
const compiledQuery = query.compile();
|
|
17
|
-
const result = await query.execute({
|
|
18
|
-
sql: compiledQuery.sql,
|
|
19
|
-
parameters: [...compiledQuery.parameters],
|
|
20
|
-
db: powerSync
|
|
21
|
-
});
|
|
22
|
-
if (signal.aborted) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
setOutputState((prev) => ({
|
|
26
|
-
...prev,
|
|
27
|
-
isLoading: false,
|
|
28
|
-
isFetching: false,
|
|
29
|
-
data: result,
|
|
30
|
-
error: undefined
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
setOutputState((prev) => ({
|
|
35
|
-
...prev,
|
|
36
|
-
isLoading: false,
|
|
37
|
-
isFetching: false,
|
|
38
|
-
data: [],
|
|
39
|
-
error
|
|
40
|
-
}));
|
|
41
|
-
}
|
|
42
|
-
}, [queryChanged, query]);
|
|
43
|
-
// Trigger initial query execution
|
|
44
|
-
// @ts-ignore: Complains about not all code paths returning a value
|
|
45
|
-
React.useEffect(() => {
|
|
46
|
-
if (active) {
|
|
47
|
-
const abortController = new AbortController();
|
|
48
|
-
runQuery(abortController.signal);
|
|
49
|
-
return () => {
|
|
50
|
-
abortController.abort();
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}, [powerSync, active, queryChanged]);
|
|
54
|
-
return {
|
|
55
|
-
...output,
|
|
56
|
-
refresh: runQuery
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
//# sourceMappingURL=useSingleQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSingleQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useSingleQuery.ts"],"names":[],"m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