Skip to content

Commit 8151a61

Browse files
authored
Merge pull request #243 from jtdLab/master
Reset a lazy Singleton by providing an instance. Thanks for catching that one!
2 parents 2651fdb + eb46e8a commit 8151a61

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

lib/get_it.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ abstract class GetIt {
408408
/// provide a [disposingFunction]. This function overrides the disposing
409409
/// you might have provided when registering.
410410
FutureOr resetLazySingleton<T extends Object>({
411-
Object? instance,
411+
T? instance,
412412
String? instanceName,
413413
FutureOr Function(T)? disposingFunction,
414414
});

lib/get_it_impl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ class _GetItImplementation implements GetIt {
10341034
/// provide a [disposingFunction]
10351035
@override
10361036
FutureOr resetLazySingleton<T extends Object>({
1037-
Object? instance,
1037+
T? instance,
10381038
String? instanceName,
10391039
FutureOr Function(T)? disposingFunction,
10401040
}) async {

test/get_it_test.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,24 @@ void main() {
509509
GetIt.I.reset();
510510
});
511511

512+
test('reset LazySingleton by instance only', () {
513+
// Arrange
514+
final getIt = GetIt.instance;
515+
constructorCounter = 0;
516+
getIt.registerLazySingleton<TestClass>(() => TestClass());
517+
final instance1 = getIt.get<TestClass>();
518+
519+
// Act
520+
GetIt.I.resetLazySingleton(instance: instance1);
521+
522+
// Assert
523+
final instance2 = getIt.get<TestClass>();
524+
expect(instance1, isNot(instance2));
525+
expect(constructorCounter, 2);
526+
527+
GetIt.I.reset();
528+
});
529+
512530
test('unregister by instance when the dispose of the register is a future',
513531
() async {
514532
final getIt = GetIt.instance;

0 commit comments

Comments
 (0)