From f914fdb55b11fe562da6f28075643ed30c2711c8 Mon Sep 17 00:00:00 2001 From: Kasefuchs Date: Sat, 13 Apr 2024 12:21:40 +0300 Subject: [PATCH] Implement getAllAsync --- lib/get_it.dart | 6 ++++++ lib/get_it_impl.dart | 38 ++++++++++++++++++++++++++++++++++++++ test/async_test.dart | 26 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/lib/get_it.dart b/lib/get_it.dart index 32b61d6..f07ba01 100644 --- a/lib/get_it.dart +++ b/lib/get_it.dart @@ -197,6 +197,12 @@ abstract class GetIt { Type? type, }); + Future> getAllAsync({ + dynamic param1, + dynamic param2, + Type? type, + }); + /// Callable class so that you can write `GetIt.instance` instead of /// `GetIt.instance.get` T call({ diff --git a/lib/get_it_impl.dart b/lib/get_it_impl.dart index beb7406..31c9ac5 100644 --- a/lib/get_it_impl.dart +++ b/lib/get_it_impl.dart @@ -578,6 +578,44 @@ class _GetItImplementation implements GetIt { return factoryToGet.getObjectAsync(param1, param2); } + @override + Future> getAllAsync({ + dynamic param1, + dynamic param2, + Type? type, + }) async { + assert( + type == null || type is T, + 'The type you passed is not a $T. This can happen ' + 'if the receiving variable is of the wrong type, or you passed a generic type and a type parameter'); + final _TypeRegistration? typeRegistration = + _currentScope.typeRegistrations[T] as _TypeRegistration?; + + throwIf( + typeRegistration == null, + StateError('GetIt: No Objects/factories with ' + 'type $T are not registered inside GetIt. ' + '\n(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;' + '\nDid you forget to register it?)'), + ); + + final factories = [ + ...typeRegistration!.factories, + ...typeRegistration.namedFactories.values + ]; + final instances = []; + for (final instanceFactory in factories) { + final Object instance; + if (instanceFactory.isAsync || instanceFactory.pendingResult != null) { + instance = await instanceFactory.getObjectAsync(param1, param2); + } else { + instance = instanceFactory.getObject(param1, param2); + } + instances.add(instance as T); + } + return instances; + } + /// registers a type so that a new instance will be created on each call of [get] on that type /// [T] type to register /// [factoryFunc] factory function for this type diff --git a/test/async_test.dart b/test/async_test.dart index 7c37fce..2d35d94 100644 --- a/test/async_test.dart +++ b/test/async_test.dart @@ -165,6 +165,32 @@ void main() { ); }); + test('get all registered instances of the same type', () async { + final getIt = GetIt.instance; + getIt.enableRegisteringMultipleInstancesOfOneType(); + constructorCounter = 0; + + getIt.registerLazySingleton( + () => TestClass2(internalCompletion: false, getIt: getIt), + ); + getIt.registerLazySingletonAsync( + () async => TestClass3(internalCompletion: false, getIt: getIt), + ); + + expect(constructorCounter, 0); + + final Iterable instances = await getIt.getAllAsync(); + + expect(instances.length, 2); + expect(instances.first is TestClass, true); + expect(instances.last is TestClass, true); + expect(constructorCounter, 2); + + GetIt.I.reset(); + getIt.allowRegisterMultipleImplementationsOfoneType = false; + }); + + test( 'signalReady will throw if any Singletons that has signalsReady==true ' 'have not signaled completion', () async {