[FIX] Missing messages after reconnect (#1470)

This commit is contained in:
Diego Mello 2019-12-13 13:23:20 -03:00 committed by GitHub
parent a3822d4941
commit 7b18bf68d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 61 deletions

View File

@ -10,16 +10,17 @@ import reduxStore from '../../createStore';
import { addUserTyping, removeUserTyping, clearUserTyping } from '../../../actions/usersTyping'; import { addUserTyping, removeUserTyping, clearUserTyping } from '../../../actions/usersTyping';
import debounce from '../../../utils/debounce'; import debounce from '../../../utils/debounce';
const unsubscribe = subscriptions => subscriptions.forEach(sub => sub.unsubscribe().catch(() => console.log('unsubscribeRoom'))); const unsubscribe = (subscriptions = []) => Promise.all(subscriptions.map(sub => sub.unsubscribe));
const removeListener = listener => listener.stop(); const removeListener = listener => listener.stop();
let promises;
let connectedListener;
let disconnectedListener;
let notifyRoomListener;
let messageReceivedListener;
export default function subscribeRoom({ rid }) { export default function subscribeRoom({ rid }) {
console.log(`[RCRN] Subscribed to room ${ rid }`); console.log(`[RCRN] Subscribed to room ${ rid }`);
let promises;
let connectedListener;
let disconnectedListener;
let notifyRoomListener;
let messageReceivedListener;
const handleConnection = () => { const handleConnection = () => {
this.loadMissedMessages({ rid }).catch(e => console.log(e)); this.loadMissedMessages({ rid }).catch(e => console.log(e));
@ -197,25 +198,35 @@ export default function subscribeRoom({ rid }) {
}); });
}); });
const stop = () => { const stop = async() => {
let params;
if (promises) { if (promises) {
promises.then(unsubscribe); try {
params = await promises;
await unsubscribe(params);
} catch (error) {
// Do nothing
}
promises = false; promises = false;
} }
if (connectedListener) { if (connectedListener) {
connectedListener.then(removeListener); params = await connectedListener;
removeListener(params);
connectedListener = false; connectedListener = false;
} }
if (disconnectedListener) { if (disconnectedListener) {
disconnectedListener.then(removeListener); params = await disconnectedListener;
removeListener(params);
disconnectedListener = false; disconnectedListener = false;
} }
if (notifyRoomListener) { if (notifyRoomListener) {
notifyRoomListener.then(removeListener); params = await notifyRoomListener;
removeListener(params);
notifyRoomListener = false; notifyRoomListener = false;
} }
if (messageReceivedListener) { if (messageReceivedListener) {
messageReceivedListener.then(removeListener); params = await messageReceivedListener;
removeListener(params);
messageReceivedListener = false; messageReceivedListener = false;
} }
reduxStore.dispatch(clearUserTyping()); reduxStore.dispatch(clearUserTyping());
@ -226,11 +237,7 @@ export default function subscribeRoom({ rid }) {
notifyRoomListener = this.sdk.onStreamData('stream-notify-room', handleNotifyRoomReceived); notifyRoomListener = this.sdk.onStreamData('stream-notify-room', handleNotifyRoomReceived);
messageReceivedListener = this.sdk.onStreamData('stream-room-messages', handleMessageReceived); messageReceivedListener = this.sdk.onStreamData('stream-room-messages', handleMessageReceived);
try { promises = this.sdk.subscribeRoom(rid);
promises = this.sdk.subscribeRoom(rid);
} catch (e) {
log(e);
}
return { return {
stop: () => stop() stop: () => stop()

View File

@ -287,7 +287,7 @@ class RoomView extends React.Component {
} }
} }
} }
this.unsubscribe(); await this.unsubscribe();
if (this.didFocusListener && this.didFocusListener.remove) { if (this.didFocusListener && this.didFocusListener.remove) {
this.didFocusListener.remove(); this.didFocusListener.remove();
} }
@ -324,38 +324,41 @@ class RoomView extends React.Component {
} }
// eslint-disable-next-line react/sort-comp // eslint-disable-next-line react/sort-comp
init = () => { init = async() => {
try { try {
this.setState({ loading: true }); this.setState({ loading: true });
this.initInteraction = InteractionManager.runAfterInteractions(async() => { const { room, joined } = this.state;
const { room, joined } = this.state; if (this.tmid) {
if (this.tmid) { await this.getThreadMessages();
await this.getThreadMessages(); } else {
} else { const newLastOpen = new Date();
const newLastOpen = new Date(); await this.getMessages(room);
await this.getMessages(room);
// if room is joined // if room is joined
if (joined) { if (joined) {
if (room.alert || room.unread || room.userMentions) { if (room.alert || room.unread || room.userMentions) {
this.setLastOpen(room.ls); this.setLastOpen(room.ls);
} else { } else {
this.setLastOpen(null); this.setLastOpen(null);
}
RocketChat.readMessages(room.rid, newLastOpen).catch(e => console.log(e));
this.unsubscribe();
this.sub = await RocketChat.subscribeRoom(room);
} }
RocketChat.readMessages(room.rid, newLastOpen).catch(e => console.log(e));
await this.unsubscribe();
this.sub = RocketChat.subscribeRoom(room);
} }
}
// We run `canAutoTranslate` again in order to refetch auto translate permission // We run `canAutoTranslate` again in order to refetch auto translate permission
// in case of a missing connection or poor connection on room open // in case of a missing connection or poor connection on room open
const canAutoTranslate = await RocketChat.canAutoTranslate(); const canAutoTranslate = await RocketChat.canAutoTranslate();
this.setState({ canAutoTranslate, loading: false }); this.setState({ canAutoTranslate, loading: false });
});
} catch (e) { } catch (e) {
this.setState({ loading: false }); this.setState({ loading: false });
log(e); this.retryInit = this.retryInit + 1 || 1;
if (this.retryInit <= 1) {
this.retryInitTimeout = setTimeout(() => {
this.init();
}, 300);
}
} }
} }
@ -386,9 +389,9 @@ class RoomView extends React.Component {
} }
} }
unsubscribe = () => { unsubscribe = async() => {
if (this.sub && this.sub.stop) { if (this.sub && this.sub.stop) {
this.sub.stop(); await this.sub.stop();
} }
} }
@ -568,27 +571,16 @@ class RoomView extends React.Component {
return ((room.prid || useRealName) && room.fname) || room.name; return ((room.prid || useRealName) && room.fname) || room.name;
} }
getMessages = async() => { getMessages = () => {
const { room } = this.state; const { room } = this.state;
try { if (room.lastOpen) {
if (room.lastOpen) { return RocketChat.loadMissedMessages(room);
await RocketChat.loadMissedMessages(room); } else {
} else { return RocketChat.loadMessagesForRoom(room);
await RocketChat.loadMessagesForRoom(room);
}
return Promise.resolve();
} catch (e) {
log(e);
} }
} }
getThreadMessages = () => { getThreadMessages = () => RocketChat.loadThreadMessages({ tmid: this.tmid, rid: this.rid })
try {
return RocketChat.loadThreadMessages({ tmid: this.tmid, rid: this.rid });
} catch (e) {
log(e);
}
}
getCustomEmoji = (name) => { getCustomEmoji = (name) => {
const { customEmojis } = this.props; const { customEmojis } = this.props;