@powersync/react 0.0.0-dev-20260202162549 → 0.0.0-dev-20260216124709
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.d.ts +10 -0
- package/lib/QueryStore.js +63 -0
- package/lib/QueryStore.js.map +1 -0
- package/lib/hooks/PowerSyncContext.d.ts +21 -0
- package/lib/hooks/PowerSyncContext.js +21 -0
- package/lib/hooks/PowerSyncContext.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.d.ts +7 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.js +26 -0
- package/lib/hooks/deprecated/usePowerSyncQuery.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.d.ts +15 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.js +30 -0
- package/lib/hooks/deprecated/usePowerSyncStatus.js.map +1 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.d.ts +17 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js +45 -0
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js.map +1 -0
- package/lib/hooks/streams.d.ts +31 -0
- package/lib/hooks/streams.js +93 -0
- package/lib/hooks/streams.js.map +1 -0
- package/lib/hooks/suspense/SuspenseQueryResult.d.ts +3 -0
- package/lib/hooks/suspense/SuspenseQueryResult.js +2 -0
- package/lib/hooks/suspense/SuspenseQueryResult.js.map +1 -0
- package/lib/hooks/suspense/suspense-utils.d.ts +16 -0
- package/lib/hooks/suspense/suspense-utils.js +76 -0
- package/lib/hooks/suspense/suspense-utils.js.map +1 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +10 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +73 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +1 -0
- package/lib/hooks/suspense/useSuspenseQuery.d.ts +53 -0
- package/lib/hooks/suspense/useSuspenseQuery.js +11 -0
- package/lib/hooks/suspense/useSuspenseQuery.js.map +1 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.d.ts +23 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +52 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js.map +1 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +6 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +26 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +1 -0
- package/lib/hooks/useStatus.d.ts +15 -0
- package/lib/hooks/useStatus.js +17 -0
- package/lib/hooks/useStatus.js.map +1 -0
- package/lib/hooks/watched/useQuery.d.ts +39 -0
- package/lib/hooks/watched/useQuery.js +43 -0
- package/lib/hooks/watched/useQuery.js.map +1 -0
- package/lib/hooks/watched/useSingleQuery.d.ts +6 -0
- package/lib/hooks/watched/useSingleQuery.js +59 -0
- package/lib/hooks/watched/useSingleQuery.js.map +1 -0
- package/lib/hooks/watched/useWatchedQuery.d.ts +13 -0
- package/lib/hooks/watched/useWatchedQuery.js +78 -0
- package/lib/hooks/watched/useWatchedQuery.js.map +1 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +20 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js +37 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js.map +1 -0
- package/lib/hooks/watched/watch-types.d.ts +87 -0
- package/lib/hooks/watched/watch-types.js +2 -0
- package/lib/hooks/watched/watch-types.js.map +1 -0
- package/lib/hooks/watched/watch-utils.d.ts +17 -0
- package/lib/hooks/watched/watch-utils.js +62 -0
- package/lib/hooks/watched/watch-utils.js.map +1 -0
- package/lib/index.d.ts +12 -0
- package/lib/index.js +11 -0
- package/lib/index.js.map +1 -0
- package/package.json +5 -5
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { CompilableQuery } from '@powersync/common';
|
|
2
|
+
import { AdditionalOptions, DifferentialHookOptions } from '../watched/watch-types.js';
|
|
3
|
+
import { ReadonlySuspenseQueryResult, SuspenseQueryResult } from './SuspenseQueryResult.js';
|
|
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 `rowComparator` is provided.
|
|
31
|
+
* const { data: lists } = useSuspenseQuery('SELECT * from lists', [], {
|
|
32
|
+
* rowComparator: {
|
|
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>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useSingleSuspenseQuery } from './useSingleSuspenseQuery.js';
|
|
2
|
+
import { useWatchedSuspenseQuery } from './useWatchedSuspenseQuery.js';
|
|
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
|
+
}
|
|
11
|
+
//# sourceMappingURL=useSuspenseQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSuspenseQuery.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useSuspenseQuery.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AA4DvE,MAAM,UAAU,gBAAgB,CAC9B,KAAwC,EACxC,aAAoB,EAAE,EACtB,UAAgE,EAAE;IAElE,QAAQ,OAAO,EAAE,YAAY,EAAE,CAAC;QAC9B,KAAK,IAAI;YACP,OAAO,sBAAsB,CAAU,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;QACrE;YACE,OAAO,uBAAuB,CAAU,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IACxE,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
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"];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createSuspendingPromise, useTemporaryHold } from './suspense-utils.js';
|
|
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
|
+
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
|
+
return dispose;
|
|
38
|
+
}, []);
|
|
39
|
+
if (query.state.error != null) {
|
|
40
|
+
// Report errors - this is caught by an error boundary
|
|
41
|
+
throw query.state.error;
|
|
42
|
+
}
|
|
43
|
+
else if (!query.state.isLoading) {
|
|
44
|
+
// Happy path data return
|
|
45
|
+
return query.state;
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
// Notify suspense is required
|
|
49
|
+
throw createSuspendingPromise(query);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=useWatchedQuerySuspenseSubscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWatchedQuerySuspenseSubscription.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAIjD,KAAY,EACI,EAAE;IAClB,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAExB,8BAA8B;IAC9B,MAAM,CAAC,EAAE,gBAAgB,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE/C,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,oDAAoD;QACpD,yEAAyE;QACzE,MAAM,OAAO,GAAG,KAAK,CAAC,gBAAgB,CAAC;YACrC,aAAa;gBACX,mBAAmB;gBACnB,gBAAgB,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;YACvC,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAI,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;QAC9B,sDAAsD;QACtD,MAAM,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC;IAC1B,CAAC;SAAM,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;QAClC,yBAAyB;QACzB,OAAO,KAAK,CAAC,KAAK,CAAC;IACrB,CAAC;SAAM,CAAC;QACN,8BAA8B;QAC9B,MAAM,uBAAuB,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
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>[]>;
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom hook that provides access to the current status of PowerSync.
|
|
3
|
+
* @returns The PowerSync Database status.
|
|
4
|
+
* @example
|
|
5
|
+
* import { useStatus } from "@powersync/react";
|
|
6
|
+
*
|
|
7
|
+
* const Component = () => {
|
|
8
|
+
* const status = useStatus();
|
|
9
|
+
*
|
|
10
|
+
* return <div>
|
|
11
|
+
* status.connected ? 'wifi' : 'wifi-off'
|
|
12
|
+
* </div>
|
|
13
|
+
* };
|
|
14
|
+
*/
|
|
15
|
+
export declare const useStatus: () => import("@powersync/common").SyncStatus;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { usePowerSyncStatus } from './deprecated/usePowerSyncStatus.js';
|
|
2
|
+
/**
|
|
3
|
+
* Custom hook that provides access to the current status of PowerSync.
|
|
4
|
+
* @returns The PowerSync Database status.
|
|
5
|
+
* @example
|
|
6
|
+
* import { useStatus } from "@powersync/react";
|
|
7
|
+
*
|
|
8
|
+
* const Component = () => {
|
|
9
|
+
* const status = useStatus();
|
|
10
|
+
*
|
|
11
|
+
* return <div>
|
|
12
|
+
* status.connected ? 'wifi' : 'wifi-off'
|
|
13
|
+
* </div>
|
|
14
|
+
* };
|
|
15
|
+
*/
|
|
16
|
+
export const useStatus = () => usePowerSyncStatus();
|
|
17
|
+
//# sourceMappingURL=useStatus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStatus.js","sourceRoot":"","sources":["../../src/hooks/useStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,oCAAoC,CAAC;AAExE;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,GAAG,EAAE,CAAC,kBAAkB,EAAE,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type CompilableQuery } from '@powersync/common';
|
|
2
|
+
import { AdditionalOptions, DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
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
|
+
* // Providing a `rowComparator` results in the hook using an incremental query under the hood.
|
|
21
|
+
* // An incremental query will only emit results when a change to the result set occurs.
|
|
22
|
+
* // The internal array object references are maintained for unchanged rows.
|
|
23
|
+
* // The returned lists array is read only when a `rowComparator` is provided.
|
|
24
|
+
* const { data: lists } = useQuery('SELECT * from lists', [], {
|
|
25
|
+
* rowComparator: {
|
|
26
|
+
* keyBy: (item) => item.id,
|
|
27
|
+
* compareBy: (item) => JSON.stringify(item)
|
|
28
|
+
* }
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* return <View>
|
|
32
|
+
* {lists.map((l) => (
|
|
33
|
+
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
34
|
+
* ))}
|
|
35
|
+
* </View>
|
|
36
|
+
* }
|
|
37
|
+
*/
|
|
38
|
+
export declare function useQuery<RowType = any>(query: string | CompilableQuery<RowType>, parameters?: any[], options?: AdditionalOptions): QueryResult<RowType>;
|
|
39
|
+
export declare function useQuery<RowType = any>(query: string | CompilableQuery<RowType>, parameters?: any[], options?: DifferentialHookOptions<RowType>): ReadonlyQueryResult<RowType>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { usePowerSync } from '../PowerSyncContext.js';
|
|
2
|
+
import { useSingleQuery } from './useSingleQuery.js';
|
|
3
|
+
import { useWatchedQuery } from './useWatchedQuery.js';
|
|
4
|
+
import { constructCompatibleQuery } from './watch-utils.js';
|
|
5
|
+
import { useAllSyncStreamsHaveSynced } from '../streams.js';
|
|
6
|
+
export function useQuery(query, parameters = [], options = {}) {
|
|
7
|
+
const powerSync = usePowerSync();
|
|
8
|
+
if (!powerSync) {
|
|
9
|
+
return {
|
|
10
|
+
..._loadingState,
|
|
11
|
+
isLoading: false,
|
|
12
|
+
error: new Error('PowerSync not configured.')
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const { parsedQuery, queryChanged } = constructCompatibleQuery(query, parameters, options);
|
|
16
|
+
const streamsHaveSynced = useAllSyncStreamsHaveSynced(powerSync, options?.streams);
|
|
17
|
+
const runOnce = options?.runQueryOnce == true;
|
|
18
|
+
const single = useSingleQuery({
|
|
19
|
+
query: parsedQuery,
|
|
20
|
+
powerSync,
|
|
21
|
+
queryChanged,
|
|
22
|
+
active: runOnce && streamsHaveSynced
|
|
23
|
+
});
|
|
24
|
+
const watched = useWatchedQuery({
|
|
25
|
+
query: parsedQuery,
|
|
26
|
+
powerSync,
|
|
27
|
+
queryChanged,
|
|
28
|
+
options: {
|
|
29
|
+
reportFetching: options.reportFetching,
|
|
30
|
+
// Maintains backwards compatibility with previous versions
|
|
31
|
+
// Differentiation is opt-in by default
|
|
32
|
+
// We emit new data for each table change by default.
|
|
33
|
+
rowComparator: options.rowComparator
|
|
34
|
+
},
|
|
35
|
+
active: !runOnce && streamsHaveSynced
|
|
36
|
+
});
|
|
37
|
+
if (!streamsHaveSynced) {
|
|
38
|
+
return { ..._loadingState };
|
|
39
|
+
}
|
|
40
|
+
return (runOnce ? single : watched) ?? _loadingState;
|
|
41
|
+
}
|
|
42
|
+
const _loadingState = { isLoading: true, isFetching: false, data: [], error: undefined };
|
|
43
|
+
//# sourceMappingURL=useQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useQuery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAEvD,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAgD5D,MAAM,UAAU,QAAQ,CACtB,KAAwC,EACxC,aAAoB,EAAE,EACtB,UAAgE,EAAE;IAElE,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO;YACL,GAAG,aAAa;YAChB,SAAS,EAAE,KAAK;YAChB,KAAK,EAAE,IAAI,KAAK,CAAC,2BAA2B,CAAC;SAC9C,CAAC;IACJ,CAAC;IACD,MAAM,EAAE,WAAW,EAAE,YAAY,EAAE,GAAG,wBAAwB,CAAC,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IAC3F,MAAM,iBAAiB,GAAG,2BAA2B,CAAC,SAAS,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;IACnF,MAAM,OAAO,GAAG,OAAO,EAAE,YAAY,IAAI,IAAI,CAAC;IAC9C,MAAM,MAAM,GAAG,cAAc,CAAU;QACrC,KAAK,EAAE,WAAW;QAClB,SAAS;QACT,YAAY;QACZ,MAAM,EAAE,OAAO,IAAI,iBAAiB;KACrC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,eAAe,CAAU;QACvC,KAAK,EAAE,WAAW;QAClB,SAAS;QACT,YAAY;QACZ,OAAO,EAAE;YACP,cAAc,EAAE,OAAO,CAAC,cAAc;YACtC,2DAA2D;YAC3D,uCAAuC;YACvC,qDAAqD;YACrD,aAAa,EAAE,OAAO,CAAC,aAAa;SACrC;QACD,MAAM,EAAE,CAAC,OAAO,IAAI,iBAAiB;KACtC,CAAC,CAAC;IAEH,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACvB,OAAO,EAAE,GAAG,aAAa,EAAE,CAAC;IAC9B,CAAC;IAED,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,aAAa,CAAC;AACvD,CAAC;AAED,MAAM,aAAa,GAAG,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
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>;
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSingleQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useSingleQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAgB,OAAuC,EAAwB,EAAE;IAC7G,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAE3D,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAuB;QACpE,SAAS,EAAE,IAAI;QACf,UAAU,EAAE,IAAI;QAChB,IAAI,EAAE,EAAE;QACR,KAAK,EAAE,SAAS;KACjB,CAAC,CAAC;IAEH,MAAM,QAAQ,GAAG,KAAK,CAAC,WAAW,CAChC,KAAK,EAAE,MAAoB,EAAE,EAAE;QAC7B,cAAc,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;QAC7F,IAAI,CAAC;YACH,MAAM,aAAa,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;YACtC,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC;gBACjC,GAAG,EAAE,aAAa,CAAC,GAAG;gBACtB,UAAU,EAAE,CAAC,GAAG,aAAa,CAAC,UAAU,CAAC;gBACzC,EAAE,EAAE,SAAS;aACd,CAAC,CAAC;YACH,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;gBACnB,OAAO;YACT,CAAC;YACD,cAAc,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBACxB,GAAG,IAAI;gBACP,SAAS,EAAE,KAAK;gBAChB,UAAU,EAAE,KAAK;gBACjB,IAAI,EAAE,MAAM;gBACZ,KAAK,EAAE,SAAS;aACjB,CAAC,CAAC,CAAC;QACN,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,cAAc,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;gBACxB,GAAG,IAAI;gBACP,SAAS,EAAE,KAAK;gBAChB,UAAU,EAAE,KAAK;gBACjB,IAAI,EAAE,EAAE;gBACR,KAAK;aACN,CAAC,CAAC,CAAC;QACN,CAAC;IACH,CAAC,EACD,CAAC,YAAY,EAAE,KAAK,CAAC,CACtB,CAAC;IAEF,kCAAkC;IAClC,mEAAmE;IACnE,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;YAC9C,QAAQ,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;YACjC,OAAO,GAAG,EAAE;gBACV,eAAe,CAAC,KAAK,EAAE,CAAC;YAC1B,CAAC,CAAC;QACJ,CAAC;IACH,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IAEtC,OAAO;QACL,GAAG,MAAM;QACT,OAAO,EAAE,QAAQ;KAClB,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
2
|
+
import { InternalHookOptions } from './watch-utils.js';
|
|
3
|
+
/**
|
|
4
|
+
* @internal This is not exported from the index.ts
|
|
5
|
+
*
|
|
6
|
+
* When an incremental 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>;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useNullableWatchedQuerySubscription } from './useWatchedQuerySubscription.js';
|
|
3
|
+
/**
|
|
4
|
+
* @internal This is not exported from the index.ts
|
|
5
|
+
*
|
|
6
|
+
* When an incremental 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 const useWatchedQuery = (options) => {
|
|
12
|
+
const { query, powerSync, queryChanged, options: hookOptions, active } = options;
|
|
13
|
+
function createWatchedQuery() {
|
|
14
|
+
if (!active) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const watch = hookOptions.rowComparator
|
|
18
|
+
? powerSync.customQuery(query).differentialWatch({
|
|
19
|
+
rowComparator: hookOptions.rowComparator,
|
|
20
|
+
reportFetching: hookOptions.reportFetching,
|
|
21
|
+
throttleMs: hookOptions.throttleMs
|
|
22
|
+
})
|
|
23
|
+
: powerSync.customQuery(query).watch({
|
|
24
|
+
reportFetching: hookOptions.reportFetching,
|
|
25
|
+
throttleMs: hookOptions.throttleMs
|
|
26
|
+
});
|
|
27
|
+
return watch;
|
|
28
|
+
}
|
|
29
|
+
const [watchedQuery, setWatchedQuery] = React.useState(createWatchedQuery);
|
|
30
|
+
const disposePendingUpdateListener = React.useRef(null);
|
|
31
|
+
React.useEffect(() => {
|
|
32
|
+
watchedQuery?.close();
|
|
33
|
+
const newQuery = createWatchedQuery();
|
|
34
|
+
setWatchedQuery(newQuery);
|
|
35
|
+
return () => {
|
|
36
|
+
disposePendingUpdateListener.current?.();
|
|
37
|
+
newQuery?.close();
|
|
38
|
+
};
|
|
39
|
+
}, [powerSync, active]);
|
|
40
|
+
/**
|
|
41
|
+
* Indicates that the query will be re-fetched due to a change in the query.
|
|
42
|
+
* We execute this in-line (not using an effect) since effects are delayed till after the hook returns.
|
|
43
|
+
* The `queryChanged` value should only be true for a single render.
|
|
44
|
+
* The `updateSettings` method is asynchronous, thus it will update the state asynchronously.
|
|
45
|
+
* In the React hooks we'd like to report that we are fetching the data for an updated query
|
|
46
|
+
* as soon as the query has been updated. This prevents a result flow where e.g. the hook:
|
|
47
|
+
* - already returned a result: isLoading, isFetching are both false
|
|
48
|
+
* - the query is updated, but the state is still isFetching=false from the previous state
|
|
49
|
+
* We override the isFetching status while the `updateSettings` method is running (if we report `isFetching`),
|
|
50
|
+
* we override this value just until the `updateSettings` method itself will update the `isFetching` status.
|
|
51
|
+
* We achieve this by registering a `settingsWillUpdate` listener on the `WatchedQuery`. This will fire
|
|
52
|
+
* just before the `isFetching` status is updated.
|
|
53
|
+
*/
|
|
54
|
+
if (queryChanged) {
|
|
55
|
+
// Keep track of this pending operation
|
|
56
|
+
watchedQuery?.updateSettings({
|
|
57
|
+
query,
|
|
58
|
+
throttleMs: hookOptions.throttleMs,
|
|
59
|
+
reportFetching: hookOptions.reportFetching
|
|
60
|
+
});
|
|
61
|
+
// This could have been called multiple times, clear any old listeners.
|
|
62
|
+
disposePendingUpdateListener.current?.();
|
|
63
|
+
disposePendingUpdateListener.current = watchedQuery?.registerListener({
|
|
64
|
+
settingsWillUpdate: () => {
|
|
65
|
+
// We'll use the fact that we have a listener at all as an indication
|
|
66
|
+
disposePendingUpdateListener.current?.();
|
|
67
|
+
disposePendingUpdateListener.current = null;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const shouldReportCurrentlyFetching = (hookOptions.reportFetching ?? true) && !!disposePendingUpdateListener.current;
|
|
72
|
+
const result = useNullableWatchedQuerySubscription(watchedQuery);
|
|
73
|
+
return {
|
|
74
|
+
...result,
|
|
75
|
+
isFetching: result?.isFetching || shouldReportCurrentlyFetching
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
//# sourceMappingURL=useWatchedQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWatchedQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useWatchedQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,mCAAmC,EAAE,MAAM,kCAAkC,CAAC;AAIvF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,OAAuF,EAClC,EAAE;IACvD,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEjF,SAAS,kBAAkB;QACzB,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,KAAK,GAAG,WAAW,CAAC,aAAa;YACrC,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,iBAAiB,CAAC;gBAC7C,aAAa,EAAE,WAAW,CAAC,aAAa;gBACxC,cAAc,EAAE,WAAW,CAAC,cAAc;gBAC1C,UAAU,EAAE,WAAW,CAAC,UAAU;aACnC,CAAC;YACJ,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC;gBACjC,cAAc,EAAE,WAAW,CAAC,cAAc;gBAC1C,UAAU,EAAE,WAAW,CAAC,UAAU;aACnC,CAAC,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC;IAC3E,MAAM,4BAA4B,GAAG,KAAK,CAAC,MAAM,CAAoB,IAAI,CAAC,CAAC;IAE3E,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,YAAY,EAAE,KAAK,EAAE,CAAC;QACtB,MAAM,QAAQ,GAAG,kBAAkB,EAAE,CAAC;QACtC,eAAe,CAAC,QAAQ,CAAC,CAAC;QAE1B,OAAO,GAAG,EAAE;YACV,4BAA4B,CAAC,OAAO,EAAE,EAAE,CAAC;YACzC,QAAQ,EAAE,KAAK,EAAE,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;IAExB;;;;;;;;;;;;;OAaG;IACH,IAAI,YAAY,EAAE,CAAC;QACjB,uCAAuC;QACvC,YAAY,EAAE,cAAc,CAAC;YAC3B,KAAK;YACL,UAAU,EAAE,WAAW,CAAC,UAAU;YAClC,cAAc,EAAE,WAAW,CAAC,cAAc;SAC3C,CAAC,CAAC;QACH,uEAAuE;QACvE,4BAA4B,CAAC,OAAO,EAAE,EAAE,CAAC;QACzC,4BAA4B,CAAC,OAAO,GAAG,YAAY,EAAE,gBAAgB,CAAC;YACpE,kBAAkB,EAAE,GAAG,EAAE;gBACvB,qEAAqE;gBACrE,4BAA4B,CAAC,OAAO,EAAE,EAAE,CAAC;gBACzC,4BAA4B,CAAC,OAAO,GAAG,IAAI,CAAC;YAC9C,CAAC;SACF,CAAC,CAAC;IACL,CAAC;IAED,MAAM,6BAA6B,GAAG,CAAC,WAAW,CAAC,cAAc,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,4BAA4B,CAAC,OAAO,CAAC;IACrH,MAAM,MAAM,GAAG,mCAAmC,CAAC,YAAY,CAAC,CAAC;IACjE,OAAO;QACL,GAAG,MAAM;QACT,UAAU,EAAE,MAAM,EAAE,UAAU,IAAI,6BAA6B;KAChE,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { WatchedQuery } from '@powersync/common';
|
|
2
|
+
/**
|
|
3
|
+
* A hook to access and subscribe to the results of an existing {@link WatchedQuery} instance.
|
|
4
|
+
* @example
|
|
5
|
+
* export const ContentComponent = () => {
|
|
6
|
+
* const { data: lists } = useWatchedQuerySubscription(listsQuery);
|
|
7
|
+
*
|
|
8
|
+
* return <View>
|
|
9
|
+
* {lists.map((l) => (
|
|
10
|
+
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
11
|
+
* ))}
|
|
12
|
+
* </View>;
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
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;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* A hook to access and subscribe to the results of an existing {@link WatchedQuery} instance.
|
|
4
|
+
* @example
|
|
5
|
+
* export const ContentComponent = () => {
|
|
6
|
+
* const { data: lists } = useWatchedQuerySubscription(listsQuery);
|
|
7
|
+
*
|
|
8
|
+
* return <View>
|
|
9
|
+
* {lists.map((l) => (
|
|
10
|
+
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
11
|
+
* ))}
|
|
12
|
+
* </View>;
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
*/
|
|
16
|
+
export const useWatchedQuerySubscription = (query) => {
|
|
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
|
|
25
|
+
React.useEffect(() => {
|
|
26
|
+
if (query) {
|
|
27
|
+
setOutputState(query.state);
|
|
28
|
+
return query.registerListener({
|
|
29
|
+
onStateChange: (state) => {
|
|
30
|
+
setOutputState({ ...state });
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}, [query]);
|
|
35
|
+
return output;
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=useWatchedQuerySubscription.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWatchedQuerySubscription.js","sourceRoot":"","sources":["../../../src/hooks/watched/useWatchedQuerySubscription.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAIzC,KAAY,EACI,EAAE;IAClB,OAAO,mCAAmC,CAAC,KAAK,CAAC,CAAC;AACpD,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAIjD,KAAmB,EACS,EAAE;IAC9B,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAE9D,mEAAmE;IACnE,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,KAAK,EAAE,CAAC;YACV,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAE5B,OAAO,KAAK,CAAC,gBAAgB,CAAC;gBAC5B,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE;oBACvB,cAAc,CAAC,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;gBAC/B,CAAC;aACF,CAAC,CAAC;QACL,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { DifferentialWatchedQueryComparator, SQLOnChangeOptions } from '@powersync/common';
|
|
2
|
+
import { UseSyncStreamOptions } from '../streams.js';
|
|
3
|
+
export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
|
|
4
|
+
/**
|
|
5
|
+
* An optional array of sync streams (with names and parameters) backing the query.
|
|
6
|
+
*
|
|
7
|
+
* When set, `useQuery` will subscribe to those streams (and automatically handle unsubscribing from them, too).
|
|
8
|
+
*
|
|
9
|
+
* If {@link QuerySyncStreamOptions} is set on a stream, `useQuery` will remain in a loading state until that stream
|
|
10
|
+
* is marked as {@link SyncSubscriptionDescription.hasSynced}. This ensures the query is not missing rows that haven't
|
|
11
|
+
* been downloaded.
|
|
12
|
+
* Note however that after an initial sync, the query will not block itself while new rows are downloading. Instead,
|
|
13
|
+
* consistent sync snapshots will be made available as they've been processed by PowerSync.
|
|
14
|
+
*
|
|
15
|
+
* @experimental Sync streams are currently in alpha.
|
|
16
|
+
*/
|
|
17
|
+
streams?: QuerySyncStreamOptions[];
|
|
18
|
+
reportFetching?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Additional options to control how `useQuery` behaves when subscribing to a stream.
|
|
22
|
+
*/
|
|
23
|
+
export interface QuerySyncStreamOptions extends UseSyncStreamOptions {
|
|
24
|
+
/**
|
|
25
|
+
* When set to `true`, a `useQuery` hook will remain in a loading state as long as the stream is resolving or
|
|
26
|
+
* downloading for the first time (in other words, until {@link SyncSubscriptionDescription.hasSynced} is true).
|
|
27
|
+
*/
|
|
28
|
+
waitForStream?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface AdditionalOptions extends HookWatchOptions {
|
|
31
|
+
runQueryOnce?: boolean;
|
|
32
|
+
}
|
|
33
|
+
export interface DifferentialHookOptions<RowType> extends HookWatchOptions {
|
|
34
|
+
/**
|
|
35
|
+
* Used to compare result sets.
|
|
36
|
+
*
|
|
37
|
+
* By default the hook will requery on any dependent table change. This will
|
|
38
|
+
* emit a new hook result even if the result set has not changed.
|
|
39
|
+
*
|
|
40
|
+
* Specifying a {@link DifferentialWatchedQueryComparator} will remove emissions for
|
|
41
|
+
* unchanged result sets.
|
|
42
|
+
* Furthermore, emitted `data` arrays will preserve object references between result set emissions
|
|
43
|
+
* for unchanged rows.
|
|
44
|
+
* @example
|
|
45
|
+
* ```javascript
|
|
46
|
+
* {
|
|
47
|
+
* rowComparator: {
|
|
48
|
+
* keyBy: (item) => item.id,
|
|
49
|
+
* compareBy: (item) => JSON.stringify(item)
|
|
50
|
+
* }
|
|
51
|
+
* }
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
|
|
55
|
+
}
|
|
56
|
+
export type ReadonlyQueryResult<RowType> = {
|
|
57
|
+
readonly data: ReadonlyArray<Readonly<RowType>>;
|
|
58
|
+
/**
|
|
59
|
+
* 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.
|
|
60
|
+
*/
|
|
61
|
+
readonly isLoading: boolean;
|
|
62
|
+
/**
|
|
63
|
+
* 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).
|
|
64
|
+
*/
|
|
65
|
+
readonly isFetching: boolean;
|
|
66
|
+
readonly error: Error | undefined;
|
|
67
|
+
/**
|
|
68
|
+
* Function used to run the query again.
|
|
69
|
+
*/
|
|
70
|
+
refresh?: (signal?: AbortSignal) => Promise<void>;
|
|
71
|
+
};
|
|
72
|
+
export type QueryResult<RowType> = {
|
|
73
|
+
data: RowType[];
|
|
74
|
+
/**
|
|
75
|
+
* 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.
|
|
76
|
+
*/
|
|
77
|
+
isLoading: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* 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).
|
|
80
|
+
*/
|
|
81
|
+
isFetching: boolean;
|
|
82
|
+
error: Error | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Function used to run the query again.
|
|
85
|
+
*/
|
|
86
|
+
refresh?: (signal?: AbortSignal) => Promise<void>;
|
|
87
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"watch-types.js","sourceRoot":"","sources":["../../../src/hooks/watched/watch-types.ts"],"names":[],"mappings":""}
|