signalReady method

void signalReady(
  1. [Object instance]
)

Implementation

void signalReady([Object instance]) {
  if (instance != null) {
    var registeredInstance = _factories.values
        .followedBy(_factoriesByName.values)
        .where((x) => identical(x.instance, instance));
    throwIf(
        registeredInstance.length > 1,
        StateError(
            'This objects instance of type ${instance.runtimeType} are registered multiple times in GetIt'));

    throwIf(
        registeredInstance.isEmpty,
        ArgumentError.value(instance,
            'There is no object type ${instance.runtimeType} registered in GetIt'));

    throwIfNot(
        registeredInstance.first.shouldSignalReady,
        ArgumentError.value(instance,
            'This instance of type ${instance.runtimeType} is not supposed to be signalled'));

    throwIf(
        registeredInstance.first.isReady,
        StateError(
            'This instance of type ${instance.runtimeType} was already signalled'));

    registeredInstance.first.isReady = true;

    /// if all registered instances that should signal ready are ready signal the [ready] and [readyFuture]
    var shouldSignalButNotReady = _factories.values
        .followedBy(_factoriesByName.values)
        .where((x) => x.shouldSignalReady && !x.isReady);
    if (shouldSignalButNotReady.isEmpty) {
      _readySignalStream.add(true);
    }
  } else {
    /// Manual signalReady without an instance

    /// In case that there are still factories that are marked to wait for a signal
    /// but aren't signalled we throw an error with details which objects are concerned
    final notReadyTypes = _factories.entries
        .where((x) => (x.value.shouldSignalReady && !x.value.isReady))
        .map<String>((x) => x.key.toString())
        .toList();
    final notReadyNames = _factoriesByName.entries
        .where((x) => (x.value.shouldSignalReady && !x.value.isReady))
        .map<String>((x) => x.key)
        .toList();
    throwIf(
        notReadyNames.isNotEmpty || notReadyTypes.isNotEmpty,
        StateError(
            'Registered types/names: $notReadyTypes  / $notReadyNames should signal ready but are not ready'));

    ///    signal the [ready] and [readyFuture]
    _readySignalStream.add(true);
  }
}