registerPrecondition method Null safety

Precondition registerPrecondition(
  1. PreconditionId id,
  2. PreconditionFunction preconditionFunction,
  3. {String? description,
  4. Iterable<PreconditionId> dependsOn = const [],
  5. dynamic resolveTimeout = const Duration(seconds: 10),
  6. dynamic satisfiedCache = Duration.zero,
  7. dynamic notSatisfiedCache = Duration.zero,
  8. StatusBuilder? statusBuilder,
  9. DependenciesStrategy? dependenciesStrategy}
)

Use this method to register your PreconditionFunction with given unique id.

Optionally:

  • provide statusBuilder which will be later used to render feedback to the user (see Precondition.build)
  • limit evaluation duration with resolveTimeout, after which the precondition is evaluated as PreconditionStatus.isFailed
  • allow precondition to cache its positive and/or negative result with satisfiedCache and notSatisfiedCache.
  • specify dependsOn - set of preconditions which must be satisfied before the repository attempts to evaluate this one

Implementation

Precondition registerPrecondition(PreconditionId id, PreconditionFunction preconditionFunction,
    {String? description,
    Iterable<PreconditionId> dependsOn: const [],
    resolveTimeout: const Duration(seconds: 10),
    satisfiedCache: Duration.zero,
    notSatisfiedCache: Duration.zero,
    StatusBuilder? statusBuilder,
    DependenciesStrategy? dependenciesStrategy}) {
  for (var dId in dependsOn) {
    if (_known[dId] == null) throw Exception("Precondition '$id' depends on '$dId', which is not registered");
  }
  if (_known.containsKey(id)) {
    throw Exception("Precondition '$id' is already registered");
  }
  var _p = Precondition._(
    id,
    preconditionFunction,
    statusBuilder ?? _nullBuilder,
    Set.unmodifiable(dependsOn),
    this,
    description: description,
    satisfiedCache: satisfiedCache,
    notSatisfiedCache: notSatisfiedCache,
    resolveTimeout: resolveTimeout,
    dependenciesStrategy: dependenciesStrategy,
  );
  _known[id] = _p;
  _log.info("Registering $_p");
  notifyListeners();
  return _p;
}