-
-
Save zhuzhuaicoding/5151643 to your computer and use it in GitHub Desktop.
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
function getGroupCallBackGenerator(timeOut,finalCallback){ | |
var waitingStatus={}; | |
var resultObject={}; | |
var started=false; | |
var timedOut=false; | |
var timer=setTimeout(function(){ | |
timedOut=true; | |
if(typeof(finalCallback)=="function"){ | |
finalCallback(resultObject); | |
} | |
timer=false; | |
},timeOut); | |
function isAllDone(){ | |
for(var ins in waitingStatus){ | |
if(waitingStatus.hasOwnProperty(ins)){ | |
if(waitingStatus[ins]){ | |
return false;; | |
} | |
} | |
} | |
return true; | |
} | |
function tryToTriggerFinalCallback(){ | |
if(!isAllDone()){ | |
return; | |
} | |
if(timedOut){ | |
return; | |
} | |
if(!started){ | |
return; | |
} | |
if(typeof(finalCallback)=="function"){ | |
if(timer){ | |
clearTimeout(timer); | |
timer=false; | |
} | |
finalCallback(resultObject); | |
} | |
} | |
return { | |
'getCallback':function (instanceName){ | |
if(waitingStatus[instanceName]!==undefined){ | |
throw "Duplicate Instance Name, While Generating the Callback..."; | |
} | |
waitingStatus[instanceName]=true; | |
return function(r){ | |
resultObject[instanceName]=r||{}; | |
waitingStatus[instanceName]=false; | |
tryToTriggerFinalCallback(); | |
} | |
}, | |
'start':function (){ | |
started=true; | |
tryToTriggerFinalCallback(); | |
}, | |
'abort':function(){ | |
started=false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment