until<T extends Object> method
Retrieves a dependency of type T
or subtypes of T
registered under
the specified groupEntity
.
If the dependency is found, it is returned; otherwise, this method waits until the dependency is registered before returning it.
If traverse
is set to true
, the search will also include all parent
containers.
Implementation
FutureOr<T> until<T extends Object>({
Entity? groupEntity,
bool traverse = true,
}) {
final groupEntity1 = groupEntity ?? focusGroup;
// Check if the dependency is already registered.
final test = getOrNull<T>(groupEntity: groupEntity1);
if (test != null) {
// Return the dependency if it is already registered.
return test;
}
CompleterOr<FutureOr<T>>? completer;
completer = completers?.registry
.getDependencyOrNull<CompleterOr<FutureOr<T>>>(
groupEntity: groupEntity1,
)
?.value;
if (completer != null) {
return completer.futureOr.thenOr((value) => value);
}
completers ??= DIBase();
// If it's not already registered, register a Completer for the type
// inside the untilsContainer.
completer = CompleterOr<FutureOr<T>>();
completers!.registry.setDependency(
Dependency<CompleterOr<FutureOr<T>>>(
completer,
metadata: DependencyMetadata(
groupEntity: groupEntity1,
),
),
);
// Wait for the register function to complete the Completer, then unregister
// the completer before returning the value.
return completer.futureOr.thenOr((value) {
completers!.registry.removeDependency<CompleterOr<FutureOr<T>>>(
groupEntity: groupEntity1,
);
return get<T>(
groupEntity: groupEntity,
traverse: traverse,
);
});
}