DependencyLoader { public void loadDependencies( JsMap<LoadMode, JsonArray> clientDependencies) { assert clientDependencies != null; JsMap<String, BiConsumer<String, ResourceLoadListener>> lazyDependencies = JsCollections .map(); clientDependencies.forEach((dependencies, mode) -> { extractLazyDependenciesAndLoadOthers(mode, dependencies).forEach( (loader, url) -> lazyDependencies.set(url, loader)); }); if (!lazyDependencies.isEmpty()) { runWhenEagerDependenciesLoaded( () -> Scheduler.get().scheduleDeferred(() -> { Console.log( "Finished loading eager dependencies, loading lazy."); lazyDependencies.forEach((loader, url) -> loadLazyDependency(url, loader)); })); } } DependencyLoader(Registry registry); static void runWhenEagerDependenciesLoaded(Command command); void loadDependencies(
            JsMap<LoadMode, JsonArray> clientDependencies); void requireHtmlImportsReady();  }
@Test public void loadStylesheet() { String TEST_URL = "http: new DependencyLoader(registry).loadDependencies( createDependenciesMap(new Dependency(Dependency.Type.STYLESHEET, TEST_URL, LoadMode.EAGER).toJson())); assertEquals(Collections.singletonList(TEST_URL), mockResourceLoader.loadingStyles); }
@Test public void loadScript() { String TEST_URL = "http: new DependencyLoader(registry).loadDependencies( createDependenciesMap(new Dependency(Dependency.Type.JAVASCRIPT, TEST_URL, LoadMode.EAGER).toJson())); assertEquals(Collections.singletonList(TEST_URL), mockResourceLoader.loadingScripts); }
@Test public void loadMultiple() { String TEST_JS_URL = "http: String TEST_JS_URL2 = "/my.js"; String TEST_CSS_URL = "https: new DependencyLoader(registry).loadDependencies(createDependenciesMap( new Dependency(Dependency.Type.JAVASCRIPT, TEST_JS_URL, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.JAVASCRIPT, TEST_JS_URL2, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.STYLESHEET, TEST_CSS_URL, LoadMode.EAGER).toJson())); assertEquals(Arrays.asList(TEST_JS_URL, TEST_JS_URL2), mockResourceLoader.loadingScripts); assertEquals(Collections.singletonList(TEST_CSS_URL), mockResourceLoader.loadingStyles); }
@Test public void ensureEagerDependenciesLoadedInOrder() { String jsUrl1 = "/1.js"; String jsUrl2 = "/2.js"; String cssUrl1 = "/1.css"; String cssUrl2 = "/2.css"; new DependencyLoader(registry).loadDependencies(createDependenciesMap( new Dependency(Dependency.Type.JAVASCRIPT, jsUrl1, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.JAVASCRIPT, jsUrl2, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.STYLESHEET, cssUrl1, LoadMode.EAGER).toJson(), new Dependency(Dependency.Type.STYLESHEET, cssUrl2, LoadMode.EAGER).toJson())); assertEquals( "jsUrl1 should come before jsUrl2, because it was added earlier", Arrays.asList(jsUrl1, jsUrl2), mockResourceLoader.loadingScripts); assertEquals( "cssUrl1 should come before cssUrl2, because it was added earlier", Arrays.asList(cssUrl1, cssUrl2), mockResourceLoader.loadingStyles); }
@Test public void ensureInlineDependenciesLoadedInOrder() { String jsContents1 = "/1.js"; String jsContents2 = "/2.js"; String cssContents1 = "/1.css"; String cssContents2 = "/2.css"; new DependencyLoader(registry).loadDependencies(createDependenciesMap( createInlineDependency(Dependency.Type.JAVASCRIPT, jsContents1), createInlineDependency(Dependency.Type.JAVASCRIPT, jsContents2), createInlineDependency(Dependency.Type.STYLESHEET, cssContents1), createInlineDependency(Dependency.Type.STYLESHEET, cssContents2))); assertEquals( "jsContents1 should come before jsContents2, because it was added earlier", Arrays.asList(jsContents1, jsContents2), mockResourceLoader.loadingScripts); assertEquals( "cssContents1 should come before cssContents2, because it was added earlier", Arrays.asList(cssContents1, cssContents2), mockResourceLoader.loadingStyles); }