Skip to content

Commit

Permalink
Merge pull request Expensify#36565 from callstack-internal/audit/impl…
Browse files Browse the repository at this point in the history
…ementation/get-ordered-raports-ids-cache-removal

[Audit][Implementation] getOrderedReportIDs - cache removal
  • Loading branch information
mountiny authored Feb 20, 2024
2 parents 3601df8 + 035d65a commit 12fc2cb
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 38 deletions.
37 changes: 0 additions & 37 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ function compareStringDates(a: string, b: string): 0 | 1 | -1 {
return 0;
}

// Define a cache object to store the memoized results
const reportIDsCache = new Map<string, string[]>();

// Function to set a key-value pair while maintaining the maximum key limit
function setWithLimit<TKey, TValue>(map: Map<TKey, TValue>, key: TKey, value: TValue) {
if (map.size >= 5) {
// If the map has reached its limit, remove the first (oldest) key-value pair
const firstKey = map.keys().next().value;
map.delete(firstKey);
}
map.set(key, value);
}

// Variable to verify if ONYX actions are loaded
let hasInitialReportActions = false;

/**
* @returns An array of reportIDs sorted in the proper order
*/
Expand All @@ -89,26 +73,6 @@ function getOrderedReportIDs(
currentPolicyID = '',
policyMemberAccountIDs: number[] = [],
): string[] {
const currentReportActions = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${currentReportId}`];
let reportActionCount = currentReportActions?.length ?? 0;
reportActionCount = Math.max(reportActionCount, 1);

// Generate a unique cache key based on the function arguments
const cachedReportsKey = JSON.stringify(
[currentReportId, allReports, betas, policies, priorityMode, reportActionCount, currentPolicyID, policyMemberAccountIDs],
// Exclude some properties not to overwhelm a cached key value with huge data, which we don't need to store in a cacheKey
(key, value: unknown) => (['participantAccountIDs', 'participants', 'lastMessageText', 'visibleChatMemberAccountIDs'].includes(key) ? undefined : value),
);

// Check if the result is already in the cache
const cachedIDs = reportIDsCache.get(cachedReportsKey);
if (cachedIDs && hasInitialReportActions) {
return cachedIDs;
}

// This is needed to prevent caching when Onyx is empty for a second render
hasInitialReportActions = Object.values(lastReportActions).length > 0;

const isInGSDMode = priorityMode === CONST.PRIORITY_MODE.GSD;
const isInDefaultMode = !isInGSDMode;
const allReportsDictValues = Object.values(allReports);
Expand Down Expand Up @@ -197,7 +161,6 @@ function getOrderedReportIDs(
// Now that we have all the reports grouped and sorted, they must be flattened into an array and only return the reportID.
// The order the arrays are concatenated in matters and will determine the order that the groups are displayed in the sidebar.
const LHNReports = [...pinnedAndGBRReports, ...draftReports, ...nonArchivedReports, ...archivedReports].map((report) => report.reportID);
setWithLimit(reportIDsCache, cachedReportsKey, LHNReports);
return LHNReports;
}

Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/sidebar/SidebarLinksData.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ const propTypes = {

const defaultProps = {
chatReports: {},
allReportActions: {},
isLoadingApp: true,
priorityMode: CONST.PRIORITY_MODE.DEFAULT,
betas: [],
Expand All @@ -111,6 +110,7 @@ const defaultProps = {
accountID: '',
},
transactionViolations: {},
allReportActions: {},
};

function SidebarLinksData({
Expand Down

0 comments on commit 12fc2cb

Please sign in to comment.