Skip to content

Commit

Permalink
queueMarkAsRead: Set timeout if there is mark request in less than 2s.
Browse files Browse the repository at this point in the history
If there are multiple requests to mark messages as read with interval
less than 2s, and if there is no further request then later were
stuck in the queue. Becuase there was only one caller to
`messagesFlags`, which was only called by user scroll event.
So even after reaching at the end of message list, unread banner is
visible with some unread count.

So now set timeout to send read message flag to server.

Fixes: zulip#3509
  • Loading branch information
jainkuniya committed Jul 18, 2019
1 parent c5b9370 commit 0a01465
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/api/queueMarkAsRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
import type { Auth } from './transportTypes';
import messagesFlags from './messages/messagesFlags';

const TIME_INTERVAL_BETWEEN_CONSECUTIVE_CALLS_MS = 2000;
let unsentMessageIds = [];
let lastSentTime = 0;
let timeout = null;

const updateMessageFlags = (auth: Auth) => {
messagesFlags(auth, unsentMessageIds, 'add', 'read');
unsentMessageIds = [];
lastSentTime = Date.now();
};

export default (auth: Auth, messageIds: number[]): void => {
unsentMessageIds.push(...messageIds);

if (Date.now() - lastSentTime > 2000) {
messagesFlags(auth, unsentMessageIds, 'add', 'read');
unsentMessageIds = [];
lastSentTime = Date.now();
if (Date.now() - lastSentTime > TIME_INTERVAL_BETWEEN_CONSECUTIVE_CALLS_MS) {
updateMessageFlags(auth);
} else if (timeout === null) {
timeout = setTimeout(() => {
updateMessageFlags(auth);
timeout = null;
}, TIME_INTERVAL_BETWEEN_CONSECUTIVE_CALLS_MS);
}
};

0 comments on commit 0a01465

Please sign in to comment.