From aea2473399e6c61496fb2d3f69dbb3628e57a829 Mon Sep 17 00:00:00 2001 From: rolux Date: Wed, 23 May 2012 18:14:36 +0200 Subject: [PATCH] update non-blocking forEach --- source/Ox/js/Async.js | 47 +++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/source/Ox/js/Async.js b/source/Ox/js/Async.js index 070f78f9..94ddb258 100644 --- a/source/Ox/js/Async.js +++ b/source/Ox/js/Async.js @@ -28,7 +28,7 @@ }; /*@ - Ox.nonblockingForEach Non-blocking forEach + Ox.nonblockingForEach Non-blocking forEach with synchronous iterator (col, iterator[, that], callback[, ms]) -> undefined @*/ Ox.nonblockingForEach = function(col, iterator, that, callback, ms) { @@ -44,23 +44,44 @@ time = +new Date(); iterate(); function iterate() { - keys[i] in col && iterator.call(that, col[keys[i]], keys[i], col, function() { - if (++i < n) { - if (+new Date() < time + ms) { - iterate(); - } else { - setTimeout(function() { - time = +new Date(); - iterate(); - }); - } + keys[i] in col && iterator.call(that, col[keys[i]], keys[i], col); + if (++i < n) { + if (+new Date() < time + ms) { + iterate(); } else { - callback(); + setTimeout(function() { + time = +new Date(); + iterate(); + }); } - }); + } else { + callback(); + } } }; + /*@ + Ox.nonblockingMap Non-blocking map with synchronous iterator + (col, iterator[, that], callback) -> undefined + col Collection + iterator Iterator function + that The iterator's this binding + callback Callback function + + @*/ Ox.nonblockingMap = function() { asyncMap.apply(null, [Ox.nonblockingForEach].concat(Ox.toArray(arguments))); };