Skip to content

Commit 73b3947

Browse files
authored
fix: reconcile remaining children to prevent double removal (#22566)
Fixes #22565
1 parent bcc5452 commit 73b3947

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

flow-server/src/main/java/com/vaadin/flow/component/ComponentEffect.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,13 @@ private static <T> void updateByChildSignals(
316316

317317
// Use LinkedList for order
318318
Element actualChild = remainingChildren.pollFirst();
319-
if (!remainingChildrenSet.contains(actualChild)) {
320-
continue; // skip children that have been removed already
319+
// Skip children that have been removed already
320+
while (actualChild != null
321+
&& !remainingChildrenSet.contains(actualChild)) {
322+
actualChild = remainingChildren.pollFirst();
323+
}
324+
if (actualChild == null) {
325+
continue;
321326
}
322327
if (!Objects.equals(actualChild, expectedChild)) {
323328
/*

flow-server/src/test/java/com/vaadin/flow/component/ComponentEffectTest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,30 +416,35 @@ public void bindChildren_removeItem_parentUpdated() {
416416
runWithFeatureFlagEnabled(() -> {
417417
ListSignal<String> taskList = new ListSignal<>(String.class);
418418
taskList.insertFirst("first");
419+
taskList.insertLast("middle");
419420
taskList.insertLast("last");
420421
TestLayout parentComponent = new TestLayout();
421422
new MockUI().add(parentComponent);
422423

423424
ComponentEffect.bindChildren(parentComponent, taskList,
424425
valueSignal -> new TestComponent(valueSignal.value()));
425426

426-
assertEquals("Parent component children count is wrong", 2,
427+
assertEquals("Parent component children count is wrong", 3,
427428
parentComponent.getComponentCount());
428429

429430
List<TestComponent> children = parentComponent.getChildren()
430431
.map(TestComponent.class::cast).toList();
431432

432433
taskList.remove(taskList.value().get(0));
433434

434-
assertEquals("Parent component children count is wrong", 1,
435+
assertEquals("Parent component children count is wrong", 2,
435436
parentComponent.getComponentCount());
437+
assertEquals("middle", ((TestComponent) parentComponent
438+
.getChildren().toList().get(0)).getValue());
436439
assertEquals("last", ((TestComponent) parentComponent.getChildren()
437-
.toList().get(0)).getValue());
440+
.toList().get(1)).getValue());
438441

439442
assertEquals(1, children.get(0).attachCounter);
440443
assertEquals(1, children.get(0).detachCounter);
441444
assertEquals(1, children.get(1).attachCounter);
442445
assertEquals(0, children.get(1).detachCounter);
446+
assertEquals(1, children.get(2).attachCounter);
447+
assertEquals(0, children.get(2).detachCounter);
443448
});
444449
}
445450

0 commit comments

Comments
 (0)