| 1 |
/** |
| 2 |
* Copyright (c) 2022 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { isNode } from 'common/Platform'; |
| 7 |
|
| 8 |
interface ITaskQueue { |
| 9 |
/** |
| 10 |
* Adds a task to the queue which will run in a future idle callback. |
| 11 |
* To avoid perceivable stalls on the mainthread, tasks with heavy workload |
| 12 |
* should split their work into smaller pieces and return `true` to get |
| 13 |
* called again until the work is done (on falsy return value). |
| 14 |
*/ |
| 15 |
enqueue(task: () => boolean | void): void; |
| 16 |
|
| 17 |
/** |
| 18 |
* Flushes the queue, running all remaining tasks synchronously. |
| 19 |
*/ |
| 20 |
flush(): void; |
| 21 |
|
| 22 |
/** |
| 23 |
* Clears any remaining tasks from the queue, these will not be run. |
| 24 |
*/ |
| 25 |
clear(): void; |
| 26 |
} |
| 27 |
|
| 28 |
interface ITaskDeadline { |
| 29 |
timeRemaining(): number; |
| 30 |
} |
| 31 |
type CallbackWithDeadline = (deadline: ITaskDeadline) => void; |
| 32 |
|
| 33 |
abstract class TaskQueue implements ITaskQueue { |
| 34 |
private _tasks: (() => boolean | void)[] = []; |
| 35 |
private _idleCallback?: number; |
| 36 |
private _i = 0; |
| 37 |
|
| 38 |
protected abstract _requestCallback(callback: CallbackWithDeadline): number; |
| 39 |
protected abstract _cancelCallback(identifier: number): void; |
| 40 |
|
| 41 |
public enqueue(task: () => boolean | void): void { |
| 42 |
this._tasks.push(task); |
| 43 |
this._start(); |
| 44 |
} |
| 45 |
|
| 46 |
public flush(): void { |
| 47 |
while (this._i < this._tasks.length) { |
| 48 |
if (!this._tasks[this._i]()) { |
| 49 |
this._i++; |
| 50 |
} |
| 51 |
} |
| 52 |
this.clear(); |
| 53 |
} |
| 54 |
|
| 55 |
public clear(): void { |
| 56 |
if (this._idleCallback) { |
| 57 |
this._cancelCallback(this._idleCallback); |
| 58 |
this._idleCallback = undefined; |
| 59 |
} |
| 60 |
this._i = 0; |
| 61 |
this._tasks.length = 0; |
| 62 |
} |
| 63 |
|
| 64 |
private _start(): void { |
| 65 |
if (!this._idleCallback) { |
| 66 |
this._idleCallback = this._requestCallback(this._process.bind(this)); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
private _process(deadline: ITaskDeadline): void { |
| 71 |
this._idleCallback = undefined; |
| 72 |
let taskDuration = 0; |
| 73 |
let longestTask = 0; |
| 74 |
let lastDeadlineRemaining = deadline.timeRemaining(); |
| 75 |
let deadlineRemaining = 0; |
| 76 |
while (this._i < this._tasks.length) { |
| 77 |
taskDuration = performance.now(); |
| 78 |
if (!this._tasks[this._i]()) { |
| 79 |
this._i++; |
| 80 |
} |
| 81 |
// other than performance.now, performance.now might not be stable (changes on wall clock |
| 82 |
// changes), this is not an issue here as a clock change during a short running task is very |
| 83 |
// unlikely in case it still happened and leads to negative duration, simply assume 1 msec |
| 84 |
taskDuration = Math.max(1, performance.now() - taskDuration); |
| 85 |
longestTask = Math.max(taskDuration, longestTask); |
| 86 |
// Guess the following task will take a similar time to the longest task in this batch, allow |
| 87 |
// additional room to try avoid exceeding the deadline |
| 88 |
deadlineRemaining = deadline.timeRemaining(); |
| 89 |
if (longestTask * 1.5 > deadlineRemaining) { |
| 90 |
// Warn when the time exceeding the deadline is over 20ms, if this happens in practice the |
| 91 |
// task should be split into sub-tasks to ensure the UI remains responsive. |
| 92 |
if (lastDeadlineRemaining - taskDuration < -20) { |
| 93 |
console.warn(`task queue exceeded allotted deadline by ${Math.abs(Math.round(lastDeadlineRemaining - taskDuration))}ms`); |
| 94 |
} |
| 95 |
this._start(); |
| 96 |
return; |
| 97 |
} |
| 98 |
lastDeadlineRemaining = deadlineRemaining; |
| 99 |
} |
| 100 |
this.clear(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* A queue of that runs tasks over several tasks via setTimeout, trying to maintain above 60 frames |
| 106 |
* per second. The tasks will run in the order they are enqueued, but they will run some time later, |
| 107 |
* and care should be taken to ensure they're non-urgent and will not introduce race conditions. |
| 108 |
*/ |
| 109 |
export class PriorityTaskQueue extends TaskQueue { |
| 110 |
protected _requestCallback(callback: CallbackWithDeadline): number { |
| 111 |
return setTimeout(() => callback(this._createDeadline(16))); |
| 112 |
} |
| 113 |
|
| 114 |
protected _cancelCallback(identifier: number): void { |
| 115 |
clearTimeout(identifier); |
| 116 |
} |
| 117 |
|
| 118 |
private _createDeadline(duration: number): ITaskDeadline { |
| 119 |
const end = performance.now() + duration; |
| 120 |
return { |
| 121 |
timeRemaining: () => Math.max(0, end - performance.now()) |
| 122 |
}; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
class IdleTaskQueueInternal extends TaskQueue { |
| 127 |
protected _requestCallback(callback: IdleRequestCallback): number { |
| 128 |
return requestIdleCallback(callback); |
| 129 |
} |
| 130 |
|
| 131 |
protected _cancelCallback(identifier: number): void { |
| 132 |
cancelIdleCallback(identifier); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* A queue of that runs tasks over several idle callbacks, trying to respect the idle callback's |
| 138 |
* deadline given by the environment. The tasks will run in the order they are enqueued, but they |
| 139 |
* will run some time later, and care should be taken to ensure they're non-urgent and will not |
| 140 |
* introduce race conditions. |
| 141 |
* |
| 142 |
* This reverts to a {@link PriorityTaskQueue} if the environment does not support idle callbacks. |
| 143 |
*/ |
| 144 |
// eslint-disable-next-line @typescript-eslint/naming-convention |
| 145 |
export const IdleTaskQueue = (!isNode && 'requestIdleCallback' in window) ? IdleTaskQueueInternal : PriorityTaskQueue; |
| 146 |
|
| 147 |
/** |
| 148 |
* An object that tracks a single debounced task that will run on the next idle frame. When called |
| 149 |
* multiple times, only the last set task will run. |
| 150 |
*/ |
| 151 |
export class DebouncedIdleTask { |
| 152 |
private _queue: ITaskQueue; |
| 153 |
|
| 154 |
constructor() { |
| 155 |
this._queue = new IdleTaskQueue(); |
| 156 |
} |
| 157 |
|
| 158 |
public set(task: () => boolean | void): void { |
| 159 |
this._queue.clear(); |
| 160 |
this._queue.enqueue(task); |
| 161 |
} |
| 162 |
|
| 163 |
public flush(): void { |
| 164 |
this._queue.flush(); |
| 165 |
} |
| 166 |
} |