Skip to content

Commit 2749ece

Browse files
committed
fix(streamedQuery): fall back to initialValue when stream yields no values
Add failing test for empty streams and update implementation to avoid returning undefined. Includes changeset for patch release.
1 parent 6226325 commit 2749ece

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

.changeset/puny-walls-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/query-core': patch
3+
---
4+
5+
Fix streamedQuery to avoid returning undefined when the stream yields no values

packages/query-core/src/__tests__/streamedQuery.test.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,37 @@ describe('streamedQuery', () => {
128128
unsubscribe()
129129
})
130130

131+
test('should handle empty streams', async () => {
132+
133+
const key = queryKey()
134+
135+
const observer = new QueryObserver(queryClient, {
136+
queryKey: key,
137+
queryFn: streamedQuery({
138+
streamFn: () => { return {async *[Symbol.asyncIterator]() {}}},
139+
}),
140+
})
141+
142+
const unsubscribe = observer.subscribe(vi.fn())
143+
144+
expect(observer.getCurrentResult()).toMatchObject({
145+
status: 'pending',
146+
fetchStatus: 'fetching',
147+
data: undefined,
148+
})
149+
150+
await vi.advanceTimersByTimeAsync(50)
151+
152+
expect(observer.getCurrentResult()).toMatchObject({
153+
status: 'success',
154+
fetchStatus: 'idle',
155+
data: [],
156+
})
157+
158+
unsubscribe()
159+
160+
})
161+
131162
test('should replace on refetch', async () => {
132163
const key = queryKey()
133164
const observer = new QueryObserver(queryClient, {

packages/query-core/src/streamedQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ export function streamedQuery<
9494
context.client.setQueryData<TData>(context.queryKey, result)
9595
}
9696

97-
return context.client.getQueryData(context.queryKey)!
97+
return context.client.getQueryData(context.queryKey) ?? initialValue
9898
}
9999
}

0 commit comments

Comments
 (0)