From 0a01465571e4af36977f3296b24e317edf8cffcc Mon Sep 17 00:00:00 2001 From: jainkuniya Date: Fri, 19 Jul 2019 00:29:06 +0530 Subject: [PATCH] queueMarkAsRead: Set timeout if there is mark request in less than 2s. 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: #3509 --- src/api/queueMarkAsRead.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/api/queueMarkAsRead.js b/src/api/queueMarkAsRead.js index 49b68bde711..77c7aecd742 100644 --- a/src/api/queueMarkAsRead.js +++ b/src/api/queueMarkAsRead.js @@ -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); } };