Use singleton EventBus in README and unit test

This commit is contained in:
Kai S. K. Engelbart 2020-09-08 09:38:46 +02:00
parent f407021cea
commit bdf7dcfeda
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
2 changed files with 7 additions and 7 deletions

View File

@ -8,6 +8,8 @@ Any class can be made an event by implementing the `IEvent` interface.
Using an instance of the `EventBus` class, an instant of the event class can be dispatched.
This means that it will be forwarded to all listeners registered for it at the event bus.
In addition, a singleton instance of the event bus is provided by the `EventBus#getInstance()` method.
To listen to events, register event handling methods using the `Event` annotation.
For this to work, the method must have a return type of `void` and declare a single parameter of the desired event type.
Additionally, the class containing the method must implement the `EventListener` interface.
@ -30,13 +32,12 @@ import dev.kske.eventbus.*;
public class SimpleEventListener implements EventListener {
public SimpleEventListener() {
EventBus eventBus = new EventBus();
// Register this listener at the event bus
eventBus.register(this);
EventBus.getInstance().register(this);
// Dispatch a SimpleEvent
eventBus.dispatch(new SimpleEvent());
EventBus.getInstance().dispatch(new SimpleEvent());
}
@Event
@ -65,7 +66,7 @@ To include it inside your project, just add the Maven repository and the depende
<dependency>
<groupId>dev.kske</groupId>
<artifactId>event-bus</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
</dependency>
</dependencies>
```

View File

@ -12,17 +12,16 @@ import org.junit.jupiter.api.*;
*/
class EventBusTest implements EventListener {
public EventBus eventBus = new EventBus();
int hits;
@BeforeEach
public void registerListener() {
eventBus.registerListener(this);
EventBus.getInstance().registerListener(this);
}
@Test
void testDispatch() {
eventBus.dispatch(new SimpleEvent());
EventBus.getInstance().dispatch(new SimpleEvent());
}
@Event(priority = 50)