Skip to content

Commit

Permalink
create a new instance even if dispose is not completed after resetLaz…
Browse files Browse the repository at this point in the history
…ySingleton
  • Loading branch information
Michał Dziwota committed Feb 15, 2023
1 parent 2ac359a commit a95a35f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1083,23 +1083,25 @@ class _GetItImplementation implements GetIt {
'There is no type ${instance.runtimeType} registered as LazySingleton in GetIt'),
);

Future<dynamic> _dispose = Future.value();
if (instanceFactory.instance != null) {
if (disposingFunction != null) {
final dispose = disposingFunction.call(instanceFactory.instance as T);
if (dispose is Future) {
await dispose;
_dispose = dispose;
}
} else {
final dispose = instanceFactory.dispose();
if (dispose is Future) {
await dispose;
_dispose = dispose;
}
}
}

instanceFactory.instance = null;
instanceFactory.pendingResult = null;
instanceFactory._readyCompleter = Completer<T>();
await _dispose;
}

List<_ServiceFactory> get _allFactories =>
Expand Down
22 changes: 22 additions & 0 deletions test/get_it_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ignore_for_file: unnecessary_type_check, avoid_redundant_argument_values, avoid_classes_with_only_static_members
import 'dart:async';

import 'package:get_it/get_it.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -529,6 +530,27 @@ void main() {
GetIt.I.reset();
});

test(
'create a new instance even if dispose is not completed after resetLazySingleton',
() {
// Arrange
final completer = Completer();
GetIt.I.registerLazySingleton<TestClass>(
() => TestClass(),
dispose: (_) => completer.future,
);
final instance1 = GetIt.I.get<TestClass>();

// Act
GetIt.I.resetLazySingleton(instance: instance1);

// Assert
final instance2 = GetIt.I.get<TestClass>();
expect(instance1, isNot(instance2));

completer.complete();
});

test('unregister by instance when the dispose of the register is a future',
() async {
final getIt = GetIt.instance;
Expand Down

0 comments on commit a95a35f

Please sign in to comment.