darkf/heart.js — pull request #11 by 00Davo (2014-01-01 08:34:41+00:00)
Title: Adds support for heart.quit() callback to correspond to love.quit()

`heart.quit()` is expected to follow the same API as `love.quit()`: It takes no arguments and returns a Boolean, which attempts to cancel the quit if true. http://love2d.org/wiki/love.quit It's triggered when the page is being unloaded, which is the closest DOM event corresponding to closing a LÖVE game, firing when the user closes the tab/window.

Note that this event does also fire when the user hits the back button or navigates to another page, but I think that's probably a good thing: `love.quit()` is often used to save game state, and one would not want to lose game state on navigation.

Because browsers, unlike LÖVE, don't let arbitrary JavaScript keep tabs open as much as it likes, the prevented-quit feature is a little silly. This is demonstrated by the provided example examples/quit.html , which will produce *two* confirmations if you try to cancel quitting on the first one.

Despite this browser limitation, the implementation is fine, and it'll be usable without silliness for other use-cases of `love.quit()`, such as saving the player's game state.