Created
November 18, 2010 00:51
-
-
Save joedeveloper/704453 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copyright (C) 2010 joe.d.developer@gmail.com | |
* Released under AGPL, http://www.gnu.org/licenses/agpl.html. | |
* | |
* Helper function to break up processing of large sets | |
* uses yui asyncqueue internally | |
* @param set {array} array to iterate over. | |
* @param fn {function} function to call on each item | |
* @param co {object} optional context object | |
* @param cb {fn} optional function to call when all processing is complete. | |
* @param limit {int} optional batch size to operate on, default 100. | |
*/ | |
batchEach: function(set, fn, co, cb, limit){ | |
if(!Y.Lang.isNumber(limit) || limit < 1){ limit = 100;} | |
if(!Y.Lang.isObject(co)){co = null;} | |
var bq = new Y.AsyncQueue(), | |
innerFn = function(subset, func, co){ | |
Y.each(subset, func, co); | |
}; | |
for(var i = 0, subset = [], setSize = set.length || set.size(); i < setSize;){ | |
subset = set.slice(i, i+=limit); | |
bq.add({ | |
args: [subset, fn, co], | |
fn:innerFn, | |
context:co | |
}); | |
} | |
if(Y.Lang.isFunction(cb)){ | |
bq.add({fn: cb, args:[co]}); | |
} | |
bq.run(); | |
} | |
}; | |
/** | |
* Bridging to use nodelists with batchEach. | |
*/ | |
Y.NodeList.prototype.slice = function(beg, end){ | |
return new Y.NodeList(this._nodes.slice(beg,end)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment