Fix EventNode bug where listeners aren't firing (#2529)

* add new test for RecursiveEvents

* fix recursive event calling

* implement by looping
This commit is contained in:
Lorenz Wrobel 2025-01-19 14:26:22 +01:00 committed by GitHub
parent c1a3ee6aca
commit 0d47d97417
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 0 deletions

View File

@ -292,10 +292,21 @@ non-sealed class EventNodeImpl<T extends Event> implements EventNode<T> {
aClass -> new Handle<>((Class<T>) aClass));
handle.invalidate();
});
invalidateRecursiveSuperclasses(eventClass);
final EventNodeImpl<? super T> parent = this.parent;
if (parent != null) parent.invalidateEvent(eventClass);
}
private void invalidateRecursiveSuperclasses(@NotNull Class<?> eventClass) {
if (RecursiveEvent.class.isAssignableFrom(eventClass)) {
for (var cls : this.handleMap.keySet()) {
if (eventClass.isAssignableFrom(cls)) {
this.handleMap.get(cls).invalidate();
}
}
}
}
private ListenerEntry<T> getEntry(Class<? extends T> type) {
return listenerMap.computeIfAbsent(type, aClass -> new ListenerEntry<>());
}

View File

@ -124,6 +124,33 @@ public class EventNodeTest {
assertTrue(result1.get(), "Recursive1 should be called due to the RecursiveEvent interface");
}
@Test
public void testRecursiveChild() {
var called1 = new AtomicBoolean(false);
var called2 = new AtomicBoolean(false);
var child1 = EventNode.all("child1");
var child2 = EventNode.all("child2");
child1.addListener(Recursive1.class, event -> called1.set(true));
child2.addListener(Recursive1.class, event -> called2.set(true));
var node = EventNode.all("main");
node.addChild(child1);
node.call(new Recursive2());
assertTrue(called1.get());
assertFalse(called2.get());
called1.set(false);
node.removeChild(child1);
node.addChild(child2);
node.call(new Recursive2());
assertFalse(called1.get());
assertTrue(called2.get());
}
// FIXME: nodes are currently unable to retrieve sub handles
//@Test
//public void recursiveSuper() {