[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 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();
let promises;
let connectedListener;
let disconnectedListener;
let notifyRoomListener;
let messageReceivedListener;
export default function subscribeRoom({ rid }) {
console.log(`[RCRN] Subscribed to room ${ rid }`);
let promises;
let connectedListener;
let disconnectedListener;
let notifyRoomListener;
let messageReceivedListener;
const handleConnection = () => {
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) {
promises.then(unsubscribe);
try {
params = await promises;
await unsubscribe(params);
} catch (error) {
// Do nothing
}
promises = false;
}
if (connectedListener) {
connectedListener.then(removeListener);
params = await connectedListener;
removeListener(params);
connectedListener = false;
}
if (disconnectedListener) {
disconnectedListener.then(removeListener);
params = await disconnectedListener;
removeListener(params);
disconnectedListener = false;
}
if (notifyRoomListener) {
notifyRoomListener.then(removeListener);
params = await notifyRoomListener;
removeListener(params);
notifyRoomListener = false;
}
if (messageReceivedListener) {
messageReceivedListener.then(removeListener);
params = await messageReceivedListener;
removeListener(params);
messageReceivedListener = false;
}
reduxStore.dispatch(clearUserTyping());
@ -226,11 +237,7 @@ export default function subscribeRoom({ rid }) {
notifyRoomListener = this.sdk.onStreamData('stream-notify-room', handleNotifyRoomReceived);
messageReceivedListener = this.sdk.onStreamData('stream-room-messages', handleMessageReceived);
try {
promises = this.sdk.subscribeRoom(rid);
} catch (e) {
log(e);
}
return {
stop: () => stop()

View File

@ -287,7 +287,7 @@ class RoomView extends React.Component {
}
}
}
this.unsubscribe();
await this.unsubscribe();
if (this.didFocusListener && this.didFocusListener.remove) {
this.didFocusListener.remove();
}
@ -324,10 +324,9 @@ class RoomView extends React.Component {
}
// eslint-disable-next-line react/sort-comp
init = () => {
init = async() => {
try {
this.setState({ loading: true });
this.initInteraction = InteractionManager.runAfterInteractions(async() => {
const { room, joined } = this.state;
if (this.tmid) {
await this.getThreadMessages();
@ -343,8 +342,8 @@ class RoomView extends React.Component {
this.setLastOpen(null);
}
RocketChat.readMessages(room.rid, newLastOpen).catch(e => console.log(e));
this.unsubscribe();
this.sub = await RocketChat.subscribeRoom(room);
await this.unsubscribe();
this.sub = RocketChat.subscribeRoom(room);
}
}
@ -352,10 +351,14 @@ class RoomView extends React.Component {
// in case of a missing connection or poor connection on room open
const canAutoTranslate = await RocketChat.canAutoTranslate();
this.setState({ canAutoTranslate, loading: false });
});
} catch (e) {
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) {
this.sub.stop();
await this.sub.stop();
}
}
@ -568,27 +571,16 @@ class RoomView extends React.Component {
return ((room.prid || useRealName) && room.fname) || room.name;
}
getMessages = async() => {
getMessages = () => {
const { room } = this.state;
try {
if (room.lastOpen) {
await RocketChat.loadMissedMessages(room);
return RocketChat.loadMissedMessages(room);
} else {
await RocketChat.loadMessagesForRoom(room);
}
return Promise.resolve();
} catch (e) {
log(e);
return RocketChat.loadMessagesForRoom(room);
}
}
getThreadMessages = () => {
try {
return RocketChat.loadThreadMessages({ tmid: this.tmid, rid: this.rid });
} catch (e) {
log(e);
}
}
getThreadMessages = () => RocketChat.loadThreadMessages({ tmid: this.tmid, rid: this.rid })
getCustomEmoji = (name) => {
const { customEmojis } = this.props;