@powersync/react 0.0.0-dev-20250715080712 → 0.0.0-dev-20250728083821

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +1 -121
  2. package/lib/QueryStore.d.ts +6 -5
  3. package/lib/QueryStore.js +6 -32
  4. package/lib/WatchedQuery.d.ts +36 -0
  5. package/lib/WatchedQuery.js +148 -0
  6. package/lib/hooks/{deprecated/usePowerSyncQuery.js → usePowerSyncQuery.js} +1 -1
  7. package/lib/hooks/{deprecated/usePowerSyncStatus.js → usePowerSyncStatus.js} +1 -1
  8. package/lib/hooks/{deprecated/usePowerSyncWatchedQuery.js → usePowerSyncWatchedQuery.js} +1 -1
  9. package/lib/hooks/useQuery.d.ts +34 -0
  10. package/lib/hooks/useQuery.js +126 -0
  11. package/lib/hooks/useStatus.js +2 -2
  12. package/lib/hooks/useSuspenseQuery.d.ts +25 -0
  13. package/lib/hooks/useSuspenseQuery.js +74 -0
  14. package/lib/index.d.ts +5 -9
  15. package/lib/index.js +5 -7
  16. package/package.json +3 -6
  17. package/lib/hooks/suspense/SuspenseQueryResult.d.ts +0 -3
  18. package/lib/hooks/suspense/SuspenseQueryResult.js +0 -1
  19. package/lib/hooks/suspense/suspense-utils.d.ts +0 -19
  20. package/lib/hooks/suspense/suspense-utils.js +0 -78
  21. package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +0 -10
  22. package/lib/hooks/suspense/useSingleSuspenseQuery.js +0 -75
  23. package/lib/hooks/suspense/useSuspenseQuery.d.ts +0 -53
  24. package/lib/hooks/suspense/useSuspenseQuery.js +0 -10
  25. package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.d.ts +0 -23
  26. package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +0 -56
  27. package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +0 -6
  28. package/lib/hooks/suspense/useWatchedSuspenseQuery.js +0 -25
  29. package/lib/hooks/watched/useQuery.d.ts +0 -38
  30. package/lib/hooks/watched/useQuery.js +0 -32
  31. package/lib/hooks/watched/useSingleQuery.d.ts +0 -3
  32. package/lib/hooks/watched/useSingleQuery.js +0 -52
  33. package/lib/hooks/watched/useWatchedQuery.d.ts +0 -13
  34. package/lib/hooks/watched/useWatchedQuery.js +0 -43
  35. package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +0 -16
  36. package/lib/hooks/watched/useWatchedQuerySubscription.js +0 -29
  37. package/lib/hooks/watched/watch-types.d.ts +0 -60
  38. package/lib/hooks/watched/watch-types.js +0 -1
  39. package/lib/hooks/watched/watch-utils.d.ts +0 -12
  40. package/lib/hooks/watched/watch-utils.js +0 -56
  41. /package/lib/hooks/{deprecated/usePowerSyncQuery.d.ts → usePowerSyncQuery.d.ts} +0 -0
  42. /package/lib/hooks/{deprecated/usePowerSyncStatus.d.ts → usePowerSyncStatus.d.ts} +0 -0
  43. /package/lib/hooks/{deprecated/usePowerSyncWatchedQuery.d.ts → usePowerSyncWatchedQuery.d.ts} +0 -0
@@ -1,43 +0,0 @@
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
- };
@@ -1,16 +0,0 @@
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"];
@@ -1,29 +0,0 @@
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
- };
@@ -1,60 +0,0 @@
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
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,12 +0,0 @@
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
- };
@@ -1,56 +0,0 @@
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
- };