update non-blocking forEach
This commit is contained in:
parent
77f4d8f3b9
commit
aea2473399
1 changed files with 34 additions and 13 deletions
|
@ -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
|
||||
@*/
|
||||
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 <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() {
|
||||
asyncMap.apply(null, [Ox.nonblockingForEach].concat(Ox.toArray(arguments)));
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue