fix: on safari, dialogs not showing anymore after clicking download in file-menu

Massimo Melina committed Apr 13, 2025 at 11:14 UTC 591cba24ffda7c23c6a05707a22201c975989565
1 file changed +7 -5
shared/dialogs.ts
+7 -5
@@ -77,11 +77,13 @@ let waitClosing = Promise.resolve()
77 let ignorePopState = false
78 async function back() {
79 ignorePopState = true
80 - let was = history.state
81 - history.back()
82 - return waitClosing = waitClosing.then(() => new Promise<void>(res => {
83 - const h = setInterval(() => was !== history.state && res() , 10)
84 - setTimeout(() => clearTimeout(h), 500)
80 + const was = history.state
81 + return waitClosing = waitClosing.then(() => new Promise<void>(async res => {
82 + await wait(10) // this is necessary for safari in case we close a dialog while it is processing a change of url (after clicking a file to download). This could be avoided by adding a target=_blank on the link, but we want to be agnostic about it. In my tests, 2ms is the minimum necessary, but 10 is safer.
83 + history.back()
84 + // wait for history.back to change history.state, up to 500ms. Events popstate and pageshow have no consistent behavior, so polling is necessary.
85 + const t = setInterval(() => was !== history.state && res(), 10)
86 + setTimeout(() => clearTimeout(t), 500)
87 }))
88 }
89