better code: leverage new debounceAsync.retain
Massimo Melina committed
Sep 17, 2023 at 18:06 UTC
5064b8cc5dfe64b8f0fae32ff7075b9f5c9ecc2e
1 file changed
+5
-15
src/github.ts
+5
-15
@@ -1,7 +1,7 @@
1
// This file is part of HFS - Copyright 2021-2023, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
2
3
import events from './events'
4
-import { DAY, httpString, httpStream, unzip, AsapStream } from './misc'
4
+import { DAY, httpString, httpStream, unzip, AsapStream, debounceAsync } from './misc'
5
import {
6
DISABLING_POSTFIX, findPluginByRepo,
7
getAvailablePlugins,
@@ -192,18 +192,8 @@ export async function searchPlugins(text='') {
192
}
193
194
// centralized hosted information, to be used as little as possible
195
-let cache
195
const FN = 'central.json'
197
-let latest = JSON.parse(readFileSync(join(__dirname, '..', FN), 'utf8')) // initially built-in is our latest
198
-export function getProjectInfo() {
199
- return cache ||= readGithubFile(`${HFS_REPO}/${HFS_REPO_BRANCH}/${FN}`)
200
- .catch(() => latest) // fall back to latest
201
- .then(x => {
202
- if (!x) throw x // go catch
203
- setTimeout(() => cache = undefined, DAY) // invalidate cache
204
- return latest = JSON.parse(x)
205
- }).catch(() => { // schedule next attempt
206
- setTimeout(() => cache = undefined, 10_000) // invalidate cache sooner on errors
207
- return latest
208
- })
209
-}
\ No newline at end of file
196
+let builtIn = JSON.parse(readFileSync(join(__dirname, '..', FN), 'utf8'))
197
+export const getProjectInfo = debounceAsync(
198
+ () => readGithubFile(`${HFS_REPO}/${HFS_REPO_BRANCH}/${FN}`).then(JSON.parse, () => builtIn), // fall back to latest
199
+ 0, { retain: DAY, retainFailure: 60_000 } )
\ No newline at end of file