@powersync/react 2.1.0 → 2.2.0
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/PowerSyncContext.d.ts +6 -1
- package/lib/hooks/PowerSyncContext.js +8 -1
- package/lib/hooks/PowerSyncContext.js.map +1 -1
- package/lib/hooks/useStatus.d.ts +4 -3
- package/lib/hooks/useStatus.js +19 -16
- package/lib/hooks/useStatus.js.map +1 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +2 -2
- package/lib/index.js.map +1 -1
- package/package.json +5 -5
- package/src/hooks/PowerSyncContext.ts +8 -1
- package/src/hooks/useStatus.ts +20 -22
- package/src/index.ts +2 -2
|
@@ -19,4 +19,9 @@ export declare const PowerSyncContext: React.Context<CommonPowerSyncDatabase | n
|
|
|
19
19
|
* };
|
|
20
20
|
*/
|
|
21
21
|
export declare function usePowerSync(): CommonPowerSyncDatabase;
|
|
22
|
-
|
|
22
|
+
/**
|
|
23
|
+
* A variant of {@link usePowerSync} that returns null when no PowerSync database is in context (instead of throwing).
|
|
24
|
+
*
|
|
25
|
+
* @returns The PowerSync database instance, or null.
|
|
26
|
+
*/
|
|
27
|
+
export declare function usePowerSyncOrNull(): CommonPowerSyncDatabase | null;
|
|
@@ -23,5 +23,12 @@ export function usePowerSync() {
|
|
|
23
23
|
throw new Error('PowerSync not configured.');
|
|
24
24
|
return db;
|
|
25
25
|
}
|
|
26
|
-
|
|
26
|
+
/**
|
|
27
|
+
* A variant of {@link usePowerSync} that returns null when no PowerSync database is in context (instead of throwing).
|
|
28
|
+
*
|
|
29
|
+
* @returns The PowerSync database instance, or null.
|
|
30
|
+
*/
|
|
31
|
+
export function usePowerSyncOrNull() {
|
|
32
|
+
return React.useContext(PowerSyncContext);
|
|
33
|
+
}
|
|
27
34
|
//# sourceMappingURL=PowerSyncContext.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PowerSyncContext.js","sourceRoot":"","sources":["../../src/hooks/PowerSyncContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC,aAAa,CAAiC,IAAI,CAAC,CAAC;AAC1F;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,YAAY;IAC1B,MAAM,EAAE,GAAG,kBAAkB,EAAE,CAAC;IAChC,IAAI,EAAE,IAAI,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC7D,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,MAAM,
|
|
1
|
+
{"version":3,"file":"PowerSyncContext.js","sourceRoot":"","sources":["../../src/hooks/PowerSyncContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC,aAAa,CAAiC,IAAI,CAAC,CAAC;AAC1F;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,YAAY;IAC1B,MAAM,EAAE,GAAG,kBAAkB,EAAE,CAAC;IAChC,IAAI,EAAE,IAAI,IAAI;QAAE,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC7D,OAAO,EAAE,CAAC;AACZ,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB;IAChC,OAAO,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;AAC5C,CAAC"}
|
package/lib/hooks/useStatus.d.ts
CHANGED
|
@@ -15,7 +15,8 @@ import { SyncStatus } from '@powersync/common';
|
|
|
15
15
|
*/
|
|
16
16
|
export declare function useStatus(): SyncStatus;
|
|
17
17
|
/**
|
|
18
|
-
* @
|
|
18
|
+
* A variant of {@link useStatus} that returns null when no PowerSync database is in context (instead of throwing).
|
|
19
|
+
*
|
|
20
|
+
* @returns The current sync status of the PowerSync database, or null.
|
|
19
21
|
*/
|
|
20
|
-
export declare function useStatusOrNull(
|
|
21
|
-
export declare function useStatusOrNull(allowNull: false): SyncStatus;
|
|
22
|
+
export declare function useStatusOrNull(): SyncStatus | null;
|
package/lib/hooks/useStatus.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useSyncExternalStore } from 'react';
|
|
2
2
|
import { usePowerSync, usePowerSyncOrNull } from './PowerSyncContext.js';
|
|
3
3
|
/**
|
|
4
4
|
* Custom hook that provides access to the current status of PowerSync.
|
|
@@ -15,21 +15,24 @@ import { usePowerSync, usePowerSyncOrNull } from './PowerSyncContext.js';
|
|
|
15
15
|
* };
|
|
16
16
|
*/
|
|
17
17
|
export function useStatus() {
|
|
18
|
-
return
|
|
18
|
+
return useStatusOf(usePowerSync());
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
});
|
|
31
|
-
return ()
|
|
32
|
-
},
|
|
33
|
-
|
|
20
|
+
/**
|
|
21
|
+
* A variant of {@link useStatus} that returns null when no PowerSync database is in context (instead of throwing).
|
|
22
|
+
*
|
|
23
|
+
* @returns The current sync status of the PowerSync database, or null.
|
|
24
|
+
*/
|
|
25
|
+
export function useStatusOrNull() {
|
|
26
|
+
return useStatusOf(usePowerSyncOrNull());
|
|
27
|
+
}
|
|
28
|
+
function useStatusOf(powerSync) {
|
|
29
|
+
const subscribe = useCallback((listener) => {
|
|
30
|
+
const dispose = powerSync?.registerListener({ statusChanged: listener });
|
|
31
|
+
return dispose ?? function () { };
|
|
32
|
+
},
|
|
33
|
+
// Ensure React treats changed databases as new stores
|
|
34
|
+
[powerSync]);
|
|
35
|
+
const getSnapshot = () => powerSync?.currentStatus ?? null;
|
|
36
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
34
37
|
}
|
|
35
38
|
//# sourceMappingURL=useStatus.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useStatus.js","sourceRoot":"","sources":["../../src/hooks/useStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useStatus.js","sourceRoot":"","sources":["../../src/hooks/useStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAGzE;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,SAAS;IACvB,OAAO,WAAW,CAAC,YAAY,EAAE,CAAE,CAAC;AACtC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,eAAe;IAC7B,OAAO,WAAW,CAAC,kBAAkB,EAAE,CAAC,CAAC;AAC3C,CAAC;AAED,SAAS,WAAW,CAAC,SAAyC;IAC5D,MAAM,SAAS,GAAG,WAAW,CAC3B,CAAC,QAAoB,EAAE,EAAE;QACvB,MAAM,OAAO,GAAG,SAAS,EAAE,gBAAgB,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC,CAAC;QACzE,OAAO,OAAO,IAAI,cAAa,CAAC,CAAC;IACnC,CAAC;IACD,sDAAsD;IACtD,CAAC,SAAS,CAAC,CACZ,CAAC;IAEF,MAAM,WAAW,GAAG,GAAG,EAAE,CAAC,SAAS,EAAE,aAAa,IAAI,IAAI,CAAC;IAE3D,OAAO,oBAAoB,CAAC,SAAS,EAAE,WAAW,EAAE,WAAW,CAAC,CAAC;AACnE,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { usePowerSync, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
1
|
+
export { usePowerSync, usePowerSyncOrNull, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
2
2
|
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
3
3
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
4
4
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
5
5
|
export { useAllSyncStreamsHaveSynced, useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
6
|
-
export { useStatus } from './hooks/useStatus.js';
|
|
6
|
+
export { useStatus, useStatusOrNull } from './hooks/useStatus.js';
|
|
7
7
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
8
8
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
9
9
|
export { AdditionalOptions, FetchReason, DifferentialSuspenseQueryOptions, QuerySyncStreamOptions, SuspenseQueryOptions } from './hooks/watched/watch-types.js';
|
package/lib/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export { usePowerSync, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
1
|
+
export { usePowerSync, usePowerSyncOrNull, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
2
2
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
3
3
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
4
4
|
export { useAllSyncStreamsHaveSynced, useSyncStream } from './hooks/streams.js';
|
|
5
|
-
export { useStatus } from './hooks/useStatus.js';
|
|
5
|
+
export { useStatus, useStatusOrNull } from './hooks/useStatus.js';
|
|
6
6
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
7
7
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
8
8
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEjG,OAAO,EAAE,gBAAgB,EAAE,MAAM,sCAAsC,CAAC;AACxE,OAAO,EAAE,mCAAmC,EAAE,MAAM,yDAAyD,CAAC;AAC9G,OAAO,EAAE,2BAA2B,EAAE,aAAa,EAAwB,MAAM,oBAAoB,CAAC;AACtG,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAClE,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AACvD,OAAO,EAAE,2BAA2B,EAAE,MAAM,gDAAgD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/react",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"homepage": "https://docs.powersync.com",
|
|
27
27
|
"peerDependencies": {
|
|
28
|
-
"@powersync/common": "^2.
|
|
28
|
+
"@powersync/common": "^2.5.0",
|
|
29
29
|
"react": "*"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
@@ -41,10 +41,10 @@
|
|
|
41
41
|
"react": "19.3.0",
|
|
42
42
|
"react-dom": "19.3.0",
|
|
43
43
|
"react-error-boundary": "^4.1.0",
|
|
44
|
-
"@powersync/common": "2.
|
|
44
|
+
"@powersync/common": "2.5.0",
|
|
45
45
|
"@powersync/drizzle-driver": "0.8.0",
|
|
46
|
-
"@powersync/node": "1.
|
|
47
|
-
"@powersync/shared-internals": "1.
|
|
46
|
+
"@powersync/node": "1.3.0",
|
|
47
|
+
"@powersync/shared-internals": "1.5.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsc -b",
|
|
@@ -25,4 +25,11 @@ export function usePowerSync() {
|
|
|
25
25
|
return db;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
/**
|
|
29
|
+
* A variant of {@link usePowerSync} that returns null when no PowerSync database is in context (instead of throwing).
|
|
30
|
+
*
|
|
31
|
+
* @returns The PowerSync database instance, or null.
|
|
32
|
+
*/
|
|
33
|
+
export function usePowerSyncOrNull(): CommonPowerSyncDatabase | null {
|
|
34
|
+
return React.useContext(PowerSyncContext);
|
|
35
|
+
}
|
package/src/hooks/useStatus.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useCallback, useSyncExternalStore } from 'react';
|
|
2
2
|
import { usePowerSync, usePowerSyncOrNull } from './PowerSyncContext.js';
|
|
3
|
-
import { SyncStatus } from '@powersync/common';
|
|
3
|
+
import { CommonPowerSyncDatabase, SyncStatus } from '@powersync/common';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Custom hook that provides access to the current status of PowerSync.
|
|
@@ -17,31 +17,29 @@ import { SyncStatus } from '@powersync/common';
|
|
|
17
17
|
* };
|
|
18
18
|
*/
|
|
19
19
|
export function useStatus(): SyncStatus {
|
|
20
|
-
return
|
|
20
|
+
return useStatusOf(usePowerSync())!;
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
/**
|
|
24
|
-
* @
|
|
24
|
+
* A variant of {@link useStatus} that returns null when no PowerSync database is in context (instead of throwing).
|
|
25
|
+
*
|
|
26
|
+
* @returns The current sync status of the PowerSync database, or null.
|
|
25
27
|
*/
|
|
26
|
-
export function useStatusOrNull(
|
|
27
|
-
|
|
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);
|
|
33
|
-
|
|
34
|
-
useEffect(() => {
|
|
35
|
-
setSyncStatus(powerSync?.currentStatus);
|
|
28
|
+
export function useStatusOrNull(): SyncStatus | null {
|
|
29
|
+
return useStatusOf(usePowerSyncOrNull());
|
|
30
|
+
}
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
32
|
+
function useStatusOf(powerSync: CommonPowerSyncDatabase | null): SyncStatus | null {
|
|
33
|
+
const subscribe = useCallback(
|
|
34
|
+
(listener: () => void) => {
|
|
35
|
+
const dispose = powerSync?.registerListener({ statusChanged: listener });
|
|
36
|
+
return dispose ?? function () {};
|
|
37
|
+
},
|
|
38
|
+
// Ensure React treats changed databases as new stores
|
|
39
|
+
[powerSync]
|
|
40
|
+
);
|
|
42
41
|
|
|
43
|
-
|
|
44
|
-
}, [powerSync]);
|
|
42
|
+
const getSnapshot = () => powerSync?.currentStatus ?? null;
|
|
45
43
|
|
|
46
|
-
return
|
|
44
|
+
return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
|
|
47
45
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export { usePowerSync, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
1
|
+
export { usePowerSync, usePowerSyncOrNull, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
2
2
|
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
3
3
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
4
4
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
5
5
|
export { useAllSyncStreamsHaveSynced, useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
6
|
-
export { useStatus } from './hooks/useStatus.js';
|
|
6
|
+
export { useStatus, useStatusOrNull } from './hooks/useStatus.js';
|
|
7
7
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
8
8
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
9
9
|
export {
|