Skip to content

api: Use streaming UTF-8 + JSON decoding for the response #972

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
merged 2 commits into from
Oct 2, 2024
Merged
Changes from all commits
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
6 changes: 4 additions & 2 deletions lib/api/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ class ApiConnection {
final int httpStatus = response.statusCode;
Map<String, dynamic>? json;
try {
final bytes = await response.stream.toBytes();
json = jsonUtf8Decoder.convert(bytes) as Map<String, dynamic>?;
// The stream-oriented `bind` method allows decoding to happen in chunks
// while the response is still being downloaded, improving latency.
final jsonStream = jsonUtf8Decoder.bind(response.stream);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be helpful to add a comment on why we do this here.

json = await jsonStream.single as Map<String, dynamic>?;
} catch (e) {
// We'll throw something below, seeing `json` is null.
}
Expand Down