Skip to content

Commit 18e9cdf

Browse files
committed
fix(web): ignore stale pull request stat refreshes
1 parent bf29367 commit 18e9cdf

3 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎apps/web/src/components/pullRequest/pullRequestList.logic.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
parsePullRequestQuery,
1414
pullRequestStatsBatches,
1515
pullRequestStatsKeysToRequest,
16+
pullRequestStatsRefreshBatches,
1617
pullRequestStatsRequestBatches,
1718
mergePullRequestDiffStats,
1819
narrowPullRequestsToFilters,
@@ -189,6 +190,25 @@ describe("visible pull request line-count targets", () => {
189190
expect(eager[0]?.input.refs.map((ref) => ref.number)).toEqual([1, 2, 3]);
190191
});
191192

193+
it("ignores a late refresh after the filter or stats policy changes", () => {
194+
const entries = [entry({ number: 1 }), entry({ number: 2 })];
195+
const entriesByKey = new Map(entries.map((item) => [pullRequestEntryKey(item), item]));
196+
const visibleKeys = new Set([pullRequestEntryKey(entries[1]!)]);
197+
const requestedScope = { key: "open", policy: "visible" } as const;
198+
const refresh = (currentScope: { key: string; policy: "visible" | "eager" }) =>
199+
pullRequestStatsRefreshBatches({
200+
requestedScope,
201+
currentScope,
202+
entriesByKey,
203+
candidateKeys: visibleKeys,
204+
statsByRow: new Map(),
205+
});
206+
207+
expect(refresh(requestedScope)?.[0]?.input.refs.map((ref) => ref.number)).toEqual([2]);
208+
expect(refresh({ key: "closed", policy: "visible" })).toBeNull();
209+
expect(refresh({ key: "open", policy: "eager" })).toBeNull();
210+
});
211+
192212
it("does not add request batches again while rows are active or cached", () => {
193213
const entries = [entry({ number: 1 }), entry({ number: 2 })];
194214
const entriesByKey = new Map(entries.map((item) => [pullRequestEntryKey(item), item]));

‎apps/web/src/components/pullRequest/pullRequestList.logic.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ export interface PullRequestStatsBatch extends PullRequestStatsTarget {
447447

448448
export type PullRequestStatsPolicy = "visible" | "eager";
449449

450+
export interface PullRequestStatsScope {
451+
readonly key: string;
452+
readonly policy: PullRequestStatsPolicy;
453+
}
454+
450455
const MAX_PULL_REQUEST_STATS_REFS = 500;
451456

452457
/** Excludes rows already covered by an active batch or the received-count cache. */
@@ -534,6 +539,33 @@ export function pullRequestStatsRequestBatches({
534539
return pullRequestStatsBatches(entriesByKey, keys);
535540
}
536541

542+
/** Ignores a refresh that finished after the list moved to another filter or stats policy. */
543+
export function pullRequestStatsRefreshBatches({
544+
requestedScope,
545+
currentScope,
546+
entriesByKey,
547+
candidateKeys,
548+
statsByRow,
549+
}: {
550+
readonly requestedScope: PullRequestStatsScope;
551+
readonly currentScope: PullRequestStatsScope;
552+
readonly entriesByKey: ReadonlyMap<string, EnvironmentPullRequestEntry>;
553+
readonly candidateKeys: ReadonlySet<string>;
554+
readonly statsByRow: ReadonlyMap<string, unknown>;
555+
}): ReadonlyArray<PullRequestStatsBatch> | null {
556+
if (requestedScope.key !== currentScope.key || requestedScope.policy !== currentScope.policy) {
557+
return null;
558+
}
559+
return pullRequestStatsRequestBatches({
560+
entriesByKey,
561+
candidateKeys,
562+
policy: requestedScope.policy,
563+
activeBatches: [],
564+
statsByRow,
565+
refresh: true,
566+
});
567+
}
568+
537569
/** Drops completed batches once every row in them has left the observer window. */
538570
export function retainVisiblePullRequestStatsBatches(
539571
batches: ReadonlyArray<PullRequestStatsBatch>,

‎apps/web/src/routes/_chat.pull-requests.tsx‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ import {
6565
withDiffStat,
6666
writePullRequestListSnapshot,
6767
scorePullRequestMatch,
68+
pullRequestStatsRefreshBatches,
6869
pullRequestStatsRequestBatches,
6970
retainVisiblePullRequestStatsBatches,
7071
type EnvironmentPullRequestEntry,
7172
type MergedPullRequestList,
7273
type PullRequestDiffStats,
7374
type PullRequestStatsBatch,
7475
type PullRequestStatsPolicy,
76+
type PullRequestStatsScope,
7577
type PullRequestPartitionsSnapshot,
7678
} from "../components/pullRequest/pullRequestList.logic";
7779
import { assignProjectsToEnvironments } from "../components/pullRequest/pullRequestProjectAssignment.logic";
@@ -621,6 +623,8 @@ function PullRequestsRouteView() {
621623
// Page size is view state, not a URL concern: a shared link should open the first page.
622624
const scopeKey = `${environmentKey}:${assignmentKey}:${search.state}:${search.involvement}:${scopedProjectId ?? ""}:${search.host ?? ""}:${search.draft ?? ""}:${search.review ?? ""}:${search.checks ?? ""}:${search.author ?? ""}:${search.labels?.join("\u0000") ?? ""}`;
623625
const filterKey = `${scopeKey}:${sentQuery}`;
626+
const statsScopeRef = useRef<PullRequestStatsScope>({ key: filterKey, policy: statsPolicy });
627+
statsScopeRef.current = { key: filterKey, policy: statsPolicy };
624628
// Where the next slice carries on from, per repository within each environment, as that
625629
// environment handed it back. Sending it is what makes a second page cost a second page rather
626630
// than the whole list again — and a repository it does not name has run out and is not read a
@@ -794,6 +798,7 @@ function PullRequestsRouteView() {
794798
// of its own work is a button that gets pressed again, and buys the whole cascade twice.
795799
const [invalidating, setInvalidating] = useState(false);
796800
const refreshFromHost = async () => {
801+
const requestedStatsScope = statsScopeRef.current;
797802
setInvalidating(true);
798803
try {
799804
// Every environment the page is reading, since what the reader pressed refresh for is the
@@ -810,16 +815,17 @@ function PullRequestsRouteView() {
810815
authoredQuery.refresh();
811816
reviewingQuery.refresh();
812817
const visible = visibleStatsKeys.current;
813-
const batches = pullRequestStatsRequestBatches({
818+
const batches = pullRequestStatsRefreshBatches({
819+
requestedScope: requestedStatsScope,
820+
currentScope: statsScopeRef.current,
814821
entriesByKey: entriesByStatsKey.current,
815-
candidateKeys: visible.key === filterKey ? visible.values : new Set(),
816-
policy: statsPolicy,
817-
activeBatches: [],
822+
candidateKeys: visible.key === requestedStatsScope.key ? visible.values : new Set(),
818823
statsByRow: statsByRowRef.current,
819-
refresh: true,
820824
});
821-
setStatsTargetState({ key: filterKey, batches });
822-
statsQuery.refresh(batches.map(({ environmentId, input }) => ({ environmentId, input })));
825+
if (batches !== null) {
826+
setStatsTargetState({ key: requestedStatsScope.key, batches });
827+
statsQuery.refresh(batches.map(({ environmentId, input }) => ({ environmentId, input })));
828+
}
823829
setDetailRefreshToken((token) => token + 1);
824830
};
825831
const refreshing = invalidating || listQuery.isPending;

0 commit comments

Comments
 (0)