mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 22:41:58 +03:00
fetch historical rates for multiple dates concurrently
fetchNeededHistoricalRates awaited each date's fetch sequentially even though they're independent requests — a sheet referencing several historical dates paid for that latency serially instead of in parallel.
This commit is contained in:
parent
697f1f0e3d
commit
40b5b94102
1 changed files with 13 additions and 4 deletions
15
src/main.ts
15
src/main.ts
|
|
@ -568,11 +568,20 @@ const HIST_DATE_RE = /(?:on|на)\s+(\d{4}-\d{2}-\d{2})/gi;
|
||||||
async function fetchNeededHistoricalRates(text: string): Promise<void> {
|
async function fetchNeededHistoricalRates(text: string): Promise<void> {
|
||||||
const dates = new Set<string>();
|
const dates = new Set<string>();
|
||||||
for (const m of text.matchAll(HIST_DATE_RE)) dates.add(m[1]!);
|
for (const m of text.matchAll(HIST_DATE_RE)) dates.add(m[1]!);
|
||||||
|
const datesToFetch = Array.from(dates).filter((date) => !engine.hasHistoricalRates(date));
|
||||||
let fetched = false;
|
let fetched = false;
|
||||||
for (const date of dates) {
|
if (datesToFetch.length > 0) {
|
||||||
if (!engine.hasHistoricalRates(date)) {
|
const results = await Promise.all(
|
||||||
|
datesToFetch.map(async (date) => {
|
||||||
const rates = await fetchHistoricalRates(date);
|
const rates = await fetchHistoricalRates(date);
|
||||||
if (rates) { engine.setHistoricalRates(date, rates); fetched = true; }
|
return { date, rates };
|
||||||
|
})
|
||||||
|
);
|
||||||
|
for (const { date, rates } of results) {
|
||||||
|
if (rates) {
|
||||||
|
engine.setHistoricalRates(date, rates);
|
||||||
|
fetched = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (fetched) { workspace.invalidateAll(); editor.refresh(); }
|
if (fetched) { workspace.invalidateAll(); editor.refresh(); }
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue