Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/VB10/vexana into release/…
Browse files Browse the repository at this point in the history
…5.0.2
  • Loading branch information
VB10 committed Nov 27, 2024
2 parents 07347c3 + 0053c25 commit 1185a28
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ packages:
path: ".."
relative: true
source: path
version: "4.1.0"
version: "5.0.1"
vm_service:
dependency: transitive
description:
Expand Down
6 changes: 2 additions & 4 deletions lib/src/mixin/network_manager_core_operation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ mixin NetworkManagerCoreOperation<E extends INetworkModel<E>> {
CancelToken? cancelToken,
bool? forceUpdateDecode,
}) async {
if (!isErrorDialog ||
_noNetworkTryCount == NetworkManagerParameters.maxRetryCount) {
if (!isErrorDialog || _noNetworkTryCount == parameters.maxRetryCount) {
return onError.call(error);
}

Expand Down Expand Up @@ -89,8 +88,7 @@ mixin NetworkManagerCoreOperation<E extends INetworkModel<E>> {
CancelToken? cancelToken,
bool? forceUpdateDecode,
}) async {
if (!isErrorDialog ||
_noNetworkTryCount == NetworkManagerParameters.maxRetryCount) {
if (!isErrorDialog || _noNetworkTryCount == parameters.maxRetryCount) {
return onError.call(error);
}

Expand Down
12 changes: 10 additions & 2 deletions lib/src/mixin/network_manager_error_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ mixin NetworkManagerErrorInterceptor {
() => _createNewRequest(error),
onRetry: (_) async =>
error = await _createError(parameters, exception),
maxAttempts: NetworkManagerParameters.maxRetryCount,
maxAttempts: parameters.maxRetryCount,
retryIf: _retryIf,
);
// onResponseParse is null, then return response
Expand All @@ -53,10 +53,17 @@ mixin NetworkManagerErrorInterceptor {
}
// Call onResponseParse callback and return response
return handler.resolve(parameters.onResponseParse!(response));
} catch (_) {
} catch (err) {
/// cancel request & call onRefreshFail callback and unlock
error.requestOptions.cancelToken?.cancel();
parameters.onRefreshFail?.call();

/// If error is DioException, then return error
if (err is DioException) {
return handler.next(err);
}

/// If error is not DioException, then return error
return handler.next(exception);
}
},
Expand All @@ -78,6 +85,7 @@ mixin NetworkManagerErrorInterceptor {
isEnableLogger: params.isEnableLogger,
isEnableTest: params.isEnableTest,
options: parameters.baseOptions,
maxRetryCount: params.maxRetryCount,
),
);
}
Expand Down
5 changes: 3 additions & 2 deletions lib/src/mixin/network_manager_parameters.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ typedef OnReply = Response<dynamic> Function(
class NetworkManagerParameters extends Equatable {
final VoidCallback? onRefreshFail;

static const int maxRetryCount = 3;

final IFileManager? fileManager;

final bool? isEnableTest;
Expand All @@ -38,6 +36,8 @@ class NetworkManagerParameters extends Equatable {

final OnReply? onResponseParse;

final int maxRetryCount;

const NetworkManagerParameters({
required BaseOptions options,
this.onRefreshFail,
Expand All @@ -50,6 +50,7 @@ class NetworkManagerParameters extends Equatable {
this.interceptor,
this.onRefreshToken,
this.onResponseParse,
this.maxRetryCount = 3,
}) : baseOptions = options;

@override
Expand Down
2 changes: 2 additions & 0 deletions lib/src/network_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class NetworkManager<E extends INetworkModel<E>> extends dio.DioMixin
IFileManager? fileManager,
Interceptor? interceptor,
OnReply? onReply,
int maxRetryCount = 3,
}) {
parameters = NetworkManagerParameters(
options: options,
Expand All @@ -54,6 +55,7 @@ class NetworkManager<E extends INetworkModel<E>> extends dio.DioMixin
onRefreshToken: onRefreshToken,
onRefreshFail: onRefreshFail,
onResponseParse: onReply,
maxRetryCount: maxRetryCount,
);
_setup();
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/common/mock_network_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class MockErrorCustomNetworkManager extends NetworkManager<EmptyModel> {
: super(
options: BaseOptions(baseUrl: baseUrl),
isEnableTest: true,
maxRetryCount: 5,
onRefreshFail: () => print('onRefreshFail Triggered'),
onRefreshToken: (e, networkManager) async {
await onRefresh.call();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/network_manager_error_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void main() {
method: RequestType.GET,
);
stopServer();
expect(retryCount, 3);
expect(retryCount, 5);
expect(response.error?.statusCode, 401);
});

Expand Down Expand Up @@ -57,7 +57,7 @@ void main() {
),
]);
stopServer();
expect(retryCount, 3);
expect(retryCount, 5);
expect(cancelToken.isCancelled, true);
});

Expand Down

0 comments on commit 1185a28

Please sign in to comment.