bpd1069 / naclports

Automatically exported from code.google.com/p/naclports
0 stars 0 forks source link

SDL_QUIT for tab close event #224

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I received the following suggestion:

> Emscripten has an "emscripten_set_beforeunload_callback" which can react to 
tab closed events. We added this to Quake2 net_humblenet.cpp so Quake itself 
can properly close the connection. Perhaps SDL could use this callback to send 
an SDL_QUIT when the tab is closed?

sbc says:

> One problem I see this this approach is the tab closed event will almost 
certainly want to run synchronously, right?   And all the game code is running 
on a background thread.. so it could get messy/complicated.  Perhaps the new 
postMessageAndAwaitResponse might be useful here.
>
> I'm more than happy to work on making out SDK port better if we have real 
world use cases.

Original issue reported on code.google.com by j...@chromium.org on 22 Jun 2015 at 11:52

GoogleCodeExporter commented 9 years ago
I've run a few tests and I don't think there is a perfect solution here.

The onbeforeunload handler is called when the tab is closed, or surfed away, or 
reloaded. But the handler has a timeout (about a second, I think), after which 
it will complete the operation without displaying the dialog.

You can call postMessage from the handler, and the NaCl module will get the 
message asynchronously. But it is possible that the user will close the tab 
before the NaCl module is finished with whatever it is doing.

Calling postMessageAndAwaitResponse does work, but it is subject to the 
watchdog timer, which is potentially worse than waiting for the user to close 
the tab. 

It also looks like the DOM is not updated while the dialog is displayed. I 
tried posting a message from the NaCl module back to JavaScript (for example, 
to display something like "Save Finished"), but you won't see anything until 
you click "Stay on this page".

Original comment by binji@chromium.org on 23 Jun 2015 at 7:56