Prevent node update from being called multiple times

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-08-27 09:51:36 +02:00
parent 909c42c8d0
commit 5c9c57439a

View File

@ -273,7 +273,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
@Override @Override
public void call(@NotNull E event) { public void call(@NotNull E event) {
if (!updated) update(); update();
final Consumer<E>[] listeners = this.listeners; final Consumer<E>[] listeners = this.listeners;
if (listeners.length == 0) return; if (listeners.length == 0) return;
for (Consumer<E> listener : listeners) { for (Consumer<E> listener : listeners) {
@ -283,12 +283,14 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
@Override @Override
public boolean hasListener() { public boolean hasListener() {
if (!updated) update(); update();
return listeners.length > 0; return listeners.length > 0;
} }
void update() { void update() {
if (updated) return;
synchronized (GLOBAL_CHILD_LOCK) { synchronized (GLOBAL_CHILD_LOCK) {
if (updated) return;
this.listenersCache.clear(); this.listenersCache.clear();
recursiveUpdate(node); recursiveUpdate(node);
this.listeners = listenersCache.toArray(Consumer[]::new); this.listeners = listenersCache.toArray(Consumer[]::new);
@ -338,7 +340,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
final Object handler = filter.castHandler(event); final Object handler = filter.castHandler(event);
final Handle<E> handle = handlers.get(handler); final Handle<E> handle = handlers.get(handler);
if (handle != null) { // Run the listeners of the mapped node if (handle != null) { // Run the listeners of the mapped node
if (!handle.updated) handle.update(); handle.update();
for (Consumer<E> listener : handle.listeners) { for (Consumer<E> listener : handle.listeners) {
listener.accept(event); listener.accept(event);
} }