Skip to content

Commit

Permalink
refactor: variable name change and updated readme
Browse files Browse the repository at this point in the history
  • Loading branch information
venkata-reddy-dev committed Mar 23, 2024
1 parent c8b4496 commit df3a12e
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ You have to pass a factory function `func` that returns an instance of an implem
### Overwriting registrations

If you try to register a type more than once you will fail with an assertion in debug mode because normally this is not needed and probably a bug.
If you really have to overwrite a registration, then you can by setting the property `allowReassignment==true`.
If you really have to overwrite a registration, then you can by setting the property `allowReassignment = true`.

### Skip Double registrations

If you try to register a type more than once and when `allowReassignment = false` you will fail with an assertion in debug mode.
If you want to just skip this double registration silently without an error, then you can by setting the property `skipDoubleRegistration = true`.

### Testing if a Singleton is already registered

Expand Down
6 changes: 4 additions & 2 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'dart:collection';

import 'package:async/async.dart';
import 'package:collection/collection.dart' show IterableExtension;
import 'package:meta/meta.dart';

part 'get_it_impl.dart';

Expand Down Expand Up @@ -152,8 +153,9 @@ abstract class GetIt {
bool allowReassignment = false;

/// By default it's throws error when [allowReassignment]= false. and trying to register same type
/// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true
bool ignoreReassignmentError = false;
/// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true
@visibleForTesting
bool skipDoubleRegistration = false;

/// Till V7.6.7 GetIt didn't allow to register multiple instances of the same type.
/// if you want to register multiple instances of the same type you can enable this
Expand Down
17 changes: 9 additions & 8 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,10 @@ class _GetItImplementation implements GetIt {
bool allowReassignment = false;

/// By default it's throws error when [allowReassignment]= false. and trying to register same type
/// If you really need, you can disable the Asserts / Errror by setting[ignoreReassignmentError]= true
/// If you really need, you can disable the Asserts / Errror by setting[skipDoubleRegistration]= true
@visibleForTesting
@override
bool ignoreReassignmentError = false;
bool skipDoubleRegistration = false;

/// Is used by several other functions to retrieve the correct [_ServiceFactory]
_ServiceFactory<T, dynamic, dynamic>?
Expand Down Expand Up @@ -1033,28 +1034,28 @@ class _GetItImplementation implements GetIt {
throwIf(
existingTypeRegistration.namedFactories.containsKey(instanceName) &&
!allowReassignment &&
!ignoreReassignmentError,
!skipDoubleRegistration,
ArgumentError(
'Object/factory with name $instanceName and '
'type $T is already registered inside GetIt. ',
),
);

/// skip registration
if (ignoreReassignmentError && !allowReassignment) {
/// skip double registration
if (skipDoubleRegistration && !allowReassignment) {
return;
}
} else {
if (existingTypeRegistration.factories.isNotEmpty) {
throwIfNot(
allowReassignment ||
GetIt.allowRegisterMultipleImplementationsOfoneType ||
ignoreReassignmentError,
skipDoubleRegistration,
ArgumentError('Type $T is already registered inside GetIt. '),
);

/// skip registration
if (ignoreReassignmentError && !allowReassignment) {
/// skip double registration
if (skipDoubleRegistration && !allowReassignment) {
return;
}
}
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ environment:
dependencies:
async: ^2.11.0
collection: ^1.17.1
meta: ^1.11.0

dev_dependencies:
lint: ^2.1.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ void main() {
test(' Throws ArgumentError ', () async {
final getIt = GetIt.instance;
getIt.allowReassignment = false;
getIt.ignoreReassignmentError = false;
getIt.skipDoubleRegistration = false;
getIt.reset();
getIt.registerSingleton<DataStore>(MockDataStore());

Expand All @@ -19,30 +19,30 @@ void main() {
final getIt = GetIt.instance;
getIt.reset();
getIt.allowReassignment = true;
getIt.ignoreReassignmentError = false;
getIt.skipDoubleRegistration = false;
getIt.registerSingleton<DataStore>(MockDataStore());
getIt.registerSingleton<DataStore>(RemoteDataStore());

expect(getIt<DataStore>(), isA<RemoteDataStore>());
});

test(' Ignores ReassignmentError ', () async {
test(' Ignores Double registration error ', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.allowReassignment = false;
getIt.ignoreReassignmentError = true;
getIt.skipDoubleRegistration = true;
getIt.registerSingleton<DataStore>(MockDataStore());
final remoteDataStore = RemoteDataStore();
getIt.registerSingleton<DataStore>(remoteDataStore);

expect(getIt<DataStore>(), isA<MockDataStore>());
});

test(' does not care about [ignoreReassignmentError] varibale ', () async {
test(' does not care about [skipDoubleRegistration] varibale ', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.allowReassignment = true;
getIt.ignoreReassignmentError = true;
getIt.skipDoubleRegistration = true;
getIt.registerSingleton<DataStore>(MockDataStore());
getIt.registerSingleton<DataStore>(RemoteDataStore());

Expand Down

0 comments on commit df3a12e

Please sign in to comment.