Extremely fast event system
Originally made for LavaHack
Event posting is very fast, but subscribing/unsubscribing maybe slow(idk i didnt test)
I recommend using it only if you dont care about subscribing/unsubscribing timing
The main problem with any standard event system is timing of event posting
There are 2 reasons of this problem
The first reason is timing for finding collection of listeners for some posted event
The second reason is timing for iterating and invoking every listener from this collection
Solution to the first problem is binding collection as static thing directly to class of event
Solution to the second problem is combining lambdas of listeners from the collection into one, but it may cause long timing for subscribing/unsubscribing listeners, but we dont care okay?
import lavahack.kevin.EventBus
class TestEvent {
//
companion object {
val BUS = EventBus<TestEvent>()
}
}
import lavahack.kevin.EventBus;
public record TestEvent(/*...*/) {
//
public static final EventBus<TestEvent> BUS = new EventBus<>();
}
import lavahack.kevin.Listener
val listenerWithDefaultPriority = Listener<TestEvent> { event -> /*action or idk*/ }
val listenerWithCustomPriority = Listener<TestEvent>(/*integer that means priority*/) { event -> /*action or idk*/ }
import lavahack.kevin.Listener;
Listener<TestEvent> listenerWithDefaultPriority = new Listener<>(event -> /*action or idk*/);
Listener<TestEvent> listenerWithCustomPriority = new Listener<>(/*integer that means priority*/, event -> /*action or idk*/);
//Subscribing listener
TestEvent.BUS.subscribe(listener)
//Unsubscribing listener
TestEvent.BUS.unsubscribe(listener)
//Posting event
TestEvent.BUS.post(event)
//Subscribing listener
TestEvent.BUS.subscribe(listener);
//Unsubscribing listener
TestEvent.BUS.unsubscribe(listener);
//Posting event
TestEvent.BUS.post(event);