summaryrefslogtreecommitdiffstats
path: root/lib/dombatcher.js
blob: fe5590e0323f1d275c5dfaa5e579c76c5f3fb68b (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

/**
 * Util
 */

function dispatch(fns) {
	var fn;
	while (fn = fns.shift()) fn();
}

/**
 * Lib
 */

function DomBatcher() {
	this.reads = [];
	this.writes = [];
}

DomBatcher.prototype.read = function(fn) {
	this._dispatcher = this._dispatcher || this.dispatch();
	this.reads.push(fn);
};

DomBatcher.prototype.write = function(fn) {
	this._dispatcher = this._dispatcher || this.dispatch();
	this.writes.push(fn);
};

DomBatcher.prototype.dispatch = function() {
	var self = this;
	return setTimeout(function() {
		dispatch(self.reads);
		dispatch(self.writes);
		delete self._dispatcher;
	}, 0);
};