Prevent exception being thrown on hasScrolledBody (#31485)
Convert scheduling to use `scheduleMicrotask` instead of `Timer.run`
This commit is contained in:
@@ -763,34 +763,38 @@ mixin SchedulerBinding on BindingBase, ServicesBinding {
|
||||
_warmUpFrame = true;
|
||||
Timeline.startSync('Warm-up frame');
|
||||
final bool hadScheduledFrame = _hasScheduledFrame;
|
||||
// We use timers here to ensure that microtasks flush in between.
|
||||
Timer.run(() {
|
||||
assert(_warmUpFrame);
|
||||
handleBeginFrame(null);
|
||||
});
|
||||
Timer.run(() {
|
||||
assert(_warmUpFrame);
|
||||
handleDrawFrame();
|
||||
// We call resetEpoch after this frame so that, in the hot reload case,
|
||||
// the very next frame pretends to have occurred immediately after this
|
||||
// warm-up frame. The warm-up frame's timestamp will typically be far in
|
||||
// the past (the time of the last real frame), so if we didn't reset the
|
||||
// epoch we would see a sudden jump from the old time in the warm-up frame
|
||||
// to the new time in the "real" frame. The biggest problem with this is
|
||||
// that implicit animations end up being triggered at the old time and
|
||||
// then skipping every frame and finishing in the new time.
|
||||
resetEpoch();
|
||||
_warmUpFrame = false;
|
||||
if (hadScheduledFrame)
|
||||
scheduleFrame();
|
||||
});
|
||||
final Completer<void> _warmUpFrameCompleter = Completer<void>();
|
||||
|
||||
// Lock events so touch events etc don't insert themselves until the
|
||||
// scheduled frame has finished.
|
||||
lockEvents(() async {
|
||||
await endOfFrame;
|
||||
await _warmUpFrameCompleter.future;
|
||||
Timeline.finishSync();
|
||||
});
|
||||
|
||||
// We use scheduleMicrotask here to ensure that microtasks flush in between.
|
||||
scheduleMicrotask(() {
|
||||
assert(_warmUpFrame);
|
||||
handleBeginFrame(null);
|
||||
|
||||
scheduleMicrotask(() {
|
||||
assert(_warmUpFrame);
|
||||
handleDrawFrame();
|
||||
// We call resetEpoch after this frame so that, in the hot reload case,
|
||||
// the very next frame pretends to have occurred immediately after this
|
||||
// warm-up frame. The warm-up frame's timestamp will typically be far in
|
||||
// the past (the time of the last real frame), so if we didn't reset the
|
||||
// epoch we would see a sudden jump from the old time in the warm-up frame
|
||||
// to the new time in the "real" frame. The biggest problem with this is
|
||||
// that implicit animations end up being triggered at the old time and
|
||||
// then skipping every frame and finishing in the new time.
|
||||
resetEpoch();
|
||||
_warmUpFrame = false;
|
||||
_warmUpFrameCompleter.complete();
|
||||
if (hadScheduledFrame)
|
||||
scheduleFrame();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Duration _firstRawTimeStampInEpoch;
|
||||
|
||||
@@ -19,6 +19,26 @@ class TestStrategy {
|
||||
}
|
||||
}
|
||||
|
||||
List<VoidCallback> runWithMicrotaskQueueSpy(VoidCallback callback) {
|
||||
final List<VoidCallback> microtaskQueue = <VoidCallback>[];
|
||||
runZoned<void>(
|
||||
() {
|
||||
callback();
|
||||
},
|
||||
zoneSpecification: ZoneSpecification(
|
||||
scheduleMicrotask: (Zone self, ZoneDelegate parent, Zone zone, void f()) {
|
||||
// Don't actually run the tasks, just record that it was scheduled.
|
||||
microtaskQueue.add(f);
|
||||
self.parent.scheduleMicrotask(() {
|
||||
f();
|
||||
microtaskQueue.remove(f);
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
return microtaskQueue;
|
||||
}
|
||||
|
||||
void main() {
|
||||
SchedulerBinding scheduler;
|
||||
setUpAll(() {
|
||||
@@ -92,27 +112,27 @@ void main() {
|
||||
});
|
||||
|
||||
test('2 calls to scheduleWarmUpFrame just schedules it once', () {
|
||||
final List<VoidCallback> timerQueueTasks = <VoidCallback>[];
|
||||
bool taskExecuted = false;
|
||||
runZoned<void>(
|
||||
() {
|
||||
// Run it twice without processing the queued tasks.
|
||||
scheduler.scheduleWarmUpFrame();
|
||||
scheduler.scheduleWarmUpFrame();
|
||||
scheduler.scheduleTask(() { taskExecuted = true; }, Priority.touch);
|
||||
},
|
||||
zoneSpecification: ZoneSpecification(
|
||||
createTimer: (Zone self, ZoneDelegate parent, Zone zone, Duration duration, void f()) {
|
||||
// Don't actually run the tasks, just record that it was scheduled.
|
||||
timerQueueTasks.add(f);
|
||||
return null;
|
||||
},
|
||||
),
|
||||
);
|
||||
final List<VoidCallback> microtaskQueue = runWithMicrotaskQueueSpy(() {
|
||||
scheduler.scheduleWarmUpFrame();
|
||||
scheduler.scheduleWarmUpFrame();
|
||||
});
|
||||
|
||||
// scheduleWarmUpFrame scheduled 2 Timers, scheduleTask scheduled 0 because
|
||||
// events are locked.
|
||||
expect(timerQueueTasks.length, 2);
|
||||
// scheduleWarmUpFrame scheduled 1 microtask
|
||||
expect(microtaskQueue.length, 1);
|
||||
});
|
||||
|
||||
test('Tasks are not executed before scheduleWarmUpFrame finishes', () async {
|
||||
bool taskExecuted = false;
|
||||
final List<VoidCallback> microtaskQueue = runWithMicrotaskQueueSpy(() {
|
||||
scheduler.scheduleTask(() { taskExecuted = true; }, Priority.touch);
|
||||
Timer.run(() { taskExecuted = true; });
|
||||
scheduler.scheduleWarmUpFrame();
|
||||
});
|
||||
|
||||
expect(microtaskQueue.isNotEmpty, true);
|
||||
await scheduler.endOfFrame;
|
||||
|
||||
expect(scheduler.schedulerPhase, SchedulerPhase.idle);
|
||||
expect(taskExecuted, false);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user