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

Fix: User experienced big delay when posting the messages #26729

Merged
merged 7 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/libs/HttpUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ Onyx.connect({
// We use the AbortController API to terminate pending request in `cancelPendingRequests`
let cancellationController = new AbortController();

/**
* API comamnds we need to calculate skew,
waterim marked this conversation as resolved.
Show resolved Hide resolved
* Regex to get API command from the command
*/
const addSkewList = ['OpenReport', 'ReconnectApp', 'OpenApp'];
const regex = /[?&]command=([^&]+)/;
waterim marked this conversation as resolved.
Show resolved Hide resolved

/**
* Send an HTTP request, and attempt to resolve the json response.
* If there is a network error, we'll set the application offline.
Expand All @@ -33,12 +40,26 @@ let cancellationController = new AbortController();
* @returns {Promise}
*/
function processHTTPRequest(url, method = 'get', body = null, canCancel = true) {
const startTime = new Date();
waterim marked this conversation as resolved.
Show resolved Hide resolved

return fetch(url, {
// We hook requests to the same Controller signal, so we can cancel them all at once
signal: canCancel ? cancellationController.signal : undefined,
method,
body,
})
.then((response) => {
const match = url.match(regex)[1];
if (addSkewList.includes(match)) {
const serverTime = new Date(response.headers.get('Date'));
const endTime = new Date();
waterim marked this conversation as resolved.
Show resolved Hide resolved
const latency = (endTime.valueOf() - startTime.valueOf()) / 2;
const skew = serverTime.valueOf() - startTime.valueOf() + latency;
waterim marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line rulesdir/prefer-actions-set-data
Onyx.merge(ONYXKEYS.NETWORK, {timeSkew: skew});
waterim marked this conversation as resolved.
Show resolved Hide resolved
}
return response;
})
.then((response) => {
// Test mode where all requests will succeed in the server, but fail to return a response
if (shouldFailAllRequests || shouldForceOffline) {
Expand Down
10 changes: 8 additions & 2 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ Onyx.connect({
callback: (val) => (loginList = val),
});

let networkTimeSkew;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (val) => (networkTimeSkew = lodashGet(val, 'timeSkew', 0)),
});

function getChatType(report) {
return report ? report.chatType : '';
}
Expand Down Expand Up @@ -1775,7 +1781,7 @@ function buildOptimisticAddCommentReportAction(text, file) {

// Remove HTML from text when applying optimistic offline comment
const textForNewComment = isAttachment ? CONST.ATTACHMENT_MESSAGE_TEXT : parser.htmlToText(htmlForNewComment);

const timestamp = new Date().valueOf() + networkTimeSkew;
return {
commentText,
reportAction: {
Expand All @@ -1791,7 +1797,7 @@ function buildOptimisticAddCommentReportAction(text, file) {
],
automatic: false,
avatar: lodashGet(allPersonalDetails, [currentUserAccountID, 'avatar'], UserUtils.getDefaultAvatarURL(currentUserAccountID)),
created: DateUtils.getDBTime(),
created: DateUtils.getDBTime(timestamp),
message: [
{
translationKey: isAttachment ? CONST.TRANSLATION_KEYS.ATTACHMENT : '',
Expand Down
11 changes: 7 additions & 4 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ Onyx.connect({
});

let isNetworkOffline = false;
let networkTimeSkew;
waterim marked this conversation as resolved.
Show resolved Hide resolved
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (val) => (isNetworkOffline = lodashGet(val, 'isOffline', false)),
callback: (val) => {
isNetworkOffline = lodashGet(val, 'isOffline', false);
networkTimeSkew = lodashGet(val, 'timeSkew', 0);
},
});

let allPersonalDetails;
Expand Down Expand Up @@ -251,9 +255,8 @@ function addActions(reportID, text = '', file) {

// Always prefer the file as the last action over text
const lastAction = attachmentAction || reportCommentAction;

const currentTime = DateUtils.getDBTime();

const timestamp = new Date().valueOf() + networkTimeSkew;
waterim marked this conversation as resolved.
Show resolved Hide resolved
const currentTime = DateUtils.getDBTime(timestamp);
const lastCommentText = ReportUtils.formatReportLastMessageText(lastAction.message[0].text);

const optimisticReport = {
Expand Down
3 changes: 3 additions & 0 deletions src/types/onyx/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ type Network = {

/** Whether we should fail all network requests */
shouldFailAllRequests?: boolean;

/** Skew between the client and server clocks */
timeSkew?: number;
};

export default Network;
Loading