@powersync/react 0.0.0-dev-20261015114518 → 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/lib/QueryStore.d.ts +10 -0
- package/lib/QueryStore.js +63 -0
- package/lib/QueryStore.js.map +1 -0
- package/lib/hooks/suspense/suspense-utils.d.ts +16 -0
- package/lib/hooks/suspense/suspense-utils.js +76 -0
- package/lib/hooks/suspense/suspense-utils.js.map +1 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +10 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +70 -0
- package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +1 -0
- package/lib/hooks/suspense/useSuspenseQuery.d.ts +3 -3
- package/lib/hooks/suspense/useSuspenseQuery.js +7 -28
- package/lib/hooks/suspense/useSuspenseQuery.js.map +1 -1
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +24 -60
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js.map +1 -1
- package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +6 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +23 -0
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +1 -0
- package/lib/hooks/watched/QueryRunner.d.ts +4 -15
- package/lib/hooks/watched/QueryRunner.js +9 -35
- package/lib/hooks/watched/QueryRunner.js.map +1 -1
- package/lib/hooks/watched/watch-types.d.ts +1 -18
- package/lib/index.d.ts +1 -1
- package/package.json +5 -5
- package/src/QueryStore.ts +81 -0
- package/src/hooks/suspense/suspense-utils.ts +86 -0
- package/src/hooks/suspense/useSingleSuspenseQuery.ts +78 -0
- package/src/hooks/suspense/useSuspenseQuery.ts +13 -40
- package/src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts +25 -65
- package/src/hooks/suspense/useWatchedSuspenseQuery.ts +33 -0
- package/src/hooks/watched/QueryRunner.ts +11 -40
- package/src/hooks/watched/watch-types.ts +1 -20
- package/src/index.ts +1 -7
- package/lib/hooks/suspense/SuspenseQueryCache.d.ts +0 -46
- package/lib/hooks/suspense/SuspenseQueryCache.js +0 -231
- package/lib/hooks/suspense/SuspenseQueryCache.js.map +0 -1
- package/src/hooks/suspense/SuspenseQueryCache.ts +0 -253
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { CommonPowerSyncDatabase, WatchCompatibleQuery, WatchedQuery } from '@powersync/common';
|
|
2
|
+
import { DifferentialHookOptions } from './hooks/watched/watch-types.js';
|
|
3
|
+
export declare function generateQueryKey(sqlStatement: string, parameters: ReadonlyArray<unknown>, options: DifferentialHookOptions<unknown>): string;
|
|
4
|
+
export declare class QueryStore {
|
|
5
|
+
private db;
|
|
6
|
+
cache: Map<string, WatchedQuery<unknown, import("@powersync/common").WatchedQueryOptions, import("@powersync/common").WatchedQueryListener<unknown>>>;
|
|
7
|
+
constructor(db: CommonPowerSyncDatabase);
|
|
8
|
+
getQuery<RowType>(key: string, query: WatchCompatibleQuery<RowType[]>, options: DifferentialHookOptions<RowType>): import("@powersync/common").DifferentialWatchedQuery<RowType>;
|
|
9
|
+
}
|
|
10
|
+
export declare function getQueryStore(db: CommonPowerSyncDatabase): QueryStore;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { WatchedQueryListenerEvent } from '@powersync/common';
|
|
2
|
+
export function generateQueryKey(sqlStatement, parameters, options) {
|
|
3
|
+
return `${sqlStatement} -- ${JSON.stringify(parameters)} -- ${JSON.stringify(options)}`;
|
|
4
|
+
}
|
|
5
|
+
export class QueryStore {
|
|
6
|
+
db;
|
|
7
|
+
cache = new Map();
|
|
8
|
+
constructor(db) {
|
|
9
|
+
this.db = db;
|
|
10
|
+
}
|
|
11
|
+
getQuery(key, query, options) {
|
|
12
|
+
if (this.cache.has(key)) {
|
|
13
|
+
return this.cache.get(key);
|
|
14
|
+
}
|
|
15
|
+
const watch = options.rowComparator
|
|
16
|
+
? this.db.customQuery(query).differentialWatch({
|
|
17
|
+
rowComparator: options.rowComparator,
|
|
18
|
+
reportFetching: options.reportFetching,
|
|
19
|
+
throttleMs: options.throttleMs
|
|
20
|
+
})
|
|
21
|
+
: this.db.customQuery(query).watch({
|
|
22
|
+
reportFetching: options.reportFetching,
|
|
23
|
+
throttleMs: options.throttleMs
|
|
24
|
+
});
|
|
25
|
+
this.cache.set(key, watch);
|
|
26
|
+
const disposer = watch.registerListener({
|
|
27
|
+
closed: () => {
|
|
28
|
+
this.cache.delete(key);
|
|
29
|
+
disposer?.();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
watch.listenerMeta.registerListener({
|
|
33
|
+
listenersChanged: (counts) => {
|
|
34
|
+
// Dispose this query if there are no subscribers present
|
|
35
|
+
// We don't use the total here since we don't want to consider `onclose` listeners
|
|
36
|
+
const relevantCounts = [
|
|
37
|
+
WatchedQueryListenerEvent.ON_DATA,
|
|
38
|
+
WatchedQueryListenerEvent.ON_STATE_CHANGE,
|
|
39
|
+
WatchedQueryListenerEvent.ON_ERROR
|
|
40
|
+
].reduce((sum, event) => {
|
|
41
|
+
return sum + (counts[event] || 0);
|
|
42
|
+
}, 0);
|
|
43
|
+
if (relevantCounts == 0) {
|
|
44
|
+
watch.close();
|
|
45
|
+
this.cache.delete(key);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return watch;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
let queryStores = undefined;
|
|
53
|
+
export function getQueryStore(db) {
|
|
54
|
+
queryStores ||= new WeakMap();
|
|
55
|
+
const existing = queryStores.get(db);
|
|
56
|
+
if (existing) {
|
|
57
|
+
return existing;
|
|
58
|
+
}
|
|
59
|
+
const store = new QueryStore(db);
|
|
60
|
+
queryStores.set(db, store);
|
|
61
|
+
return store;
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=QueryStore.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryStore.js","source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
+
/**
|
|
3
|
+
* The store will dispose this query if it has no subscribers attached to it.
|
|
4
|
+
* The suspense promise adds a subscriber to the query, but the promise could resolve
|
|
5
|
+
* before this component is committed. The promise will release it's listener once the query is no longer loading.
|
|
6
|
+
* This temporary hold is used to ensure that the query is not disposed in the interim.
|
|
7
|
+
* Creates a subscription for state change which creates a temporary hold on the query
|
|
8
|
+
*/
|
|
9
|
+
export declare const useTemporaryHold: (watchedQuery?: WatchedQuery<unknown>) => void;
|
|
10
|
+
/**
|
|
11
|
+
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
12
|
+
* This creates a promise which registers a listener on the watched query.
|
|
13
|
+
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
14
|
+
* while the component is suspended.
|
|
15
|
+
*/
|
|
16
|
+
export declare const createSuspendingPromise: (query: WatchedQuery<unknown>) => Promise<void>;
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
/**
|
|
3
|
+
* The store will dispose this query if it has no subscribers attached to it.
|
|
4
|
+
* The suspense promise adds a subscriber to the query, but the promise could resolve
|
|
5
|
+
* before this component is committed. The promise will release it's listener once the query is no longer loading.
|
|
6
|
+
* This temporary hold is used to ensure that the query is not disposed in the interim.
|
|
7
|
+
* Creates a subscription for state change which creates a temporary hold on the query
|
|
8
|
+
*/
|
|
9
|
+
export const useTemporaryHold = (watchedQuery) => {
|
|
10
|
+
const releaseTemporaryHold = React.useRef(undefined);
|
|
11
|
+
const addedHoldTo = React.useRef(undefined);
|
|
12
|
+
if (addedHoldTo.current !== watchedQuery) {
|
|
13
|
+
// The query changed, we no longer need the previous hold if present
|
|
14
|
+
releaseTemporaryHold.current?.();
|
|
15
|
+
releaseTemporaryHold.current = undefined;
|
|
16
|
+
addedHoldTo.current = watchedQuery;
|
|
17
|
+
if (!watchedQuery || !watchedQuery.state.isLoading) {
|
|
18
|
+
// No query to hold or no reason to hold, return
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
// Create a hold by subscribing
|
|
22
|
+
const disposeSubscription = watchedQuery.registerListener({
|
|
23
|
+
onStateChange: (state) => { }
|
|
24
|
+
});
|
|
25
|
+
let timeout;
|
|
26
|
+
const disposeClosedListener = watchedQuery.registerListener({
|
|
27
|
+
closed: () => {
|
|
28
|
+
if (timeout) {
|
|
29
|
+
clearTimeout(timeout);
|
|
30
|
+
}
|
|
31
|
+
disposeClosedListener();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
const releaseHold = () => {
|
|
35
|
+
disposeSubscription();
|
|
36
|
+
disposeClosedListener();
|
|
37
|
+
};
|
|
38
|
+
releaseTemporaryHold.current = releaseHold;
|
|
39
|
+
const timeoutPollMs = 5_000;
|
|
40
|
+
const checkHold = () => {
|
|
41
|
+
if (watchedQuery.closed || !watchedQuery.state.isLoading || watchedQuery.state.error) {
|
|
42
|
+
// No need to keep a temporary hold on this query
|
|
43
|
+
releaseHold();
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
// Need to keep the hold, check again after timeout
|
|
47
|
+
timeout = setTimeout(checkHold, timeoutPollMs);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// Set a timeout to conditionally remove the temporary hold
|
|
51
|
+
timeout = setTimeout(checkHold, timeoutPollMs);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* React suspense relies on a promise that resolves once the initial data has loaded.
|
|
56
|
+
* This creates a promise which registers a listener on the watched query.
|
|
57
|
+
* Registering a listener on the watched query will ensure that the query is not disposed
|
|
58
|
+
* while the component is suspended.
|
|
59
|
+
*/
|
|
60
|
+
export const createSuspendingPromise = (query) => {
|
|
61
|
+
return new Promise((resolve) => {
|
|
62
|
+
// The listener here will dispose itself once the loading is done
|
|
63
|
+
// This decreases the number of listeners on the query
|
|
64
|
+
// even if the component is unmounted
|
|
65
|
+
const dispose = query.registerListener({
|
|
66
|
+
onStateChange: (state) => {
|
|
67
|
+
// Returns to the hook if loading is completed or if loading resulted in an error
|
|
68
|
+
if (!state.isLoading || state.error) {
|
|
69
|
+
resolve();
|
|
70
|
+
dispose();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=suspense-utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"suspense-utils.js","sourceRoot":"","sources":["../../../src/hooks/suspense/suspense-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,YAAoC,EAAE,EAAE;IACvE,MAAM,oBAAoB,GAAG,KAAK,CAAC,MAAM,CAA2B,SAAS,CAAC,CAAC;IAC/E,MAAM,WAAW,GAAG,KAAK,CAAC,MAAM,CAAoC,SAAS,CAAC,CAAC;IAE/E,IAAI,WAAW,CAAC,OAAO,KAAK,YAAY,EAAE,CAAC;QACzC,oEAAoE;QACpE,oBAAoB,CAAC,OAAO,EAAE,EAAE,CAAC;QACjC,oBAAoB,CAAC,OAAO,GAAG,SAAS,CAAC;QACzC,WAAW,CAAC,OAAO,GAAG,YAAY,CAAC;QAEnC,IAAI,CAAC,YAAY,IAAI,CAAC,YAAY,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;YACnD,gDAAgD;YAChD,OAAO;QACT,CAAC;QAED,+BAA+B;QAC/B,MAAM,mBAAmB,GAAG,YAAY,CAAC,gBAAgB,CAAC;YACxD,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE,GAAE,CAAC;SAC7B,CAAC,CAAC;QAEH,IAAI,OAAkD,CAAC;QAEvD,MAAM,qBAAqB,GAAG,YAAY,CAAC,gBAAgB,CAAC;YAC1D,MAAM,EAAE,GAAG,EAAE;gBACX,IAAI,OAAO,EAAE,CAAC;oBACZ,YAAY,CAAC,OAAO,CAAC,CAAC;gBACxB,CAAC;gBACD,qBAAqB,EAAE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alOptions } from '../watched/watch-types.js';
|
|
3
|
+
import { SuspenseQueryResult } from './SuspenseQueryResult.js';
|
|
4
|
+
/**
|
|
5
|
+
* Use a query which is not watched, but suspends until the initial result has loaded.
|
|
6
|
+
* Internally this uses a WatchedQuery during suspense for state management. The watched
|
|
7
|
+
* query is potentially disposed, if there are no subscribers attached to it, after the initial load.
|
|
8
|
+
* The query can be refreshed by calling the `refresh` function after initial load.
|
|
9
|
+
*/
|
|
10
|
+
export declare const useSingleSuspenseQuery: <T = any>(query: string | CompilableQuery<T>, parameters?: any[], options?: AdditionalOptions) => SuspenseQueryResult<T>;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { generateQueryKey, getQueryStore } from '../../QueryStore.js';
|
|
3
|
+
import { usePowerSync } from '../PowerSyncContext.js';
|
|
4
|
+
import { constructCompatibleQuery } from '../watched/watch-utils.js';
|
|
5
|
+
import { createSuspendingPromise, useTemporaryHold } from './suspense-utils.js';
|
|
6
|
+
/**
|
|
7
|
+
* Use a query which is not watched, but suspends until the initial result has loaded.
|
|
8
|
+
* Internally this uses a WatchedQuery during suspense for state management. The watched
|
|
9
|
+
* query is potentially disposed, if there are no subscribers attached to it, after the initial load.
|
|
10
|
+
* The query can be refreshed by calling the `refresh` function after initial load.
|
|
11
|
+
*/
|
|
12
|
+
export const useSingleSuspenseQuery = (query, parameters = [], options = {}) => {
|
|
13
|
+
const powerSync = usePowerSync();
|
|
14
|
+
// Manually track data for single queries
|
|
15
|
+
const [data, setData] = React.useState(null);
|
|
16
|
+
const [error, setError] = React.useState(null);
|
|
17
|
+
// Note, we don't need to check if the query changed since we fetch the WatchedQuery
|
|
18
|
+
// from the store given these query params
|
|
19
|
+
const parsedQuery = constructCompatibleQuery(query, parameters);
|
|
20
|
+
const { sql: parsedSql, parameters: parsedParameters } = parsedQuery.compile();
|
|
21
|
+
const key = generateQueryKey(parsedSql, parsedParameters, options);
|
|
22
|
+
const store = getQueryStore(powerSync);
|
|
23
|
+
// Only use a temporary watched query if we don't have data yet.
|
|
24
|
+
const watchedQuery = data ? undefined : store.getQuery(key, parsedQuery, options);
|
|
25
|
+
useTemporaryHold(watchedQuery);
|
|
26
|
+
React.useEffect(() => {
|
|
27
|
+
// Set the initial yielded data
|
|
28
|
+
// it should be available once we commit the component
|
|
29
|
+
if (watchedQuery?.state.error) {
|
|
30
|
+
setError(watchedQuery.state.error);
|
|
31
|
+
}
|
|
32
|
+
else if (watchedQuery?.state.isLoading === false) {
|
|
33
|
+
setData(watchedQuery.state.data);
|
|
34
|
+
setError(null);
|
|
35
|
+
}
|
|
36
|
+
}, []);
|
|
37
|
+
if (error != null) {
|
|
38
|
+
// Report errors - this is caught by an error boundary
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
else if (data || watchedQuery?.state.isLoading === false) {
|
|
42
|
+
// Happy path data return
|
|
43
|
+
return {
|
|
44
|
+
data: data ?? watchedQuery?.state.data ?? [],
|
|
45
|
+
refresh: async (signal) => {
|
|
46
|
+
try {
|
|
47
|
+
const compiledQuery = parsedQuery.compile();
|
|
48
|
+
const result = await parsedQuery.execute({
|
|
49
|
+
sql: compiledQuery.sql,
|
|
50
|
+
parameters: [...compiledQuery.parameters],
|
|
51
|
+
db: powerSync
|
|
52
|
+
});
|
|
53
|
+
if (signal?.aborted) {
|
|
54
|
+
return; // Abort if the signal is already aborted
|
|
55
|
+
}
|
|
56
|
+
setData(result);
|
|
57
|
+
setError(null);
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
setError(e);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
// Notify suspense is required
|
|
67
|
+
throw createSuspendingPromise(watchedQuery);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
//# sourceMappingURL=useSingleSuspenseQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSingle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AC,CAAU,CAAC,CAAC;gBACvB,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,8BAA8B;QAC9B,MAAM,uBAAuB,CAAC,YAAa,CAAC,CAAC;IAC/C,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { CompilableQuery } from '@powersync/common';
|
|
2
|
-
import {
|
|
2
|
+
import { AdditionalOptions, DifferentialHookOptions } from '../watched/watch-types.js';
|
|
3
3
|
import { ReadonlySuspenseQueryResult, SuspenseQueryResult } from './SuspenseQueryResult.js';
|
|
4
4
|
/**
|
|
5
5
|
* A hook to access the results of a watched query that suspends until the initial result has loaded.
|
|
@@ -49,5 +49,5 @@ import { ReadonlySuspenseQueryResult, SuspenseQueryResult } from './SuspenseQuer
|
|
|
49
49
|
* );
|
|
50
50
|
* }
|
|
51
51
|
*/
|
|
52
|
-
export declare function useSuspenseQuery<RowType = any>(query: string | CompilableQuery<RowType>, parameters?: any[], options?:
|
|
53
|
-
export declare function useSuspenseQuery<RowType = any>(query: string | CompilableQuery<RowType>,
|
|
52
|
+
export declare function useSuspenseQuery<RowType = any>(query: string | CompilableQuery<RowType>, parameters?: any[], options?: AdditionalOptions): SuspenseQueryResult<RowType>;
|
|
53
|
+
export declare function useSuspenseQuery<RowType = any>(query: string | CompilableQuery<RowType>, paramerers?: any[], options?: DifferentialHookOptions<RowType>): ReadonlySuspenseQueryResult<RowType>;
|
|
@@ -1,32 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { configError, configKey, resolveConfig } from '../watched/QueryRunner.js';
|
|
4
|
-
import { constructCompatibleQuery } from '../watched/watch-utils.js';
|
|
5
|
-
import { getSuspenseQueryCache } from './SuspenseQueryCache.js';
|
|
1
|
+
import { useSingleSuspenseQuery } from './useSingleSuspenseQuery.js';
|
|
2
|
+
import { useWatchedSuspenseQuery } from './useWatchedSuspenseQuery.js';
|
|
6
3
|
export function useSuspenseQuery(query, parameters = [], options = {}) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
true);
|
|
13
|
-
if (config.kind != 'query') {
|
|
14
|
-
// Missing database or a query that failed to compile, report.
|
|
15
|
-
throw configError(config);
|
|
4
|
+
switch (options?.runQueryOnce) {
|
|
5
|
+
case true:
|
|
6
|
+
return useSingleSuspenseQuery(query, parameters, options);
|
|
7
|
+
default:
|
|
8
|
+
return useWatchedSuspenseQuery(query, parameters, options);
|
|
16
9
|
}
|
|
17
|
-
// A component that suspends before its first commit loses all its state, so the runner for this query is stored in
|
|
18
|
-
// a cache shared between components instead of in a ref.
|
|
19
|
-
const cache = getSuspenseQueryCache(config.db);
|
|
20
|
-
const key = configKey(config);
|
|
21
|
-
const suspenseQuery = useMemo(() => cache.query(key, config), [cache, key]);
|
|
22
|
-
suspenseQuery.markUsed();
|
|
23
|
-
const result = useSyncExternalStore(suspenseQuery.subscribe, suspenseQuery.getSnapshot, suspenseQuery.getSnapshot);
|
|
24
|
-
if (result.error != null) {
|
|
25
|
-
throw result.error;
|
|
26
|
-
}
|
|
27
|
-
else if (result.isLoading) {
|
|
28
|
-
throw suspenseQuery.waitForLoaded();
|
|
29
|
-
}
|
|
30
|
-
return { data: result.data, refresh: result.refresh };
|
|
31
10
|
}
|
|
32
11
|
//# sourceMappingURL=useSuspenseQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSuspenseQuery.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useSuspenseQuery.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useSuspenseQuery.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useSuspenseQuery.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AA4DvE,MAAM,UAAU,gBAAgB,CAC9B,KAAwC,EACxC,aAAoB,EAAE,EACtB,UAAgE,EAAE;IAElE,QAAQ,OAAO,EAAE,YAAY,EAAE,CAAC;QAC9B,KAAK,IAAI;YACP,OAAO,sBAAsB,CAAU,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;QACrE;YACE,OAAO,uBAAuB,CAAU,KAAK,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;IACxE,CAAC;AACH,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createSuspendingPromise, useTemporaryHold } from './suspense-utils.js';
|
|
2
3
|
/**
|
|
3
4
|
* A hook to access and subscribe to the results of an existing {@link WatchedQuery}.
|
|
4
5
|
* @example
|
|
@@ -21,68 +22,31 @@ import { useCallback, useSyncExternalStore } from 'react';
|
|
|
21
22
|
* }
|
|
22
23
|
*/
|
|
23
24
|
export const useWatchedQuerySuspenseSubscription = (query) => {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
useTemporaryHold(query);
|
|
26
|
+
// Force update state function
|
|
27
|
+
const [, setUpdateCounter] = React.useState(0);
|
|
28
|
+
React.useEffect(() => {
|
|
29
|
+
// This runs when the component came out of suspense
|
|
30
|
+
// This add a permanent hold since a listener has been added to the query
|
|
31
|
+
const dispose = query.registerListener({
|
|
32
|
+
onStateChange() {
|
|
33
|
+
// Trigger rerender
|
|
34
|
+
setUpdateCounter((prev) => prev + 1);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return dispose;
|
|
38
|
+
}, []);
|
|
39
|
+
if (query.state.error != null) {
|
|
40
|
+
// Report errors - this is caught by an error boundary
|
|
41
|
+
throw query.state.error;
|
|
29
42
|
}
|
|
30
|
-
else if (!state.isLoading) {
|
|
31
|
-
return
|
|
43
|
+
else if (!query.state.isLoading) {
|
|
44
|
+
// Happy path data return
|
|
45
|
+
return query.state;
|
|
32
46
|
}
|
|
33
47
|
else {
|
|
34
|
-
//
|
|
35
|
-
throw
|
|
48
|
+
// Notify suspense is required
|
|
49
|
+
throw createSuspendingPromise(query);
|
|
36
50
|
}
|
|
37
51
|
};
|
|
38
|
-
let suspendingPromises;
|
|
39
|
-
let stateSnapshots;
|
|
40
|
-
/**
|
|
41
|
-
* A promise that resolves once the query has loaded, cached to be suitable for hooks.
|
|
42
|
-
*/
|
|
43
|
-
function suspendUntilDataOrError(query) {
|
|
44
|
-
const promises = (suspendingPromises ??= new WeakMap());
|
|
45
|
-
const existing = promises.get(query);
|
|
46
|
-
if (existing) {
|
|
47
|
-
return existing;
|
|
48
|
-
}
|
|
49
|
-
const promise = new Promise((resolve) => {
|
|
50
|
-
// The listener here will dispose itself once the loading is done.
|
|
51
|
-
// This decreases the number of listeners on the query even if the component is unmounted.
|
|
52
|
-
const dispose = query.registerListener({
|
|
53
|
-
onStateChange: (state) => {
|
|
54
|
-
// Returns to the hook if loading is completed or if loading resulted in an error
|
|
55
|
-
if (!state.isLoading || state.error) {
|
|
56
|
-
promises.delete(query);
|
|
57
|
-
resolve();
|
|
58
|
-
dispose();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
promises.set(query, promise);
|
|
64
|
-
return promise;
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Returns a stable copy of the query's state.
|
|
68
|
-
*
|
|
69
|
-
* Watched queries update their `state` object in place, so it can't be used as a snapshot for `useSyncExternalStore`
|
|
70
|
-
* directly.
|
|
71
|
-
*/
|
|
72
|
-
function snapshotState(query) {
|
|
73
|
-
const state = query.state;
|
|
74
|
-
const snapshots = (stateSnapshots ??= new WeakMap());
|
|
75
|
-
const cached = snapshots.get(query);
|
|
76
|
-
if (cached != null &&
|
|
77
|
-
cached.data === state.data &&
|
|
78
|
-
cached.isLoading === state.isLoading &&
|
|
79
|
-
cached.isFetching === state.isFetching &&
|
|
80
|
-
cached.error === state.error &&
|
|
81
|
-
cached.lastUpdated === state.lastUpdated) {
|
|
82
|
-
return cached;
|
|
83
|
-
}
|
|
84
|
-
const snapshot = { ...state };
|
|
85
|
-
snapshots.set(query, snapshot);
|
|
86
|
-
return snapshot;
|
|
87
|
-
}
|
|
88
52
|
//# sourceMappingURL=useWatchedQuerySuspenseSubscription.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWatchedQuerySuspenseSubscription.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useWatchedQuerySuspenseSubscription.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEhF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAIjD,KAAY,EACI,EAAE;IAClB,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAExB,8BAA8B;IAC9B,MAAM,CAAC,EAAE,gBAAgB,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAE/C,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,oDAAoD;QACpD,yEAAyE;QACzE,MAAM,OAAO,GAAG,KAAK,CAAC,gBAAgB,CAAC;YACrC,aAAa;gBACX,mBAAmB;gBACnB,gBAAgB,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;YACvC,CAAC;SACF,CAAC,CAAC;QAEH,OAAO,OAAO,CAAC;IACjB,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,IAAI,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,IAAI,EAAE,CAAC;QAC9B,sDAAsD;QACtD,MAAM,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC;IAC1B,CAAC;SAAM,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,SAAS,EAAE,CAAC;QAClC,yBAAyB;QACzB,OAAO,KAAK,CAAC,KAAK,CAAC;IACrB,CAAC;SAAM,CAAC;QACN,8BAA8B;QAC9B,MAAM,uBAAuB,CAAC,KAAK,CAAC,CAAC;IACvC,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { CompilableQuery } from '@powersync/common';
|
|
2
|
+
import { AdditionalOptions } from '../watched/watch-types.js';
|
|
3
|
+
/**
|
|
4
|
+
* @internal This is not exported in the index.ts
|
|
5
|
+
*/
|
|
6
|
+
export declare const useWatchedSuspenseQuery: <T = any>(query: string | CompilableQuery<T>, parameters?: any[], options?: AdditionalOptions) => import("@powersync/common").WatchedQueryState<readonly Readonly<T>[]>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { generateQueryKey, getQueryStore } from '../../QueryStore.js';
|
|
2
|
+
import { usePowerSync } from '../PowerSyncContext.js';
|
|
3
|
+
import { constructCompatibleQuery } from '../watched/watch-utils.js';
|
|
4
|
+
import { useWatchedQuerySuspenseSubscription } from './useWatchedQuerySuspenseSubscription.js';
|
|
5
|
+
/**
|
|
6
|
+
* @internal This is not exported in the index.ts
|
|
7
|
+
*/
|
|
8
|
+
export const useWatchedSuspenseQuery = (query, parameters = [], options = {}) => {
|
|
9
|
+
const powerSync = usePowerSync();
|
|
10
|
+
// Note, we don't need to check if the query changed since we fetch the WatchedQuery
|
|
11
|
+
// from the store given these query params
|
|
12
|
+
const parsedQuery = constructCompatibleQuery(query, parameters);
|
|
13
|
+
const { sql: parsedSql, parameters: parsedParameters } = parsedQuery.compile();
|
|
14
|
+
const key = generateQueryKey(parsedSql, parsedParameters, options);
|
|
15
|
+
// When the component is suspended, all state is discarded. We don't get
|
|
16
|
+
// any notification of that. So checkoutQuery reserves a temporary hold
|
|
17
|
+
// on the query.
|
|
18
|
+
// Once the component "commits", we exchange that for a permanent hold.
|
|
19
|
+
const store = getQueryStore(powerSync);
|
|
20
|
+
const watchedQuery = store.getQuery(key, parsedQuery, options);
|
|
21
|
+
return useWatchedQuerySuspenseSubscription(watchedQuery);
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=useWatchedSuspenseQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useWatchedSuspenseQuery.js","sourceRoot":"","sources":["../../../src/hooks/suspense/useWatchedSuspenseQuery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAEtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,mCAAmC,EAAE,MAAM,0CAA0C,CAAC;AAE/F;;GAEG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CACrC,KAAkC,EAClC,aAAoB,EAAE,EACtB,UAA6B,EAAE,EAC/B,EAAE;IACF,MAAM,SAAS,GAAG,YAAY,EAAE,CAAC;IAEjC,oFAAoF;IACpF,0CAA0C;IAC1C,MAAM,WAAW,GAAG,wBAAwB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IAChE,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,UAAU,EAAE,gBAAgB,EAAE,GAAG,WAAW,CAAC,OAAO,EAAE,CAAC;IAE/E,MAAM,GAAG,GAAG,gBAAgB,CAAC,SAAS,EAAE,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAEnE,wEAAwE;IACxE,uEAAuE;IACvE,gBAAgB;IAChB,uEAAuE;IACvE,MAAM,KAAK,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IACvC,MAAM,YAAY,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;IAE/D,OAAO,mCAAmC,CAAC,YAAY,CAAC,CAAC;AAC3D,CAAC,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { BaseListener, BaseObserver, CommonPowerSyncDatabase, DifferentialWatchedQueryComparator } from '@powersync/common';
|
|
2
2
|
import { AdditionalOptions, DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
3
3
|
import { WatchCompatibleQueryWithParams } from './watch-utils.js';
|
|
4
|
-
|
|
4
|
+
type Result<RowType> = QueryResult<RowType> | ReadonlyQueryResult<RowType>;
|
|
5
5
|
/**
|
|
6
6
|
* The configured state a {@link QueryRunner} is in.
|
|
7
7
|
*
|
|
@@ -16,7 +16,7 @@ export type RunnerConfig<RowType> = {
|
|
|
16
16
|
kind: 'compile-error';
|
|
17
17
|
error: Error;
|
|
18
18
|
} | QueryConfig<RowType>;
|
|
19
|
-
|
|
19
|
+
interface QueryConfig<RowType> {
|
|
20
20
|
kind: 'query';
|
|
21
21
|
db: CommonPowerSyncDatabase;
|
|
22
22
|
query: WatchCompatibleQueryWithParams<RowType[]>;
|
|
@@ -28,15 +28,6 @@ export interface QueryConfig<RowType> {
|
|
|
28
28
|
throttleMs: number | undefined;
|
|
29
29
|
reportFetching: boolean;
|
|
30
30
|
}
|
|
31
|
-
/**
|
|
32
|
-
* Returns a string identifying a query config, such that two configs have the same key if {@link configEquals} considers
|
|
33
|
-
* them equal (apart from the database, which callers are expected to scope keys to).
|
|
34
|
-
*/
|
|
35
|
-
export declare function configKey<RowType>(config: QueryConfig<RowType>): string;
|
|
36
|
-
/**
|
|
37
|
-
* For configs that can't run a query, returns the error to report.
|
|
38
|
-
*/
|
|
39
|
-
export declare function configError<RowType>(config: Exclude<RunnerConfig<RowType>, QueryConfig<RowType>>): Error | null;
|
|
40
31
|
/**
|
|
41
32
|
* Resolves the inputs of `useQuery` into a comparable {@link RunnerConfig}.
|
|
42
33
|
*
|
|
@@ -80,11 +71,9 @@ export declare class QueryRunner<RowType> extends BaseObserver<QueryRunnerListen
|
|
|
80
71
|
* Applies the inputs of a committed render to this runner. This does nothing if they haven't changed.
|
|
81
72
|
*
|
|
82
73
|
* This modifies state and should not run during a render.
|
|
83
|
-
*
|
|
84
|
-
* `initial` can be an existing result from another runner. This is used for one-shot suspense queries where state is
|
|
85
|
-
* only shared in the initial loading state and then forked to give each component an independent runner.
|
|
86
74
|
*/
|
|
87
|
-
configure(next: RunnerConfig<RowType
|
|
75
|
+
configure(next: RunnerConfig<RowType>): void;
|
|
88
76
|
private startWork;
|
|
89
77
|
private notifyListeners;
|
|
90
78
|
}
|
|
79
|
+
export {};
|
|
@@ -22,20 +22,6 @@ function configEquals(a, b) {
|
|
|
22
22
|
a.reportFetching == b.reportFetching);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
/**
|
|
26
|
-
* Returns a string identifying a query config, such that two configs have the same key if {@link configEquals} considers
|
|
27
|
-
* them equal (apart from the database, which callers are expected to scope keys to).
|
|
28
|
-
*/
|
|
29
|
-
export function configKey(config) {
|
|
30
|
-
const { runOnce, rowComparator, throttleMs, reportFetching, sql, stringifiedParams } = config;
|
|
31
|
-
return JSON.stringify([runOnce, !!rowComparator, throttleMs ?? null, reportFetching, sql, stringifiedParams]);
|
|
32
|
-
}
|
|
33
|
-
/**
|
|
34
|
-
* For configs that can't run a query, returns the error to report.
|
|
35
|
-
*/
|
|
36
|
-
export function configError(config) {
|
|
37
|
-
return idleResultFor(config).error ?? null;
|
|
38
|
-
}
|
|
39
25
|
const _loadingState = { isLoading: true, isFetching: false, data: [], error: undefined };
|
|
40
26
|
function idleResultFor(config) {
|
|
41
27
|
switch (config.kind) {
|
|
@@ -156,19 +142,15 @@ export class QueryRunner extends BaseObserver {
|
|
|
156
142
|
* Applies the inputs of a committed render to this runner. This does nothing if they haven't changed.
|
|
157
143
|
*
|
|
158
144
|
* This modifies state and should not run during a render.
|
|
159
|
-
*
|
|
160
|
-
* `initial` can be an existing result from another runner. This is used for one-shot suspense queries where state is
|
|
161
|
-
* only shared in the initial loading state and then forked to give each component an independent runner.
|
|
162
145
|
*/
|
|
163
|
-
configure(next
|
|
146
|
+
configure(next) {
|
|
164
147
|
if (this.config != null && configEquals(this.config, next)) {
|
|
165
148
|
return;
|
|
166
149
|
}
|
|
167
150
|
const pending = this.uncommitedConfigChange;
|
|
168
151
|
this.config = next;
|
|
169
|
-
this.idleResult =
|
|
170
|
-
|
|
171
|
-
: pending != null && pending.isOptimisticStateChange && configEquals(pending.config, next)
|
|
152
|
+
this.idleResult =
|
|
153
|
+
pending != null && pending.isOptimisticStateChange && configEquals(pending.config, next)
|
|
172
154
|
? // We already returned this as an optimistic state in a previous resultFor call. Use the same result to not
|
|
173
155
|
// cause another render.
|
|
174
156
|
pending.result
|
|
@@ -205,38 +187,30 @@ class SingleQueryWork {
|
|
|
205
187
|
constructor(config, initial, emit) {
|
|
206
188
|
this.config = config;
|
|
207
189
|
this.emit = emit;
|
|
208
|
-
if (!initial.isLoading && initial.error == null) {
|
|
209
|
-
// Seeded with an existing result, no need to run it again.
|
|
210
|
-
this.result = { ...initial, refresh: this.refresh };
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
const fetchReason = 'initial';
|
|
214
190
|
this.result = {
|
|
215
191
|
...initial,
|
|
216
192
|
isFetching: true,
|
|
217
|
-
fetchReason,
|
|
193
|
+
fetchReason: 'initial',
|
|
218
194
|
refresh: this.refresh
|
|
219
195
|
};
|
|
220
|
-
this.run(
|
|
196
|
+
this.run();
|
|
221
197
|
}
|
|
222
198
|
refresh = async (signal) => {
|
|
223
199
|
if (this.disposed)
|
|
224
200
|
return;
|
|
225
|
-
await this.run(
|
|
201
|
+
await this.run(signal);
|
|
226
202
|
};
|
|
227
203
|
setResult(update) {
|
|
228
204
|
this.result = { ...this.result, ...update };
|
|
229
205
|
this.emit();
|
|
230
206
|
}
|
|
231
|
-
async run(
|
|
207
|
+
async run(outerSignal) {
|
|
232
208
|
this.abortController?.abort();
|
|
233
209
|
const controller = (this.abortController = new AbortController());
|
|
234
210
|
const signal = controller.signal;
|
|
235
211
|
const isAborted = () => signal.aborted || outerSignal?.aborted == true;
|
|
236
|
-
if (!this.result.isLoading || this.result.error) {
|
|
237
|
-
|
|
238
|
-
const isLoading = fetchReason === 'initial' || this.result.error != null;
|
|
239
|
-
this.setResult({ isLoading, isFetching: true, error: undefined, fetchReason });
|
|
212
|
+
if (!this.result.isLoading || !this.result.isFetching || this.result.error) {
|
|
213
|
+
this.setResult({ isLoading: true, isFetching: true, error: undefined, fetchReason: 'initial' });
|
|
240
214
|
}
|
|
241
215
|
const { query, sql, parameters, db } = this.config;
|
|
242
216
|
try {
|