Skip to content

popkiolok/EventSystem

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EventSystem

Dynamic event system with multiple instances support.

Features

  • Create multiple EventSystems.
  • Control listeners in EventContainer.
  • Call millions of listeners per second.
  • Create self-destroying tasks.
  • Create listeners in multiple ways.

Adding event

class MyEvent(val hello: String) : Event()
class SomeCancellableEvent : Cancellable()

Calling event

val eventSystem = EventSystem {}
// Events registered automatically on the first call
eventSystem.call(MyEvent())

val event = SomeCancellableEvent()
eventSystem.call(event)
if (event.cancelled) {
    // ...
}

Listening for event

val container = EventContainer("MyContainer", eventSystem)
container.attach(Listener(MyEvent::class) { event -> println("MyEvent called with ${event.hello}.") })
class MyClassWithEvent {
    @EventListener
    fun printHello(event: MyEvent) {
        println("MyEvent called with ${event.hello}.")
    }
}

class Main {
    fun main() {
        container.attachAll(getEventListeners(MyClassWithEvent::class), MyClassWithEvent())
    }
}

Destroying listeners

container.detachAll()
class SomeClassWithEvent {
    @EventListener
    fun printHello(event: MyEvent, self: EventExecutor<MyEvent>) {
        println("MyEvent called with ${event.hello}.")
        if (/* condition */) {
            self.detach()
        }
    }
}

Benchmark

Benchmark                                                           Mode  Cnt         Score        Error  Units
eventSystemCreateBenchmark                                         thrpt    5  33749765.427 ± 113034.559  ops/s
eventSystemFirstCallBenchmark                                      thrpt    5  10583192.825 ±  41961.393  ops/s
eventSystemThatAlreadyContains5ListenersRegisterListenerBenchmark  thrpt    5  20201957.656 ± 229894.558  ops/s
eventSystemWith30EventsAnd3or4ListenersForEachEventCallBenchmark   thrpt    5   9992935.003 ± 146237.852  ops/s

Gradle

repositories {
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.InfazeMC:EventSystem:2.0.0'
}

About

Dynamic scalable Event System.

Resources

Stars

Watchers

Forks

Packages

No packages published