Skip to content

Commit 38cb83b

Browse files
fix: remove detach listener when javascript execution completes (#16836) (#16848)
* fix: remove detach listener when javascript execution completes A detach listener is added for pending javascript invocation owner nodes to clean up the queue, if the owner gets detached. This change also removes the detach listener when the javascript execution completes. * make sure only one detach listener per node is registered * change map type for detach listeners Co-authored-by: Marco Collovati <marco@vaadin.com>
1 parent f469f56 commit 38cb83b

2 files changed

Lines changed: 206 additions & 6 deletions

File tree

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

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@
4949
import com.vaadin.flow.component.page.Page;
5050
import com.vaadin.flow.dom.ElementUtil;
5151
import com.vaadin.flow.dom.impl.BasicElementStateProvider;
52+
import com.vaadin.flow.function.SerializableConsumer;
5253
import com.vaadin.flow.internal.ConstantPool;
5354
import com.vaadin.flow.internal.JsonCodec;
55+
import com.vaadin.flow.internal.StateNode;
5456
import com.vaadin.flow.internal.StateTree;
5557
import com.vaadin.flow.internal.UrlUtil;
5658
import com.vaadin.flow.internal.nodefeature.LoadingIndicatorConfigurationMap;
@@ -70,6 +72,7 @@
7072
import com.vaadin.flow.router.internal.AfterNavigationHandler;
7173
import com.vaadin.flow.router.internal.BeforeEnterHandler;
7274
import com.vaadin.flow.router.internal.BeforeLeaveHandler;
75+
import com.vaadin.flow.server.Command;
7376
import com.vaadin.flow.server.VaadinService;
7477
import com.vaadin.flow.server.VaadinSession;
7578
import com.vaadin.flow.server.communication.PushConnection;
@@ -170,6 +173,8 @@ public List<Object> getParameters() {
170173

171174
private List<PendingJavaScriptInvocation> pendingJsInvocations = new ArrayList<>();
172175

176+
private final HashMap<StateNode, PendingJavaScriptInvocationDetachListener> pendingJsInvocationDetachListeners = new HashMap<>();
177+
173178
/**
174179
* The related UI.
175180
*/
@@ -569,11 +574,6 @@ public void addJavaScriptInvocation(
569574
PendingJavaScriptInvocation invocation) {
570575
session.checkHasLock();
571576
pendingJsInvocations.add(invocation);
572-
573-
invocation.getOwner()
574-
.addDetachListener(() -> pendingJsInvocations
575-
.removeIf(pendingInvocation -> pendingInvocation
576-
.equals(invocation)));
577577
}
578578

579579
/**
@@ -598,10 +598,61 @@ public List<PendingJavaScriptInvocation> dumpPendingJavaScriptInvocations() {
598598
pendingJsInvocations = getPendingJavaScriptInvocations()
599599
.filter(invocation -> !invocation.getOwner().isVisible())
600600
.collect(Collectors.toCollection(ArrayList::new));
601-
601+
pendingJsInvocations
602+
.forEach(this::registerDetachListenerForPendingInvocation);
602603
return readyToSend;
603604
}
604605

606+
@SuppressWarnings({ "rawtypes", "unchecked" })
607+
private void registerDetachListenerForPendingInvocation(
608+
PendingJavaScriptInvocation invocation) {
609+
610+
PendingJavaScriptInvocationDetachListener listener = pendingJsInvocationDetachListeners
611+
.computeIfAbsent(invocation.getOwner(), node -> {
612+
PendingJavaScriptInvocationDetachListener detachListener = new PendingJavaScriptInvocationDetachListener();
613+
detachListener.registration = Registration.combine(
614+
() -> pendingJsInvocationDetachListeners
615+
.remove(node),
616+
node.addDetachListener(detachListener));
617+
return detachListener;
618+
});
619+
listener.invocationList.add(invocation);
620+
621+
SerializableConsumer callback = unused -> listener
622+
.onInvocationCompleted(invocation);
623+
invocation.then(callback, callback);
624+
}
625+
626+
private class PendingJavaScriptInvocationDetachListener implements Command {
627+
private final Set<PendingJavaScriptInvocation> invocationList = new HashSet<>();
628+
629+
private Registration registration;
630+
631+
@Override
632+
public void execute() {
633+
if (!invocationList.isEmpty()) {
634+
List<PendingJavaScriptInvocation> copy = new ArrayList<>(
635+
invocationList);
636+
invocationList.clear();
637+
copy.forEach(this::removePendingInvocation);
638+
}
639+
}
640+
641+
private void removePendingInvocation(
642+
PendingJavaScriptInvocation invocation) {
643+
UIInternals.this.pendingJsInvocations.removeIf(
644+
pendingInvocation -> pendingInvocation.equals(invocation));
645+
if (invocationList.isEmpty() && registration != null) {
646+
registration.remove();
647+
}
648+
}
649+
650+
void onInvocationCompleted(PendingJavaScriptInvocation invocation) {
651+
invocationList.remove(invocation);
652+
removePendingInvocation(invocation);
653+
}
654+
}
655+
605656
/**
606657
* Gets the pending javascript invocations added with
607658
* {@link #addJavaScriptInvocation(PendingJavaScriptInvocation)} after last

‎flow-server/src/test/java/com/vaadin/flow/component/internal/UIInternalsTest.java‎

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.junit.Assert;
1212
import org.junit.Before;
1313
import org.junit.Test;
14+
import org.mockito.ArgumentMatchers;
1415
import org.mockito.Mock;
1516
import org.mockito.Mockito;
1617
import org.mockito.MockitoAnnotations;
@@ -24,6 +25,10 @@
2425
import com.vaadin.flow.di.DefaultInstantiator;
2526
import com.vaadin.flow.dom.Element;
2627
import com.vaadin.flow.function.DeploymentConfiguration;
28+
import com.vaadin.flow.internal.JsonCodec;
29+
import com.vaadin.flow.internal.StateNode;
30+
import com.vaadin.flow.internal.nodefeature.ElementChildrenList;
31+
import com.vaadin.flow.internal.nodefeature.ElementData;
2732
import com.vaadin.flow.router.Location;
2833
import com.vaadin.flow.router.ParentLayout;
2934
import com.vaadin.flow.router.Route;
@@ -274,6 +279,150 @@ public void showRouteTarget_navigateToAnotherLayoutHierarchy_detachedLayoutHiera
274279
subLayout.getElement().getChildren().count());
275280
}
276281

282+
@Test
283+
public void dumpPendingJavaScriptInvocations_detachListenerRegisteredOnce() {
284+
StateNode node = Mockito.spy(new StateNode(ElementData.class));
285+
node.getFeature(ElementData.class).setVisible(false);
286+
internals.getStateTree().getRootNode()
287+
.getFeature(ElementChildrenList.class).add(0, node);
288+
289+
internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node,
290+
new UIInternals.JavaScriptInvocation("")));
291+
internals.dumpPendingJavaScriptInvocations();
292+
internals.dumpPendingJavaScriptInvocations();
293+
internals.dumpPendingJavaScriptInvocations();
294+
295+
Mockito.verify(node, Mockito.times(1))
296+
.addDetachListener(ArgumentMatchers.any());
297+
}
298+
299+
@Test
300+
public void dumpPendingJavaScriptInvocations_multipleInvocationPerNode_onlyOneDetachListenerRegistered() {
301+
StateNode node = Mockito.spy(new StateNode(ElementData.class));
302+
node.getFeature(ElementData.class).setVisible(false);
303+
internals.getStateTree().getRootNode()
304+
.getFeature(ElementChildrenList.class).add(0, node);
305+
306+
internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node,
307+
new UIInternals.JavaScriptInvocation("1")));
308+
internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node,
309+
new UIInternals.JavaScriptInvocation("2")));
310+
internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node,
311+
new UIInternals.JavaScriptInvocation("3")));
312+
internals.dumpPendingJavaScriptInvocations();
313+
314+
Mockito.verify(node, Mockito.times(1))
315+
.addDetachListener(ArgumentMatchers.any());
316+
}
317+
318+
@Test
319+
public void dumpPendingJavaScriptInvocations_registerOneDetachListenerPerNode() {
320+
StateNode node1 = Mockito.spy(new StateNode(ElementData.class));
321+
node1.getFeature(ElementData.class).setVisible(false);
322+
internals.getStateTree().getRootNode()
323+
.getFeature(ElementChildrenList.class).add(0, node1);
324+
internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node1,
325+
new UIInternals.JavaScriptInvocation("1")));
326+
327+
StateNode node2 = Mockito.spy(new StateNode(ElementData.class));
328+
node2.getFeature(ElementData.class).setVisible(false);
329+
internals.getStateTree().getRootNode()
330+
.getFeature(ElementChildrenList.class).add(0, node2);
331+
internals.addJavaScriptInvocation(new PendingJavaScriptInvocation(node2,
332+
new UIInternals.JavaScriptInvocation("1")));
333+
334+
internals.dumpPendingJavaScriptInvocations();
335+
336+
Mockito.verify(node1, Mockito.times(1))
337+
.addDetachListener(ArgumentMatchers.any());
338+
Mockito.verify(node2, Mockito.times(1))
339+
.addDetachListener(ArgumentMatchers.any());
340+
}
341+
342+
@Test
343+
public void dumpPendingJavaScriptInvocations_invocationCompletes_pendingListPurged() {
344+
StateNode node = Mockito.spy(new StateNode(ElementData.class));
345+
node.getFeature(ElementData.class).setVisible(false);
346+
internals.getStateTree().getRootNode()
347+
.getFeature(ElementChildrenList.class).add(0, node);
348+
349+
PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation(
350+
node, new UIInternals.JavaScriptInvocation(""));
351+
internals.addJavaScriptInvocation(invocation);
352+
internals.dumpPendingJavaScriptInvocations();
353+
354+
Mockito.verify(node, Mockito.times(1))
355+
.addDetachListener(ArgumentMatchers.any());
356+
357+
invocation.complete(JsonCodec.encodeWithTypeInfo("OK"));
358+
359+
Assert.assertEquals(0,
360+
internals.getPendingJavaScriptInvocations().count());
361+
}
362+
363+
@Test
364+
public void dumpPendingJavaScriptInvocations_invocationFails_pendingListPurged() {
365+
StateNode node = Mockito.spy(new StateNode(ElementData.class));
366+
node.getFeature(ElementData.class).setVisible(false);
367+
internals.getStateTree().getRootNode()
368+
.getFeature(ElementChildrenList.class).add(0, node);
369+
370+
PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation(
371+
node, new UIInternals.JavaScriptInvocation(""));
372+
internals.addJavaScriptInvocation(invocation);
373+
internals.dumpPendingJavaScriptInvocations();
374+
375+
Mockito.verify(node, Mockito.times(1))
376+
.addDetachListener(ArgumentMatchers.any());
377+
378+
invocation.completeExceptionally(JsonCodec.encodeWithTypeInfo("ERROR"));
379+
380+
Assert.assertEquals(0,
381+
internals.getPendingJavaScriptInvocations().count());
382+
}
383+
384+
@Test
385+
public void dumpPendingJavaScriptInvocations_invocationCanceled_pendingListPurged() {
386+
StateNode node = Mockito.spy(new StateNode(ElementData.class));
387+
node.getFeature(ElementData.class).setVisible(false);
388+
internals.getStateTree().getRootNode()
389+
.getFeature(ElementChildrenList.class).add(0, node);
390+
391+
PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation(
392+
node, new UIInternals.JavaScriptInvocation(""));
393+
internals.addJavaScriptInvocation(invocation);
394+
internals.dumpPendingJavaScriptInvocations();
395+
396+
Mockito.verify(node, Mockito.times(1))
397+
.addDetachListener(ArgumentMatchers.any());
398+
399+
invocation.cancelExecution();
400+
401+
Assert.assertEquals(0,
402+
internals.getPendingJavaScriptInvocations().count());
403+
}
404+
405+
@Test
406+
public void dumpPendingJavaScriptInvocations_nodeDetached_pendingListPurged() {
407+
StateNode node = Mockito.spy(new StateNode(ElementData.class));
408+
node.getFeature(ElementData.class).setVisible(false);
409+
internals.getStateTree().getRootNode()
410+
.getFeature(ElementChildrenList.class).add(0, node);
411+
412+
PendingJavaScriptInvocation invocation = new PendingJavaScriptInvocation(
413+
node, new UIInternals.JavaScriptInvocation(""));
414+
internals.addJavaScriptInvocation(invocation);
415+
internals.dumpPendingJavaScriptInvocations();
416+
417+
Mockito.verify(node, Mockito.times(1))
418+
.addDetachListener(ArgumentMatchers.any());
419+
420+
node.setParent(null);
421+
422+
Assert.assertEquals(0,
423+
internals.getPendingJavaScriptInvocations().count());
424+
}
425+
277426
private PushConfiguration setUpInitialPush() {
278427
DeploymentConfiguration config = Mockito
279428
.mock(DeploymentConfiguration.class);

0 commit comments

Comments
 (0)