From e48b8b2cbfd06677cf7bd79e46ff252322920534 Mon Sep 17 00:00:00 2001 From: Jimmy Zelinskie Date: Mon, 2 Mar 2015 13:28:38 -0500 Subject: [PATCH] repo-list: deduplicate repos as they load --- static/js/pages/repo-list.js | 39 ++++++++++-------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/static/js/pages/repo-list.js b/static/js/pages/repo-list.js index 74f33ae61..a71b19a90 100644 --- a/static/js/pages/repo-list.js +++ b/static/js/pages/repo-list.js @@ -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); }); } };