Skip to content

Commit

Permalink
Made some minor changes to better handle when a streaming request is …
Browse files Browse the repository at this point in the history
…aborted so that we stop reading gracefully.
  • Loading branch information
Steve Browne committed May 3, 2022
1 parent e627db5 commit 32ad527
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/src/client/transport/fetch_transport.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ class FetchHttpRequest {
Stream<int> get onError => onErrorController.stream;

// Response information
CancelableOperation<dynamic>? _cancelable;
CancelableOperation<dynamic>? _cancelableFetch;
CancelableOperation<dynamic>? _cancelableSend;
dynamic _response;
dynamic get response => _response;
int get status =>
Expand All @@ -125,6 +126,12 @@ class FetchHttpRequest {
List<String>.from(js_util.callMethod(obj, 'keys', []));

Future send([List<int>? data]) async {
final doSend = _doSend(data);
_cancelableSend = CancelableOperation.fromFuture(doSend);
await doSend;
}

Future _doSend([List<int>? data]) async {
final wgs = WorkerGlobalScope.instance;
_setReadyState(HttpRequest.LOADING);

Expand All @@ -139,11 +146,15 @@ class FetchHttpRequest {
referrerPolicy: referrerPolicy,
body: data,
headers: js_util.jsify(headers));
final operation = _cancelable = CancelableOperation.fromFuture(
final operation = _cancelableFetch = CancelableOperation.fromFuture(
js_util.promiseToFuture(js_util.callMethod(wgs, 'fetch', [uri, init])));

_response = await operation.value;
_setReadyState(HttpRequest.HEADERS_RECEIVED);
if (_cancelableSend?.isCanceled ?? false) {
return;
}

if (status < 200 || status >= 300) {
onErrorController.add(status);
}
Expand All @@ -156,6 +167,9 @@ class FetchHttpRequest {

while (true) {
final result = await js_util.promiseToFuture(reader.read());
if (_cancelableSend?.isCanceled ?? false) {
return;
}
final value = js_util.getProperty(result, 'value');
if (value != null) {
onProgressController.add(value as Uint8List);
Expand All @@ -180,7 +194,8 @@ class FetchHttpRequest {
}

void abort() async {
await _cancelable?.cancel();
await _cancelableFetch?.cancel();
await _cancelableSend?.cancel();
close();
}

Expand Down

0 comments on commit 32ad527

Please sign in to comment.