Skip to content

Commit 4704723

Browse files
cleanup interfaces
1 parent 83e99e2 commit 4704723

5 files changed

Lines changed: 58 additions & 28 deletions

File tree

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ export interface WatchOnChangeHandler {
109109
onError?: (error: Error) => void;
110110
}
111111

112+
export interface ComparatorWatchOptions<DataType> {
113+
mode: 'comparison';
114+
comparator?: WatchedQueryComparator<DataType>;
115+
}
116+
117+
export interface IncrementalWatchOptions<DataType> {
118+
watch: WatchedQueryOptions<DataType>;
119+
processor?: ComparatorWatchOptions<DataType>;
120+
}
121+
112122
export interface PowerSyncDBListener extends StreamingSyncImplementationListener {
113123
initialized: () => void;
114124
schemaChanged: (schema: Schema) => void;
@@ -872,23 +882,19 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
872882
}
873883

874884
// TODO names and types
875-
incrementalWatch<DataType>(
876-
options: { watchOptions: WatchedQueryOptions<DataType> } & {
877-
mode: 'comparison';
878-
comparator?: WatchedQueryComparator<DataType>;
879-
}
880-
): WatchedQuery<DataType> {
881-
switch (options.mode) {
885+
incrementalWatch<DataType>(options: IncrementalWatchOptions<DataType>): WatchedQuery<DataType> {
886+
const { watch, processor } = options;
887+
888+
switch (options.processor?.mode) {
882889
case 'comparison':
890+
default:
883891
return new OnChangeQueryProcessor({
884892
db: this,
885-
comparator: options.comparator ?? {
893+
comparator: processor?.comparator ?? {
886894
checkEquality: (a, b) => JSON.stringify(a) == JSON.stringify(b)
887895
},
888-
watchOptions: options.watchOptions
896+
watchOptions: watch
889897
});
890-
default:
891-
throw new Error(`Invalid mode specified ${options.mode}`);
892898
}
893899
}
894900

packages/common/src/client/watched/WatchedQuery.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CompiledQuery } from 'src/types/types.js';
21
import { BaseListener, BaseObserverInterface } from '../../utils/BaseObserver.js';
32

43
export interface WatchedQueryState<Data> {
@@ -26,14 +25,19 @@ export interface WatchedQueryState<Data> {
2625
data: Data;
2726
}
2827

28+
export interface WatchCompiledQuery {
29+
sql: string;
30+
parameters: any[];
31+
}
2932
/**
33+
*
3034
* @internal
3135
* Similar to {@link CompatibleQuery}, except the `execute` method
3236
* does not enforce an Array result type.
3337
*/
3438
export interface WatchCompatibleQuery<ResultType> {
35-
execute(compiled: CompiledQuery): Promise<ResultType>;
36-
compile(): CompiledQuery;
39+
execute(compiled: WatchCompiledQuery): Promise<ResultType>;
40+
compile(): WatchCompiledQuery;
3741
}
3842

3943
/**

packages/react/src/QueryStore.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ export class QueryStore {
2020
}
2121

2222
const watchedQuery = this.db.incrementalWatch({
23-
mode: 'comparison',
24-
watchOptions: {
23+
watch: {
2524
query,
2625
placeholderData: [],
2726
throttleMs: options.throttleMs

packages/react/src/hooks/useQuery.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ const useWatchedQuery = <RowType = unknown>(
121121
const createWatchedQuery = React.useCallback(() => {
122122
return powerSync.incrementalWatch<RowType[]>({
123123
// This always enables comparison. Might want to be able to disable this??
124-
mode: 'comparison',
125-
watchOptions: {
124+
watch: {
126125
placeholderData: [],
127126
query,
128127
throttleMs: hookOptions.throttleMs,
@@ -182,14 +181,20 @@ export const constructCompatibleQuery = <RowType>(
182181
return {
183182
compile: () => ({
184183
sql: query,
185-
parameters: parameters
184+
parameters
186185
}),
187186
execute: () => powerSync.getAll(query, parameters)
188187
};
189188
} else {
190189
return {
191190
// Generics differ a bit but holistically this is the same
192-
compile: () => query.compile(),
191+
compile: () => {
192+
const compiled = query.compile();
193+
return {
194+
sql: compiled.sql,
195+
parameters: [...compiled.parameters]
196+
};
197+
},
193198
execute: () => query.execute()
194199
};
195200
}

packages/web/tests/watch.test.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ describe('Watch Tests', { sequential: true }, () => {
327327

328328
it('should stream watch results', async () => {
329329
const watch = powersync.incrementalWatch({
330-
mode: 'comparison',
331-
watchOptions: {
330+
watch: {
332331
query: {
333332
compile: () => ({
334333
sql: 'SELECT * FROM assets',
335334
parameters: []
336335
}),
337336
execute: ({ sql, parameters }) => powersync.getAll(sql, parameters)
338-
}
337+
},
338+
placeholderData: []
339339
}
340340
});
341341

@@ -369,8 +369,16 @@ describe('Watch Tests', { sequential: true }, () => {
369369

370370
it('should only report updates for relevant changes', async () => {
371371
const watch = powersync.incrementalWatch({
372-
sql: 'SELECT * FROM assets where make = ?',
373-
parameters: ['test']
372+
watch: {
373+
query: {
374+
compile: () => ({
375+
sql: 'SELECT * FROM assets where make = ?',
376+
parameters: ['test']
377+
}),
378+
execute: ({ sql, parameters }) => powersync.getAll(sql, parameters)
379+
},
380+
placeholderData: []
381+
}
374382
});
375383

376384
let notificationCount = 0;
@@ -398,9 +406,17 @@ describe('Watch Tests', { sequential: true }, () => {
398406

399407
it('should not report fetching status', async () => {
400408
const watch = powersync.incrementalWatch({
401-
sql: 'SELECT * FROM assets where make = ?',
402-
parameters: ['test'],
403-
reportFetching: false
409+
watch: {
410+
query: {
411+
compile: () => ({
412+
sql: 'SELECT * FROM assets where make = ?',
413+
parameters: ['test']
414+
}),
415+
execute: ({ sql, parameters }) => powersync.getAll(sql, parameters)
416+
},
417+
placeholderData: [],
418+
reportFetching: false
419+
}
404420
});
405421

406422
expect(watch.state.isFetching).false;

0 commit comments

Comments
 (0)