From 2492371b505865b8f5212b2e1fc86699d9ed3b35 Mon Sep 17 00:00:00 2001
From: Diego Mello <diegolmello@gmail.com>
Date: Fri, 3 May 2019 11:54:57 -0300
Subject: [PATCH] [FIX] Check if room is mounted before setting state (#864)

* 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
---
 app/views/RoomView/index.js      | 12 +++++++++++-
 app/views/RoomsListView/index.js | 14 +++++++++-----
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/app/views/RoomView/index.js b/app/views/RoomView/index.js
index 082563187..58915b3c8 100644
--- a/app/views/RoomView/index.js
+++ b/app/views/RoomView/index.js
@@ -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`);
 	}
 
@@ -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`);
 	}
@@ -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;
@@ -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`);
 	}
@@ -293,6 +300,9 @@ export default class RoomView extends LoggedView {
 	}
 
 	internalSetState = (...args) => {
+		if (!this.mounted) {
+			return;
+		}
 		if (isIOS && this.beginAnimating) {
 			LayoutAnimation.easeInEaseOut();
 		}
@@ -347,7 +357,7 @@ export default class RoomView extends LoggedView {
 		}
 	}
 
-	setLastOpen = lastOpen => this.setState({ lastOpen });
+	setLastOpen = lastOpen => this.internalSetState({ lastOpen });
 
 	joinRoom = async() => {
 		try {
diff --git a/app/views/RoomsListView/index.js b/app/views/RoomsListView/index.js
index a1a42124e..aa21eb0ce 100644
--- a/app/views/RoomsListView/index.js
+++ b/app/views/RoomsListView/index.js
@@ -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';
@@ -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() {
@@ -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`);
 	}
 
@@ -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>
 		);
 	}
-- 
GitLab