From fc1192f1560f11d61965076e008d90ad42a030e4 Mon Sep 17 00:00:00 2001 From: rolux Date: Sat, 1 Jun 2013 15:25:37 +0200 Subject: [PATCH] Ox.Async: allow for passing false to serialForEach callback (break); allow for ommiting final callback of forEach methods; improve tests --- source/Ox/js/Async.js | 58 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 12 deletions(-) diff --git a/source/Ox/js/Async.js b/source/Ox/js/Async.js index 07291b34..acca402f 100644 --- a/source/Ox/js/Async.js +++ b/source/Ox/js/Async.js @@ -133,10 +133,21 @@ callback Callback function that The iterator's this binding callback Callback function + + > 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)); + }; - > 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 Callback function that The iterator's this binding callback Callback function + + > 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)); + }; - > 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) {