diff --git a/src/js/client.js b/src/js/client.js index c7eb826ea..6d2ead23b 100644 --- a/src/js/client.js +++ b/src/js/client.js @@ -85,10 +85,24 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout } function defaultErrorHandler(callback) { + function handleServerOffline() { + if (client.offline) return; + client.offline = true; + + (function onlineCheck() { + $http.get(client.apiOrigin + '/api/v1/cloudron/status', {}).success(function (data, status) { + client.offline = false; + client._reconnectListener.forEach(function (handler) { handler(); }); + }).error(function (data, status) { + $timeout(onlineCheck, 5000); + }); + })(); + } + return function (data, status) { // handle request killed by browser (eg. cors issue) if (data === null && status === -1) { - client.offline = true; + handleServerOffline(); return callback(new ClientError('Request cancelled by browser')); } @@ -103,7 +117,7 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout } if (status >= 500) { - client.offline = true; + handleServerOffline(); return callback(new ClientError(status, data)); } @@ -118,7 +132,6 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout function defaultSuccessHandler(callback) { return function (data, status) { - client.offline = false; return callback(null, data, status); }; } @@ -206,6 +219,7 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout this._ready = false; this._configListener = []; this._readyListener = []; + this._reconnectListener = []; this._userInfo = { id: null, username: null, @@ -318,6 +332,11 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout if (this._config && this._config.apiServerOrigin) callback(this._config); }; + Client.prototype.onReconnect = function (callback) { + if (this._ready) callback(); + else this._reconnectListener.push(callback); + }; + Client.prototype.resetAvatar = function () { this.avatar = this.apiOrigin + '/api/v1/cloudron/avatar?' + String(Math.random()).slice(2); diff --git a/src/js/main.js b/src/js/main.js index 0e0bb4123..f01094911 100644 --- a/src/js/main.js +++ b/src/js/main.js @@ -41,12 +41,12 @@ angular.module('Application').controller('MainController', ['$scope', '$route', }); }; - function refreshNotifications() { - Client.getNotifications(false, 1, 20, function (error, results) { + function refreshNotifications(poll) { + Client.getNotifications(false, 1, 100, function (error, results) { if (error) console.error(error); else $scope.notifications = results; - $timeout(refreshNotifications, 60 * 1000); + if (poll) $timeout(refreshNotifications, 60 * 1000); }); } @@ -112,7 +112,7 @@ angular.module('Application').controller('MainController', ['$scope', '$route', $scope.initialized = true; - refreshNotifications(); + refreshNotifications(true); $scope.updateSubscriptionStatus(); }); @@ -127,6 +127,10 @@ angular.module('Application').controller('MainController', ['$scope', '$route', } }); + Client.onReconnect(function () { + refreshNotifications(false); + }); + init(); // setup all the dialog focus handling diff --git a/src/views/notifications.html b/src/views/notifications.html index b796187b2..1985bbb33 100644 --- a/src/views/notifications.html +++ b/src/views/notifications.html @@ -44,7 +44,7 @@

{{ notification.messageJson | json }}
- + diff --git a/src/views/notifications.js b/src/views/notifications.js index 29004d120..b58f299e3 100644 --- a/src/views/notifications.js +++ b/src/views/notifications.js @@ -1,7 +1,6 @@ 'use strict'; -/* global asyncForEach:false */ -/* global angular:false */ +/* global asyncForEach, angular, $ */ angular.module('Application').controller('NotificationsController', ['$scope', '$timeout', 'Client', function ($scope, $timeout, Client) { @@ -15,16 +14,6 @@ angular.module('Application').controller('NotificationsController', ['$scope', ' $('#rebootModal').modal('show'); }, - waitForReboot: function () { - if (Client.offline) return $scope.reboot.busy = false; // at this point, we are showing the offline banner - - Client.getStatus(function (error, status) { - if (error) return $timeout($scope.reboot.waitForReboot, 5000); - - $scope.reboot.busy = false; - }); - }, - submit: function () { $scope.reboot.busy = true; @@ -33,8 +22,8 @@ angular.module('Application').controller('NotificationsController', ['$scope', ' $('#rebootModal').modal('hide'); - // show "busy" indicator for 5 seconds to show some ui activity - $timeout(function () { $scope.reboot.waitForReboot(); }, 5000); + // trigger refetch to show offline banner + $timeout(function () { Client.getStatus(function () {}); }, 5000); }); } }; @@ -45,7 +34,7 @@ angular.module('Application').controller('NotificationsController', ['$scope', ' busy: true, refresh: function () { - Client.getNotifications(false, 1, 20, function (error, result) { + Client.getNotifications(false, 1, 100, function (error, result) { if (error) return console.error(error); // collapse by default @@ -119,4 +108,8 @@ angular.module('Application').controller('NotificationsController', ['$scope', ' Client.onReady(function () { $scope.notifications.refresh(); }); + + Client.onReconnect(function () { + $scope.notifications.refresh(); + }) }]);