summaryrefslogtreecommitdiffstats
path: root/lib/dom-batch.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dom-batch.js')
-rw-r--r--lib/dom-batch.js51
1 files changed, 44 insertions, 7 deletions
diff --git a/lib/dom-batch.js b/lib/dom-batch.js
index 1e1bcb4..2f80172 100644
--- a/lib/dom-batch.js
+++ b/lib/dom-batch.js
@@ -46,8 +46,8 @@
* @param {Function} fn
* @api public
*/
- DomBatch.prototype.read = function(fn) {
- this.reads.push(fn);
+ DomBatch.prototype.read = function(fn, ctx) {
+ add(this.reads, fn, ctx);
this.request('read');
};
@@ -58,8 +58,8 @@
* @param {Function} fn
* @api public
*/
- DomBatch.prototype.write = function(fn) {
- this.writes.push(fn);
+ DomBatch.prototype.write = function(fn, ctx) {
+ add(this.writes, fn, ctx);
this.request('write');
};
@@ -128,12 +128,22 @@
* Calls each job in
* the list passed.
*
+ * If a context has been
+ * stored on the function
+ * then it is used, else the
+ * current `this` is used.
+ *
* @param {Array} list
* @api private
*/
DomBatch.prototype.run = function(list) {
+ var fn;
+ var ctx;
+
while (list.length) {
- list.shift().call(this);
+ fn = list.shift();
+ ctx = fn._dbctx || this;
+ fn.call(ctx);
}
};
@@ -167,8 +177,35 @@
* Util
*/
- function remove(array, item) {
- var index = array.indexOf(item);
+ /**
+ * Adds a function to
+ * the given array.
+ *
+ * If a context is given
+ * it is stored on the
+ * function object for
+ * later.
+ *
+ * @param {Array} array
+ * @param {Function} fn
+ * @param {Object} ctx
+ * @api private
+ */
+ function add(array, fn, ctx) {
+ if (ctx) fn._dbctx = ctx;
+ array.push(fn);
+ }
+
+ /**
+ * Removes a function
+ * from the given array.
+ *
+ * @param {Array} array
+ * @param {Function} item
+ * @api private
+ */
+ function remove(array, fn) {
+ var index = array.indexOf(fn);
if (~index) array.splice(index, 1);
}