diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-07-07 09:54:54 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-07-07 09:54:54 +0200 |
| commit | 35f2a91585f4c94de2e4668eab99890fa50c22a9 (patch) | |
| tree | e89ffb2c783fb047ece5163ab80d78f419906ded /app/lib/backend.js | |
| parent | db78c15f3445899bc0760bfea5194ae1d7896f47 (diff) | |
| parent | 518c934c98395f165977fd28837f4817cee664e7 (diff) | |
| download | mullvadvpn-35f2a91585f4c94de2e4668eab99890fa50c22a9.tar.xz mullvadvpn-35f2a91585f4c94de2e4668eab99890fa50c22a9.zip | |
Merge branch 'up-down-events'
Diffstat (limited to 'app/lib/backend.js')
| -rw-r--r-- | app/lib/backend.js | 42 |
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) { |
