Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure all children are collected when the parent node is inactive (#4473) #4478

Merged
merged 1 commit into from
Aug 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions flow-server/src/main/java/com/vaadin/flow/internal/StateTree.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public boolean isAttached() {
* @see StateTree#beforeClientResponse(StateNode, SerializableConsumer)
* @see StateTree#runExecutionsBeforeClientResponse()
*/
public static final class BeforeClientResponseEntry implements Serializable {
public static final class BeforeClientResponseEntry
implements Serializable {
private static final Comparator<BeforeClientResponseEntry> COMPARING_INDEX = Comparator
.comparingInt(BeforeClientResponseEntry::getIndex);

Expand Down Expand Up @@ -235,13 +236,20 @@ public StateNode getNodeById(int id) {
* a consumer accepting node changes
*/
public void collectChanges(Consumer<NodeChange> collector) {
Set<StateNode> dirtyNodesSet = collectDirtyNodes();

dirtyNodesSet.forEach(StateNode::updateActiveState);
Set<StateNode> allDirtyNodes = new LinkedHashSet<>();
boolean evaluateNewDirtyNodes = true;

// The updateActiveState method can create new dirty nodes, so they need
// to be collected as well
while (evaluateNewDirtyNodes) {
Set<StateNode> dirtyNodesSet = collectDirtyNodes();
dirtyNodesSet.forEach(StateNode::updateActiveState);
evaluateNewDirtyNodes = allDirtyNodes.addAll(dirtyNodesSet);
}

// TODO fire preCollect events

dirtyNodesSet.forEach(node -> node.collectChanges(collector));
allDirtyNodes.forEach(node -> node.collectChanges(collector));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;

import org.apache.commons.lang3.SerializationUtils;
import org.junit.Assert;
Expand All @@ -47,6 +48,8 @@
import com.vaadin.flow.internal.nodefeature.NodeFeature;
import com.vaadin.tests.util.TestUtil;

import elemental.json.JsonObject;

public class StateTreeTest {
private StateTree tree = new StateTree(new UI().getInternals(),
ElementChildrenList.class);
Expand All @@ -72,6 +75,23 @@ public boolean isAttached() {
}
}

public static class CollectableNode extends StateNode {

public CollectableNode() {
super(ElementData.class, ElementChildrenList.class);
}

@Override
public void collectChanges(Consumer<NodeChange> collector) {
collector.accept(new NodeChange(this) {
@Override
protected void populateJson(JsonObject json,
ConstantPool constantPool) {
}
});
}
}

@Test
public void testRootNodeState() {
StateNode rootNode = tree.getRootNode();
Expand Down Expand Up @@ -504,4 +524,33 @@ public void collectChanges_updateActiveState() {
Mockito.verify(node2).updateActiveState();
}

@Test
public void collectChanges_parentIsInactive_childrenAreCollected() {
StateNode node1 = new CollectableNode();
StateNode node2 = new CollectableNode();
StateNode node3 = new CollectableNode();

node1.setTree(tree);
node2.setTree(tree);
node3.setTree(tree);

node1.getFeature(ElementChildrenList.class).add(0, node2);
node2.getFeature(ElementChildrenList.class).add(0, node3);

// cleanup the current dirty nodes
tree.collectChanges(node -> {
});

node1.getFeature(ElementData.class).setVisible(false);

List<StateNode> collectedNodes = new ArrayList<>(3);

tree.collectChanges(change -> collectedNodes.add(change.getNode()));

Assert.assertEquals(3, collectedNodes.size());
Assert.assertTrue(collectedNodes.contains(node1));
Assert.assertTrue(collectedNodes.contains(node2));
Assert.assertTrue(collectedNodes.contains(node3));
}

}