Search in sources :

Example 1 with ResourceLoadListener

use of com.vaadin.client.ResourceLoader.ResourceLoadListener in project flow by vaadin.

the class DependencyLoader method extractLazyDependenciesAndLoadOthers.

private JsMap<String, BiConsumer<String, ResourceLoadListener>> extractLazyDependenciesAndLoadOthers(LoadMode loadMode, JsonArray dependencies) {
    JsMap<String, BiConsumer<String, ResourceLoadListener>> lazyDependencies = JsCollections.map();
    for (int i = 0; i < dependencies.length(); i++) {
        JsonObject dependencyJson = dependencies.getObject(i);
        Dependency.Type type = Dependency.Type.valueOf(dependencyJson.getString(Dependency.KEY_TYPE));
        BiConsumer<String, ResourceLoadListener> resourceLoader = getResourceLoader(type, loadMode);
        if (type == Dependency.Type.DYNAMIC_IMPORT) {
            loadDependencyEagerly(dependencyJson.getString(Dependency.KEY_URL), resourceLoader);
        } else {
            switch(loadMode) {
                case EAGER:
                    loadDependencyEagerly(getDependencyUrl(dependencyJson), resourceLoader);
                    break;
                case LAZY:
                    lazyDependencies.set(getDependencyUrl(dependencyJson), resourceLoader);
                    break;
                case INLINE:
                    loadDependencyEagerly(dependencyJson.getString(Dependency.KEY_CONTENTS), resourceLoader);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown load mode = " + loadMode);
            }
        }
    }
    return lazyDependencies;
}
Also used : JsonObject(elemental.json.JsonObject) ResourceLoadListener(com.vaadin.client.ResourceLoader.ResourceLoadListener) Dependency(com.vaadin.flow.shared.ui.Dependency) BiConsumer(java.util.function.BiConsumer)

Example 2 with ResourceLoadListener

use of com.vaadin.client.ResourceLoader.ResourceLoadListener in project flow by vaadin.

the class AtmospherePushConnection method runWhenAtmosphereLoaded.

private void runWhenAtmosphereLoaded(final Command command) {
    if (isAtmosphereLoaded()) {
        command.execute();
    } else {
        final String pushJs = getVersionedPushJs();
        Console.log("Loading " + pushJs);
        ResourceLoader loader = registry.getResourceLoader();
        String pushScriptUrl = registry.getApplicationConfiguration().getContextRootUrl() + pushJs;
        ResourceLoadListener loadListener = new ResourceLoadListener() {

            @Override
            public void onLoad(ResourceLoadEvent event) {
                if (isAtmosphereLoaded()) {
                    Console.log(pushJs + " loaded");
                    command.execute();
                } else {
                    // If bootstrap tried to load
                    // vaadinPush.js, ResourceLoader assumes it succeeded
                    // even if it failed (#11673)
                    onError(event);
                }
            }

            @Override
            public void onError(ResourceLoadEvent event) {
                getConnectionStateHandler().pushScriptLoadError(event.getResourceData());
            }
        };
        loader.loadScript(pushScriptUrl, loadListener);
    }
}
Also used : ResourceLoader(com.vaadin.client.ResourceLoader) ResourceLoadEvent(com.vaadin.client.ResourceLoader.ResourceLoadEvent) ResourceLoadListener(com.vaadin.client.ResourceLoader.ResourceLoadListener)

Aggregations

ResourceLoadListener (com.vaadin.client.ResourceLoader.ResourceLoadListener)2 ResourceLoader (com.vaadin.client.ResourceLoader)1 ResourceLoadEvent (com.vaadin.client.ResourceLoader.ResourceLoadEvent)1 Dependency (com.vaadin.flow.shared.ui.Dependency)1 JsonObject (elemental.json.JsonObject)1 BiConsumer (java.util.function.BiConsumer)1