Search in sources :

Example 6 with MapPutChange

use of com.vaadin.flow.internal.change.MapPutChange in project flow by vaadin.

the class NodeValue method collectChanges.

@Override
public void collectChanges(Consumer<NodeChange> collector) {
    Serializable originalValue = getNode().getChangeTracker(this, () -> null);
    assert originalValue != null;
    if (originalValue == NULL_MARKER) {
        originalValue = null;
    }
    if (!Objects.equals(originalValue, this.value)) {
        collector.accept(new MapPutChange(this, getKey(), value));
    } else if (!isPopulated) {
        collector.accept(new EmptyChange(this));
    }
    isPopulated = true;
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) UniqueSerializable(com.vaadin.flow.shared.util.UniqueSerializable) Serializable(java.io.Serializable) EmptyChange(com.vaadin.flow.internal.change.EmptyChange)

Example 7 with MapPutChange

use of com.vaadin.flow.internal.change.MapPutChange in project flow by vaadin.

the class UidlWriter method runIfNewTemplateChange.

private static void runIfNewTemplateChange(NodeChange change, Consumer<TemplateNode> consumer) {
    if (change instanceof MapPutChange) {
        MapPutChange put = (MapPutChange) change;
        if (put.getFeature() == TemplateMap.class && put.getKey().equals(NodeProperties.ROOT_TEMPLATE_ID)) {
            Integer id = (Integer) put.getValue();
            TemplateNode templateNode = TemplateNode.get(id.intValue());
            consumer.accept(templateNode);
        }
    }
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) TemplateNode(com.vaadin.flow.template.angular.TemplateNode) TemplateMap(com.vaadin.flow.internal.nodefeature.TemplateMap)

Example 8 with MapPutChange

use of com.vaadin.flow.internal.change.MapPutChange in project flow by vaadin.

the class StateNodeTest method assertCollectChanges_initiallyVisible.

private void assertCollectChanges_initiallyVisible(StateNode stateNode, ElementPropertyMap properties, Consumer<Boolean> activityUpdater) {
    VisibilityData visibility = stateNode.getFeature(VisibilityData.class);
    // check that normal flow works as it should (without any inactivity)
    properties.setProperty("foo", "bar");
    List<NodeChange> changes = new ArrayList<>();
    stateNode.collectChanges(changes::add);
    Assert.assertEquals(2, changes.size());
    // node is attached event
    Assert.assertThat(changes.get(0), CoreMatchers.instanceOf(NodeAttachChange.class));
    // the property update event
    Assert.assertThat(changes.get(1), CoreMatchers.instanceOf(MapPutChange.class));
    changes.clear();
    // now make the node inactive via the VisibiltyData
    activityUpdater.accept(false);
    // now the node becomes inactive and should send only changes for
    // VisibiltyData, but don't loose changes for other features
    properties.setProperty("foo", "baz");
    TestStateTree tree = (TestStateTree) stateNode.getOwner();
    tree.dirtyNodes.clear();
    stateNode.collectChanges(changes::add);
    // activity updater may modify visibility of the node itself or its
    // ancestor. The number of changes will depend on whether the subject
    // node is visible or not
    boolean visibilityChanged = !visibility.isVisible();
    // The only possible change is visibility value change
    Assert.assertEquals(visibilityChanged ? 1 : 0, changes.size());
    MapPutChange change;
    if (visibilityChanged) {
        Assert.assertEquals(0, tree.dirtyNodes.size());
        Assert.assertThat(changes.get(0), CoreMatchers.instanceOf(MapPutChange.class));
        change = (MapPutChange) changes.get(0);
        Assert.assertEquals(VisibilityData.class, change.getFeature());
    } else {
        // the target node should be marked as dirty because it's visible
        // but its parent is inactive
        Assert.assertEquals(1, tree.dirtyNodes.size());
        tree.dirtyNodes.contains(stateNode);
    }
    changes.clear();
    // make the node active again
    activityUpdater.accept(true);
    stateNode.collectChanges(changes::add);
    // Two possible changes: probable visibility value change and property
    // update change
    Assert.assertEquals(visibilityChanged ? 2 : 1, changes.size());
    Assert.assertThat(changes.get(0), CoreMatchers.instanceOf(MapPutChange.class));
    change = (MapPutChange) changes.get(0);
    MapPutChange propertyChange;
    if (visibilityChanged) {
        MapPutChange visibilityChange = VisibilityData.class.equals(change.getFeature()) ? change : (MapPutChange) changes.get(1);
        propertyChange = change.equals(visibilityChange) ? (MapPutChange) changes.get(1) : change;
    } else {
        propertyChange = change;
    }
    Assert.assertEquals(ElementPropertyMap.class, propertyChange.getFeature());
    Assert.assertEquals("baz", propertyChange.getValue());
    // Don't make any changes, check that there are no changes collected
    changes.clear();
    stateNode.collectChanges(changes::add);
    Assert.assertEquals(0, changes.size());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) ArrayList(java.util.ArrayList) VisibilityData(com.vaadin.flow.internal.nodefeature.VisibilityData)

