refactor(RouteRegistry): optimize recognize()

This commit is contained in:
Victor Berchet 2015-05-14 15:38:16 +02:00
parent fc13cdab3a
commit 0114cd97b6
1 changed files with 18 additions and 7 deletions

View File

@ -87,18 +87,21 @@ export class RouteRegistry {
var children = StringMapWrapper.create(), var children = StringMapWrapper.create(),
allMapped = true; allMapped = true;
StringMapWrapper.forEach(candidate['handler']['components'], (component, name) => { var components = candidate['handler']['components'];
if (!allMapped) { var componentNames = StringMapWrapper.keys(components);
return;
} for (var cmpIndex = 0; cmpIndex < componentNames.length; cmpIndex++) {
var name = componentNames[cmpIndex];
var component = components[name];
var childInstruction = this.recognize(candidate['unmatchedUrl'], component); var childInstruction = this.recognize(candidate['unmatchedUrl'], component);
if (isPresent(childInstruction)) { if (isPresent(childInstruction)) {
childInstruction.params = candidate['params']; childInstruction.params = candidate['params'];
children[name] = childInstruction; children[name] = childInstruction;
} else { } else {
allMapped = false; allMapped = false;
break;
} }
}); }
if (allMapped) { if (allMapped) {
ListWrapper.push(fullSolutions, new Instruction({ ListWrapper.push(fullSolutions, new Instruction({
@ -112,8 +115,16 @@ export class RouteRegistry {
} }
if (fullSolutions.length > 0) { if (fullSolutions.length > 0) {
ListWrapper.sort(fullSolutions, (a, b) => a.cost < b.cost ? -1 : 1); var lowCost = fullSolutions[0].cost;
return fullSolutions[0]; var lowIndex = 0;
for (var solIdx = 1; solIdx < fullSolutions.length; solIdx++) {
if (fullSolutions[solIdx].cost < lowCost) {
lowCost = fullSolutions[solIdx].cost;
lowIndex = solIdx;
}
}
return fullSolutions[lowIndex];
} }
return null; return null;