Skip to content

Commit 8f1ee56

Browse files
authored
frontend: Store file source in cache, refs #189. (#202)
1 parent ed2b949 commit 8f1ee56

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

frontend/src/common.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,25 @@ function monitorOptions() {
205205
}
206206

207207
// hgmo.
208+
const sourceCache = {};
209+
export async function getSource(file, revision) {
210+
if (!revision || revision === "latest") {
211+
revision = "tip";
212+
}
213+
const url = `https://hg.mozilla.org/mozilla-central/raw-file/${revision}/${file}`;
208214

209-
export async function getSource(file) {
210-
const response = await fetch(
211-
`https://hg.mozilla.org/mozilla-central/raw-file/tip/${file}`
212-
);
213-
return response.text();
215+
let source = cacheGet(sourceCache, url);
216+
if (source) {
217+
return source;
218+
}
219+
220+
const response = await fetch(url);
221+
source = await response.text();
222+
source = source.split("\n");
223+
224+
cacheSet(sourceCache, url, source);
225+
226+
return source;
214227
}
215228

216229
// Filtering.

frontend/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ async function showDirectory(dir, revision, files) {
129129
}
130130

131131
async function showFile(file, revision) {
132-
const source = await getSource(file.path);
132+
const source = await getSource(file.path, revision);
133133

134134
let language;
135135
if (file.path.endsWith("cpp") || file.path.endsWith("h")) {
@@ -150,7 +150,7 @@ async function showFile(file, revision) {
150150
navbar: buildNavbar(file.path, revision),
151151
revision: revision || REV_LATEST,
152152
language,
153-
lines: source.split("\n").map((line, nb) => {
153+
lines: source.map((line, nb) => {
154154
const coverage = file.coverage[nb];
155155
let cssClass = "";
156156
let hits = null;

0 commit comments

Comments
 (0)