2020-09-03 10:05:00 +02:00
# Event Bus
2020-09-02 11:25:13 +02:00
2020-09-06 14:57:17 +02:00
## Introduction
2020-09-03 10:05:00 +02:00
This library allows passing events between different objects without them having a direct reference to each other.
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.
2020-09-08 09:38:46 +02:00
In addition, a singleton instance of the event bus is provided by the `EventBus#getInstance()` method.
2020-09-03 10:05:00 +02:00
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.
2020-09-06 14:57:17 +02:00
## A Simple Example
2020-09-03 10:05:00 +02:00
Lets look at a simple example: we declare the empty class `SimpleEvent` that implements `IEvent` and can thus be used as an event.
```java
import dev.kske.eventbus.IEvent;
public class SimpleEvent implements IEvent {}
```
Next, an event listener for the `SimpleEvent` is declared:
```java
import dev.kske.eventbus.*;
public class SimpleEventListener implements EventListener {
public SimpleEventListener() {
// Register this listener at the event bus
2020-09-08 09:38:46 +02:00
EventBus.getInstance().register(this);
2020-09-03 10:05:00 +02:00
// Dispatch a SimpleEvent
2020-09-08 09:38:46 +02:00
EventBus.getInstance().dispatch(new SimpleEvent());
2020-09-03 10:05:00 +02:00
}
@Event
private void onSimpleEvent(SimpleEvent event) {
System.out.println("SimpleEvent received!");
}
}
```
2020-09-06 14:57:17 +02:00
In this case, an event bus is created and used locally. In a more sophisticated example the class would acquire an external event bus that is used by multiple classes.
## Installation
Event Bus is currently hosted at [kske.dev ](https://kske.dev ).
To include it inside your project, just add the Maven repository and the dependency to your `pom.xml` :
```xml
< repositories >
< repository >
< id > kske-repo< / id >
< url > https://kske.dev/maven-repo< / url >
< / repository >
< / repositories >
< dependencies >
< dependency >
< groupId > dev.kske< / groupId >
< artifactId > event-bus< / artifactId >
2020-09-08 09:38:46 +02:00
< version > 0.0.2< / version >
2020-09-06 14:57:17 +02:00
< / dependency >
< / dependencies >
```