Skip to content

Commit b0a889c

Browse files
authored
fix: ignore nonexistent nodes during resync (#14749)
Ignore changes for nodes that are not available in the tree when a resync is underway. This will get the page to a working condition after a resync as all changes for existing nodes are executed. Fixes #14232 Fixes #14470
1 parent 6def298 commit b0a889c

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

‎flow-client/src/main/java/com/vaadin/client/flow/StateTree.java‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class StateTree {
5050
private JsMap<Integer, String> nodeFeatureDebugName;
5151

5252
private boolean updateInProgress;
53+
private boolean resync;
5354

5455
/**
5556
* Creates a new instance connected to the given registry.
@@ -152,6 +153,26 @@ public void prepareForResync() {
152153
node.setParent(null);
153154
}
154155
});
156+
setResync(true);
157+
}
158+
159+
/**
160+
* Check if tree is resynchronizing after a {@link #prepareForResync}
161+
*
162+
* @return true if resync called
163+
*/
164+
public boolean isResync() {
165+
return resync;
166+
}
167+
168+
/**
169+
* Set the resynchronization state for the StateTree.
170+
*
171+
* @param resync
172+
* resynchronization state to set
173+
*/
174+
public void setResync(boolean resync) {
175+
this.resync = resync;
155176
}
156177

157178
/**
@@ -482,5 +503,4 @@ String getFeatureDebugName(int id) {
482503
return "Unknown node feature: " + id;
483504
}
484505
}
485-
486506
}

‎flow-client/src/main/java/com/vaadin/client/flow/TreeChangeProcessor.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,16 @@ public static JsSet<StateNode> processChanges(StateTree tree,
6464
for (int i = 0; i < length; i++) {
6565
JsonObject change = changes.getObject(i);
6666
if (!isAttach(change)) {
67-
nodes.add(processChange(tree, change));
67+
final StateNode value = processChange(tree, change);
68+
if (value != null) {
69+
nodes.add(value);
70+
}
6871
}
6972
}
7073
return nodes;
7174
} finally {
7275
tree.setUpdateInProgress(false);
76+
tree.setResync(false);
7377
}
7478
}
7579

@@ -112,7 +116,11 @@ public static StateNode processChange(StateTree tree, JsonObject change) {
112116
int nodeId = (int) change.getNumber(JsonConstants.CHANGE_NODE);
113117

114118
StateNode node = tree.getNode(nodeId);
115-
assert node != null;
119+
if (node == null && tree.isResync()) {
120+
// Resync should not stop handling changes
121+
return node;
122+
}
123+
assert node != null : "No attached node found";
116124

117125
switch (type) {
118126
case JsonConstants.CHANGE_TYPE_NOOP:

‎flow-client/src/test/java/com/vaadin/client/flow/TreeChangeProcessorTest.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ public void testPutChange() {
6363
Assert.assertEquals(tree.getRootNode(), node);
6464
}
6565

66+
@Test
67+
public void resync_withDetachForNonexistentNode_noAssertionFailure() {
68+
StateNode child = new StateNode(2, tree);
69+
70+
JsonObject change = detachChange(child.getId());
71+
72+
tree.prepareForResync();
73+
74+
try {
75+
TreeChangeProcessor.processChange(tree, change);
76+
} catch (AssertionError ae) {
77+
Assert.fail("Should not fail for an nonexistent node on resync");
78+
}
79+
}
80+
6681
@Test
6782
public void testMapRemoveChange() {
6883
MapProperty property = tree.getRootNode().getMap(ns).getProperty(myKey);

0 commit comments

Comments
 (0)