From 2b49784ae5b7f535b91a3dc79584e951581758da Mon Sep 17 00:00:00 2001 From: Github Date: Mon, 17 Jun 2024 13:37:18 +0200 Subject: [PATCH 1/2] add debounce and memoization to triggerUnreadUpdate --- src/libs/UnreadIndicatorUpdater/index.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/libs/UnreadIndicatorUpdater/index.ts b/src/libs/UnreadIndicatorUpdater/index.ts index fd1fc3f8ee2e..05754a960e40 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); -}; +}, 300); Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT, From 49336df93f962aa3dc4868bc77dd6acdd54abef3 Mon Sep 17 00:00:00 2001 From: Github Date: Tue, 18 Jun 2024 09:36:45 +0200 Subject: [PATCH 2/2] extract debounce time to const --- src/CONST.ts | 1 + src/libs/UnreadIndicatorUpdater/index.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CONST.ts b/src/CONST.ts index 9311816c38a2..19bd95a7c917 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -940,6 +940,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 05754a960e40..7698433c33c1 100644 --- a/src/libs/UnreadIndicatorUpdater/index.ts +++ b/src/libs/UnreadIndicatorUpdater/index.ts @@ -46,7 +46,7 @@ const triggerUnreadUpdate = debounce(() => { const unreadReports = memoizedGetUnreadReportsForUnreadIndicator(allReports, currentReportID); updateUnread(unreadReports.length); -}, 300); +}, CONST.TIMING.UNREAD_UPDATE_DEBOUNCE_TIME); Onyx.connect({ key: ONYXKEYS.COLLECTION.REPORT,