summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorErik Larkö <erik@mullvad.net>2017-07-28 10:20:07 +0200
committerErik Larkö <erik@mullvad.net>2017-08-08 14:05:07 +0200
commit2346c28fd37c371aa759aa24ad16722951a7561e (patch)
treee98bb11611da548d9c94e3d8fb6aa8f06867538c
parent696c6b0d3a3f33925f35f7fe3939f246c64c44f9 (diff)
downloadmullvadvpn-2346c28fd37c371aa759aa24ad16722951a7561e.tar.xz
mullvadvpn-2346c28fd37c371aa759aa24ad16722951a7561e.zip
Add ONLINE and OFFLINE actions and remove generic connection action
-rw-r--r--app/lib/backend.js10
-rw-r--r--app/redux/connection/actions.js42
-rw-r--r--app/redux/connection/reducers.js6
-rw-r--r--test/reducers.spec.js14
4 files changed, 38 insertions, 34 deletions
diff --git a/app/lib/backend.js b/app/lib/backend.js
index e8c8cbd464..2d39d4b7cd 100644
--- a/app/lib/backend.js
+++ b/app/lib/backend.js
@@ -257,17 +257,21 @@ export class Backend {
*/
_startReachability() {
window.addEventListener('online', () => {
- this._store.dispatch(connectionActions.connectionChange({ isOnline: true }));
+ this._store.dispatch(connectionActions.online());
});
window.addEventListener('offline', () => {
// force disconnect since there is no real connection anyway.
this.disconnect();
- this._store.dispatch(connectionActions.connectionChange({ isOnline: false }));
+ this._store.dispatch(connectionActions.offline());
});
// update online status in background
setTimeout(() => {
- this._store.dispatch(connectionActions.connectionChange({ isOnline: navigator.onLine }));
+ const action = navigator.onLine
+ ? connectionActions.online()
+ : connectionActions.offline();
+
+ this._store.dispatch(action);
}, 0);
}
diff --git a/app/redux/connection/actions.js b/app/redux/connection/actions.js
index e7fd03bc77..eebc503a18 100644
--- a/app/redux/connection/actions.js
+++ b/app/redux/connection/actions.js
@@ -3,7 +3,6 @@
import { clipboard } from 'electron';
import type { Backend } from '../../lib/backend';
-import type { ConnectionReduxState } from './reducers.js';
import type { ReduxGetState, ReduxDispatch } from '../store';
import type { Coordinate2d } from '../../types';
@@ -31,11 +30,6 @@ type DisconnectedAction = {
type: 'DISCONNECTED',
};
-type ConnectionChangeAction = {
- type: 'CONNECTION_CHANGE',
- newData: $Shape<ConnectionReduxState>,
-};
-
type NewPublicIpAction = {
type: 'NEW_PUBLIC_IP',
ip: string,
@@ -52,12 +46,21 @@ type NewLocationAction = {
newLocation: Location,
};
-export type ConnectionAction = ConnectionChangeAction
- | NewPublicIpAction
+type OnlineAction = {
+ type: 'ONLINE',
+};
+
+type OfflineAction = {
+ type: 'OFFLINE',
+};
+
+export type ConnectionAction = NewPublicIpAction
| NewLocationAction
| ConnectingAction
| ConnectedAction
- | DisconnectedAction;
+ | DisconnectedAction
+ | OnlineAction
+ | OfflineAction;
function connectingTo(serverAddress: string): ConnectingAction {
return {
@@ -84,13 +87,6 @@ function disconnected(): DisconnectedAction {
};
}
-function connectionChange(newData: $Shape<ConnectionReduxState>): ConnectionChangeAction {
- return {
- type: 'CONNECTION_CHANGE',
- newData: newData,
- };
-}
-
function newPublicIp(ip: string): NewPublicIpAction {
return {
type: 'NEW_PUBLIC_IP',
@@ -105,6 +101,18 @@ function newLocation(newLoc: Location): NewLocationAction {
};
}
+function online(): OnlineAction {
+ return {
+ type: 'ONLINE',
+ };
+}
+
+function offline(): OfflineAction {
+ return {
+ type: 'OFFLINE',
+ };
+}
+
-export default { connect, disconnect, copyIPAddress, connectionChange, newPublicIp, newLocation, connectingTo, connecting, connected, disconnected };
+export default { connect, disconnect, copyIPAddress, newPublicIp, newLocation, connectingTo, connecting, connected, disconnected, online, offline };
diff --git a/app/redux/connection/reducers.js b/app/redux/connection/reducers.js
index 457a6c1bd6..8f389df159 100644
--- a/app/redux/connection/reducers.js
+++ b/app/redux/connection/reducers.js
@@ -46,6 +46,12 @@ export default function(state: ConnectionReduxState = initialState, action: Redu
case 'DISCONNECTED':
return { ...state, ...{ status: 'disconnected' }};
+ case 'ONLINE':
+ return { ...state, ...{ isOnline: true }};
+
+ case 'OFFLINE':
+ return { ...state, ...{ isOnline: false }};
+
default:
return state;
}
diff --git a/test/reducers.spec.js b/test/reducers.spec.js
index 236497a057..3b759c4e9b 100644
--- a/test/reducers.spec.js
+++ b/test/reducers.spec.js
@@ -1,26 +1,12 @@
// @flow
import { expect } from 'chai';
-import connectionReducer from '../app/redux/connection/reducers';
import settingsReducer from '../app/redux/settings/reducers';
import { defaultServer } from '../app/config';
describe('reducers', () => {
const previousState: any = {};
- it('should handle CONNECTION_CHANGE', () => {
- const action = {
- type: 'CONNECTION_CHANGE',
- newData: {
- status: 'connected',
- serverAddress: '2.1.1.2',
- clientIp: '2.1.1.1'
- }
- };
- const test = Object.assign({}, action.newData);
- expect(connectionReducer(previousState, action)).to.deep.equal(test);
- });
-
it('should handle SETTINGS_UPDATE', () => {
const action = {
type: 'UPDATE_SETTINGS',