@powersync/react 0.0.0-dev-20260216124709 → 0.0.0-dev-20260630141119
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 +2 -3
- package/lib/hooks/streams.d.ts +7 -1
- package/lib/hooks/streams.js +42 -69
- package/lib/hooks/streams.js.map +1 -1
- package/lib/hooks/useStatus.d.ts +1 -1
- package/lib/hooks/useStatus.js +15 -2
- package/lib/hooks/useStatus.js.map +1 -1
- package/lib/hooks/watched/useWatchedQuery.js +4 -1
- package/lib/hooks/watched/useWatchedQuery.js.map +1 -1
- package/lib/hooks/watched/watch-types.d.ts +0 -2
- package/lib/hooks/watched/watch-utils.js.map +1 -1
- package/lib/index.d.ts +2 -5
- package/lib/index.js +1 -4
- package/lib/index.js.map +1 -1
- package/package.json +8 -7
- package/src/hooks/streams.ts +41 -65
- package/src/hooks/useStatus.ts +18 -2
- package/src/hooks/watched/useWatchedQuery.ts +5 -1
- package/src/hooks/watched/watch-types.ts +0 -2
- package/src/hooks/watched/watch-utils.ts +1 -1
- package/src/index.ts +2 -5
- package/lib/hooks/deprecated/usePowerSyncQuery.d.ts +0 -7
- package/lib/hooks/deprecated/usePowerSyncQuery.js +0 -26
- package/lib/hooks/deprecated/usePowerSyncQuery.js.map +0 -1
- package/lib/hooks/deprecated/usePowerSyncStatus.d.ts +0 -15
- package/lib/hooks/deprecated/usePowerSyncStatus.js +0 -30
- package/lib/hooks/deprecated/usePowerSyncStatus.js.map +0 -1
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.d.ts +0 -17
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js +0 -45
- package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js.map +0 -1
- package/src/hooks/deprecated/usePowerSyncQuery.ts +0 -28
- package/src/hooks/deprecated/usePowerSyncStatus.ts +0 -33
- package/src/hooks/deprecated/usePowerSyncWatchedQuery.ts +0 -60
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { AbstractPowerSyncDatabase } from '@powersync/common';
|
|
2
1
|
import React from 'react';
|
|
3
|
-
export declare const PowerSyncContext: React.Context<
|
|
2
|
+
export declare const PowerSyncContext: React.Context<import("@powersync/common").CommonPowerSyncDatabase>;
|
|
4
3
|
/**
|
|
5
4
|
* Custom hook that provides access to the PowerSync context.
|
|
6
5
|
* @returns The PowerSync Database instance.
|
|
@@ -18,4 +17,4 @@ export declare const PowerSyncContext: React.Context<AbstractPowerSyncDatabase>;
|
|
|
18
17
|
* </ul>
|
|
19
18
|
* };
|
|
20
19
|
*/
|
|
21
|
-
export declare const usePowerSync: () =>
|
|
20
|
+
export declare const usePowerSync: () => import("@powersync/common").CommonPowerSyncDatabase;
|
package/lib/hooks/streams.d.ts
CHANGED
|
@@ -26,6 +26,12 @@ export interface UseSyncStreamOptions extends SyncStreamSubscribeOptions {
|
|
|
26
26
|
*/
|
|
27
27
|
export declare function useSyncStream(options: UseSyncStreamOptions): SyncStreamStatus | null;
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
29
|
+
* Creates multiple PowerSync stream subscriptions. Subscriptions are kept alive as long as the
|
|
30
|
+
* React component calling this function. When it unmounts, or when the streams array contents
|
|
31
|
+
* change, all previous subscriptions are unsubscribed before new ones are created.
|
|
32
|
+
*/
|
|
33
|
+
export declare function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStreamStatus[];
|
|
34
|
+
/**
|
|
35
|
+
* Returns `true` once all streams in the array have synced at least once.
|
|
30
36
|
*/
|
|
31
37
|
export declare function useAllSyncStreamsHaveSynced(db: AbstractPowerSyncDatabase, streams: QuerySyncStreamOptions[] | undefined): boolean;
|
package/lib/hooks/streams.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useMemo
|
|
1
|
+
import { useEffect, useMemo } from 'react';
|
|
2
2
|
import { usePowerSync } from './PowerSyncContext.js';
|
|
3
3
|
import { useStatus } from './useStatus.js';
|
|
4
4
|
/**
|
|
@@ -10,84 +10,57 @@ import { useStatus } from './useStatus.js';
|
|
|
10
10
|
* @returns The status for that stream, or `null` if the stream is currently being resolved.
|
|
11
11
|
*/
|
|
12
12
|
export function useSyncStream(options) {
|
|
13
|
-
|
|
13
|
+
return useSyncStreams([options])[0];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Creates multiple PowerSync stream subscriptions. Subscriptions are kept alive as long as the
|
|
17
|
+
* React component calling this function. When it unmounts, or when the streams array contents
|
|
18
|
+
* change, all previous subscriptions are unsubscribed before new ones are created.
|
|
19
|
+
*/
|
|
20
|
+
export function useSyncStreams(streamOptions) {
|
|
14
21
|
const db = usePowerSync();
|
|
15
22
|
const status = useStatus();
|
|
16
|
-
const
|
|
23
|
+
const stringifiedOptions = useMemo(() => JSON.stringify(streamOptions), [streamOptions]);
|
|
24
|
+
const syncStreams = useMemo(() => streamOptions.map((options) => {
|
|
25
|
+
return {
|
|
26
|
+
stream: db.syncStream(options.name, options.parameters ?? undefined),
|
|
27
|
+
options
|
|
28
|
+
};
|
|
29
|
+
}), [stringifiedOptions]);
|
|
17
30
|
useEffect(() => {
|
|
18
31
|
let active = true;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
.subscribe(options)
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
});
|
|
32
|
+
const resolvedSubs = [];
|
|
33
|
+
for (const entry of syncStreams) {
|
|
34
|
+
entry.stream.subscribe(entry.options).then((sub) => {
|
|
35
|
+
if (active) {
|
|
36
|
+
resolvedSubs.push(sub);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
// The cleanup function already ran, unsubscribe immediately.
|
|
40
|
+
sub.unsubscribe();
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
32
44
|
return () => {
|
|
33
45
|
active = false;
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
for (const sub of resolvedSubs) {
|
|
47
|
+
sub.unsubscribe();
|
|
48
|
+
}
|
|
37
49
|
};
|
|
38
|
-
}, [
|
|
39
|
-
return
|
|
50
|
+
}, [stringifiedOptions]);
|
|
51
|
+
return useMemo(() => syncStreams.map((entry) => status.forStream(entry.stream) ?? null), [status, stringifiedOptions]);
|
|
40
52
|
}
|
|
41
53
|
/**
|
|
42
|
-
*
|
|
54
|
+
* Returns `true` once all streams in the array have synced at least once.
|
|
43
55
|
*/
|
|
44
56
|
export function useAllSyncStreamsHaveSynced(db, streams) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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;
|
|
57
|
+
const statuses = useSyncStreams(streams ?? []);
|
|
58
|
+
if (!streams)
|
|
59
|
+
return true;
|
|
60
|
+
return streams.every((stream, i) => {
|
|
61
|
+
if (!stream.waitForStream)
|
|
62
|
+
return true;
|
|
63
|
+
return statuses[i]?.subscription?.hasSynced === true;
|
|
64
|
+
});
|
|
92
65
|
}
|
|
93
66
|
//# sourceMappingURL=streams.js.map
|
package/lib/hooks/streams.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"streams.js","sourceRoot":"","sources":["../../src/hooks/streams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"streams.js","sourceRoot":"","sources":["../../src/hooks/streams.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,OAAO,EAAY,MAAM,OAAO,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAQrD,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAoB3C;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,OAA6B;IACzD,OAAO,cAAc,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACtC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,cAAc,CAAC,aAAqC;IAClE,MAAM,EAAE,GAAG,YAAY,EAAE,CAAC;IAC1B,MAAM,MAAM,GAAG,SAAS,EAAE,CAAC;IAE3B,MAAM,kBAAkB,GAAG,OAAO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AAC,EACxE,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAC7B,CAAC;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,2BAA2B,CACzC,EAA6B,EAC7B,OAA6C;IAE7C,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,IAAI,EAAE,CAAC,CAAC;IAE/C,IAAI,CAAC,OAAO;QAAE,OAAO,IAAI,CAAC;IAE1B,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;QACjC,IAAI,CAAC,MAAM,CAAC,aAAa;YAAE,OAAO,IAAI,CAAC;QACvC,OAAO,QAAQ,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,SAAS,KAAK,IAAI,CAAC;IACvD,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/lib/hooks/useStatus.d.ts
CHANGED
package/lib/hooks/useStatus.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { PowerSyncContext } from './PowerSyncContext.js';
|
|
2
3
|
/**
|
|
3
4
|
* Custom hook that provides access to the current status of PowerSync.
|
|
4
5
|
* @returns The PowerSync Database status.
|
|
@@ -13,5 +14,17 @@ import { usePowerSyncStatus } from './deprecated/usePowerSyncStatus.js';
|
|
|
13
14
|
* </div>
|
|
14
15
|
* };
|
|
15
16
|
*/
|
|
16
|
-
export
|
|
17
|
+
export function useStatus() {
|
|
18
|
+
const powerSync = useContext(PowerSyncContext);
|
|
19
|
+
const [syncStatus, setSyncStatus] = useState(powerSync.currentStatus);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const listener = powerSync.registerListener({
|
|
22
|
+
statusChanged: (status) => {
|
|
23
|
+
setSyncStatus(status);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
return () => listener?.();
|
|
27
|
+
}, [powerSync]);
|
|
28
|
+
return syncStatus;
|
|
29
|
+
}
|
|
17
30
|
//# 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,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,SAAS;IACvB,MAAM,SAAS,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAC/C,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;IAEtE,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,SAAS,CAAC,gBAAgB,CAAC;YAC1C,aAAa,EAAE,CAAC,MAAM,EAAE,EAAE;gBACxB,aAAa,CAAC,MAAM,CAAC,CAAC;YACxB,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC;IAC5B,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,OAAO,UAAU,CAAC;AACpB,CAAC"}
|
|
@@ -70,8 +70,11 @@ export const useWatchedQuery = (options) => {
|
|
|
70
70
|
}
|
|
71
71
|
const shouldReportCurrentlyFetching = (hookOptions.reportFetching ?? true) && !!disposePendingUpdateListener.current;
|
|
72
72
|
const result = useNullableWatchedQuerySubscription(watchedQuery);
|
|
73
|
+
// Result is only undefined when there is no data available yet, defaults are defined accordingly
|
|
73
74
|
return {
|
|
74
|
-
|
|
75
|
+
data: result?.data ?? [],
|
|
76
|
+
isLoading: result?.isLoading ?? true,
|
|
77
|
+
error: result?.error,
|
|
75
78
|
isFetching: result?.isFetching || shouldReportCurrentlyFetching
|
|
76
79
|
};
|
|
77
80
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWatchedQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useWatchedQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,mCAAmC,EAAE,MAAM,kCAAkC,CAAC;AAIvF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAC7B,OAAuF,EAClC,EAAE;IACvD,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IAEjF,SAAS,kBAAkB;QACzB,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,KAAK,GAAG,WAAW,CAAC,aAAa;YACrC,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,iBAAiB,CAAC;gBAC7C,aAAa,EAAE,WAAW,CAAC,aAAa;gBACxC,cAAc,EAAE,WAAW,CAAC,cAAc;gBAC1C,UAAU,EAAE,WAAW,CAAC,UAAU;aACnC,CAAC;YACJ,CAAC,CAAC,SAAS,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC;gBACjC,cAAc,EAAE,WAAW,CAAC,cAAc;gBAC1C,UAAU,EAAE,WAAW,CAAC,UAAU;aACnC,CAAC,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC;IAC3E,MAAM,4BAA4B,GAAG,KAAK,CAAC,MAAM,CAAoB,IAAI,CAAC,CAAC;IAE3E,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,YAAY,EAAE,KAAK,E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AAC;YAC3B,KAAK;YACL,UAAU,EAAE,WAAW,CAAC,UAAU;YAClC,cAAc,EAAE,WAAW,CAAC,cAAc;SAC3C,CAAC,CAAC;QACH,uEAAuE;QACvE,4BAA4B,CAAC,OAAO,EAAE,EAAE,CAAC;QACzC,4BAA4B,CAAC,OAAO,GAAG,YAAY,EAAE,gBAAgB,CAAC;YACpE,kBAAkB,EAAE,GAAG,EAAE;gBACvB,qEAAqE;gBACrE,4BAA4B,CAAC,OAAO,EAAE,EAAE,CAAC;gBACzC,4BAA4B,CAAC,OAAO,GAAG,IAAI,CAAC;YAC9C,CAAC;SACF,CAAC,CAAC;IACL,CAAC;IAED,MAAM,6BAA6B,GAAG,CAAC,WAAW,CAAC,cAAc,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,4BAA4B,CAAC,OAAO,CAAC;IACrH,MAAM,MAAM,GAAG,mCAAmC,CAAC,YAAY,CAAC,CAAC;IAEjE,iGAAiG;IACjG,OAAO;QACL,IAAI,EAAE,MAAM,EAAE,IAAI,IAAI,EAAE;QACxB,SAAS,EAAE,MAAM,EAAE,SAAS,IAAI,IAAI;QACpC,KAAK,EAAE,MAAM,EAAE,KAAK;QACpB,UAAU,EAAE,MAAM,EAAE,UAAU,IAAI,6BAA6B;KAChE,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -11,8 +11,6 @@ export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
|
|
|
11
11
|
* been downloaded.
|
|
12
12
|
* Note however that after an initial sync, the query will not block itself while new rows are downloading. Instead,
|
|
13
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
14
|
*/
|
|
17
15
|
streams?: QuerySyncStreamOptions[];
|
|
18
16
|
reportFetching?: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watch-utils.js","sourceRoot":"","sources":["../../../src/hooks/watched/watch-utils.ts"],"names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CAAC;IAE7D,OAAO;QACL,WAAW;QACX,YAAY;KACb,CAAC;AACJ,CAAC,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery.js';
|
|
2
|
-
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus.js';
|
|
3
|
-
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery.js';
|
|
4
1
|
export * from './hooks/PowerSyncContext.js';
|
|
5
2
|
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
6
3
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
7
4
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
8
|
-
export { useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
5
|
+
export { useAllSyncStreamsHaveSynced, useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
9
6
|
export { useStatus } from './hooks/useStatus.js';
|
|
10
7
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
11
8
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
12
|
-
export { AdditionalOptions } from './hooks/watched/watch-types.js';
|
|
9
|
+
export { AdditionalOptions, QuerySyncStreamOptions } from './hooks/watched/watch-types.js';
|
package/lib/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery.js';
|
|
2
|
-
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus.js';
|
|
3
|
-
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery.js';
|
|
4
1
|
export * from './hooks/PowerSyncContext.js';
|
|
5
2
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
6
3
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
7
|
-
export { useSyncStream } from './hooks/streams.js';
|
|
4
|
+
export { useAllSyncStreamsHaveSynced, useSyncStream } from './hooks/streams.js';
|
|
8
5
|
export { useStatus } from './hooks/useStatus.js';
|
|
9
6
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
10
7
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAE5C,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,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,6BAA6B,CAAC;AACvD,OAAO,EAAE,2BAA2B,EAAE,MAAM,gDAAgD,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/react",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20260630141119",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
8
|
-
"description": "React components for
|
|
8
|
+
"description": "React components for PowerSync",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"main": "./lib/index.js",
|
|
11
11
|
"module": "./lib/index.js",
|
|
@@ -18,14 +18,14 @@
|
|
|
18
18
|
"type": "git",
|
|
19
19
|
"url": "git+https://github.com/powersync-ja/powersync-js.git"
|
|
20
20
|
},
|
|
21
|
-
"author": "
|
|
21
|
+
"author": "PowerSync",
|
|
22
22
|
"license": "Apache-2.0",
|
|
23
23
|
"bugs": {
|
|
24
24
|
"url": "https://github.com/powersync-ja/powersync-js/issues"
|
|
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-20260630141119",
|
|
29
29
|
"react": "*"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
@@ -39,9 +39,10 @@
|
|
|
39
39
|
"react": "18.3.1",
|
|
40
40
|
"react-dom": "18.3.1",
|
|
41
41
|
"react-error-boundary": "^4.1.0",
|
|
42
|
-
"@powersync/common": "0.0.0-dev-
|
|
43
|
-
"@powersync/drizzle-driver": "0.0.0-dev-
|
|
44
|
-
"@powersync/web": "0.0.0-dev-
|
|
42
|
+
"@powersync/common": "0.0.0-dev-20260630141119",
|
|
43
|
+
"@powersync/drizzle-driver": "0.0.0-dev-20260630141119",
|
|
44
|
+
"@powersync/web": "0.0.0-dev-20260630141119",
|
|
45
|
+
"@powersync/shared-internals": "1.0.0"
|
|
45
46
|
},
|
|
46
47
|
"scripts": {
|
|
47
48
|
"build": "tsc -b",
|
package/src/hooks/streams.ts
CHANGED
|
@@ -36,96 +36,72 @@ export interface UseSyncStreamOptions extends SyncStreamSubscribeOptions {
|
|
|
36
36
|
* @returns The status for that stream, or `null` if the stream is currently being resolved.
|
|
37
37
|
*/
|
|
38
38
|
export function useSyncStream(options: UseSyncStreamOptions): SyncStreamStatus | null {
|
|
39
|
-
|
|
39
|
+
return useSyncStreams([options])[0];
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Creates multiple PowerSync stream subscriptions. Subscriptions are kept alive as long as the
|
|
44
|
+
* React component calling this function. When it unmounts, or when the streams array contents
|
|
45
|
+
* change, all previous subscriptions are unsubscribed before new ones are created.
|
|
46
|
+
*/
|
|
47
|
+
export function useSyncStreams(streamOptions: UseSyncStreamOptions[]): SyncStreamStatus[] {
|
|
40
48
|
const db = usePowerSync();
|
|
41
49
|
const status = useStatus();
|
|
42
|
-
|
|
50
|
+
|
|
51
|
+
const stringifiedOptions = useMemo(() => JSON.stringify(streamOptions), [streamOptions]);
|
|
52
|
+
const syncStreams = useMemo(
|
|
53
|
+
() =>
|
|
54
|
+
streamOptions.map((options) => {
|
|
55
|
+
return {
|
|
56
|
+
stream: db.syncStream(options.name, options.parameters ?? undefined),
|
|
57
|
+
options
|
|
58
|
+
};
|
|
59
|
+
}),
|
|
60
|
+
[stringifiedOptions]
|
|
61
|
+
);
|
|
43
62
|
|
|
44
63
|
useEffect(() => {
|
|
45
64
|
let active = true;
|
|
46
|
-
|
|
65
|
+
const resolvedSubs: SyncStreamSubscription[] = [];
|
|
47
66
|
|
|
48
|
-
|
|
49
|
-
.subscribe(options)
|
|
50
|
-
.then((sub) => {
|
|
67
|
+
for (const entry of syncStreams) {
|
|
68
|
+
entry.stream.subscribe(entry.options).then((sub) => {
|
|
51
69
|
if (active) {
|
|
52
|
-
|
|
53
|
-
setSubscription(sub);
|
|
70
|
+
resolvedSubs.push(sub);
|
|
54
71
|
} else {
|
|
55
72
|
// The cleanup function already ran, unsubscribe immediately.
|
|
56
73
|
sub.unsubscribe();
|
|
57
74
|
}
|
|
58
75
|
});
|
|
76
|
+
}
|
|
59
77
|
|
|
60
78
|
return () => {
|
|
61
79
|
active = false;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
80
|
+
for (const sub of resolvedSubs) {
|
|
81
|
+
sub.unsubscribe();
|
|
82
|
+
}
|
|
65
83
|
};
|
|
66
|
-
}, [
|
|
84
|
+
}, [stringifiedOptions]);
|
|
67
85
|
|
|
68
|
-
return
|
|
86
|
+
return useMemo(
|
|
87
|
+
() => syncStreams.map((entry) => status.forStream(entry.stream) ?? null),
|
|
88
|
+
[status, stringifiedOptions]
|
|
89
|
+
);
|
|
69
90
|
}
|
|
70
91
|
|
|
71
92
|
/**
|
|
72
|
-
*
|
|
93
|
+
* Returns `true` once all streams in the array have synced at least once.
|
|
73
94
|
*/
|
|
74
95
|
export function useAllSyncStreamsHaveSynced(
|
|
75
96
|
db: AbstractPowerSyncDatabase,
|
|
76
97
|
streams: QuerySyncStreamOptions[] | undefined
|
|
77
98
|
): boolean {
|
|
78
|
-
|
|
79
|
-
// want to update underlying subscriptions each time, though.
|
|
80
|
-
const hash = useMemo(() => streams && JSON.stringify(streams), [streams]);
|
|
81
|
-
const [synced, setHasSynced] = useState(streams == null || streams.every((e) => e.waitForStream != true));
|
|
82
|
-
|
|
83
|
-
useEffect(() => {
|
|
84
|
-
if (streams) {
|
|
85
|
-
setHasSynced(false);
|
|
86
|
-
|
|
87
|
-
const promises: Promise<SyncStreamSubscription>[] = [];
|
|
88
|
-
const abort = new AbortController();
|
|
89
|
-
for (const stream of streams) {
|
|
90
|
-
promises.push(db.syncStream(stream.name, stream.parameters).subscribe(stream));
|
|
91
|
-
}
|
|
99
|
+
const statuses = useSyncStreams(streams ?? []);
|
|
92
100
|
|
|
93
|
-
|
|
94
|
-
Promise.all(promises).then(async (resolvedStreams) => {
|
|
95
|
-
function allHaveSynced(status: SyncStatus) {
|
|
96
|
-
return resolvedStreams.every((s, i) => {
|
|
97
|
-
const request = streams[i];
|
|
98
|
-
return !request.waitForStream || status.forStream(s)?.subscription?.hasSynced;
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// Wait for the effect to be cancelled or all streams having synced.
|
|
103
|
-
await db.waitForStatus(allHaveSynced, abort.signal);
|
|
104
|
-
if (abort.signal.aborted) {
|
|
105
|
-
// Was cancelled
|
|
106
|
-
} else {
|
|
107
|
-
// Has synced, update public state.
|
|
108
|
-
setHasSynced(true);
|
|
109
|
-
|
|
110
|
-
// Wait for cancellation before clearing subscriptions.
|
|
111
|
-
await new Promise<void>((resolve) => {
|
|
112
|
-
abort.signal.addEventListener('abort', () => resolve());
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// Effect was definitely cancelled at this point, so drop the subscriptions.
|
|
117
|
-
for (const stream of resolvedStreams) {
|
|
118
|
-
stream.unsubscribe();
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
return () => abort.abort();
|
|
123
|
-
} else {
|
|
124
|
-
// There are no streams, so all of them have synced.
|
|
125
|
-
setHasSynced(true);
|
|
126
|
-
return undefined;
|
|
127
|
-
}
|
|
128
|
-
}, [hash]);
|
|
101
|
+
if (!streams) return true;
|
|
129
102
|
|
|
130
|
-
return
|
|
103
|
+
return streams.every((stream, i) => {
|
|
104
|
+
if (!stream.waitForStream) return true;
|
|
105
|
+
return statuses[i]?.subscription?.hasSynced === true;
|
|
106
|
+
});
|
|
131
107
|
}
|
package/src/hooks/useStatus.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { useContext, useEffect, useState } from 'react';
|
|
2
|
+
import { PowerSyncContext } from './PowerSyncContext.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Custom hook that provides access to the current status of PowerSync.
|
|
@@ -14,4 +15,19 @@ import { usePowerSyncStatus } from './deprecated/usePowerSyncStatus.js';
|
|
|
14
15
|
* </div>
|
|
15
16
|
* };
|
|
16
17
|
*/
|
|
17
|
-
export
|
|
18
|
+
export function useStatus() {
|
|
19
|
+
const powerSync = useContext(PowerSyncContext);
|
|
20
|
+
const [syncStatus, setSyncStatus] = useState(powerSync.currentStatus);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
const listener = powerSync.registerListener({
|
|
24
|
+
statusChanged: (status) => {
|
|
25
|
+
setSyncStatus(status);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
return () => listener?.();
|
|
30
|
+
}, [powerSync]);
|
|
31
|
+
|
|
32
|
+
return syncStatus;
|
|
33
|
+
}
|
|
@@ -82,8 +82,12 @@ export const useWatchedQuery = <RowType = unknown>(
|
|
|
82
82
|
|
|
83
83
|
const shouldReportCurrentlyFetching = (hookOptions.reportFetching ?? true) && !!disposePendingUpdateListener.current;
|
|
84
84
|
const result = useNullableWatchedQuerySubscription(watchedQuery);
|
|
85
|
+
|
|
86
|
+
// Result is only undefined when there is no data available yet, defaults are defined accordingly
|
|
85
87
|
return {
|
|
86
|
-
|
|
88
|
+
data: result?.data ?? [],
|
|
89
|
+
isLoading: result?.isLoading ?? true,
|
|
90
|
+
error: result?.error,
|
|
87
91
|
isFetching: result?.isFetching || shouldReportCurrentlyFetching
|
|
88
92
|
};
|
|
89
93
|
};
|
|
@@ -12,8 +12,6 @@ export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
|
|
|
12
12
|
* been downloaded.
|
|
13
13
|
* Note however that after an initial sync, the query will not block itself while new rows are downloading. Instead,
|
|
14
14
|
* consistent sync snapshots will be made available as they've been processed by PowerSync.
|
|
15
|
-
*
|
|
16
|
-
* @experimental Sync streams are currently in alpha.
|
|
17
15
|
*/
|
|
18
16
|
streams?: QuerySyncStreamOptions[];
|
|
19
17
|
reportFetching?: boolean;
|
|
@@ -48,7 +48,7 @@ export const constructCompatibleQuery = <RowType>(
|
|
|
48
48
|
parameters: any[] = [],
|
|
49
49
|
options: AdditionalOptions
|
|
50
50
|
) => {
|
|
51
|
-
const powerSync = usePowerSync()
|
|
51
|
+
const powerSync = usePowerSync()!;
|
|
52
52
|
const stringifiedParameters = React.useMemo(() => JSON.stringify(parameters), [parameters]);
|
|
53
53
|
|
|
54
54
|
const parsedQuery = React.useMemo<WatchCompatibleQueryWithParams<RowType[]>>(() => {
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery.js';
|
|
2
|
-
export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus.js';
|
|
3
|
-
export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery.js';
|
|
4
1
|
export * from './hooks/PowerSyncContext.js';
|
|
5
2
|
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
6
3
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
7
4
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
8
|
-
export { useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
5
|
+
export { useAllSyncStreamsHaveSynced, useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
|
|
9
6
|
export { useStatus } from './hooks/useStatus.js';
|
|
10
7
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
11
8
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
12
|
-
export { AdditionalOptions } from './hooks/watched/watch-types.js';
|
|
9
|
+
export { AdditionalOptions, QuerySyncStreamOptions } from './hooks/watched/watch-types.js';
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @deprecated use {@link useQuery} instead.
|
|
3
|
-
*
|
|
4
|
-
* A hook to access a single static query.
|
|
5
|
-
* For an updated result, use {@link usePowerSyncWatchedQuery} instead.
|
|
6
|
-
*/
|
|
7
|
-
export declare const usePowerSyncQuery: <T = any>(sqlStatement: string, parameters?: any[]) => T[];
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { usePowerSync } from '../PowerSyncContext.js';
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated use {@link useQuery} instead.
|
|
5
|
-
*
|
|
6
|
-
* A hook to access a single static query.
|
|
7
|
-
* For an updated result, use {@link usePowerSyncWatchedQuery} instead.
|
|
8
|
-
*/
|
|
9
|
-
export const usePowerSyncQuery = (sqlStatement, parameters = []) => {
|
|
10
|
-
const powerSync = usePowerSync();
|
|
11
|
-
if (!powerSync) {
|
|
12
|
-
return [];
|
|
13
|
-
}
|
|
14
|
-
// This will return the previous parameters until the contents of parameters array change,
|
|
15
|
-
// thereby providing a stable array reference and preventing excessive useEffect runs
|
|
16
|
-
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
|
|
17
|
-
const [data, setData] = React.useState([]);
|
|
18
|
-
React.useEffect(() => {
|
|
19
|
-
powerSync.readLock(async (tx) => {
|
|
20
|
-
const result = await tx.getAll(sqlStatement, parameters);
|
|
21
|
-
setData(result);
|
|
22
|
-
});
|
|
23
|
-
}, [powerSync, sqlStatement, memoizedParams]);
|
|
24
|
-
return data;
|
|
25
|
-
};
|
|
26
|
-
//# sourceMappingURL=usePowerSyncQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"usePowerSyncQuery.js","sourceRoot":"","sources":["../../../src/hooks/deprecated/usePowerSyncQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAU,YAAoB,EAAE,aAAoB,EAAE,EAAO,EAAE;IAC9F,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,0FAA0F;IAC1F,qFAAqF;IACrF,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IACxE,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAM,EAAE,CAAC,CAAC;IAEhD,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE;YAC9B,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,CAAI,YAAY,EAAE,UAAU,CAAC,CAAC;YAC5D,OAAO,CAAC,MAAM,CAAC,CAAC;QAClB,CAAC,CAAC,CAAC;IACL,CAAC,EAAE,CAAC,SAAS,EAAE,YAAY,EAAE,cAAc,CAAC,CAAC,CAAC;IAE9C,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @deprecated Use {@link useStatus} instead.
|
|
3
|
-
*
|
|
4
|
-
* Custom hook that provides access to the current status of PowerSync.
|
|
5
|
-
* @returns The PowerSync Database status.
|
|
6
|
-
* @example
|
|
7
|
-
* const Component = () => {
|
|
8
|
-
* const status = usePowerSyncStatus();
|
|
9
|
-
*
|
|
10
|
-
* return <div>
|
|
11
|
-
* status.connected ? 'wifi' : 'wifi-off'
|
|
12
|
-
* </div>
|
|
13
|
-
* };
|
|
14
|
-
*/
|
|
15
|
-
export declare const usePowerSyncStatus: () => import("@powersync/common").SyncStatus;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { useContext, useEffect, useState } from 'react';
|
|
2
|
-
import { PowerSyncContext } from '../PowerSyncContext.js';
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated Use {@link useStatus} instead.
|
|
5
|
-
*
|
|
6
|
-
* Custom hook that provides access to the current status of PowerSync.
|
|
7
|
-
* @returns The PowerSync Database status.
|
|
8
|
-
* @example
|
|
9
|
-
* const Component = () => {
|
|
10
|
-
* const status = usePowerSyncStatus();
|
|
11
|
-
*
|
|
12
|
-
* return <div>
|
|
13
|
-
* status.connected ? 'wifi' : 'wifi-off'
|
|
14
|
-
* </div>
|
|
15
|
-
* };
|
|
16
|
-
*/
|
|
17
|
-
export const usePowerSyncStatus = () => {
|
|
18
|
-
const powerSync = useContext(PowerSyncContext);
|
|
19
|
-
const [syncStatus, setSyncStatus] = useState(powerSync.currentStatus);
|
|
20
|
-
useEffect(() => {
|
|
21
|
-
const listener = powerSync.registerListener({
|
|
22
|
-
statusChanged: (status) => {
|
|
23
|
-
setSyncStatus(status);
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
return () => listener?.();
|
|
27
|
-
}, [powerSync]);
|
|
28
|
-
return syncStatus;
|
|
29
|
-
};
|
|
30
|
-
//# sourceMappingURL=usePowerSyncStatus.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"usePowerSyncStatus.js","sourceRoot":"","sources":["../../../src/hooks/deprecated/usePowerSyncStatus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACxD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE1D;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,GAAG,EAAE;IACrC,MAAM,SAAS,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAC/C,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC;IAEtE,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,QAAQ,GAAG,SAAS,CAAC,gBAAgB,CAAC;YAC1C,aAAa,EAAE,CAAC,MAAM,EAAE,EAAE;gBACxB,aAAa,CAAC,MAAM,CAAC,CAAC;YACxB,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,GAAG,EAAE,CAAC,QAAQ,EAAE,EAAE,CAAC;IAC5B,CAAC,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC;IAEhB,OAAO,UAAU,CAAC;AACpB,CAAC,CAAC"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { SQLWatchOptions } from '@powersync/common';
|
|
2
|
-
/**
|
|
3
|
-
* @deprecated use {@link useQuery} instead.
|
|
4
|
-
*
|
|
5
|
-
* A hook to access the results of a watched query.
|
|
6
|
-
* @example
|
|
7
|
-
* export const Component = () => {
|
|
8
|
-
* const lists = usePowerSyncWatchedQuery('SELECT * from lists');
|
|
9
|
-
*
|
|
10
|
-
* return <View>
|
|
11
|
-
* {lists.map((l) => (
|
|
12
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
13
|
-
* ))}
|
|
14
|
-
* </View>
|
|
15
|
-
* }
|
|
16
|
-
*/
|
|
17
|
-
export declare const usePowerSyncWatchedQuery: <T = any>(sqlStatement: string, parameters?: any[], options?: Omit<SQLWatchOptions, "signal">) => T[];
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { usePowerSync } from '../PowerSyncContext.js';
|
|
3
|
-
/**
|
|
4
|
-
* @deprecated use {@link useQuery} instead.
|
|
5
|
-
*
|
|
6
|
-
* A hook to access the results of a watched query.
|
|
7
|
-
* @example
|
|
8
|
-
* export const Component = () => {
|
|
9
|
-
* const lists = usePowerSyncWatchedQuery('SELECT * from lists');
|
|
10
|
-
*
|
|
11
|
-
* return <View>
|
|
12
|
-
* {lists.map((l) => (
|
|
13
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
14
|
-
* ))}
|
|
15
|
-
* </View>
|
|
16
|
-
* }
|
|
17
|
-
*/
|
|
18
|
-
export const usePowerSyncWatchedQuery = (sqlStatement, parameters = [], options = {}) => {
|
|
19
|
-
const powerSync = usePowerSync();
|
|
20
|
-
if (!powerSync) {
|
|
21
|
-
return [];
|
|
22
|
-
}
|
|
23
|
-
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
|
|
24
|
-
const memoizedOptions = React.useMemo(() => options, [JSON.stringify(options)]);
|
|
25
|
-
const [data, setData] = React.useState([]);
|
|
26
|
-
const abortController = React.useRef(new AbortController());
|
|
27
|
-
React.useEffect(() => {
|
|
28
|
-
// Abort any previous watches
|
|
29
|
-
abortController.current?.abort();
|
|
30
|
-
abortController.current = new AbortController();
|
|
31
|
-
powerSync.watch(sqlStatement, parameters, {
|
|
32
|
-
onResult(results) {
|
|
33
|
-
setData(results.rows?._array ?? []);
|
|
34
|
-
}
|
|
35
|
-
}, {
|
|
36
|
-
...options,
|
|
37
|
-
signal: abortController.current.signal
|
|
38
|
-
});
|
|
39
|
-
return () => {
|
|
40
|
-
abortController.current?.abort();
|
|
41
|
-
};
|
|
42
|
-
}, [powerSync, sqlStatement, memoizedParams, memoizedOptions]);
|
|
43
|
-
return data;
|
|
44
|
-
};
|
|
45
|
-
//# sourceMappingURL=usePowerSyncWatchedQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"usePowerSyncWatchedQuery.js","sourceRoot":"","sources":["../../../src/hooks/deprecated/usePowerSyncWatchedQuery.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,YAAoB,EACpB,aAAoB,EAAE,EACtB,UAA2C,EAAE,EACxC,EAAE;IACP,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IACjC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC;IACxE,MAAM,eAAe,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAChF,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAM,EAAE,CAAC,CAAC;IAChD,MAAM,eAAe,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,eAAe,EAAE,CAAC,CAAC;IAE5D,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,6BAA6B;QAC7B,eAAe,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;QACjC,eAAe,CAAC,OAAO,GAAG,IAAI,eAAe,EAAE,CAAC;QAEhD,SAAS,CAAC,KAAK,CACb,YAAY,EACZ,UAAU,EACV;YACE,QAAQ,CAAC,OAAO;gBACd,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,IAAI,EAAE,CAAC,CAAC;YACtC,CAAC;SACF,EACD;YACE,GAAG,OAAO;YACV,MAAM,EAAE,eAAe,CAAC,OAAO,CAAC,MAAM;SACvC,CACF,CAAC;QAEF,OAAO,GAAG,EAAE;YACV,eAAe,CAAC,OAAO,EAAE,KAAK,EAAE,CAAC;QACnC,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,EAAE,YAAY,EAAE,cAAc,EAAE,eAAe,CAAC,CAAC,CAAC;IAE/D,OAAO,IAAI,CAAC;AACd,CAAC,CAAC"}
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { usePowerSync } from '../PowerSyncContext.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @deprecated use {@link useQuery} instead.
|
|
6
|
-
*
|
|
7
|
-
* A hook to access a single static query.
|
|
8
|
-
* For an updated result, use {@link usePowerSyncWatchedQuery} instead.
|
|
9
|
-
*/
|
|
10
|
-
export const usePowerSyncQuery = <T = any>(sqlStatement: string, parameters: any[] = []): T[] => {
|
|
11
|
-
const powerSync = usePowerSync();
|
|
12
|
-
if (!powerSync) {
|
|
13
|
-
return [];
|
|
14
|
-
}
|
|
15
|
-
// This will return the previous parameters until the contents of parameters array change,
|
|
16
|
-
// thereby providing a stable array reference and preventing excessive useEffect runs
|
|
17
|
-
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
|
|
18
|
-
const [data, setData] = React.useState<T[]>([]);
|
|
19
|
-
|
|
20
|
-
React.useEffect(() => {
|
|
21
|
-
powerSync.readLock(async (tx) => {
|
|
22
|
-
const result = await tx.getAll<T>(sqlStatement, parameters);
|
|
23
|
-
setData(result);
|
|
24
|
-
});
|
|
25
|
-
}, [powerSync, sqlStatement, memoizedParams]);
|
|
26
|
-
|
|
27
|
-
return data;
|
|
28
|
-
};
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { useContext, useEffect, useState } from 'react';
|
|
2
|
-
import { PowerSyncContext } from '../PowerSyncContext.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @deprecated Use {@link useStatus} instead.
|
|
6
|
-
*
|
|
7
|
-
* Custom hook that provides access to the current status of PowerSync.
|
|
8
|
-
* @returns The PowerSync Database status.
|
|
9
|
-
* @example
|
|
10
|
-
* const Component = () => {
|
|
11
|
-
* const status = usePowerSyncStatus();
|
|
12
|
-
*
|
|
13
|
-
* return <div>
|
|
14
|
-
* status.connected ? 'wifi' : 'wifi-off'
|
|
15
|
-
* </div>
|
|
16
|
-
* };
|
|
17
|
-
*/
|
|
18
|
-
export const usePowerSyncStatus = () => {
|
|
19
|
-
const powerSync = useContext(PowerSyncContext);
|
|
20
|
-
const [syncStatus, setSyncStatus] = useState(powerSync.currentStatus);
|
|
21
|
-
|
|
22
|
-
useEffect(() => {
|
|
23
|
-
const listener = powerSync.registerListener({
|
|
24
|
-
statusChanged: (status) => {
|
|
25
|
-
setSyncStatus(status);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
return () => listener?.();
|
|
30
|
-
}, [powerSync]);
|
|
31
|
-
|
|
32
|
-
return syncStatus;
|
|
33
|
-
};
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import { SQLWatchOptions } from '@powersync/common';
|
|
2
|
-
import React from 'react';
|
|
3
|
-
import { usePowerSync } from '../PowerSyncContext.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @deprecated use {@link useQuery} instead.
|
|
7
|
-
*
|
|
8
|
-
* A hook to access the results of a watched query.
|
|
9
|
-
* @example
|
|
10
|
-
* export const Component = () => {
|
|
11
|
-
* const lists = usePowerSyncWatchedQuery('SELECT * from lists');
|
|
12
|
-
*
|
|
13
|
-
* return <View>
|
|
14
|
-
* {lists.map((l) => (
|
|
15
|
-
* <Text key={l.id}>{JSON.stringify(l)}</Text>
|
|
16
|
-
* ))}
|
|
17
|
-
* </View>
|
|
18
|
-
* }
|
|
19
|
-
*/
|
|
20
|
-
export const usePowerSyncWatchedQuery = <T = any>(
|
|
21
|
-
sqlStatement: string,
|
|
22
|
-
parameters: any[] = [],
|
|
23
|
-
options: Omit<SQLWatchOptions, 'signal'> = {}
|
|
24
|
-
): T[] => {
|
|
25
|
-
const powerSync = usePowerSync();
|
|
26
|
-
if (!powerSync) {
|
|
27
|
-
return [];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const memoizedParams = React.useMemo(() => parameters, [...parameters]);
|
|
31
|
-
const memoizedOptions = React.useMemo(() => options, [JSON.stringify(options)]);
|
|
32
|
-
const [data, setData] = React.useState<T[]>([]);
|
|
33
|
-
const abortController = React.useRef(new AbortController());
|
|
34
|
-
|
|
35
|
-
React.useEffect(() => {
|
|
36
|
-
// Abort any previous watches
|
|
37
|
-
abortController.current?.abort();
|
|
38
|
-
abortController.current = new AbortController();
|
|
39
|
-
|
|
40
|
-
powerSync.watch(
|
|
41
|
-
sqlStatement,
|
|
42
|
-
parameters,
|
|
43
|
-
{
|
|
44
|
-
onResult(results) {
|
|
45
|
-
setData(results.rows?._array ?? []);
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
{
|
|
49
|
-
...options,
|
|
50
|
-
signal: abortController.current.signal
|
|
51
|
-
}
|
|
52
|
-
);
|
|
53
|
-
|
|
54
|
-
return () => {
|
|
55
|
-
abortController.current?.abort();
|
|
56
|
-
};
|
|
57
|
-
}, [powerSync, sqlStatement, memoizedParams, memoizedOptions]);
|
|
58
|
-
|
|
59
|
-
return data;
|
|
60
|
-
};
|