| 1 | /** |
| 2 | * Each entry's last commit for the file list, waited on only so long. An |
| 3 | * answer not yet kept walks history, which can take seconds on a large or |
| 4 | * busy repository; the page goes out without it then, the column empty, |
| 5 | * while the walk finishes in the background (waitUntil) so the next view |
| 6 | * has it from the cache. The page's stream never waits on it past its own |
| 7 | * timeout. |
| 8 | */ |
| 9 | import { waitUntil } from "cloudflare:workers"; |
| 10 | |
| 11 | import type { RepoPath, Viewer } from "@g1t/contracts"; |
| 12 | |
| 13 | import type { FileCommits } from "./commit-people"; |
| 14 | import { repos } from "./services.server"; |
| 15 | |
| 16 | /** How long the page waits for the column. */ |
| 17 | const WAIT_MS = 3_000; |
| 18 | |
| 19 | export function lastCommitsFor(path: RepoPath, viewer: Viewer, ref: string | null, treePath: string): Promise<FileCommits | null> { |
| 20 | const walk = repos |
| 21 | .lastCommits(path, viewer, ref, treePath) |
| 22 | // The rows show a commit's subject and age, never who made it: their |
| 23 | // addresses stay on the server. |
| 24 | .then((found): FileCommits | null => |
| 25 | found.ok |
| 26 | ? { |
| 27 | complete: found.value.complete, |
| 28 | entries: found.value.entries.map(({ name, commit }) => ({ |
| 29 | name, |
| 30 | commit: { hash: commit.hash, message: commit.message, authoredAt: commit.authoredAt }, |
| 31 | })), |
| 32 | } |
| 33 | : null, |
| 34 | ) |
| 35 | .catch(() => null); |
| 36 | waitUntil(walk); |
| 37 | return Promise.race([walk, new Promise<null>((resolve) => setTimeout(() => resolve(null), WAIT_MS))]); |
| 38 | } |