update non-blocking forEach

This commit is contained in:
rolux 2012-05-23 18:14:36 +02:00
parent 77f4d8f3b9
commit aea2473399

View file

@ -28,7 +28,7 @@
}; };
/*@ /*@
Ox.nonblockingForEach <f> Non-blocking forEach Ox.nonblockingForEach <f> Non-blocking forEach with synchronous iterator
(col, iterator[, that], callback[, ms]) -> <u> undefined (col, iterator[, that], callback[, ms]) -> <u> undefined
@*/ @*/
Ox.nonblockingForEach = function(col, iterator, that, callback, ms) { Ox.nonblockingForEach = function(col, iterator, that, callback, ms) {
@ -44,7 +44,7 @@
time = +new Date(); time = +new Date();
iterate(); iterate();
function iterate() { function iterate() {
keys[i] in col && iterator.call(that, col[keys[i]], keys[i], col, function() { keys[i] in col && iterator.call(that, col[keys[i]], keys[i], col);
if (++i < n) { if (++i < n) {
if (+new Date() < time + ms) { if (+new Date() < time + ms) {
iterate(); iterate();
@ -57,10 +57,31 @@
} else { } else {
callback(); callback();
} }
});
} }
}; };
/*@
Ox.nonblockingMap <f> Non-blocking map with synchronous iterator
(col, iterator[, that], callback) -> <u> undefined
col <a|o|s> Collection
iterator <f> Iterator function
that <o> The iterator's this binding
callback <f> Callback function
<script>
var time = +new Date();
Ox.serialMap(
Ox.range(10),
function (value, index, array, callback) {
setTimeout(function() {
callback(+new Date() - time);
}, Ox.random(1000));
},
function(results) {
Ox.print(results);
}
);
</script>
@*/
Ox.nonblockingMap = function() { Ox.nonblockingMap = function() {
asyncMap.apply(null, [Ox.nonblockingForEach].concat(Ox.toArray(arguments))); asyncMap.apply(null, [Ox.nonblockingForEach].concat(Ox.toArray(arguments)));
}; };