Skip to content
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

Implement getAllAsync #361

Merged
merged 1 commit into from
Apr 15, 2024
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
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
Loading