Skip to content

Commit 15c0d43

Browse files
committed
Allow commit meta request to run in parallel with data request
1 parent 820810e commit 15c0d43

File tree

2 files changed

+71
-59
lines changed

2 files changed

+71
-59
lines changed

web/src/lib/diff-viewer-multi-file.svelte.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,12 @@ export class MultiFileDiffViewerState {
497497
await tick();
498498
await animationFramePromise();
499499

500+
// Start potential multiple web requests in parallel
501+
const metaPromise = meta();
502+
const generatorPromise = patches();
503+
500504
// Update metadata
501-
this.diffMetadata = await meta();
505+
this.diffMetadata = await metaPromise;
502506
await tick();
503507
await animationFramePromise();
504508

@@ -508,7 +512,7 @@ export class MultiFileDiffViewerState {
508512
await animationFramePromise();
509513

510514
// Setup generator
511-
const generator = await patches();
515+
const generator = await generatorPromise;
512516
await tick();
513517
await animationFramePromise();
514518

@@ -549,14 +553,15 @@ export class MultiFileDiffViewerState {
549553
}
550554
}
551555

552-
private async loadPatchesGithub(resultPromise: Promise<GithubDiffResult>) {
556+
private async loadPatchesGithub(resultOrPromise: Promise<GithubDiffResult> | GithubDiffResult) {
553557
return await this.loadPatches(
554558
async () => {
555-
return { type: "github", details: (await resultPromise).info };
559+
const result = resultOrPromise instanceof Promise ? await resultOrPromise : resultOrPromise;
560+
return { type: "github", details: await result.info };
556561
},
557562
async () => {
558-
const result = await resultPromise;
559-
return parseMultiFilePatchGithub(result.info, await result.response, this.loadingState);
563+
const result = resultOrPromise instanceof Promise ? await resultOrPromise : resultOrPromise;
564+
return parseMultiFilePatchGithub(await result.info, await result.response, this.loadingState);
560565
},
561566
);
562567
}

web/src/lib/github.svelte.ts

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type GithubDiff = {
2121
};
2222

2323
export type GithubDiffResult = {
24-
info: GithubDiff;
24+
info: Promise<GithubDiff>;
2525
response: Promise<string>;
2626
};
2727

@@ -112,7 +112,7 @@ export async function fetchGithubPRComparison(token: string | null, owner: strin
112112
const base = prInfo.base.sha;
113113
const head = prInfo.head.sha;
114114
const title = `${prInfo.title} (#${prInfo.number})`;
115-
return await fetchGithubComparison(token, owner, repo, base, head, title, prInfo.html_url);
115+
return fetchGithubComparison(token, owner, repo, base, head, title, prInfo.html_url);
116116
}
117117

118118
function injectOptionalToken(token: string | null, opts: RequestInit) {
@@ -124,7 +124,7 @@ function injectOptionalToken(token: string | null, opts: RequestInit) {
124124
}
125125
}
126126

127-
export async function fetchGithubPRInfo(token: string | null, owner: string, repo: string, prNumber: string): Promise<GithubPR> {
127+
async function fetchGithubPRInfo(token: string | null, owner: string, repo: string, prNumber: string): Promise<GithubPR> {
128128
const opts: RequestInit = {
129129
headers: {
130130
Accept: "application/json",
@@ -152,67 +152,74 @@ export function parseMultiFilePatchGithub(details: GithubDiff, patch: string, lo
152152
});
153153
}
154154

155-
export async function fetchGithubComparison(
155+
export function fetchGithubComparison(
156156
token: string | null,
157157
owner: string,
158158
repo: string,
159159
base: string,
160160
head: string,
161161
description?: string,
162162
url?: string,
163-
): Promise<GithubDiffResult> {
164-
const opts: RequestInit = {
165-
headers: {
166-
Accept: "application/vnd.github.v3.diff",
167-
},
163+
): GithubDiffResult {
164+
return {
165+
info: (async () => {
166+
if (!url) {
167+
url = `https://github.com/${owner}/${repo}/compare/${base}...${head}`;
168+
}
169+
if (!description) {
170+
description = `Comparing ${trimCommitHash(base)}...${trimCommitHash(head)}`;
171+
}
172+
return { owner, repo, base, head, description, backlink: url };
173+
})(),
174+
response: (async () => {
175+
const opts: RequestInit = {
176+
headers: {
177+
Accept: "application/vnd.github.v3.diff",
178+
},
179+
};
180+
injectOptionalToken(token, opts);
181+
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/compare/${base}...${head}`, opts);
182+
if (!response.ok) {
183+
throw Error(`Failed to retrieve comparison (${response.status}): ${await response.text()}`);
184+
}
185+
return await response.text();
186+
})(),
168187
};
169-
injectOptionalToken(token, opts);
170-
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/compare/${base}...${head}`, opts);
171-
if (response.ok) {
172-
if (!description) {
173-
description = `Comparing ${trimCommitHash(base)}...${trimCommitHash(head)}`;
174-
}
175-
if (!url) {
176-
url = `https://github.com/${owner}/${repo}/compare/${base}...${head}`;
177-
}
178-
const info = { owner, repo, base, head, description, backlink: url };
179-
return { response: response.text(), info };
180-
} else {
181-
throw Error(`Failed to retrieve comparison (${response.status}): ${await response.text()}`);
182-
}
183188
}
184189

185-
export async function fetchGithubCommitDiff(token: string | null, owner: string, repo: string, commit: string): Promise<GithubDiffResult> {
186-
const diffOpts: RequestInit = {
187-
headers: {
188-
Accept: "application/vnd.github.v3.diff",
189-
},
190-
};
191-
injectOptionalToken(token, diffOpts);
190+
export function fetchGithubCommitDiff(token: string | null, owner: string, repo: string, commit: string): GithubDiffResult {
192191
const url = `https://api.github.com/repos/${owner}/${repo}/commits/${commit}`;
193-
const response = await fetch(url, diffOpts);
194-
if (response.ok) {
195-
const metaOpts: RequestInit = {
196-
headers: {
197-
Accept: "application/vnd.github+json",
198-
},
199-
};
200-
injectOptionalToken(token, metaOpts);
201-
const metaResponse = await fetch(url, metaOpts);
202-
if (!metaResponse.ok) {
203-
throw Error(`Failed to retrieve commit meta (${metaResponse.status}): ${await metaResponse.text()}`);
204-
}
205-
const meta: GithubCommitDetails = await metaResponse.json();
206-
const firstParent = meta.parents[0].sha;
207-
const description = `${meta.commit.message.split("\n")[0]} (${trimCommitHash(commit)})`;
208-
const info = { owner, repo, base: firstParent, head: commit, description, backlink: meta.html_url };
209-
return {
210-
response: response.text(),
211-
info,
212-
};
213-
} else {
214-
throw Error(`Failed to retrieve commit diff (${response.status}): ${await response.text()}`);
215-
}
192+
return {
193+
info: (async () => {
194+
const metaOpts: RequestInit = {
195+
headers: {
196+
Accept: "application/vnd.github+json",
197+
},
198+
};
199+
injectOptionalToken(token, metaOpts);
200+
const metaResponse = await fetch(url, metaOpts);
201+
if (!metaResponse.ok) {
202+
throw Error(`Failed to retrieve commit meta (${metaResponse.status}): ${await metaResponse.text()}`);
203+
}
204+
const meta: GithubCommitDetails = await metaResponse.json();
205+
const firstParent = meta.parents[0].sha;
206+
const description = `${meta.commit.message.split("\n")[0]} (${trimCommitHash(commit)})`;
207+
return { owner, repo, base: firstParent, head: commit, description, backlink: meta.html_url };
208+
})(),
209+
response: (async () => {
210+
const diffOpts: RequestInit = {
211+
headers: {
212+
Accept: "application/vnd.github.v3.diff",
213+
},
214+
};
215+
injectOptionalToken(token, diffOpts);
216+
const response = await fetch(url, diffOpts);
217+
if (!response.ok) {
218+
throw Error(`Failed to retrieve commit diff (${response.status}): ${await response.text()}`);
219+
}
220+
return await response.text();
221+
})(),
222+
};
216223
}
217224

218225
export async function fetchGithubFile(token: string | null, owner: string, repo: string, path: string, ref: string): Promise<Blob> {

0 commit comments

Comments
 (0)