Skip to content

Commit

Permalink
Send null to signal the end of the stream
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-james-dev committed Aug 15, 2023
1 parent 1845e38 commit 5f409fb
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions pkgs/java_http/lib/src/java_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,19 @@ class JavaClient extends BaseClient {
final responseHeaders = await events.next as Map<String, String>;

Stream<List<int>> responseBodyStream(Stream<dynamic> events) async* {
await for (final event in events) {
if (event is ClientException) {
throw event;
} else if (event is List<int>) {
if (event.length == 1 && event[0] == -1) {
receivePort.close();
try {
await for (final event in events) {
if (event is List<int>) {
yield event;
} else if (event is ClientException) {
throw event;
} else if (event == null) {
return;
}
yield event;
}
} finally {
// TODO: Should we kill the isolate here?
receivePort.close();
}
}

Expand Down Expand Up @@ -132,7 +135,7 @@ class JavaClient extends BaseClient {
httpUrlConnection.disconnect();

// Signals to the receiving isolate that we are done sending events.
request.sendPort.send([-1]);
request.sendPort.send(null);
}

void _setRequestBody(
Expand All @@ -153,6 +156,7 @@ class JavaClient extends BaseClient {
final statusCode = httpUrlConnection.getResponseCode();

if (statusCode == -1) {
// TODO: Send exception instead of throw.
throw ClientException(
'Status code can not be discerned from the response.', requestUrl);
}
Expand Down

0 comments on commit 5f409fb

Please sign in to comment.