Skip to content

Commit 42b9908

Browse files
author
gobbimar
committed
feat(query-core): require initialValue when using custom reducer in streamedQuery
Add type safety by making initialValue mandatory when providing a custom reducer function. This prevents runtime errors and ensures proper data initialization for custom data structures beyond simple arrays. Use conditional types to enforce the relationship between reducer and initialValue parameters, maintaining backward compatibility for simple array-based streaming while requiring explicit initialization for custom reducers. BREAKING CHANGE: When using a custom reducer function with streamedQuery, the initialValue parameter is now required and must be provided.
1 parent d46d488 commit 42b9908

1 file changed

Lines changed: 6 additions & 12 deletions

File tree

packages/query-core/src/streamedQuery.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,16 @@ export function streamedQuery<
4141
TQueryFnData = unknown,
4242
TData = Array<TQueryFnData>,
4343
TQueryKey extends QueryKey = QueryKey,
44-
>(params: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<
44+
>({
45+
queryFn,
46+
refetchMode = 'reset',
47+
reducer = (items, chunk) => addToEnd(items as Array<TQueryFnData>, chunk) as TData,
48+
initialValue = [] as TData,
49+
}: StreamedQueryParams<TQueryFnData, TData, TQueryKey>): QueryFunction<
4550
TData,
4651
TQueryKey
4752
> {
48-
let reducer;
49-
let initialValue;
50-
const {refetchMode='reset', queryFn} = params;
5153

52-
if('reducer' in params && typeof params.reducer === 'function'){
53-
reducer=params.reducer;
54-
initialValue=params.initialValue;
55-
}else{
56-
initialValue=[] as TData;
57-
reducer=(items: TData, chunk: TQueryFnData) => addToEnd(items as Array<TQueryFnData>, chunk) as TData;
58-
}
59-
6054
return async (context) => {
6155
const query = context.client
6256
.getQueryCache()

0 commit comments

Comments
 (0)