@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
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { QueryResult } from './watch-types.js';
|
|
2
|
-
import { InternalHookOptions } from './watch-utils.js';
|
|
3
|
-
/**
|
|
4
|
-
* @internal not exported from `index.ts`
|
|
5
|
-
*/
|
|
6
|
-
export declare const useSingleQuery: <RowType = any>(options: InternalHookOptions<RowType[]>) => QueryResult<RowType>;
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
/**
|
|
3
|
-
* @internal not exported from `index.ts`
|
|
4
|
-
*/
|
|
5
|
-
export const useSingleQuery = (options) => {
|
|
6
|
-
const { query, powerSync, queryChanged, active } = options;
|
|
7
|
-
const [output, setOutputState] = React.useState({
|
|
8
|
-
isLoading: true,
|
|
9
|
-
isFetching: true,
|
|
10
|
-
data: [],
|
|
11
|
-
error: undefined
|
|
12
|
-
});
|
|
13
|
-
const runQuery = React.useCallback(async (signal) => {
|
|
14
|
-
setOutputState((prev) => ({ ...prev, isLoading: true, isFetching: true, error: undefined }));
|
|
15
|
-
try {
|
|
16
|
-
const compiledQuery = query.compile();
|
|
17
|
-
const result = await query.execute({
|
|
18
|
-
sql: compiledQuery.sql,
|
|
19
|
-
parameters: [...compiledQuery.parameters],
|
|
20
|
-
db: powerSync
|
|
21
|
-
});
|
|
22
|
-
if (signal.aborted) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
setOutputState((prev) => ({
|
|
26
|
-
...prev,
|
|
27
|
-
isLoading: false,
|
|
28
|
-
isFetching: false,
|
|
29
|
-
data: result,
|
|
30
|
-
error: undefined
|
|
31
|
-
}));
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
setOutputState((prev) => ({
|
|
35
|
-
...prev,
|
|
36
|
-
isLoading: false,
|
|
37
|
-
isFetching: false,
|
|
38
|
-
data: [],
|
|
39
|
-
error
|
|
40
|
-
}));
|
|
41
|
-
}
|
|
42
|
-
}, [queryChanged, query]);
|
|
43
|
-
// Trigger initial query execution
|
|
44
|
-
// @ts-ignore: Complains about not all code paths returning a value
|
|
45
|
-
React.useEffect(() => {
|
|
46
|
-
if (active) {
|
|
47
|
-
const abortController = new AbortController();
|
|
48
|
-
runQuery(abortController.signal);
|
|
49
|
-
return () => {
|
|
50
|
-
abortController.abort();
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
}, [powerSync, active, queryChanged]);
|
|
54
|
-
return {
|
|
55
|
-
...output,
|
|
56
|
-
refresh: runQuery
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
//# sourceMappingURL=useSingleQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useSingleQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useSingleQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAI1B;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAgB,OAAuC,EAAwB,EAAE;IAC7G,MAAM,E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ACN,CAAC,CAAC,CAAC;QACN,CAAC;IACH,CAAC,EACD,CAAC,YAAY,EAAE,KAAK,CAAC,CACtB,CAAC;IAEF,kCAAkC;IAClC,mEAAmE;IACnE,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,eAAe,GAAG,IAAI,eAAe,EAAE,CAAC;YAC9C,QAAQ,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;YACjC,OAAO,GAAG,EAAE;gBACV,eAAe,CAAC,KAAK,EAAE,CAAC;YAC1B,CAAC,CAAC;QACJ,CAAC;IACH,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC;IAEtC,OAAO;QACL,GAAG,MAAM;QACT,OAAO,EAAE,QAAQ;KAClB,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
2
|
-
import { InternalHookOptions } from './watch-utils.js';
|
|
3
|
-
/**
|
|
4
|
-
* @internal This is not exported from the index.ts
|
|
5
|
-
*
|
|
6
|
-
* When an incremental query is used the return type is readonly. This is required
|
|
7
|
-
* since the implementation requires a stable ref.
|
|
8
|
-
* For legacy compatibility we allow mutating when a standard query is used. Mutations should
|
|
9
|
-
* not affect the internal implementation in this case.
|
|
10
|
-
*/
|
|
11
|
-
export declare const useWatchedQuery: <RowType = unknown>(options: InternalHookOptions<RowType[]> & {
|
|
12
|
-
options: DifferentialHookOptions<RowType>;
|
|
13
|
-
}) => QueryResult<RowType> | ReadonlyQueryResult<RowType>;
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { useNullableWatchedQuerySubscription } from './useWatchedQuerySubscription.js';
|
|
3
|
-
/**
|
|
4
|
-
* @internal This is not exported from the index.ts
|
|
5
|
-
*
|
|
6
|
-
* When an incremental query is used the return type is readonly. This is required
|
|
7
|
-
* since the implementation requires a stable ref.
|
|
8
|
-
* For legacy compatibility we allow mutating when a standard query is used. Mutations should
|
|
9
|
-
* not affect the internal implementation in this case.
|
|
10
|
-
*/
|
|
11
|
-
export const useWatchedQuery = (options) => {
|
|
12
|
-
const { query, powerSync, queryChanged, options: hookOptions, active } = options;
|
|
13
|
-
function createWatchedQuery() {
|
|
14
|
-
if (!active) {
|
|
15
|
-
return null;
|
|
16
|
-
}
|
|
17
|
-
const watch = hookOptions.rowComparator
|
|
18
|
-
? powerSync.customQuery(query).differentialWatch({
|
|
19
|
-
rowComparator: hookOptions.rowComparator,
|
|
20
|
-
reportFetching: hookOptions.reportFetching,
|
|
21
|
-
throttleMs: hookOptions.throttleMs
|
|
22
|
-
})
|
|
23
|
-
: powerSync.customQuery(query).watch({
|
|
24
|
-
reportFetching: hookOptions.reportFetching,
|
|
25
|
-
throttleMs: hookOptions.throttleMs
|
|
26
|
-
});
|
|
27
|
-
return watch;
|
|
28
|
-
}
|
|
29
|
-
const [watchedQuery, setWatchedQuery] = React.useState(createWatchedQuery);
|
|
30
|
-
const disposePendingUpdateListener = React.useRef(null);
|
|
31
|
-
React.useEffect(() => {
|
|
32
|
-
watchedQuery?.close();
|
|
33
|
-
const newQuery = createWatchedQuery();
|
|
34
|
-
setWatchedQuery(newQuery);
|
|
35
|
-
return () => {
|
|
36
|
-
disposePendingUpdateListener.current?.();
|
|
37
|
-
newQuery?.close();
|
|
38
|
-
};
|
|
39
|
-
}, [powerSync, active]);
|
|
40
|
-
/**
|
|
41
|
-
* Indicates that the query will be re-fetched due to a change in the query.
|
|
42
|
-
* We execute this in-line (not using an effect) since effects are delayed till after the hook returns.
|
|
43
|
-
* The `queryChanged` value should only be true for a single render.
|
|
44
|
-
* The `updateSettings` method is asynchronous, thus it will update the state asynchronously.
|
|
45
|
-
* In the React hooks we'd like to report that we are fetching the data for an updated query
|
|
46
|
-
* as soon as the query has been updated. This prevents a result flow where e.g. the hook:
|
|
47
|
-
* - already returned a result: isLoading, isFetching are both false
|
|
48
|
-
* - the query is updated, but the state is still isFetching=false from the previous state
|
|
49
|
-
* We override the isFetching status while the `updateSettings` method is running (if we report `isFetching`),
|
|
50
|
-
* we override this value just until the `updateSettings` method itself will update the `isFetching` status.
|
|
51
|
-
* We achieve this by registering a `settingsWillUpdate` listener on the `WatchedQuery`. This will fire
|
|
52
|
-
* just before the `isFetching` status is updated.
|
|
53
|
-
*/
|
|
54
|
-
if (queryChanged) {
|
|
55
|
-
// Keep track of this pending operation
|
|
56
|
-
watchedQuery?.updateSettings({
|
|
57
|
-
query,
|
|
58
|
-
throttleMs: hookOptions.throttleMs,
|
|
59
|
-
reportFetching: hookOptions.reportFetching
|
|
60
|
-
});
|
|
61
|
-
// This could have been called multiple times, clear any old listeners.
|
|
62
|
-
disposePendingUpdateListener.current?.();
|
|
63
|
-
disposePendingUpdateListener.current = watchedQuery?.registerListener({
|
|
64
|
-
settingsWillUpdate: () => {
|
|
65
|
-
// We'll use the fact that we have a listener at all as an indication
|
|
66
|
-
disposePendingUpdateListener.current?.();
|
|
67
|
-
disposePendingUpdateListener.current = null;
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
const shouldReportCurrentlyFetching = (hookOptions.reportFetching ?? true) && !!disposePendingUpdateListener.current;
|
|
72
|
-
const result = useNullableWatchedQuerySubscription(watchedQuery);
|
|
73
|
-
// Result is only undefined when there is no data available yet, defaults are defined accordingly
|
|
74
|
-
return {
|
|
75
|
-
data: result?.data ?? [],
|
|
76
|
-
isLoading: result?.isLoading ?? true,
|
|
77
|
-
error: result?.error,
|
|
78
|
-
isFetching: result?.isFetching || shouldReportCurrentlyFetching
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
//# sourceMappingURL=useWatchedQuery.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useWatchedQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useWatchedQuery.ts"],"names":[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AAE,KAAK,EAAE,CAAC;QACpB,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC;IAExB;;;;;;;;;;;;;OAaG;IACH,IAAI,YAAY,EAAE,CAAC;QACjB,uCAAuC;QACvC,YAAY,EAAE,cAAc,CAAC;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"}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { QueryResult } from './watch-types.js';
|
|
3
|
-
import { InternalHookOptions } from './watch-utils.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @internal not exported from `index.ts`
|
|
7
|
-
*/
|
|
8
|
-
export const useSingleQuery = <RowType = any>(options: InternalHookOptions<RowType[]>): QueryResult<RowType> => {
|
|
9
|
-
const { query, powerSync, queryChanged, active } = options;
|
|
10
|
-
|
|
11
|
-
const [output, setOutputState] = React.useState<QueryResult<RowType>>({
|
|
12
|
-
isLoading: true,
|
|
13
|
-
isFetching: true,
|
|
14
|
-
data: [],
|
|
15
|
-
error: undefined
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
const runQuery = React.useCallback(
|
|
19
|
-
async (signal?: AbortSignal) => {
|
|
20
|
-
setOutputState((prev) => ({ ...prev, isLoading: true, isFetching: true, error: undefined }));
|
|
21
|
-
try {
|
|
22
|
-
const compiledQuery = query.compile();
|
|
23
|
-
const result = await query.execute({
|
|
24
|
-
sql: compiledQuery.sql,
|
|
25
|
-
parameters: [...compiledQuery.parameters],
|
|
26
|
-
db: powerSync
|
|
27
|
-
});
|
|
28
|
-
if (signal.aborted) {
|
|
29
|
-
return;
|
|
30
|
-
}
|
|
31
|
-
setOutputState((prev) => ({
|
|
32
|
-
...prev,
|
|
33
|
-
isLoading: false,
|
|
34
|
-
isFetching: false,
|
|
35
|
-
data: result,
|
|
36
|
-
error: undefined
|
|
37
|
-
}));
|
|
38
|
-
} catch (error) {
|
|
39
|
-
setOutputState((prev) => ({
|
|
40
|
-
...prev,
|
|
41
|
-
isLoading: false,
|
|
42
|
-
isFetching: false,
|
|
43
|
-
data: [],
|
|
44
|
-
error
|
|
45
|
-
}));
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
[queryChanged, query]
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
// Trigger initial query execution
|
|
52
|
-
// @ts-ignore: Complains about not all code paths returning a value
|
|
53
|
-
React.useEffect(() => {
|
|
54
|
-
if (active) {
|
|
55
|
-
const abortController = new AbortController();
|
|
56
|
-
runQuery(abortController.signal);
|
|
57
|
-
return () => {
|
|
58
|
-
abortController.abort();
|
|
59
|
-
};
|
|
60
|
-
}
|
|
61
|
-
}, [powerSync, active, queryChanged]);
|
|
62
|
-
|
|
63
|
-
return {
|
|
64
|
-
...output,
|
|
65
|
-
refresh: runQuery
|
|
66
|
-
};
|
|
67
|
-
};
|
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { useNullableWatchedQuerySubscription } from './useWatchedQuerySubscription.js';
|
|
3
|
-
import { DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
4
|
-
import { InternalHookOptions } from './watch-utils.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* @internal This is not exported from the index.ts
|
|
8
|
-
*
|
|
9
|
-
* When an incremental query is used the return type is readonly. This is required
|
|
10
|
-
* since the implementation requires a stable ref.
|
|
11
|
-
* For legacy compatibility we allow mutating when a standard query is used. Mutations should
|
|
12
|
-
* not affect the internal implementation in this case.
|
|
13
|
-
*/
|
|
14
|
-
export const useWatchedQuery = <RowType = unknown>(
|
|
15
|
-
options: InternalHookOptions<RowType[]> & { options: DifferentialHookOptions<RowType> }
|
|
16
|
-
): QueryResult<RowType> | ReadonlyQueryResult<RowType> => {
|
|
17
|
-
const { query, powerSync, queryChanged, options: hookOptions, active } = options;
|
|
18
|
-
|
|
19
|
-
function createWatchedQuery() {
|
|
20
|
-
if (!active) {
|
|
21
|
-
return null;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const watch = hookOptions.rowComparator
|
|
25
|
-
? powerSync.customQuery(query).differentialWatch({
|
|
26
|
-
rowComparator: hookOptions.rowComparator,
|
|
27
|
-
reportFetching: hookOptions.reportFetching,
|
|
28
|
-
throttleMs: hookOptions.throttleMs
|
|
29
|
-
})
|
|
30
|
-
: powerSync.customQuery(query).watch({
|
|
31
|
-
reportFetching: hookOptions.reportFetching,
|
|
32
|
-
throttleMs: hookOptions.throttleMs
|
|
33
|
-
});
|
|
34
|
-
return watch;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const [watchedQuery, setWatchedQuery] = React.useState(createWatchedQuery);
|
|
38
|
-
const disposePendingUpdateListener = React.useRef<() => void | null>(null);
|
|
39
|
-
|
|
40
|
-
React.useEffect(() => {
|
|
41
|
-
watchedQuery?.close();
|
|
42
|
-
const newQuery = createWatchedQuery();
|
|
43
|
-
setWatchedQuery(newQuery);
|
|
44
|
-
|
|
45
|
-
return () => {
|
|
46
|
-
disposePendingUpdateListener.current?.();
|
|
47
|
-
newQuery?.close();
|
|
48
|
-
};
|
|
49
|
-
}, [powerSync, active]);
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Indicates that the query will be re-fetched due to a change in the query.
|
|
53
|
-
* We execute this in-line (not using an effect) since effects are delayed till after the hook returns.
|
|
54
|
-
* The `queryChanged` value should only be true for a single render.
|
|
55
|
-
* The `updateSettings` method is asynchronous, thus it will update the state asynchronously.
|
|
56
|
-
* In the React hooks we'd like to report that we are fetching the data for an updated query
|
|
57
|
-
* as soon as the query has been updated. This prevents a result flow where e.g. the hook:
|
|
58
|
-
* - already returned a result: isLoading, isFetching are both false
|
|
59
|
-
* - the query is updated, but the state is still isFetching=false from the previous state
|
|
60
|
-
* We override the isFetching status while the `updateSettings` method is running (if we report `isFetching`),
|
|
61
|
-
* we override this value just until the `updateSettings` method itself will update the `isFetching` status.
|
|
62
|
-
* We achieve this by registering a `settingsWillUpdate` listener on the `WatchedQuery`. This will fire
|
|
63
|
-
* just before the `isFetching` status is updated.
|
|
64
|
-
*/
|
|
65
|
-
if (queryChanged) {
|
|
66
|
-
// Keep track of this pending operation
|
|
67
|
-
watchedQuery?.updateSettings({
|
|
68
|
-
query,
|
|
69
|
-
throttleMs: hookOptions.throttleMs,
|
|
70
|
-
reportFetching: hookOptions.reportFetching
|
|
71
|
-
});
|
|
72
|
-
// This could have been called multiple times, clear any old listeners.
|
|
73
|
-
disposePendingUpdateListener.current?.();
|
|
74
|
-
disposePendingUpdateListener.current = watchedQuery?.registerListener({
|
|
75
|
-
settingsWillUpdate: () => {
|
|
76
|
-
// We'll use the fact that we have a listener at all as an indication
|
|
77
|
-
disposePendingUpdateListener.current?.();
|
|
78
|
-
disposePendingUpdateListener.current = null;
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const shouldReportCurrentlyFetching = (hookOptions.reportFetching ?? true) && !!disposePendingUpdateListener.current;
|
|
84
|
-
const result = useNullableWatchedQuerySubscription(watchedQuery);
|
|
85
|
-
|
|
86
|
-
// Result is only undefined when there is no data available yet, defaults are defined accordingly
|
|
87
|
-
return {
|
|
88
|
-
data: result?.data ?? [],
|
|
89
|
-
isLoading: result?.isLoading ?? true,
|
|
90
|
-
error: result?.error,
|
|
91
|
-
isFetching: result?.isFetching || shouldReportCurrentlyFetching
|
|
92
|
-
};
|
|
93
|
-
};
|