-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
227 lines (203 loc) · 5.06 KB
/
index.js
File metadata and controls
227 lines (203 loc) · 5.06 KB
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
/**
* FastDom
*
* Eliminates layout thrashing
* by batching DOM read/write
* interactions.
*
* @author Wilson Page <wilsonpage@me.com>
* @author Kornel Lesinski <kornel.lesinski@ft.com>
*/
;(function(fastdom){
'use strict';
// Normalize rAF
var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || function(cb) {
return setTimeout(cb, 16);
};
/**
* Creates a fresh
* FastDom instance.
*
* @constructor
*/
function FastDom() {
// Placing the rAF method
// on the instance allows
// us to replace it with
// a stub for testing.
this.raf = raf.bind(window);
this.reads = [];
this.writes = [];
this.deferred = [];
this.onError = undefined;
this.flush = this.flush.bind(this);
}
/**
* Adds a job to the
* read batch and schedules
* a new frame if need be.
*
* @param {Function} fn
* @public
*/
FastDom.prototype.read = function(fn, ctx) {
var job = {fn: fn, ctx: ctx};
this.reads.push(job);
this.scheduleBatch();
return job;
};
/**
* Adds a job to the
* write batch and schedules
* a new frame if need be.
*
* @param {Function} fn
* @public
*/
FastDom.prototype.write = function(fn, ctx) {
var job = {fn: fn, ctx: ctx};
this.writes.push(job);
this.scheduleBatch();
return job;
};
/**
* Defers the given job
* by the number of frames
* specified.
*
* If no frames are given
* then the job is run in
* the next free frame.
*
* @param {Number} frame
* @param {Function} fn
* @public
*/
FastDom.prototype.defer = function(frame, fn, ctx) {
// Accepts two arguments
if (typeof frame === 'function') {
ctx = fn;
fn = frame;
frame = 1;
}
var job;
if (frame > 1) {
var lastFn = fn;
var that = this;
fn = function() {
if (--job.frame > 0) {
that.deferred.push(job);
that.scheduleBatch();
} else {
lastFn.call(job.ctx);
}
};
}
job = {fn: fn, ctx: ctx, frame: frame};
this.deferred.push(job);
this.scheduleBatch();
return job;
};
/**
* Clears a scheduled 'read',
* 'write' or 'defer' job.
*
* @param {Object} id
* @public
*/
FastDom.prototype.clear = function(job) {
var idx;
idx = this.reads.indexOf(job);
if (idx >= 0) {
this.reads.splice(idx, 1);
return;
}
idx = this.writes.indexOf(job);
if (idx >= 0) {
this.writes.splice(idx, 1);
return;
}
idx = this.deferred.indexOf(job);
if (idx >= 0) {
this.deferred.splice(idx, 1);
return;
}
};
/**
* Schedules a new read/write
* batch if one isn't pending.
*
* @private
*/
FastDom.prototype.scheduleBatch = function() {
if (this.scheduled) return;
this.scheduled = true;
this.raf(this.flush);
};
FastDom.prototype.flush = function() {
var start = Date.now();
var error;
var readBatchTimeLimit = 13; // Stop running more read jobs if frame took more than this many ms (fudge factor)
var writeBatchTimeLimit = 9; // Spend less time on writes assuming browser will need time to process them
var deferredTimeLimit = 12;
try {
if (this.runBatch(this.reads, 10000, start, readBatchTimeLimit)) {
if (this.runBatch(this.writes, 10000, start, writeBatchTimeLimit)) {
this.runBatch(this.deferred, this.deferred.length, start, deferredTimeLimit); // deferred.length ensures newly deferred jobs aren't run immediately
}
}
} catch (e) {
error = e;
}
this.scheduled = false;
if (this.reads.length || this.writes.length || this.deferred.length) {
this.scheduleBatch();
}
if (error) {
if (this.onError) {
this.onError(error);
} else {
throw error;
}
}
};
/**
* Runs given jobs until frameTimeLimit (in ms) is reached
*
* We run this inside a try catch
* so that if any jobs error, we
* are able to recover and continue
* to flush the batch until it's empty.
*
* @private
*/
FastDom.prototype.runBatch = function(list, maxJobs, start, frameTimeLimit) {
var job;
while (maxJobs-- && (job = list.shift())) {
job.fn.call(job.ctx);
var took = Date.now() - start;
if (took > frameTimeLimit) {
// If it dropped below 30fps, then it's a jank, so at this point we may as well jank it all the way and flush the entire queue
if (took > 30) {
frameTimeLimit = 1e10;
continue;
}
return false;
}
}
return true;
};
// We only ever want there to be
// one instance of FastDom in an app
fastdom = fastdom || new FastDom();
/**
* Expose 'fastdom'
*/
if (typeof module !== 'undefined' && module.exports) {
module.exports = fastdom;
} else if (typeof define === 'function' && define.amd) {
define(function(){ return fastdom; });
} else {
window['fastdom'] = fastdom;
}
})(window.fastdom);