summaryrefslogtreecommitdiffhomepage
path: root/app/lib/backend.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib/backend.js')
-rw-r--r--app/lib/backend.js42
1 files changed, 25 insertions, 17 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js
index 377e27e702..da2bc60613 100644
--- a/app/lib/backend.js
+++ b/app/lib/backend.js
@@ -9,6 +9,9 @@ import connectionActions from '../redux/connection/actions';
import type { ReduxStore } from '../redux/store';
import { push } from 'react-router-redux';
+import type { BackendState } from './ipc-facade';
+import type { ConnectionState } from '../redux/connection/reducers';
+
export type EventType = 'connect' | 'connecting' | 'disconnect' | 'login' | 'logging' | 'logout' | 'updatedIp' | 'updatedLocation' | 'updatedReachability';
export type ErrorType = 'NO_CREDIT' | 'NO_INTERNET' | 'INVALID_ACCOUNT';
@@ -231,15 +234,6 @@ export class Backend {
.then( () => {
return this._ipc.connect();
})
- .then(() => {
- this._emit('connect', addr);
- this._store.dispatch(connectionActions.connectionChange({
- status: 'connected',
- serverAddress: addr,
- }));
-
- this.sync(); // TODO: This is a pooooooor way of updating the location and the IP and stuff
- })
.catch(e => {
log.info('Failed connecting to', addr, e);
this._emit('connect', undefined, e);
@@ -252,11 +246,6 @@ export class Backend {
disconnect(): Promise<void> {
// @TODO: Failure modes
return this._ipc.disconnect()
- .then(() => {
- // emit: disconnect
- this._emit('disconnect');
- this.sync(); // TODO: This is a pooooooor way of updating the location and the IP and stuff
- })
.catch(e => {
log.info('Failed to disconnect', e);
});
@@ -280,9 +269,28 @@ export class Backend {
}
_registerIpcListeners() {
- /*this._ipc.on('connection-info', (newConnectionInfo) => {
- log.info('Got new connection info from backend', newConnectionInfo);
- });*/
+ this._ipc.registerStateListener(newState => {
+ log.info('Got new state from backend', newState);
+
+ const newStatus = this._backendStateToConnectionState(newState);
+ this._store.dispatch(connectionActions.connectionChange({
+ status: newStatus,
+ }));
+
+ this.sync();
+ });
+ }
+
+ _backendStateToConnectionState(backendState: BackendState): ConnectionState {
+ switch(backendState) {
+ case 'unsecured':
+ return 'disconnected';
+ case 'secured':
+ return 'connected';
+
+ default:
+ throw new Error('Unknown backend state: ' + backendState);
+ }
}
on(event: EventType, listener: Function) {