Unverified Commit db35611e authored by Jia Hao's avatar Jia Hao Committed by GitHub

Schedule tasks which are futures to completion (#112269)

parent 98c04987
...@@ -50,7 +50,7 @@ typedef FrameCallback = void Function(Duration timeStamp); ...@@ -50,7 +50,7 @@ typedef FrameCallback = void Function(Duration timeStamp);
/// ///
/// The type argument `T` is the task's return value. Consider `void` if the /// The type argument `T` is the task's return value. Consider `void` if the
/// task does not return a value. /// task does not return a value.
typedef TaskCallback<T> = T Function(); typedef TaskCallback<T> = FutureOr<T> Function();
/// Signature for the [SchedulerBinding.schedulingStrategy] callback. Called /// Signature for the [SchedulerBinding.schedulingStrategy] callback. Called
/// whenever the system needs to decide whether a task at a given /// whenever the system needs to decide whether a task at a given
...@@ -409,8 +409,12 @@ mixin SchedulerBinding on BindingBase { ...@@ -409,8 +409,12 @@ mixin SchedulerBinding on BindingBase {
} }
final PriorityQueue<_TaskEntry<dynamic>> _taskQueue = HeapPriorityQueue<_TaskEntry<dynamic>>(_taskSorter); final PriorityQueue<_TaskEntry<dynamic>> _taskQueue = HeapPriorityQueue<_TaskEntry<dynamic>>(_taskSorter);
/// Schedules the given `task` with the given `priority` and returns a /// Schedules the given `task` with the given `priority`.
/// [Future] that completes to the `task`'s eventual return value. ///
/// If `task` returns a future, the future returned by [scheduleTask] will
/// complete after the former future has been scheduled to completion.
/// Otherwise, the returned future for [scheduleTask] will complete with the
/// same value returned by `task` after it has been scheduled.
/// ///
/// The `debugLabel` and `flow` are used to report the task to the [Timeline], /// The `debugLabel` and `flow` are used to report the task to the [Timeline],
/// for use when profiling. /// for use when profiling.
......
...@@ -250,6 +250,31 @@ void main() { ...@@ -250,6 +250,31 @@ void main() {
warmUpDrawFrame(); warmUpDrawFrame();
expect(scheduler.hasScheduledFrame, isTrue); expect(scheduler.hasScheduledFrame, isTrue);
}); });
test('Can schedule futures to completion', () async {
bool isCompleted = false;
// `Future` is disallowed in this file due to the import of
// scheduler_tester.dart so annotations cannot be specified.
// ignore: always_specify_types
final result = scheduler.scheduleTask(
() async {
// Yield, so if awaiting `result` did not wait for completion of this
// task, the assertion on `isCompleted` will fail.
await null;
await null;
isCompleted = true;
return 1;
},
Priority.idle,
);
scheduler.handleEventLoopCallback();
await result;
expect(isCompleted, true);
});
} }
class DummyTimer implements Timer { class DummyTimer implements Timer {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment