diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2018-09-10 17:14:42 +0300 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2018-09-10 17:56:02 +0300 |
| commit | 5dc130b3526a43acc73622a66f861b7b0169fc4f (patch) | |
| tree | 3264dd922f711b8eb26463b36753badd00cdfcec /gui | |
| parent | deb7bd7cc7caa0612af87840d63bcf547fe67871 (diff) | |
| download | mullvadvpn-5dc130b3526a43acc73622a66f861b7b0169fc4f.tar.xz mullvadvpn-5dc130b3526a43acc73622a66f861b7b0169fc4f.zip | |
Add SubscriptionListener to streamline nullability handling
Diffstat (limited to 'gui')
| -rw-r--r-- | gui/packages/desktop/src/renderer/app.js | 21 | ||||
| -rw-r--r-- | gui/packages/desktop/src/renderer/lib/daemon-rpc.js | 28 |
2 files changed, 33 insertions, 16 deletions
diff --git a/gui/packages/desktop/src/renderer/app.js b/gui/packages/desktop/src/renderer/app.js index 6d68cf4e1b..66154bcc9d 100644 --- a/gui/packages/desktop/src/renderer/app.js +++ b/gui/packages/desktop/src/renderer/app.js @@ -14,7 +14,7 @@ import { createMemoryHistory } from 'history'; import makeRoutes from './routes'; import ReconnectionBackoff from './lib/reconnection-backoff'; -import { DaemonRpc, ConnectionObserver } from './lib/daemon-rpc'; +import { DaemonRpc, ConnectionObserver, SubscriptionListener } from './lib/daemon-rpc'; import NotificationController from './lib/notification-controller'; import setShutdownHandler from './lib/shutdown-handler'; import { NoAccountError } from './errors'; @@ -499,18 +499,19 @@ export default class AppRenderer { this._connectedToDaemon = false; } - async _subscribeStateListener() { - await this._daemonRpc.subscribeStateListener((newState, error) => { - if (error) { - log.error(`Failed to deserialize the new state: ${error.message}`); - } - - if (newState) { + _subscribeStateListener(): Promise<void> { + const listener = new SubscriptionListener( + (newState: TunnelStateTransition) => { log.debug(`Got state update: '${JSON.stringify(newState)}'`); this._onChangeTunnelState(newState); - } - }); + }, + (error: Error) => { + log.error(`Failed to deserialize the new state: ${error.message}`); + }, + ); + + return this._daemonRpc.subscribeStateListener(listener); } _fetchInitialState() { diff --git a/gui/packages/desktop/src/renderer/lib/daemon-rpc.js b/gui/packages/desktop/src/renderer/lib/daemon-rpc.js index ca45c74af2..198a3e764d 100644 --- a/gui/packages/desktop/src/renderer/lib/daemon-rpc.js +++ b/gui/packages/desktop/src/renderer/lib/daemon-rpc.js @@ -278,6 +278,24 @@ export class ConnectionObserver { }; } +export class SubscriptionListener<T> { + _eventHandler: (payload: T) => void; + _errorHandler: (error: Error) => void; + + constructor(eventHandler: (payload: T) => void, errorHandler: (error: Error) => void) { + this._eventHandler = eventHandler; + this._errorHandler = errorHandler; + } + + _onEvent(payload: T) { + this._eventHandler(payload); + } + + _onError(error: Error) { + this._errorHandler(error); + } +} + export interface DaemonRpcProtocol { connect({ path: string }): void; disconnect(): void; @@ -297,7 +315,7 @@ export interface DaemonRpcProtocol { disconnectTunnel(): Promise<void>; getLocation(): Promise<Location>; getState(): Promise<TunnelStateTransition>; - subscribeStateListener((state: ?TunnelStateTransition, error: ?Error) => void): Promise<void>; + subscribeStateListener(listener: SubscriptionListener<TunnelStateTransition>): Promise<void>; addConnectionObserver(observer: ConnectionObserver): void; removeConnectionObserver(observer: ConnectionObserver): void; getAccountHistory(): Promise<Array<AccountToken>>; @@ -484,15 +502,13 @@ export class DaemonRpc implements DaemonRpcProtocol { } } - subscribeStateListener( - listener: (state: ?TunnelStateTransition, error: ?Error) => void, - ): Promise<void> { + subscribeStateListener(listener: SubscriptionListener<TunnelStateTransition>): Promise<void> { return this._transport.subscribe('new_state', (payload) => { try { const newState = validate(TunnelStateTransitionSchema, payload); - listener(newState, null); + listener._onEvent(newState); } catch (error) { - listener(null, new ResponseParseError('Invalid payload from new_state', error)); + listener._onError(new ResponseParseError('Invalid payload from new_state', error)); } }); } |
