[FIX] ThreadMessagesView crashing on load (#1997)

This commit is contained in:
Diego Mello 2020-04-06 16:07:40 -03:00 committed by GitHub
parent afb68c74c1
commit 0298c38b3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 17 deletions

View File

@ -71,12 +71,10 @@ class ThreadMessagesView extends React.Component {
} }
componentWillUnmount() { componentWillUnmount() {
console.countReset(`${ this.constructor.name }.render calls`);
if (this.mountInteraction && this.mountInteraction.cancel) { if (this.mountInteraction && this.mountInteraction.cancel) {
this.mountInteraction.cancel(); this.mountInteraction.cancel();
} }
if (this.loadInteraction && this.loadInteraction.cancel) {
this.loadInteraction.cancel();
}
if (this.syncInteraction && this.syncInteraction.cancel) { if (this.syncInteraction && this.syncInteraction.cancel) {
this.syncInteraction.cancel(); this.syncInteraction.cancel();
} }
@ -89,13 +87,14 @@ class ThreadMessagesView extends React.Component {
} }
// eslint-disable-next-line react/sort-comp // eslint-disable-next-line react/sort-comp
subscribeData = () => { subscribeData = async() => {
try { try {
const db = database.active; const db = database.active;
this.subObservable = db.collections const subscription = await db.collections
.get('subscriptions') .get('subscriptions')
.findAndObserve(this.rid); .find(this.rid);
this.subSubscription = this.subObservable const observable = subscription.observe();
this.subSubscription = observable
.subscribe((data) => { .subscribe((data) => {
this.subscription = data; this.subscription = data;
}); });
@ -116,14 +115,14 @@ class ThreadMessagesView extends React.Component {
} }
}); });
} catch (e) { } catch (e) {
log(e); // Do nothing
} }
} }
// eslint-disable-next-line react/sort-comp // eslint-disable-next-line react/sort-comp
init = () => { init = () => {
if (!this.subscription) { if (!this.subscription) {
return; this.load();
} }
try { try {
const lastThreadSync = new Date(); const lastThreadSync = new Date();
@ -138,6 +137,13 @@ class ThreadMessagesView extends React.Component {
} }
updateThreads = async({ update, remove, lastThreadSync }) => { updateThreads = async({ update, remove, lastThreadSync }) => {
// if there's no subscription, manage data on this.state.messages
// note: sync will never be called without subscription
if (!this.subscription) {
this.setState(({ messages }) => ({ messages: [...messages, ...update] }));
return;
}
try { try {
const db = database.active; const db = database.active;
const threadsCollection = db.collections.get('threads'); const threadsCollection = db.collections.get('threads');
@ -198,14 +204,11 @@ class ThreadMessagesView extends React.Component {
rid: this.rid, count: API_FETCH_COUNT, offset: messages.length rid: this.rid, count: API_FETCH_COUNT, offset: messages.length
}); });
if (result.success) { if (result.success) {
this.loadInteraction = InteractionManager.runAfterInteractions(() => {
this.updateThreads({ update: result.threads, lastThreadSync }); this.updateThreads({ update: result.threads, lastThreadSync });
this.setState({ this.setState({
loading: false, loading: false,
end: result.count < API_FETCH_COUNT end: result.count < API_FETCH_COUNT
}); });
});
} }
} catch (e) { } catch (e) {
log(e); log(e);
@ -319,6 +322,7 @@ class ThreadMessagesView extends React.Component {
} }
render() { render() {
console.count(`${ this.constructor.name }.render calls`);
const { loading, messages } = this.state; const { loading, messages } = this.state;
const { theme } = this.props; const { theme } = this.props;
@ -335,7 +339,7 @@ class ThreadMessagesView extends React.Component {
renderItem={this.renderItem} renderItem={this.renderItem}
style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]} style={[styles.list, { backgroundColor: themes[theme].backgroundColor }]}
contentContainerStyle={styles.contentContainer} contentContainerStyle={styles.contentContainer}
keyExtractor={item => item.id} keyExtractor={item => item._id}
onEndReached={this.load} onEndReached={this.load}
onEndReachedThreshold={0.5} onEndReachedThreshold={0.5}
maxToRenderPerBatch={5} maxToRenderPerBatch={5}