Permalink
Browse files

add async.js and compile

  • Loading branch information...
vol4ok committed Mar 23, 2012
1 parent fd1e8e1 commit 945926580ef1ee320d73fc110db67a1fcdde61e4
Showing with 120 additions and 0 deletions.
  1. +1 −0 Cakefile
  2. +119 −0 lib/core.js
View
@@ -12,6 +12,7 @@ TARGETS = [
'function.js'
'string.js'
'misc.js'
+ 'async.js'
'dom.js'
'events.js'
'ajax.js' ]
View
@@ -1058,7 +1058,126 @@ var $ = Core;
};
$.noop = function(){};
+ $.identity = function(value) { return value; };
+})(Core);
+/*** ASYNC.JS ***/
+
+/*!
+ * Copyright (c) 2010 Caolan McMahon
+ * Copyright (c) 2012 Andrew Volkov <hello@vol4ok.net>
+ */
+
+(function($){
+
+ var noop = $.noop;
+
+ $.asyncEach = function (object, iterator, callback) {
+ callback = callback || noop;
+ var i
+ , items = $.isArray(object) ? object : $.values(object)
+ , len = items.length
+ , completed = 0;
+ if (!len) return callback();
+ for (i = 0; i < len; i++) {
+ iterator(items[i], function (err) {
+ if (err) {
+ callback(err);
+ callback = noop;
+ } else if (++completed === len)
+ callback();
+ });
+ }
+ };
+
+ $.syncEach = function (object, iterator, callback) {
+ callback = callback || noop;
+ var iterate
+ , items = $.isArray(object) ? object : $.values(object)
+ , len = items.length
+ , i = 0;
+ (iterate = function() {
+ iterator(items[i], function (err) {
+ if (err) {
+ callback(err);
+ callback = noop;
+ } else if (++i === len)
+ callback();
+ else
+ iterate();
+ });
+ })();
+ };
+
+ var asyncMap, syncMap;
+
+ asyncMap = $.asyncMap = function (object, iterator, callback) {
+ callback = callback || noop;
+ var completed = 0
+ , results = $.isArray(object) ? [] : {};
+ if ($.isEmpty(object)) return callback(null, results)
+ $.each(object, function(val, key) {
+ completed++;
+ iterator(val, function (err, result) {
+ if (err) {
+ callback(err, results);
+ callback = noop
+ } else {
+ results[key] = result
+ if (--completed === 0)
+ callback(null, results);
+ }
+ });
+ });
+ };
+
+ syncMap = $.syncMap = function (object, iterator, callback) {
+ callback = callback || noop;
+ var iterate, items, len
+ , i = 0
+ , results = $.isArray(object) ? [] : {};
+ items = $.map(object, function(val, key) {
+ return {key: key, val: val}
+ });
+ len = items.length;
+ if (!len) return callback(null, results);
+ (iterate = function() {
+ iterator(items[i].val, function(err, result) {
+ if (err) {
+ callback(err);
+ callback = noop;
+ } else {
+ results[items[i].key] = result
+ if (++i === len)
+ callback(null, results);
+ else
+ iterate();
+ }
+
+ });
+ })();
+ }
+
+ $.parallel = function (tasks, callback) {
+ asyncMap(tasks, function(task, callback){
+ task(function(err) {
+ var args = $.slice.call(arguments, 1);
+ if (args.length <= 1) args = args[0];
+ callback.call(null, err, args);
+ });
+ }, callback);
+ };
+
+ $.series = function (tasks, callback) {
+ syncMap(tasks, function(task, callback){
+ task(function(err) {
+ var args = $.slice.call(arguments, 1);
+ if (args.length <= 1) args = args[0];
+ callback.call(null, err, args);
+ });
+ }, callback);
+ };
+
})(Core);
/*** DOM.JS ***/

0 comments on commit 9459265

Please sign in to comment.