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