From b9ae0159013f5b2122a1fbb10064fa9360a9d4ab Mon Sep 17 00:00:00 2001 From: Alex Li Date: Fri, 15 Mar 2024 10:41:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20More=20callbacks=20and=20u?= =?UTF-8?q?nify=20usages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/dio_mixin.dart | 46 ++++++++++++++++++++---------------- dio/lib/src/interceptor.dart | 4 ++-- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/dio/lib/src/dio_mixin.dart b/dio/lib/src/dio_mixin.dart index 38ec18bda..3c7db7fb6 100644 --- a/dio/lib/src/dio_mixin.dart +++ b/dio/lib/src/dio_mixin.dart @@ -375,7 +375,7 @@ abstract class DioMixin implements Dio { // Convert the request interceptor to a functional callback in which // we can handle the return value of interceptor callback. FutureOr Function(dynamic) requestInterceptorWrapper( - InterceptorSendCallback interceptor, + InterceptorSendCallback cb, ) { return (dynamic incomingState) async { final state = incomingState as InterceptorState; @@ -383,25 +383,23 @@ abstract class DioMixin implements Dio { return listenCancelForAsyncTask( requestOptions.cancelToken, Future(() async { - final requestHandler = RequestInterceptorHandler(); - final res = - interceptor(state.data as RequestOptions, requestHandler); - if (res is Future) { - await res; + final handler = RequestInterceptorHandler(); + final callback = cb(state.data as RequestOptions, handler); + if (callback is Future) { + await callback; } - return requestHandler.future; + return handler.future; }), ); - } else { - return state; } + return state; }; } // Convert the response interceptor to a functional callback in which // we can handle the return value of interceptor callback. FutureOr Function(dynamic) responseInterceptorWrapper( - InterceptorSuccessCallback interceptor, + InterceptorSuccessCallback cb, ) { return (dynamic incomingState) async { final state = incomingState as InterceptorState; @@ -409,10 +407,13 @@ abstract class DioMixin implements Dio { state.type == InterceptorResultType.resolveCallFollowing) { return listenCancelForAsyncTask( requestOptions.cancelToken, - Future(() { - final responseHandler = ResponseInterceptorHandler(); - interceptor(state.data as Response, responseHandler); - return responseHandler.future; + Future(() async { + final handler = ResponseInterceptorHandler(); + final callback = cb(state.data as Response, handler); + if (callback is Future) { + await callback; + } + return handler.future; }), ); } else { @@ -424,16 +425,19 @@ abstract class DioMixin implements Dio { // Convert the error interceptor to a functional callback in which // we can handle the return value of interceptor callback. FutureOr Function(Object) errorInterceptorWrapper( - InterceptorErrorCallback interceptor, + InterceptorErrorCallback cb, ) { return (error) { final state = error is InterceptorState ? error : InterceptorState(assureDioException(error, requestOptions)); Future handleError() async { - final errorHandler = ErrorInterceptorHandler(); - interceptor(state.data, errorHandler); - return errorHandler.future; + final handler = ErrorInterceptorHandler(); + final callback = cb(state.data, handler); + if (callback is Future) { + await callback; + } + return handler.future; } // The request has already been cancelled, @@ -441,15 +445,15 @@ abstract class DioMixin implements Dio { if (state.data is DioException && state.data.type == DioExceptionType.cancel) { return handleError(); - } else if (state.type == InterceptorResultType.next || + } + if (state.type == InterceptorResultType.next || state.type == InterceptorResultType.rejectCallFollowing) { return listenCancelForAsyncTask( requestOptions.cancelToken, Future(handleError), ); - } else { - throw error; } + throw error; }; } diff --git a/dio/lib/src/interceptor.dart b/dio/lib/src/interceptor.dart index 51f7c289a..5259e8ba5 100644 --- a/dio/lib/src/interceptor.dart +++ b/dio/lib/src/interceptor.dart @@ -213,13 +213,13 @@ typedef InterceptorSendCallback = FutureOr Function( ); /// The signature of [Interceptor.onResponse]. -typedef InterceptorSuccessCallback = void Function( +typedef InterceptorSuccessCallback = FutureOr Function( Response response, ResponseInterceptorHandler handler, ); /// The signature of [Interceptor.onError]. -typedef InterceptorErrorCallback = void Function( +typedef InterceptorErrorCallback = FutureOr Function( DioException error, ErrorInterceptorHandler handler, );