[Pkg-javascript-commits] [node-async] 35/480: added noConflict

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-async.

commit 4fc05dda1d5b68f7808ed850060d9706bdaf85b6
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Mon Aug 2 22:24:53 2010 +0100

    added noConflict
---
 lib/async.js       | 108 ++++++++++++++++++++++++++++++-----------------------
 test/test-async.js |  27 ++++++++++++++
 2 files changed, 88 insertions(+), 47 deletions(-)

diff --git a/lib/async.js b/lib/async.js
index 1541b73..e07ce6d 100644
--- a/lib/async.js
+++ b/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 ////
 
@@ -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){
@@ -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(){
@@ -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));
         };
     };
 
@@ -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);
@@ -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 = [];
@@ -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 = [];
@@ -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){
@@ -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);
@@ -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);
@@ -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});
@@ -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);
@@ -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){
@@ -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);
@@ -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);
@@ -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);
@@ -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)));
@@ -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);
+})();
diff --git a/test/test-async.js b/test/test-async.js
index e844c54..abeafd5 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -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();
+    });
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git



More information about the Pkg-javascript-commits mailing list