Skip to content

Commit dab9d32

Browse files
vaadin-bottepiczp13
authored
fix: Disable sending updates to client for effectively non-visible nodes (#15885) (#16810)
* Disable sending updates to client for effectively non-visible nodes * Fix test: Always set tested component visible first; only assume node has changed if testvalue differs from original value * Cleanup, and refactoring, in Element, StateNode, UIInternals classes + mvn formatter. --------- Co-authored-by: Teppo Kurki <teppo.kurki@vaadin.com> Co-authored-by: Peter Czuczor <czuczor@gmail.com>
1 parent d90fb57 commit dab9d32

5 files changed

Lines changed: 74 additions & 9 deletions

File tree

‎flow-html-components/src/test/java/com/vaadin/flow/component/html/HtmlComponentSmokeTest.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ private static boolean isSpecialSetter(Method method) {
240240
}
241241

242242
private static void testSetter(HtmlComponent instance, Method setter) {
243+
instance.setVisible(true);
244+
243245
Class<?> propertyType = setter.getParameterTypes()[0];
244246

245247
Method getter = findGetter(setter);
@@ -272,16 +274,30 @@ private static void testSetter(HtmlComponent instance, Method setter) {
272274
StateNode elementNode = instance.getElement().getNode();
273275

274276
try {
277+
Object originalGetterValue = null;
278+
279+
try {
280+
originalGetterValue = getter.invoke(instance);
281+
if (isOptional) {
282+
originalGetterValue = ((Optional<?>) originalGetterValue)
283+
.orElse(null);
284+
}
285+
} catch (InvocationTargetException e) {
286+
// Unable to retrieve original value, assuming null
287+
}
288+
275289
// Purge all pending changes
276290
elementNode.collectChanges(c -> {
277291
});
278292

279293
setter.invoke(instance, testValue);
280294

281295
// Might have to add a blacklist for this logic at some point
282-
Assert.assertTrue(
283-
setter + " should update the underlying state node",
284-
hasPendingChanges(elementNode));
296+
if (!testValue.equals(originalGetterValue)) {
297+
Assert.assertTrue(
298+
setter + " should update the underlying state node",
299+
hasPendingChanges(elementNode));
300+
}
285301

286302
Object getterValue = getter.invoke(instance);
287303
if (isOptional) {

‎flow-server/src/main/java/com/vaadin/flow/component/internal/UIInternals.java‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,17 @@ public void addJavaScriptInvocation(
569569
PendingJavaScriptInvocation invocation) {
570570
session.checkHasLock();
571571
pendingJsInvocations.add(invocation);
572+
573+
invocation.getOwner()
574+
.addDetachListener(() -> pendingJsInvocations
575+
.removeIf(pendingInvocation -> pendingInvocation
576+
.equals(invocation)));
572577
}
573578

574579
/**
575-
* Gets all the pending JavaScript invocations and clears the queue.
580+
* Gets all the pending JavaScript invocations that are ready to be sent to
581+
* a client. Retains pending JavaScript invocations owned by invisible
582+
* components in the queue.
576583
*
577584
* @return a list of pending JavaScript invocations
578585
*/
@@ -583,13 +590,16 @@ public List<PendingJavaScriptInvocation> dumpPendingJavaScriptInvocations() {
583590
return Collections.emptyList();
584591
}
585592

586-
List<PendingJavaScriptInvocation> currentList = getPendingJavaScriptInvocations()
593+
List<PendingJavaScriptInvocation> readyToSend = getPendingJavaScriptInvocations()
594+
.filter(invocation -> invocation.getOwner().isVisible())
587595
.peek(PendingJavaScriptInvocation::setSentToBrowser)
588596
.collect(Collectors.toList());
589597

590-
pendingJsInvocations = new ArrayList<>();
598+
pendingJsInvocations = getPendingJavaScriptInvocations()
599+
.filter(invocation -> !invocation.getOwner().isVisible())
600+
.collect(Collectors.toCollection(ArrayList::new));
591601

592-
return currentList;
602+
return readyToSend;
593603
}
594604

595605
/**

‎flow-server/src/main/java/com/vaadin/flow/dom/Element.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.vaadin.flow.dom;
1717

1818
import java.io.Serializable;
19+
import java.util.ArrayList;
1920
import java.util.HashMap;
2021
import java.util.List;
2122
import java.util.Locale;
@@ -1333,8 +1334,8 @@ public <T extends Component> T as(Class<T> componentType) {
13331334
* at the same time that {@link Page#executeJs(String, Serializable...)}
13341335
* calls are invoked.
13351336
* <p>
1336-
* If the element is not attached, the function call will be deferred until
1337-
* the element is attached.
1337+
* If the element is not attached or not visible, the function call will be
1338+
* deferred until the element is attached and visible.
13381339
*
13391340
* @see JsonCodec JsonCodec for supported argument types
13401341
*
@@ -1392,6 +1393,9 @@ public PendingJavaScriptResult callJsFunction(String functionName,
13921393
* <code>'prefix' + $0</code> instead of <code>'prefix$0'</code> and
13931394
* <code>value[$0]</code> instead of <code>value.$0</code> since JavaScript
13941395
* variables aren't evaluated inside strings or property names.
1396+
* <p>
1397+
* If the element is not attached or not visible, the function call will be
1398+
* deferred until the element is attached and visible.
13951399
*
13961400
* @param expression
13971401
* the JavaScript expression to invoke
@@ -1466,6 +1470,10 @@ public Optional<ShadowRoot> getShadowRoot() {
14661470
/**
14671471
* Sets the element visibility value.
14681472
*
1473+
* Also executes pending javascript invocations, if their execution was
1474+
* requested while the element was not visible, and the element is now set
1475+
* as visible.
1476+
*
14691477
* @param visible
14701478
* the element visibility value
14711479
* @return this element

‎flow-server/src/main/java/com/vaadin/flow/internal/StateNode.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@
3838

3939
import com.vaadin.flow.component.ComponentUtil;
4040
import com.vaadin.flow.component.UI;
41+
import com.vaadin.flow.component.internal.PendingJavaScriptInvocation;
4142
import com.vaadin.flow.function.SerializableConsumer;
4243
import com.vaadin.flow.internal.StateTree.BeforeClientResponseEntry;
4344
import com.vaadin.flow.internal.StateTree.ExecutionRegistration;
4445
import com.vaadin.flow.internal.change.NodeAttachChange;
4546
import com.vaadin.flow.internal.change.NodeChange;
4647
import com.vaadin.flow.internal.change.NodeDetachChange;
48+
import com.vaadin.flow.internal.nodefeature.ElementData;
4749
import com.vaadin.flow.internal.nodefeature.InertData;
4850
import com.vaadin.flow.internal.nodefeature.NodeFeature;
4951
import com.vaadin.flow.internal.nodefeature.NodeFeatureRegistry;
@@ -632,6 +634,15 @@ public void collectChanges(Consumer<NodeChange> collector) {
632634
if (!isAttached()) {
633635
return;
634636
}
637+
638+
if (isInitialChanges && !isVisible()) {
639+
if (hasFeature(ElementData.class)) {
640+
doCollectChanges(collector,
641+
Stream.of(getFeature(ElementData.class)));
642+
}
643+
return;
644+
}
645+
635646
if (isInactive()) {
636647
if (isInitialChanges) {
637648
// send only required (reported) features updates
@@ -956,6 +967,25 @@ public boolean isInactive() {
956967
return getParent().isInactive();
957968
}
958969

970+
/**
971+
* Checks (recursively towards the parent node) whether the node is
972+
* effectively visible.
973+
* <p>
974+
* Non-visible node should not participate in any RPC communication.
975+
*
976+
* @return {@code true} if the node is effectively visible
977+
*/
978+
public boolean isVisible() {
979+
if (hasFeature(ElementData.class)) {
980+
boolean isVisibleSelf = getFeature(ElementData.class).isVisible();
981+
if (!isVisibleSelf || getParent() == null) {
982+
return isVisibleSelf;
983+
}
984+
return parent.isVisible();
985+
}
986+
return getParent() == null || parent.isVisible();
987+
}
988+
959989
/**
960990
* Returns whether or not this state node is inert and it should not receive
961991
* any updates from the client side. Inert state is inherited from parent,

‎flow-server/src/main/java/com/vaadin/flow/internal/StateTree.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,4 +459,5 @@ private Set<StateNode> doCollectDirtyNodes(boolean reset) {
459459
public void prepareForResync() {
460460
rootNode.prepareForResync();
461461
}
462+
462463
}

0 commit comments

Comments
 (0)