diff --git a/chopper/lib/src/base.dart b/chopper/lib/src/base.dart index a649b781..211f76b5 100644 --- a/chopper/lib/src/base.dart +++ b/chopper/lib/src/base.dart @@ -45,7 +45,7 @@ base class ChopperClient { final bool _clientIsInternal; - final http.CancellationToken? cancellationToken; + http.CancellationToken? _cancellationToken; /// Creates and configures a [ChopperClient]. /// @@ -109,7 +109,7 @@ base class ChopperClient { this.authenticator, this.converter, this.errorConverter, - this.cancellationToken, + http.CancellationToken? cancellationToken, Iterable? services, }) : assert( baseUrl == null || !baseUrl.hasQuery, @@ -118,6 +118,7 @@ base class ChopperClient { ), baseUrl = baseUrl ?? Uri(), httpClient = client ?? http.Client(), + _cancellationToken = cancellationToken, _clientIsInternal = client == null { _services = { for (final ChopperService service in services?.toSet() ?? []) @@ -167,21 +168,28 @@ base class ChopperClient { Request request, { ConvertRequest? requestConverter, ConvertResponse? responseConverter, - http.CancellationToken? cancellationToken }) async { final call = Call( request: request, client: this, requestCallback: _requestController.add, - cancellationToken: cancellationToken, ); - final response = await call.execute( - requestConverter, - responseConverter, - ); + Response response; + try { + response = await call.execute( + requestConverter, + responseConverter, + cancellationToken: _cancellationToken + ); - _responseController.add(response); + _responseController.add(response); + + } + on http.CancelledException { + _cancellationToken = http.CancellationToken(); + rethrow; + } return response; } @@ -325,6 +333,11 @@ base class ChopperClient { ), ); + /// Cancels any requests by the cancellation token (if exists) + void cancelRequests(){ + _cancellationToken?.cancel(); + } + /// Disposes this [ChopperClient] to clean up memory. /// /// **Warning**: If a custom [http.Client] was provided while creating this `ChopperClient`, diff --git a/chopper/lib/src/chain/call.dart b/chopper/lib/src/chain/call.dart index ddf92ba6..d4d052f5 100644 --- a/chopper/lib/src/chain/call.dart +++ b/chopper/lib/src/chain/call.dart @@ -20,7 +20,6 @@ class Call { required this.request, required this.client, required this.requestCallback, - this.cancellationToken, }); /// Request to be executed. @@ -32,11 +31,10 @@ class Call { /// Callback to send intercepted and converted request to the stream controller. final void Function(Request event) requestCallback; - final http.CancellationToken? cancellationToken; - Future> execute( ConvertRequest? requestConverter, ConvertResponse? responseConverter, + {http.CancellationToken? cancellationToken} ) async { final interceptors = [ RequestConverterInterceptor(client.converter, requestConverter), diff --git a/chopper/lib/src/interceptors/http_call_interceptor.dart b/chopper/lib/src/interceptors/http_call_interceptor.dart index 48047920..f619a3d4 100644 --- a/chopper/lib/src/interceptors/http_call_interceptor.dart +++ b/chopper/lib/src/interceptors/http_call_interceptor.dart @@ -24,7 +24,7 @@ class HttpCallInterceptor implements InternalInterceptor { FutureOr> intercept( Chain chain) async { final finalRequest = await chain.request.toBaseRequest(); - final streamRes = await _httpClient.send( + final streamRes = await _httpClient.send( finalRequest, cancellationToken: cancellationToken); if (isTypeOf>>()) {