-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
213 lines (188 loc) · 4.82 KB
/
index.js
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
(function (factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
window.idleCallbackShim = factory();
}
}(function(){
'use strict';
var scheduleStart, throttleDelay, lazytimer, lazyraf;
var root = typeof window != 'undefined' ?
window :
typeof global != undefined ?
global :
this || {};
var requestAnimationFrame = root.cancelRequestAnimationFrame && root.requestAnimationFrame || setTimeout;
var cancelRequestAnimationFrame = root.cancelRequestAnimationFrame || clearTimeout;
var tasks = [];
var runAttempts = 0;
var isRunning = false;
var remainingTime = 7;
var minThrottle = 35;
var throttle = 125;
var index = 0;
var taskStart = 0;
var tasklength = 0;
var IdleDeadline = {
get didTimeout(){
return false;
},
timeRemaining: function(){
var timeRemaining = remainingTime - (Date.now() - taskStart);
return Math.max(0, timeRemaining)
},
};
var setInactive = debounce(function(){
remainingTime = 22;
throttle = 66;
minThrottle = 0;
});
function debounce(fn){
var id, timestamp;
var wait = 99;
var check = function(){
var last = (Date.now()) - timestamp;
if (last < wait) {
id = setTimeout(check, wait - last);
} else {
id = null;
fn();
}
};
return function(){
timestamp = Date.now();
if(!id){
id = setTimeout(check, wait);
}
};
}
function abortRunning(){
if(isRunning){
if(lazyraf){
cancelRequestAnimationFrame(lazyraf);
}
if(lazytimer){
clearTimeout(lazytimer);
}
isRunning = false;
}
}
function onInputorMutation(){
if(throttle != 125){
remainingTime = 7;
throttle = 125;
minThrottle = 35;
if(isRunning) {
abortRunning();
scheduleLazy();
}
}
setInactive();
}
function scheduleAfterRaf() {
lazyraf = null;
lazytimer = setTimeout(runTasks, 0);
}
function scheduleRaf(){
lazytimer = null;
requestAnimationFrame(scheduleAfterRaf);
}
function scheduleLazy(){
if(isRunning){return;}
throttleDelay = throttle - (Date.now() - taskStart);
scheduleStart = Date.now();
isRunning = true;
if(minThrottle && throttleDelay < minThrottle){
throttleDelay = minThrottle;
}
if(throttleDelay > 9){
lazytimer = setTimeout(scheduleRaf, throttleDelay);
} else {
throttleDelay = 0;
scheduleRaf();
}
}
function runTasks(){
var task, i, len;
var timeThreshold = remainingTime > 9 ?
9 :
1
;
taskStart = Date.now();
isRunning = false;
lazytimer = null;
if(runAttempts > 2 || taskStart - throttleDelay - 50 < scheduleStart){
for(i = 0, len = tasks.length; i < len && IdleDeadline.timeRemaining() > timeThreshold; i++){
task = tasks.shift();
tasklength++;
if(task){
task(IdleDeadline);
}
}
}
if(tasks.length){
scheduleLazy();
} else {
runAttempts = 0;
}
}
function requestIdleCallbackShim(task){
index++;
tasks.push(task);
scheduleLazy();
return index;
}
function cancelIdleCallbackShim(id){
var index = id - 1 - tasklength;
if(tasks[index]){
tasks[index] = null;
}
}
if(!root.requestIdleCallback || !root.cancelIdleCallback){
root.requestIdleCallback = requestIdleCallbackShim;
root.cancelIdleCallback = cancelIdleCallbackShim;
if(root.document && document.addEventListener){
root.addEventListener('scroll', onInputorMutation, true);
root.addEventListener('resize', onInputorMutation);
document.addEventListener('focus', onInputorMutation, true);
document.addEventListener('mouseover', onInputorMutation, true);
['click', 'keypress', 'touchstart', 'mousedown'].forEach(function(name){
document.addEventListener(name, onInputorMutation, {capture: true, passive: true});
});
if(root.MutationObserver){
new MutationObserver( onInputorMutation ).observe( document.documentElement, {childList: true, subtree: true, attributes: true} );
}
}
} else {
try{
root.requestIdleCallback(function(){}, {timeout: 0});
} catch(e){
(function(rIC){
var timeRemainingProto, timeRemaining;
root.requestIdleCallback = function(fn, timeout){
if(timeout && typeof timeout.timeout == 'number'){
return rIC(fn, timeout.timeout);
}
return rIC(fn);
};
if(root.IdleCallbackDeadline && (timeRemainingProto = IdleCallbackDeadline.prototype)){
timeRemaining = Object.getOwnPropertyDescriptor(timeRemainingProto, 'timeRemaining');
if(!timeRemaining || !timeRemaining.configurable || !timeRemaining.get){return;}
Object.defineProperty(timeRemainingProto, 'timeRemaining', {
value: function(){
return timeRemaining.get.call(this);
},
enumerable: true,
configurable: true,
});
}
})(root.requestIdleCallback)
}
}
return {
request: requestIdleCallbackShim,
cancel: cancelIdleCallbackShim,
};
}));