diff --git a/source/Ox/js/Async.js b/source/Ox/js/Async.js index 151d16c9..24e97df3 100644 --- a/source/Ox/js/Async.js +++ b/source/Ox/js/Async.js @@ -2,14 +2,14 @@ (function() { - function asyncMap(forEach, col, iterator, that, callback) { - var type = Ox.typeOf(col), + function asyncMap(forEach, collection, iterator, that, callback) { + var type = Ox.typeOf(collection), results = type == 'object' ? {} : []; callback = Ox.last(arguments); that = arguments.length == 5 ? that : null; - forEach(col, function(val, key, obj, callback) { - iterator(val, key, obj, function(v) { - results[key] = v; + forEach(collection, function(value, key, collection, callback) { + iterator(value, key, collection, function(value) { + results[key] = value; callback(); }); }, that, function() { @@ -17,15 +17,16 @@ }); }; - Ox.asyncMap = function(arr, iterator, that, callback) { + Ox.asyncMap = function(array, iterator, that, callback) { + array = Ox.makeArray(array); callback = Ox.last(arguments); that = arguments.length == 4 ? that : null; - if (arr.some(Ox.isArray)) { - Ox.serialMap(arr, function(val, key, obj, callback) { - Ox.parallelMap(Ox.makeArray(val), iterator, callback); + if (array.some(Ox.isArray)) { + Ox.serialMap(array, function(value, key, array, callback) { + Ox.parallelMap(Ox.makeArray(value), iterator, callback); }, callback); } else { - Ox.parallelMap(Ox.makeArray(val), iterator, callback); + Ox.parallelMap(array, iterator, callback); } }; @@ -33,13 +34,16 @@ Ox.nonblockingForEach Non-blocking forEach with synchronous iterator (col, iterator[, that], callback[, ms]) -> undefined @*/ - Ox.nonblockingForEach = function(col, iterator, that, callback, ms) { - var i = 0, keys, last = Ox.last(arguments), n, time, type = Ox.typeOf(col); + Ox.nonblockingForEach = function(collection, iterator, that, callback, ms) { + var i = 0, keys, last = Ox.last(arguments), + n, time, type = Ox.typeOf(collection); callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2]; - col = type == 'array' || type == 'object' ? col : Ox.toArray(col); - keys = type == 'object' ? Object.keys(col) : Ox.range(col.length); + collection = type == 'array' || type == 'object' + ? collection : Ox.toArray(collection); + keys = type == 'object' + ? Object.keys(collection) : Ox.range(collection.length); ms = ms || 1000; - n = Ox.len(col); + n = Ox.len(collection); that = arguments.length == 5 || ( arguments.length == 4 && Ox.isFunction(last) ) ? that : null; @@ -47,18 +51,18 @@ iterate(); function iterate() { Ox.forEach(keys.slice(i), function(key) { - if (key in col) { + if (key in collection) { try { - iterator.call(that, col[key], key, col); - } catch(e) { - if (e === Ox.BreakError) { + iterator.call(that, collection[key], key, collection); + } catch(error) { + if (error === Ox.BreakError) { i = n; } throw e; } } i++; - +new Date() >= time + ms && Ox.Break()(); + +new Date() >= time + ms && Ox.Break(); }); if (i < n) { setTimeout(function() { @@ -73,8 +77,8 @@ /*@ Ox.nonblockingMap Non-blocking map with synchronous iterator - (col, iterator[, that]) -> undefined - col Collection + (collection, iterator[, that]) -> undefined + collection Collection iterator Iterator function that The iterator's this binding callback Callback function @@ -92,16 +96,16 @@ ); @*/ - Ox.nonblockingMap = function(col, iterator, that, callback, ms) { + Ox.nonblockingMap = function(collection, iterator, that, callback, ms) { var last = Ox.last(arguments), - type = Ox.typeOf(col), + type = Ox.typeOf(collection), results = type == 'object' ? {} : []; callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2]; that = arguments.length == 5 || ( arguments.length == 4 && Ox.isFunction(last) ) ? that : null; - Ox.nonblockingForEach(col, function(val, key, obj) { - results[key] = iterator.call(that, val, key, obj); + Ox.nonblockingForEach(collection, function(value, key, collection) { + results[key] = iterator.call(that, value, key, collection); }, function() { callback(type == 'string' ? results.join('') : results); }, ms); @@ -109,24 +113,25 @@ /*@ Ox.parallelForEach forEach with asynchronous iterator, running in parallel - (col, iterator[, that], callback) -> undefined - col Collection + (collection, iterator[, that], callback) -> undefined + collection Collection iterator Iterator function - val <*> Value + value <*> Value key Key - obj The collection + collection The collection callback Callback function that The iterator's this binding callback Callback function @*/ - Ox.parallelForEach = function(col, iterator, that, callback) { - var i = 0, n, type = Ox.typeOf(col); + Ox.parallelForEach = function(collection, iterator, that, callback) { + var i = 0, n, type = Ox.typeOf(collection); callback = Ox.last(arguments); - col = type == 'array' || type == 'object' ? col : Ox.toArray(col); - n = Ox.len(col); + collection = type == 'array' || type == 'object' + ? collection : Ox.toArray(collection); + n = Ox.len(collection); that = arguments.length == 4 ? that : null; - Ox.forEach(col, function(val, key, obj) { - iterator.call(that, val, key, obj, function() { + Ox.forEach(collection, function(value, key, collection) { + iterator.call(that, value, key, collection, function() { ++i == n && callback(); }); }); @@ -134,12 +139,12 @@ /*@ Ox.parallelMap Parallel map with asynchronous iterator - (col, iterator[, that], callback) -> undefined - col Collection + (collection, iterator[, that], callback) -> undefined + collection Collection iterator Iterator function - val <*> Value + value <*> Value key Key - obj The collection + collection The collection callback Callback function that The iterator's this binding callback Callback function @@ -165,44 +170,51 @@ /*@ Ox.serialForEach forEach with asynchronous iterator, run serially - (col, iterator[, that], callback) -> undefined - col Collection + (collection, iterator[, that], callback) -> undefined + collection Collection iterator Iterator function - val <*> Value + value <*> Value key Key - obj The collection + collection The collection callback Callback function that The iterator's this binding callback Callback function @*/ - Ox.serialForEach = function(col, iterator, that, callback) { - var i = 0, keys, n, type = Ox.typeOf(col); + Ox.serialForEach = function(collection, iterator, that, callback) { + var i = 0, keys, n, type = Ox.typeOf(collection); callback = Ox.last(arguments); - col = type == 'array' || type == 'object' ? col : Ox.toArray(col); - keys = type == 'object' ? Object.keys(col) : Ox.range(col.length); - n = Ox.len(col); + collection = type == 'array' || type == 'object' + ? collection : Ox.toArray(collection); + keys = type == 'object' + ? Object.keys(collection) : Ox.range(collection.length); + n = Ox.len(collection); that = arguments.length == 4 ? that : null; iterate(); function iterate() { - keys[i] in col && iterator.call(that, col[keys[i]], keys[i], col, function() { - ++i < n ? iterate() : callback(); - }); + keys[i] in collection && iterator.call( + that, + collection[keys[i]], + keys[i], + collection, + function() { + ++i < n ? iterate() : callback(); + } + ); } }; /*@ Ox.serialMap Serial map with asynchronous iterator - (col, iterator[, that], callback) -> undefined - col Collection + (collection, iterator[, that], callback) -> undefined + collection Collection iterator Iterator function - val <*> Value + value <*> Value key Key - obj The collection + collection The collection callback Callback function that The iterator's this binding callback Callback function - err Error object - results Results + results Results @*/ - Ox.serialMap = function(col, iterator, that, callback) { + Ox.serialMap = function(collection, iterator, that, callback) { asyncMap.apply(null, [Ox.serialForEach].concat(Ox.toArray(arguments))); };