Skip to content
This repository has been archived by the owner on Feb 26, 2022. It is now read-only.

Commit

Permalink
Bug 812417: Listen for domwindowclosed instead of unload when closing a
Browse files Browse the repository at this point in the history
toplevel window.
  • Loading branch information
ochameau committed Apr 15, 2013
1 parent 566553d commit ccd30b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 12 additions & 4 deletions lib/sdk/window/helpers.js
Expand Up @@ -4,6 +4,7 @@
'use strict';

const { defer } = require('../core/promise');
const events = require('../system/events');
const { open: openWindow, onFocus } = require('./utils');

function open(uri, options) {
Expand All @@ -12,11 +13,18 @@ function open(uri, options) {
exports.open = open;

function close(window) {
// unload event could happen so fast that it is not resolved
// if we listen to unload after calling close()
let p = promise(window, 'unload');
// We shouldn't wait for unload, as it is dispatched
// before the window is actually closed.
// `domwindowclosed` is a better match.
let deferred = defer();
events.on("domwindowclosed", function onclose({subject}) {
if (subject == window) {
events.off("domwindowclosed", onclose);
deferred.resolve(window);
}
}, true);
window.close();
return p;
return deferred.promise;
}
exports.close = close;

Expand Down
5 changes: 4 additions & 1 deletion test/test-window-utils2.js
Expand Up @@ -61,7 +61,10 @@ exports.testBackgroundify = function(assert, done) {
'backgroundifyied window is in the list of windows');

// Wait for the window unload before ending test
close(window).then(done);
// backgroundified windows doesn't dispatch domwindowclosed event
// so that we have to manually wait for unload event
window.onunload = done;
window.close();
};

exports.testIsBrowser = function(assert) {
Expand Down

0 comments on commit ccd30b4

Please sign in to comment.