Skip to content

Commit

Permalink
create report-transactions map to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
TMisiukiewicz committed Jan 15, 2025
1 parent 7b9e55e commit cc54548
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type BuildOptimisticTransactionParams = {
};

let allTransactions: OnyxCollection<Transaction> = {};
let transactionsByReport: Record<string, Set<string>> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.TRANSACTION,
waitForCollectionCallback: true,
Expand All @@ -76,6 +77,18 @@ Onyx.connect({
return;
}
allTransactions = Object.fromEntries(Object.entries(value).filter(([, transaction]) => !!transaction));

transactionsByReport = {};
Object.entries(allTransactions).forEach(([transactionID, transaction]) => {
if (!transaction?.reportID) {
return;
}

if (!transactionsByReport[transaction.reportID]) {
transactionsByReport[transaction.reportID] = new Set();
}
transactionsByReport[transaction.reportID].add(transactionID);
});
},
});

Expand Down Expand Up @@ -818,10 +831,22 @@ function hasRoute(transaction: OnyxEntry<Transaction>, isDistanceRequestType?: b
}

function getAllReportTransactions(reportID?: string, transactions?: OnyxCollection<Transaction>): Transaction[] {
const reportTransactions: Transaction[] = Object.values(transactions ?? allTransactions ?? {}).filter(
(transaction): transaction is Transaction => !!transaction && transaction.reportID === reportID,
);
return reportTransactions;
if (!reportID) {
return [];
}

if (transactions) {
return Object.values(transactions).filter((transaction): transaction is Transaction => !!transaction && transaction.reportID === reportID);
}

const transactionIDs = transactionsByReport[reportID];
if (!transactionIDs) {
return [];
}

return Array.from(transactionIDs)
.map((transactionID) => allTransactions?.[transactionID])
.filter((transaction): transaction is Transaction => !!transaction);
}

function waypointHasValidAddress(waypoint: RecentWaypoint | Waypoint): boolean {
Expand Down

0 comments on commit cc54548

Please sign in to comment.