EventBus unregister
This commit is contained in:
@@ -17,16 +17,20 @@ public class EventBus {
|
||||
private Queue<Event> eventQueue;
|
||||
private Map<Class<? extends Event>, List<Pair<Object, Method>>> subscribes = new HashMap<>();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void registerSubscribes(Object subscriberObject) {
|
||||
Stream.of(subscriberObject.getClass().getDeclaredMethods())
|
||||
private Stream<Method> getMethods(Object subscriberObject) {
|
||||
return Stream.of(subscriberObject.getClass().getDeclaredMethods())
|
||||
.filter(method -> method.isAnnotationPresent(Subscriber.class))
|
||||
.filter(method -> method.getReturnType().equals(Void.TYPE))
|
||||
.filter(method -> method.getParameterCount() == 1)
|
||||
.filter(method -> Event.class.isAssignableFrom(method.getParameterTypes()[0]))
|
||||
.filter(method -> Event.class.isAssignableFrom(method.getParameterTypes()[0]));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void registerSubscribes(Object subscriberObject) {
|
||||
getMethods(subscriberObject)
|
||||
.forEach(method -> {
|
||||
Class<? extends Event> type = (Class<? extends Event>) method.getParameterTypes()[0];
|
||||
List<Pair<Object, Method>> pairs;
|
||||
final Class<? extends Event> type = (Class<? extends Event>) method.getParameterTypes()[0];
|
||||
final List<Pair<Object, Method>> pairs;
|
||||
if (subscribes.containsKey(type)) {
|
||||
pairs = subscribes.get(type);
|
||||
} else {
|
||||
@@ -36,4 +40,20 @@ public class EventBus {
|
||||
pairs.add(new Pair<>(subscriberObject, method));
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void unregisterSubscribes(Object subscriberObject) {
|
||||
getMethods(subscriberObject)
|
||||
.forEach(method -> {
|
||||
final Class<? extends Event> type = (Class<? extends Event>) method.getParameterTypes()[0];
|
||||
if (subscribes.containsKey(type)) {
|
||||
final List<Pair<Object, Method>> pairs = subscribes.get(type);
|
||||
pairs.removeIf(pair -> pair.getKey() == subscriberObject);
|
||||
|
||||
if (pairs.isEmpty()) {
|
||||
subscribes.remove(type);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import javafx.util.Pair;
|
||||
import mc.core.eventbus.Event;
|
||||
import mc.core.eventbus.EventBus;
|
||||
import mc.core.eventbus.Subscriber;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.internal.util.reflection.Whitebox;
|
||||
|
||||
@@ -17,14 +17,22 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
class TestEventBus {
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<Class<? extends Event>, List<Pair<Object, Method>>> getEventBusFieldSubscribes() {
|
||||
return (Map<Class<? extends Event>, List<Pair<Object, Method>>>) Whitebox.getInternalState(EventBus.getInsnance(), "subscribes");
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void before() {
|
||||
getEventBusFieldSubscribes().clear();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testRegisterSubscribes() {
|
||||
DumbEventHandler handler = new DumbEventHandler();
|
||||
EventBus.getInsnance().registerSubscribes(handler);
|
||||
|
||||
Map<Object, List<Pair<Object, Method>>> subscribes =
|
||||
(Map<Object, List<Pair<Object, Method>>>)
|
||||
Whitebox.getInternalState(EventBus.getInsnance(), "subscribes");
|
||||
Map<Class<? extends Event>, List<Pair<Object, Method>>> subscribes = getEventBusFieldSubscribes();
|
||||
assertEquals(1, subscribes.size());
|
||||
|
||||
List<Pair<Object, Method>> pairs = subscribes.values().iterator().next();
|
||||
@@ -35,6 +43,17 @@ class TestEventBus {
|
||||
assertEquals("corectSubscribe", pair.getValue().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUnregisterSubscribes() {
|
||||
DumbEventHandler handler = new DumbEventHandler();
|
||||
EventBus.getInsnance().registerSubscribes(handler);
|
||||
|
||||
EventBus.getInsnance().unregisterSubscribes(handler);
|
||||
|
||||
Map<Class<? extends Event>, List<Pair<Object, Method>>> subscribes = getEventBusFieldSubscribes();
|
||||
assertEquals(0, subscribes.size());
|
||||
}
|
||||
|
||||
private class DumbEvent implements Event {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user