Ox.Async: allow for passing false to serialForEach callback (break); allow for ommiting final callback of forEach methods; improve tests

This commit is contained in:
rolux 2013-06-01 15:25:37 +02:00
parent f820e06455
commit fc1192f156

View file

@ -133,10 +133,21 @@
callback <f> Callback function
that <o> The iterator's this binding
callback <f> Callback function
<script>
Ox.test.pfeNumber = 0;
Ox.test.pfeIterator = function(value, index, array, callback) {
if (index < 5) {
Ox.test.pfeNumber++;
}
setTimeout(callback);
};
</script>
> Ox.parallelForEach(Ox.range(10), Ox.test.pfeIterator, function() { Ox.test(Ox.test.pfeNumber, 5); })
undefined
@*/
Ox.parallelForEach = function(collection, iterator, that, callback) {
var i = 0, n, type = Ox.typeOf(collection);
callback = Ox.last(arguments);
callback = callback || (arguments.length == 3 ? arguments[2] : Ox.noop);
collection = type == 'array' || type == 'object'
? collection : Ox.toArray(collection);
n = Ox.len(collection);
@ -173,8 +184,11 @@
// Ox.print(results);
// }
// );
Ox.test.pmIterator = function(value, index, array, callback) {
setTimeout(callback(value - index));
};
</script>
> Ox.parallelMap(Ox.range(100000), Ox.noop, function(r) { Ox.test(r.length, 100000); })
> Ox.parallelMap(Ox.range(10), Ox.test.pmIterator, function(r) { Ox.test(Ox.sum(r), 0); })
undefined
@*/
Ox.parallelMap = function() {
@ -192,10 +206,21 @@
callback <f> Callback function
that <o> The iterator's this binding
callback <f> Callback function
<script>
Ox.test.sfeNumber = 0;
Ox.test.sfeIterator = function(value, index, array, callback) {
Ox.test.sfeNumber++;
setTimeout(function() {
callback(index < 4);
});
};
</script>
> Ox.serialForEach(Ox.range(10), Ox.test.sfeIterator, function() { Ox.test(Ox.test.sfeNumber, 5); })
undefined
@*/
Ox.serialForEach = function(collection, iterator, that, callback) {
var i = 0, keys, n, type = Ox.typeOf(collection);
callback = Ox.last(arguments);
callback = callback || (arguments.length == 3 ? arguments[2] : Ox.noop);
collection = type == 'array' || type == 'object'
? collection : Ox.toArray(collection);
keys = type == 'object'
@ -203,16 +228,22 @@
n = Ox.len(collection);
that = arguments.length == 4 ? that : null;
iterate();
function iterate() {
keys[i] in collection && iterator.call(
that,
collection[keys[i]],
keys[i],
collection,
function() {
function iterate(value) {
if (value !== false) {
if (keys[i] in collection) {
iterator.call(
that,
collection[keys[i]],
keys[i],
collection,
++i < n ? iterate : callback
);
} else {
++i < n ? iterate() : callback();
}
);
} else {
callback();
}
}
};
@ -241,8 +272,11 @@
// Ox.print(results);
// }
// );
Ox.test.smIterator = function(value, index, array, callback) {
setTimeout(callback(value - index));
};
</script>
> Ox.serialMap(Ox.range(1000), Ox.noop, function(r) { Ox.test(r.length, 1000); })
> Ox.serialMap(Ox.range(10), Ox.test.smIterator, function(r) { Ox.test(Ox.sum(r), 0); })
undefined
@*/
Ox.serialMap = function(collection, iterator, that, callback) {