|
7 | 7 | type PullRequestListEntry, |
8 | 8 | type PullRequestListInput, |
9 | 9 | type PullRequestListResult, |
| 10 | + type PullRequestListStatsInput, |
10 | 11 | } from "@t3tools/contracts"; |
11 | 12 | import type { PullRequestListControls } from "../../persistence/mobile-preferences"; |
12 | 13 |
|
@@ -50,6 +51,71 @@ export const pullRequestStatKey = ( |
50 | 51 | row: Pick<PullRequestDiffStat, "projectId" | "repository" | "number">, |
51 | 52 | ) => `${row.projectId}:${row.repository}:${row.number}`; |
52 | 53 |
|
| 54 | +/** |
| 55 | + * Diff stats an environment has answered, the stats query that answered each, and each row's |
| 56 | + * `updatedAt` as its list last answered it. |
| 57 | + */ |
| 58 | +export interface PullRequestStatsCache { |
| 59 | + readonly stats: ReadonlyMap<string, PullRequestDiffStat>; |
| 60 | + readonly answeredBy: ReadonlyMap<string, PullRequestListStatsInput>; |
| 61 | + readonly updatedAt: ReadonlyMap<string, string>; |
| 62 | +} |
| 63 | + |
| 64 | +export const EMPTY_PULL_REQUEST_STATS_CACHE: PullRequestStatsCache = { |
| 65 | + stats: new Map(), |
| 66 | + answeredBy: new Map(), |
| 67 | + updatedAt: new Map(), |
| 68 | +}; |
| 69 | + |
| 70 | +/** |
| 71 | + * Records the rows a list answered. Stats carry no head, so a row whose `updatedAt` moved (a push, |
| 72 | + * among other changes) drops its cached stat, and the stats queries that answered it are returned |
| 73 | + * to refresh: one can still be fresh and would otherwise answer the row's next read with the old |
| 74 | + * counts. |
| 75 | + */ |
| 76 | +export function withListedPullRequests( |
| 77 | + cache: PullRequestStatsCache, |
| 78 | + entries: readonly Pick< |
| 79 | + PullRequestListEntry, |
| 80 | + "projectId" | "repository" | "number" | "updatedAt" |
| 81 | + >[], |
| 82 | +): { |
| 83 | + readonly cache: PullRequestStatsCache; |
| 84 | + readonly refresh: readonly PullRequestListStatsInput[]; |
| 85 | +} { |
| 86 | + const moved = entries.filter( |
| 87 | + (entry) => cache.updatedAt.get(pullRequestStatKey(entry)) !== entry.updatedAt, |
| 88 | + ); |
| 89 | + if (!moved.length) return { cache, refresh: [] }; |
| 90 | + const stats = new Map(cache.stats); |
| 91 | + const answeredBy = new Map(cache.answeredBy); |
| 92 | + const updatedAt = new Map(cache.updatedAt); |
| 93 | + const refresh = new Set<PullRequestListStatsInput>(); |
| 94 | + for (const entry of moved) { |
| 95 | + const key = pullRequestStatKey(entry); |
| 96 | + const query = answeredBy.get(key); |
| 97 | + if (query) refresh.add(query); |
| 98 | + stats.delete(key); |
| 99 | + answeredBy.delete(key); |
| 100 | + updatedAt.set(key, entry.updatedAt); |
| 101 | + } |
| 102 | + return { cache: { stats, answeredBy, updatedAt }, refresh: [...refresh] }; |
| 103 | +} |
| 104 | + |
| 105 | +export function withAnsweredPullRequestStats( |
| 106 | + cache: PullRequestStatsCache, |
| 107 | + input: PullRequestListStatsInput, |
| 108 | + answered: readonly PullRequestDiffStat[], |
| 109 | +): PullRequestStatsCache { |
| 110 | + const stats = new Map(cache.stats); |
| 111 | + const answeredBy = new Map(cache.answeredBy); |
| 112 | + for (const row of answered) { |
| 113 | + stats.set(pullRequestStatKey(row), row); |
| 114 | + answeredBy.set(pullRequestStatKey(row), input); |
| 115 | + } |
| 116 | + return { ...cache, stats, answeredBy }; |
| 117 | +} |
| 118 | + |
53 | 119 | /** |
54 | 120 | * Fills rows that have no diff stat yet from the stats answers on screen. Returns `entries` |
55 | 121 | * itself when no row changes, so the list keeps its identity across renders. |
|
0 commit comments