Propagate binding listeners

This commit is contained in:
TheMode 2021-08-17 03:10:16 +02:00
parent 488fe06f01
commit d0a5c781ca

View File

@ -156,12 +156,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
final var eventType = listener.getEventType(); final var eventType = listener.getEventType();
var entry = getEntry(eventType); var entry = getEntry(eventType);
entry.listeners.add((EventListener<T>) listener); entry.listeners.add((EventListener<T>) listener);
final var parent = this.parent; propagateToParent(eventType, 1);
if (parent != null) {
synchronized (parent.lock) {
parent.propagateChildCountChange(eventType, 1);
}
}
} }
return this; return this;
} }
@ -174,11 +169,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
if (entry == null) return this; if (entry == null) return this;
var listeners = entry.listeners; var listeners = entry.listeners;
final boolean removed = listeners.remove(listener); final boolean removed = listeners.remove(listener);
if (removed && parent != null) { if (removed) propagateToParent(eventType, -1);
synchronized (parent.lock) {
parent.propagateChildCountChange(eventType, -1);
}
}
} }
return this; return this;
} }
@ -198,6 +189,7 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
synchronized (mappedNodeCache) { synchronized (mappedNodeCache) {
entry.mappedNode.put(value, (EventNode<T>) nodeImpl); entry.mappedNode.put(value, (EventNode<T>) nodeImpl);
mappedNodeCache.put(value, entry); mappedNodeCache.put(value, entry);
// TODO propagate
} }
}); });
} }
@ -209,7 +201,12 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
synchronized (mappedNodeCache) { synchronized (mappedNodeCache) {
var entry = mappedNodeCache.remove(value); var entry = mappedNodeCache.remove(value);
if (entry == null) return false; if (entry == null) return false;
return entry.mappedNode.remove(value) != null; final EventNode<T> previousNode = entry.mappedNode.remove(value);
if (previousNode != null) {
// TODO propagate
return true;
}
return false;
} }
} }
} }
@ -219,7 +216,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
synchronized (GLOBAL_CHILD_LOCK) { synchronized (GLOBAL_CHILD_LOCK) {
for (var eventType : binding.eventTypes()) { for (var eventType : binding.eventTypes()) {
var entry = getEntry((Class<? extends T>) eventType); var entry = getEntry((Class<? extends T>) eventType);
entry.bindingConsumers.add((Consumer<T>) binding.consumer(eventType)); final boolean added = entry.bindingConsumers.add((Consumer<T>) binding.consumer(eventType));
if (added) propagateToParent((Class<? extends T>) eventType, 1);
} }
} }
} }
@ -230,7 +228,8 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
for (var eventType : binding.eventTypes()) { for (var eventType : binding.eventTypes()) {
var entry = listenerMap.get(eventType); var entry = listenerMap.get(eventType);
if (entry == null) return; if (entry == null) return;
entry.bindingConsumers.remove(binding.consumer(eventType)); final boolean removed = entry.bindingConsumers.remove(binding.consumer(eventType));
if (removed) propagateToParent((Class<? extends T>) eventType, -1);
} }
} }
} }
@ -274,6 +273,15 @@ class EventNodeImpl<T extends Event> implements EventNode<T> {
} }
} }
private void propagateToParent(Class<? extends T> eventClass, int count) {
final var parent = this.parent;
if (parent != null) {
synchronized (parent.lock) {
parent.propagateChildCountChange(eventClass, count);
}
}
}
private void propagateNode(EventNodeImpl<? extends T> child, IntUnaryOperator operator) { private void propagateNode(EventNodeImpl<? extends T> child, IntUnaryOperator operator) {
synchronized (lock) { synchronized (lock) {
final var listeners = child.listenerMap; final var listeners = child.listenerMap;