GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mobile): a pull request diff reloads later pages after an earlier… · pingdotgg/t3code@d346353

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/mobile/src/features/pull-requests
    • pullRequestDiffPages.test.ts
    • pullRequestDiffPages.ts
    • usePullRequestDiff.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts‎

Copy file name to clipboardExpand all lines: apps/mobile/src/features/pull-requests/pullRequestDiffPages.test.ts
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
combinePullRequestDiffPages,
88
getCachedPullRequestDiffPages,
99
requestPullRequestDiffPaths,
10+
createPullRequestDiffPagesMemory,
1011
walkPullRequestDiffPages,
1112
type PullRequestDiffPage,
1213
} from "./pullRequestDiffPages";
@@ -184,6 +185,32 @@ describe("paged mobile pull request diffs", () => {
184185
expect(walk(1, ["d.ts"])).toEqual({ cursors: [null, "2"], frontier: "3" });
185186
});
186187

188+
it("holds a later page from before an earlier page changed until it reloads", () => {
189+
const answers = new Map<string | null, PullRequestDiffResult>([
190+
[null, { ...diff("a.ts"), nextCursor: "2" }],
191+
["2", { ...diff("b.ts"), nextCursor: "3" }],
192+
["3", diff("c.ts")],
193+
]);
194+
const read = (cursor: string | null) => answers.get(cursor) ?? null;
195+
const known = createPullRequestDiffPagesMemory();
196+
const walk = () => walkPullRequestDiffPages(read, 3, [], () => [], undefined, known);
197+
198+
expect(walk().pages).toHaveLength(3);
199+
answers.set(null, { ...diff("a2.ts"), nextCursor: "2" });
200+
expect(walk()).toMatchObject({ frontier: "2", reload: "2" });
201+
const held = walk();
202+
expect([held.frontier, held.reload]).toEqual(["2", undefined]);
203+
answers.set("2", { ...diff("b2.ts"), nextCursor: "3" });
204+
expect(walk()).toMatchObject({ frontier: "3", reload: "3" });
205+
answers.set("3", diff("c.ts"));
206+
const { pages } = walk();
207+
expect(pages.map((page) => page.result.patch)).toEqual([
208+
answers.get(null)!.patch,
209+
answers.get("2")!.patch,
210+
diff("c.ts").patch,
211+
]);
212+
});
213+
187214
it("pages past test-only pages until a file the screen shows loads", () => {
188215
const answers = new Map<string | null, PullRequestDiffResult>([
189216
[null, { ...diff("a.test.ts"), nextCursor: "2" }],
Collapse file

‎apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts‎

Copy file name to clipboardExpand all lines: apps/mobile/src/features/pull-requests/pullRequestDiffPages.ts
+48-6Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,69 @@ export function getCachedPullRequestDiffPages(
5454
}
5555

5656
/**
57-
* Follows each loaded page's next cursor until `count` pages and every wanted path have loaded,
58-
* and, given `shown`, until a file the screen shows has loaded: a screen with nothing to show
59-
* has no scroll position to page further from. `frontier` is the cursor the walk stopped on: the last page read, or the one still loading or
60-
* failed. A page whose answer changed leads to its new cursors, so stale later pages drop out.
57+
* What a walk saw last time. Some hosts' cursors are bare page numbers, so a later page cached
58+
* before an earlier page changed still matches its cursor; such a page is held until it reloads.
59+
*/
60+
export interface PullRequestDiffPagesMemory {
61+
readonly results: Map<string | null, PullRequestDiffResult>;
62+
readonly stale: WeakSet<PullRequestDiffResult>;
63+
readonly reloading: WeakSet<PullRequestDiffResult>;
64+
}
65+
66+
export function createPullRequestDiffPagesMemory(): PullRequestDiffPagesMemory {
67+
return { results: new Map(), stale: new WeakSet(), reloading: new WeakSet() };
68+
}
69+
70+
function samePullRequestDiffPage(a: PullRequestDiffResult, b: PullRequestDiffResult): boolean {
71+
return (
72+
a === b ||
73+
(a.patch === b.patch &&
74+
a.truncated === b.truncated &&
75+
a.nextCursor === b.nextCursor &&
76+
JSON.stringify(a.omittedFileStats ?? []) === JSON.stringify(b.omittedFileStats ?? []))
77+
);
78+
}
79+
80+
/**
81+
* Follows next cursors until `count` pages, every wanted path, and (given `shown`) one shown file
82+
* have loaded. `frontier` is the cursor it stopped on: the last page read, or one loading, failed,
83+
* or held as stale. `reload` names a page that just went stale, for the caller to refetch.
6184
*/
6285
export function walkPullRequestDiffPages(
6386
read: (cursor: string | null) => PullRequestDiffResult | null,
6487
count: number,
6588
wanted: readonly string[],
6689
paths: (page: PullRequestDiffPage) => readonly string[],
6790
shown?: (path: string) => boolean,
68-
): { readonly pages: readonly PullRequestDiffPage[]; readonly frontier: string | null } {
91+
memory?: PullRequestDiffPagesMemory,
92+
): {
93+
readonly pages: readonly PullRequestDiffPage[];
94+
readonly frontier: string | null;
95+
readonly reload?: string | null | undefined;
96+
} {
6997
const pages: PullRequestDiffPage[] = [];
7098
const missing = new Set(wanted);
71-
let showing = !shown;
7299
const seen = new Set<string | null>();
100+
let showing = !shown;
73101
let cursor: string | null = null;
74102
for (;;) {
75103
seen.add(cursor);
76104
const result = read(cursor);
77105
if (result === null) return { pages, frontier: cursor };
106+
if (memory) {
107+
if (memory.stale.has(result)) {
108+
if (memory.reloading.has(result)) return { pages, frontier: cursor };
109+
memory.reloading.add(result);
110+
return { pages, frontier: cursor, reload: cursor };
111+
}
112+
const previous = memory.results.get(cursor);
113+
memory.results.set(cursor, result);
114+
// Every page remembered past this one may come from the old answer, whichever reader
115+
// walked that far.
116+
if (previous !== undefined && !samePullRequestDiffPage(previous, result))
117+
for (const [other, answer] of memory.results)
118+
if (!seen.has(other)) memory.stale.add(answer);
119+
}
78120
const page = { cursor, result };
79121
pages.push(page);
80122
for (const path of paths(page)) {
Collapse file

‎apps/mobile/src/features/pull-requests/usePullRequestDiff.ts‎

Copy file name to clipboardExpand all lines: apps/mobile/src/features/pull-requests/usePullRequestDiff.ts
+18-8Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import { useEnvironmentQuery } from "../../state/query";
1515
import { usePullToRefresh } from "../../state/use-pull-to-refresh";
1616
import type { ReviewParsedDiff } from "../review/reviewModel";
1717
import {
18+
createPullRequestDiffPagesMemory,
1819
getCachedPullRequestDiffPages,
1920
parsePullRequestDiffPage,
2021
requestPullRequestDiffPaths,
22+
type PullRequestDiffPagesMemory,
2123
walkPullRequestDiffPages,
2224
} from "./pullRequestDiffPages";
2325
import { isPullRequestTestFile } from "./pullRequestFileFilters";
@@ -97,6 +99,9 @@ const pageTarget = (target: PullRequestDiffTarget, cursor: string | null) => ({
9799
// Pages parse once however many readers combine them; each answer is its own object.
98100
const parseCache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>();
99101

102+
// Shared by every walk of one diff, which may stop at different depths.
103+
const pageMemories = new Map<string, PullRequestDiffPagesMemory>();
104+
100105
// Reads the cached pages in order and mounts the next one a reader still needs, so paging toward
101106
// a wanted file continues as each page lands, without the screen asking again.
102107
const diffPagesFamily = Atom.family((key: string) => {
@@ -111,21 +116,26 @@ const diffPagesFamily = Atom.family((key: string) => {
111116
? (path: string) =>
112117
!until.hideTestFiles || path === until.includePath || !isPullRequestTestFile(path)
113118
: undefined;
114-
return Atom.make((get) =>
115-
walkPullRequestDiffPages(
116-
(cursor) =>
117-
Option.getOrNull(
118-
AsyncResult.value(get(composerPullRequests.diff(pageTarget(target, cursor)))),
119-
),
119+
let memory = pageMemories.get(scope);
120+
if (!memory) pageMemories.set(scope, (memory = createPullRequestDiffPagesMemory()));
121+
return Atom.make((get) => {
122+
const pageAtom = (cursor: string | null) =>
123+
composerPullRequests.diff(pageTarget(target, cursor));
124+
const walk = walkPullRequestDiffPages(
125+
(cursor) => Option.getOrNull(AsyncResult.value(get(pageAtom(cursor)))),
120126
count,
121127
wanted,
122128
(page) => {
123129
const parsed = parsePullRequestDiffPage(page, parseCache, scope);
124130
return parsed.kind === "files" ? parsed.files.map((file) => file.path) : [];
125131
},
126132
shown,
127-
),
128-
);
133+
memory,
134+
);
135+
const { reload } = walk;
136+
if (reload !== undefined) queueMicrotask(() => get.refresh(pageAtom(reload)));
137+
return walk;
138+
});
129139
});
130140

131141
const NO_PATHS: readonly string[] = [];

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.