@powersync/react 2.0.1 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +15 -0
- 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/SuspenseQueryCache.d.ts +46 -0
- package/lib/hooks/suspense/SuspenseQueryCache.js +231 -0
- package/lib/hooks/suspense/SuspenseQueryCache.js.map +1 -0
- package/lib/hooks/suspense/useSuspenseQuery.d.ts +3 -3
- package/lib/hooks/suspense/useSuspenseQuery.js +28 -7
- package/lib/hooks/suspense/useSuspenseQuery.js.map +1 -1
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +60 -24
- package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.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 +90 -0
- package/lib/hooks/watched/QueryRunner.js +366 -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 +39 -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/hooks/PowerSyncContext.ts +9 -3
- package/src/hooks/streams.ts +14 -20
- package/src/hooks/suspense/SuspenseQueryCache.ts +253 -0
- package/src/hooks/suspense/useSuspenseQuery.ts +40 -13
- package/src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts +65 -25
- package/src/hooks/useStatus.ts +21 -7
- package/src/hooks/watched/QueryRunner.ts +484 -0
- package/src/hooks/watched/useQuery.ts +15 -41
- package/src/hooks/watched/useWatchedQuerySubscription.ts +2 -2
- package/src/hooks/watched/watch-types.ts +42 -3
- package/src/hooks/watched/watch-utils.ts +8 -54
- package/src/index.ts +8 -2
- package/lib/QueryStore.d.ts +0 -10
- package/lib/QueryStore.js +0 -63
- package/lib/QueryStore.js.map +0 -1
- package/lib/hooks/suspense/suspense-utils.d.ts +0 -16
- package/lib/hooks/suspense/suspense-utils.js +0 -76
- package/lib/hooks/suspense/suspense-utils.js.map +0 -1
- package/lib/hooks/suspense/useSingleSuspenseQuery.d.ts +0 -10
- package/lib/hooks/suspense/useSingleSuspenseQuery.js +0 -73
- package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +0 -1
- package/lib/hooks/suspense/useWatchedSuspenseQuery.d.ts +0 -6
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js +0 -26
- package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +0 -1
- 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/QueryStore.ts +0 -81
- package/src/hooks/suspense/suspense-utils.ts +0 -86
- package/src/hooks/suspense/useSingleSuspenseQuery.ts +0 -81
- package/src/hooks/suspense/useWatchedSuspenseQuery.ts +0 -36
- package/src/hooks/watched/useSingleQuery.ts +0 -67
- package/src/hooks/watched/useWatchedQuery.ts +0 -93
package/lib/hooks/useStatus.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { usePowerSync, usePowerSyncOrNull } from './PowerSyncContext.js';
|
|
3
3
|
/**
|
|
4
4
|
* Custom hook that provides access to the current status of PowerSync.
|
|
5
5
|
* @returns The PowerSync Database status.
|
|
@@ -15,16 +15,21 @@ import { PowerSyncContext } from './PowerSyncContext.js';
|
|
|
15
15
|
* };
|
|
16
16
|
*/
|
|
17
17
|
export function useStatus() {
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
return useStatusOrNull(false);
|
|
19
|
+
}
|
|
20
|
+
export function useStatusOrNull(allowNull = true) {
|
|
21
|
+
// Conditional hook is okay, this is an internal hook and allowNull is a constant on each call site.
|
|
22
|
+
const powerSync = allowNull ? usePowerSyncOrNull() : usePowerSync();
|
|
23
|
+
const [syncStatus, setSyncStatus] = useState(powerSync?.currentStatus);
|
|
20
24
|
useEffect(() => {
|
|
21
|
-
|
|
25
|
+
setSyncStatus(powerSync?.currentStatus);
|
|
26
|
+
const listener = powerSync?.registerListener({
|
|
22
27
|
statusChanged: (status) => {
|
|
23
28
|
setSyncStatus(status);
|
|
24
29
|
}
|
|
25
30
|
});
|
|
26
31
|
return () => listener?.();
|
|
27
32
|
}, [powerSync]);
|
|
28
|
-
return syncStatus;
|
|
33
|
+
return syncStatus ?? null;
|
|
29
34
|
}
|
|
30
35
|
//# 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,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC5C,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAGzE;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,SAAS;IACvB,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC;AAChC,CAAC;AAQD,MAAM,UAAU,eAAe,CAAC,YAAqB,IAAI;IACvD,oGAAoG;IACpG,MAAM,SAAS,GAAG,SAAS,CAAC,CAAC,CAAC,kBAAkB,EAAE,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC;IACpE,MAAM,CAAC,UAAU,EAAE,aAAa,CAAC,GAAG,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;IAEvE,SAAS,CAAC,GAAG,EAAE;QACb,aAAa,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;QAExC,MAAM,QAAQ,GAAG,SAAS,EAAE,gBAAgB,CAAC;YAC3C,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,IAAI,IAAI,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { BaseListener, BaseObserver, CommonPowerSyncDatabase, DifferentialWatchedQueryComparator } from '@powersync/common';
|
|
2
|
+
import { AdditionalOptions, DifferentialHookOptions, QueryResult, ReadonlyQueryResult } from './watch-types.js';
|
|
3
|
+
import { WatchCompatibleQueryWithParams } from './watch-utils.js';
|
|
4
|
+
export type Result<RowType> = QueryResult<RowType> | ReadonlyQueryResult<RowType>;
|
|
5
|
+
/**
|
|
6
|
+
* The configured state a {@link QueryRunner} is in.
|
|
7
|
+
*
|
|
8
|
+
* Creating these instances has no side effects (in particular, doesn't start database queries) and can be done in
|
|
9
|
+
* renders.
|
|
10
|
+
*/
|
|
11
|
+
export type RunnerConfig<RowType> = {
|
|
12
|
+
kind: 'missing-db';
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'waiting-for-streams';
|
|
15
|
+
} | {
|
|
16
|
+
kind: 'compile-error';
|
|
17
|
+
error: Error;
|
|
18
|
+
} | QueryConfig<RowType>;
|
|
19
|
+
export interface QueryConfig<RowType> {
|
|
20
|
+
kind: 'query';
|
|
21
|
+
db: CommonPowerSyncDatabase;
|
|
22
|
+
query: WatchCompatibleQueryWithParams<RowType[]>;
|
|
23
|
+
sql: string;
|
|
24
|
+
parameters: ReadonlyArray<any>;
|
|
25
|
+
stringifiedParams: string;
|
|
26
|
+
runOnce: boolean;
|
|
27
|
+
rowComparator: DifferentialWatchedQueryComparator<RowType> | undefined;
|
|
28
|
+
throttleMs: number | undefined;
|
|
29
|
+
reportFetching: boolean;
|
|
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
|
+
/**
|
|
41
|
+
* Resolves the inputs of `useQuery` into a comparable {@link RunnerConfig}.
|
|
42
|
+
*
|
|
43
|
+
* This is pure (apart from compiling the query) and can run while rendering.
|
|
44
|
+
*/
|
|
45
|
+
export declare function resolveConfig<RowType>(db: CommonPowerSyncDatabase | null, query: WatchCompatibleQueryWithParams<RowType[]>, options: AdditionalOptions & DifferentialHookOptions<RowType>, streamsHaveSynced: boolean): RunnerConfig<RowType>;
|
|
46
|
+
export interface QueryRunnerListener extends BaseListener {
|
|
47
|
+
onChange(): void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* An imperative store backing the `useQuery` hook. There is always one instance of this per `useQuery` hook.
|
|
51
|
+
*
|
|
52
|
+
* On every render, {@link resultFor} checks for config changes and returns the current result. Configuration changes
|
|
53
|
+
* are not applied during renders. Instead, {@link configure} is used to apply configuration changes in a commit. This
|
|
54
|
+
* ensures multiple {@link resultFor} calls in uncommited renders are cheap and don't trigger queries.
|
|
55
|
+
*
|
|
56
|
+
* Work is only active while at least one listener is registered.
|
|
57
|
+
*/
|
|
58
|
+
export declare class QueryRunner<RowType> extends BaseObserver<QueryRunnerListener> {
|
|
59
|
+
private config;
|
|
60
|
+
private idleResult;
|
|
61
|
+
/**
|
|
62
|
+
* The active work, only non-null while we have listeners.
|
|
63
|
+
*/
|
|
64
|
+
private work;
|
|
65
|
+
/**
|
|
66
|
+
* A cached result for a config that hasn't been applied yet, so that repeated renders return a stable object.
|
|
67
|
+
*/
|
|
68
|
+
private uncommitedConfigChange;
|
|
69
|
+
readonly subscribe: (onChange: () => void) => (() => void);
|
|
70
|
+
readonly getSnapshot: () => Result<RowType>;
|
|
71
|
+
/**
|
|
72
|
+
* Returns the result to render for the given config, without changing the runner.
|
|
73
|
+
*
|
|
74
|
+
* If the config has already been applied, this is the current snapshot. Otherwise, it's the result we expect to
|
|
75
|
+
* report right after {@link configure} applies it, so that changed inputs are reflected in the same render (even if
|
|
76
|
+
* that render is ultimately dropped).
|
|
77
|
+
*/
|
|
78
|
+
resultFor(config: RunnerConfig<RowType>, snapshot: Result<RowType>): Result<RowType>;
|
|
79
|
+
/**
|
|
80
|
+
* Applies the inputs of a committed render to this runner. This does nothing if they haven't changed.
|
|
81
|
+
*
|
|
82
|
+
* 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
|
+
*/
|
|
87
|
+
configure(next: RunnerConfig<RowType>, initial?: Result<RowType>): void;
|
|
88
|
+
private startWork;
|
|
89
|
+
private notifyListeners;
|
|
90
|
+
}
|
|
@@ -0,0 +1,366 @@
|
|
|
1
|
+
import { BaseObserver } from '@powersync/common';
|
|
2
|
+
function configEquals(a, b) {
|
|
3
|
+
if (a === b)
|
|
4
|
+
return true;
|
|
5
|
+
switch (a.kind) {
|
|
6
|
+
case 'missing-db':
|
|
7
|
+
case 'waiting-for-streams':
|
|
8
|
+
return a.kind == b.kind;
|
|
9
|
+
case 'compile-error':
|
|
10
|
+
// compile() runs on every render, so a failing query throws a new error each time.
|
|
11
|
+
return b.kind == 'compile-error' && a.error.message == b.error.message;
|
|
12
|
+
case 'query':
|
|
13
|
+
return (b.kind == 'query' &&
|
|
14
|
+
a.db === b.db &&
|
|
15
|
+
a.sql == b.sql &&
|
|
16
|
+
a.stringifiedParams == b.stringifiedParams &&
|
|
17
|
+
a.runOnce == b.runOnce &&
|
|
18
|
+
// We don't compare rowComparator by reference, since it's typically an inline object changed on every rerender.
|
|
19
|
+
// Only toggling between differential and regular queries is considered a re-run worthy chamge.
|
|
20
|
+
!!a.rowComparator == !!b.rowComparator &&
|
|
21
|
+
a.throttleMs == b.throttleMs &&
|
|
22
|
+
a.reportFetching == b.reportFetching);
|
|
23
|
+
}
|
|
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
|
+
const _loadingState = { isLoading: true, isFetching: false, data: [], error: undefined };
|
|
40
|
+
function idleResultFor(config) {
|
|
41
|
+
switch (config.kind) {
|
|
42
|
+
case 'missing-db':
|
|
43
|
+
return { isLoading: false, isFetching: false, data: [], error: new Error('PowerSync not configured.') };
|
|
44
|
+
case 'waiting-for-streams':
|
|
45
|
+
return _loadingState;
|
|
46
|
+
case 'compile-error':
|
|
47
|
+
return { isLoading: false, isFetching: false, data: [], error: config.error };
|
|
48
|
+
case 'query':
|
|
49
|
+
// Single queries always report fetching while they run.
|
|
50
|
+
const isFetching = config.runOnce || config.reportFetching;
|
|
51
|
+
return {
|
|
52
|
+
isLoading: true,
|
|
53
|
+
isFetching,
|
|
54
|
+
data: [],
|
|
55
|
+
error: undefined,
|
|
56
|
+
fetchReason: isFetching ? 'initial' : undefined
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Resolves the inputs of `useQuery` into a comparable {@link RunnerConfig}.
|
|
62
|
+
*
|
|
63
|
+
* This is pure (apart from compiling the query) and can run while rendering.
|
|
64
|
+
*/
|
|
65
|
+
export function resolveConfig(db, query, options, streamsHaveSynced) {
|
|
66
|
+
if (db == null) {
|
|
67
|
+
return { kind: 'missing-db' };
|
|
68
|
+
}
|
|
69
|
+
if (!streamsHaveSynced) {
|
|
70
|
+
return { kind: 'waiting-for-streams' };
|
|
71
|
+
}
|
|
72
|
+
let sql;
|
|
73
|
+
let parameters;
|
|
74
|
+
try {
|
|
75
|
+
const compiled = query.compile();
|
|
76
|
+
sql = compiled.sql;
|
|
77
|
+
parameters = compiled.parameters;
|
|
78
|
+
}
|
|
79
|
+
catch (error) {
|
|
80
|
+
return { kind: 'compile-error', error: error };
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
kind: 'query',
|
|
84
|
+
db,
|
|
85
|
+
query,
|
|
86
|
+
sql,
|
|
87
|
+
parameters,
|
|
88
|
+
stringifiedParams: query.stringifiedParameters ?? JSON.stringify(parameters),
|
|
89
|
+
runOnce: options.runQueryOnce == true,
|
|
90
|
+
rowComparator: options.rowComparator,
|
|
91
|
+
throttleMs: options.throttleMs,
|
|
92
|
+
reportFetching: options.reportFetching ?? true
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* An imperative store backing the `useQuery` hook. There is always one instance of this per `useQuery` hook.
|
|
97
|
+
*
|
|
98
|
+
* On every render, {@link resultFor} checks for config changes and returns the current result. Configuration changes
|
|
99
|
+
* are not applied during renders. Instead, {@link configure} is used to apply configuration changes in a commit. This
|
|
100
|
+
* ensures multiple {@link resultFor} calls in uncommited renders are cheap and don't trigger queries.
|
|
101
|
+
*
|
|
102
|
+
* Work is only active while at least one listener is registered.
|
|
103
|
+
*/
|
|
104
|
+
export class QueryRunner extends BaseObserver {
|
|
105
|
+
config = null;
|
|
106
|
+
idleResult = _loadingState;
|
|
107
|
+
/**
|
|
108
|
+
* The active work, only non-null while we have listeners.
|
|
109
|
+
*/
|
|
110
|
+
work = null;
|
|
111
|
+
/**
|
|
112
|
+
* A cached result for a config that hasn't been applied yet, so that repeated renders return a stable object.
|
|
113
|
+
*/
|
|
114
|
+
uncommitedConfigChange = null;
|
|
115
|
+
subscribe = (onChange) => {
|
|
116
|
+
const listener = { onChange };
|
|
117
|
+
const unregister = this.registerListener(listener);
|
|
118
|
+
if (this.listeners.size == 1) {
|
|
119
|
+
this.startWork();
|
|
120
|
+
this.notifyListeners();
|
|
121
|
+
}
|
|
122
|
+
return () => {
|
|
123
|
+
unregister();
|
|
124
|
+
if (this.listeners.size == 0) {
|
|
125
|
+
this.work?.dispose();
|
|
126
|
+
this.work = null;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
};
|
|
130
|
+
getSnapshot = () => {
|
|
131
|
+
return this.work?.result ?? this.idleResult;
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* Returns the result to render for the given config, without changing the runner.
|
|
135
|
+
*
|
|
136
|
+
* If the config has already been applied, this is the current snapshot. Otherwise, it's the result we expect to
|
|
137
|
+
* report right after {@link configure} applies it, so that changed inputs are reflected in the same render (even if
|
|
138
|
+
* that render is ultimately dropped).
|
|
139
|
+
*/
|
|
140
|
+
resultFor(config, snapshot) {
|
|
141
|
+
if (this.config != null && configEquals(this.config, config)) {
|
|
142
|
+
return snapshot;
|
|
143
|
+
}
|
|
144
|
+
const pending = this.uncommitedConfigChange;
|
|
145
|
+
if (pending != null && pending.snapshot === snapshot && configEquals(pending.config, config)) {
|
|
146
|
+
return pending.result;
|
|
147
|
+
}
|
|
148
|
+
// A re-render has changed the query, and we'll start running that query in a useEffect hook calling configure.
|
|
149
|
+
// For now, optimistically return the loading state that query would start.
|
|
150
|
+
const fromWork = this.work?.pendingResultFor(config);
|
|
151
|
+
const result = fromWork ?? idleResultFor(config);
|
|
152
|
+
this.uncommitedConfigChange = { config, snapshot, result, isOptimisticStateChange: fromWork == null };
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Applies the inputs of a committed render to this runner. This does nothing if they haven't changed.
|
|
157
|
+
*
|
|
158
|
+
* 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
|
+
*/
|
|
163
|
+
configure(next, initial) {
|
|
164
|
+
if (this.config != null && configEquals(this.config, next)) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
const pending = this.uncommitedConfigChange;
|
|
168
|
+
this.config = next;
|
|
169
|
+
this.idleResult = initial
|
|
170
|
+
? initial
|
|
171
|
+
: pending != null && pending.isOptimisticStateChange && configEquals(pending.config, next)
|
|
172
|
+
? // We already returned this as an optimistic state in a previous resultFor call. Use the same result to not
|
|
173
|
+
// cause another render.
|
|
174
|
+
pending.result
|
|
175
|
+
: idleResultFor(next);
|
|
176
|
+
this.uncommitedConfigChange = null;
|
|
177
|
+
if (this.work == null || !this.work.tryUpdate(next)) {
|
|
178
|
+
this.work?.dispose();
|
|
179
|
+
this.work = null;
|
|
180
|
+
if (this.listeners.size > 0) {
|
|
181
|
+
this.startWork();
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
this.notifyListeners();
|
|
185
|
+
}
|
|
186
|
+
startWork() {
|
|
187
|
+
const config = this.config;
|
|
188
|
+
if (config == null || config.kind != 'query') {
|
|
189
|
+
// In waiting or error state, don't run anything.
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const emit = () => this.notifyListeners();
|
|
193
|
+
this.work = new (config.runOnce ? SingleQueryWork : WatchedQueryWork)(config, this.idleResult, emit);
|
|
194
|
+
}
|
|
195
|
+
notifyListeners() {
|
|
196
|
+
this.iterateListeners((l) => l.onChange?.());
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
class SingleQueryWork {
|
|
200
|
+
config;
|
|
201
|
+
emit;
|
|
202
|
+
result;
|
|
203
|
+
abortController = null;
|
|
204
|
+
disposed = false;
|
|
205
|
+
constructor(config, initial, emit) {
|
|
206
|
+
this.config = config;
|
|
207
|
+
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
|
+
this.result = {
|
|
215
|
+
...initial,
|
|
216
|
+
isFetching: true,
|
|
217
|
+
fetchReason,
|
|
218
|
+
refresh: this.refresh
|
|
219
|
+
};
|
|
220
|
+
this.run(fetchReason);
|
|
221
|
+
}
|
|
222
|
+
refresh = async (signal) => {
|
|
223
|
+
if (this.disposed)
|
|
224
|
+
return;
|
|
225
|
+
await this.run('refresh', signal);
|
|
226
|
+
};
|
|
227
|
+
setResult(update) {
|
|
228
|
+
this.result = { ...this.result, ...update };
|
|
229
|
+
this.emit();
|
|
230
|
+
}
|
|
231
|
+
async run(fetchReason, outerSignal) {
|
|
232
|
+
this.abortController?.abort();
|
|
233
|
+
const controller = (this.abortController = new AbortController());
|
|
234
|
+
const signal = controller.signal;
|
|
235
|
+
const isAborted = () => signal.aborted || outerSignal?.aborted == true;
|
|
236
|
+
if (!this.result.isLoading || this.result.error) {
|
|
237
|
+
// After an error, data is empty, so we're loading again rather than refreshing existing rows.
|
|
238
|
+
const isLoading = fetchReason === 'initial' || this.result.error != null;
|
|
239
|
+
this.setResult({ isLoading, isFetching: true, error: undefined, fetchReason });
|
|
240
|
+
}
|
|
241
|
+
const { query, sql, parameters, db } = this.config;
|
|
242
|
+
try {
|
|
243
|
+
const data = await query.execute({ sql, parameters: [...parameters], db });
|
|
244
|
+
if (isAborted())
|
|
245
|
+
return;
|
|
246
|
+
this.setResult({ isLoading: false, isFetching: false, data, error: undefined, fetchReason: undefined });
|
|
247
|
+
}
|
|
248
|
+
catch (error) {
|
|
249
|
+
if (isAborted())
|
|
250
|
+
return;
|
|
251
|
+
this.setResult({ isLoading: false, isFetching: false, data: [], error: error, fetchReason: undefined });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
pendingResultFor() {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
tryUpdate() {
|
|
258
|
+
return false;
|
|
259
|
+
}
|
|
260
|
+
dispose() {
|
|
261
|
+
this.abortController?.abort();
|
|
262
|
+
this.abortController = null;
|
|
263
|
+
this.disposed = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
class WatchedQueryWork {
|
|
267
|
+
config;
|
|
268
|
+
emit;
|
|
269
|
+
result;
|
|
270
|
+
watch;
|
|
271
|
+
disposeListener;
|
|
272
|
+
/**
|
|
273
|
+
* Set after calling `updateSettings` until the watched query starts applying the new settings. We report
|
|
274
|
+
* `isFetching` in the meantime so that the hook doesn't briefly appear settled with stale data.
|
|
275
|
+
*/
|
|
276
|
+
pendingUpdate = false;
|
|
277
|
+
/**
|
|
278
|
+
* Set by {@link tryUpdate} until the updated query has produced its first result. Until then, `data` still holds the
|
|
279
|
+
* previous query's rows, which is reported as `fetchReason: 'settings-changed'`.
|
|
280
|
+
*/
|
|
281
|
+
awaitingUpdatedResult = false;
|
|
282
|
+
reportFetching;
|
|
283
|
+
constructor(config, initial, emit) {
|
|
284
|
+
this.config = config;
|
|
285
|
+
this.emit = emit;
|
|
286
|
+
this.result = initial;
|
|
287
|
+
this.reportFetching = config.reportFetching;
|
|
288
|
+
const { db, query, rowComparator, throttleMs, reportFetching } = config;
|
|
289
|
+
const customQuery = db.customQuery(query);
|
|
290
|
+
this.watch = rowComparator
|
|
291
|
+
? customQuery.differentialWatch({ rowComparator, reportFetching, throttleMs })
|
|
292
|
+
: customQuery.watch({ reportFetching, throttleMs });
|
|
293
|
+
this.disposeListener = this.watch.registerListener({
|
|
294
|
+
onStateChange: (state) => {
|
|
295
|
+
if (this.awaitingUpdatedResult && !this.pendingUpdate && !state.isFetching) {
|
|
296
|
+
// The first completed fetch after the new settings were applied is the updated query's result.
|
|
297
|
+
this.awaitingUpdatedResult = false;
|
|
298
|
+
}
|
|
299
|
+
this.result = this.mapState(state);
|
|
300
|
+
this.emit();
|
|
301
|
+
},
|
|
302
|
+
settingsWillUpdate: () => {
|
|
303
|
+
// The watched query sets isFetching itself after this event, so we stop overriding it. We don't emit
|
|
304
|
+
// here because the state may not reflect that yet, the next onStateChange delivers the updated state.
|
|
305
|
+
this.pendingUpdate = false;
|
|
306
|
+
}
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
mapState(state) {
|
|
310
|
+
// The state object may be mutated by the watched query, so we copy it into a fresh snapshot.
|
|
311
|
+
const isFetching = state.isFetching || (this.pendingUpdate && this.reportFetching);
|
|
312
|
+
return {
|
|
313
|
+
data: state.data,
|
|
314
|
+
isLoading: state.isLoading,
|
|
315
|
+
isFetching,
|
|
316
|
+
error: state.error ?? undefined,
|
|
317
|
+
fetchReason: this.fetchReasonFor(state.isLoading, isFetching)
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
fetchReasonFor(isLoading, isFetching) {
|
|
321
|
+
if (!isFetching)
|
|
322
|
+
return undefined;
|
|
323
|
+
if (isLoading)
|
|
324
|
+
return 'initial';
|
|
325
|
+
return this.awaitingUpdatedResult ? 'settings-changed' : 'tables-changed';
|
|
326
|
+
}
|
|
327
|
+
canUpdate(config) {
|
|
328
|
+
return (config.kind == 'query' &&
|
|
329
|
+
!config.runOnce &&
|
|
330
|
+
config.db === this.config.db &&
|
|
331
|
+
!!config.rowComparator == !!this.config.rowComparator);
|
|
332
|
+
}
|
|
333
|
+
pendingResultFor(config) {
|
|
334
|
+
if (!this.canUpdate(config)) {
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
// Keep the previous data while the updated query runs.
|
|
338
|
+
const isFetching = this.result.isFetching || config.reportFetching;
|
|
339
|
+
return {
|
|
340
|
+
...this.result,
|
|
341
|
+
isFetching,
|
|
342
|
+
fetchReason: !isFetching ? undefined : this.result.isLoading ? 'initial' : 'settings-changed'
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
tryUpdate(config) {
|
|
346
|
+
if (!this.canUpdate(config)) {
|
|
347
|
+
return false;
|
|
348
|
+
}
|
|
349
|
+
this.config = config;
|
|
350
|
+
this.reportFetching = config.reportFetching;
|
|
351
|
+
this.pendingUpdate = true;
|
|
352
|
+
this.awaitingUpdatedResult = true;
|
|
353
|
+
this.watch.updateSettings({
|
|
354
|
+
query: config.query,
|
|
355
|
+
throttleMs: config.throttleMs,
|
|
356
|
+
reportFetching: config.reportFetching
|
|
357
|
+
});
|
|
358
|
+
this.result = this.mapState(this.watch.state);
|
|
359
|
+
return true;
|
|
360
|
+
}
|
|
361
|
+
dispose() {
|
|
362
|
+
this.disposeListener();
|
|
363
|
+
this.watch.close();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
//# sourceMappingURL=QueryRunner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1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CAAC,KAAK,CAAC,cAAc,CAAC;YACxB,KAAK,EAAE,MAAM,CAAC,KAAK;YACnB,UAAU,EAAE,MAAM,CAAC,UAAU;YAC7B,cAAc,EAAE,MAAM,CAAC,cAAc;SACtC,CAAC,CAAC;QACH,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC9C,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,IAAI,CAAC,eAAe,EAAE,CAAC;QACvB,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -1,44 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { useWatchedQuery } from './useWatchedQuery.js';
|
|
4
|
-
import { constructCompatibleQuery } from './watch-utils.js';
|
|
1
|
+
import { useContext, useEffect, useRef, useSyncExternalStore } from 'react';
|
|
2
|
+
import { PowerSyncContext } from '../PowerSyncContext.js';
|
|
5
3
|
import { useAllSyncStreamsHaveSynced } from '../streams.js';
|
|
4
|
+
import { QueryRunner, resolveConfig } from './QueryRunner.js';
|
|
5
|
+
import { constructCompatibleQuery } from './watch-utils.js';
|
|
6
6
|
export function useQuery(query, parameters = [], options = {}) {
|
|
7
|
-
const powerSync =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const runOnce = options?.runQueryOnce == true;
|
|
18
|
-
const single = useSingleQuery({
|
|
19
|
-
query: parsedQuery,
|
|
20
|
-
powerSync,
|
|
21
|
-
queryChanged,
|
|
22
|
-
active: runOnce && streamsHaveSynced
|
|
23
|
-
});
|
|
24
|
-
const watched = useWatchedQuery({
|
|
25
|
-
query: parsedQuery,
|
|
26
|
-
powerSync,
|
|
27
|
-
queryChanged,
|
|
28
|
-
options: {
|
|
29
|
-
reportFetching: options.reportFetching,
|
|
30
|
-
throttleMs: options.throttleMs,
|
|
31
|
-
// Maintains backwards compatibility with previous versions
|
|
32
|
-
// Differentiation is opt-in by default
|
|
33
|
-
// We emit new data for each table change by default.
|
|
34
|
-
rowComparator: options.rowComparator
|
|
35
|
-
},
|
|
36
|
-
active: !runOnce && streamsHaveSynced
|
|
37
|
-
});
|
|
38
|
-
if (!streamsHaveSynced) {
|
|
39
|
-
return { ..._loadingState };
|
|
40
|
-
}
|
|
41
|
-
return (runOnce ? single : watched) ?? _loadingState;
|
|
7
|
+
const powerSync = useContext(PowerSyncContext);
|
|
8
|
+
const parsedQuery = constructCompatibleQuery(query, parameters);
|
|
9
|
+
const streamsHaveSynced = useAllSyncStreamsHaveSynced(null, options.streams);
|
|
10
|
+
const runnerRef = useRef(null);
|
|
11
|
+
const runner = (runnerRef.current ??= new QueryRunner());
|
|
12
|
+
const config = resolveConfig(powerSync, parsedQuery, options, streamsHaveSynced);
|
|
13
|
+
const snapshot = useSyncExternalStore(runner.subscribe, runner.getSnapshot, runner.getSnapshot);
|
|
14
|
+
// Apply the inputs once this render has been committed, so that discarded renders don't start running queries.
|
|
15
|
+
useEffect(() => runner.configure(config));
|
|
16
|
+
return runner.resultFor(config, snapshot);
|
|
42
17
|
}
|
|
43
|
-
const _loadingState = { isLoading: true, isFetching: false, data: [], error: undefined };
|
|
44
18
|
//# sourceMappingURL=useQuery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useQuery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useQuery.js","sourceRoot":"","sources":["../../../src/hooks/watched/useQuery.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC5E,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,2BAA2B,EAAE,MAAM,eAAe,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAE9D,OAAO,EAAE,wBAAwB,EAAE,MAAM,kBAAkB,CAAC;AAgD5D,MAAM,UAAU,QAAQ,CACtB,KAAwC,EACxC,aAAoB,EAAE,EACtB,UAAgE,EAAE;IAElE,MAAM,SAAS,GAAG,UAAU,CAAC,gBAAgB,CAAC,CAAC;IAC/C,MAAM,WAAW,GAAG,wBAAwB,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;IAChE,MAAM,iBAAiB,GAAG,2BAA2B,CAAC,IAAI,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAE7E,MAAM,SAAS,GAAG,MAAM,CAA8B,IAAI,CAAC,CAAC;IAC5D,MAAM,MAAM,GAAG,CAAC,SAAS,CAAC,OAAO,KAAK,IAAI,WAAW,EAAW,CAAC,CAAC;IAClE,MAAM,MAAM,GAAG,aAAa,CAAC,SAAS,EAAE,WAAW,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC;IAEjF,MAAM,QAAQ,GAAG,oBAAoB,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAEhG,+GAA+G;IAC/G,SAAS,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;IAC1C,OAAO,MAAM,CAAC,SAAS,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;AAC5C,CAAC"}
|
|
@@ -17,4 +17,4 @@ export declare const useWatchedQuerySubscription: <ResultType = unknown, Query e
|
|
|
17
17
|
/**
|
|
18
18
|
* @internal
|
|
19
19
|
*/
|
|
20
|
-
export declare const useNullableWatchedQuerySubscription: <ResultType = unknown, Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>>(query:
|
|
20
|
+
export declare const useNullableWatchedQuerySubscription: <ResultType = unknown, Query extends WatchedQuery<ResultType> = WatchedQuery<ResultType>>(query: WatchedQuery<ResultType> | null) => Query["state"] | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWatchedQuerySubscription.js","sourceRoot":"","sources":["../../../src/hooks/watched/useWatchedQuerySubscription.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAIzC,KAAY,EACI,EAAE;IAClB,OAAO,mCAAmC,CAAC,KAAK,
|
|
1
|
+
{"version":3,"file":"useWatchedQuerySubscription.js","sourceRoot":"","sources":["../../../src/hooks/watched/useWatchedQuerySubscription.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B;;;;;;;;;;;;;GAaG;AACH,MAAM,CAAC,MAAM,2BAA2B,GAAG,CAIzC,KAAY,EACI,EAAE;IAClB,OAAO,mCAAmC,CAAC,KAAK,CAAE,CAAC;AACrD,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,mCAAmC,GAAG,CAIjD,KAAsC,EACV,EAAE;IAC9B,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAE9D,mEAAmE;IACnE,KAAK,CAAC,SAAS,CAAC,GAAG,EAAE;QACnB,IAAI,KAAK,EAAE,CAAC;YACV,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YAE5B,OAAO,KAAK,CAAC,gBAAgB,CAAC;gBAC5B,aAAa,EAAE,CAAC,KAAK,EAAE,EAAE;oBACvB,cAAc,CAAC,EAAE,GAAG,KAAK,EAAE,CAAC,CAAC;gBAC/B,CAAC;aACF,CAAC,CAAC;QACL,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC"}
|