Search in sources :

Example 1 with UIInternals

use of com.vaadin.flow.component.internal.UIInternals in project flow by vaadin.

the class ComponentTest method usesChain.

@Test
public void usesChain() {
    UIInternals internals = new UI().getInternals();
    internals.addComponentDependencies(UsesUsesComponentWithDependencies.class);
    Map<String, Dependency> pendingDependencies = getDependenciesMap(internals.getDependencyList().getPendingSendToClient());
    Assert.assertEquals(5, pendingDependencies.size());
    assertDependency(Dependency.Type.HTML_IMPORT, "usesuses.html", pendingDependencies);
    assertDependency(Dependency.Type.HTML_IMPORT, "html.html", pendingDependencies);
    assertDependency(Dependency.Type.JAVASCRIPT, "uses.js", pendingDependencies);
    assertDependency(Dependency.Type.JAVASCRIPT, "js.js", pendingDependencies);
    assertDependency(Dependency.Type.STYLESHEET, "css.css", pendingDependencies);
}
Also used : UIInternals(com.vaadin.flow.component.internal.UIInternals) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 2 with UIInternals

use of com.vaadin.flow.component.internal.UIInternals in project flow by vaadin.

the class ComponentTest method circularDependencies.

@Test
public void circularDependencies() {
    UIInternals internals = new UI().getInternals();
    DependencyList dependencyList = internals.getDependencyList();
    internals.addComponentDependencies(CircularDependencies1.class);
    Map<String, Dependency> pendingDependencies = getDependenciesMap(dependencyList.getPendingSendToClient());
    Assert.assertEquals(2, pendingDependencies.size());
    assertDependency(Dependency.Type.JAVASCRIPT, "dep1.js", pendingDependencies);
    assertDependency(Dependency.Type.JAVASCRIPT, "dep2.js", pendingDependencies);
    internals = new UI().getInternals();
    dependencyList = internals.getDependencyList();
    internals.addComponentDependencies(CircularDependencies2.class);
    pendingDependencies = getDependenciesMap(dependencyList.getPendingSendToClient());
    Assert.assertEquals(2, pendingDependencies.size());
    assertDependency(Dependency.Type.JAVASCRIPT, "dep2.js", pendingDependencies);
    assertDependency(Dependency.Type.JAVASCRIPT, "dep1.js", pendingDependencies);
}
Also used : DependencyList(com.vaadin.flow.component.internal.DependencyList) UIInternals(com.vaadin.flow.component.internal.UIInternals) Dependency(com.vaadin.flow.shared.ui.Dependency) Test(org.junit.Test)

Example 3 with UIInternals

use of com.vaadin.flow.component.internal.UIInternals in project flow by vaadin.

the class UidlWriter method createUidl.

/**
 * Creates a JSON object containing all pending changes to the given UI.
 *
 * @param ui
 *            The {@link UI} whose changes to write
 * @param async
 *            True if this message is sent by the server asynchronously,
 *            false if it is a response to a client message.
 * @return JSON object containing the UIDL response
 */
public JsonObject createUidl(UI ui, boolean async) {
    JsonObject response = Json.createObject();
    UIInternals uiInternals = ui.getInternals();
    VaadinSession session = ui.getSession();
    VaadinService service = session.getService();
    // Purge pending access calls as they might produce additional changes
    // to write out
    service.runPendingAccessTasks(session);
    // Paints components
    getLogger().debug("* Creating response to client");
    int syncId = service.getDeploymentConfiguration().isSyncIdCheckEnabled() ? uiInternals.getServerSyncId() : -1;
    response.put(ApplicationConstants.SERVER_SYNC_ID, syncId);
    int nextClientToServerMessageId = uiInternals.getLastProcessedClientToServerId() + 1;
    response.put(ApplicationConstants.CLIENT_TO_SERVER_ID, nextClientToServerMessageId);
    SystemMessages messages = ui.getSession().getService().getSystemMessages(ui.getLocale(), null);
    JsonObject meta = new MetadataWriter().createMetadata(ui, false, async, messages);
    if (meta.keys().length > 0) {
        response.put("meta", meta);
    }
    JsonArray stateChanges = Json.createArray();
    JsonObject templates = Json.createObject();
    encodeChanges(ui, stateChanges, templates);
    populateDependencies(response, session, uiInternals.getDependencyList());
    if (uiInternals.getConstantPool().hasNewConstants()) {
        response.put("constants", uiInternals.getConstantPool().dumpConstants());
    }
    if (stateChanges.length() != 0) {
        response.put("changes", stateChanges);
    }
    if (templates.keys().length > 0) {
        response.put("templates", templates);
    }
    List<JavaScriptInvocation> executeJavaScriptList = uiInternals.dumpPendingJavaScriptInvocations();
    if (!executeJavaScriptList.isEmpty()) {
        response.put(JsonConstants.UIDL_KEY_EXECUTE, encodeExecuteJavaScriptList(executeJavaScriptList));
    }
    if (ui.getSession().getService().getDeploymentConfiguration().isRequestTiming()) {
        response.put("timings", createPerformanceData(ui));
    }
    uiInternals.incrementServerId();
    return response;
}
Also used : JsonArray(elemental.json.JsonArray) VaadinSession(com.vaadin.flow.server.VaadinSession) VaadinService(com.vaadin.flow.server.VaadinService) JsonObject(elemental.json.JsonObject) UIInternals(com.vaadin.flow.component.internal.UIInternals) SystemMessages(com.vaadin.flow.server.SystemMessages) JavaScriptInvocation(com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)

