summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/dombatcher.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/dombatcher.js b/lib/dombatcher.js
new file mode 100644
index 0000000..fe5590e
--- /dev/null
+++ b/lib/dombatcher.js
@@ -0,0 +1,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);
+};