Skip to content

Commit

Permalink
[FIX] Check if room is mounted before setting state (#864)
Browse files Browse the repository at this point in the history
* Tweaks on sequential threads messages

* Update tests

* Fix quote

* Prevent from deleting thread start message when positioned inside the thread

* Remove thread listener from RightButtons

* Fix error on thread start parse

* Stop parsing threads on render

* Check replied thread only if necessary

* Fix messages don't displaying

* Fix threads e2e

* RoomsListView.updateState slice

* Stop fetching hidden messages on threads

* Check if RoomView is mounted before rendering

* Refactor navigation events on RoomsListView

* Fix lint

* Fix listener
  • Loading branch information
diegolmello committed May 3, 2019
1 parent a243b1c commit 2492371
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
12 changes: 11 additions & 1 deletion app/views/RoomView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ export default class RoomView extends LoggedView {
this.beginAnimatingTimeout = setTimeout(() => this.beginAnimating = true, 300);
this.messagebox = React.createRef();
safeAddListener(this.rooms, this.updateRoom);
this.willBlurListener = props.navigation.addListener('willBlur', () => this.mounted = false);
this.mounted = false;
console.timeEnd(`${ this.constructor.name } init`);
}

Expand All @@ -139,6 +141,7 @@ export default class RoomView extends LoggedView {
} else {
EventEmitter.addEventListener('connected', this.handleConnected);
}
this.mounted = true;
});
console.timeEnd(`${ this.constructor.name } mount`);
}
Expand Down Expand Up @@ -188,6 +191,7 @@ export default class RoomView extends LoggedView {
}

componentWillUnmount() {
this.mounted = false;
const { editing, replying } = this.props;
if (!editing && this.messagebox && this.messagebox.current && this.messagebox.current.text) {
const { text } = this.messagebox.current;
Expand Down Expand Up @@ -230,6 +234,9 @@ export default class RoomView extends LoggedView {
if (this.initInteraction && this.initInteraction.cancel) {
this.initInteraction.cancel();
}
if (this.willBlurListener && this.willBlurListener.remove) {
this.willBlurListener.remove();
}
EventEmitter.removeListener('connected', this.handleConnected);
console.countReset(`${ this.constructor.name }.render calls`);
}
Expand Down Expand Up @@ -293,6 +300,9 @@ export default class RoomView extends LoggedView {
}

internalSetState = (...args) => {
if (!this.mounted) {
return;
}
if (isIOS && this.beginAnimating) {
LayoutAnimation.easeInEaseOut();
}
Expand Down Expand Up @@ -347,7 +357,7 @@ export default class RoomView extends LoggedView {
}
}

setLastOpen = lastOpen => this.setState({ lastOpen });
setLastOpen = lastOpen => this.internalSetState({ lastOpen });

joinRoom = async() => {
try {
Expand Down
14 changes: 9 additions & 5 deletions app/views/RoomsListView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'react-native';
import { connect } from 'react-redux';
import { isEqual } from 'lodash';
import { SafeAreaView, NavigationEvents } from 'react-navigation';
import { SafeAreaView } from 'react-navigation';
import Orientation from 'react-native-orientation-locker';

import database, { safeAddListener } from '../../lib/realm';
Expand Down Expand Up @@ -134,6 +134,8 @@ export default class RoomsListView extends LoggedView {
livechat: []
};
Orientation.unlockAllOrientations();
this.didFocusListener = props.navigation.addListener('didFocus', () => BackHandler.addEventListener('hardwareBackPress', this.handleBackPress));
this.willBlurListener = props.navigation.addListener('willBlur', () => BackHandler.addEventListener('hardwareBackPress', this.handleBackPress));
}

componentDidMount() {
Expand Down Expand Up @@ -210,6 +212,12 @@ export default class RoomsListView extends LoggedView {
if (this.updateStateInteraction && this.updateStateInteraction.cancel) {
this.updateStateInteraction.cancel();
}
if (this.didFocusListener && this.didFocusListener.remove) {
this.didFocusListener.remove();
}
if (this.willBlurListener && this.willBlurListener.remove) {
this.willBlurListener.remove();
}
console.countReset(`${ this.constructor.name }.render calls`);
}

Expand Down Expand Up @@ -558,10 +566,6 @@ export default class RoomsListView extends LoggedView {
: null
}
{showServerDropdown ? <ServerDropdown /> : null}
<NavigationEvents
onDidFocus={() => BackHandler.addEventListener('hardwareBackPress', this.handleBackPress)}
onWillBlur={() => BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress)}
/>
</SafeAreaView>
);
}
Expand Down

0 comments on commit 2492371

Please sign in to comment.