diff --git a/src/js/client.js b/src/js/client.js index 27a26a31b..6aa7da211 100644 --- a/src/js/client.js +++ b/src/js/client.js @@ -372,6 +372,7 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout this._configListener = []; this._readyListener = []; this._reconnectListener = []; + this._fetchingAppstoreAppsListener = []; this._userInfo = { id: null, username: null, @@ -2825,23 +2826,40 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout }); }; + Client.prototype._onAppstoreApps = function (callback) { + if (!this._fetchingAppstoreApps) {console.log('not fetching'); callback(); } + else this._fetchingAppstoreAppsListener.push(callback); + }; + 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) { - if (this._appstoreAppCache.length !== 0) return callback(null, this._appstoreAppCache); + var that = this; - this.getAppstoreApps(callback); + if (this._appstoreAppCache.length !== 0) return callback(null, this._appstoreAppCache); + if (!that._fetchingAppstoreApps) return this.getAppstoreApps(callback); + + this._onAppstoreApps(function () { that.getAppstoreAppsFast(callback) }); }; Client.prototype.getSubscription = function (callback) { diff --git a/src/views/appstore.js b/src/views/appstore.js index 54fadfb64..438bf1a9b 100644 --- a/src/views/appstore.js +++ b/src/views/appstore.js @@ -386,31 +386,27 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$tran }; function onSubscribed(callback) { - Client.getAppstoreApps(function (error) { + // 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); - // start with all apps listing. this also sets $scope.apps accordingly - $scope.showCategory(''); + $scope.domains = result; - // do this in background - Client.refreshConfig(); // refresh domain, user, group limit etc - fetchUsers(); - fetchGroups(); + // 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); - // domains is required since we populate the dropdown with domains[0] - Client.getDomains(function (error, result) { - if (error) return callback(error); + setTimeout(function () { $('#appstoreSearch').focus(); }, 1); - $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(); - }); + callback(); }); } @@ -605,16 +601,6 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$tran }); } - function getMemory(callback) { - Client.memory(function (error, memory) { - if (error) console.error(error); - - $scope.memory = memory; - - callback(); - }); - } - function init() { $scope.ready = false; @@ -637,9 +623,15 @@ angular.module('Application').controller('AppStoreController', ['$scope', '$tran }); } + // fetch early + Client.onReady(function () { Client.getAppstoreApps(function () {}); }); + + Client.onReady(init); + Client.onReady(function () { - getMemory(function () { - init(); + Client.memory(function (error, memory) { + if (error) console.error(error); + $scope.memory = memory; }); });