Fix PR status display: deduplicate concurrent requests and simplify cache logic (#38)

This commit is contained in:
Jinjing
2026-03-22 16:28:35 -07:00
committed by GitHub
parent 0a88bea0e6
commit ddbad7a13f
2 changed files with 54 additions and 41 deletions
@@ -114,20 +114,12 @@ const WorktreeCard = React.memo(function WorktreeCard({
return liveTabs.length > 0 ? 'active' : 'inactive'
}, [hasTerminals, tabs])
// Fetch PR data (debounced via ref guard)
const prFetchedRef = useRef<string | null>(null)
// Fetch PR data on mount – the store's isFresh() check prevents redundant API calls
useEffect(() => {
if (
repo &&
!worktree.isBare &&
pr === undefined &&
prCacheKey &&
prCacheKey !== prFetchedRef.current
) {
prFetchedRef.current = prCacheKey
if (repo && !worktree.isBare && prCacheKey) {
fetchPRForBranch(repo.path, branch)
}
}, [repo, worktree.isBare, pr, fetchPRForBranch, branch, prCacheKey])
}, [repo, worktree.isBare, fetchPRForBranch, branch, prCacheKey])
// Fetch issue data (debounced via ref guard)
const issueFetchedRef = useRef<string | null>(null)
+51 -30
View File
@@ -9,6 +9,9 @@ export interface CacheEntry<T> {
const CACHE_TTL = 300_000 // 5 minutes (stale data shown instantly, then refreshed)
const inflightPRRequests = new Map<string, Promise<PRInfo | null>>()
const inflightIssueRequests = new Map<string, Promise<IssueInfo | null>>()
function isFresh<T>(entry: CacheEntry<T> | undefined): entry is CacheEntry<T> {
return entry !== undefined && Date.now() - entry.fetchedAt < CACHE_TTL
}
@@ -57,43 +60,61 @@ export const createGitHubSlice: StateCreator<AppState, [], [], GitHubSlice> = (s
const cacheKey = `${repoPath}::${branch}`
const cached = get().prCache[cacheKey]
if (isFresh(cached)) return cached.data
const inflightRequest = inflightPRRequests.get(cacheKey)
if (inflightRequest) return inflightRequest
try {
const pr = await window.api.gh.prForBranch({ repoPath, branch })
set((s) => ({
prCache: { ...s.prCache, [cacheKey]: { data: pr, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return pr
} catch (err) {
console.error('Failed to fetch PR:', err)
set((s) => ({
prCache: { ...s.prCache, [cacheKey]: { data: null, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return null
}
const request = (async () => {
try {
const pr = await window.api.gh.prForBranch({ repoPath, branch })
set((s) => ({
prCache: { ...s.prCache, [cacheKey]: { data: pr, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return pr
} catch (err) {
console.error('Failed to fetch PR:', err)
set((s) => ({
prCache: { ...s.prCache, [cacheKey]: { data: null, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return null
} finally {
inflightPRRequests.delete(cacheKey)
}
})()
inflightPRRequests.set(cacheKey, request)
return request
},
fetchIssue: async (repoPath, number) => {
const cacheKey = `${repoPath}::${number}`
const cached = get().issueCache[cacheKey]
if (isFresh(cached)) return cached.data
const inflightRequest = inflightIssueRequests.get(cacheKey)
if (inflightRequest) return inflightRequest
try {
const issue = await window.api.gh.issue({ repoPath, number })
set((s) => ({
issueCache: { ...s.issueCache, [cacheKey]: { data: issue, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return issue
} catch (err) {
console.error('Failed to fetch issue:', err)
set((s) => ({
issueCache: { ...s.issueCache, [cacheKey]: { data: null, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return null
}
const request = (async () => {
try {
const issue = await window.api.gh.issue({ repoPath, number })
set((s) => ({
issueCache: { ...s.issueCache, [cacheKey]: { data: issue, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return issue
} catch (err) {
console.error('Failed to fetch issue:', err)
set((s) => ({
issueCache: { ...s.issueCache, [cacheKey]: { data: null, fetchedAt: Date.now() } }
}))
debouncedSaveCache(get())
return null
} finally {
inflightIssueRequests.delete(cacheKey)
}
})()
inflightIssueRequests.set(cacheKey, request)
return request
}
})