diff --git a/src/CONST.ts b/src/CONST.ts index 3d67a951111e..abeedbc0f9d5 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -941,6 +941,7 @@ const CONST = { COMMENT_LENGTH_DEBOUNCE_TIME: 500, SEARCH_OPTION_LIST_DEBOUNCE_TIME: 300, RESIZE_DEBOUNCE_TIME: 100, + UNREAD_UPDATE_DEBOUNCE_TIME: 300, }, SEARCH_TABLE_COLUMNS: { RECEIPT: 'receipt', diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index fd1fc3f8ee2e..7698433c33c1 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -1,3 +1,5 @@ +import debounce from 'lodash/debounce'; +import memoize from 'lodash/memoize'; import type {OnyxCollection} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; import * as ReportUtils from '@libs/ReportUtils'; @@ -35,13 +37,16 @@ export default function getUnreadReportsForUnreadIndicator(reports: OnyxCollecti ); } -const triggerUnreadUpdate = () => { +const memoizedGetUnreadReportsForUnreadIndicator = memoize(getUnreadReportsForUnreadIndicator); + +const triggerUnreadUpdate = debounce(() => { const currentReportID = navigationRef.isReady() ? Navigation.getTopmostReportId() ?? '-1' : '-1'; // We want to keep notification count consistent with what can be accessed from the LHN list - const unreadReports = getUnreadReportsForUnreadIndicator(allReports, currentReportID); + const unreadReports = memoizedGetUnreadReportsForUnreadIndicator(allReports, currentReportID); + updateUnread(unreadReports.length); -}; +}, CONST.TIMING.UNREAD_UPDATE_DEBOUNCE_TIME); Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT,