Skip to content

Instantly share code, notes, and snippets.

@tissak
Created June 1, 2010 08:20
Show Gist options
  • Save tissak/420709 to your computer and use it in GitHub Desktop.
Save tissak/420709 to your computer and use it in GitHub Desktop.
// fire a callback after enough keys have been passed to a generated function
var dependencySet = function(conditionKeys, callback){
// convert a list of keys to trackable hash
var conditionSet = {}
for(var x=0;x<conditionKeys.length;x++){
conditionSet[conditionKeys[x]] = false;
}
// return a function that will wait till it's
// been called with all keys and then fire the callback
return function(val){
if(conditionSet[val] == null){ console.error("Bad argument",val); return; }
conditionSet[val]=true
var allTrue = true;
for(var x in conditionSet){
if(conditionSet[x]==false){
allTrue = false;
}
}
if(allTrue){
callback();
}
}
}
// usage
var t = dependencySet(['a','b'],function(){ console.log("asd"); });
t("a");
t("b");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment