summaryrefslogtreecommitdiffstats
path: root/lib/dom-batch.js
blob: e1cd5d71cf6026b4bf0799c616407bdaca176211 (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
(function() {
  var batcher = {};
  var reads = [];
  var writes = [];
  var batch;

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

  domBatch.read = function(fn) {
    batch = batch || setBatch();
    reads.push(fn);
  };

  domBatch.write = function(fn) {
    batch = batch || setBatch();
    writes.push(fn);
  };

  function setBatch() {
    return setTimeout(function() {
      call(reads);
      call(writes);
      batch = null;
    }, 0);
  }

  // Expose the library
  if (typeof exports === "object") {
    module.exports = batcher;
  } else if (typeof define === "function" && define.amd) {
    define(batcher);
  } else {
    window['batcher'] = batcher;
  }
}());