Created
July 18, 2011 23:30
-
-
Save laughinghan/1090948 to your computer and use it in GitHub Desktop.
Join
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Join | |
* Joins async callbacks with arguments concatenated. | |
* Usage: | |
* var join = new Join; | |
* getScore (angel, buffy, join()); | |
* getNextMatch (angel, join()); | |
* recordVisitAndGetInfo (angel, buffy, join()); | |
* join.after = function(score, next, vinfo) { | |
if (this.score > 0.9 && ! this.vinfo.last_visit) { | |
sendVisitorEmail(angel, buffy); | |
} | |
doSomeFinalThings(score, next, vinfo); | |
* }; | |
*/ | |
function Join() { | |
var waitingFor = 0, args = []; | |
return function() { | |
var argIndex = waitingFor; | |
waitingFor += 1; | |
return function() { | |
args[argIndex] = arguments; | |
waitingFor -= 1; | |
if (waitingFor === 0) | |
join.after.apply(null, args.reduce(__apply_concat, [])); | |
}; | |
}; | |
} | |
var __apply_concat = Function.apply.bind([].concat); | |
//equivalent to function(concatedArgs, args){ return [].concat.apply(concatedArgs, args); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment