@@ -104,7 +104,8 @@ class FetchHttpRequest {
104
104
Stream <int > get onError => onErrorController.stream;
105
105
106
106
// Response information
107
- CancelableOperation <dynamic >? _cancelable;
107
+ CancelableOperation <dynamic >? _cancelableFetch;
108
+ CancelableOperation <dynamic >? _cancelableSend;
108
109
dynamic _response;
109
110
dynamic get response => _response;
110
111
int get status =>
@@ -125,6 +126,12 @@ class FetchHttpRequest {
125
126
List <String >.from (js_util.callMethod (obj, 'keys' , []));
126
127
127
128
Future send ([List <int >? data]) async {
129
+ final doSend = _doSend (data);
130
+ _cancelableSend = CancelableOperation .fromFuture (doSend);
131
+ await doSend;
132
+ }
133
+
134
+ Future _doSend ([List <int >? data]) async {
128
135
final wgs = WorkerGlobalScope .instance;
129
136
_setReadyState (HttpRequest .LOADING );
130
137
@@ -139,11 +146,15 @@ class FetchHttpRequest {
139
146
referrerPolicy: referrerPolicy,
140
147
body: data,
141
148
headers: js_util.jsify (headers));
142
- final operation = _cancelable = CancelableOperation .fromFuture (
149
+ final operation = _cancelableFetch = CancelableOperation .fromFuture (
143
150
js_util.promiseToFuture (js_util.callMethod (wgs, 'fetch' , [uri, init])));
144
151
145
152
_response = await operation.value;
146
153
_setReadyState (HttpRequest .HEADERS_RECEIVED );
154
+ if (_cancelableSend? .isCanceled ?? false ) {
155
+ return ;
156
+ }
157
+
147
158
if (status < 200 || status >= 300 ) {
148
159
onErrorController.add (status);
149
160
}
@@ -156,6 +167,9 @@ class FetchHttpRequest {
156
167
157
168
while (true ) {
158
169
final result = await js_util.promiseToFuture (reader.read ());
170
+ if (_cancelableSend? .isCanceled ?? false ) {
171
+ return ;
172
+ }
159
173
final value = js_util.getProperty (result, 'value' );
160
174
if (value != null ) {
161
175
onProgressController.add (value as Uint8List );
@@ -180,7 +194,8 @@ class FetchHttpRequest {
180
194
}
181
195
182
196
void abort () async {
183
- await _cancelable? .cancel ();
197
+ await _cancelableFetch? .cancel ();
198
+ await _cancelableSend? .cancel ();
184
199
close ();
185
200
}
186
201
0 commit comments