signal-desktop/js/views/network_status_view.js

123 lines
3.4 KiB
JavaScript
Raw Normal View History

/* global Whisper, extension, Backbone, moment, i18n */
// eslint-disable-next-line func-names
2018-04-27 21:25:04 +00:00
(function() {
'use strict';
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.NetworkStatusView = Whisper.View.extend({
className: 'network-status',
templateName: 'networkStatus',
initialize() {
2018-04-27 21:25:04 +00:00
this.$el.hide();
2018-04-27 21:25:04 +00:00
this.renderIntervalHandle = setInterval(this.update.bind(this), 5000);
extension.windows.onClosed(() => {
clearInterval(this.renderIntervalHandle);
});
2018-04-27 21:25:04 +00:00
setTimeout(this.finishConnectingGracePeriod.bind(this), 5000);
2018-04-27 21:25:04 +00:00
this.withinConnectingGracePeriod = true;
this.setSocketReconnectInterval(null);
2018-04-27 21:25:04 +00:00
window.addEventListener('online', this.update.bind(this));
window.addEventListener('offline', this.update.bind(this));
2018-04-27 21:25:04 +00:00
this.model = new Backbone.Model();
this.listenTo(this.model, 'change', this.onChange);
},
onReconnectTimer() {
2018-04-27 21:25:04 +00:00
this.setSocketReconnectInterval(60000);
},
finishConnectingGracePeriod() {
2018-04-27 21:25:04 +00:00
this.withinConnectingGracePeriod = false;
},
setSocketReconnectInterval(millis) {
2018-04-27 21:25:04 +00:00
this.socketReconnectWaitDuration = moment.duration(millis);
},
navigatorOnLine() {
2018-04-27 21:25:04 +00:00
return navigator.onLine;
},
getSocketStatus() {
2018-04-27 21:25:04 +00:00
return window.getSocketStatus();
},
getNetworkStatus() {
let message = '';
let instructions = '';
let hasInterruption = false;
let action = null;
let buttonClass = null;
const socketStatus = this.getSocketStatus();
2018-04-27 21:25:04 +00:00
switch (socketStatus) {
case WebSocket.CONNECTING:
message = i18n('connecting');
this.setSocketReconnectInterval(null);
break;
case WebSocket.OPEN:
this.setSocketReconnectInterval(null);
break;
case WebSocket.CLOSED:
2018-04-27 21:25:04 +00:00
message = i18n('disconnected');
instructions = i18n('checkNetworkConnection');
hasInterruption = true;
break;
case WebSocket.CLOSING:
default:
2018-04-27 21:25:04 +00:00
message = i18n('disconnected');
instructions = i18n('checkNetworkConnection');
hasInterruption = true;
break;
}
2018-04-27 21:25:04 +00:00
if (
socketStatus === WebSocket.CONNECTING &&
2018-04-27 21:25:04 +00:00
!this.withinConnectingGracePeriod
) {
hasInterruption = true;
}
if (this.socketReconnectWaitDuration.asSeconds() > 0) {
instructions = i18n('attemptingReconnection', [
this.socketReconnectWaitDuration.asSeconds(),
]);
}
if (!this.navigatorOnLine()) {
hasInterruption = true;
message = i18n('offline');
instructions = i18n('checkNetworkConnection');
} else if (!Whisper.Registration.isDone()) {
hasInterruption = true;
message = i18n('Unlinked');
instructions = i18n('unlinkedWarning');
action = i18n('relink');
buttonClass = 'openInstaller';
}
2018-04-27 21:25:04 +00:00
return {
message,
instructions,
hasInterruption,
action,
buttonClass,
2018-04-27 21:25:04 +00:00
};
},
update() {
const status = this.getNetworkStatus();
2018-04-27 21:25:04 +00:00
this.model.set(status);
},
render_attributes() {
2018-04-27 21:25:04 +00:00
return this.model.attributes;
},
onChange() {
2018-04-27 21:25:04 +00:00
this.render();
if (this.model.attributes.hasInterruption) {
this.$el.slideDown();
} else {
this.$el.hide();
}
},
});
})();