@samitouri / QOSami-HFS / commits / af545e25

better code

Massimo Melina committed Jun 25, 2024 at 22:13 UTC af545e25fb4aad2ec80a4c17b1fee5432b956ead
7 files changed +12 -12
src/acme.ts
+2 -2
@@ -106,7 +106,7 @@ export const makeCert = debounceAsync(async (domain: string, email?: string, alt
106 await fs.writeFile(KEY_FILE, res.key)
107 cert.set(CERT_FILE) // update config
108 privateKey.set(KEY_FILE)
109 -}, 0)
109 +})
110
111 const acmeDomain = defineConfig('acme_domain', '')
112 const acmeEmail = defineConfig('acme_email', '')
@@ -126,5 +126,5 @@ const renewCert = debounceAsync(async () => {
126 return console.log("certificate still good")
127 await makeCert(domain, acmeEmail.get(), altNames)
128 .catch(e => console.log("error renewing certificate: ", String(e.message || e)))
129 -}, 0, { retain: DAY, retainFailure: HOUR })
129 +}, { retain: DAY, retainFailure: HOUR })
130
src/debounceAsync.ts
+4 -4
@@ -4,9 +4,9 @@
4 export function debounceAsync<Cancelable extends boolean = false, A extends unknown[] = unknown[], R = unknown>(
5 // the function you want to not call too often, too soon
6 callback: (...args: A) => Promise<R>,
7 - // time to wait after invocation of the debounced function. If you call again while waiting, the timer starts again.
8 - wait: number=100,
7 options: {
8 + // time to wait after invocation of the debounced function. If you call again while waiting, the timer starts again.
9 + wait?: number,
10 // in a train of invocations, should we execute also the first one, or just the last one?
11 leading?: boolean,
12 // since the wait-ing is renewed at each invocation, indefinitely, do you want to put a cap to it?
@@ -21,7 +21,7 @@ export function debounceAsync<Cancelable extends boolean = false, A extends unkn
21 ) {
22 type MaybeUndefined<T> = Cancelable extends true ? undefined | T : T
23 type MaybeR = MaybeUndefined<R>
24 - const { leading=false, maxWait=Infinity, cancelable=false, retain=0, retainFailure } = options
24 + const { wait=0, leading=false, maxWait=Infinity, cancelable=false, retain=0, retainFailure } = options
25 let started = 0 // latest callback invocation
26 let runningCallback: Promise<R> | undefined // latest callback invocation result
27 let latestDebouncer: Promise<MaybeR | R> // latest wrapper invocation
@@ -90,7 +90,7 @@ export function singleWorkerFromBatchWorker<Args extends any[]>(batchWorker: (ba
90 const ret = batchWorker(batch)
91 batch = [] // this is reset as batchWorker starts, but without waiting
92 return ret
93 - }, 100, { maxWait })
93 + }, { wait: 100, maxWait })
94 return (...args: Args) => {
95 const idx = batch.push(args) - 1
96 return debounced().then((x: any) => x[idx])
src/github.ts
+1 -1
@@ -219,4 +219,4 @@ export const getProjectInfo = debounceAsync(
219 () => readGithubFile(`${HFS_REPO}/${HFS_REPO_BRANCH}/${FN}`)
220 .then(JSON.parse, () => null)
221 .then(x => Object.assign({ ...builtIn }, DEV ? null : x) ), // fall back to built-in
222 - 0, { retain: DAY, retainFailure: 60_000 } )
\ No newline at end of file
222 + { retain: DAY, retainFailure: 60_000 })
\ No newline at end of file
src/nat.ts
+2 -2
@@ -28,7 +28,7 @@ export const defaultBaseUrl = proxy({
28 export const upnpClient = new Client({ timeout: 4_000 })
29 const originalMethod = upnpClient.getGateway
30 // other client methods call getGateway too, so this will ensure they reuse this same result
31 -upnpClient.getGateway = debounceAsync(() => originalMethod.apply(upnpClient), 0, { retain: HOUR, retainFailure: 30_000 })
31 +upnpClient.getGateway = debounceAsync(() => originalMethod.apply(upnpClient), { retain: HOUR, retainFailure: 30_000 })
32 upnpClient.getGateway().then(res => {
33 console.debug('upnp', res.gateway.description)
34 }, e => console.debug('upnp failed:', e.message || String(e)))
@@ -60,7 +60,7 @@ export const getPublicIps = debounceAsync(async () => {
60 return validIps
61 }) )))
62 return defaultBaseUrl.publicIps = _.uniq(ips.flat())
63 -}, 0, { retain: 10 * MINUTE })
63 +}, { retain: 10 * MINUTE })
64
65 export const getNatInfo = debounceAsync(async () => {
66 const res = await haveTimeout(10_000, upnpClient.getGateway()).catch(() => null)
src/plugins.ts
+1 -1
@@ -308,7 +308,7 @@ export function getAvailablePlugins() {
308 return Object.values(availablePlugins)
309 }
310
311 -const rescanAsap = debounceAsync(rescan, 1000)
311 +const rescanAsap = debounceAsync(rescan, { wait: 1000 })
312 if (!existsSync(PATH))
313 try { mkdirSync(PATH) }
314 catch {}
src/serveGuiFiles.ts
+1 -1
@@ -65,7 +65,7 @@ function adjustBundlerLinks(ctx: Koa.Context, uri: string, data: string | Buffer
65 const getFaviconTimestamp = debounceAsync(async () => {
66 const f = favicon.get()
67 return !f ? 0 : fs.stat(f).then(x => x?.mtimeMs || 0, () => 0)
68 -}, 0, { retain: 5_000 })
68 +}, { retain: 5_000 })
69
70 async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
71 const session = await refresh_session({}, ctx)
src/watchLoad.ts
+1 -1
@@ -13,7 +13,7 @@ interface WatchLoadReturn { unwatch:WatchLoadCanceller, save: WriteFile, getText
13 export function watchLoad(path:string, parser:(data:any)=>void|Promise<void>, { failedOnFirstAttempt, immediateFirst }:Options={}): WatchLoadReturn {
14 let doing = false
15 let watcher: FSWatcher | undefined
16 - const debounced = debounceAsync(load, 500, { maxWait: 1000 })
16 + const debounced = debounceAsync(load, { wait: 500, maxWait: 1000 })
17 let retry: NodeJS.Timeout
18 let last: string | undefined
19 install(true)