FakeTimeNgZoneStabilizer constructor

FakeTimeNgZoneStabilizer(
  1. TimerHookZone timerZone,
  2. NgZone ngZone, {
  3. int? maxIterations,
})

Creates a new stabilizer which uses a combination of zones.

Optionally, specify the maxIterations that will be attempted to elapse all pending timers before giving up and throwing "will never complete". In most cases, the default value defaultMaxIterations is appropriate but it may be increased in code that has heavy usage of repetitive timers.

Implementation

factory FakeTimeNgZoneStabilizer(
  TimerHookZone timerZone,
  NgZone ngZone, {
  int? maxIterations,
}) {
  // All non-periodic timers that have been started, but not completed.
  final pendingTimers = PriorityQueue<_FakeTimer>();

  // The parent zone that adds hooks around every non-periodic timer.
  late final FakeTimeNgZoneStabilizer stabilizer;

  timerZone.createTimer = (self, parent, zone, duration, callback) {
    _FakeTimer instance;
    instance = _FakeTimer(
      (_) => zone.run(callback),
      pendingTimers.remove,
      duration,
      stabilizer._lastElapse + duration,
      isPeriodic: false,
    );
    pendingTimers.add(instance);
    return instance;
  };

  timerZone.createPeriodicTimer = (self, parent, zone, duration, callback) {
    _FakeTimer instance;
    instance = _FakeTimer(
      (timer) => zone.run(() => callback(timer)),
      pendingTimers.remove,
      duration,
      stabilizer._lastElapse + duration,
      isPeriodic: true,
    );
    pendingTimers.add(instance);
    return instance;
  };

  return stabilizer = FakeTimeNgZoneStabilizer._(
    ngZone,
    pendingTimers,
    maxIterations: maxIterations,
  );
}