diff options
-rw-r--r-- | lib/dom-batch.js | 38 | ||||
-rw-r--r-- | lib/dombatch.js | 29 |
2 files changed, 38 insertions, 29 deletions
diff --git a/lib/dom-batch.js b/lib/dom-batch.js new file mode 100644 index 0000000..e1cd5d7 --- /dev/null +++ b/lib/dom-batch.js @@ -0,0 +1,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; + } +}());
\ No newline at end of file diff --git a/lib/dombatch.js b/lib/dombatch.js deleted file mode 100644 index 190bb9c..0000000 --- a/lib/dombatch.js +++ /dev/null @@ -1,29 +0,0 @@ - -var domBatch = {}; -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); -}
\ No newline at end of file |