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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/**
* FastDom
*
* Eliminates layout thrashing
* by batching DOM read/write
* interactions.
*
* @author Wilson Page <wilsonpage@me.com>
*/
;(function(fastdom){
'use strict';
// Normalize rAF
var raf = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| window.msRequestAnimationFrame
|| function(cb) { return window.setTimeout(cb, 1000 / 60); };
/**
* Creates a fresh
* FastDom instance.
*
* @constructor
*/
function FastDom() {
this.frames = [];
this.lastId = 0;
// Placing the rAF method
// on the instance allows
// us to replace it with
// a stub for testing.
this.raf = raf;
this.batch = {
hash: {},
read: [],
write: [],
mode: null
};
}
/**
* 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 = this.add('read', fn, ctx);
var id = job.id;
// Add this job to the read queue
this.batch.read.push(job.id);
// We should *not* schedule a new frame if:
// 1. We're 'reading'
// 2. A frame is already scheduled
var doesntNeedFrame = this.batch.mode === 'reading'
|| this.batch.scheduled;
// If a frame isn't needed, return
if (doesntNeedFrame) return id;
// Schedule a new
// frame, then return
this.scheduleBatch();
return id;
};
/**
* 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 = this.add('write', fn, ctx);
var mode = this.batch.mode;
var id = job.id;
// Push the job id into the queue
this.batch.write.push(job.id);
// We should *not* schedule a new frame if:
// 1. We are 'writing'
// 2. We are 'reading'
// 3. A frame is already scheduled.
var doesntNeedFrame = mode === 'writing'
|| mode === 'reading'
|| this.batch.scheduled;
// If a frame isn't needed, return
if (doesntNeedFrame) return id;
// Schedule a new
// frame, then return
this.scheduleBatch();
return id;
};
/**
* 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 self = this;
var index = frame - 1;
return this.schedule(index, function() {
self.run({
fn: fn,
ctx: ctx
});
});
};
/**
* Clears a scheduled 'read',
* 'write' or 'defer' job.
*
* @param {Number|String} id
* @public
*/
FastDom.prototype.clear = function(id) {
// Defer jobs are cleared differently
if (typeof id === 'function') {
return this.clearFrame(id);
}
// Allow ids to be passed as strings
id = Number(id);
var job = this.batch.hash[id];
if (!job) return;
var list = this.batch[job.type];
var index = list.indexOf(id);
// Clear references
delete this.batch.hash[id];
if (~index) list.splice(index, 1);
};
/**
* Clears a scheduled frame.
*
* @param {Function} frame
* @private
*/
FastDom.prototype.clearFrame = function(frame) {
var index = this.frames.indexOf(frame);
if (~index) this.frames.splice(index, 1);
};
/**
* Schedules a new read/write
* batch if one isn't pending.
*
* @private
*/
FastDom.prototype.scheduleBatch = function() {
var self = this;
// Schedule batch for next frame
this.schedule(0, function() {
self.batch.scheduled = false;
self.runBatch();
});
// Set flag to indicate
// a frame has been scheduled
this.batch.scheduled = true;
};
/**
* Generates a unique
* id for a job.
*
* @return {Number}
* @private
*/
FastDom.prototype.uniqueId = function() {
return ++this.lastId;
};
/**
* Calls each job in
* the list passed.
*
* If a context has been
* stored on the function
* then it is used, else the
* current `this` is used.
*
* @param {Array} list
* @private
*/
FastDom.prototype.flush = function(list) {
var id;
while (id = list.shift()) {
this.run(this.batch.hash[id]);
}
};
/**
* Runs any 'read' jobs followed
* by any 'write' jobs.
*
* 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() {
try {
// Set the mode to 'reading',
// then empty all read jobs
this.batch.mode = 'reading';
this.flush(this.batch.read);
// Set the mode to 'writing'
// then empty all write jobs
this.batch.mode = 'writing';
this.flush(this.batch.write);
this.batch.mode = null;
} catch (e) {
this.runBatch();
throw e;
}
};
/**
* Adds a new job to
* the given batch.
*
* @param {Array} list
* @param {Function} fn
* @param {Object} ctx
* @returns {Number} id
* @private
*/
FastDom.prototype.add = function(type, fn, ctx) {
var id = this.uniqueId();
return this.batch.hash[id] = {
id: id,
fn: fn,
ctx: ctx,
type: type
};
};
/**
* Runs a given job.
*
* Applications using FastDom
* have the options of setting
* `fastdom.onError`.
*
* This will catch any
* errors that may throw
* inside callbacks, which
* is useful as often DOM
* nodes have been removed
* since a job was scheduled.
*
* Example:
*
* fastdom.onError = function(e) {
* // Runs when jobs error
* };
*
* @param {Object} job
* @private
*/
FastDom.prototype.run = function(job){
var ctx = job.ctx || this;
var fn = job.fn;
// Clear reference to the job
delete this.batch.hash[job.id];
// If no `onError` handler
// has been registered, just
// run the job normally.
if (!this.onError) {
return fn.call(ctx);
}
// If an `onError` handler
// has been registered, catch
// errors that throw inside
// callbacks, and run the
// handler instead.
try { fn.call(ctx); } catch (e) {
this.onError(e);
}
};
/**
* Starts a rAF loop
* to empty the frame queue.
*
* @private
*/
FastDom.prototype.loop = function() {
var self = this;
var raf = this.raf;
// Don't start more than one loop
if (this.looping) return;
raf(function frame() {
var fn = self.frames.shift();
// If no more frames,
// stop looping
if (!self.frames.length) {
self.looping = false;
// Otherwise, schedule the
// next frame
} else {
raf(frame);
}
// Run the frame. Note that
// this may throw an error
// in user code, but all
// fastdom tasks are dealt
// with already so the code
// will continue to iterate
if (fn) fn();
});
this.looping = true;
};
/**
* Adds a function to
* a specified index
* of the frame queue.
*
* @param {Number} index
* @param {Function} fn
* @return {Function}
* @private
*/
FastDom.prototype.schedule = function(index, fn) {
// Make sure this slot
// hasn't already been
// taken. If it has, try
// re-scheduling for the next slot
if (this.frames[index]) {
return this.schedule(index + 1, fn);
}
// Start the rAF
// loop to empty
// the frame queue
this.loop();
// Insert this function into
// the frames queue and return
return this.frames[index] = fn;
};
// 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);
|