Example 4 with UIInternals

use of com.vaadin.flow.component.internal.UIInternals in project flow by vaadin.

the class UidlWriter method encodeChanges.

/**
 * Encodes the state tree changes of the given UI. The executions registered
 * at
 * {@link StateTree#beforeClientResponse(com.vaadin.flow.internal.StateNode, com.vaadin.flow.function.SerializableConsumer)}
 * at evaluated before the changes are encoded.
 *
 * @param ui
 *            the UI
 * @param stateChanges
 *            a JSON array to put state changes into
 * @param templates
 *            a JSON object to put new template nodes into
 * @see StateTree#runExecutionsBeforeClientResponse()
 */
private void encodeChanges(UI ui, JsonArray stateChanges, JsonObject templates) {
    UIInternals uiInternals = ui.getInternals();
    StateTree stateTree = uiInternals.getStateTree();
    stateTree.runExecutionsBeforeClientResponse();
    Consumer<TemplateNode> templateEncoder = new Consumer<TemplateNode>() {

        @Override
        public void accept(TemplateNode templateNode) {
            // Send to client if it's a new template
            if (!uiInternals.isTemplateSent(templateNode)) {
                uiInternals.setTemplateSent(templateNode);
                templates.put(Integer.toString(templateNode.getId()), templateNode.toJson(this));
            }
        }
    };
    Set<Class<? extends Component>> componentsWithDependencies = new LinkedHashSet<>();
    stateTree.collectChanges(change -> {
        // Ensure new templates are sent to the client
        runIfNewTemplateChange(change, templateEncoder);
        if (attachesComponent(change)) {
            change.getNode().getFeature(ComponentMapping.class).getComponent().ifPresent(component -> addComponentHierarchy(ui, componentsWithDependencies, component));
        }
        // Encode the actual change
        stateChanges.set(stateChanges.length(), change.toJson(uiInternals.getConstantPool()));
    });
    componentsWithDependencies.forEach(uiInternals::addComponentDependencies);
}
Also used : TemplateNode(com.vaadin.flow.template.angular.TemplateNode) LinkedHashSet(java.util.LinkedHashSet) StateTree(com.vaadin.flow.internal.StateTree) Consumer(java.util.function.Consumer) UIInternals(com.vaadin.flow.component.internal.UIInternals) Component(com.vaadin.flow.component.Component)

Aggregations

UIInternals (com.vaadin.flow.component.internal.UIInternals)4 Dependency (com.vaadin.flow.shared.ui.Dependency)2 Test (org.junit.Test)2 Component (com.vaadin.flow.component.Component)1 DependencyList (com.vaadin.flow.component.internal.DependencyList)1 JavaScriptInvocation (com.vaadin.flow.component.internal.UIInternals.JavaScriptInvocation)1 StateTree (com.vaadin.flow.internal.StateTree)1 SystemMessages (com.vaadin.flow.server.SystemMessages)1 VaadinService (com.vaadin.flow.server.VaadinService)1 VaadinSession (com.vaadin.flow.server.VaadinSession)1 TemplateNode (com.vaadin.flow.template.angular.TemplateNode)1 JsonArray (elemental.json.JsonArray)1 JsonObject (elemental.json.JsonObject)1 LinkedHashSet (java.util.LinkedHashSet)1 Consumer (java.util.function.Consumer)1