Began replacing calls to helpers with calls to memoized functions
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
preventLoop = function(inputFunction){
|
||||
var self = this;
|
||||
if (!_.isFunction(inputFunction)){
|
||||
throw new Meteor.Error(
|
||||
"Not a function",
|
||||
@@ -9,23 +10,26 @@ preventLoop = function(inputFunction){
|
||||
//if we try to visit the same argument twice before resolving its value
|
||||
//we are in a dependency loop and need to GTFO
|
||||
var visitedArgs = [];
|
||||
return function(argument){
|
||||
var value;
|
||||
return function(){
|
||||
var result;
|
||||
var hash = _.reduce(arguments, function(memo, arg) {
|
||||
return memo + arg;
|
||||
}, "");
|
||||
//we're still evaluating this attribute, must be in a loop
|
||||
if (_.contains(visitedArgs, argument)) {
|
||||
if (_.contains(visitedArgs, hash)) {
|
||||
console.warn("dependency loop detected");
|
||||
return NaN;
|
||||
} else {
|
||||
//push this skill to the list of visited skills
|
||||
//push this hash to the list of visited hashes
|
||||
//we can't visit it again unless it returns first
|
||||
visitedArgs.push(argument);
|
||||
visitedArgs.push(hash);
|
||||
}
|
||||
try {
|
||||
value = inputFunction.call(this, argument);
|
||||
result = inputFunction.apply(this, arguments);
|
||||
} finally{
|
||||
//this argument returns or fails, pull it from the array
|
||||
visitedArgs = _.without(visitedArgs, argument);
|
||||
//this hash returns or fails, pull it from the array
|
||||
visitedArgs = _.without(visitedArgs, hash);
|
||||
}
|
||||
return value;
|
||||
return result;
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user