]> source.dussan.org Git - vaadin-framework.git/commitdiff
Load vaadinPush.js on demand (#11506)
authorLeif Åstrand <leif@vaadin.com>
Thu, 18 Apr 2013 13:05:40 +0000 (16:05 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 19 Apr 2013 11:54:37 +0000 (11:54 +0000)
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

client/src/com/vaadin/client/ApplicationConfiguration.java
client/src/com/vaadin/client/ApplicationConnection.java
client/src/com/vaadin/client/communication/PushConnection.java
server/src/com/vaadin/server/BootstrapHandler.java
shared/src/com/vaadin/shared/ApplicationConstants.java

index a6cc3cf531d5942b68d0418f2bc4b05476419c42..e6578fa018aed70ef301f88932d4f022918cc99a 100644 (file)
@@ -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) {
index bc8d82cd304f5344a47f39a869c24c1386c3c040..c9d083a1de3603d9cc48085539b8e710059b67fd 100644 (file)
@@ -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) {
index 8619cd00d2db59bef9fb43743c8652f1ae78f4ef..b872460de4cc590a452588235b22f18a48cf8ba4 100644 (file)
@@ -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.");
+                        }
+                    });
+        }
+    }
 }
index 9d21e19a18149754f0e5ab6be37dd4fc7bc1f537..1592b1eb6c49ff9dbfd7e56d856dbb26729bcc51 100644 (file)
@@ -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";
index 5f23a3dc38f32db8125d8dd98d29a710f90dea8b..6b0c8e724433ec8334dfbb53803599a394074a04 100644 (file)
@@ -72,4 +72,10 @@ public class ApplicationConstants implements Serializable {
      * </p>
      */
     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";
 }