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
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
base.require('base.utils');
base.exportTo('base', function() {
// Setting this to true will cause stack traces to get dumped into the
// tasks. When an exception happens the original stack will be printed.
//
// NOTE: This should never be set committed as true.
var recordRAFStacks = false;
var pendingPreAFs = [];
var pendingRAFs = [];
var pendingIdleCallbacks = [];
var currentRAFDispatchList = undefined;
var rafScheduled = false;
function scheduleRAF() {
if (rafScheduled)
return;
rafScheduled = true;
if (window.requestAnimationFrame) {
window.requestAnimationFrame(processRequests);
} else {
var delta = Date.now() - window.performance.now();
window.webkitRequestAnimationFrame(function(domTimeStamp) {
processRequests(domTimeStamp - delta);
});
}
}
function onAnimationFrameError(e, opt_stack) {
if (opt_stack)
console.log(opt_stack);
if (e.message)
console.error(e.message, e.stack);
else
console.error(e);
}
function runTask(task, frameBeginTime) {
try {
task.callback.call(task.context, frameBeginTime);
} catch (e) {
base.onAnimationFrameError(e, task.stack);
}
}
function processRequests(frameBeginTime) {
// We assume that we want to do a maximum of 10ms optional work per frame.
// Hopefully rAF will eventually pass this in for us.
var rafCompletionDeadline = frameBeginTime + 10;
rafScheduled = false;
var currentPreAFs = pendingPreAFs;
currentRAFDispatchList = pendingRAFs;
pendingPreAFs = [];
pendingRAFs = [];
var hasRAFTasks = currentPreAFs.length || currentRAFDispatchList.length;
for (var i = 0; i < currentPreAFs.length; i++)
runTask(currentPreAFs[i], frameBeginTime);
while (currentRAFDispatchList.length > 0)
runTask(currentRAFDispatchList.shift(), frameBeginTime);
currentRAFDispatchList = undefined;
if (!hasRAFTasks) {
while (pendingIdleCallbacks.length > 0) {
runTask(pendingIdleCallbacks.shift());
// Check timer after running at least one idle task to avoid buggy
// window.performance.now() on some platforms from blocking the idle
// task queue.
if (window.performance.now() >= rafCompletionDeadline)
break;
}
}
if (pendingIdleCallbacks.length > 0)
scheduleRAF();
}
function getStack_() {
if (!recordRAFStacks)
return '';
var stackLines = base.stackTrace();
// Strip off getStack_.
stackLines.shift();
return stackLines.join('\n');
}
function requestPreAnimationFrame(callback, opt_this) {
pendingPreAFs.push({
callback: callback,
context: opt_this || window,
stack: getStack_()});
scheduleRAF();
}
function requestAnimationFrameInThisFrameIfPossible(callback, opt_this) {
if (!currentRAFDispatchList) {
requestAnimationFrame(callback, opt_this);
return;
}
currentRAFDispatchList.push({
callback: callback,
context: opt_this || window,
stack: getStack_()});
return;
}
function requestAnimationFrame(callback, opt_this) {
pendingRAFs.push({
callback: callback,
context: opt_this || window,
stack: getStack_()});
scheduleRAF();
}
function requestIdleCallback(callback, opt_this) {
pendingIdleCallbacks.push({
callback: callback,
context: opt_this || window,
stack: getStack_()});
scheduleRAF();
}
function forcePendingRAFTasksToRun(frameBeginTime) {
if (!rafScheduled)
return;
processRequests(frameBeginTime);
}
return {
onAnimationFrameError: onAnimationFrameError,
requestPreAnimationFrame: requestPreAnimationFrame,
requestAnimationFrame: requestAnimationFrame,
requestAnimationFrameInThisFrameIfPossible:
requestAnimationFrameInThisFrameIfPossible,
requestIdleCallback: requestIdleCallback,
forcePendingRAFTasksToRun: forcePendingRAFTasksToRun
};
});
|