@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
|
@@ -0,0 +1,340 @@
|
|
|
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
|
+
const _loadingState = { isLoading: true, isFetching: false, data: [], error: undefined };
|
|
26
|
+
function idleResultFor(config) {
|
|
27
|
+
switch (config.kind) {
|
|
28
|
+
case 'missing-db':
|
|
29
|
+
return { isLoading: false, isFetching: false, data: [], error: new Error('PowerSync not configured.') };
|
|
30
|
+
case 'waiting-for-streams':
|
|
31
|
+
return _loadingState;
|
|
32
|
+
case 'compile-error':
|
|
33
|
+
return { isLoading: false, isFetching: false, data: [], error: config.error };
|
|
34
|
+
case 'query':
|
|
35
|
+
// Single queries always report fetching while they run.
|
|
36
|
+
const isFetching = config.runOnce || config.reportFetching;
|
|
37
|
+
return {
|
|
38
|
+
isLoading: true,
|
|
39
|
+
isFetching,
|
|
40
|
+
data: [],
|
|
41
|
+
error: undefined,
|
|
42
|
+
fetchReason: isFetching ? 'initial' : undefined
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolves the inputs of `useQuery` into a comparable {@link RunnerConfig}.
|
|
48
|
+
*
|
|
49
|
+
* This is pure (apart from compiling the query) and can run while rendering.
|
|
50
|
+
*/
|
|
51
|
+
export function resolveConfig(db, query, options, streamsHaveSynced) {
|
|
52
|
+
if (db == null) {
|
|
53
|
+
return { kind: 'missing-db' };
|
|
54
|
+
}
|
|
55
|
+
if (!streamsHaveSynced) {
|
|
56
|
+
return { kind: 'waiting-for-streams' };
|
|
57
|
+
}
|
|
58
|
+
let sql;
|
|
59
|
+
let parameters;
|
|
60
|
+
try {
|
|
61
|
+
const compiled = query.compile();
|
|
62
|
+
sql = compiled.sql;
|
|
63
|
+
parameters = compiled.parameters;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
return { kind: 'compile-error', error: error };
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
kind: 'query',
|
|
70
|
+
db,
|
|
71
|
+
query,
|
|
72
|
+
sql,
|
|
73
|
+
parameters,
|
|
74
|
+
stringifiedParams: query.stringifiedParameters ?? JSON.stringify(parameters),
|
|
75
|
+
runOnce: options.runQueryOnce == true,
|
|
76
|
+
rowComparator: options.rowComparator,
|
|
77
|
+
throttleMs: options.throttleMs,
|
|
78
|
+
reportFetching: options.reportFetching ?? true
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* An imperative store backing the `useQuery` hook. There is always one instance of this per `useQuery` hook.
|
|
83
|
+
*
|
|
84
|
+
* On every render, {@link resultFor} checks for config changes and returns the current result. Configuration changes
|
|
85
|
+
* are not applied during renders. Instead, {@link configure} is used to apply configuration changes in a commit. This
|
|
86
|
+
* ensures multiple {@link resultFor} calls in uncommited renders are cheap and don't trigger queries.
|
|
87
|
+
*
|
|
88
|
+
* Work is only active while at least one listener is registered.
|
|
89
|
+
*/
|
|
90
|
+
export class QueryRunner extends BaseObserver {
|
|
91
|
+
config = null;
|
|
92
|
+
idleResult = _loadingState;
|
|
93
|
+
/**
|
|
94
|
+
* The active work, only non-null while we have listeners.
|
|
95
|
+
*/
|
|
96
|
+
work = null;
|
|
97
|
+
/**
|
|
98
|
+
* A cached result for a config that hasn't been applied yet, so that repeated renders return a stable object.
|
|
99
|
+
*/
|
|
100
|
+
uncommitedConfigChange = null;
|
|
101
|
+
subscribe = (onChange) => {
|
|
102
|
+
const listener = { onChange };
|
|
103
|
+
const unregister = this.registerListener(listener);
|
|
104
|
+
if (this.listeners.size == 1) {
|
|
105
|
+
this.startWork();
|
|
106
|
+
this.notifyListeners();
|
|
107
|
+
}
|
|
108
|
+
return () => {
|
|
109
|
+
unregister();
|
|
110
|
+
if (this.listeners.size == 0) {
|
|
111
|
+
this.work?.dispose();
|
|
112
|
+
this.work = null;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
};
|
|
116
|
+
getSnapshot = () => {
|
|
117
|
+
return this.work?.result ?? this.idleResult;
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Returns the result to render for the given config, without changing the runner.
|
|
121
|
+
*
|
|
122
|
+
* If the config has already been applied, this is the current snapshot. Otherwise, it's the result we expect to
|
|
123
|
+
* report right after {@link configure} applies it, so that changed inputs are reflected in the same render (even if
|
|
124
|
+
* that render is ultimately dropped).
|
|
125
|
+
*/
|
|
126
|
+
resultFor(config, snapshot) {
|
|
127
|
+
if (this.config != null && configEquals(this.config, config)) {
|
|
128
|
+
return snapshot;
|
|
129
|
+
}
|
|
130
|
+
const pending = this.uncommitedConfigChange;
|
|
131
|
+
if (pending != null && pending.snapshot === snapshot && configEquals(pending.config, config)) {
|
|
132
|
+
return pending.result;
|
|
133
|
+
}
|
|
134
|
+
// A re-render has changed the query, and we'll start running that query in a useEffect hook calling configure.
|
|
135
|
+
// For now, optimistically return the loading state that query would start.
|
|
136
|
+
const fromWork = this.work?.pendingResultFor(config);
|
|
137
|
+
const result = fromWork ?? idleResultFor(config);
|
|
138
|
+
this.uncommitedConfigChange = { config, snapshot, result, isOptimisticStateChange: fromWork == null };
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Applies the inputs of a committed render to this runner. This does nothing if they haven't changed.
|
|
143
|
+
*
|
|
144
|
+
* This modifies state and should not run during a render.
|
|
145
|
+
*/
|
|
146
|
+
configure(next) {
|
|
147
|
+
if (this.config != null && configEquals(this.config, next)) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const pending = this.uncommitedConfigChange;
|
|
151
|
+
this.config = next;
|
|
152
|
+
this.idleResult =
|
|
153
|
+
pending != null && pending.isOptimisticStateChange && configEquals(pending.config, next)
|
|
154
|
+
? // We already returned this as an optimistic state in a previous resultFor call. Use the same result to not
|
|
155
|
+
// cause another render.
|
|
156
|
+
pending.result
|
|
157
|
+
: idleResultFor(next);
|
|
158
|
+
this.uncommitedConfigChange = null;
|
|
159
|
+
if (this.work == null || !this.work.tryUpdate(next)) {
|
|
160
|
+
this.work?.dispose();
|
|
161
|
+
this.work = null;
|
|
162
|
+
if (this.listeners.size > 0) {
|
|
163
|
+
this.startWork();
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
this.notifyListeners();
|
|
167
|
+
}
|
|
168
|
+
startWork() {
|
|
169
|
+
const config = this.config;
|
|
170
|
+
if (config == null || config.kind != 'query') {
|
|
171
|
+
// In waiting or error state, don't run anything.
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
const emit = () => this.notifyListeners();
|
|
175
|
+
this.work = new (config.runOnce ? SingleQueryWork : WatchedQueryWork)(config, this.idleResult, emit);
|
|
176
|
+
}
|
|
177
|
+
notifyListeners() {
|
|
178
|
+
this.iterateListeners((l) => l.onChange?.());
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
class SingleQueryWork {
|
|
182
|
+
config;
|
|
183
|
+
emit;
|
|
184
|
+
result;
|
|
185
|
+
abortController = null;
|
|
186
|
+
disposed = false;
|
|
187
|
+
constructor(config, initial, emit) {
|
|
188
|
+
this.config = config;
|
|
189
|
+
this.emit = emit;
|
|
190
|
+
this.result = {
|
|
191
|
+
...initial,
|
|
192
|
+
isFetching: true,
|
|
193
|
+
fetchReason: 'initial',
|
|
194
|
+
refresh: this.refresh
|
|
195
|
+
};
|
|
196
|
+
this.run();
|
|
197
|
+
}
|
|
198
|
+
refresh = async (signal) => {
|
|
199
|
+
if (this.disposed)
|
|
200
|
+
return;
|
|
201
|
+
await this.run(signal);
|
|
202
|
+
};
|
|
203
|
+
setResult(update) {
|
|
204
|
+
this.result = { ...this.result, ...update };
|
|
205
|
+
this.emit();
|
|
206
|
+
}
|
|
207
|
+
async run(outerSignal) {
|
|
208
|
+
this.abortController?.abort();
|
|
209
|
+
const controller = (this.abortController = new AbortController());
|
|
210
|
+
const signal = controller.signal;
|
|
211
|
+
const isAborted = () => signal.aborted || outerSignal?.aborted == true;
|
|
212
|
+
if (!this.result.isLoading || !this.result.isFetching || this.result.error) {
|
|
213
|
+
this.setResult({ isLoading: true, isFetching: true, error: undefined, fetchReason: 'initial' });
|
|
214
|
+
}
|
|
215
|
+
const { query, sql, parameters, db } = this.config;
|
|
216
|
+
try {
|
|
217
|
+
const data = await query.execute({ sql, parameters: [...parameters], db });
|
|
218
|
+
if (isAborted())
|
|
219
|
+
return;
|
|
220
|
+
this.setResult({ isLoading: false, isFetching: false, data, error: undefined, fetchReason: undefined });
|
|
221
|
+
}
|
|
222
|
+
catch (error) {
|
|
223
|
+
if (isAborted())
|
|
224
|
+
return;
|
|
225
|
+
this.setResult({ isLoading: false, isFetching: false, data: [], error: error, fetchReason: undefined });
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
pendingResultFor() {
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
tryUpdate() {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
dispose() {
|
|
235
|
+
this.abortController?.abort();
|
|
236
|
+
this.abortController = null;
|
|
237
|
+
this.disposed = true;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
class WatchedQueryWork {
|
|
241
|
+
config;
|
|
242
|
+
emit;
|
|
243
|
+
result;
|
|
244
|
+
watch;
|
|
245
|
+
disposeListener;
|
|
246
|
+
/**
|
|
247
|
+
* Set after calling `updateSettings` until the watched query starts applying the new settings. We report
|
|
248
|
+
* `isFetching` in the meantime so that the hook doesn't briefly appear settled with stale data.
|
|
249
|
+
*/
|
|
250
|
+
pendingUpdate = false;
|
|
251
|
+
/**
|
|
252
|
+
* Set by {@link tryUpdate} until the updated query has produced its first result. Until then, `data` still holds the
|
|
253
|
+
* previous query's rows, which is reported as `fetchReason: 'settings-changed'`.
|
|
254
|
+
*/
|
|
255
|
+
awaitingUpdatedResult = false;
|
|
256
|
+
reportFetching;
|
|
257
|
+
constructor(config, initial, emit) {
|
|
258
|
+
this.config = config;
|
|
259
|
+
this.emit = emit;
|
|
260
|
+
this.result = initial;
|
|
261
|
+
this.reportFetching = config.reportFetching;
|
|
262
|
+
const { db, query, rowComparator, throttleMs, reportFetching } = config;
|
|
263
|
+
const customQuery = db.customQuery(query);
|
|
264
|
+
this.watch = rowComparator
|
|
265
|
+
? customQuery.differentialWatch({ rowComparator, reportFetching, throttleMs })
|
|
266
|
+
: customQuery.watch({ reportFetching, throttleMs });
|
|
267
|
+
this.disposeListener = this.watch.registerListener({
|
|
268
|
+
onStateChange: (state) => {
|
|
269
|
+
if (this.awaitingUpdatedResult && !this.pendingUpdate && !state.isFetching) {
|
|
270
|
+
// The first completed fetch after the new settings were applied is the updated query's result.
|
|
271
|
+
this.awaitingUpdatedResult = false;
|
|
272
|
+
}
|
|
273
|
+
this.result = this.mapState(state);
|
|
274
|
+
this.emit();
|
|
275
|
+
},
|
|
276
|
+
settingsWillUpdate: () => {
|
|
277
|
+
// The watched query sets isFetching itself after this event, so we stop overriding it. We don't emit
|
|
278
|
+
// here because the state may not reflect that yet, the next onStateChange delivers the updated state.
|
|
279
|
+
this.pendingUpdate = false;
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
mapState(state) {
|
|
284
|
+
// The state object may be mutated by the watched query, so we copy it into a fresh snapshot.
|
|
285
|
+
const isFetching = state.isFetching || (this.pendingUpdate && this.reportFetching);
|
|
286
|
+
return {
|
|
287
|
+
data: state.data,
|
|
288
|
+
isLoading: state.isLoading,
|
|
289
|
+
isFetching,
|
|
290
|
+
error: state.error ?? undefined,
|
|
291
|
+
fetchReason: this.fetchReasonFor(state.isLoading, isFetching)
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
fetchReasonFor(isLoading, isFetching) {
|
|
295
|
+
if (!isFetching)
|
|
296
|
+
return undefined;
|
|
297
|
+
if (isLoading)
|
|
298
|
+
return 'initial';
|
|
299
|
+
return this.awaitingUpdatedResult ? 'settings-changed' : 'tables-changed';
|
|
300
|
+
}
|
|
301
|
+
canUpdate(config) {
|
|
302
|
+
return (config.kind == 'query' &&
|
|
303
|
+
!config.runOnce &&
|
|
304
|
+
config.db === this.config.db &&
|
|
305
|
+
!!config.rowComparator == !!this.config.rowComparator);
|
|
306
|
+
}
|
|
307
|
+
pendingResultFor(config) {
|
|
308
|
+
if (!this.canUpdate(config)) {
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
// Keep the previous data while the updated query runs.
|
|
312
|
+
const isFetching = this.result.isFetching || config.reportFetching;
|
|
313
|
+
return {
|
|
314
|
+
...this.result,
|
|
315
|
+
isFetching,
|
|
316
|
+
fetchReason: !isFetching ? undefined : this.result.isLoading ? 'initial' : 'settings-changed'
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
tryUpdate(config) {
|
|
320
|
+
if (!this.canUpdate(config)) {
|
|
321
|
+
return false;
|
|
322
|
+
}
|
|
323
|
+
this.config = config;
|
|
324
|
+
this.reportFetching = config.reportFetching;
|
|
325
|
+
this.pendingUpdate = true;
|
|
326
|
+
this.awaitingUpdatedResult = true;
|
|
327
|
+
this.watch.updateSettings({
|
|
328
|
+
query: config.query,
|
|
329
|
+
throttleMs: config.throttleMs,
|
|
330
|
+
reportFetching: config.reportFetching
|
|
331
|
+
});
|
|
332
|
+
this.result = this.mapState(this.watch.state);
|
|
333
|
+
return true;
|
|
334
|
+
}
|
|
335
|
+
dispose() {
|
|
336
|
+
this.disposeListener();
|
|
337
|
+
this.watch.close();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
//# sourceMappingURL=QueryRunner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueryRunner.js","sourceRoot":"","sources":["../../../src/hooks/watched/QueryRunner.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EAKb,MAAM,mBAAmB,CAAC;AAyC3B,SAAS,YAAY,CAAU,CAAwB,EAAE,CAAwB;IAC/E,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IA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ACrB,IAAI,CAAC,cAAc,GAAG,MAAM,CAAC,cAAc,CAAC;QAC5C,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC;QAC1B,IAAI,CAAC,qBAAqB,GAAG,IAAI,CAAC;QAClC,IAAI,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"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
|
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"}
|
|
@@ -6,9 +6,9 @@ export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
|
|
|
6
6
|
*
|
|
7
7
|
* When set, `useQuery` will subscribe to those streams (and automatically handle unsubscribing from them, too).
|
|
8
8
|
*
|
|
9
|
-
* If {@link QuerySyncStreamOptions} is set on a stream, `useQuery` will remain in a loading state until
|
|
10
|
-
* is marked as {@link SyncSubscriptionDescription.hasSynced}. This ensures the query is not missing rows
|
|
11
|
-
* been downloaded.
|
|
9
|
+
* If {@link QuerySyncStreamOptions.waitForStream} is set on a stream, `useQuery` will remain in a loading state until
|
|
10
|
+
* that stream is marked as {@link SyncSubscriptionDescription.hasSynced}. This ensures the query is not missing rows
|
|
11
|
+
* that haven't 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
14
|
*/
|
|
@@ -51,6 +51,15 @@ export interface DifferentialHookOptions<RowType> extends HookWatchOptions {
|
|
|
51
51
|
*/
|
|
52
52
|
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
|
|
53
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Why a `useQuery` hook is currently fetching.
|
|
56
|
+
*
|
|
57
|
+
* - `'initial'`: the first load of this query, `isLoading` is true as well.
|
|
58
|
+
* - `'tables-changed'`: a table the query depends on changed and the same query is re-running. `data` is still current.
|
|
59
|
+
* - `'settings-changed'`: the SQL, parameters or options changed. `data` still holds the previous query's rows until the
|
|
60
|
+
* updated query has produced its first result.
|
|
61
|
+
*/
|
|
62
|
+
export type FetchReason = 'initial' | 'tables-changed' | 'settings-changed';
|
|
54
63
|
export type ReadonlyQueryResult<RowType> = {
|
|
55
64
|
readonly data: ReadonlyArray<Readonly<RowType>>;
|
|
56
65
|
/**
|
|
@@ -62,6 +71,11 @@ export type ReadonlyQueryResult<RowType> = {
|
|
|
62
71
|
*/
|
|
63
72
|
readonly isFetching: boolean;
|
|
64
73
|
readonly error: Error | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Why the query is running while `isFetching` is true, `undefined` otherwise. `'settings-changed'` means the
|
|
76
|
+
* SQL, parameters or options changed and `data` still holds the previous query's rows until the new result arrives.
|
|
77
|
+
*/
|
|
78
|
+
readonly fetchReason?: FetchReason;
|
|
65
79
|
/**
|
|
66
80
|
* Function used to run the query again.
|
|
67
81
|
*/
|
|
@@ -78,6 +92,11 @@ export type QueryResult<RowType> = {
|
|
|
78
92
|
*/
|
|
79
93
|
isFetching: boolean;
|
|
80
94
|
error: Error | undefined;
|
|
95
|
+
/**
|
|
96
|
+
* Why the query is running while `isFetching` is true, `undefined` otherwise. `'settings-changed'` means the
|
|
97
|
+
* SQL, parameters or options changed and `data` still holds the previous query's rows until the new result arrives.
|
|
98
|
+
*/
|
|
99
|
+
fetchReason?: FetchReason;
|
|
81
100
|
/**
|
|
82
101
|
* Function used to run the query again.
|
|
83
102
|
*/
|
|
@@ -1,17 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export type InternalHookOptions<DataType> = {
|
|
4
|
-
query: WatchCompatibleQuery<DataType>;
|
|
5
|
-
powerSync: AbstractPowerSyncDatabase;
|
|
6
|
-
queryChanged: boolean;
|
|
7
|
-
active: boolean;
|
|
8
|
-
};
|
|
9
|
-
interface WatchCompatibleQueryWithParams<T> extends WatchCompatibleQuery<T> {
|
|
1
|
+
import { CompilableQuery, WatchCompatibleQuery } from '@powersync/common';
|
|
2
|
+
export interface WatchCompatibleQueryWithParams<T> extends WatchCompatibleQuery<T> {
|
|
10
3
|
stringifiedParameters?: string;
|
|
11
4
|
}
|
|
12
|
-
export declare const
|
|
13
|
-
export declare const constructCompatibleQuery: <RowType>(query: string | CompilableQuery<RowType>, parameters: any[], options: AdditionalOptions) => {
|
|
14
|
-
parsedQuery: WatchCompatibleQueryWithParams<RowType[]>;
|
|
15
|
-
queryChanged: boolean;
|
|
16
|
-
};
|
|
17
|
-
export {};
|
|
5
|
+
export declare const constructCompatibleQuery: <RowType>(query: string | CompilableQuery<RowType>, parameters?: any[]) => WatchCompatibleQueryWithParams<RowType[]>;
|
|
@@ -1,29 +1,7 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
|
-
import {
|
|
3
|
-
export const
|
|
4
|
-
|
|
5
|
-
try {
|
|
6
|
-
_compiled = query.compile();
|
|
7
|
-
}
|
|
8
|
-
catch (error) {
|
|
9
|
-
return false; // If compilation fails, we assume the query has changed
|
|
10
|
-
}
|
|
11
|
-
const compiled = _compiled;
|
|
12
|
-
const stringifiedParams = query.stringifiedParameters ?? JSON.stringify(compiled.parameters);
|
|
13
|
-
const stringifiedOptions = JSON.stringify(options);
|
|
14
|
-
const previousQueryRef = React.useRef({ sqlStatement: compiled.sql, stringifiedParams, stringifiedOptions });
|
|
15
|
-
if (previousQueryRef.current.sqlStatement !== compiled.sql ||
|
|
16
|
-
previousQueryRef.current.stringifiedParams != stringifiedParams ||
|
|
17
|
-
previousQueryRef.current.stringifiedOptions != stringifiedOptions) {
|
|
18
|
-
previousQueryRef.current.sqlStatement = compiled.sql;
|
|
19
|
-
previousQueryRef.current.stringifiedParams = stringifiedParams;
|
|
20
|
-
previousQueryRef.current.stringifiedOptions = stringifiedOptions;
|
|
21
|
-
return true;
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
};
|
|
25
|
-
export const constructCompatibleQuery = (query, parameters = [], options) => {
|
|
26
|
-
const powerSync = usePowerSync();
|
|
2
|
+
import { usePowerSyncOrNull } from '../PowerSyncContext.js';
|
|
3
|
+
export const constructCompatibleQuery = (query, parameters = []) => {
|
|
4
|
+
const powerSync = usePowerSyncOrNull();
|
|
27
5
|
const stringifiedParameters = React.useMemo(() => JSON.stringify(parameters), [parameters]);
|
|
28
6
|
const parsedQuery = React.useMemo(() => {
|
|
29
7
|
if (typeof query == 'string') {
|
|
@@ -33,7 +11,7 @@ export const constructCompatibleQuery = (query, parameters = [], options) => {
|
|
|
33
11
|
parameters
|
|
34
12
|
}),
|
|
35
13
|
execute: () => powerSync.getAll(query, parameters),
|
|
36
|
-
// Setting this is a small optimization that avoids
|
|
14
|
+
// Setting this is a small optimization that avoids QueryRunner recomputing the JSON representation.
|
|
37
15
|
stringifiedParameters
|
|
38
16
|
};
|
|
39
17
|
}
|
|
@@ -53,10 +31,6 @@ export const constructCompatibleQuery = (query, parameters = [], options) => {
|
|
|
53
31
|
};
|
|
54
32
|
}
|
|
55
33
|
}, [query, powerSync, stringifiedParameters]);
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
parsedQuery,
|
|
59
|
-
queryChanged
|
|
60
|
-
};
|
|
34
|
+
return parsedQuery;
|
|
61
35
|
};
|
|
62
36
|
//# sourceMappingURL=watch-utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"watch-utils.js","sourceRoot":"","sources":["../../../src/hooks/watched/watch-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"watch-utils.js","sourceRoot":"","sources":["../../../src/hooks/watched/watch-utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAM5D,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAU,KAAwC,EAAE,aAAoB,EAAE,EAAE,EAAE;IACpH,MAAM,SAAS,GAAG,kBAAkB,EAAE,CAAC;IACvC,MAAM,qBAAqB,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC;IAE5F,MAAM,WAAW,GAAG,KAAK,CAAC,OAAO,CAA4C,GAAG,EAAE;QAChF,IAAI,OAAO,KAAK,IAAI,QAAQ,EAAE,CAAC;YAC7B,OAAO;gBACL,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC;oBACd,GAAG,EAAE,KAAK;oBACV,UAAU;iBACX,CAAC;gBACF,OAAO,EAAE,GAAG,EAAE,CAAC,SAAU,CAAC,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC;gBACnD,oGAAoG;gBACpG,qBAAqB;aACtB,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,OAAO;gBACL,0DAA0D;gBAC1D,OAAO,EAAE,GAAG,EAAE;oBACZ,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC;oBACjC,OAAO;wBACL,GAAG,EAAE,QAAQ,CAAC,GAAG;wBACjB,UAAU,EAAE,CAAC,GAAG,QAAQ,CAAC,UAAU,CAAC;qBACrC,CAAC;gBACJ,CAAC;gBACD,OAAO,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,EAAE;gBAC9B,6GAA6G;gBAC7G,YAAY;aACb,CAAC;QACJ,CAAC;IACH,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,EAAE,qBAAqB,CAAC,CAAC,CAAC;IAE9C,OAAO,WAAW,CAAC;AACrB,CAAC,CAAC"}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { usePowerSync, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
2
2
|
export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
|
|
3
3
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
4
4
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
@@ -6,4 +6,4 @@ export { useAllSyncStreamsHaveSynced, useSyncStream, UseSyncStreamOptions } from
|
|
|
6
6
|
export { useStatus } from './hooks/useStatus.js';
|
|
7
7
|
export { useQuery } from './hooks/watched/useQuery.js';
|
|
8
8
|
export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
|
|
9
|
-
export { AdditionalOptions, QuerySyncStreamOptions } from './hooks/watched/watch-types.js';
|
|
9
|
+
export { AdditionalOptions, FetchReason, QuerySyncStreamOptions } from './hooks/watched/watch-types.js';
|
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { usePowerSync, PowerSyncContext } from './hooks/PowerSyncContext.js';
|
|
2
2
|
export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
|
|
3
3
|
export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
|
|
4
4
|
export { useAllSyncStreamsHaveSynced, useSyncStream } from './hooks/streams.js';
|
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,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAE7E,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"}
|