@powersync/react 1.8.0 → 1.8.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/QueryStore.js +1 -0
- package/lib/QueryStore.js.map +1 -0
- package/lib/hooks/PowerSyncContext.js +1 -0
- package/lib/hooks/PowerSyncContext.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.js +1 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.js +1 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js +1 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js.map +1 -0
- package/lib/hooks/streams.d.ts +1 -1
- package/lib/hooks/streams.js +1 -0
- package/lib/hooks/streams.js.map +1 -0
- package/lib/hooks/suspense/SuspenseQueryResult.js +1 -0
- package/lib/hooks/suspense/SuspenseQueryResult.js.map +1 -0
- package/lib/hooks/suspense/suspense-utils.js +1 -0
- package/lib/hooks/suspense/suspense-utils.js.map +1 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +1 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +1 -0
- package/lib/hooks/suspense/useSuspenseQuery.js +1 -0
- package/lib/hooks/suspense/useSuspenseQuery.js.map +1 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +1 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js.map +1 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +1 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +1 -0
- package/lib/hooks/useStatus.js +1 -0
- package/lib/hooks/useStatus.js.map +1 -0
- package/lib/hooks/watched/useQuery.js +1 -0
- package/lib/hooks/watched/useQuery.js.map +1 -0
- package/lib/hooks/watched/useSingleQuery.js +1 -0
- package/lib/hooks/watched/useSingleQuery.js.map +1 -0
- package/lib/hooks/watched/useWatchedQuery.js +1 -0
- package/lib/hooks/watched/useWatchedQuery.js.map +1 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js +1 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js.map +1 -0
- package/lib/hooks/watched/watch-types.js +1 -0
- package/lib/hooks/watched/watch-types.js.map +1 -0
- package/lib/hooks/watched/watch-utils.js +1 -0
- package/lib/hooks/watched/watch-utils.js.map +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -0
- package/package.json +8 -7
- package/src/QueryStore.ts +81 -0
- package/src/hooks/PowerSyncContext.ts +22 -0
- package/src/hooks/deprecated/usePowerSyncQuery.ts +28 -0
- package/src/hooks/deprecated/usePowerSyncStatus.ts +33 -0
- package/src/hooks/deprecated/usePowerSyncWatchedQuery.ts +60 -0
- package/src/hooks/streams.ts +131 -0
- package/src/hooks/suspense/SuspenseQueryResult.ts +4 -0
- package/src/hooks/suspense/suspense-utils.ts +86 -0
- package/src/hooks/suspense/useSingleSuspenseQuery.ts +81 -0
- package/src/hooks/suspense/useSuspenseQuery.ts +76 -0
- package/src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts +60 -0
- package/src/hooks/suspense/useWatchedSuspenseQuery.ts +36 -0
- package/src/hooks/useStatus.ts +17 -0
- package/src/hooks/watched/useQuery.ts +98 -0
- package/src/hooks/watched/useSingleQuery.ts +67 -0
- package/src/hooks/watched/useWatchedQuery.ts +89 -0
- package/src/hooks/watched/useWatchedQuerySubscription.ts +52 -0
- package/src/hooks/watched/watch-types.ts +93 -0
- package/src/hooks/watched/watch-utils.ts +88 -0
- package/src/index.ts +12 -0
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useNullableWatchedQuerySubscription } from './useWatchedQuerySubscription.js';
|
|
3
|
+
import { DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
4
|
+
import { InternalHookOptions } from './watch-utils.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal This is not exported from the index.ts
|
|
8
|
+
*
|
|
9
|
+
* When an incremental query is used the return type is readonly. This is required
|
|
10
|
+
* since the implementation requires a stable ref.
|
|
11
|
+
* For legacy compatibility we allow mutating when a standard query is used. Mutations should
|
|
12
|
+
* not affect the internal implementation in this case.
|
|
13
|
+
*/
|
|
14
|
+
export const useWatchedQuery = <RowType = unknown>(
|
|
15
|
+
options: InternalHookOptions<RowType[]> & { options: DifferentialHookOptions<RowType> }
|
|
16
|
+
): QueryResult<RowType> | ReadonlyQueryResult<RowType> => {
|
|
17
|
+
const { query, powerSync, queryChanged, options: hookOptions, active } = options;
|
|
18
|
+
|
|
19
|
+
function createWatchedQuery() {
|
|
20
|
+
if (!active) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const watch = hookOptions.rowComparator
|
|
25
|
+
? powerSync.customQuery(query).differentialWatch({
|
|
26
|
+
rowComparator: hookOptions.rowComparator,
|
|
27
|
+
reportFetching: hookOptions.reportFetching,
|
|
28
|
+
throttleMs: hookOptions.throttleMs
|
|
29
|
+
})
|
|
30
|
+
: powerSync.customQuery(query).watch({
|
|
31
|
+
reportFetching: hookOptions.reportFetching,
|
|
32
|
+
throttleMs: hookOptions.throttleMs
|
|
33
|
+
});
|
|
34
|
+
return watch;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const [watchedQuery, setWatchedQuery] = React.useState(createWatchedQuery);
|
|
38
|
+
const disposePendingUpdateListener = React.useRef<() => void | null>(null);
|
|
39
|
+
|
|
40
|
+
React.useEffect(() => {
|
|
41
|
+
watchedQuery?.close();
|
|
42
|
+
const newQuery = createWatchedQuery();
|
|
43
|
+
setWatchedQuery(newQuery);
|
|
44
|
+
|
|
45
|
+
return () => {
|
|
46
|
+
disposePendingUpdateListener.current?.();
|
|
47
|
+
newQuery?.close();
|
|
48
|
+
};
|
|
49
|
+
}, [powerSync, active]);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Indicates that the query will be re-fetched due to a change in the query.
|
|
53
|
+
* We execute this in-line (not using an effect) since effects are delayed till after the hook returns.
|
|
54
|
+
* The `queryChanged` value should only be true for a single render.
|
|
55
|
+
* The `updateSettings` method is asynchronous, thus it will update the state asynchronously.
|
|
56
|
+
* In the React hooks we'd like to report that we are fetching the data for an updated query
|
|
57
|
+
* as soon as the query has been updated. This prevents a result flow where e.g. the hook:
|
|
58
|
+
* - already returned a result: isLoading, isFetching are both false
|
|
59
|
+
* - the query is updated, but the state is still isFetching=false from the previous state
|
|
60
|
+
* We override the isFetching status while the `updateSettings` method is running (if we report `isFetching`),
|
|
61
|
+
* we override this value just until the `updateSettings` method itself will update the `isFetching` status.
|
|
62
|
+
* We achieve this by registering a `settingsWillUpdate` listener on the `WatchedQuery`. This will fire
|
|
63
|
+
* just before the `isFetching` status is updated.
|
|
64
|
+
*/
|
|
65
|
+
if (queryChanged) {
|
|
66
|
+
// Keep track of this pending operation
|
|
67
|
+
watchedQuery?.updateSettings({
|
|
68
|
+
query,
|
|
69
|
+
throttleMs: hookOptions.throttleMs,
|
|
70
|
+
reportFetching: hookOptions.reportFetching
|
|
71
|
+
});
|
|
72
|
+
// This could have been called multiple times, clear any old listeners.
|
|
73
|
+
disposePendingUpdateListener.current?.();
|
|
74
|
+
disposePendingUpdateListener.current = watchedQuery?.registerListener({
|
|
75
|
+
settingsWillUpdate: () => {
|
|
76
|
+
// We'll use the fact that we have a listener at all as an indication
|
|
77
|
+
disposePendingUpdateListener.current?.();
|
|
78
|
+
disposePendingUpdateListener.current = null;
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const shouldReportCurrentlyFetching = (hookOptions.reportFetching ?? true) && !!disposePendingUpdateListener.current;
|
|
84
|
+
const result = useNullableWatchedQuerySubscription(watchedQuery);
|
|
85
|
+
return {
|
|
86
|
+
...result,
|
|
87
|
+
isFetching: result?.isFetching || shouldReportCurrentlyFetching
|
|
88
|
+
};
|
|
89
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { WatchedQuery } from '@powersync/common';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A hook to access and subscribe to the results of an existing {@link WatchedQuery} instance.
|
|
6
|
+
* @example
|
|
7
|
+
* export const ContentComponent = () => {
|
|
8
|
+
* const { data: lists } = useWatchedQuerySubscription(listsQuery);
|
|
9
|
+
*
|
|
10
|
+
* return <View>
|
|
11
|
+
* {lists.map((l) => (
|
|
12
|
+
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
13
|
+
* ))}
|
|
14
|
+
* </View>;
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
export const useWatchedQuerySubscription = <
|
|
19
|
+
ResultType = unknown,
|
|
20
|
+
Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>
|
|
21
|
+
>(
|
|
22
|
+
query: Query
|
|
23
|
+
): Query['state'] => {
|
|
24
|
+
return useNullableWatchedQuerySubscription(query);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export const useNullableWatchedQuerySubscription = <
|
|
31
|
+
ResultType = unknown,
|
|
32
|
+
Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>
|
|
33
|
+
>(
|
|
34
|
+
query: Query | null
|
|
35
|
+
): Query['state'] | undefined => {
|
|
36
|
+
const [output, setOutputState] = React.useState(query?.state);
|
|
37
|
+
|
|
38
|
+
// @ts-ignore: Complains about not all code paths returning a value
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
if (query) {
|
|
41
|
+
setOutputState(query.state);
|
|
42
|
+
|
|
43
|
+
return query.registerListener({
|
|
44
|
+
onStateChange: (state) => {
|
|
45
|
+
setOutputState({ ...state });
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}, [query]);
|
|
50
|
+
|
|
51
|
+
return output;
|
|
52
|
+
};
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { DifferentialWatchedQueryComparator, SQLOnChangeOptions, SyncSubscriptionDescription } from '@powersync/common';
|
|
2
|
+
import { UseSyncStreamOptions } from '../streams.js';
|
|
3
|
+
|
|
4
|
+
export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
|
|
5
|
+
/**
|
|
6
|
+
* An optional array of sync streams (with names and parameters) backing the query.
|
|
7
|
+
*
|
|
8
|
+
* When set, `useQuery` will subscribe to those streams (and automatically handle unsubscribing from them, too).
|
|
9
|
+
*
|
|
10
|
+
* If {@link QuerySyncStreamOptions} is set on a stream, `useQuery` will remain in a loading state until that stream
|
|
11
|
+
* is marked as {@link SyncSubscriptionDescription.hasSynced}. This ensures the query is not missing rows that haven't
|
|
12
|
+
* been downloaded.
|
|
13
|
+
* Note however that after an initial sync, the query will not block itself while new rows are downloading. Instead,
|
|
14
|
+
* consistent sync snapshots will be made available as they've been processed by PowerSync.
|
|
15
|
+
*
|
|
16
|
+
* @experimental Sync streams are currently in alpha.
|
|
17
|
+
*/
|
|
18
|
+
streams?: QuerySyncStreamOptions[];
|
|
19
|
+
reportFetching?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Additional options to control how `useQuery` behaves when subscribing to a stream.
|
|
24
|
+
*/
|
|
25
|
+
export interface QuerySyncStreamOptions extends UseSyncStreamOptions {
|
|
26
|
+
/**
|
|
27
|
+
* When set to `true`, a `useQuery` hook will remain in a loading state as long as the stream is resolving or
|
|
28
|
+
* downloading for the first time (in other words, until {@link SyncSubscriptionDescription.hasSynced} is true).
|
|
29
|
+
*/
|
|
30
|
+
waitForStream?: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface AdditionalOptions extends HookWatchOptions {
|
|
34
|
+
runQueryOnce?: boolean;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface DifferentialHookOptions<RowType> extends HookWatchOptions {
|
|
38
|
+
/**
|
|
39
|
+
* Used to compare result sets.
|
|
40
|
+
*
|
|
41
|
+
* By default the hook will requery on any dependent table change. This will
|
|
42
|
+
* emit a new hook result even if the result set has not changed.
|
|
43
|
+
*
|
|
44
|
+
* Specifying a {@link DifferentialWatchedQueryComparator} will remove emissions for
|
|
45
|
+
* unchanged result sets.
|
|
46
|
+
* Furthermore, emitted `data` arrays will preserve object references between result set emissions
|
|
47
|
+
* for unchanged rows.
|
|
48
|
+
* @example
|
|
49
|
+
* ```javascript
|
|
50
|
+
* {
|
|
51
|
+
* rowComparator: {
|
|
52
|
+
* keyBy: (item) => item.id,
|
|
53
|
+
* compareBy: (item) => JSON.stringify(item)
|
|
54
|
+
* }
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export type ReadonlyQueryResult<RowType> = {
|
|
62
|
+
readonly data: ReadonlyArray<Readonly<RowType>>;
|
|
63
|
+
/**
|
|
64
|
+
* Indicates the initial loading state (hard loading). Loading becomes false once the first set of results from the watched query is available or an error occurs.
|
|
65
|
+
*/
|
|
66
|
+
readonly isLoading: boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Indicates whether the query is currently fetching data, is true during the initial load and any time when the query is re-evaluating (useful for large queries).
|
|
69
|
+
*/
|
|
70
|
+
readonly isFetching: boolean;
|
|
71
|
+
readonly error: Error | undefined;
|
|
72
|
+
/**
|
|
73
|
+
* Function used to run the query again.
|
|
74
|
+
*/
|
|
75
|
+
refresh?: (signal?: AbortSignal) => Promise<void>;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
export type QueryResult<RowType> = {
|
|
79
|
+
data: RowType[];
|
|
80
|
+
/**
|
|
81
|
+
* Indicates the initial loading state (hard loading). Loading becomes false once the first set of results from the watched query is available or an error occurs.
|
|
82
|
+
*/
|
|
83
|
+
isLoading: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Indicates whether the query is currently fetching data, is true during the initial load and any time when the query is re-evaluating (useful for large queries).
|
|
86
|
+
*/
|
|
87
|
+
isFetching: boolean;
|
|
88
|
+
error: Error | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* Function used to run the query again.
|
|
91
|
+
*/
|
|
92
|
+
refresh?: (signal?: AbortSignal) => Promise<void>;
|
|
93
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase, CompilableQuery, CompiledQuery, WatchCompatibleQuery } from '@powersync/common';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { usePowerSync } from '../PowerSyncContext.js';
|
|
4
|
+
import { AdditionalOptions } from './watch-types.js';
|
|
5
|
+
|
|
6
|
+
export type InternalHookOptions<DataType> = {
|
|
7
|
+
query: WatchCompatibleQuery<DataType>;
|
|
8
|
+
powerSync: AbstractPowerSyncDatabase;
|
|
9
|
+
queryChanged: boolean;
|
|
10
|
+
active: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
interface WatchCompatibleQueryWithParams<T> extends WatchCompatibleQuery<T> {
|
|
14
|
+
stringifiedParameters?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const checkQueryChanged = <T>(query: WatchCompatibleQueryWithParams<T>, options: AdditionalOptions) => {
|
|
18
|
+
let _compiled: CompiledQuery;
|
|
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();
|
|
52
|
+
const stringifiedParameters = React.useMemo(() => JSON.stringify(parameters), [parameters]);
|
|
53
|
+
|
|
54
|
+
const parsedQuery = React.useMemo<WatchCompatibleQueryWithParams<RowType[]>>(() => {
|
|
55
|
+
if (typeof query == 'string') {
|
|
56
|
+
return {
|
|
57
|
+
compile: () => ({
|
|
58
|
+
sql: query,
|
|
59
|
+
parameters
|
|
60
|
+
}),
|
|
61
|
+
execute: () => powerSync.getAll(query, parameters),
|
|
62
|
+
// Setting this is a small optimization that avoids checkQueryChanged recomputing the JSON representation.
|
|
63
|
+
stringifiedParameters
|
|
64
|
+
};
|
|
65
|
+
} else {
|
|
66
|
+
return {
|
|
67
|
+
// Generics differ a bit but holistically this is the same
|
|
68
|
+
compile: () => {
|
|
69
|
+
const compiled = query.compile();
|
|
70
|
+
return {
|
|
71
|
+
sql: compiled.sql,
|
|
72
|
+
parameters: [...compiled.parameters]
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
execute: () => query.execute()
|
|
76
|
+
// Note that we can't set stringifiedParameters here because we only know parameters after the query has been
|
|
77
|
+
// compiled.
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}, [query, powerSync, stringifiedParameters]);
|
|
81
|
+
|
|
82
|
+
const queryChanged = checkQueryChanged(parsedQuery, options);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
parsedQuery,
|
|
86
|
+
queryChanged
|
|
87
|
+
};
|
|
88
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery.js';
|
|
2
|
+
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus.js';
|
|
3
|
+
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery.js';
|
|
4
|
+
export * from './hooks/PowerSyncContext.js';
|
|
5
|
+
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
6
|
+
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
7
|
+
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
8
|
+
export { useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
9
|
+
export { useStatus } from './hooks/useStatus.js';
|
|
10
|
+
export { useQuery } from './hooks/watched/useQuery.js';
|
|
11
|
+
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
12
|
+
export { AdditionalOptions } from './hooks/watched/watch-types.js';
|