diff --git a/src/js/client.js b/src/js/client.js index 6aa7da211..53262f44d 100644 --- a/src/js/client.js +++ b/src/js/client.js @@ -372,7 +372,6 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout this._configListener = []; this._readyListener = []; this._reconnectListener = []; - this._fetchingAppstoreAppsListener = []; this._userInfo = { id: null, username: null, @@ -2834,32 +2833,20 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout Client.prototype.getAppstoreApps = function (callback) { var that = this; - if (that._fetchingAppstoreApps) return this.getAppstoreAppsFast(callback); - - that._fetchingAppstoreApps = true; - get('/api/v1/appstore/apps', null, function (error, data, status) { - that._fetchingAppstoreApps = false; - if (error) return callback(error); if (status !== 200) return callback(new ClientError(status, data)); angular.copy(data.apps, that._appstoreAppCache); - that._fetchingAppstoreAppsListener.forEach(function (callback) { callback(); }); - that._fetchingAppstoreAppsListener = []; - return callback(null, that._appstoreAppCache); }); }; Client.prototype.getAppstoreAppsFast = function (callback) { - var that = this; - if (this._appstoreAppCache.length !== 0) return callback(null, this._appstoreAppCache); - if (!that._fetchingAppstoreApps) return this.getAppstoreApps(callback); - this._onAppstoreApps(function () { that.getAppstoreAppsFast(callback) }); + this.getAppstoreApps(callback); }; Client.prototype.getSubscription = function (callback) { diff --git a/src/views/appstore.js b/src/views/appstore.js index 438bf1a9b..91f29b906 100644 --- a/src/views/appstore.js +++ b/src/views/appstore.js @@ -376,40 +376,16 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$tran return; } + // start with all apps listing. this also sets $scope.apps accordingly + $scope.showCategory(''); + getSubscription(function (error) { if (error) return console.error(error); - - onSubscribed(function (error) { if (error) console.error(error); }); }); }); } }; - function onSubscribed(callback) { - // start with all apps listing. this also sets $scope.apps accordingly - $scope.showCategory(''); - - // do this in background - Client.refreshConfig(); // refresh domain, user, group limit etc - fetchUsers(); - fetchGroups(); - - // domains is required since we populate the dropdown with domains[0] - Client.getDomains(function (error, result) { - if (error) return callback(error); - - $scope.domains = result; - - // show install app dialog immediately if an app id was passed in the query - // hashChangeListener calls $apply, so make sure we don't double digest here - setTimeout(hashChangeListener, 1); - - setTimeout(function () { $('#appstoreSearch').focus(); }, 1); - - callback(); - }); - } - // TODO does not support testing apps in search $scope.search = function () { if (!$scope.searchString) return $scope.showCategory($scope.cachedCategory); @@ -576,6 +552,17 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$tran }); } + function fetchMemory() { + Client.memory(function (error, memory) { + if (error) { + console.error(error); + return $timeout(fetchMemory, 5000); + } + + $scope.memory = memory; + }); + } + function getSubscription(callback) { Client.getSubscription(function (error, subscription) { if (error) { @@ -602,39 +589,46 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$tran } function init() { - $scope.ready = false; + Client.getAppstoreAppsFast(function (error) { + $scope.ready = true; - getSubscription(function (error) { - if (error) { - console.error(error); + if (error && error.statusCode === 402) { + $scope.validSubscription = false; + return; + } else if (error) { + console.error('Failed to get apps. Will retry.', error); return $timeout(init, 1000); } - if (!$scope.validSubscription) { // show the login form - $scope.ready = true; - return; - } + $scope.validSubscription = true; - onSubscribed(function (error) { - if (error) console.error(error); + $scope.showCategory(''); - $scope.ready = true; + // refresh everything in background + getSubscription(function (error) { if (error) console.error('Failed to get subscription.', error); }); + Client.getAppstoreApps(function (error) { if (error) console.error('Failed to fetch apps.', error); }); + Client.refreshConfig(); // refresh domain, user, group limit etc + fetchUsers(); + fetchGroups(); + fetchMemory(); + + // domains is required since we populate the dropdown with domains[0] + Client.getDomains(function (error, result) { + if (error) return console.error('Error getting domains.', error); + + $scope.domains = result; + + // show install app dialog immediately if an app id was passed in the query + // hashChangeListener calls $apply, so make sure we don't double digest here + setTimeout(hashChangeListener, 1); + + setTimeout(function () { $('#appstoreSearch').focus(); }, 1); }); }); } - // fetch early - Client.onReady(function () { Client.getAppstoreApps(function () {}); }); - Client.onReady(init); - Client.onReady(function () { - Client.memory(function (error, memory) { - if (error) console.error(error); - $scope.memory = memory; - }); - }); - // note: do not use hide.bs.model since it is called immediately from switchToAppsView which is already in angular scope $('#appInstallModal').on('hidden.bs.modal', function () { // clear the appid and version in the search bar when dialog is cancelled