Skip to content

Commit

Permalink
⚡️ Remove the redundant stream transform (#2037)
Browse files Browse the repository at this point in the history
No issues were fixed by this.

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [ ] I have added the required tests to prove the fix/feature I'm
adding
- [ ] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

No semantic changes during the request.
  • Loading branch information
AlexV525 authored Nov 24, 2023
1 parent 2efc766 commit 6e83a11
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ See the [Migration Guide][] for the complete breaking changes list.**

## Unreleased

*None.*
- Improve `SyncTransformer`'s stream transform.

## 5.3.4

Expand Down
21 changes: 4 additions & 17 deletions dio/lib/src/transformers/sync_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,24 @@ class SyncTransformer extends Transformer {
return responseBody;
}

final showDownloadProgress = options.onReceiveProgress != null;
final int totalLength;
if (showDownloadProgress) {
if (options.onReceiveProgress != null) {
totalLength = int.parse(
responseBody.headers[Headers.contentLengthHeader]?.first ?? '-1',
);
} else {
totalLength = 0;
}

int received = 0;
final stream = responseBody.stream.transform<Uint8List>(
StreamTransformer.fromHandlers(
handleData: (data, sink) {
sink.add(data);
if (showDownloadProgress) {
received += data.length;
options.onReceiveProgress?.call(received, totalLength);
}
},
),
);

final streamCompleter = Completer<void>();
int finalLength = 0;
// Keep references to the data chunks and concatenate them later.
final chunks = <Uint8List>[];
final subscription = stream.listen(
(chunk) {
final subscription = responseBody.stream.listen(
(Uint8List chunk) {
finalLength += chunk.length;
chunks.add(chunk);
options.onReceiveProgress?.call(finalLength, totalLength);
},
onError: (Object error, StackTrace stackTrace) {
streamCompleter.completeError(error, stackTrace);
Expand Down

0 comments on commit 6e83a11

Please sign in to comment.