fix: avoid giving console messages twice about port listening

Massimo Melina committed Feb 9, 2022 at 12:00 UTC e101259ceb08ba8f6bfe6a7e22082b0380643358
2 files changed +10 -7
src/listen.ts
+5 -3
@@ -17,6 +17,7 @@ subscribeConfig<number>({ k:'port', defaultValue: 80 }, async port => {
17 await stopServer(httpSrv)
18 httpSrv = http.createServer(app.callback())
19 port = await startServer(httpSrv, { port, name:'http' })
20 + if (!port) return
21 httpSrv.on('connection', newConnection)
22 printUrls(port, 'http')
23 })
@@ -82,6 +83,7 @@ async function considerHttps() {
83 port: !cert || !key ? -1 : getConfig('https_port'),
84 name: 'https'
85 })
86 + if (!port) return
87 httpsSrv.on('connection', socket =>
88 newConnection(socket, true))
89 printUrls(port, 'https')
@@ -105,13 +107,13 @@ function startServer(srv: http.Server, { port, name, net='0.0.0.0' }: StartServe
107 resolve(ad.port)
108 }).on('error', e => {
109 const { code } = e as any
108 - console.error(code === 'EADDRINUSE' ? `couldn't listen on port ${port}` : e)
109 - reject(e)
110 + console.error(code === 'EADDRINUSE' ? `couldn't listen on busy port ${port}` : String(e))
111 + resolve(0)
112 })
113 }
114 catch(e) {
115 console.error("couldn't listen on port", port, String(e))
114 - reject(e)
116 + resolve(0)
117 }
118 })
119 }
src/misc.ts
+5 -4
@@ -164,13 +164,14 @@ export function onOffMap(em: EventEmitter, events: { [eventName:string]: (...arg
164 // avoid for an async function to be overlapped with another execution while awaiting
165 export function debounceAsync(cb: any, ms: number=100, ...args:any[]) {
166 const debounced = _.debounce(cb, ms, ...args)
167 - let busy = false
167 + let running: false | number = false
168 return async () => {
169 - while (busy)
169 + if (running && Date.now() - running < ms) return
170 + while (running)
171 await wait(ms)
171 - busy = true
172 + running = Date.now()
173 try { return await debounced() }
173 - finally { busy = false }
174 + finally { running = false }
175 }
176 }
177