blob: 1e1bcb4e933eafe665b6333ea6e4edd4deab4899 (
plain)
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
|
/**
* DOM-Batch
*
* Eliminates layout thrashing
* by batching DOM read/write
* interactions.
*
* @author Wilson Page <wilsonpage@me.com>
*/
;(function(dom){
'use strict';
// RequestAnimationFrame Polyfill
var raf = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| function(cb) { window.setTimeout(cb, 1000 / 60); };
// Use existing instance if
// one already exists in
// this app, else make one.
dom = (dom instanceof DomBatch)
? dom
: new DomBatch();
/**
* Creates a fresh
* DomBatch instance.
*
* @constructor
*/
function DomBatch() {
this.reads = [];
this.writes = [];
this.mode = null;
this.pending = false;
}
/**
* Adds a job to
* the read queue.
*
* @param {Function} fn
* @api public
*/
DomBatch.prototype.read = function(fn) {
this.reads.push(fn);
this.request('read');
};
/**
* Adds a job to
* the write queue.
*
* @param {Function} fn
* @api public
*/
DomBatch.prototype.write = function(fn) {
this.writes.push(fn);
this.request('write');
};
/**
* Removes a job from
* the 'reads' queue.
*
* @param {Function} fn
* @api public
*/
DomBatch.prototype.clearRead = function(fn) {
remove(this.reads, fn);
};
/**
* Removes a job from
* the 'writes' queue.
*
* @param {Function} fn
* @api public
*/
DomBatch.prototype.clearWrite = function(fn) {
remove(this.writes, fn);
};
/**
* Makes the decision as to
* whether a the frame needs
* to be scheduled.
*
* @param {String} type
* @api private
*/
DomBatch.prototype.request = function(type) {
var self = this;
// If we are currently writing, we don't
// need to scedule a new frame as this
// job will be emptied from the write queue
if (this.mode === 'writing' && type === 'write') return;
// If we are reading we don't need to schedule
// a new frame as this read will be emptied
// in the currently active read queue
if (this.mode === 'reading' && type === 'read') return;
// If we are reading we don't need to schedule
// a new frame and this write job will be run
// after the read queue has been emptied in the
// currently active frame.
if (this.mode === 'reading' && type === 'write') return;
// If there is already a frame
// scheduled, don't schedule another one
if (this.pending) return;
// Schedule frame (preserving context)
raf(function() { self.frame(); });
// Set flag to indicate
// a frame has been scheduled
this.pending = true;
};
/**
* Calls each job in
* the list passed.
*
* @param {Array} list
* @api private
*/
DomBatch.prototype.run = function(list) {
while (list.length) {
list.shift().call(this);
}
};
/**
* Runs any read jobs followed
* by any write jobs.
*
* @api private
*/
DomBatch.prototype.frame = function() {
// Set the pending flag to
// false so that any new requests
// that come in will schedule a new frame
this.pending = false;
// Set the mode to 'reading',
// then empty all read jobs
this.mode = 'reading';
this.run(this.reads);
// Set the mode to 'writing'
// then empty all write jobs
this.mode = 'writing';
this.run(this.writes);
this.mode = null;
};
/**
* Util
*/
function remove(array, item) {
var index = array.indexOf(item);
if (~index) array.splice(index, 1);
}
/**
* Expose 'DomBatch'
*/
if (typeof exports === "object") {
module.exports = dom;
} else if (typeof define === "function" && define.amd) {
define(function(){ return dom; });
} else {
window['dom'] = dom;
}
})(window.dom);
|