repo-list: deduplicate repos as they load

This commit is contained in:
Jimmy Zelinskie 2015-03-02 13:28:38 -05:00
parent 9dd6e8e639
commit e48b8b2cbf

View file

@ -20,6 +20,7 @@
$scope.namespace = null;
$scope.page = 1;
$scope.publicPageCount = null;
$scope.all_repositories = {};
// When loading the UserService, if the user is logged in, create a list of
// relevant namespaces for later collecting relevant repositories.
@ -45,7 +46,6 @@
}
loadStarredRepos();
loadRepos();
deduplicateRepos();
}
}
);
@ -81,30 +81,14 @@
}, ApiService.errorDisplay('Could not unstar repository'));
};
var deduplicateRepos = function() {
// Wait for namespaces to load.
$scope.namespaces.then(function() {
// Wait for both starred repos and each individual namespace's repos to load.
var waitList = [$scope.starred_repositories].concat($scope.namespaces.repositories);
$q.all(waitList).then(function() {
var starred = {};
// Cache starred repos.
$scope.starred_repositories.value.map(function(repo) {
starred[repo.namspace + '/' + repo.name] = repo;
});
// If we find one of the starred repos elsewhere, replace it with
// the same object.
$scope.namespaces.value.map(function(namespace) {
namespace.repositories.value.map(function(repo) {
var found = starred[repo.namespace + '/' + repo.name];
if (found) {
repo = found;
// Finds a duplicate repo if it exists. If it doesn't, inserts the repo.
var findDuplicateRepo = function(repo) {
var found = $scope.all_repositories[repo.namespace + '/' + repo.name];
if (found != undefined) {
return found;
} else {
$scope.all_repositories[repo.namespace + '/' + repo.name] = repo;
}
});
});
});
});
};
var loadStarredRepos = function() {
@ -114,14 +98,13 @@
$scope.starred_repositories = ApiService.listStarredReposAsResource().get(function(resp) {
return resp.repositories.map(function(repo) {
repo = findDuplicateRepo(repo);
repo.is_starred = true;
return repo;
});
});
};
// Iterate over all of the $scope.namespaces and collect their respective
// repositories.
var loadRepos = function() {
if ($scope.namespaces.length == 0 || $scope.user.anonymous) {
return;
@ -136,7 +119,7 @@
'namespace': namespaceName,
};
namespace.repositories = ApiService.listReposAsResource().withOptions(options).get(function(resp) {
return resp.repositories;
return resp.repositories.map(findDuplicateRepo);
});
}
};