@powersync/react 0.0.0-dev-20260827080125 → 0.0.0-dev-20261020125654
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 +15 -0
- package/lib/QueryStore.d.ts +3 -3
- package/lib/QueryStore.js.map +1 -1
- package/lib/hooks/PowerSyncContext.d.ts +4 -2
- package/lib/hooks/PowerSyncContext.js +7 -1
- package/lib/hooks/PowerSyncContext.js.map +1 -1
- package/lib/hooks/streams.d.ts +4 -3
- package/lib/hooks/streams.js +11 -10
- package/lib/hooks/streams.js.map +1 -1
- package/lib/hooks/suspense/suspense-utils.js +2 -2
- package/lib/hooks/suspense/suspense-utils.js.map +1 -1
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +3 -6
- package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +1 -1
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +1 -4
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +1 -1
- package/lib/hooks/useStatus.d.ts +7 -1
- package/lib/hooks/useStatus.js +11 -6
- package/lib/hooks/useStatus.js.map +1 -1
- package/lib/hooks/watched/QueryRunner.d.ts +79 -0
- package/lib/hooks/watched/QueryRunner.js +340 -0
- package/lib/hooks/watched/QueryRunner.js.map +1 -0
- package/lib/hooks/watched/useQuery.js +14 -40
- package/lib/hooks/watched/useQuery.js.map +1 -1
- package/lib/hooks/watched/useWatchedQuerySubscription.d.ts +1 -1
- package/lib/hooks/watched/useWatchedQuerySubscription.js.map +1 -1
- package/lib/hooks/watched/watch-types.d.ts +22 -3
- package/lib/hooks/watched/watch-utils.d.ts +3 -15
- package/lib/hooks/watched/watch-utils.js +5 -31
- package/lib/hooks/watched/watch-utils.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +1 -1
- package/lib/index.js.map +1 -1
- package/package.json +11 -9
- package/src/QueryStore.ts +4 -4
- package/src/hooks/PowerSyncContext.ts +9 -3
- package/src/hooks/streams.ts +14 -20
- package/src/hooks/suspense/suspense-utils.ts +4 -4
- package/src/hooks/suspense/useSingleSuspenseQuery.ts +4 -7
- package/src/hooks/suspense/useWatchedSuspenseQuery.ts +1 -4
- package/src/hooks/useStatus.ts +21 -7
- package/src/hooks/watched/QueryRunner.ts +455 -0
- package/src/hooks/watched/useQuery.ts +15 -41
- package/src/hooks/watched/useWatchedQuerySubscription.ts +2 -2
- package/src/hooks/watched/watch-types.ts +23 -3
- package/src/hooks/watched/watch-utils.ts +8 -54
- package/src/index.ts +2 -2
- package/lib/hooks/watched/useSingleQuery.d.ts +0 -6
- package/lib/hooks/watched/useSingleQuery.js +0 -59
- package/lib/hooks/watched/useSingleQuery.js.map +0 -1
- package/lib/hooks/watched/useWatchedQuery.d.ts +0 -13
- package/lib/hooks/watched/useWatchedQuery.js +0 -81
- package/lib/hooks/watched/useWatchedQuery.js.map +0 -1
- package/src/hooks/watched/useSingleQuery.ts +0 -67
- package/src/hooks/watched/useWatchedQuery.ts +0 -93
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-20261020125654",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -25,24 +25,26 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://docs.powersync.com",
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@powersync/common": "0.0.0-dev-
|
|
28
|
+
"@powersync/common": "0.0.0-dev-20261020125654",
|
|
29
29
|
"react": "*"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@testing-library/react": "^16.0.0",
|
|
33
|
+
"@types/node": "^24.0.0",
|
|
34
|
+
"better-sqlite3": "^13.0.3",
|
|
33
35
|
"drizzle-orm": "^0.44.7",
|
|
34
36
|
"p-defer": "^4.0.1",
|
|
35
|
-
"@types/react": "^
|
|
36
|
-
"@types/react-dom": "^
|
|
37
|
+
"@types/react": "^19.3.0",
|
|
38
|
+
"@types/react-dom": "^19.3.0",
|
|
37
39
|
"chart.js": "^4.5.0",
|
|
38
40
|
"jsdom": "^24.0.0",
|
|
39
|
-
"react": "
|
|
40
|
-
"react-dom": "
|
|
41
|
+
"react": "19.3.0",
|
|
42
|
+
"react-dom": "19.3.0",
|
|
41
43
|
"react-error-boundary": "^4.1.0",
|
|
42
|
-
"@powersync/shared-internals": "0.0.0-dev-
|
|
43
|
-
"@powersync/common": "0.0.0-dev-
|
|
44
|
+
"@powersync/shared-internals": "0.0.0-dev-20261020125654",
|
|
45
|
+
"@powersync/common": "0.0.0-dev-20261020125654",
|
|
44
46
|
"@powersync/drizzle-driver": "0.8.0",
|
|
45
|
-
"@powersync/
|
|
47
|
+
"@powersync/node": "0.0.0-dev-20261020125654"
|
|
46
48
|
},
|
|
47
49
|
"scripts": {
|
|
48
50
|
"build": "tsc -b",
|
package/src/QueryStore.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
CommonPowerSyncDatabase,
|
|
3
3
|
WatchCompatibleQuery,
|
|
4
4
|
WatchedQuery,
|
|
5
5
|
WatchedQueryListenerEvent
|
|
@@ -17,7 +17,7 @@ export function generateQueryKey(
|
|
|
17
17
|
export class QueryStore {
|
|
18
18
|
cache = new Map<string, WatchedQuery<unknown>>();
|
|
19
19
|
|
|
20
|
-
constructor(private db:
|
|
20
|
+
constructor(private db: CommonPowerSyncDatabase) {}
|
|
21
21
|
|
|
22
22
|
getQuery<RowType>(key: string, query: WatchCompatibleQuery<RowType[]>, options: DifferentialHookOptions<RowType>) {
|
|
23
23
|
if (this.cache.has(key)) {
|
|
@@ -67,9 +67,9 @@ export class QueryStore {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
|
-
let queryStores: WeakMap<
|
|
70
|
+
let queryStores: WeakMap<CommonPowerSyncDatabase, QueryStore> | undefined = undefined;
|
|
71
71
|
|
|
72
|
-
export function getQueryStore(db:
|
|
72
|
+
export function getQueryStore(db: CommonPowerSyncDatabase): QueryStore {
|
|
73
73
|
queryStores ||= new WeakMap();
|
|
74
74
|
const existing = queryStores.get(db);
|
|
75
75
|
if (existing) {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CommonPowerSyncDatabase } from '@powersync/common';
|
|
2
2
|
import React from 'react';
|
|
3
3
|
|
|
4
|
-
export const PowerSyncContext = React.createContext<
|
|
4
|
+
export const PowerSyncContext = React.createContext<CommonPowerSyncDatabase | null>(null);
|
|
5
5
|
/**
|
|
6
6
|
* Custom hook that provides access to the PowerSync context.
|
|
7
7
|
* @returns The PowerSync Database instance.
|
|
@@ -19,4 +19,10 @@ export const PowerSyncContext = React.createContext<AbstractPowerSyncDatabase |
|
|
|
19
19
|
* </ul>
|
|
20
20
|
* };
|
|
21
21
|
*/
|
|
22
|
-
export
|
|
22
|
+
export function usePowerSync() {
|
|
23
|
+
const db = usePowerSyncOrNull();
|
|
24
|
+
if (db == null) throw new Error('PowerSync not configured.');
|
|
25
|
+
return db;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const usePowerSyncOrNull = () => React.useContext(PowerSyncContext);
|
package/src/hooks/streams.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import { useEffect, useMemo
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
SyncStatus,
|
|
6
|
-
SyncStreamStatus,
|
|
7
|
-
SyncStreamSubscribeOptions,
|
|
8
|
-
SyncStreamSubscription
|
|
9
|
-
} from '@powersync/common';
|
|
10
|
-
import { useStatus } from './useStatus.js';
|
|
1
|
+
import { useEffect, useMemo } from 'react';
|
|
2
|
+
import { usePowerSyncOrNull } from './PowerSyncContext.js';
|
|
3
|
+
import { SyncStreamStatus, SyncStreamSubscribeOptions, SyncStreamSubscription } from '@powersync/common';
|
|
4
|
+
import { useStatusOrNull } from './useStatus.js';
|
|
11
5
|
import { QuerySyncStreamOptions } from './watched/watch-types.js';
|
|
12
6
|
|
|
13
7
|
/**
|
|
@@ -44,20 +38,20 @@ export function useSyncStream(options: UseSyncStreamOptions): SyncStreamStatus |
|
|
|
44
38
|
* React component calling this function. When it unmounts, or when the streams array contents
|
|
45
39
|
* change, all previous subscriptions are unsubscribed before new ones are created.
|
|
46
40
|
*/
|
|
47
|
-
export function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStreamStatus[] {
|
|
48
|
-
const db =
|
|
49
|
-
const status =
|
|
41
|
+
export function useSyncStreams(streamOptions: UseSyncStreamOptions[]): (SyncStreamStatus | null)[] {
|
|
42
|
+
const db = usePowerSyncOrNull();
|
|
43
|
+
const status = useStatusOrNull();
|
|
50
44
|
|
|
51
45
|
const stringifiedOptions = useMemo(() => JSON.stringify(streamOptions), [streamOptions]);
|
|
52
46
|
const syncStreams = useMemo(
|
|
53
47
|
() =>
|
|
54
48
|
streamOptions.map((options) => {
|
|
55
49
|
return {
|
|
56
|
-
stream: db
|
|
50
|
+
stream: db?.syncStream(options.name, options.parameters ?? undefined),
|
|
57
51
|
options
|
|
58
52
|
};
|
|
59
53
|
}),
|
|
60
|
-
[stringifiedOptions]
|
|
54
|
+
[db, stringifiedOptions]
|
|
61
55
|
);
|
|
62
56
|
|
|
63
57
|
useEffect(() => {
|
|
@@ -65,7 +59,7 @@ export function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStrea
|
|
|
65
59
|
const resolvedSubs: SyncStreamSubscription[] = [];
|
|
66
60
|
|
|
67
61
|
for (const entry of syncStreams) {
|
|
68
|
-
entry.stream
|
|
62
|
+
entry.stream?.subscribe(entry.options).then((sub) => {
|
|
69
63
|
if (active) {
|
|
70
64
|
resolvedSubs.push(sub);
|
|
71
65
|
} else {
|
|
@@ -81,11 +75,11 @@ export function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStrea
|
|
|
81
75
|
sub.unsubscribe();
|
|
82
76
|
}
|
|
83
77
|
};
|
|
84
|
-
}, [stringifiedOptions]);
|
|
78
|
+
}, [db, stringifiedOptions]);
|
|
85
79
|
|
|
86
80
|
return useMemo(
|
|
87
|
-
() => syncStreams.map((entry) => status
|
|
88
|
-
[status,
|
|
81
|
+
() => syncStreams.map((entry) => (entry.stream && status?.forStream(entry.stream)) ?? null),
|
|
82
|
+
[status, syncStreams]
|
|
89
83
|
);
|
|
90
84
|
}
|
|
91
85
|
|
|
@@ -93,7 +87,7 @@ export function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStrea
|
|
|
93
87
|
* Returns `true` once all streams in the array have synced at least once.
|
|
94
88
|
*/
|
|
95
89
|
export function useAllSyncStreamsHaveSynced(
|
|
96
|
-
|
|
90
|
+
_db: unknown, // TODO (breaking): Remove
|
|
97
91
|
streams: QuerySyncStreamOptions[] | undefined
|
|
98
92
|
): boolean {
|
|
99
93
|
const statuses = useSyncStreams(streams ?? []);
|
|
@@ -9,7 +9,7 @@ import React from 'react';
|
|
|
9
9
|
* Creates a subscription for state change which creates a temporary hold on the query
|
|
10
10
|
*/
|
|
11
11
|
export const useTemporaryHold = (watchedQuery?: WatchedQuery<unknown>) => {
|
|
12
|
-
const releaseTemporaryHold = React.useRef<() => void | undefined>(undefined);
|
|
12
|
+
const releaseTemporaryHold = React.useRef<(() => void) | undefined>(undefined);
|
|
13
13
|
const addedHoldTo = React.useRef<WatchedQuery<unknown> | undefined>(undefined);
|
|
14
14
|
|
|
15
15
|
if (addedHoldTo.current !== watchedQuery) {
|
|
@@ -28,7 +28,7 @@ export const useTemporaryHold = (watchedQuery?: WatchedQuery<unknown>) => {
|
|
|
28
28
|
onStateChange: (state) => {}
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
let timeout: ReturnType<typeof setTimeout
|
|
31
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
32
32
|
|
|
33
33
|
const disposeClosedListener = watchedQuery.registerListener({
|
|
34
34
|
closed: () => {
|
|
@@ -53,12 +53,12 @@ export const useTemporaryHold = (watchedQuery?: WatchedQuery<unknown>) => {
|
|
|
53
53
|
releaseHold();
|
|
54
54
|
} else {
|
|
55
55
|
// Need to keep the hold, check again after timeout
|
|
56
|
-
setTimeout(checkHold, timeoutPollMs);
|
|
56
|
+
timeout = setTimeout(checkHold, timeoutPollMs);
|
|
57
57
|
}
|
|
58
58
|
};
|
|
59
59
|
|
|
60
60
|
// Set a timeout to conditionally remove the temporary hold
|
|
61
|
-
setTimeout(checkHold, timeoutPollMs);
|
|
61
|
+
timeout = setTimeout(checkHold, timeoutPollMs);
|
|
62
62
|
}
|
|
63
63
|
};
|
|
64
64
|
|
|
@@ -19,9 +19,6 @@ export const useSingleSuspenseQuery = <T = any>(
|
|
|
19
19
|
options: AdditionalOptions = {}
|
|
20
20
|
): SuspenseQueryResult<T> => {
|
|
21
21
|
const powerSync = usePowerSync();
|
|
22
|
-
if (!powerSync) {
|
|
23
|
-
throw new Error('PowerSync not configured.');
|
|
24
|
-
}
|
|
25
22
|
|
|
26
23
|
// Manually track data for single queries
|
|
27
24
|
const [data, setData] = React.useState<T[] | null>(null);
|
|
@@ -29,14 +26,14 @@ export const useSingleSuspenseQuery = <T = any>(
|
|
|
29
26
|
|
|
30
27
|
// Note, we don't need to check if the query changed since we fetch the WatchedQuery
|
|
31
28
|
// from the store given these query params
|
|
32
|
-
const
|
|
29
|
+
const parsedQuery = constructCompatibleQuery(query, parameters);
|
|
33
30
|
const { sql: parsedSql, parameters: parsedParameters } = parsedQuery.compile();
|
|
34
31
|
|
|
35
32
|
const key = generateQueryKey(parsedSql, parsedParameters, options);
|
|
36
33
|
const store = getQueryStore(powerSync);
|
|
37
34
|
|
|
38
35
|
// Only use a temporary watched query if we don't have data yet.
|
|
39
|
-
const watchedQuery = data ?
|
|
36
|
+
const watchedQuery = data ? undefined : (store.getQuery(key, parsedQuery, options) as WatchedQuery<T[]>);
|
|
40
37
|
useTemporaryHold(watchedQuery);
|
|
41
38
|
React.useEffect(() => {
|
|
42
39
|
// Set the initial yielded data
|
|
@@ -64,13 +61,13 @@ export const useSingleSuspenseQuery = <T = any>(
|
|
|
64
61
|
parameters: [...compiledQuery.parameters],
|
|
65
62
|
db: powerSync
|
|
66
63
|
});
|
|
67
|
-
if (signal
|
|
64
|
+
if (signal?.aborted) {
|
|
68
65
|
return; // Abort if the signal is already aborted
|
|
69
66
|
}
|
|
70
67
|
setData(result);
|
|
71
68
|
setError(null);
|
|
72
69
|
} catch (e) {
|
|
73
|
-
setError(e);
|
|
70
|
+
setError(e as Error);
|
|
74
71
|
}
|
|
75
72
|
}
|
|
76
73
|
};
|
|
@@ -14,13 +14,10 @@ export const useWatchedSuspenseQuery = <T = any>(
|
|
|
14
14
|
options: AdditionalOptions = {}
|
|
15
15
|
) => {
|
|
16
16
|
const powerSync = usePowerSync();
|
|
17
|
-
if (!powerSync) {
|
|
18
|
-
throw new Error('PowerSync not configured.');
|
|
19
|
-
}
|
|
20
17
|
|
|
21
18
|
// Note, we don't need to check if the query changed since we fetch the WatchedQuery
|
|
22
19
|
// from the store given these query params
|
|
23
|
-
const
|
|
20
|
+
const parsedQuery = constructCompatibleQuery(query, parameters);
|
|
24
21
|
const { sql: parsedSql, parameters: parsedParameters } = parsedQuery.compile();
|
|
25
22
|
|
|
26
23
|
const key = generateQueryKey(parsedSql, parsedParameters, options);
|
package/src/hooks/useStatus.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { usePowerSync, usePowerSyncOrNull } from './PowerSyncContext.js';
|
|
3
|
+
import { SyncStatus } from '@powersync/common';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Custom hook that provides access to the current status of PowerSync.
|
|
@@ -15,12 +16,25 @@ import { PowerSyncContext } from './PowerSyncContext.js';
|
|
|
15
16
|
* </div>
|
|
16
17
|
* };
|
|
17
18
|
*/
|
|
18
|
-
export function useStatus() {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
export function useStatus(): SyncStatus {
|
|
20
|
+
return useStatusOrNull(false);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
export function useStatusOrNull(allowNull?: boolean): SyncStatus | null;
|
|
27
|
+
export function useStatusOrNull(allowNull: false): SyncStatus;
|
|
28
|
+
|
|
29
|
+
export function useStatusOrNull(allowNull: boolean = true): SyncStatus | null {
|
|
30
|
+
// Conditional hook is okay, this is an internal hook and allowNull is a constant on each call site.
|
|
31
|
+
const powerSync = allowNull ? usePowerSyncOrNull() : usePowerSync();
|
|
32
|
+
const [syncStatus, setSyncStatus] = useState(powerSync?.currentStatus);
|
|
21
33
|
|
|
22
34
|
useEffect(() => {
|
|
23
|
-
|
|
35
|
+
setSyncStatus(powerSync?.currentStatus);
|
|
36
|
+
|
|
37
|
+
const listener = powerSync?.registerListener({
|
|
24
38
|
statusChanged: (status) => {
|
|
25
39
|
setSyncStatus(status);
|
|
26
40
|
}
|
|
@@ -29,5 +43,5 @@ export function useStatus() {
|
|
|
29
43
|
return () => listener?.();
|
|
30
44
|
}, [powerSync]);
|
|
31
45
|
|
|
32
|
-
return syncStatus;
|
|
46
|
+
return syncStatus ?? null;
|
|
33
47
|
}
|