Skip to content

Commit

Permalink
V7.6.8
Browse files Browse the repository at this point in the history
  • Loading branch information
escamoteur committed Apr 3, 2024
1 parent 29ab627 commit fc5eee7
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## [7.6.7] - 18.01.204
## [7.6.8] - 03.04.2024
* merged PR by @venkata-reddy-dev https://github.com/fluttercommunity/get_it/pull/356 adding new `skipDoubleRegistration` flag for testing

## [7.6.7] - 18.01.2024

* merged PR by @subzero911 https://github.com/fluttercommunity/get_it/pull/330

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ You have to pass a factory function `func` that returns an instance of an implem
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`.

### Skip Double registrations
### Skip Double registrations while testing

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`.
This is only available inside tests where is can be handy.

### Testing if a Singleton is already registered

Expand Down
5 changes: 4 additions & 1 deletion lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,10 @@ abstract class GetIt {
/// 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
/// and use `getAll()` to retrieve all instances of that parent type
static bool allowRegisterMultipleImplementationsOfoneType = false;
void enableRegisteringMultipleInstancesOfOneType();

@visibleForTesting
bool allowRegisterMultipleImplementationsOfoneType = false;

/// retrieves or creates an instance of a registered type [T] depending on the registration
/// function used for this type or based on a name.
Expand Down
11 changes: 9 additions & 2 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,13 @@ class _GetItImplementation implements GetIt {
@visibleForTesting
@override
bool skipDoubleRegistration = false;
@override
void enableRegisteringMultipleInstancesOfOneType() {
allowRegisterMultipleImplementationsOfoneType = true;
}

@override
bool allowRegisterMultipleImplementationsOfoneType = false;

/// Is used by several other functions to retrieve the correct [_ServiceFactory]
_ServiceFactory<T, dynamic, dynamic>?
Expand Down Expand Up @@ -1049,7 +1056,7 @@ class _GetItImplementation implements GetIt {
if (existingTypeRegistration.factories.isNotEmpty) {
throwIfNot(
allowReassignment ||
GetIt.allowRegisterMultipleImplementationsOfoneType ||
allowRegisterMultipleImplementationsOfoneType ||
skipDoubleRegistration,
ArgumentError('Type $T is already registered inside GetIt. '),
);
Expand Down Expand Up @@ -1096,7 +1103,7 @@ class _GetItImplementation implements GetIt {
if (instanceName != null) {
typeRegistration.namedFactories[instanceName] = serviceFactory;
} else {
if (GetIt.allowRegisterMultipleImplementationsOfoneType) {
if (allowRegisterMultipleImplementationsOfoneType) {
typeRegistration.factories.add(serviceFactory);
} else {
if (typeRegistration.factories.isNotEmpty) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ packages:
source: hosted
version: "0.12.16+1"
meta:
dependency: transitive
dependency: "direct main"
description:
name: meta
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
Expand Down
6 changes: 4 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: get_it
description: Simple direct Service Locator that allows to decouple the interface from a concrete implementation and to access the concrete implementation from everywhere in your App"
version: 7.6.7+1
version: 7.6.8
maintainer: Thomas Burkhart (@escamoteur)
homepage: https://github.com/fluttercommunity/get_it

funding:
- https://github.com/sponsors/escamoteur/

environment:
sdk: '>=3.0.0 <4.0.0'

Expand Down
4 changes: 2 additions & 2 deletions test/get_it_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void main() {

test('get all registered instances of the same type', () {
final getIt = GetIt.instance;
GetIt.allowRegisterMultipleImplementationsOfoneType = true;
getIt.enableRegisteringMultipleInstancesOfOneType();
constructorCounter = 0;

getIt.registerLazySingleton<TestBaseClass>(
Expand All @@ -424,7 +424,7 @@ void main() {
expect(constructorCounter, 2);

GetIt.I.reset();
GetIt.allowRegisterMultipleImplementationsOfoneType = false;
getIt.allowRegisterMultipleImplementationsOfoneType = false;
});

test('reset lazy Singleton when the disposing function is a future',
Expand Down

0 comments on commit fc5eee7

Please sign in to comment.