@powersync/react 0.0.0-dev-20250925184532 → 0.0.0-dev-20251003085035
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/hooks/streams.d.ts +31 -0
- package/lib/hooks/streams.js +92 -0
- package/lib/hooks/watched/useQuery.js +14 -4
- package/lib/hooks/watched/watch-types.d.ts +25 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase, SyncStreamStatus, SyncStreamSubscribeOptions } from '@powersync/common';
|
|
2
|
+
import { QuerySyncStreamOptions } from './watched/watch-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A sync stream to subscribe to in {@link useSyncStream}.
|
|
5
|
+
*
|
|
6
|
+
* For more details on sync streams, see the [documentation](https://docs.powersync.com/usage/sync-streams).
|
|
7
|
+
*/
|
|
8
|
+
export interface UseSyncStreamOptions extends SyncStreamSubscribeOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The name of the stream to subscribe to.
|
|
11
|
+
*/
|
|
12
|
+
name: string;
|
|
13
|
+
/**
|
|
14
|
+
* Parameters for the stream subscription. A single stream can have multiple subscriptions with different parameter
|
|
15
|
+
* sets.
|
|
16
|
+
*/
|
|
17
|
+
parameters?: Record<string, any> | null;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a PowerSync stream subscription. The subscription is kept alive as long as the React component calling this
|
|
21
|
+
* function. When it unmounts, {@link SyncStreamSubscription.unsubscribe} is called
|
|
22
|
+
*
|
|
23
|
+
* For more details on sync streams, see the [documentation](https://docs.powersync.com/usage/sync-streams).
|
|
24
|
+
*
|
|
25
|
+
* @returns The status for that stream, or `null` if the stream is currently being resolved.
|
|
26
|
+
*/
|
|
27
|
+
export declare function useSyncStream(options: UseSyncStreamOptions): SyncStreamStatus | null;
|
|
28
|
+
/**
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
export declare function useAllSyncStreamsHaveSynced(db: AbstractPowerSyncDatabase, streams: QuerySyncStreamOptions[] | undefined): boolean;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { usePowerSync } from './PowerSyncContext.js';
|
|
3
|
+
import { useStatus } from './useStatus.js';
|
|
4
|
+
/**
|
|
5
|
+
* Creates a PowerSync stream subscription. The subscription is kept alive as long as the React component calling this
|
|
6
|
+
* function. When it unmounts, {@link SyncStreamSubscription.unsubscribe} is called
|
|
7
|
+
*
|
|
8
|
+
* For more details on sync streams, see the [documentation](https://docs.powersync.com/usage/sync-streams).
|
|
9
|
+
*
|
|
10
|
+
* @returns The status for that stream, or `null` if the stream is currently being resolved.
|
|
11
|
+
*/
|
|
12
|
+
export function useSyncStream(options) {
|
|
13
|
+
const { name, parameters } = options;
|
|
14
|
+
const db = usePowerSync();
|
|
15
|
+
const status = useStatus();
|
|
16
|
+
const [subscription, setSubscription] = useState(null);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
let active = true;
|
|
19
|
+
let subscription = null;
|
|
20
|
+
db.syncStream(name, parameters)
|
|
21
|
+
.subscribe(options)
|
|
22
|
+
.then((sub) => {
|
|
23
|
+
if (active) {
|
|
24
|
+
subscription = sub;
|
|
25
|
+
setSubscription(sub);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// The cleanup function already ran, unsubscribe immediately.
|
|
29
|
+
sub.unsubscribe();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return () => {
|
|
33
|
+
active = false;
|
|
34
|
+
// If we don't have a subscription yet, it'll still get cleaned up once the promise resolves because we've set
|
|
35
|
+
// active to false.
|
|
36
|
+
subscription?.unsubscribe();
|
|
37
|
+
};
|
|
38
|
+
}, [name, parameters]);
|
|
39
|
+
return subscription && status.forStream(subscription);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export function useAllSyncStreamsHaveSynced(db, streams) {
|
|
45
|
+
// Since streams are a user-supplied array, they will likely be different each time this function is called. We don't
|
|
46
|
+
// want to update underlying subscriptions each time, though.
|
|
47
|
+
const hash = useMemo(() => streams && JSON.stringify(streams), [streams]);
|
|
48
|
+
const [synced, setHasSynced] = useState(streams == null || streams.every((e) => e.waitForStream != true));
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (streams) {
|
|
51
|
+
setHasSynced(false);
|
|
52
|
+
const promises = [];
|
|
53
|
+
const abort = new AbortController();
|
|
54
|
+
for (const stream of streams) {
|
|
55
|
+
promises.push(db.syncStream(stream.name, stream.parameters).subscribe(stream));
|
|
56
|
+
}
|
|
57
|
+
// First, wait for all subscribe() calls to make all subscriptions active.
|
|
58
|
+
Promise.all(promises).then(async (resolvedStreams) => {
|
|
59
|
+
function allHaveSynced(status) {
|
|
60
|
+
return resolvedStreams.every((s, i) => {
|
|
61
|
+
const request = streams[i];
|
|
62
|
+
return !request.waitForStream || status.forStream(s)?.subscription?.hasSynced;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
// Wait for the effect to be cancelled or all streams having synced.
|
|
66
|
+
await db.waitForStatus(allHaveSynced, abort.signal);
|
|
67
|
+
if (abort.signal.aborted) {
|
|
68
|
+
// Was cancelled
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Has synced, update public state.
|
|
72
|
+
setHasSynced(true);
|
|
73
|
+
// Wait for cancellation before clearing subscriptions.
|
|
74
|
+
await new Promise((resolve) => {
|
|
75
|
+
abort.signal.addEventListener('abort', () => resolve());
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
// Effect was definitely cancelled at this point, so drop the subscriptions.
|
|
79
|
+
for (const stream of resolvedStreams) {
|
|
80
|
+
stream.unsubscribe();
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return () => abort.abort();
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
// There are no streams, so all of them have synced.
|
|
87
|
+
setHasSynced(true);
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}, [hash]);
|
|
91
|
+
return synced;
|
|
92
|
+
}
|
|
@@ -2,18 +2,24 @@ import { usePowerSync } from '../PowerSyncContext.js';
|
|
|
2
2
|
import { useSingleQuery } from './useSingleQuery.js';
|
|
3
3
|
import { useWatchedQuery } from './useWatchedQuery.js';
|
|
4
4
|
import { constructCompatibleQuery } from './watch-utils.js';
|
|
5
|
+
import { useAllSyncStreamsHaveSynced } from '../streams.js';
|
|
5
6
|
export function useQuery(query, parameters = [], options = {}) {
|
|
6
7
|
const powerSync = usePowerSync();
|
|
7
8
|
if (!powerSync) {
|
|
8
|
-
return {
|
|
9
|
+
return {
|
|
10
|
+
..._loadingState,
|
|
11
|
+
isLoading: false,
|
|
12
|
+
error: new Error('PowerSync not configured.')
|
|
13
|
+
};
|
|
9
14
|
}
|
|
10
15
|
const { parsedQuery, queryChanged } = constructCompatibleQuery(query, parameters, options);
|
|
16
|
+
const streamsHaveSynced = useAllSyncStreamsHaveSynced(powerSync, options?.streams);
|
|
11
17
|
const runOnce = options?.runQueryOnce == true;
|
|
12
18
|
const single = useSingleQuery({
|
|
13
19
|
query: parsedQuery,
|
|
14
20
|
powerSync,
|
|
15
21
|
queryChanged,
|
|
16
|
-
active: runOnce
|
|
22
|
+
active: runOnce && streamsHaveSynced
|
|
17
23
|
});
|
|
18
24
|
const watched = useWatchedQuery({
|
|
19
25
|
query: parsedQuery,
|
|
@@ -26,7 +32,11 @@ export function useQuery(query, parameters = [], options = {}) {
|
|
|
26
32
|
// We emit new data for each table change by default.
|
|
27
33
|
rowComparator: options.rowComparator
|
|
28
34
|
},
|
|
29
|
-
active: !runOnce
|
|
35
|
+
active: !runOnce && streamsHaveSynced
|
|
30
36
|
});
|
|
31
|
-
|
|
37
|
+
if (!streamsHaveSynced) {
|
|
38
|
+
return { ..._loadingState };
|
|
39
|
+
}
|
|
40
|
+
return (runOnce ? single : watched) ?? _loadingState;
|
|
32
41
|
}
|
|
42
|
+
const _loadingState = { isLoading: true, isFetching: false, data: [], error: undefined };
|
|
@@ -1,7 +1,32 @@
|
|
|
1
1
|
import { DifferentialWatchedQueryComparator, SQLOnChangeOptions } from '@powersync/common';
|
|
2
|
+
import { UseSyncStreamOptions } from '../streams.js';
|
|
2
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[];
|
|
3
18
|
reportFetching?: boolean;
|
|
4
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
|
+
}
|
|
5
30
|
export interface AdditionalOptions extends HookWatchOptions {
|
|
6
31
|
runQueryOnce?: boolean;
|
|
7
32
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './hooks/PowerSyncContext.js';
|
|
|
5
5
|
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
6
6
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
7
7
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
8
|
+
export { useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
8
9
|
export { useStatus } from './hooks/useStatus.js';
|
|
9
10
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
10
11
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
package/lib/index.js
CHANGED
|
@@ -4,6 +4,7 @@ export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatched
|
|
|
4
4
|
export * from './hooks/PowerSyncContext.js';
|
|
5
5
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
6
6
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
7
|
+
export { useSyncStream } from './hooks/streams.js';
|
|
7
8
|
export { useStatus } from './hooks/useStatus.js';
|
|
8
9
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
9
10
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
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-20251003085035",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"homepage": "https://docs.powersync.com",
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@powersync/common": "0.0.0-dev-
|
|
27
|
+
"@powersync/common": "0.0.0-dev-20251003085035",
|
|
28
28
|
"react": "*"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"react": "18.3.1",
|
|
36
36
|
"react-dom": "18.3.1",
|
|
37
37
|
"react-error-boundary": "^4.1.0",
|
|
38
|
-
"@powersync/common": "0.0.0-dev-
|
|
39
|
-
"@powersync/web": "0.0.0-dev-
|
|
38
|
+
"@powersync/common": "0.0.0-dev-20251003085035",
|
|
39
|
+
"@powersync/web": "0.0.0-dev-20251003085035",
|
|
40
40
|
"@powersync/drizzle-driver": "0.6.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|