Skip to content

Commit

Permalink
⚡️ More callbacks and unify usages
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Mar 15, 2024
1 parent 84aa773 commit b9ae015
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
46 changes: 25 additions & 21 deletions dio/lib/src/dio_mixin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -375,44 +375,45 @@ 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;
if (state.type == InterceptorResultType.next) {
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<dynamic> Function(dynamic) responseInterceptorWrapper(
InterceptorSuccessCallback interceptor,
InterceptorSuccessCallback cb,
) {
return (dynamic incomingState) async {
final state = incomingState as InterceptorState;
if (state.type == InterceptorResultType.next ||
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 {
Expand All @@ -424,32 +425,35 @@ 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<dynamic> Function(Object) errorInterceptorWrapper(
InterceptorErrorCallback interceptor,
InterceptorErrorCallback cb,
) {
return (error) {
final state = error is InterceptorState
? error
: InterceptorState(assureDioException(error, requestOptions));
Future<InterceptorState> 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,
// there is no need to listen for another cancellation.
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;
};
}

Expand Down
4 changes: 2 additions & 2 deletions dio/lib/src/interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,13 @@ typedef InterceptorSendCallback = FutureOr<void> Function(
);

/// The signature of [Interceptor.onResponse].
typedef InterceptorSuccessCallback = void Function(
typedef InterceptorSuccessCallback = FutureOr<void> Function(
Response<dynamic> response,
ResponseInterceptorHandler handler,
);

/// The signature of [Interceptor.onError].
typedef InterceptorErrorCallback = void Function(
typedef InterceptorErrorCallback = FutureOr<void> Function(
DioException error,
ErrorInterceptorHandler handler,
);
Expand Down

0 comments on commit b9ae015

Please sign in to comment.