Skip to content

Commit 2851efb

Browse files
committed
chore: extract-method isCacheFresh()
1 parent c17ae72 commit 2851efb

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/versions.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,6 @@ export class BaseVersions implements Versions {
183183
}
184184
}
185185

186-
const VERSION_CACHE_TTL_MS = 4 * 60 * 60 * 1000; // cache for N hours
187-
188186
/**
189187
* Implementation of Versions that self-populates from release information at
190188
* https://releases.electronjs.org/releases.json .
@@ -210,6 +208,11 @@ export class ElectronVersions extends BaseVersions {
210208
return json;
211209
}
212210

211+
private static isCacheFresh(cacheTimeMs: number, now: number): boolean {
212+
const VERSION_CACHE_TTL_MS = 4 * 60 * 60 * 1000; // cache for N hours
213+
return now <= cacheTimeMs + VERSION_CACHE_TTL_MS;
214+
}
215+
213216
public static async create(
214217
paths: Partial<Paths> = {},
215218
): Promise<ElectronVersions> {
@@ -220,10 +223,10 @@ export class ElectronVersions extends BaseVersions {
220223
const now = Date.now();
221224
try {
222225
const st = await fs.stat(versionsCache);
223-
if (now <= st.mtimeMs + VERSION_CACHE_TTL_MS)
226+
if (ElectronVersions.isCacheFresh(st.mtimeMs, now))
224227
versions = (await fs.readJson(versionsCache)) as unknown;
225228
} catch (err) {
226-
// cache file missing
229+
// cache file missing or cannot be read
227230
}
228231

229232
if (!versions) {
@@ -244,7 +247,7 @@ export class ElectronVersions extends BaseVersions {
244247
// if it's still fresh, do nothing
245248
const { mtimeMs, versionsCache } = this;
246249
const now = Date.now();
247-
if (now <= mtimeMs + VERSION_CACHE_TTL_MS) return;
250+
if (ElectronVersions.isCacheFresh(mtimeMs, now)) return;
248251

249252
// update the cache
250253
try {

0 commit comments

Comments
 (0)