Skip to content

Commit

Permalink
Deprecate and avoid unload event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Jul 18, 2024
1 parent 4aee3bd commit f806e66
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 35 deletions.
18 changes: 14 additions & 4 deletions user/src/com/google/gwt/user/client/Window.java
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ public HandlerManager getHandlers() {
// Package protected for testing.
static WindowHandlers handlers;
private static boolean closeHandlersInitialized;
private static boolean beforeCloseHandlersInitialized;
private static boolean scrollHandlersInitialized;
private static boolean resizeHandlersInitialized;
private static int lastResizeWidth;
Expand All @@ -518,7 +519,10 @@ public HandlerManager getHandlers() {
*
* @param handler the handler
* @return returns the handler registration
* @deprecated This method requires the use of the {@code unload} browser event, which is
* deprecated in all browsers.
*/
@Deprecated
public static HandlerRegistration addCloseHandler(CloseHandler<Window> handler) {
maybeInitializeCloseHandlers();
return addHandler(CloseEvent.getType(), handler);
Expand All @@ -531,7 +535,6 @@ public static HandlerRegistration addCloseHandler(CloseHandler<Window> handler)
* @return returns the handler registration
*/
public static HandlerRegistration addResizeHandler(ResizeHandler handler) {
maybeInitializeCloseHandlers();
maybeInitializeResizeHandlers();
return addHandler(ResizeEvent.getType(), handler);
}
Expand All @@ -556,7 +559,7 @@ public static void addWindowCloseListener(WindowCloseListener listener) {
*/
public static HandlerRegistration addWindowClosingHandler(
ClosingHandler handler) {
maybeInitializeCloseHandlers();
maybeInitializeBeforeCloseHandlers();
return addHandler(Window.ClosingEvent.getType(), handler);
}

Expand All @@ -579,7 +582,6 @@ public static void addWindowResizeListener(WindowResizeListener listener) {
*/
public static HandlerRegistration addWindowScrollHandler(
Window.ScrollHandler handler) {
maybeInitializeCloseHandlers();
maybeInitializeScrollHandlers();
return addHandler(Window.ScrollEvent.getType(), handler);
}
Expand Down Expand Up @@ -910,13 +912,21 @@ private static WindowHandlers getHandlers() {
return handlers;
}

@Deprecated
private static void maybeInitializeCloseHandlers() {
if (GWT.isClient() && !closeHandlersInitialized) {
impl.initWindowCloseHandler();
impl.initWindowUnloadHandler();
closeHandlersInitialized = true;
}
}

private static void maybeInitializeBeforeCloseHandlers() {
if (GWT.isClient() && !beforeCloseHandlersInitialized) {
impl.initWindowBeforeUnloadHandler();
beforeCloseHandlersInitialized = true;
}
}

private static void maybeInitializeResizeHandlers() {
if (GWT.isClient() && !resizeHandlersInitialized) {
impl.initWindowResizeHandler();
Expand Down
43 changes: 26 additions & 17 deletions user/src/com/google/gwt/user/client/impl/WindowImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,30 @@ public native String getHash() /*-{
public native String getQueryString() /*-{
return $wnd.location.search;
}-*/;

public native void initWindowCloseHandler() /*-{

@Deprecated
public void initWindowCloseHandler() {
initWindowUnloadHandler();
initWindowBeforeUnloadHandler();
}

@Deprecated
public native void initWindowUnloadHandler() /*-{
var oldOnUnload = $wnd.onunload;
$wnd.onunload = $entry(function(evt) {
try {
@com.google.gwt.user.client.Window::onClosed()();
} finally {
oldOnUnload && oldOnUnload(evt);
$wnd.onunload = null;
}
});
}-*/;

public native void initWindowBeforeUnloadHandler() /*-{
var oldOnBeforeUnload = $wnd.onbeforeunload;
var oldOnUnload = $wnd.onunload;
// Old mozilla doesn't like $entry's explicit return statement and
// will always pop up a confirmation dialog. This is worked around by
// just wrapping the call to onClosing(), which still has the semantics
Expand All @@ -43,7 +62,9 @@ public native void initWindowCloseHandler() /*-{
try {
ret = $entry(@com.google.gwt.user.client.Window::onClosing())();
} finally {
oldRet = oldOnBeforeUnload && oldOnBeforeUnload(evt);
try {
oldRet = oldOnBeforeUnload && oldOnBeforeUnload(evt);
} catch (ignored) {}
}
// Avoid returning null as IE6 will coerce it into a string.
// Ensure that "" gets returned properly.
Expand All @@ -55,18 +76,6 @@ public native void initWindowCloseHandler() /*-{
}
// returns undefined.
};
$wnd.onunload = $entry(function(evt) {
try {
@com.google.gwt.user.client.Window::onClosed()();
} finally {
oldOnUnload && oldOnUnload(evt);
$wnd.onresize = null;
$wnd.onscroll = null;
$wnd.onbeforeunload = null;
$wnd.onunload = null;
}
});
}-*/;

public native void initWindowResizeHandler() /*-{
Expand Down
14 changes: 0 additions & 14 deletions user/src/com/google/gwt/user/client/ui/RootPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
import com.google.gwt.dom.client.BodyElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.i18n.client.BidiUtils;
import com.google.gwt.i18n.client.HasDirection;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Window;

import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -188,8 +185,6 @@ public static RootPanel get(String id) {
// on the first RootPanel.get(String) or RootPanel.get()
// call.
if (rootPanels.size() == 0) {
hookWindowClosing();

// If we're in a RTL locale, set the RTL directionality
// on the entire document.
if (LocaleInfo.getCurrentLocale().isRTL()) {
Expand Down Expand Up @@ -258,15 +253,6 @@ private static native Element getRootElement() /*-{
return $doc;
}-*/;

private static void hookWindowClosing() {
// Catch the window closing event.
Window.addCloseHandler(new CloseHandler<Window>() {
public void onClose(CloseEvent<Window> closeEvent) {
detachWidgets();
}
});
}

/*
* Checks to see whether the given element has any parent element that
* belongs to a widget. This is not terribly efficient, and is thus only used
Expand Down

0 comments on commit f806e66

Please sign in to comment.