Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/puny-walls-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/query-core': patch
---

Fix streamedQuery to avoid returning undefined when the stream yields no values
31 changes: 31 additions & 0 deletions packages/query-core/src/__tests__/streamedQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,37 @@ describe('streamedQuery', () => {
unsubscribe()
})

test('should handle empty streams', async () => {

const key = queryKey()

const observer = new QueryObserver(queryClient, {
queryKey: key,
queryFn: streamedQuery({
streamFn: () => { return {async *[Symbol.asyncIterator]() {}}},
}),
})

const unsubscribe = observer.subscribe(vi.fn())

expect(observer.getCurrentResult()).toMatchObject({
status: 'pending',
fetchStatus: 'fetching',
data: undefined,
})

await vi.advanceTimersByTimeAsync(50)

expect(observer.getCurrentResult()).toMatchObject({
status: 'success',
fetchStatus: 'idle',
data: [],
})

unsubscribe()

})

test('should replace on refetch', async () => {
const key = queryKey()
const observer = new QueryObserver(queryClient, {
Expand Down
2 changes: 1 addition & 1 deletion packages/query-core/src/streamedQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,6 @@ export function streamedQuery<
context.client.setQueryData<TData>(context.queryKey, result)
}

return context.client.getQueryData(context.queryKey)!
return context.client.getQueryData(context.queryKey) ?? initialValue
}
}