MM-9857 Disconnect WS for Android when sent to the background (#1511)

This commit is contained in:
Elias Nahum 2018-03-16 16:20:27 +02:00 committed by GitHub
parent cc64cc5dc6
commit df1df76eae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,6 +5,7 @@ import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {injectIntl, intlShape} from 'react-intl';
import {
AppState,
Platform,
View,
} from 'react-native';
@ -66,6 +67,10 @@ class Channel extends PureComponent {
}
componentDidMount() {
if (Platform.OS === 'android') {
AppState.addEventListener('change', this.handleAppStateChange);
}
if (tracker.initialLoad) {
this.props.actions.recordLoadTime('Start time', 'initialLoad');
}
@ -95,6 +100,10 @@ class Channel extends PureComponent {
EventEmitter.off('leave_team', this.handleLeaveTeam);
this.networkListener.removeEventListener();
if (Platform.OS === 'android') {
AppState.removeEventListener('change', this.handleAppStateChange);
}
closeWebSocket();
stopPeriodicStatusUpdates();
}
@ -141,6 +150,25 @@ class Channel extends PureComponent {
}
});
handleAppStateChange = async (appState) => {
const {actions} = this.props;
const {
closeWebSocket,
initWebSocket,
startPeriodicStatusUpdates,
stopPeriodicStatusUpdates,
} = actions;
const isActive = appState === 'active';
if (isActive) {
initWebSocket(Platform.OS);
startPeriodicStatusUpdates();
} else {
closeWebSocket(true);
stopPeriodicStatusUpdates();
}
};
handleConnectionChange = (isConnected) => {
const {actions} = this.props;
const {