Skip to content

Commit

Permalink
added noConflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Caolan McMahon committed Aug 2, 2010
1 parent 19d57ef commit 4fc05dd
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 47 deletions.
108 changes: 61 additions & 47 deletions lib/async.js
@@ -1,4 +1,18 @@
(function(exports){
(function(){

var async = {};

// global on the server, window in the browser
var root = this;
var previous_async = root.async;

if(typeof module !== 'undefined' && module.exports) module.exports = async;
else root.async = async;

async.noConflict = function(){
root.async = previous_async;
return async;
};

//// cross-browser compatiblity functions ////

Expand Down Expand Up @@ -46,14 +60,14 @@
//// exported async module functions ////

//// nextTick implementation with browser-compatible fallback ////
exports.nextTick = function(fn){
async.nextTick = function(fn){
if(typeof process == 'undefined' || !(process.nextTick)){
setTimeout(fn, 0);
}
else process.nextTick(fn);
};

exports.forEach = function(arr, iterator, callback){
async.forEach = function(arr, iterator, callback){
if(!arr.length) return callback();
var completed = 0;
_forEach(arr, function(x){
Expand All @@ -70,7 +84,7 @@
});
};

exports.forEachSeries = function(arr, iterator, callback){
async.forEachSeries = function(arr, iterator, callback){
if(!arr.length) return callback();
var completed = 0;
var iterate = function(){
Expand All @@ -93,13 +107,13 @@
var doParallel = function(fn){
return function(){
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [exports.forEach].concat(args));
return fn.apply(null, [async.forEach].concat(args));
};
};
var doSeries = function(fn){
return function(){
var args = Array.prototype.slice.call(arguments);
return fn.apply(null, [exports.forEachSeries].concat(args));
return fn.apply(null, [async.forEachSeries].concat(args));
};
};

Expand All @@ -118,14 +132,14 @@
callback(err, results);
});
};
exports.map = doParallel(_asyncMap);
exports.mapSeries = doSeries(_asyncMap);
async.map = doParallel(_asyncMap);
async.mapSeries = doSeries(_asyncMap);


// reduce only has a series version, as doing reduce in parallel won't
// work in many situations.
exports.reduce = function(arr, memo, iterator, callback){
exports.forEachSeries(arr, function(x, callback){
async.reduce = function(arr, memo, iterator, callback){
async.forEachSeries(arr, function(x, callback){
iterator(memo, x, function(err, v){
memo = v;
callback(err);
Expand All @@ -135,16 +149,16 @@
});
};
// inject alias
exports.inject = exports.reduce;
async.inject = async.reduce;
// foldl alias
exports.foldl = exports.reduce;
async.foldl = async.reduce;

exports.reduceRight = function(arr, memo, iterator, callback){
async.reduceRight = function(arr, memo, iterator, callback){
var reversed = _map(arr, function(x){return x;}).reverse();
exports.reduce(reversed, memo, iterator, callback);
async.reduce(reversed, memo, iterator, callback);
};
// foldr alias
exports.foldr = exports.reduceRight;
async.foldr = async.reduceRight;

var _filter = function(eachfn, arr, iterator, callback){
var results = [];
Expand All @@ -164,11 +178,11 @@
}));
});
};
exports.filter = doParallel(_filter);
exports.filterSeries = doSeries(_filter);
async.filter = doParallel(_filter);
async.filterSeries = doSeries(_filter);
// select alias
exports.select = exports.filter;
exports.selectSeries = exports.filterSeries;
async.select = async.filter;
async.selectSeries = async.filterSeries;

var _reject = function(eachfn, arr, iterator, callback){
var results = [];
Expand All @@ -188,8 +202,8 @@
}));
});
};
exports.reject = doParallel(_reject);
exports.rejectSeries = doSeries(_reject);
async.reject = doParallel(_reject);
async.rejectSeries = doSeries(_reject);

var _detect = function(eachfn, arr, iterator, main_callback){
eachfn(arr, function(x, callback){
Expand All @@ -201,11 +215,11 @@
main_callback();
});
};
exports.detect = doParallel(_detect);
exports.detectSeries = doSeries(_detect);
async.detect = doParallel(_detect);
async.detectSeries = doSeries(_detect);

exports.some = function(arr, iterator, main_callback){
exports.forEach(arr, function(x, callback){
async.some = function(arr, iterator, main_callback){
async.forEach(arr, function(x, callback){
iterator(x, function(v){
if(v){
main_callback(true);
Expand All @@ -218,10 +232,10 @@
});
};
// any alias
exports.any = exports.some;
async.any = async.some;

exports.every = function(arr, iterator, main_callback){
exports.forEach(arr, function(x, callback){
async.every = function(arr, iterator, main_callback){
async.forEach(arr, function(x, callback){
iterator(x, function(v){
if(!v){
main_callback(false);
Expand All @@ -234,10 +248,10 @@
});
};
// all alias
exports.all = exports.every;
async.all = async.every;

exports.sortBy = function(arr, iterator, callback){
exports.map(arr, function(x, callback){
async.sortBy = function(arr, iterator, callback){
async.map(arr, function(x, callback){
iterator(x, function(err, criteria){
if(err) callback(err);
else callback(null, {value: x, criteria: criteria});
Expand All @@ -251,7 +265,7 @@
})
};

exports.auto = function(tasks, callback){
async.auto = function(tasks, callback){
callback = callback || function(){};
var keys = _keys(tasks);
if(!keys.length) return callback(null);
Expand Down Expand Up @@ -312,7 +326,7 @@
});
};

exports.waterfall = function(tasks, callback){
async.waterfall = function(tasks, callback){
if(!tasks.length) return callback();
callback = callback || function(){};
var wrapIterator = function(iterator){
Expand All @@ -326,16 +340,16 @@
var next = iterator.next();
if(next) args.push(wrapIterator(next));
else args.push(callback);
exports.nextTick(function(){iterator.apply(null, args);});
async.nextTick(function(){iterator.apply(null, args);});
}
};
};
wrapIterator(exports.iterator(tasks))();
wrapIterator(async.iterator(tasks))();
};

exports.parallel = function(tasks, callback){
async.parallel = function(tasks, callback){
callback = callback || function(){};
exports.map(tasks, function(fn, callback){
async.map(tasks, function(fn, callback){
if(fn){
fn(function(err){
var args = Array.prototype.slice.call(arguments,1);
Expand All @@ -346,9 +360,9 @@
}, callback);
};

exports.series = function(tasks, callback){
async.series = function(tasks, callback){
callback = callback || function(){};
exports.mapSeries(tasks, function(fn, callback){
async.mapSeries(tasks, function(fn, callback){
if(fn){
fn(function(err){
var args = Array.prototype.slice.call(arguments,1);
Expand All @@ -359,7 +373,7 @@
}, callback);
};

exports.iterator = function(tasks){
async.iterator = function(tasks){
var makeCallback = function(index){
var fn = function(){
if(tasks.length) tasks[index].apply(null, arguments);
Expand All @@ -373,7 +387,7 @@
return makeCallback(0);
};

exports.apply = function(fn){
async.apply = function(fn){
var args = Array.prototype.slice.call(arguments, 1);
return function(){
fn.apply(null, args.concat(Array.prototype.slice.call(arguments)));
Expand All @@ -396,10 +410,10 @@
}]));
};
};
exports.log = _console_fn('log');
exports.dir = _console_fn('dir');
/*exports.info = _console_fn('info');
exports.warn = _console_fn('warn');
exports.error = _console_fn('error');*/
async.log = _console_fn('log');
async.dir = _console_fn('dir');
/*async.info = _console_fn('info');
async.warn = _console_fn('warn');
async.error = _console_fn('error');*/

})((typeof exports == 'undefined') ? this['async']={}: exports);
})();
27 changes: 27 additions & 0 deletions test/test-async.js
Expand Up @@ -841,3 +841,30 @@ exports['nextTick in the browser'] = function(test){
}, 50);
setTimeout(test.done, 100);
};

exports['noConflict'] = function(test){
test.expect(3);
var fs = require('fs');
var filename = __dirname + '/../lib/async.js';
fs.readFile(filename, function(err, content){
if(err) return test.done();
var Script = process.binding('evals').Script;

var s = new Script(content, filename);
var s2 = new Script(
content + 'this.async2 = this.async.noConflict();',
filename
);

var sandbox1 = {async: 'oldvalue'};
s.runInNewContext(sandbox1);
test.ok(sandbox1.async);

var sandbox2 = {async: 'oldvalue'};
s2.runInNewContext(sandbox2);
test.equals(sandbox2.async, 'oldvalue');
test.ok(sandbox2.async2);

test.done();
});
};

0 comments on commit 4fc05dd

Please sign in to comment.