Skip to content

Commit ac77bd4

Browse files
authored
clock_control: break loops when conditions may change after waiting for microtasks (#9075)
1 parent 631605c commit ac77bd4

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

app/lib/task/clock_control.dart

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,22 @@ final class ClockController {
305305
Duration? minimumStep,
306306
}) async {
307307
final deadline = timeout != null ? clock.fromNowBy(timeout) : null;
308-
while (_pendingTimers.isNotEmpty &&
308+
309+
bool shouldLoop() =>
310+
_pendingTimers.isNotEmpty &&
309311
(deadline == null ||
310-
_pendingTimers.first._elapsesAtInFakeTime.isBefore(deadline))) {
312+
_pendingTimers.first._elapsesAtInFakeTime.isBefore(deadline));
313+
314+
while (shouldLoop()) {
311315
if (await condition()) {
312316
return;
313317
}
314318

315319
// Wait for all microtasks to run
316320
await _waitForMicroTasks();
321+
if (!shouldLoop()) {
322+
break;
323+
}
317324

318325
// Jump into the future, until the point in time that the next timer is
319326
// pending.
@@ -371,10 +378,15 @@ final class ClockController {
371378
/// moving backwards in time. That allows [elapseTime] to be called with
372379
/// a zero duration.
373380
Future<void> _elapseTo(DateTime futureTime) async {
374-
while (_pendingTimers.isNotEmpty &&
375-
_pendingTimers.first._elapsesAtInFakeTime.isBefore(futureTime)) {
381+
bool shouldLoop() =>
382+
_pendingTimers.isNotEmpty &&
383+
_pendingTimers.first._elapsesAtInFakeTime.isBefore(futureTime);
384+
while (shouldLoop()) {
376385
// Wait for all microtasks to run
377386
await _waitForMicroTasks();
387+
if (!shouldLoop()) {
388+
break;
389+
}
378390

379391
// Jump into the future, until the point in time that the next timer is
380392
// pending.

0 commit comments

Comments
 (0)