Skip to content

Commit

Permalink
Limit to one trend export at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
idpaterson committed Nov 23, 2023
1 parent 778326e commit 4d5d46f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
56 changes: 32 additions & 24 deletions src/pages/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
BalanceHistoryCallbackProgress,
fetchDailyBalancesForTrend,
TrendBalanceHistoryCallbackProgress,
TrendEntry,
} from '@root/src/shared/lib/accounts';
import { throttle } from '@root/src/shared/lib/events';
import stateStorage from '@root/src/shared/storages/stateStorage';
Expand Down Expand Up @@ -232,33 +233,40 @@ const handleDownloadAllAccountBalances = async (sendResponse: () => void) => {
}
};

let pendingTrendBalances: Promise<TrendEntry[]>;

/** Download daily balances for the specified trend. */
const handleDownloadTrendBalances = async (sendResponse: () => void) => {
try {
const throttledSendDownloadTrendBalancesProgress = throttle(
sendDownloadTrendBalancesProgress,
THROTTLE_INTERVAL_MS,
);

const { trend } = await trendStorage.get();
await trendStorage.set({
trend,
status: TrendDownloadStatus.Loading,
progress: { completePercentage: 0 },
});
const balances = await fetchDailyBalancesForTrend({
trend,
onProgress: throttledSendDownloadTrendBalancesProgress,
});
const { reportType } = trend;
const csv = formatBalancesAsCSV({ balances, reportType });

chrome.downloads.download({
url: `data:text/csv,${csv}`,
filename: 'mint-trend-daily-balances.csv',
});

await trendStorage.patch({ status: TrendDownloadStatus.Success });
if (pendingTrendBalances) {
// already downloading
await pendingTrendBalances;
} else {
const throttledSendDownloadTrendBalancesProgress = throttle(
sendDownloadTrendBalancesProgress,
THROTTLE_INTERVAL_MS,
);
const { trend } = await trendStorage.get();
await trendStorage.set({
trend,
status: TrendDownloadStatus.Loading,
progress: { completePercentage: 0 },
});
pendingTrendBalances = fetchDailyBalancesForTrend({
trend,
onProgress: throttledSendDownloadTrendBalancesProgress,
});
const balances = await pendingTrendBalances;
const { reportType } = trend;
const csv = formatBalancesAsCSV({ balances, reportType });

chrome.downloads.download({
url: `data:text/csv,${csv}`,
filename: 'mint-trend-daily-balances.csv',
});

await trendStorage.patch({ status: TrendDownloadStatus.Success });
}
} catch (e) {
await trendStorage.patch({ status: TrendDownloadStatus.Error });
} finally {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/lib/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type FixedDateFilter =
| 'ALL_TIME'
| 'CUSTOM';

type TrendEntry = {
export type TrendEntry = {
amount: number;
date: string;
// this is determined by the type of report we fetch (DEBTS_TIME/ASSETS_TIME)
Expand Down

0 comments on commit 4d5d46f

Please sign in to comment.