|
1 | | -// Only fetch new release.json if more than 5 minutes have passed. |
2 | | -const configFetchInterval = 5 * 60 * 1000; |
| 1 | +const DEFAULT_FETCH_INTERVAL = 5 * 60 * 1000; |
3 | 2 |
|
4 | | -// Fetched release data. |
5 | | -let releaseData = null; |
| 3 | +const DEFAULT_RELEASE = { |
| 4 | + releaseTime: null, |
| 5 | + releaseTimestamp: null, |
| 6 | + releaseVersion: null, |
| 7 | +}; |
6 | 8 |
|
7 | | -// Last time the release was fetched. |
8 | | -let latestFetchTimestamp = 0; |
| 9 | +class ReleaseLoader { |
| 10 | + #releaseData = null; |
| 11 | + #latestFetchTimestamp = null; |
| 12 | + #activePromise = null; |
| 13 | + #fetchFn; |
| 14 | + #nowFn; |
| 15 | + #fetchInterval; |
9 | 16 |
|
10 | | -let activePromise = null; |
| 17 | + /** |
| 18 | + * @param {object} options |
| 19 | + * @param {Function} options.fetchFn - Fetch implementation. Defaults to global fetch. |
| 20 | + * @param {Function} options.nowFn - Returns current time in ms. Defaults to Date.now. |
| 21 | + * @param {number} options.fetchInterval - Cache lifetime in ms. Defaults to 5 minutes. |
| 22 | + */ |
| 23 | + constructor({ |
| 24 | + fetchFn = (...args) => fetch(...args), |
| 25 | + nowFn = () => Date.now(), |
| 26 | + fetchInterval = DEFAULT_FETCH_INTERVAL, |
| 27 | + } = {}) { |
| 28 | + this.#fetchFn = fetchFn; |
| 29 | + this.#nowFn = nowFn; |
| 30 | + this.#fetchInterval = fetchInterval; |
| 31 | + } |
11 | 32 |
|
12 | | -const ReleaseLoader = { |
13 | 33 | async loadRelease() { |
14 | | - if (activePromise !== null) { |
15 | | - return activePromise; |
| 34 | + if (this.#activePromise !== null) { |
| 35 | + return this.#activePromise; |
16 | 36 | } |
17 | 37 |
|
18 | | - const nowTimestamp = new Date().getTime(); |
| 38 | + const nowTimestamp = this.#nowFn(); |
19 | 39 |
|
20 | 40 | // Return early without going through activePromise so the caller always |
21 | 41 | // receives a real promise, not null. |
22 | | - if (latestFetchTimestamp + configFetchInterval >= nowTimestamp) { |
23 | | - return Promise.resolve(releaseData); |
| 42 | + if ( |
| 43 | + this.#latestFetchTimestamp !== null && |
| 44 | + this.#latestFetchTimestamp + this.#fetchInterval >= nowTimestamp |
| 45 | + ) { |
| 46 | + return Promise.resolve(this.#releaseData); |
24 | 47 | } |
25 | 48 |
|
26 | | - activePromise = fetch(`/release.json?t=${nowTimestamp}`) |
| 49 | + this.#activePromise = this.#fetchFn(`/release.json?t=${nowTimestamp}`) |
27 | 50 | .then((response) => response.json()) |
28 | 51 | .then((data) => { |
29 | | - latestFetchTimestamp = nowTimestamp; |
30 | | - releaseData = data; |
31 | | - return releaseData; |
| 52 | + this.#latestFetchTimestamp = nowTimestamp; |
| 53 | + this.#releaseData = data; |
| 54 | + return this.#releaseData; |
32 | 55 | }) |
33 | 56 | .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; |
| 57 | + if (this.#releaseData !== null) { |
| 58 | + // Advance the timestamp so the next call uses the cache instead of |
| 59 | + // immediately retrying after a failed fetch. |
| 60 | + this.#latestFetchTimestamp = nowTimestamp; |
| 61 | + return this.#releaseData; |
39 | 62 | } |
40 | 63 |
|
41 | 64 | /* eslint-disable-next-line no-console */ |
42 | 65 | console.warn("Could not find release.json. Returning defaults."); |
43 | 66 |
|
44 | | - // Return defaults. |
45 | | - return { |
46 | | - releaseTime: null, |
47 | | - releaseTimestamp: null, |
48 | | - releaseVersion: null, |
49 | | - }; |
| 67 | + return DEFAULT_RELEASE; |
50 | 68 | }) |
51 | 69 | .finally(() => { |
52 | 70 | // 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; |
| 71 | + // single in-flight fetch. It is cleared on both success and failure. |
| 72 | + this.#activePromise = null; |
55 | 73 | }); |
56 | 74 |
|
57 | | - return activePromise; |
58 | | - }, |
59 | | -}; |
| 75 | + return this.#activePromise; |
| 76 | + } |
| 77 | +} |
60 | 78 |
|
61 | | -Object.freeze(ReleaseLoader); |
| 79 | +// Default singleton for production use. |
| 80 | +const releaseLoader = new ReleaseLoader(); |
| 81 | +export default releaseLoader; |
62 | 82 |
|
63 | | -export default ReleaseLoader; |
| 83 | +export { ReleaseLoader }; |
0 commit comments