Skip to content

Commit 0f031ae

Browse files
committed
6871: Fixed bugs in releaseLoader
1 parent 7c860de commit 0f031ae

1 file changed

Lines changed: 39 additions & 32 deletions

File tree

assets/shared/release-loader.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,48 @@ let activePromise = null;
1111

1212
const ReleaseLoader = {
1313
async loadRelease() {
14-
if (activePromise) {
14+
if (activePromise !== null) {
1515
return activePromise;
1616
}
1717

18-
activePromise = new Promise((resolve, reject) => {
19-
const nowTimestamp = new Date().getTime();
20-
21-
if (latestFetchTimestamp + configFetchInterval >= nowTimestamp) {
22-
resolve(releaseData);
23-
} else {
24-
fetch(`/release.json?t=${nowTimestamp}`)
25-
.then((response) => response.json())
26-
.then((data) => {
27-
latestFetchTimestamp = nowTimestamp;
28-
releaseData = data;
29-
resolve(releaseData);
30-
})
31-
.catch((err) => {
32-
if (releaseData !== null) {
33-
resolve(releaseData);
34-
} else {
35-
/* eslint-disable-next-line no-console */
36-
console.warn("Could not find release.json. Returning defaults.");
37-
38-
return {
39-
releaseTimestamp: null,
40-
releaseVersion: null,
41-
};
42-
}
43-
})
44-
.finally(() => {
45-
activePromise = null;
46-
});
47-
}
48-
});
18+
const nowTimestamp = new Date().getTime();
19+
20+
// Return early without going through activePromise so the caller always
21+
// receives a real promise, not null.
22+
if (latestFetchTimestamp + configFetchInterval >= nowTimestamp) {
23+
return Promise.resolve(releaseData);
24+
}
25+
26+
activePromise = fetch(`/release.json?t=${nowTimestamp}`)
27+
.then((response) => response.json())
28+
.then((data) => {
29+
latestFetchTimestamp = nowTimestamp;
30+
releaseData = data;
31+
return releaseData;
32+
})
33+
.catch(() => {
34+
if (releaseData !== null) {
35+
// Bug 3 fix: advance the timestamp so the next call uses the
36+
// cache instead of immediately retrying after a failed fetch.
37+
latestFetchTimestamp = nowTimestamp;
38+
return releaseData;
39+
}
40+
41+
/* eslint-disable-next-line no-console */
42+
console.warn("Could not find release.json. Returning defaults.");
43+
44+
// Return defaults.
45+
return {
46+
releaseTime: null,
47+
releaseTimestamp: null,
48+
releaseVersion: null,
49+
};
50+
})
51+
.finally(() => {
52+
// Always clear activePromise via finally so concurrent callers share a
53+
// single in-flight fetch and it is cleared on both success and failure.
54+
activePromise = null;
55+
});
4956

5057
return activePromise;
5158
},

0 commit comments

Comments
 (0)