diff options
| -rw-r--r-- | app/lib/backend.js | 26 | ||||
| -rw-r--r-- | app/lib/ipc-facade.js | 26 | ||||
| -rw-r--r-- | test/mocks/ipc.js | 84 |
3 files changed, 101 insertions, 35 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js index 377e27e702..a253595926 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'; @@ -280,9 +283,26 @@ 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, + })); + }); + } + + _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) { diff --git a/app/lib/ipc-facade.js b/app/lib/ipc-facade.js index 89c29ce795..19daf71b97 100644 --- a/app/lib/ipc-facade.js +++ b/app/lib/ipc-facade.js @@ -18,6 +18,7 @@ const LocationSchema = object({ city: string, }); +export type BackendState = 'secured' | 'unsecured'; export interface IpcFacade { getAccountData(AccountNumber): Promise<AccountData>, @@ -27,6 +28,8 @@ export interface IpcFacade { disconnect(): Promise<void>, getIp(): Promise<Ip>, getLocation(): Promise<Location>, + getState(): Promise<BackendState>, + registerStateListener((BackendState) => void): void, } export class RealIpc implements IpcFacade { @@ -93,4 +96,27 @@ export class RealIpc implements IpcFacade { } }); } + + getState(): Promise<BackendState> { + return this._ipc.send('get_state') + .then(raw => { + return this._parseBackendState(raw); + }); + } + + _parseBackendState(raw: mixed): BackendState { + if (raw === 'secured' || raw === 'unsecured') { + return raw; + } else { + throw new InvalidReply(raw); + } + } + + registerStateListener(listener: (BackendState) => void) { + this._ipc.on('new_state', (rawEvent) => { + const parsedEvent : BackendState = this._parseBackendState(rawEvent); + + listener(parsedEvent); + }); + } } diff --git a/test/mocks/ipc.js b/test/mocks/ipc.js index bb1a872454..3225fe9ed9 100644 --- a/test/mocks/ipc.js +++ b/test/mocks/ipc.js @@ -1,37 +1,57 @@ // @flow -import type { IpcFacade } from '../../app/lib/ipc-facade'; +import type { IpcFacade, BackendState } from '../../app/lib/ipc-facade'; -export function newMockIpc() { - return Object.assign({}, mockIpc); +interface MockIpc { + sendNewState: (BackendState) => void; + -getAccountData: *; + -connect: *; } -const mockIpc: IpcFacade = { +export function newMockIpc() { + + const stateListeners = []; - getAccountData: () => { - return new Promise(r => r({ - paid_until: '', - })); - }, - setAccount: () => { - return new Promise(r => r()); - }, - setCountry: () => { - return new Promise(r => r()); - }, - connect: () => { - return new Promise(r => r()); - }, - disconnect: () => { - return new Promise(r => r()); - }, - getIp: () => { - return new Promise(r => r('1.2.3.4')); - }, - getLocation: () => { - return new Promise(r => r({ - city: '', - country: '', - latlong: [], - })); - }, -}; + const mockIpc: IpcFacade & MockIpc = { + + getAccountData: () => { + return new Promise(r => r({ + paid_until: '', + })); + }, + setAccount: () => { + return new Promise(r => r()); + }, + setCountry: () => { + return new Promise(r => r()); + }, + connect: () => { + return new Promise(r => r()); + }, + disconnect: () => { + return new Promise(r => r()); + }, + getIp: () => { + return new Promise(r => r('1.2.3.4')); + }, + getLocation: () => { + return new Promise(r => r({ + city: '', + country: '', + latlong: [], + })); + }, + getState: () => { + return new Promise(r => r('unsecured')); + }, + registerStateListener: (listener: (BackendState) => void) => { + stateListeners.push(listener); + }, + sendNewState: (state: BackendState) => { + for(const l of stateListeners) { + l(state); + } + }, + }; + + return mockIpc; +} |