Example 9 with MapPutChange

use of com.vaadin.flow.internal.change.MapPutChange in project flow by vaadin.

the class StateTreeTest method allValuesAfterReattach.

@Test
public void allValuesAfterReattach() {
    StateNode node1 = tree.getRootNode();
    StateNode node2 = new StateNode(ElementData.class);
    StateNodeTest.setParent(node2, node1);
    node2.getFeature(ElementData.class).setTag("foo");
    collectChangesExceptChildrenAddRemove();
    StateNodeTest.setParent(node2, null);
    collectChangesExceptChildrenAddRemove();
    StateNodeTest.setParent(node2, node1);
    List<NodeChange> changes = collectChangesExceptChildrenAddRemove();
    Assert.assertEquals("Should be two changes.", 2, changes.size());
    Assert.assertTrue("First change should re-attach the node.", changes.get(0) instanceof NodeAttachChange);
    Assert.assertTrue("Second change should re-put the value.", changes.get(1) instanceof MapPutChange);
    MapPutChange nodeChange = (MapPutChange) changes.get(1);
    Assert.assertEquals(ElementData.class, nodeChange.getFeature());
    Assert.assertEquals("tag", nodeChange.getKey());
    Assert.assertEquals("foo", nodeChange.getValue());
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) NodeChange(com.vaadin.flow.internal.change.NodeChange) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) ElementData(com.vaadin.flow.internal.nodefeature.ElementData) Test(org.junit.Test)

Example 10 with MapPutChange

use of com.vaadin.flow.internal.change.MapPutChange in project flow by vaadin.

the class MapPutChangeTest method getValue.

private JsonValue getValue(Object input) {
    MapPutChange change = new MapPutChange(feature, "myKey", input);
    JsonObject json = change.toJson(null);
    return json.get(JsonConstants.CHANGE_PUT_VALUE);
}
Also used : MapPutChange(com.vaadin.flow.internal.change.MapPutChange) JsonObject(elemental.json.JsonObject)

Aggregations

MapPutChange (com.vaadin.flow.internal.change.MapPutChange)14 NodeChange (com.vaadin.flow.internal.change.NodeChange)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 JsonObject (elemental.json.JsonObject)5 StateNodeTest (com.vaadin.flow.internal.StateNodeTest)3 EmptyChange (com.vaadin.flow.internal.change.EmptyChange)3 NodeAttachChange (com.vaadin.flow.internal.change.NodeAttachChange)3 Serializable (java.io.Serializable)3 MapRemoveChange (com.vaadin.flow.internal.change.MapRemoveChange)2 AbstractNodeFeatureTest (com.vaadin.flow.internal.nodefeature.AbstractNodeFeatureTest)2 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)2 VisibilityData (com.vaadin.flow.internal.nodefeature.VisibilityData)2 UniqueSerializable (com.vaadin.flow.shared.util.UniqueSerializable)2 UI (com.vaadin.flow.component.UI)1 Element (com.vaadin.flow.dom.Element)1 ElementFactory (com.vaadin.flow.dom.ElementFactory)1 StateNode (com.vaadin.flow.internal.StateNode)1 NodeDetachChange (com.vaadin.flow.internal.change.NodeDetachChange)1 ElementAttributeMap (com.vaadin.flow.internal.nodefeature.ElementAttributeMap)1