better code: avoid setTimeout
Massimo Melina committed
Sep 21, 2024 at 15:08 UTC
f7420e182f5f970725f5d65a3f7ffad2275ba293
4 files changed
+12
-6
admin/src/InternetPage.ts
+7
-1
@@ -366,7 +366,13 @@ export default function InternetPage() {
366
h('li', {}, "There could be a firewall, try configuring or disabling it."),
367
(data.externalPort || data.internalPort!) <= 1024 && h('li', {},
368
"Your Internet Provider may be blocking ports under 1024. ",
369
- data.upnp && h(Button, { size: 'small', onClick() { close(); mapPort(HIGHER_PORT).then(verifyAgain) } }, "Try " + HIGHER_PORT) ),
369
+ data.upnp && h(Button, {
370
+ size: 'small',
371
+ onClick() {
372
+ close();
373
+ mapPort(HIGHER_PORT).then(verifyAgain)
374
+ }
375
+ }, "Try " + HIGHER_PORT)),
376
data.mapped && h('li', {}, "A bug in your modem/router, try rebooting it."),
377
h('li', {}, MSG_ISP),
378
)), 'warning')
frontend/src/fileMenu.ts
+1
-2
@@ -150,8 +150,7 @@ async function rename(entry: DirEntry) {
150
const MSG = t`Operation successful`
151
if (uri === location.pathname) //current folder
152
return alertDialog(MSG).then(() =>
153
- setTimeout(() => // after history.back() issued by closing the dialog
154
- getHFS().navigate(uri + '../' + pathEncode(dest) + '/') ))
153
+ getHFS().navigate(uri + '../' + pathEncode(dest) + '/') )
154
// update state instead of re-getting the list
155
const newN = n.replace(/(.*?)[^/]+(\/?)$/, (_,before,after) => before + dest + after)
156
const newEntry = new DirEntry(newN, { key: n, ...entry }) // by keeping old key, we avoid unmounting the element, that's causing focus lost
frontend/src/login.ts
+1
-1
@@ -117,7 +117,7 @@ export async function loginDialog(closable=true, reloadAfter=true) {
117
going = true
118
try {
119
const res = await login(usr, pwd)
120
- close(true)
120
+ await close(true)
121
toast(t`Logged in`, 'success')
122
if (res?.redirect)
123
setTimeout(() => // workaround: the history.back() issued by closing the dialog is messing with our navigation
shared/dialogs.ts
+3
-2
@@ -203,7 +203,7 @@ export function newDialog(options: DialogOptions) {
203
if (history.state?.$dialog === $id)
204
options.closed = back()
205
closeDialogAt(i, v)
206
- return options
206
+ return options.closed
207
}
208
}
209
@@ -227,7 +227,8 @@ function closeDialogAt(i: number, value?: any) {
227
const [d] = dialogs.splice(i,1)
228
d.restoreFocus?.focus?.() // if element is not HTMLElement, it doesn't have focus method
229
d.closingValue = value
230
- d?.onClose?.(value)
230
+ Promise.resolve(d.closed).then(() =>
231
+ d?.onClose?.(value))
232
return d
233
}
234