registerPrecondition method

Precondition registerPrecondition(
  1. PreconditionId id,
  2. PreconditionFunction preconditionFunction, {
  3. String? description,
  4. Iterable<_Dependency> dependsOn = const [],
  5. dynamic resolveTimeout = const Duration(seconds: 10),
  6. dynamic staySatisfiedCacheDuration = Duration.zero,
  7. dynamic stayFailedCacheDuration = Duration.zero,
  8. InitPreconditionFunction? initFunction,
})

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 (default is 10s)
  • 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<_Dependency> dependsOn = const [],
  resolveTimeout = const Duration(seconds: 10),
  staySatisfiedCacheDuration = Duration.zero,
  stayFailedCacheDuration = Duration.zero,
  InitPreconditionFunction? initFunction,
}) {
  for (var dId in dependsOn) {
    var dp = _known[dId._targetId];
    if (dp == null) throw Exception("Precondition '$id' depends on '$dId', which is not (yet?) registered");
    dId._target = dp;
  }
  if (_known.containsKey(id)) {
    throw Exception("Precondition '$id' is already registered");
  }
  var _p = Precondition._(
    id,
    preconditionFunction,
    Set.unmodifiable(dependsOn),
    this,
    description: description,
    staySatisfiedCacheDuration: staySatisfiedCacheDuration,
    stayFailedCacheDuration: stayFailedCacheDuration,
    initFunction: initFunction,
    resolveTimeout: resolveTimeout,
  );
  _known[id] = _p;
  _log.info("Registering $_p");
  notifyListeners();
  return _p;
}