Merge pull request #4143 from Tyriar/idle_deadline
Use the browser's IdleDeadline to determine max task time
This commit is contained in:
+16
-17
@@ -11,16 +11,8 @@
|
|||||||
export class IdleTaskQueue {
|
export class IdleTaskQueue {
|
||||||
private _tasks: (() => void)[] = [];
|
private _tasks: (() => void)[] = [];
|
||||||
private _idleCallback?: number;
|
private _idleCallback?: number;
|
||||||
private _maxTaskDuration: number;
|
|
||||||
private _i = 0;
|
private _i = 0;
|
||||||
|
|
||||||
/**
|
|
||||||
* @param targetFps The target frame rate.
|
|
||||||
*/
|
|
||||||
constructor(targetFps: number = 240) {
|
|
||||||
this._maxTaskDuration = 1000 / targetFps;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a task to the queue which will run in a future idle callback.
|
* Adds a task to the queue which will run in a future idle callback.
|
||||||
*/
|
*/
|
||||||
@@ -53,16 +45,22 @@ export class IdleTaskQueue {
|
|||||||
|
|
||||||
private _start(): void {
|
private _start(): void {
|
||||||
if (!this._idleCallback) {
|
if (!this._idleCallback) {
|
||||||
this._idleCallback = requestIdleCallback(() => this._process());
|
this._idleCallback = requestIdleCallback(this._process.bind(this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _process(): void {
|
private _process(deadline: IdleDeadline): void {
|
||||||
const start = performance.now();
|
|
||||||
this._idleCallback = undefined;
|
this._idleCallback = undefined;
|
||||||
|
let taskDuration = 0;
|
||||||
|
let longestTask = 0;
|
||||||
while (this._i < this._tasks.length) {
|
while (this._i < this._tasks.length) {
|
||||||
|
taskDuration = performance.now();
|
||||||
this._tasks[this._i++]();
|
this._tasks[this._i++]();
|
||||||
if (performance.now() - start > this._maxTaskDuration) {
|
taskDuration = performance.now() - taskDuration;
|
||||||
|
longestTask = Math.max(taskDuration, longestTask);
|
||||||
|
// Guess the following task will take a similar time to the longest task in this batch, allow
|
||||||
|
// additional room to try avoid exceeding the deadline
|
||||||
|
if (longestTask * 1.5 > deadline.timeRemaining()) {
|
||||||
this._start();
|
this._start();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -71,14 +69,15 @@ export class IdleTaskQueue {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An object that tracks a single debounced task that will run on the next idle frame. When called
|
||||||
|
* multiple times, only the last set task will run.
|
||||||
|
*/
|
||||||
export class DebouncedIdleTask {
|
export class DebouncedIdleTask {
|
||||||
private _queue: IdleTaskQueue;
|
private _queue: IdleTaskQueue;
|
||||||
|
|
||||||
/**
|
constructor() {
|
||||||
* @param targetFps The target frame rate.
|
this._queue = new IdleTaskQueue();
|
||||||
*/
|
|
||||||
constructor(targetFps: number = 240) {
|
|
||||||
this._queue = new IdleTaskQueue(targetFps);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public set(task: () => void): void {
|
public set(task: () => void): void {
|
||||||
|
|||||||
Reference in New Issue
Block a user