GeezerVison is showing github.com — fix(mobile): a pull request diff reloads later pages after an earlier… · pingdotgg/t3code@d346353
File treeExpand file treeCollapse file tree Open diff view settings apps/mobile/src/features/pull-requests
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
| 7 | 7 | combinePullRequestDiffPages, | 8 | 8 | getCachedPullRequestDiffPages, | 9 | 9 | requestPullRequestDiffPaths, | | 10 | + createPullRequestDiffPagesMemory, | 10 | 11 | walkPullRequestDiffPages, | 11 | 12 | type PullRequestDiffPage, | 12 | 13 | } from "./pullRequestDiffPages"; | @@ -184,6 +185,32 @@ describe("paged mobile pull request diffs", () => { | 184 | 185 | expect(walk(1, ["d.ts"])).toEqual({ cursors: [null, "2"], frontier: "3" }); | 185 | 186 | }); | 186 | 187 |
| | 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 | + | 187 | 214 | it("pages past test-only pages until a file the screen shows loads", () => { | 188 | 215 | const answers = new Map<string | null, PullRequestDiffResult>([ | 189 | 216 | [null, { ...diff("a.test.ts"), nextCursor: "2" }], | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -54,27 +54,69 @@ export function getCachedPullRequestDiffPages( | 54 | 54 | } | 55 | 55 |
| 56 | 56 | /** | 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. | 61 | 84 | */ | 62 | 85 | export function walkPullRequestDiffPages( | 63 | 86 | read: (cursor: string | null) => PullRequestDiffResult | null, | 64 | 87 | count: number, | 65 | 88 | wanted: readonly string[], | 66 | 89 | paths: (page: PullRequestDiffPage) => readonly string[], | 67 | 90 | 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 | +} { | 69 | 97 | const pages: PullRequestDiffPage[] = []; | 70 | 98 | const missing = new Set(wanted); | 71 | | - let showing = !shown; | 72 | 99 | const seen = new Set<string | null>(); | | 100 | + let showing = !shown; | 73 | 101 | let cursor: string | null = null; | 74 | 102 | for (;;) { | 75 | 103 | seen.add(cursor); | 76 | 104 | const result = read(cursor); | 77 | 105 | 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 | + } | 78 | 120 | const page = { cursor, result }; | 79 | 121 | pages.push(page); | 80 | 122 | for (const path of paths(page)) { | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -15,9 +15,11 @@ import { useEnvironmentQuery } from "../../state/query"; | 15 | 15 | import { usePullToRefresh } from "../../state/use-pull-to-refresh"; | 16 | 16 | import type { ReviewParsedDiff } from "../review/reviewModel"; | 17 | 17 | import { | | 18 | + createPullRequestDiffPagesMemory, | 18 | 19 | getCachedPullRequestDiffPages, | 19 | 20 | parsePullRequestDiffPage, | 20 | 21 | requestPullRequestDiffPaths, | | 22 | + type PullRequestDiffPagesMemory, | 21 | 23 | walkPullRequestDiffPages, | 22 | 24 | } from "./pullRequestDiffPages"; | 23 | 25 | import { isPullRequestTestFile } from "./pullRequestFileFilters"; | @@ -97,6 +99,9 @@ const pageTarget = (target: PullRequestDiffTarget, cursor: string | null) => ({ | 97 | 99 | // Pages parse once however many readers combine them; each answer is its own object. | 98 | 100 | const parseCache = new WeakMap<PullRequestDiffResult, ReviewParsedDiff>(); | 99 | 101 |
| | 102 | +// Shared by every walk of one diff, which may stop at different depths. | | 103 | +const pageMemories = new Map<string, PullRequestDiffPagesMemory>(); | | 104 | + | 100 | 105 | // Reads the cached pages in order and mounts the next one a reader still needs, so paging toward | 101 | 106 | // a wanted file continues as each page lands, without the screen asking again. | 102 | 107 | const diffPagesFamily = Atom.family((key: string) => { | @@ -111,21 +116,26 @@ const diffPagesFamily = Atom.family((key: string) => { | 111 | 116 | ? (path: string) => | 112 | 117 | !until.hideTestFiles || path === until.includePath || !isPullRequestTestFile(path) | 113 | 118 | : 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)))), | 120 | 126 | count, | 121 | 127 | wanted, | 122 | 128 | (page) => { | 123 | 129 | const parsed = parsePullRequestDiffPage(page, parseCache, scope); | 124 | 130 | return parsed.kind === "files" ? parsed.files.map((file) => file.path) : []; | 125 | 131 | }, | 126 | 132 | shown, | 127 | | - ), | 128 | | - ); | | 133 | + memory, | | 134 | + ); | | 135 | + const { reload } = walk; | | 136 | + if (reload !== undefined) queueMicrotask(() => get.refresh(pageAtom(reload))); | | 137 | + return walk; | | 138 | + }); | 129 | 139 | }); | 130 | 140 |
| 131 | 141 | const NO_PATHS: readonly string[] = []; | |
|