Skip to content

Commit

Permalink
Merge pull request #361 from kasefuchs/master
Browse files Browse the repository at this point in the history
Implement getAllAsync
looks good, thanks
  • Loading branch information
escamoteur authored Apr 15, 2024
2 parents 1acf4b7 + f914fdb commit 6822965
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/get_it.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ abstract class GetIt {
Type? type,
});

Future<Iterable<T>> getAllAsync<T extends Object>({
dynamic param1,
dynamic param2,
Type? type,
});

/// Callable class so that you can write `GetIt.instance<MyType>` instead of
/// `GetIt.instance.get<MyType>`
T call<T extends Object>({
Expand Down
38 changes: 38 additions & 0 deletions lib/get_it_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,44 @@ class _GetItImplementation implements GetIt {
return factoryToGet.getObjectAsync<T>(param1, param2);
}

@override
Future<Iterable<T>> getAllAsync<T extends Object>({
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<T>? typeRegistration =
_currentScope.typeRegistrations[T] as _TypeRegistration<T>?;

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 = <T>[];
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
Expand Down
26 changes: 26 additions & 0 deletions test/async_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestBaseClass>(
() => TestClass2(internalCompletion: false, getIt: getIt),
);
getIt.registerLazySingletonAsync<TestBaseClass>(
() async => TestClass3(internalCompletion: false, getIt: getIt),
);

expect(constructorCounter, 0);

final Iterable<TestBaseClass> instances = await getIt.getAllAsync<TestBaseClass>();

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 {
Expand Down

0 comments on commit 6822965

Please sign in to comment.