Skip to content

Add support to reset lazy singleton instance #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,58 @@ class GetIt {
_factoriesByName.clear();
}

/// Clears the instance of a lazy singleton registered type, being able to call the factory function on the first call of [get] on that type.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean on the next call of [get]

void resetLazySingleton<T>({Object instance,
String instanceName,
void Function(T) disposingFunction}) {
if (instance != null) {
var registeredInstance = _factories.values
.followedBy(_factoriesByName.values)
.where((x) => identical(x.instance, instance));

throwIf(
registeredInstance.isEmpty,
ArgumentError.value(instance,
'There is no object type ${instance.runtimeType} registered in GetIt'),
);

throwIf(
registeredInstance.first.factoryType != _ServiceFactoryType.lazy,
ArgumentError.value(instance,
'There is no type ${instance.runtimeType} registered as LazySingleton in GetIt'),
);

var _factory = registeredInstance.first;
disposingFunction?.call(_factory.instance);
_factory.instance = null;
} else {
throwIfNot(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use throwIf and remove the ! I did the same just now on the unregister function

!(((const Object() is! T) && instanceName != null)),
ArgumentError(
'GetIt: You have to provide either a type OR a name not both.'),
);
throwIfNot(
(instanceName != null && _factoriesByName.containsKey(instanceName)) ||
_factories.containsKey(T),
ArgumentError(
'No Type registered ${T.toString()} or instance Name must not be null'),
);
throwIfNot(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to check this

(instanceName != null && _factoriesByName.containsKey(instanceName) && _factoriesByName[instanceName].factoryType == _ServiceFactoryType.lazy) ||
(_factories.containsKey(T) && _factories[T].factoryType == _ServiceFactoryType.lazy),
ArgumentError.value(instance,
'There is no type ${T.toString()} registered as LazySingleton in GetIt'),
);
if (instanceName == null) {
disposingFunction?.call(get<T>());
_factories[T]?.instance = null;
} else {
disposingFunction?.call(get(instanceName));
_factoriesByName[T]?.instance = null;
}
}
}

void _register<T>(
{@required _ServiceFactoryType type,
FactoryFunc factoryFunc,
Expand Down
31 changes: 31 additions & 0 deletions test/get_it_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,37 @@ void main() {
GetIt.I.reset();
});

test('reset lazySingleton', () {
var getIt = GetIt.instance;
constructorCounter = 0;
getIt.registerLazySingleton<TestBaseClass>(() => TestClass());

expect(constructorCounter, 0);

var instance1 = getIt.get<TestBaseClass>();

expect(instance1 is TestClass, true);
expect(constructorCounter, 1);

var instance2 = getIt.get<TestBaseClass>();

expect(instance1, instance2);

expect(constructorCounter, 1);

GetIt.I.resetLazySingleton<TestBaseClass>();

var instance3 = getIt.get<TestBaseClass>();

expect(instance3 is TestClass, true);

expect(instance1, isNot(instance3));

expect(constructorCounter, 2);

GetIt.I.reset();
});

test('unregister by instance', () {
var getIt = GetIt.instance;
disposeCounter = 0;
Expand Down