Skip to content

Instantly share code, notes, and snippets.

@natesilva
Created September 10, 2012 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natesilva/3692687 to your computer and use it in GitHub Desktop.
Save natesilva/3692687 to your computer and use it in GitHub Desktop.
Chocolat mixin onUnload does not fire when window is closed using the red "X"
//
// To test:
//
// (Use this in a Chocolat Mixin. Drop in any default.html file.)
//
// 1. Press Cmd-Opt-Ctrl-H and note the window appears.
// 2. Press "OK" and note that onUnload fires.
// 3. Repeat several times until you're convinced it works.
//
// Then:
//
// 4. Press Cmd-Opt-Ctrl-H. The window will appear.
// 5. Close the window using the red "X" in the corner. Note that
// onUnload does not fire.
// 6. Press Cmd-Opt-Ctrl-H. The window will *probably* appear.
// 7. Close it again using the red "X".
// 8. Open again and close using the red "X". After the second or
// third time it no longer works.
//
// Hypothesis: Closing the window with the red "X" deallocates it.
// When it's re-opened, we are seeing a window that has been or is
// about to be deallocated. It just happens to work--at first.
//
// Problem: Without a way to detect if the window exists or has been
// deallocated, it's hard to have a clean UI for mixins like JSHint.
//
// global, so we can try to re-use the window
var win;
Hooks.addMenuItem('Actions/JavaScript/Testing 1 2 3', 'cmd-opt-ctrl-h', function() {
if (win === undefined) {
win = new Window();
win.htmlPath = 'default.html';
win.onUnload = function() {
Alert.show('debug', 'onUnload', ['OK']);
win = undefined;
};
win.buttons = ["OK"];
win.onButtonClick = function() {
// Note: onUnload will be fired.
win.close();
};
win.run();
} else {
// this may work one or two times, but then it will fail
win.show();
}
});
@natesilva
Copy link
Author

Also, win.isVisible() will eventually return false but calling win.show() does not show it. Because the window has been deallocated?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment