From a29a87528ede1f3d903e26011f8c8d5bf413d205 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sat, 3 Oct 2026 12:04:39 -0400 Subject: [PATCH] refactor(sonar): avoid awaiting promises sequentially in loops (S9382) --- .../routes/github/LoginWithDeviceFlow.tsx | 36 +++++++++++-------- src/renderer/utils/forges/github/enrich.ts | 35 ++++++++++-------- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/src/renderer/routes/github/LoginWithDeviceFlow.tsx b/src/renderer/routes/github/LoginWithDeviceFlow.tsx index 68cd0c1e9..4019958d7 100644 --- a/src/renderer/routes/github/LoginWithDeviceFlow.tsx +++ b/src/renderer/routes/github/LoginWithDeviceFlow.tsx @@ -87,25 +87,31 @@ export const GitHubLoginWithDeviceFlowRoute: FC = () => { const startPolling = async () => { setIsPolling(true); - try { - while (isActive && Date.now() < session.expiresAt) { - const token = await loginWithDeviceFlowPoll(forge, session); - - if (token && isActive) { - await loginWithDeviceFlowComplete(forge, token, session.hostname); - navigate('/'); - return; + const poll = async (): Promise => { + if (!isActive || Date.now() >= session.expiresAt) { + if (isActive) { + setError('Device code expired. Please start again.'); } - - const intervalMs = Math.max(5000, session.intervalSeconds * 1000); - await new Promise((resolve) => { - timeoutId = setTimeout(resolve, intervalMs); - }); + return; } - if (isActive) { - setError('Device code expired. Please start again.'); + const token = await loginWithDeviceFlowPoll(forge, session); + + if (token && isActive) { + await loginWithDeviceFlowComplete(forge, token, session.hostname); + navigate('/'); + return; } + + const intervalMs = Math.max(5000, session.intervalSeconds * 1000); + await new Promise((resolve) => { + timeoutId = setTimeout(resolve, intervalMs); + }); + await poll(); + }; + + try { + await poll(); } catch (err) { if (isActive) { rendererLogError('LoginWithDeviceFlow', 'Failed to poll device flow', toError(err)); diff --git a/src/renderer/utils/forges/github/enrich.ts b/src/renderer/utils/forges/github/enrich.ts index de4979bc9..27be14e2d 100644 --- a/src/renderer/utils/forges/github/enrich.ts +++ b/src/renderer/utils/forges/github/enrich.ts @@ -38,28 +38,35 @@ export async function enrichGitHubNotifications( async function fetchInBatches( notifications: RawGitifyNotification[], ): Promise> { - const merged = new Map(); const supportedNotifications = notifications.filter( (notification) => createNotificationHandler(notification).supportsMergedQueryEnrichment, ); const batchSize = GITHUB_API_MERGE_BATCH_SIZE; - + const batches: RawGitifyNotification[][] = []; for (let start = 0; start < supportedNotifications.length; start += batchSize) { - const batchIndex = Math.floor(start / batchSize) + 1; - const slice = supportedNotifications.slice(start, start + batchSize); + batches.push(supportedNotifications.slice(start, start + batchSize)); + } - try { - const batchResults = await fetchNotificationDetailsForList(slice); - for (const [notification, repository] of batchResults) { - merged.set(notification, repository); + const batchResults = await Promise.all( + batches.map(async (slice, index) => { + try { + return await fetchNotificationDetailsForList(slice); + } catch (err) { + rendererLogError( + 'enrichGitHubNotifications', + `Failed to fetch merged notification details for batch ${index + 1}`, + toError(err), + ); + return new Map(); } - } catch (err) { - rendererLogError( - 'enrichGitHubNotifications', - `Failed to fetch merged notification details for batch ${batchIndex}`, - toError(err), - ); + }), + ); + + const merged = new Map(); + for (const results of batchResults) { + for (const [notification, repository] of results) { + merged.set(notification, repository); } }