Add reconnect handler and make reboot state better reflected in the notfications

This commit is contained in:
Johannes Zellner
2020-03-06 02:38:17 -08:00
parent 1555b143a9
commit 19e2919d5b
4 changed files with 39 additions and 23 deletions

View File

@@ -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);

View File

@@ -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