@powersync/react 0.0.0-dev-20250318143512 → 0.0.0-dev-20250715080712
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 +121 -1
- package/lib/QueryStore.d.ts +5 -6
- package/lib/QueryStore.js +32 -6
- package/lib/hooks/{usePowerSyncQuery.js → deprecated/usePowerSyncQuery.js} +1 -1
- package/lib/hooks/{usePowerSyncStatus.js → deprecated/usePowerSyncStatus.js} +1 -1
- package/lib/hooks/{usePowerSyncWatchedQuery.js → deprecated/usePowerSyncWatchedQuery.js} +1 -1
- package/lib/hooks/suspense/SuspenseQueryResult.d.ts +3 -0
- package/lib/hooks/suspense/SuspenseQueryResult.js +1 -0
- package/lib/hooks/suspense/suspense-utils.d.ts +19 -0
- package/lib/hooks/suspense/suspense-utils.js +78 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +10 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +75 -0
- package/lib/hooks/suspense/useSuspenseQuery.d.ts +53 -0
- package/lib/hooks/suspense/useSuspenseQuery.js +10 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.d.ts +23 -0
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +56 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +6 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +25 -0
- package/lib/hooks/useStatus.js +2 -2
- package/lib/hooks/watched/useQuery.d.ts +38 -0
- package/lib/hooks/watched/useQuery.js +32 -0
- package/lib/hooks/watched/useSingleQuery.d.ts +3 -0
- package/lib/hooks/watched/useSingleQuery.js +52 -0
- package/lib/hooks/watched/useWatchedQuery.d.ts +13 -0
- package/lib/hooks/watched/useWatchedQuery.js +43 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +16 -0
- package/lib/hooks/watched/useWatchedQuerySubscription.js +29 -0
- package/lib/hooks/watched/watch-types.d.ts +60 -0
- package/lib/hooks/watched/watch-types.js +1 -0
- package/lib/hooks/watched/watch-utils.d.ts +12 -0
- package/lib/hooks/watched/watch-utils.js +56 -0
- package/lib/index.d.ts +9 -5
- package/lib/index.js +7 -5
- package/package.json +8 -5
- package/lib/WatchedQuery.d.ts +0 -35
- package/lib/WatchedQuery.js +0 -145
- package/lib/hooks/useQuery.d.ts +0 -34
- package/lib/hooks/useQuery.js +0 -125
- package/lib/hooks/useSuspenseQuery.d.ts +0 -25
- package/lib/hooks/useSuspenseQuery.js +0 -74
- /package/lib/hooks/{usePowerSyncQuery.d.ts → deprecated/usePowerSyncQuery.d.ts} +0 -0
- /package/lib/hooks/{usePowerSyncStatus.d.ts → deprecated/usePowerSyncStatus.d.ts} +0 -0
- /package/lib/hooks/{usePowerSyncWatchedQuery.d.ts → deprecated/usePowerSyncWatchedQuery.d.ts} +0 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { usePowerSync } from '../PowerSyncContext';
|
|
2
|
+
import { useSingleQuery } from './useSingleQuery';
|
|
3
|
+
import { useWatchedQuery } from './useWatchedQuery';
|
|
4
|
+
import { constructCompatibleQuery } from './watch-utils';
|
|
5
|
+
export function useQuery(query, parameters = [], options = {}) {
|
|
6
|
+
const powerSync = usePowerSync();
|
|
7
|
+
if (!powerSync) {
|
|
8
|
+
return { isLoading: false, isFetching: false, data: [], error: new Error('PowerSync not configured.') };
|
|
9
|
+
}
|
|
10
|
+
const { parsedQuery, queryChanged } = constructCompatibleQuery(query, parameters, options);
|
|
11
|
+
switch (options?.runQueryOnce) {
|
|
12
|
+
case true:
|
|
13
|
+
return useSingleQuery({
|
|
14
|
+
query: parsedQuery,
|
|
15
|
+
powerSync,
|
|
16
|
+
queryChanged
|
|
17
|
+
});
|
|
18
|
+
default:
|
|
19
|
+
return useWatchedQuery({
|
|
20
|
+
query: parsedQuery,
|
|
21
|
+
powerSync,
|
|
22
|
+
queryChanged,
|
|
23
|
+
options: {
|
|
24
|
+
reportFetching: options.reportFetching,
|
|
25
|
+
// Maintains backwards compatibility with previous versions
|
|
26
|
+
// Differentiation is opt-in by default
|
|
27
|
+
// We emit new data for each table change by default.
|
|
28
|
+
differentiator: options.differentiator
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
export const useSingleQuery = (options) => {
|
|
3
|
+
const { query, powerSync, queryChanged } = options;
|
|
4
|
+
const [output, setOutputState] = React.useState({
|
|
5
|
+
isLoading: true,
|
|
6
|
+
isFetching: true,
|
|
7
|
+
data: [],
|
|
8
|
+
error: undefined
|
|
9
|
+
});
|
|
10
|
+
const runQuery = React.useCallback(async (signal) => {
|
|
11
|
+
setOutputState((prev) => ({ ...prev, isLoading: true, isFetching: true, error: undefined }));
|
|
12
|
+
try {
|
|
13
|
+
const compiledQuery = query.compile();
|
|
14
|
+
const result = await query.execute({
|
|
15
|
+
sql: compiledQuery.sql,
|
|
16
|
+
parameters: [...compiledQuery.parameters],
|
|
17
|
+
db: powerSync
|
|
18
|
+
});
|
|
19
|
+
if (signal.aborted) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
setOutputState((prev) => ({
|
|
23
|
+
...prev,
|
|
24
|
+
isLoading: false,
|
|
25
|
+
isFetching: false,
|
|
26
|
+
data: result,
|
|
27
|
+
error: undefined
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
setOutputState((prev) => ({
|
|
32
|
+
...prev,
|
|
33
|
+
isLoading: false,
|
|
34
|
+
isFetching: false,
|
|
35
|
+
data: [],
|
|
36
|
+
error
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
}, [queryChanged, query]);
|
|
40
|
+
// Trigger initial query execution
|
|
41
|
+
React.useEffect(() => {
|
|
42
|
+
const abortController = new AbortController();
|
|
43
|
+
runQuery(abortController.signal);
|
|
44
|
+
return () => {
|
|
45
|
+
abortController.abort();
|
|
46
|
+
};
|
|
47
|
+
}, [powerSync, queryChanged]);
|
|
48
|
+
return {
|
|
49
|
+
...output,
|
|
50
|
+
refresh: runQuery
|
|
51
|
+
};
|
|
52
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types';
|
|
2
|
+
import { InternalHookOptions } from './watch-utils';
|
|
3
|
+
/**
|
|
4
|
+
* @internal This is not exported from the index.ts
|
|
5
|
+
*
|
|
6
|
+
* When a differential query is used the return type is readonly. This is required
|
|
7
|
+
* since the implementation requires a stable ref.
|
|
8
|
+
* For legacy compatibility we allow mutating when a standard query is used. Mutations should
|
|
9
|
+
* not affect the internal implementation in this case.
|
|
10
|
+
*/
|
|
11
|
+
export declare const useWatchedQuery: <RowType = unknown>(options: InternalHookOptions<RowType[]> & {
|
|
12
|
+
options: DifferentialHookOptions<RowType>;
|
|
13
|
+
}) => QueryResult<RowType> | ReadonlyQueryResult<RowType>;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { useWatchedQuerySubscription } from './useWatchedQuerySubscription';
|
|
3
|
+
/**
|
|
4
|
+
* @internal This is not exported from the index.ts
|
|
5
|
+
*
|
|
6
|
+
* When a differential query is used the return type is readonly. This is required
|
|
7
|
+
* since the implementation requires a stable ref.
|
|
8
|
+
* For legacy compatibility we allow mutating when a standard query is used. Mutations should
|
|
9
|
+
* not affect the internal implementation in this case.
|
|
10
|
+
*/
|
|
11
|
+
export const useWatchedQuery = (options) => {
|
|
12
|
+
const { query, powerSync, queryChanged, options: hookOptions } = options;
|
|
13
|
+
const createWatchedQuery = React.useCallback(() => {
|
|
14
|
+
const watch = hookOptions.differentiator
|
|
15
|
+
? powerSync.customQuery(query).differentialWatch({
|
|
16
|
+
differentiator: hookOptions.differentiator,
|
|
17
|
+
reportFetching: hookOptions.reportFetching,
|
|
18
|
+
throttleMs: hookOptions.throttleMs
|
|
19
|
+
})
|
|
20
|
+
: powerSync.customQuery(query).watch({
|
|
21
|
+
reportFetching: hookOptions.reportFetching,
|
|
22
|
+
throttleMs: hookOptions.throttleMs
|
|
23
|
+
});
|
|
24
|
+
return watch;
|
|
25
|
+
}, []);
|
|
26
|
+
const [watchedQuery, setWatchedQuery] = React.useState(createWatchedQuery);
|
|
27
|
+
React.useEffect(() => {
|
|
28
|
+
watchedQuery.close();
|
|
29
|
+
setWatchedQuery(createWatchedQuery);
|
|
30
|
+
}, [powerSync]);
|
|
31
|
+
// Indicates that the query will be re-fetched due to a change in the query.
|
|
32
|
+
// Used when `isFetching` hasn't been set to true yet due to React execution.
|
|
33
|
+
React.useEffect(() => {
|
|
34
|
+
if (queryChanged) {
|
|
35
|
+
watchedQuery.updateSettings({
|
|
36
|
+
query,
|
|
37
|
+
throttleMs: hookOptions.throttleMs,
|
|
38
|
+
reportFetching: hookOptions.reportFetching
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}, [queryChanged]);
|
|
42
|
+
return useWatchedQuerySubscription(watchedQuery);
|
|
43
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
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"];
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
const [output, setOutputState] = React.useState(query.state);
|
|
18
|
+
React.useEffect(() => {
|
|
19
|
+
const dispose = query.registerListener({
|
|
20
|
+
onStateChange: (state) => {
|
|
21
|
+
setOutputState({ ...state });
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return () => {
|
|
25
|
+
dispose();
|
|
26
|
+
};
|
|
27
|
+
}, [query]);
|
|
28
|
+
return output;
|
|
29
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { SQLOnChangeOptions, WatchedQueryDifferentiator } from '@powersync/common';
|
|
2
|
+
export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
|
|
3
|
+
reportFetching?: boolean;
|
|
4
|
+
}
|
|
5
|
+
export interface AdditionalOptions extends HookWatchOptions {
|
|
6
|
+
runQueryOnce?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface DifferentialHookOptions<RowType> extends HookWatchOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Used to detect and report differences in query result sets.
|
|
11
|
+
*
|
|
12
|
+
* By default the hook will requery on any dependent table change. This will
|
|
13
|
+
* emit a new hook result even if the result set has not changed.
|
|
14
|
+
*
|
|
15
|
+
* Specifying a {@link WatchedQueryDifferentiator} will remove emissions for
|
|
16
|
+
* unchanged result sets and preserve Array object references between result set emissions.
|
|
17
|
+
* @example
|
|
18
|
+
* ```javascript
|
|
19
|
+
* {
|
|
20
|
+
* differentiator: {
|
|
21
|
+
* identify: (item) => item.id,
|
|
22
|
+
* compareBy: (item) => JSON.stringify(item)
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
differentiator?: WatchedQueryDifferentiator<RowType>;
|
|
28
|
+
}
|
|
29
|
+
export type ReadonlyQueryResult<RowType> = {
|
|
30
|
+
readonly data: ReadonlyArray<Readonly<RowType>>;
|
|
31
|
+
/**
|
|
32
|
+
* 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.
|
|
33
|
+
*/
|
|
34
|
+
readonly isLoading: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 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).
|
|
37
|
+
*/
|
|
38
|
+
readonly isFetching: boolean;
|
|
39
|
+
readonly error: Error | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* Function used to run the query again.
|
|
42
|
+
*/
|
|
43
|
+
refresh?: (signal?: AbortSignal) => Promise<void>;
|
|
44
|
+
};
|
|
45
|
+
export type QueryResult<RowType> = {
|
|
46
|
+
data: RowType[];
|
|
47
|
+
/**
|
|
48
|
+
* 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.
|
|
49
|
+
*/
|
|
50
|
+
isLoading: boolean;
|
|
51
|
+
/**
|
|
52
|
+
* 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).
|
|
53
|
+
*/
|
|
54
|
+
isFetching: boolean;
|
|
55
|
+
error: Error | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Function used to run the query again.
|
|
58
|
+
*/
|
|
59
|
+
refresh?: (signal?: AbortSignal) => Promise<void>;
|
|
60
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase, CompilableQuery, WatchCompatibleQuery } from '@powersync/common';
|
|
2
|
+
import { AdditionalOptions } from './watch-types';
|
|
3
|
+
export type InternalHookOptions<DataType> = {
|
|
4
|
+
query: WatchCompatibleQuery<DataType>;
|
|
5
|
+
powerSync: AbstractPowerSyncDatabase;
|
|
6
|
+
queryChanged: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const checkQueryChanged: <T>(query: WatchCompatibleQuery<T>, options: AdditionalOptions) => boolean;
|
|
9
|
+
export declare const constructCompatibleQuery: <RowType>(query: string | CompilableQuery<RowType>, parameters: any[], options: AdditionalOptions) => {
|
|
10
|
+
parsedQuery: WatchCompatibleQuery<RowType[]>;
|
|
11
|
+
queryChanged: boolean;
|
|
12
|
+
};
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { usePowerSync } from '../PowerSyncContext';
|
|
3
|
+
export const checkQueryChanged = (query, options) => {
|
|
4
|
+
let _compiled;
|
|
5
|
+
try {
|
|
6
|
+
_compiled = query.compile();
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
return false; // If compilation fails, we assume the query has changed
|
|
10
|
+
}
|
|
11
|
+
const compiled = _compiled;
|
|
12
|
+
const stringifiedParams = JSON.stringify(compiled.parameters);
|
|
13
|
+
const stringifiedOptions = JSON.stringify(options);
|
|
14
|
+
const previousQueryRef = React.useRef({ sqlStatement: compiled.sql, stringifiedParams, stringifiedOptions });
|
|
15
|
+
if (previousQueryRef.current.sqlStatement !== compiled.sql ||
|
|
16
|
+
previousQueryRef.current.stringifiedParams != stringifiedParams ||
|
|
17
|
+
previousQueryRef.current.stringifiedOptions != stringifiedOptions) {
|
|
18
|
+
previousQueryRef.current.sqlStatement = compiled.sql;
|
|
19
|
+
previousQueryRef.current.stringifiedParams = stringifiedParams;
|
|
20
|
+
previousQueryRef.current.stringifiedOptions = stringifiedOptions;
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
};
|
|
25
|
+
export const constructCompatibleQuery = (query, parameters = [], options) => {
|
|
26
|
+
const powerSync = usePowerSync();
|
|
27
|
+
const parsedQuery = React.useMemo(() => {
|
|
28
|
+
if (typeof query == 'string') {
|
|
29
|
+
return {
|
|
30
|
+
compile: () => ({
|
|
31
|
+
sql: query,
|
|
32
|
+
parameters
|
|
33
|
+
}),
|
|
34
|
+
execute: () => powerSync.getAll(query, parameters)
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
return {
|
|
39
|
+
// Generics differ a bit but holistically this is the same
|
|
40
|
+
compile: () => {
|
|
41
|
+
const compiled = query.compile();
|
|
42
|
+
return {
|
|
43
|
+
sql: compiled.sql,
|
|
44
|
+
parameters: [...compiled.parameters]
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
execute: () => query.execute()
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
}, [query, powerSync]);
|
|
51
|
+
const queryChanged = checkQueryChanged(parsedQuery, options);
|
|
52
|
+
return {
|
|
53
|
+
parsedQuery,
|
|
54
|
+
queryChanged
|
|
55
|
+
};
|
|
56
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
+
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery';
|
|
2
|
+
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus';
|
|
3
|
+
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery';
|
|
1
4
|
export * from './hooks/PowerSyncContext';
|
|
2
|
-
export {
|
|
5
|
+
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult';
|
|
6
|
+
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery';
|
|
7
|
+
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription';
|
|
3
8
|
export { useStatus } from './hooks/useStatus';
|
|
4
|
-
export { useQuery } from './hooks/useQuery';
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
7
|
-
export { usePowerSyncStatus } from './hooks/usePowerSyncStatus';
|
|
9
|
+
export { useQuery } from './hooks/watched/useQuery';
|
|
10
|
+
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription';
|
|
11
|
+
export { AdditionalOptions } from './hooks/watched/watch-types';
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery';
|
|
2
|
+
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus';
|
|
3
|
+
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery';
|
|
1
4
|
export * from './hooks/PowerSyncContext';
|
|
2
|
-
export {
|
|
5
|
+
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery';
|
|
6
|
+
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription';
|
|
3
7
|
export { useStatus } from './hooks/useStatus';
|
|
4
|
-
export { useQuery } from './hooks/useQuery';
|
|
5
|
-
export {
|
|
6
|
-
export { usePowerSyncWatchedQuery } from './hooks/usePowerSyncWatchedQuery';
|
|
7
|
-
export { usePowerSyncStatus } from './hooks/usePowerSyncStatus';
|
|
8
|
+
export { useQuery } from './hooks/watched/useQuery';
|
|
9
|
+
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/react",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20250715080712",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -22,16 +22,19 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://docs.powersync.com",
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@powersync/common": "
|
|
25
|
+
"@powersync/common": "0.0.0-dev-20250715080712",
|
|
26
26
|
"react": "*"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@testing-library/react": "^15.0.2",
|
|
30
|
-
"@types/react": "^18.
|
|
30
|
+
"@types/react": "^18.3.1",
|
|
31
|
+
"chart.js": "^4.5.0",
|
|
31
32
|
"jsdom": "^24.0.0",
|
|
32
|
-
"react": "18.
|
|
33
|
+
"react": "18.3.1",
|
|
34
|
+
"react-dom": "18.3.1",
|
|
33
35
|
"react-error-boundary": "^4.1.0",
|
|
34
|
-
"@powersync/common": "
|
|
36
|
+
"@powersync/common": "0.0.0-dev-20250715080712",
|
|
37
|
+
"@powersync/web": "0.0.0-dev-20250715080712"
|
|
35
38
|
},
|
|
36
39
|
"scripts": {
|
|
37
40
|
"build": "tsc -b",
|
package/lib/WatchedQuery.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { AbstractPowerSyncDatabase, BaseListener, BaseObserver, CompilableQuery, Disposable } from '@powersync/common';
|
|
2
|
-
import { AdditionalOptions } from './hooks/useQuery';
|
|
3
|
-
export declare class Query<T> {
|
|
4
|
-
rawQuery: string | CompilableQuery<T>;
|
|
5
|
-
sqlStatement: string;
|
|
6
|
-
queryParameters: any[];
|
|
7
|
-
}
|
|
8
|
-
export interface WatchedQueryListener extends BaseListener {
|
|
9
|
-
onUpdate: () => void;
|
|
10
|
-
disposed: () => void;
|
|
11
|
-
}
|
|
12
|
-
export declare class WatchedQuery extends BaseObserver<WatchedQueryListener> implements Disposable {
|
|
13
|
-
readyPromise: Promise<void>;
|
|
14
|
-
isReady: boolean;
|
|
15
|
-
currentData: any[] | undefined;
|
|
16
|
-
currentError: any;
|
|
17
|
-
tables: any[] | undefined;
|
|
18
|
-
private temporaryHolds;
|
|
19
|
-
private controller;
|
|
20
|
-
private db;
|
|
21
|
-
private resolveReady;
|
|
22
|
-
readonly query: Query<unknown>;
|
|
23
|
-
readonly options: AdditionalOptions;
|
|
24
|
-
constructor(db: AbstractPowerSyncDatabase, query: Query<unknown>, options: AdditionalOptions);
|
|
25
|
-
addTemporaryHold(): () => void;
|
|
26
|
-
registerListener(listener: Partial<WatchedQueryListener>): () => void;
|
|
27
|
-
private fetchTables;
|
|
28
|
-
fetchData(): Promise<void>;
|
|
29
|
-
private maybeListen;
|
|
30
|
-
private setData;
|
|
31
|
-
private setError;
|
|
32
|
-
private onUpdateListenersCount;
|
|
33
|
-
private maybeDispose;
|
|
34
|
-
dispose(): Promise<void>;
|
|
35
|
-
}
|
package/lib/WatchedQuery.js
DELETED
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { BaseObserver, runOnSchemaChange } from '@powersync/common';
|
|
2
|
-
export class Query {
|
|
3
|
-
rawQuery;
|
|
4
|
-
sqlStatement;
|
|
5
|
-
queryParameters;
|
|
6
|
-
}
|
|
7
|
-
export class WatchedQuery extends BaseObserver {
|
|
8
|
-
readyPromise;
|
|
9
|
-
isReady = false;
|
|
10
|
-
currentData;
|
|
11
|
-
currentError;
|
|
12
|
-
tables;
|
|
13
|
-
temporaryHolds = new Set();
|
|
14
|
-
controller;
|
|
15
|
-
db;
|
|
16
|
-
resolveReady;
|
|
17
|
-
query;
|
|
18
|
-
options;
|
|
19
|
-
constructor(db, query, options) {
|
|
20
|
-
super();
|
|
21
|
-
this.db = db;
|
|
22
|
-
this.query = query;
|
|
23
|
-
this.options = options;
|
|
24
|
-
this.readyPromise = new Promise((resolve) => {
|
|
25
|
-
this.resolveReady = resolve;
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
addTemporaryHold() {
|
|
29
|
-
const ref = new Object();
|
|
30
|
-
this.temporaryHolds.add(ref);
|
|
31
|
-
this.maybeListen();
|
|
32
|
-
let timeout;
|
|
33
|
-
const release = () => {
|
|
34
|
-
this.temporaryHolds.delete(ref);
|
|
35
|
-
if (timeout) {
|
|
36
|
-
clearTimeout(timeout);
|
|
37
|
-
}
|
|
38
|
-
this.maybeDispose();
|
|
39
|
-
};
|
|
40
|
-
const timeoutRelease = () => {
|
|
41
|
-
if (this.isReady || this.controller == null) {
|
|
42
|
-
release();
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
// If the query is taking long, keep the temporary hold.
|
|
46
|
-
timeout = setTimeout(timeoutRelease, 5_000);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
timeout = setTimeout(timeoutRelease, 5_000);
|
|
50
|
-
return release;
|
|
51
|
-
}
|
|
52
|
-
registerListener(listener) {
|
|
53
|
-
const disposer = super.registerListener(listener);
|
|
54
|
-
this.maybeListen();
|
|
55
|
-
return () => {
|
|
56
|
-
disposer();
|
|
57
|
-
this.maybeDispose();
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
async fetchTables() {
|
|
61
|
-
try {
|
|
62
|
-
this.tables = await this.db.resolveTables(this.query.sqlStatement, this.query.queryParameters, this.options);
|
|
63
|
-
}
|
|
64
|
-
catch (e) {
|
|
65
|
-
console.error('Failed to fetch tables:', e);
|
|
66
|
-
this.setError(e);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
async fetchData() {
|
|
70
|
-
try {
|
|
71
|
-
const result = typeof this.query.rawQuery == 'string'
|
|
72
|
-
? await this.db.getAll(this.query.sqlStatement, this.query.queryParameters)
|
|
73
|
-
: await this.query.rawQuery.execute();
|
|
74
|
-
const data = result ?? [];
|
|
75
|
-
this.setData(data);
|
|
76
|
-
}
|
|
77
|
-
catch (e) {
|
|
78
|
-
console.error('Failed to fetch data:', e);
|
|
79
|
-
this.setError(e);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
maybeListen() {
|
|
83
|
-
if (this.controller != null) {
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
if (this.onUpdateListenersCount() == 0 && this.temporaryHolds.size == 0) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
const controller = new AbortController();
|
|
90
|
-
this.controller = controller;
|
|
91
|
-
const onError = (error) => {
|
|
92
|
-
this.setError(error);
|
|
93
|
-
};
|
|
94
|
-
const watchQuery = async (abortSignal) => {
|
|
95
|
-
await this.fetchTables();
|
|
96
|
-
await this.fetchData();
|
|
97
|
-
if (!this.options.runQueryOnce) {
|
|
98
|
-
this.db.onChangeWithCallback({
|
|
99
|
-
onChange: async () => {
|
|
100
|
-
await this.fetchData();
|
|
101
|
-
},
|
|
102
|
-
onError
|
|
103
|
-
}, {
|
|
104
|
-
...this.options,
|
|
105
|
-
signal: abortSignal,
|
|
106
|
-
tables: this.tables
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
runOnSchemaChange(watchQuery, this.db, { signal: this.controller.signal });
|
|
111
|
-
}
|
|
112
|
-
setData(results) {
|
|
113
|
-
this.isReady = true;
|
|
114
|
-
this.currentData = results;
|
|
115
|
-
this.currentError = undefined;
|
|
116
|
-
this.resolveReady?.();
|
|
117
|
-
this.iterateListeners((l) => l.onUpdate?.());
|
|
118
|
-
}
|
|
119
|
-
setError(error) {
|
|
120
|
-
this.isReady = true;
|
|
121
|
-
this.currentData = undefined;
|
|
122
|
-
this.currentError = error;
|
|
123
|
-
this.resolveReady?.();
|
|
124
|
-
this.iterateListeners((l) => l.onUpdate?.());
|
|
125
|
-
}
|
|
126
|
-
onUpdateListenersCount() {
|
|
127
|
-
return Array.from(this.listeners).filter((listener) => listener.onUpdate !== undefined).length;
|
|
128
|
-
}
|
|
129
|
-
maybeDispose() {
|
|
130
|
-
if (this.onUpdateListenersCount() == 0 && this.temporaryHolds.size == 0) {
|
|
131
|
-
this.controller?.abort();
|
|
132
|
-
this.controller = undefined;
|
|
133
|
-
this.isReady = false;
|
|
134
|
-
this.currentData = undefined;
|
|
135
|
-
this.currentError = undefined;
|
|
136
|
-
this.dispose();
|
|
137
|
-
this.readyPromise = new Promise((resolve, reject) => {
|
|
138
|
-
this.resolveReady = resolve;
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
async dispose() {
|
|
143
|
-
this.iterateAsyncListeners(async (l) => l.disposed?.());
|
|
144
|
-
}
|
|
145
|
-
}
|
package/lib/hooks/useQuery.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { type CompilableQuery, type SQLWatchOptions } from '@powersync/common';
|
|
2
|
-
export interface AdditionalOptions extends Omit<SQLWatchOptions, 'signal'> {
|
|
3
|
-
runQueryOnce?: boolean;
|
|
4
|
-
}
|
|
5
|
-
export type QueryResult<T> = {
|
|
6
|
-
data: T[];
|
|
7
|
-
/**
|
|
8
|
-
* 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.
|
|
9
|
-
*/
|
|
10
|
-
isLoading: boolean;
|
|
11
|
-
/**
|
|
12
|
-
* 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).
|
|
13
|
-
*/
|
|
14
|
-
isFetching: boolean;
|
|
15
|
-
error: Error | undefined;
|
|
16
|
-
/**
|
|
17
|
-
* Function used to run the query again.
|
|
18
|
-
*/
|
|
19
|
-
refresh?: (signal?: AbortSignal) => Promise<void>;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* A hook to access the results of a watched query.
|
|
23
|
-
* @example
|
|
24
|
-
* export const Component = () => {
|
|
25
|
-
* const { data: lists } = useQuery('SELECT * from lists');
|
|
26
|
-
*
|
|
27
|
-
* return <View>
|
|
28
|
-
* {lists.map((l) => (
|
|
29
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
30
|
-
* ))}
|
|
31
|
-
* </View>
|
|
32
|
-
* }
|
|
33
|
-
*/
|
|
34
|
-
export declare const useQuery: <T = any>(query: string | CompilableQuery<T>, parameters?: any[], options?: AdditionalOptions) => QueryResult<T>;
|