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);
}
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<?>);
}
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));
}
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);
}
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));
}
Aggregations