Added unified, simple event bus to eventually replace controller "notifiers"

This commit is contained in:
catbref 2020-08-05 13:20:19 +01:00
parent ec2c9d2a44
commit ce5cf87094
3 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,5 @@
package org.qortal.event;
public interface Event {
}

View File

@ -0,0 +1,33 @@
package org.qortal.event;
import java.util.ArrayList;
import java.util.List;
public enum EventBus {
INSTANCE;
private static final List<Listener> LISTENERS = new ArrayList<>();
public void addListener(Listener newListener) {
synchronized (LISTENERS) {
LISTENERS.add(newListener);
}
}
public void removeListener(Listener listener) {
synchronized (LISTENERS) {
LISTENERS.remove(listener);
}
}
public void notify(Event event) {
List<Listener> clonedListeners;
synchronized (LISTENERS) {
clonedListeners = new ArrayList<>(LISTENERS);
}
for (Listener listener : clonedListeners)
listener.listen(event);
}
}

View File

@ -0,0 +1,6 @@
package org.qortal.event;
@FunctionalInterface
public interface Listener {
void listen(Event event);
}