Skip to content

Commit

Permalink
CancellationToken is used internally in order to cancel requests sepa…
Browse files Browse the repository at this point in the history
…rately
  • Loading branch information
Kfir Matityahu committed Dec 16, 2024
1 parent 7751860 commit 9ad1b9c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
31 changes: 22 additions & 9 deletions chopper/lib/src/base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ base class ChopperClient {

final bool _clientIsInternal;

final http.CancellationToken? cancellationToken;
http.CancellationToken? _cancellationToken;

/// Creates and configures a [ChopperClient].
///
Expand Down Expand Up @@ -109,7 +109,7 @@ base class ChopperClient {
this.authenticator,
this.converter,
this.errorConverter,
this.cancellationToken,
http.CancellationToken? cancellationToken,
Iterable<ChopperService>? services,
}) : assert(
baseUrl == null || !baseUrl.hasQuery,
Expand All @@ -118,6 +118,7 @@ base class ChopperClient {
),
baseUrl = baseUrl ?? Uri(),
httpClient = client ?? http.Client(),
_cancellationToken = cancellationToken,
_clientIsInternal = client == null {
_services = <Type, ChopperService>{
for (final ChopperService service in services?.toSet() ?? [])
Expand Down Expand Up @@ -167,21 +168,28 @@ base class ChopperClient {
Request request, {
ConvertRequest? requestConverter,
ConvertResponse<BodyType>? responseConverter,
http.CancellationToken? cancellationToken
}) async {
final call = Call(
request: request,
client: this,
requestCallback: _requestController.add,
cancellationToken: cancellationToken,
);

final response = await call.execute<BodyType, InnerType>(
requestConverter,
responseConverter,
);
Response<BodyType> response;
try {
response = await call.execute<BodyType, InnerType>(
requestConverter,
responseConverter,
cancellationToken: _cancellationToken
);

_responseController.add(response);
_responseController.add(response);

}
on http.CancelledException {
_cancellationToken = http.CancellationToken();
rethrow;
}

return response;
}
Expand Down Expand Up @@ -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`,
Expand Down
4 changes: 1 addition & 3 deletions chopper/lib/src/chain/call.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Call {
required this.request,
required this.client,
required this.requestCallback,
this.cancellationToken,
});

/// Request to be executed.
Expand All @@ -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<Response<BodyType>> execute<BodyType, InnerType>(
ConvertRequest? requestConverter,
ConvertResponse<BodyType>? responseConverter,
{http.CancellationToken? cancellationToken}
) async {
final interceptors = <Interceptor>[
RequestConverterInterceptor(client.converter, requestConverter),
Expand Down
2 changes: 1 addition & 1 deletion chopper/lib/src/interceptors/http_call_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HttpCallInterceptor implements InternalInterceptor {
FutureOr<Response<BodyType>> intercept<BodyType>(
Chain<BodyType> chain) async {
final finalRequest = await chain.request.toBaseRequest();
final streamRes = await _httpClient.send(
final streamRes = await _httpClient.send(
finalRequest, cancellationToken: cancellationToken);

if (isTypeOf<BodyType, Stream<List<int>>>()) {
Expand Down

0 comments on commit 9ad1b9c

Please sign in to comment.