Skip to content

Commit

Permalink
Merge pull request #43824 from callstack-internal/fix/43276-unnecessa…
Browse files Browse the repository at this point in the history
…ry-calls-off-triggerUnreadUpdate

add debounce and memoization to triggerUnreadUpdate
  • Loading branch information
mountiny authored Jun 19, 2024
2 parents 768da63 + 49336df commit af9be0a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
11 changes: 8 additions & 3 deletions src/libs/UnreadIndicatorUpdater/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit af9be0a

Please sign in to comment.