Search in sources :

Example 1 with NodeAttachChange

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

the class StateNode method collectChanges.

/**
 * Collects all changes made to this node since the last time
 * {@link #collectChanges(Consumer)} has been called. If the node is
 * recently attached, then the reported changes will be relative to a newly
 * created node.
 *
 * @param collector
 *            a consumer accepting node changes
 */
public void collectChanges(Consumer<NodeChange> collector) {
    boolean isAttached = isAttached();
    if (isAttached != wasAttached) {
        if (isAttached) {
            collector.accept(new NodeAttachChange(this));
            // Make all changes show up as if the node was recently attached
            clearChanges();
            getFeatures().values().forEach(NodeFeature::generateChangesFromEmpty);
        } else {
            collector.accept(new NodeDetachChange(this));
        }
        wasAttached = isAttached;
    }
    if (!isAttached()) {
        return;
    }
    if (isInactive()) {
        if (!isInactiveSelf) {
            /*
                 * We are here if: the node itself is not inactive but it has
                 * some ascendant which is inactive.
                 *
                 * In this case we send only some subset of changes (not from
                 * all the features). But we should send changes for all
                 * remaining features. Normally it automatically happens if the
                 * node becomes "visible". But if it was visible with some
                 * invisible parent then only the parent becomes dirty (when
                 * it's set visible) and this child will never participate in
                 * collection of changes since it's not marked as dirty.
                 *
                 * So here such node (which is active itself but its ascendant
                 * is inactive) we mark as dirty again to be able to collect its
                 * changes later on when its ascendant becomes active.
                 */
            getOwner().markAsDirty(this);
        }
        if (isInitialChanges) {
            // send only required (reported) features updates
            Stream<NodeFeature> initialFeatures = Stream.concat(getFeatures().entrySet().stream().filter(entry -> isReportedFeature(entry.getKey())).map(Entry::getValue), getDisalowFeatures());
            doCollectChanges(collector, initialFeatures);
        } else {
            doCollectChanges(collector, getDisalowFeatures());
        }
    } else {
        doCollectChanges(collector, getFeatures().values().stream());
    }
}
Also used : NodeDetachChange(com.vaadin.flow.internal.change.NodeDetachChange) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) NodeFeature(com.vaadin.flow.internal.nodefeature.NodeFeature)

Example 2 with NodeAttachChange

use of com.vaadin.flow.internal.change.NodeAttachChange 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 3 with NodeAttachChange

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

the class StateTreeTest method testTreeChangeCollection.

@Test
public void testTreeChangeCollection() {
    StateNode node2 = StateNodeTest.createEmptyNode();
    StateNodeTest.setParent(node2, tree.getRootNode());
    List<NodeChange> changes = collectChangesExceptChildrenAddRemove();
    Assert.assertEquals(1, changes.size());
    NodeAttachChange nodeChange = (NodeAttachChange) changes.get(0);
    Assert.assertSame(node2, nodeChange.getNode());
}
Also used : NodeChange(com.vaadin.flow.internal.change.NodeChange) NodeAttachChange(com.vaadin.flow.internal.change.NodeAttachChange) Test(org.junit.Test)

Aggregations

NodeAttachChange (com.vaadin.flow.internal.change.NodeAttachChange)3 NodeChange (com.vaadin.flow.internal.change.NodeChange)2 Test (org.junit.Test)2 MapPutChange (com.vaadin.flow.internal.change.MapPutChange)1 NodeDetachChange (com.vaadin.flow.internal.change.NodeDetachChange)1 ElementData (com.vaadin.flow.internal.nodefeature.ElementData)1 NodeFeature (com.vaadin.flow.internal.nodefeature.NodeFeature)1