generated from antfu/starter-ts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
idleQueue.ts
236 lines (201 loc) · 6.95 KB
/
idleQueue.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { cIC, rIC } from './idleCbWithPolyfill'
import { createQueueMicrotask } from './utils/queueMicrotask'
import { isBrowser, isSafari } from './utils/env'
import { now } from './utils/now'
interface State {
time: number
visibilityState: 'hidden' | 'visible' | 'prerender' | 'unloaded'
}
type Task = (state: State) => void
const DEFAULT_MIN_TASK_TIME: number = 0
const DEFAULT_MAX_TASKS_PER_ITERATION: number = 100
/**
* Returns true if the IdleDeadline object exists and the remaining time is
* less or equal to than the minTaskTime. Otherwise returns false.
*/
function shouldYield(deadline?: IdleDeadline, minTaskTime?: number): boolean {
// deadline.timeRemaining() means the time remaining till the browser is idle
return !!(deadline && deadline.timeRemaining() <= (minTaskTime || 0))
}
/**
* This class manages a queue of tasks designed to execute during idle browser time.
*
* It allows checking whether tasks are pending and ensures task execution even
* in situations like page unload.
*/
export class IdleQueue {
private idleCallbackHandle_: number | null = null
private taskQueue_: { state: State, task: Task, minTaskTime: number }[] = []
private isProcessing_: boolean = false
private state_: State | null = null
private defaultMinTaskTime_: number = DEFAULT_MIN_TASK_TIME
private maxTasksPerIteration_: number = DEFAULT_MAX_TASKS_PER_ITERATION
private ensureTasksRun_: boolean = false
private queueMicrotask?: (callback: VoidFunction) => void
/**
* Creates the IdleQueue instance and adds lifecycle event listeners to
* run the queue if the page is hidden (with fallback behavior for Safari).
*/
constructor({
ensureTasksRun = false,
defaultMinTaskTime = DEFAULT_MIN_TASK_TIME,
maxTasksPerIteration = DEFAULT_MAX_TASKS_PER_ITERATION,
}: { ensureTasksRun?: boolean, defaultMinTaskTime?: number, maxTasksPerIteration?: number } = {}) {
this.defaultMinTaskTime_ = defaultMinTaskTime
this.ensureTasksRun_ = ensureTasksRun
this.maxTasksPerIteration_ = maxTasksPerIteration
// bind methods
this.runTasksImmediately = this.runTasksImmediately.bind(this)
this.runTasks_ = this.runTasks_.bind(this)
if (isBrowser && this.ensureTasksRun_) {
addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden')
this.runTasksImmediately()
}, true)
if (isSafari) {
// Safari workaround: Due to unreliable event behavior, we use 'beforeunload'
// to ensure tasks run if a tab/window is closed unexpectedly.
// NOTE: we only add this to Safari because adding it to Firefox would
// prevent the page from being eligible for bfcache.
addEventListener('beforeunload', this.runTasksImmediately, true)
}
}
}
pushTask(task: Task, options?: { minTaskTime?: number }): void {
this.addTask_(task, options)
}
unshiftTask(task: Task, options?: { minTaskTime?: number }): void {
this.addTask_(task, options, true)
}
/**
* Runs all scheduled tasks synchronously.
*/
runTasksImmediately(): void {
// By not passing a deadline, all tasks will be run sync.
this.runTasks_()
}
hasPendingTasks(): boolean {
return this.taskQueue_.length > 0
}
/**
* Clears all pending tasks for the queue and stops any scheduled tasks
* from running.
*/
clearPendingTasks(): void {
this.taskQueue_ = []
this.cancelScheduledRun_()
}
/**
* Returns the state object for the currently running task. If no task is
* running, null is returned.
*/
getState(): State | null {
return this.state_
}
/**
* Destroys the instance by un-registering all added event listeners and
* removing any overridden methods.
*/
destroy(): void {
this.taskQueue_ = []
this.cancelScheduledRun_()
if (isBrowser && this.ensureTasksRun_) {
removeEventListener('visibilitychange', this.runTasksImmediately, true)
if (isSafari)
removeEventListener('beforeunload', this.runTasksImmediately, true)
}
}
private addTask_(
task: Task,
options?: { minTaskTime?: number },
unshift: boolean = false,
): void {
const state: State = {
time: now(),
visibilityState: isBrowser ? document.visibilityState : 'visible',
}
const minTaskTime: number = Math.max(
0,
(options && options.minTaskTime) || this.defaultMinTaskTime_,
)
const taskQueueItem = {
state,
task,
minTaskTime,
}
if (unshift)
this.taskQueue_.unshift(taskQueueItem)
else
this.taskQueue_.push(taskQueueItem)
this.scheduleTasksToRun_()
}
/**
* Schedules the task queue to be processed. If the document is in the
* hidden state, they queue is scheduled as a microtask so it can be run
* in cases where a macrotask couldn't (like if the page is unloading). If
* the document is in the visible state, `requestIdleCallback` is used.
*/
private scheduleTasksToRun_(): void {
if (
isBrowser
&& this.ensureTasksRun_
&& document.visibilityState === 'hidden'
) {
if (!this.queueMicrotask)
this.queueMicrotask = createQueueMicrotask()
this.queueMicrotask(this.runTasks_)
}
else {
if (!this.idleCallbackHandle_)
this.idleCallbackHandle_ = rIC(this.runTasks_) as number
}
}
/**
* Runs as many tasks in the queue as it can before reaching the
* deadline. If no deadline is passed, it will run all tasks.
* If an `IdleDeadline` object is passed (as is with `requestIdleCallback`)
* then the tasks are run until there's no time remaining, at which point
* we yield to input or other script and wait until the next idle time.
*/
private runTasks_(deadline?: IdleDeadline): void {
this.cancelScheduledRun_()
if (!this.isProcessing_) {
this.isProcessing_ = true
let tasksProcessed = 0
// Process tasks until there's no time left, and for fixed iterations so that the main thread is not kept blocked,
// and till we need to yield to input.
while (
this.hasPendingTasks()
&& tasksProcessed < this.maxTasksPerIteration_
&& !shouldYield(deadline, this.taskQueue_[0].minTaskTime)
) {
const taskQueueItem = this.taskQueue_.shift()
if (taskQueueItem) {
const { task, state } = taskQueueItem
this.state_ = state
try {
task(state)
}
catch (error) {
console.error('Error running IdleQueue Task: ', error)
}
this.state_ = null
tasksProcessed++
}
}
this.isProcessing_ = false
if (this.hasPendingTasks()) {
// Schedule the rest of the tasks for the next idle time.
this.scheduleTasksToRun_()
}
}
}
/**
* Cancels any scheduled idle callback and removes the handler (if set).
*/
private cancelScheduledRun_(): void {
if (this.idleCallbackHandle_)
cIC(this.idleCallbackHandle_)
this.idleCallbackHandle_ = null
}
}