summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArtur <artur@vaadin.com>2017-01-23 15:05:52 +0200
committerGitHub <noreply@github.com>2017-01-23 15:05:52 +0200
commit1b7f597161df6425e6033258236f9bf247284521 (patch)
tree0a37b21264129340986ff4bdae469e10567d0511
parent838c1d949b6a902fc1bdcabe107158d401d27425 (diff)
downloadvaadin-framework-1b7f597161df6425e6033258236f9bf247284521.tar.gz
vaadin-framework-1b7f597161df6425e6033258236f9bf247284521.zip
Code cleanup: Move dependency loading logic to a separate class (#8290)
-rw-r--r--client/src/main/java/com/vaadin/client/ApplicationConfiguration.java12
-rw-r--r--client/src/main/java/com/vaadin/client/ApplicationConnection.java89
-rw-r--r--client/src/main/java/com/vaadin/client/DependencyLoader.java164
-rw-r--r--client/src/main/java/com/vaadin/client/communication/MessageHandler.java9
4 files changed, 186 insertions, 88 deletions
diff --git a/client/src/main/java/com/vaadin/client/ApplicationConfiguration.java b/client/src/main/java/com/vaadin/client/ApplicationConfiguration.java
index 102dc15761..e599eef61b 100644
--- a/client/src/main/java/com/vaadin/client/ApplicationConfiguration.java
+++ b/client/src/main/java/com/vaadin/client/ApplicationConfiguration.java
@@ -624,14 +624,18 @@ public class ApplicationConfiguration implements EntryPoint {
}
/**
+ * Runs the given command when all pending dependencies have been loaded, or
+ * immediately if no dependencies are being loaded.
+ *
* @since 7.6
- * @param c
+ * @param command
+ * the command to run
*/
- public static void runWhenDependenciesLoaded(Command c) {
+ public static void runWhenDependenciesLoaded(Command command) {
if (dependenciesLoading == 0) {
- c.execute();
+ command.execute();
} else {
- callbacks.add(c);
+ callbacks.add(command);
}
}
diff --git a/client/src/main/java/com/vaadin/client/ApplicationConnection.java b/client/src/main/java/com/vaadin/client/ApplicationConnection.java
index 1fe1f83cb1..9d9a66173d 100644
--- a/client/src/main/java/com/vaadin/client/ApplicationConnection.java
+++ b/client/src/main/java/com/vaadin/client/ApplicationConnection.java
@@ -26,7 +26,6 @@ import com.google.gwt.aria.client.Roles;
import com.google.gwt.core.client.Duration;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
-import com.google.gwt.core.client.JsArrayString;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.EventBus;
@@ -42,8 +41,6 @@ import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ApplicationConfiguration.ErrorMessage;
-import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
-import com.vaadin.client.ResourceLoader.ResourceLoadListener;
import com.vaadin.client.communication.ConnectionStateHandler;
import com.vaadin.client.communication.Heartbeat;
import com.vaadin.client.communication.MessageHandler;
@@ -387,6 +384,7 @@ public class ApplicationConnection implements HasHandlers {
serverRpcQueue.setConnection(this);
messageHandler.setConnection(this);
messageSender.setConnection(this);
+ dependencyLoader.setConnection(this);
ComponentLocator componentLocator = new ComponentLocator(this);
@@ -765,79 +763,6 @@ public class ApplicationConnection implements HasHandlers {
return getLoadingIndicator().isVisible();
}
- public void loadStyleDependencies(JsArrayString dependencies) {
- // Assuming no reason to interpret in a defined order
- ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
- @Override
- public void onLoad(ResourceLoadEvent event) {
- ApplicationConfiguration.endDependencyLoading();
- }
-
- @Override
- public void onError(ResourceLoadEvent event) {
- getLogger().severe(event.getResourceUrl()
- + " could not be loaded, or the load detection failed because the stylesheet is empty.");
- // The show must go on
- onLoad(event);
- }
- };
- ResourceLoader loader = ResourceLoader.get();
- for (int i = 0; i < dependencies.length(); i++) {
- String url = translateVaadinUri(dependencies.get(i));
- ApplicationConfiguration.startDependencyLoading();
- loader.loadStylesheet(url, resourceLoadListener);
- }
- }
-
- public void loadScriptDependencies(final JsArrayString dependencies) {
- if (dependencies.length() == 0) {
- return;
- }
-
- // Listener that loads the next when one is completed
- ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
- @Override
- public void onLoad(ResourceLoadEvent event) {
- if (dependencies.length() != 0) {
- String url = translateVaadinUri(dependencies.shift());
- ApplicationConfiguration.startDependencyLoading();
- // Load next in chain (hopefully already preloaded)
- event.getResourceLoader().loadScript(url, this);
- }
- // Call start for next before calling end for current
- ApplicationConfiguration.endDependencyLoading();
- }
-
- @Override
- public void onError(ResourceLoadEvent event) {
- getLogger().severe(
- event.getResourceUrl() + " could not be loaded.");
- // The show must go on
- onLoad(event);
- }
- };
-
- ResourceLoader loader = ResourceLoader.get();
-
- // Start chain by loading first
- String url = translateVaadinUri(dependencies.shift());
- ApplicationConfiguration.startDependencyLoading();
- loader.loadScript(url, resourceLoadListener);
-
- if (ResourceLoader.supportsInOrderScriptExecution()) {
- for (int i = 0; i < dependencies.length(); i++) {
- String preloadUrl = translateVaadinUri(dependencies.get(i));
- loader.loadScript(preloadUrl, null);
- }
- } else {
- // Preload all remaining
- for (int i = 0; i < dependencies.length(); i++) {
- String preloadUrl = translateVaadinUri(dependencies.get(i));
- loader.preloadResource(preloadUrl, null);
- }
- }
- }
-
private void addVariableToQueue(String connectorId, String variableName,
Object value, boolean immediate) {
boolean lastOnly = !immediate;
@@ -1280,6 +1205,9 @@ public class ApplicationConnection implements HasHandlers {
private ConnectorMap connectorMap = GWT.create(ConnectorMap.class);
+ private final DependencyLoader dependencyLoader = GWT
+ .create(DependencyLoader.class);
+
/**
* Use to notify that the given component's caption has changed; layouts may
* have to be recalculated.
@@ -1636,4 +1564,13 @@ public class ApplicationConnection implements HasHandlers {
return getMessageHandler().getLastSeenServerSyncId();
}
+ /**
+ * Gets the instance which handles loading of dependencies.
+ *
+ * @return the dependency loader for this connection
+ */
+ public DependencyLoader getDependencyLoader() {
+ return dependencyLoader;
+ }
+
}
diff --git a/client/src/main/java/com/vaadin/client/DependencyLoader.java b/client/src/main/java/com/vaadin/client/DependencyLoader.java
new file mode 100644
index 0000000000..c644cd87db
--- /dev/null
+++ b/client/src/main/java/com/vaadin/client/DependencyLoader.java
@@ -0,0 +1,164 @@
+/*
+ * Copyright 2000-2016 Vaadin Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.vaadin.client;
+
+import java.util.logging.Logger;
+
+import com.google.gwt.core.client.JsArrayString;
+import com.google.gwt.user.client.Command;
+import com.vaadin.client.ResourceLoader.ResourceLoadEvent;
+import com.vaadin.client.ResourceLoader.ResourceLoadListener;
+
+/**
+ * Handles loading of dependencies (style sheets and scripts) in the
+ * application.
+ *
+ * Use {@link ApplicationConfiguration#runWhenDependenciesLoaded(Command)} to
+ * execute a command after all dependencies have finished loading.
+ *
+ * @author Vaadin Ltd
+ * @since 8.0.0
+ */
+public class DependencyLoader {
+
+ private static final String STYLE_DEPENDENCIES = "styleDependencies";
+ private static final String SCRIPT_DEPENDENCIES = "scriptDependencies";
+ private ApplicationConnection connection = null;
+
+ /**
+ * Sets the ApplicationConnection this instance is connected to.
+ *
+ * Only used internally.
+ *
+ * @param connection
+ * The ApplicationConnection for this instance
+ */
+ public void setConnection(ApplicationConnection connection) {
+ if (this.connection != null) {
+ throw new IllegalStateException(
+ "Application connection has already been set");
+ }
+ if (connection == null) {
+ throw new IllegalArgumentException(
+ "ApplicationConnection can not be null");
+ }
+ this.connection = connection;
+ }
+
+ /**
+ * Loads the any dependencies present in the given json snippet.
+ *
+ * Scans the key "{@literal scriptDependencies}" for JavaScripts and the key
+ * "{@literal styleDependencies}" for style sheets.
+ *
+ * Ensures that the given JavaScript dependencies are loaded in the given
+ * order. Does not ensure anything about stylesheet order.
+ *
+ * @param json
+ * the JSON containing the dependencies to load
+ */
+ public void loadDependencies(ValueMap json) {
+ if (json.containsKey(SCRIPT_DEPENDENCIES)) {
+ loadScriptDependencies(json.getJSStringArray(SCRIPT_DEPENDENCIES));
+ }
+ if (json.containsKey(STYLE_DEPENDENCIES)) {
+ loadStyleDependencies(json.getJSStringArray(STYLE_DEPENDENCIES));
+ }
+
+ }
+
+ private void loadStyleDependencies(JsArrayString dependencies) {
+ // Assuming no reason to interpret in a defined order
+ ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
+ @Override
+ public void onLoad(ResourceLoadEvent event) {
+ ApplicationConfiguration.endDependencyLoading();
+ }
+
+ @Override
+ public void onError(ResourceLoadEvent event) {
+ getLogger().severe(event.getResourceUrl()
+ + " could not be loaded, or the load detection failed because the stylesheet is empty.");
+ // The show must go on
+ onLoad(event);
+ }
+ };
+ ResourceLoader loader = ResourceLoader.get();
+ for (int i = 0; i < dependencies.length(); i++) {
+ String url = translateVaadinUri(dependencies.get(i));
+ ApplicationConfiguration.startDependencyLoading();
+ loader.loadStylesheet(url, resourceLoadListener);
+ }
+ }
+
+ private void loadScriptDependencies(final JsArrayString dependencies) {
+ if (dependencies.length() == 0) {
+ return;
+ }
+
+ // Listener that loads the next when one is completed
+ ResourceLoadListener resourceLoadListener = new ResourceLoadListener() {
+ @Override
+ public void onLoad(ResourceLoadEvent event) {
+ if (dependencies.length() != 0) {
+ String url = translateVaadinUri(dependencies.shift());
+ ApplicationConfiguration.startDependencyLoading();
+ // Load next in chain (hopefully already preloaded)
+ event.getResourceLoader().loadScript(url, this);
+ }
+ // Call start for next before calling end for current
+ ApplicationConfiguration.endDependencyLoading();
+ }
+
+ @Override
+ public void onError(ResourceLoadEvent event) {
+ getLogger().severe(
+ event.getResourceUrl() + " could not be loaded.");
+ // The show must go on
+ onLoad(event);
+ }
+ };
+
+ ResourceLoader loader = ResourceLoader.get();
+
+ // Start chain by loading first
+ String url = translateVaadinUri(dependencies.shift());
+ ApplicationConfiguration.startDependencyLoading();
+ loader.loadScript(url, resourceLoadListener);
+
+ if (ResourceLoader.supportsInOrderScriptExecution()) {
+ for (int i = 0; i < dependencies.length(); i++) {
+ String preloadUrl = translateVaadinUri(dependencies.get(i));
+ loader.loadScript(preloadUrl, null);
+ }
+ } else {
+ // Preload all remaining
+ for (int i = 0; i < dependencies.length(); i++) {
+ String preloadUrl = translateVaadinUri(dependencies.get(i));
+ loader.preloadResource(preloadUrl, null);
+ }
+ }
+ }
+
+ private String translateVaadinUri(String url) {
+ return connection.translateVaadinUri(url);
+ }
+
+ private static Logger getLogger() {
+ return Logger.getLogger(DependencyLoader.class.getName());
+ }
+
+}
diff --git a/client/src/main/java/com/vaadin/client/communication/MessageHandler.java b/client/src/main/java/com/vaadin/client/communication/MessageHandler.java
index a8e22ea53b..5057d574fa 100644
--- a/client/src/main/java/com/vaadin/client/communication/MessageHandler.java
+++ b/client/src/main/java/com/vaadin/client/communication/MessageHandler.java
@@ -382,14 +382,7 @@ public class MessageHandler {
}
getLogger().info("Handling resource dependencies");
- if (json.containsKey("scriptDependencies")) {
- connection.loadScriptDependencies(
- json.getJSStringArray("scriptDependencies"));
- }
- if (json.containsKey("styleDependencies")) {
- connection.loadStyleDependencies(
- json.getJSStringArray("styleDependencies"));
- }
+ connection.getDependencyLoader().loadDependencies(json);
handleUIDLDuration.logDuration(
" * Handling type mappings from server completed", 10);