From: Leif Åstrand Date: Thu, 18 Apr 2013 13:05:40 +0000 (+0300) Subject: Load vaadinPush.js on demand (#11506) X-Git-Tag: 7.1.0.beta1~93 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=c44f8380b2f83920676fe993a240976d52437adf;p=vaadin-framework.git Load vaadinPush.js on demand (#11506) For optimal performance, the script will still be included in the host HTML if push is enabled when the UI is bootstrapped. Change-Id: I2245b45434f3097c32fd3580267f692dce3b7649 --- diff --git a/client/src/com/vaadin/client/ApplicationConfiguration.java b/client/src/com/vaadin/client/ApplicationConfiguration.java index a6cc3cf531..e6578fa018 100644 --- a/client/src/com/vaadin/client/ApplicationConfiguration.java +++ b/client/src/com/vaadin/client/ApplicationConfiguration.java @@ -261,7 +261,16 @@ public class ApplicationConfiguration implements EntryPoint { } public String getThemeUri() { - return vaadinDirUrl + "themes/" + getThemeName(); + return getVaadinDirUrl() + "themes/" + getThemeName(); + } + + /** + * Gets the URL of the VAADIN directory on the server. + * + * @return the URL of the VAADIN directory + */ + public String getVaadinDirUrl() { + return vaadinDirUrl; } public void setAppId(String appId) { diff --git a/client/src/com/vaadin/client/ApplicationConnection.java b/client/src/com/vaadin/client/ApplicationConnection.java index bc8d82cd30..c9d083a1de 100644 --- a/client/src/com/vaadin/client/ApplicationConnection.java +++ b/client/src/com/vaadin/client/ApplicationConnection.java @@ -3402,23 +3402,27 @@ public class ApplicationConnection { */ public void setPushEnabled(boolean enabled) { if (enabled && push == null) { - /* - * TODO support for loading atmosphere.js on demand will be added in - * another commit. - */ - push = GWT.create(PushConnection.class); - push.init(this); - final String pushUri = addGetParameters( - translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX - + ApplicationConstants.PUSH_PATH + '/'), - UIConstants.UI_ID_PARAMETER + "=" - + getConfiguration().getUIId()); + final PushConnection push = GWT.create(PushConnection.class); + push.init(this); - Scheduler.get().scheduleDeferred(new Command() { + push.runWhenAtmosphereLoaded(new Command() { @Override public void execute() { - push.connect(pushUri); + ApplicationConnection.this.push = push; + + final String pushUri = addGetParameters( + translateVaadinUri(ApplicationConstants.APP_PROTOCOL_PREFIX + + ApplicationConstants.PUSH_PATH + '/'), + UIConstants.UI_ID_PARAMETER + "=" + + getConfiguration().getUIId()); + + Scheduler.get().scheduleDeferred(new Command() { + @Override + public void execute() { + push.connect(pushUri); + } + }); } }); } else if (!enabled && push != null) { diff --git a/client/src/com/vaadin/client/communication/PushConnection.java b/client/src/com/vaadin/client/communication/PushConnection.java index 8619cd00d2..b872460de4 100644 --- a/client/src/com/vaadin/client/communication/PushConnection.java +++ b/client/src/com/vaadin/client/communication/PushConnection.java @@ -19,8 +19,13 @@ package com.vaadin.client.communication; import java.util.ArrayList; import com.google.gwt.core.client.JavaScriptObject; +import com.google.gwt.user.client.Command; import com.vaadin.client.ApplicationConnection; +import com.vaadin.client.ResourceLoader; +import com.vaadin.client.ResourceLoader.ResourceLoadEvent; +import com.vaadin.client.ResourceLoader.ResourceLoadListener; import com.vaadin.client.VConsole; +import com.vaadin.shared.ApplicationConstants; /** * Represents the client-side endpoint of a bidirectional ("push") communication @@ -326,4 +331,43 @@ public class PushConnection { /*-{ $wnd.jQueryVaadin.atmosphere.unsubscribeUrl(url); }-*/; + + private static native boolean isAtmosphereLoaded() + /*-{ + return $wnd.jQueryVaadin != undefined; + }-*/; + + /** + * Runs the provided command when the Atmosphere javascript has been loaded. + * If the script has already been loaded, the command is run immediately. + * + * @param command + * the command to run when Atmosphere has been loaded. + */ + public void runWhenAtmosphereLoaded(final Command command) { + assert command != null; + + if (isAtmosphereLoaded()) { + command.execute(); + } else { + VConsole.log("Loading " + ApplicationConstants.VAADIN_PUSH_JS); + ResourceLoader.get().loadScript( + connection.getConfiguration().getVaadinDirUrl() + + ApplicationConstants.VAADIN_PUSH_JS, + new ResourceLoadListener() { + @Override + public void onLoad(ResourceLoadEvent event) { + VConsole.log(ApplicationConstants.VAADIN_PUSH_JS + + " loaded"); + command.execute(); + } + + @Override + public void onError(ResourceLoadEvent event) { + VConsole.log(event.getResourceUrl() + + " could not be loaded. Push will not work."); + } + }); + } + } } diff --git a/server/src/com/vaadin/server/BootstrapHandler.java b/server/src/com/vaadin/server/BootstrapHandler.java index 9d21e19a18..1592b1eb6c 100644 --- a/server/src/com/vaadin/server/BootstrapHandler.java +++ b/server/src/com/vaadin/server/BootstrapHandler.java @@ -385,7 +385,7 @@ public abstract class BootstrapHandler extends SynchronizedRequestHandler { // Load client-side dependencies for push support fragmentNodes.add(new Element(Tag.valueOf("script"), "").attr( "type", "text/javascript").attr("src", - vaadinLocation + "vaadinPush.js")); + vaadinLocation + ApplicationConstants.VAADIN_PUSH_JS)); } String bootstrapLocation = vaadinLocation + "vaadinBootstrap.js"; diff --git a/shared/src/com/vaadin/shared/ApplicationConstants.java b/shared/src/com/vaadin/shared/ApplicationConstants.java index 5f23a3dc38..6b0c8e7244 100644 --- a/shared/src/com/vaadin/shared/ApplicationConstants.java +++ b/shared/src/com/vaadin/shared/ApplicationConstants.java @@ -72,4 +72,10 @@ public class ApplicationConstants implements Serializable { *

*/ public static final String VAADIN_DIR_URL = "vaadinDir"; + + /** + * The name of the javascript containing push support. The file is located + * in the VAADIN directory. + */ + public static final String VAADIN_PUSH_JS = "vaadinPush.js"; }