From 72555347e4ebf52be55546aa74d46a9dfc8a6d85 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 24 Jun 2026 16:49:46 +0800 Subject: [PATCH] Extract handlePullRequestRefreshOnSelection to reduce duplication MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Consolidate the cooldown-cancellation and PR-refresh scheduling logic that was duplicated across the same-repo and cross-repo branches of setSelectedWorktreeID into a single helper method. This also fixes the comment scope — the original comment only mentioned "within the same repo" but the same logic applies to entering a new repo. Signed-off-by: Alex --- .../WorktreeInfoWatcherManager.swift | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/supacode/Features/Repositories/BusinessLogic/WorktreeInfoWatcherManager.swift b/supacode/Features/Repositories/BusinessLogic/WorktreeInfoWatcherManager.swift index a78e228b..f7a80a25 100644 --- a/supacode/Features/Repositories/BusinessLogic/WorktreeInfoWatcherManager.swift +++ b/supacode/Features/Repositories/BusinessLogic/WorktreeInfoWatcherManager.swift @@ -221,39 +221,17 @@ final class WorktreeInfoWatcherManager { emitLineChangesChanged(worktreeID: worktreeID) syncLineChangesActivity(for: worktreeID) } - // When switching to a different worktree within the same repo, cancel any active cooldown - // so the PR refresh fires immediately. Re-selecting the same worktree still respects the cooldown. + // When switching to a different worktree, cancel any active cooldown so the PR refresh + // fires immediately. Re-selecting the same worktree still respects the cooldown. if let previousRepository, previousRepository == nextRepository { - let lastWorktreeForRepo = lastSelectedWorktreeIDByRepo[previousRepository] - let isDifferentWorktree = lastWorktreeForRepo != worktreeID - if isDifferentWorktree { - cancelPullRequestSelectionCooldown(for: previousRepository) - } - updatePullRequestSchedule( - repositoryRootURL: previousRepository, - immediate: shouldImmediatelyRefreshPullRequests(repositoryRootURL: previousRepository) - ) - if let worktreeID { - lastSelectedWorktreeIDByRepo[previousRepository] = worktreeID - } + handlePullRequestRefreshOnSelection(repositoryRootURL: previousRepository, worktreeID: worktreeID) return } if let previousRepository { updatePullRequestSchedule(repositoryRootURL: previousRepository, immediate: false) } if let nextRepository { - let lastWorktreeForRepo = lastSelectedWorktreeIDByRepo[nextRepository] - let isDifferentWorktree = lastWorktreeForRepo != worktreeID - if isDifferentWorktree { - cancelPullRequestSelectionCooldown(for: nextRepository) - } - updatePullRequestSchedule( - repositoryRootURL: nextRepository, - immediate: shouldImmediatelyRefreshPullRequests(repositoryRootURL: nextRepository) - ) - if let worktreeID { - lastSelectedWorktreeIDByRepo[nextRepository] = worktreeID - } + handlePullRequestRefreshOnSelection(repositoryRootURL: nextRepository, worktreeID: worktreeID) } } @@ -803,6 +781,23 @@ final class WorktreeInfoWatcherManager { eventContinuation?.yield(event) } + private func handlePullRequestRefreshOnSelection( + repositoryRootURL: URL, + worktreeID: Worktree.ID? + ) { + let lastWorktreeForRepo = lastSelectedWorktreeIDByRepo[repositoryRootURL] + if lastWorktreeForRepo != worktreeID { + cancelPullRequestSelectionCooldown(for: repositoryRootURL) + } + updatePullRequestSchedule( + repositoryRootURL: repositoryRootURL, + immediate: shouldImmediatelyRefreshPullRequests(repositoryRootURL: repositoryRootURL) + ) + if let worktreeID { + lastSelectedWorktreeIDByRepo[repositoryRootURL] = worktreeID + } + } + private func cancelPullRequestSelectionCooldown(for repositoryRootURL: URL) { pullRequestSelectionCooldownTasksByRepo.removeValue(forKey: repositoryRootURL)?.task.cancel() } -- 2.51.2