Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure outbox messages are sent in the correct order #3272

Merged
merged 4 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 2 additions & 8 deletions src/boot/AppEventHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import type {
Orientation as OrientationT,
UserIdMap,
} from '../types';
import { getSession, getUnreadByHuddlesMentionsAndPMs, getUsersById } from '../selectors';
import { getUnreadByHuddlesMentionsAndPMs, getUsersById } from '../selectors';
import { handleInitialNotification, NotificationListener } from '../notification';
import {
appOnline,
appOrientation,
appState,
initSafeAreaInsets,
reportPresence,
trySendMessages,
} from '../actions';

const componentStyles = StyleSheet.create({
Expand All @@ -33,7 +32,6 @@ const componentStyles = StyleSheet.create({
});

type Props = {|
needsInitialFetch: boolean,
dispatch: Dispatch,
children: ChildrenArray<*>,
unreadCount: number,
Expand All @@ -47,12 +45,9 @@ class AppEventHandlers extends PureComponent<Props> {
};

handleConnectivityChange = connectionInfo => {
const { dispatch, needsInitialFetch } = this.props;
const { dispatch } = this.props;
const isConnected = connectionInfo.type !== 'none' && connectionInfo.type !== 'unknown';
dispatch(appOnline(isConnected));
if (!needsInitialFetch && isConnected) {
dispatch(trySendMessages());
}
};

handleAppStateChange = state => {
Expand Down Expand Up @@ -100,7 +95,6 @@ class AppEventHandlers extends PureComponent<Props> {
}

export default connect((state: GlobalState) => ({
needsInitialFetch: getSession(state).needsInitialFetch,
usersById: getUsersById(state),
unreadCount: getUnreadByHuddlesMentionsAndPMs(state),
}))(AppEventHandlers);
4 changes: 2 additions & 2 deletions src/message/fetchActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { ALL_PRIVATE_NARROW } from '../utils/narrow';
import { tryUntilSuccessful } from '../utils/async';
import { getFetchedMessagesForNarrow } from '../chat/narrowsSelectors';
import { initNotifications } from '../notification/notificationActions';
import { addToOutbox, trySendMessages } from '../outbox/outboxActions';
import { addToOutbox, sendOutbox } from '../outbox/outboxActions';
import { realmInit } from '../realm/realmActions';
import { initStreams } from '../streams/streamsActions';
import { reportPresence } from '../users/usersActions';
Expand Down Expand Up @@ -196,7 +196,7 @@ export const fetchInitialData = () => async (dispatch: Dispatch, getState: GetSt
dispatch(fetchMessagesInNarrow(session.lastNarrow));
}

dispatch(trySendMessages());
dispatch(sendOutbox());
};

export const doInitialFetch = () => async (dispatch: Dispatch, getState: GetState) => {
Expand Down
34 changes: 22 additions & 12 deletions src/outbox/outboxActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { sendMessage } from '../api';
import { getSelfUserDetail } from '../users/userSelectors';
import { getUserByEmail, getUsersAndWildcards } from '../users/userHelpers';
import { isStreamNarrow, isPrivateOrGroupNarrow } from '../utils/narrow';
import progressiveTimeout from '../utils/progressiveTimeout';

export const messageSendStart = (outbox: Outbox): MessageSendStartAction => ({
type: MESSAGE_SEND_START,
Expand All @@ -47,16 +48,12 @@ export const messageSendComplete = (localMessageId: number): MessageSendComplete
local_message_id: localMessageId,
});

export const trySendMessages = () => (dispatch: Dispatch, getState: GetState) => {
export const trySendMessages = (dispatch: Dispatch, getState: GetState): boolean => {
const state = getState();
if (state.outbox.length === 0 || state.session.outboxSending) {
return;
}
dispatch(toggleOutboxSending(true));
const auth = getAuth(state);
const outboxToSend = state.outbox.filter(outbox => !outbox.isSent);
outboxToSend.forEach(async item => {
try {
try {
outboxToSend.forEach(async item => {
await sendMessage(
auth,
item.type,
Expand All @@ -67,10 +64,23 @@ export const trySendMessages = () => (dispatch: Dispatch, getState: GetState) =>
state.session.eventQueueId,
);
dispatch(messageSendComplete(item.timestamp));
} catch (e) {
logErrorRemotely(e, 'error caught while sending');
}
});
});
return true;
} catch (e) {
logErrorRemotely(e, 'error caught while sending');
return false;
}
};

export const sendOutbox = () => async (dispatch: Dispatch, getState: GetState) => {
const state = getState();
if (state.outbox.length === 0 || state.session.outboxSending) {
return;
}
dispatch(toggleOutboxSending(true));
while (!trySendMessages(dispatch, getState)) {
await progressiveTimeout(); // eslint-disable-line no-await-in-loop
}
dispatch(toggleOutboxSending(false));
};

Expand Down Expand Up @@ -140,5 +150,5 @@ export const addToOutbox = (narrow: Narrow, content: string) => async (
reactions: [],
}),
);
dispatch(trySendMessages());
dispatch(sendOutbox());
};