Search in sources :

Example 26 with StateTree

use of com.vaadin.flow.internal.StateTree 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
 * @see StateTree#runExecutionsBeforeClientResponse()
 */
private void encodeChanges(UI ui, JsonArray stateChanges) {
    UIInternals uiInternals = ui.getInternals();
    StateTree stateTree = uiInternals.getStateTree();
    stateTree.runExecutionsBeforeClientResponse();
    Set<Class<? extends Component>> componentsWithDependencies = new LinkedHashSet<>();
    stateTree.collectChanges(change -> {
        if (attachesComponent(change)) {
            ComponentMapping.getComponent(change.getNode()).ifPresent(component -> addComponentHierarchy(ui, componentsWithDependencies, component));
        }
        // Encode the actual change
        stateChanges.set(stateChanges.length(), change.toJson(uiInternals.getConstantPool()));
    });
    componentsWithDependencies.forEach(uiInternals::addComponentDependencies);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StateTree(com.vaadin.flow.internal.StateTree) UIInternals(com.vaadin.flow.component.internal.UIInternals) Component(com.vaadin.flow.component.Component)

Example 27 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class NodeListAddRemoveTest method clear_collectChanges_resetChangeTracker_reattach_clearEventIsCollected.

@Test
public void clear_collectChanges_resetChangeTracker_reattach_clearEventIsCollected() {
    resetToRemoveAfterAddCase();
    nodeList.add("foo");
    nodeList.clear();
    StateTree tree = new StateTree(new UI().getInternals(), ElementChildrenList.class);
    // attach the feature node to the tree
    tree.getRootNode().getFeature(ElementChildrenList.class).add(nodeList.getNode());
    nodeList.add("bar");
    // detach the feature node to the tree
    tree.getRootNode().getFeature(ElementChildrenList.class).remove(0);
    // if there was no changes collection after detach (and node is attached
    // again) then the node has not been detached de-facto: detach-attach is
    // no-op in this case, so to avoid no-op the changes should be collected
    // in between
    nodeList.getNode().collectChanges(change -> {
    });
    // reattach it back
    tree.getRootNode().getFeature(ElementChildrenList.class).add(nodeList.getNode());
    List<NodeChange> changes = new ArrayList<>();
    nodeList.getNode().collectChanges(changes::add);
    Assert.assertEquals(3, changes.size());
    Assert.assertEquals(NodeAttachChange.class, changes.get(0).getClass());
    Assert.assertEquals(ListClearChange.class, changes.get(1).getClass());
    Assert.assertEquals(ListAddChange.class, changes.get(2).getClass());
    changes.clear();
    nodeList.add("baz");
    nodeList.getNode().collectChanges(changes::add);
    // Now there is no anymore clear change (so the previous one is not
    // preserved)
    Assert.assertEquals(1, changes.size());
    Assert.assertTrue(changes.get(0) instanceof ListAddChange<?>);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) NodeChange(com.vaadin.flow.internal.change.NodeChange) UI(com.vaadin.flow.component.UI) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 28 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class NodeMapTest method put_sameValue_neverProduceChange_nodeIsNotDirty.

@Test
public void put_sameValue_neverProduceChange_nodeIsNotDirty() {
    UI ui = new UI();
    StateNode node = new StateNode(ElementPropertyMap.class);
    StateTree tree = ui.getInternals().getStateTree();
    tree.getRootNode().getFeature(ElementChildrenList.class).add(node);
    NeverProduceChangeMap map = new NeverProduceChangeMap(node);
    // clear dirty nodes
    tree.collectChanges(change -> {
    });
    map.put("foo", "bar");
    Set<StateNode> nodes = tree.collectDirtyNodes();
    Assert.assertFalse(nodes.contains(node));
    // clear dirty nodes
    tree.collectChanges(change -> {
    });
    Assert.assertTrue(tree.collectDirtyNodes().isEmpty());
    // set another value
    map.put("foo", "baz");
    nodes = tree.collectDirtyNodes();
    Assert.assertFalse(nodes.contains(node));
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Example 29 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class NodeMapTest method put_sameValue_hasNoEffect.

@Test
public void put_sameValue_hasNoEffect() {
    StateTree tree = new StateTree(new UI().getInternals(), ElementChildrenList.class);
    StateNode child = new StateNode();
    AtomicBoolean listenerIsCalled = new AtomicBoolean();
    child.addAttachListener(() -> {
        Assert.assertFalse(listenerIsCalled.get());
        listenerIsCalled.set(true);
    });
    nodeMap.put("foo", child);
    tree.getRootNode().getFeature(ElementChildrenList.class).add(child.getParent());
    Assert.assertTrue(listenerIsCalled.get());
    // The attach listener is not called one more time
    nodeMap.put("foo", child);
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Example 30 with StateTree

use of com.vaadin.flow.internal.StateTree in project flow by vaadin.

the class NodeMapTest method put_sameValue_alwaysProduceChange_nodeIsDirty.

@Test
public void put_sameValue_alwaysProduceChange_nodeIsDirty() {
    UI ui = new UI();
    StateNode node = new StateNode(ElementPropertyMap.class);
    StateTree tree = ui.getInternals().getStateTree();
    tree.getRootNode().getFeature(ElementChildrenList.class).add(node);
    AlwaysProduceChangeMap map = new AlwaysProduceChangeMap(node);
    // clear dirty nodes
    tree.collectChanges(change -> {
    });
    map.put("foo", "bar");
    Set<StateNode> nodes = tree.collectDirtyNodes();
    Assert.assertTrue(nodes.contains(node));
    // clear dirty nodes
    tree.collectChanges(change -> {
    });
    Assert.assertTrue(tree.collectDirtyNodes().isEmpty());
    // set once again the same value
    map.put("foo", "bar");
    nodes = tree.collectDirtyNodes();
    Assert.assertTrue(nodes.contains(node));
}
Also used : StateTree(com.vaadin.flow.internal.StateTree) UI(com.vaadin.flow.component.UI) StateNode(com.vaadin.flow.internal.StateNode) Test(org.junit.Test) StateNodeTest(com.vaadin.flow.internal.StateNodeTest)

Aggregations

StateTree (com.vaadin.flow.internal.StateTree)32 Test (org.junit.Test)21 UI (com.vaadin.flow.component.UI)20 StateNode (com.vaadin.flow.internal.StateNode)18 JsonObject (elemental.json.JsonObject)10 Element (com.vaadin.flow.dom.Element)8 ArrayList (java.util.ArrayList)7 NodeChange (com.vaadin.flow.internal.change.NodeChange)6 UIInternals (com.vaadin.flow.component.internal.UIInternals)4 ChildElementConsumer (com.vaadin.flow.dom.ChildElementConsumer)4 AttachExistingElementFeature (com.vaadin.flow.internal.nodefeature.AttachExistingElementFeature)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Component (com.vaadin.flow.component.Component)3 DomEvent (com.vaadin.flow.dom.DomEvent)3 DomListenerRegistration (com.vaadin.flow.dom.DomListenerRegistration)3 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 ElementChildrenList (com.vaadin.flow.internal.nodefeature.ElementChildrenList)3 JsonValue (elemental.json.JsonValue)3 NodeOwner (com.vaadin.flow.internal.NodeOwner)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2