use generic event future

This commit is contained in:
Jörg Prante 2023-12-11 12:06:16 +01:00
parent ef0a26bbd1
commit 104dde2568
2 changed files with 7 additions and 9 deletions

View file

@ -20,7 +20,7 @@ public class GenericEventManager {
} }
public void post(DefaultGenericEvent event, public void post(DefaultGenericEvent event,
CompletableFuture<Boolean> future) { CompletableFuture<GenericEvent> future) {
SubscriberRegistry subscriberRegistry = eventBus.getSubscribers(); SubscriberRegistry subscriberRegistry = eventBus.getSubscribers();
Set<Subscriber> set = subscriberRegistry.getSubscribersForTesting(event.getClass()); Set<Subscriber> set = subscriberRegistry.getSubscribersForTesting(event.getClass());
event.setListener(new WrappedListener(event.getListener(), set.size(), future)); event.setListener(new WrappedListener(event.getListener(), set.size(), future));
@ -33,9 +33,9 @@ public class GenericEventManager {
private int size; private int size;
private final CompletableFuture<Boolean> future; private final CompletableFuture<GenericEvent> future;
public WrappedListener(Listener listener, int size, CompletableFuture<Boolean> future) { public WrappedListener(Listener listener, int size, CompletableFuture<GenericEvent> future) {
this.listener = listener; this.listener = listener;
this.size = size; this.size = size;
this.future = future; this.future = future;
@ -47,7 +47,7 @@ public class GenericEventManager {
listener.listen(event); listener.listen(event);
} }
if (--size == 0) { if (--size == 0) {
future.complete(true); future.complete(event);
} }
} }
} }

View file

@ -57,14 +57,12 @@ public class EventManagerTest {
.register(consumer2) .register(consumer2)
.loadEventConsumers() .loadEventConsumers()
.build(); .build();
CompletableFuture<Boolean> future = new CompletableFuture<>(); CompletableFuture<GenericEvent> future = new CompletableFuture<>();
eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> { eventManager.getGenericEventManager().post(new DefaultGenericEvent(e -> {
logger.log(Level.INFO, "received event " + e); logger.log(Level.INFO, "received event " + e);
}), future); }), future);
Boolean b = future.get(); GenericEvent e = future.get();
if (b != null && b) { logger.log(Level.INFO, "the event " + e + " was received by all consumers, continuing");
logger.log(Level.INFO, "the event was received by all consumers, continuing");
}
} }
private static class TestEventConsumer implements EventConsumer { private static class TestEventConsumer implements EventConsumer {