#`onCloseRequested` not properly cleaning up?

36 messages Β· Page 1 of 1 (latest)

worthy tide
#

I'm using onCloseRequested to confirm shutdown of an app while it's doing certain critical work.
the code looks like:

const unlisten = await appWindow.onCloseRequested(async (ev) => {
    const confirmed = await confirm('[confirmation message]');
    if (!confirmed) {
        // user did not confirm closing the window; let's prevent it
        ev.preventDefault();
    }
});
// do work
unlisten()

However, after the work is done and unlisten() called, the window cannot be closed normally.
It doesn't ask for confirmation, it simply does nothing.

somber axle
#

If you call unlisten immediately then your listener effectively does nothing

#
// stop listening
unlisten()
worthy tide
#

no there's a bunch of time-consuming work in between

#

which was replaced with just // do work here because of irrelevance

somber axle
#

Ok so if I get it right you're adding the listener to prevent the user from exiting solely during the execution of the heavy work, after which you want it to not be registered anymore. Now it makes more sense πŸ™‚

Tonnes of users have copy pasted the code from the website and not thought about what unlisten actually does, that's why I thought that might be what you were doing πŸ™‚

worthy tide
#

yeah

somber axle
#

I'll have to set up a little project for myself to see if I can't replicate your behavior, I don't use the prevention enough nor the JS end of things to know what's wrong, so 1sec πŸ™‚

somber axle
#

Initial testing for me at least seems to work as you want πŸ€”

worthy tide
#

huh

#

it closes fine after unlisten?

somber axle
#

Yep

#

Gonna do a bit more testing before I say anything definitive but it's looking like it works for me atm

worthy tide
#

I wonder why it's not working for me

#

maybe something not working well with sveltekit?

somber axle
#

Waaaait a minute, think I'm encountering an issue here, gimme a sec

#

Huh, this is weird, it worked initially, then I experimented with faking some heavy computations and now it's not working anymore πŸ€”

worthy tide
#

wha-

somber axle
#

Ah wait now I know why it worked, because I never called unlisten in the initial mode. See this is why I said it looked fine during initial testing πŸ˜…

It seems like the unlisten command breaks things. If I had to guess wildly it unlistens more than what's intended or something like that, like, removing the actual close event handler, something like that

#

As a workaround I would just never call unlisten and instead use a boolean to check if it should be used

#

Something like this

        await appWindow.onCloseRequested(async (ev) => {
            if (self.unlisten) return
            const confirmed = await confirm("Do you want to proceed?");
            if (!confirmed) {
                ev.preventDefault();
            }
        });
#
        let counter = 0;
        self.inter = setInterval(() => {
            console.log("Hello from setInterval");
            counter += 1;
            if (counter > 5) {
                self.unlisten = true
                clearInterval(self.inter)
            }
        }, 1000);
#

The unlisten function definitely needs to be fixed because this is definitely not the expected behavior

#

Hope the temporary workaround works for you while we investigate πŸ˜…

worthy tide
#

thanks

#

should I go create an issue at the repo

somber axle
worthy tide
#

alright gimme a sec

#

making a minimal repro

worthy tide
#

okay #7119 is open

#

another workaround:

#

apparently re-registering an onCloseRequested handler fixes closing again?

somber axle
#

That's very weird 🀨

#

And thanks for making the issue πŸ€—

worthy tide