advance method

void advance(
  1. Duration d
)

推进非负时长,依次触发窗口内的计时器,包含回调中新建的计时器。 同时到期按创建顺序触发;负时长抛 ArgumentError,嵌套推进抛 StateError。 回调抛错会向调用方传播,当前时间停在该计时器的到期时刻。

Implementation

void advance(Duration d) {
  if (d.isNegative) throw ArgumentError.value(d, 'd', '不能倒退时间');
  if (_advancing) throw StateError('不能嵌套推进时钟');
  _advancing = true;
  final end = _now.add(d);
  try {
    while (true) {
      _FakeTimer? next;
      for (final timer in _timers) {
        if (timer.deadline.isAfter(end)) continue;
        if (next == null || timer.deadline.isBefore(next.deadline)) {
          next = timer;
        }
      }
      if (next == null) break;
      _now = next.deadline;
      _timers.remove(next);
      next._active = false;
      next._tick = 1;
      next.callback();
    }
    _now = end;
  } finally {
    _advancing = false;
  }
}