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