-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(query-core): add custom reducer support to streamedQuery #9532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TkDodo
merged 14 commits into
TanStack:main
from
marcog83:feature/added-optional-reducer-to-streamed-query
Sep 4, 2025
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
d2c4807
feat(query-core): add custom reducer support to streamedQuery
9474d4c
Merge branch 'main' into feature/added-optional-reducer-to-streamed-q…
TkDodo 890e373
ci: apply automated fixes
autofix-ci[bot] 0a4e065
feat(query-core): require initialValue when using custom reducer in s…
d46d488
feat(query-core): require initialValue when using custom reducer in s…
42b9908
feat(query-core): require initialValue when using custom reducer in s…
14b6241
removed personal vscode workspace file
5f68a62
updated documentation
20e1d8d
Merge remote-tracking branch 'origin/main' into feature/added-optiona…
da93cc7
fix(docs): clarify reducer function description in streamedQuery docu…
7876335
Merge branch 'main' into feature/added-optional-reducer-to-streamed-q…
TkDodo e3a54ea
ci: apply automated fixes
autofix-ci[bot] ade50e9
fix(tests): Code Review :: update streamedQuery tests to use correct …
d627594
Merge remote-tracking branch 'origin/feature/added-optional-reducer-t…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -350,13 +350,18 @@ describe('streamedQuery', () => { | |||||
unsubscribe() | ||||||
}) | ||||||
|
||||||
test('should support maxChunks', async () => { | ||||||
test('should support custom reducer', async () => { | ||||||
const key = queryKey() | ||||||
|
||||||
const observer = new QueryObserver(queryClient, { | ||||||
queryKey: key, | ||||||
queryFn: streamedQuery({ | ||||||
queryFn: () => createAsyncNumberGenerator(3), | ||||||
maxChunks: 2, | ||||||
queryFn: streamedQuery<number, Record<number, boolean>>({ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let’s not pass types this way - as it won’t test if type inference works. Instead, let’s type the initialValue:
|
||||||
queryFn: () => createAsyncNumberGenerator(2), | ||||||
reducer: (acc, chunk) => ({ | ||||||
...acc, | ||||||
[chunk]: true, | ||||||
}), | ||||||
initialValue: {} | ||||||
}), | ||||||
}) | ||||||
|
||||||
|
@@ -368,41 +373,34 @@ describe('streamedQuery', () => { | |||||
data: undefined, | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'fetching', | ||||||
data: [0], | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'fetching', | ||||||
data: [0, 1], | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
await vi.advanceTimersByTimeAsync(100) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'idle', | ||||||
data: [1, 2], | ||||||
data: { | ||||||
0: true, | ||||||
1: true, | ||||||
}, | ||||||
}) | ||||||
|
||||||
unsubscribe() | ||||||
}) | ||||||
|
||||||
test('maxChunks with append refetch', async () => { | ||||||
test('should support custom reducer with initialValue', async () => { | ||||||
const key = queryKey() | ||||||
const observer = new QueryObserver(queryClient, { | ||||||
queryKey: key, | ||||||
queryFn: streamedQuery({ | ||||||
queryFn: () => createAsyncNumberGenerator(3), | ||||||
maxChunks: 2, | ||||||
refetchMode: 'append', | ||||||
queryFn: streamedQuery<number, Record<number, boolean>>({ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here please:
Suggested change
|
||||||
queryFn: () => createAsyncNumberGenerator(2), | ||||||
reducer: (acc, chunk) => ({ | ||||||
...acc, | ||||||
[chunk]: true, | ||||||
}), | ||||||
initialValue: { | ||||||
10: true, | ||||||
11: true, | ||||||
}, | ||||||
}), | ||||||
}) | ||||||
|
||||||
|
@@ -414,62 +412,17 @@ describe('streamedQuery', () => { | |||||
data: undefined, | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'fetching', | ||||||
data: [0], | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'fetching', | ||||||
data: [0, 1], | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'idle', | ||||||
data: [1, 2], | ||||||
}) | ||||||
|
||||||
void observer.refetch() | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(10) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'fetching', | ||||||
data: [1, 2], | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(40) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'fetching', | ||||||
data: [2, 0], | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'fetching', | ||||||
data: [0, 1], | ||||||
}) | ||||||
|
||||||
await vi.advanceTimersByTimeAsync(50) | ||||||
await vi.advanceTimersByTimeAsync(100) | ||||||
|
||||||
expect(observer.getCurrentResult()).toMatchObject({ | ||||||
status: 'success', | ||||||
fetchStatus: 'idle', | ||||||
data: [1, 2], | ||||||
data: { | ||||||
10: true, | ||||||
11: true, | ||||||
0: true, | ||||||
1: true, | ||||||
}, | ||||||
}) | ||||||
|
||||||
unsubscribe() | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.