Skip to content

Commit d87a902

Browse files
rename comparator to rowComparator for useQuery and query.differentialWatch methods.
1 parent c477a56 commit d87a902

File tree

16 files changed

+35
-34
lines changed

16 files changed

+35
-34
lines changed

.changeset/nine-pens-ring.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
[Potentially breaking change] The `useQuery` hook results are now explicitly defined as readonly. These values should not be mutated.
66

7-
- Added the ability to limit re-renders by specifying a `comparator` for query results. The `useQuery` hook will only emit `data` changes when the data has changed.
7+
- Added the ability to limit re-renders by specifying a `rowComparator` for query results. The `useQuery` hook will only emit `data` changes when the data has changed.
88

99
```javascript
1010
// The data here will maintain previous object references for unchanged items.
1111
const { data } = useQuery('SELECT * FROM lists WHERE name = ?', ['aname'], {
12-
comparator: {
12+
rowComparator: {
1313
keyBy: (item) => item.id,
1414
compareBy: (item) => JSON.stringify(item)
1515
}

.changeset/swift-guests-explain.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
'@powersync/react': minor
33
---
44

5-
- Added the ability to limit re-renders by specifying a `comparator` for query results. The `useQuery` hook will only emit `data` changes when the data has changed.
5+
- Added the ability to limit re-renders by specifying a `rowComparator` for query results. The `useQuery` hook will only emit `data` changes when the data has changed.
66

77
```javascript
88
// The data here will maintain previous object references for unchanged items.
99
const { data } = useQuery('SELECT * FROM lists WHERE name = ?', ['aname'], {
10-
comparator: {
10+
rowComparator: {
1111
keyBy: (item) => item.id,
1212
compareBy: (item) => JSON.stringify(item)
1313
}

demos/react-supabase-todolist/src/app/views/sql-console/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default function SQLConsolePage() {
6363
* The query here will only emit results when the query data set changes.
6464
* Result sets are compared by serializing each item to JSON and comparing the strings.
6565
*/
66-
comparator: {
66+
rowComparator: {
6767
keyBy: (item: any) => JSON.stringify(item),
6868
compareBy: (item: any) => JSON.stringify(item)
6969
}

packages/common/src/client/CustomQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class CustomQuery<RowType> implements Query<RowType> {
4444
differentialWatch(differentialWatchOptions: DifferentialWatchedQueryOptions<RowType>) {
4545
return new DifferentialQueryProcessor<RowType>({
4646
db: this.options.db,
47-
comparator: differentialWatchOptions?.comparator,
47+
rowComparator: differentialWatchOptions?.rowComparator,
4848
placeholderData: differentialWatchOptions?.placeholderData ?? [],
4949
watchOptions: {
5050
...this.resolveOptions(differentialWatchOptions),

packages/common/src/client/watched/processors/DifferentialQueryProcessor.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ export interface DifferentialWatchedQueryOptions<RowType> extends WatchedQueryOp
6565
placeholderData?: RowType[];
6666

6767
/**
68-
* Comparator used to identify and compare items in the result set.
68+
* Comparator used to identify and compare rows in the result set.
6969
* If not provided, the default comparator will be used which keys items by their `id` property if available,
7070
* otherwise it uses JSON stringification of the entire item for keying and comparison.
7171
* @defaultValue {@link DEFAULT_COMPARATOR}
7272
*/
73-
comparator?: DifferentialWatchedQueryComparator<RowType>;
73+
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
7474
}
7575

7676
/**
@@ -99,7 +99,7 @@ export type DifferentialWatchedQuery<RowType> = WatchedQuery<
9999
*/
100100
export interface DifferentialQueryProcessorOptions<RowType>
101101
extends AbstractQueryProcessorOptions<RowType[], DifferentialWatchedQuerySettings<RowType>> {
102-
comparator?: DifferentialWatchedQueryComparator<RowType>;
102+
rowComparator?: DifferentialWatchedQueryComparator<RowType>;
103103
}
104104

105105
type DataHashMap<RowType> = Map<string, { hash: string; item: RowType }>;
@@ -144,7 +144,7 @@ export class DifferentialQueryProcessor<RowType>
144144

145145
constructor(protected options: DifferentialQueryProcessorOptions<RowType>) {
146146
super(options);
147-
this.comparator = options.comparator ?? DEFAULT_COMPARATOR;
147+
this.comparator = options.rowComparator ?? DEFAULT_COMPARATOR;
148148
}
149149

150150
/*

packages/react/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ function MyWidget() {
346346
// Note that isFetching is set (by default) whenever the query is being fetched/checked.
347347
// This will result in `MyWidget` re-rendering for any change to the `cats` table.
348348
const { data, isLoading, isFetching } = useQuery(`SELECT * FROM cats WHERE breed = 'tabby'`, [], {
349-
comparator: {
349+
rowComparator: {
350350
keyBy: (item) => item.id,
351351
compareBy: (item) => JSON.stringify(item)
352352
}
@@ -374,7 +374,7 @@ function MyWidget() {
374374
// When reportFetching == false the object returned from useQuery will only be changed when the data, isLoading or error state changes.
375375
// This method performs a comparison in memory in order to determine changes.
376376
const { data, isLoading } = useQuery(`SELECT * FROM cats WHERE breed = 'tabby'`, [], {
377-
comparator: {
377+
rowComparator: {
378378
keyBy: (item) => item.id,
379379
compareBy: (item) => JSON.stringify(item)
380380
}

packages/react/src/QueryStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ export class QueryStore {
2424
return this.cache.get(key) as WatchedQuery<RowType[]>;
2525
}
2626

27-
const watch = options.comparator
27+
const watch = options.rowComparator
2828
? this.db.customQuery(query).differentialWatch({
29-
comparator: options.comparator,
29+
rowComparator: options.rowComparator,
3030
reportFetching: options.reportFetching,
3131
throttleMs: options.throttleMs
3232
})

packages/react/src/hooks/suspense/useSuspenseQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { useWatchedSuspenseQuery } from './useWatchedSuspenseQuery';
3232
* // The internal array object references are maintained for unchanged rows.
3333
* // The returned lists array is read only when a `comparator` is provided.
3434
* const { data: lists } = useSuspenseQuery('SELECT * from lists', [], {
35-
* comparator: {
35+
* rowComparator: {
3636
* keyBy: (item) => item.id,
3737
* compareBy: (item) => JSON.stringify(item)
3838
* }

packages/react/src/hooks/watched/useQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { constructCompatibleQuery } from './watch-utils';
2626
* // The internal array object references are maintained for unchanged rows.
2727
* // The returned lists array is read only when a `comparator` is provided.
2828
* const { data: lists } = useQuery('SELECT * from lists', [], {
29-
* comparator: {
29+
* rowComparator: {
3030
* keyBy: (item) => item.id,
3131
* compareBy: (item) => JSON.stringify(item)
3232
* }
@@ -78,7 +78,7 @@ export function useQuery<RowType = any>(
7878
// Maintains backwards compatibility with previous versions
7979
// Differentiation is opt-in by default
8080
// We emit new data for each table change by default.
81-
comparator: options.comparator
81+
rowComparator: options.rowComparator
8282
}
8383
});
8484
}

packages/react/src/hooks/watched/useWatchedQuery.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export const useWatchedQuery = <RowType = unknown>(
1717
const { query, powerSync, queryChanged, options: hookOptions } = options;
1818

1919
const createWatchedQuery = React.useCallback(() => {
20-
const watch = hookOptions.comparator
20+
const watch = hookOptions.rowComparator
2121
? powerSync.customQuery(query).differentialWatch({
22-
comparator: hookOptions.comparator,
22+
rowComparator: hookOptions.rowComparator,
2323
reportFetching: hookOptions.reportFetching,
2424
throttleMs: hookOptions.throttleMs
2525
})

0 commit comments

Comments
 (0)