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 Aug 11, 2019
1 parent b1052b2 commit ff4328d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/api/__tests__/queueMarkAsRead-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ describe('queueMarkAsRead', () => {

expect(messagesFlags.default).toHaveBeenCalledTimes(2);
});

test('should call messagesFlags after 2s to clear queue', async () => {
queueMarkAsRead({}, [1, 2, 3]);
queueMarkAsRead({}, [4, 5, 6]);

await sleep(3000);

expect(messagesFlags.default).toHaveBeenCalledTimes(2);
});
});
12 changes: 10 additions & 2 deletions src/api/queueMarkAsRead.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
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;

/**
* Exported so that it can be used in test
Expand All @@ -12,13 +14,19 @@ let lastSentTime = 0;
export const resetAll = () => {
unsentMessageIds = [];
lastSentTime = 0;
timeout = null;
};

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

Expand Down

0 comments on commit ff4328d

Please sign in to comment.