Compare commits
No commits in common. "b915a5c4901d34a4ee4f21a0701573381109e4e7" and "b758f4cef1aa0407242a105b90e1725dee7b7ae2" have entirely different histories.
b915a5c490
...
b758f4cef1
@ -27,21 +27,7 @@ public final class EventBus {
|
||||
*/
|
||||
private static final class DispatchState {
|
||||
|
||||
/**
|
||||
* Indicates that the last event handler invoked has called {@link EventBus#cancel}. In that
|
||||
* case, the event is not dispatched further.
|
||||
*
|
||||
* @since 0.1.0
|
||||
*/
|
||||
boolean isCancelled;
|
||||
|
||||
/**
|
||||
* Is incremented when {@link EventBus#dispatch(Object)} is invoked and decremented when it
|
||||
* finishes. This allows keeping track of nested dispatches.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
int nestingCount;
|
||||
boolean isDispatching, isCancelled;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,11 +79,9 @@ public final class EventBus {
|
||||
Objects.requireNonNull(event);
|
||||
logger.log(Level.INFO, "Dispatching event {0}", event);
|
||||
|
||||
// Look up dispatch state
|
||||
// Set dispatch state
|
||||
var state = dispatchState.get();
|
||||
|
||||
// Increment nesting count (becomes > 1 during nested dispatches)
|
||||
++state.nestingCount;
|
||||
state.isDispatching = true;
|
||||
|
||||
Iterator<EventHandler> handlers = getHandlersFor(event.getClass());
|
||||
if (handlers.hasNext()) {
|
||||
@ -110,14 +94,14 @@ public final class EventBus {
|
||||
try {
|
||||
handlers.next().execute(event);
|
||||
} catch (InvocationTargetException e) {
|
||||
if (e.getCause() instanceof Error)
|
||||
|
||||
// Transparently pass error to the caller
|
||||
throw (Error) e.getCause();
|
||||
else if (event instanceof DeadEvent || event instanceof ExceptionEvent)
|
||||
if (event instanceof DeadEvent || event instanceof ExceptionEvent)
|
||||
|
||||
// Warn about system event not being handled
|
||||
logger.log(Level.WARNING, event + " not handled due to exception", e);
|
||||
else if (e.getCause() instanceof Error)
|
||||
|
||||
// Transparently pass error to the caller
|
||||
throw (Error) e.getCause();
|
||||
else
|
||||
|
||||
// Dispatch exception event
|
||||
@ -134,8 +118,8 @@ public final class EventBus {
|
||||
dispatch(new DeadEvent(this, event));
|
||||
}
|
||||
|
||||
// Decrement nesting count (becomes 0 when all dispatches on the thread are finished)
|
||||
--state.nestingCount;
|
||||
// Reset dispatch state
|
||||
state.isDispatching = false;
|
||||
|
||||
logger.log(Level.DEBUG, "Finished dispatching event {0}", event);
|
||||
}
|
||||
@ -171,7 +155,7 @@ public final class EventBus {
|
||||
*/
|
||||
public void cancel() {
|
||||
var state = dispatchState.get();
|
||||
if (state.nestingCount > 0 && !state.isCancelled)
|
||||
if (state.isDispatching && !state.isCancelled)
|
||||
state.isCancelled = true;
|
||||
else
|
||||
throw new EventBusException("Calling thread not an active dispatching thread!");
|
||||
|
@ -1,73 +0,0 @@
|
||||
package dev.kske.eventbus.core;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
/**
|
||||
* Tests nested event dispatches.
|
||||
*
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since 1.2.0
|
||||
*/
|
||||
class NestedTest {
|
||||
|
||||
EventBus bus;
|
||||
boolean nestedHit;
|
||||
|
||||
/**
|
||||
* Constructs an event bus and registers this test instance as an event listener.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@BeforeEach
|
||||
void registerListener() {
|
||||
bus = new EventBus();
|
||||
bus.registerListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a simple event, which should in turn cause a string to be dispatched as a nested
|
||||
* event. If the corresponding handler sets {@link #nestedHit} to {@code true}, the test is
|
||||
* successful.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Test
|
||||
void testNestedDispatch() {
|
||||
bus.dispatch(new SimpleEvent());
|
||||
assertTrue(nestedHit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatches a string as a nested event and cancels the current dispatch afterwards.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Event(SimpleEvent.class)
|
||||
void onSimpleEvent() {
|
||||
bus.dispatch("Nested event");
|
||||
bus.cancel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets {@link #nestedHit} to {@code true} indicating that nested dispatches work.
|
||||
*
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Event(String.class)
|
||||
void onString() {
|
||||
nestedHit = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fails the test if an exception is caused during the dispatch.
|
||||
*
|
||||
* @param e the event containing the exception
|
||||
* @since 1.2.0
|
||||
*/
|
||||
@Event
|
||||
void onException(ExceptionEvent e) {
|
||||
fail("Exception during dispatch", e.getCause());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user