predict method

Future predict(
  1. double amount, {
  2. Duration? step,
})

Increments the value by a certain amount over a duration using an exponential backoff. Helps prevent large pauses while operations are / warming up.

Implementation

Future predict(double amount, {Duration? step}) async {
  // We don't allow multiple pads to run
  print("Predicting $amount (target of ${_actual + amount})");
  double progress = amount;
  _padded = _actual;
  final target = _actual + amount;
  for (var i = 1; i < 40; i++) {
    if (_actual >= target) {
      log.info("Reached target: $_actual $target -> We had $progress left");
      break;
    }
    if (progress <= 1) {
      log.fine(
          "Progress is less than 1: $progress towards our goal of $target");
      break;
    }

    final p = math.min(3, math.max(0.3, progress / i));
    if (p < 0) {
      log.fine(
          "Reached the negatives? $p towards our progress $progress goal of $target");
      break;
    }
    progress -= p;
    if (progress > 0) {
      log.fine("Predict increment by $p (target $target)");
      _padded += p;
      _sync();
    }
    await Future.delayed(step ?? 100.ms);
  }
}