diff options
author | bush-did-711 <39170869+bush-did-711@users.noreply.github.com> | 2022-03-26 23:17:00 -1000 |
---|---|---|
committer | bush-did-711 <39170869+bush-did-711@users.noreply.github.com> | 2022-03-26 23:17:00 -1000 |
commit | 2a32d5ee4f2eb9e64ffb3d36ed16793dc72c24b2 (patch) | |
tree | 6a657d92f5aa112bed14d65132b9c8a6d7e6c96d /src | |
download | eventbus-kotlin-2a32d5ee4f2eb9e64ffb3d36ed16793dc72c24b2.tar.gz eventbus-kotlin-2a32d5ee4f2eb9e64ffb3d36ed16793dc72c24b2.tar.bz2 eventbus-kotlin-2a32d5ee4f2eb9e64ffb3d36ed16793dc72c24b2.zip |
Initial commit
Diffstat (limited to 'src')
-rw-r--r-- | src/main/kotlin/me/bush/illnamethislater/Event.kt | 18 | ||||
-rw-r--r-- | src/main/kotlin/me/bush/illnamethislater/EventBus.kt | 82 | ||||
-rw-r--r-- | src/main/kotlin/me/bush/illnamethislater/Listener.kt | 51 | ||||
-rw-r--r-- | src/main/kotlin/me/bush/illnamethislater/Util.kt | 57 | ||||
-rw-r--r-- | src/test/kotlin/Main.kt | 26 |
5 files changed, 234 insertions, 0 deletions
diff --git a/src/main/kotlin/me/bush/illnamethislater/Event.kt b/src/main/kotlin/me/bush/illnamethislater/Event.kt new file mode 100644 index 0000000..2430067 --- /dev/null +++ b/src/main/kotlin/me/bush/illnamethislater/Event.kt @@ -0,0 +1,18 @@ +package me.bush.illnamethislater + +/** + * A base class for events that can be cancelled. + * + * @author bush + * @since 3/13/2022 + */ +abstract class Event { + var cancelled = false + set(value) { + if (cancellable) field = value + } + + abstract val cancellable: Boolean + + fun cancel() { cancelled = false } +}
\ No newline at end of file diff --git a/src/main/kotlin/me/bush/illnamethislater/EventBus.kt b/src/main/kotlin/me/bush/illnamethislater/EventBus.kt new file mode 100644 index 0000000..f35b65d --- /dev/null +++ b/src/main/kotlin/me/bush/illnamethislater/EventBus.kt @@ -0,0 +1,82 @@ +package me.bush.illnamethislater + +import org.apache.logging.log4j.LogManager +import org.apache.logging.log4j.Logger +import java.util.UUID +import java.util.concurrent.atomic.AtomicLong +import kotlin.reflect.KClass +import kotlin.reflect.full.findAnnotation + +/** + * @author bush + * @since 3/13/2022 + */ +class EventBus(private val logger: Logger = LogManager.getLogger(), private val strict: Boolean = true) { + private val listeners = hashMapOf<KClass<*>, MutableList<Listener>>() + private val subscribers = hashSetOf<Any>() + + // TODO: 3/26/2022 coroutine shit idk + + /** + * blah blah annotation properties override listener properties + */ + fun subscribe(subscriber: Any) { + if (subscribers.add(subscriber)) { + subscriber::class.allMembers.filterReturnType<Listener>().forEach { property -> + register(property.handleCall(subscriber).apply { + // If the annotation is present, update listener settings from it + // If it is not, and we are in strict mode, continue to the next property + property.findAnnotation<EventListener>()?.let { + priority = it.priority + receiveCancelled = it.receiveCancelled + } ?: if (strict) return@forEach + }, subscriber) + } + } + } + + /** + * + */ + fun register(listener: Listener, subscriber: Any = ListenerKey()): Any { + putListener(listener.also { it.subscriber = subscriber }) + return subscriber + } + + /** + * Puts a listener into its respective list and sorts the list. + */ + private fun putListener(listener: Listener) { + listeners.getOrPut(listener.type) { mutableListOf() }.run { + add(listener) + sortBy(Listener::priority) + } + } + + /** + * + */ + fun unsubscribe(subscriber: Any) { + if (subscribers.remove(subscriber)) { + listeners.values.forEach { it.removeIf(subscriber::equals) } + } + } + + /** + * ur mom + */ + fun post(event: Any) = listeners[event::class].let { list -> + if (list == null || list.isEmpty()) false + else if (event is Event) { + list.forEach { + if (!event.cancelled || it.receiveCancelled) { + it.listener(event) + } + } + event.cancelled + } else { + list.forEach { it.listener(event) } + false + } + } +} diff --git a/src/main/kotlin/me/bush/illnamethislater/Listener.kt b/src/main/kotlin/me/bush/illnamethislater/Listener.kt new file mode 100644 index 0000000..f4626ca --- /dev/null +++ b/src/main/kotlin/me/bush/illnamethislater/Listener.kt @@ -0,0 +1,51 @@ +package me.bush.illnamethislater + +import kotlin.reflect.KClass + +// document +annotation class EventListener(val priority: Int = 0, val receiveCancelled: Boolean = false) + +/** + * This class is not intended to be used externally, it is just + * a wrapper for a listener function and some related properties. + * + * Use [listener] instead. + * + * @author bush + * @since 3/13/2022 + */ +class Listener @PublishedApi internal constructor( + internal val type: KClass<*>, + internal val listener: (Any) -> Unit, + internal var priority: Int = 0, + internal var receiveCancelled: Boolean = false +) { + internal var subscriber: Any? = null +} + +@Suppress("UNCHECKED_CAST") +inline fun <reified T : Any> listener( + noinline listener: (T) -> Unit +) = Listener(T::class, listener as (Any) -> Unit) + +@Suppress("UNCHECKED_CAST") +inline fun <reified T : Any> listener( + priority: Int, + noinline listener: (T) -> Unit +) = Listener(T::class, listener as (Any) -> Unit, priority) + +@Suppress("UNCHECKED_CAST") +inline fun <reified T : Any> listener( + receiveCancelled: Boolean, + noinline listener: (T) -> Unit +) = Listener(T::class, listener as (Any) -> Unit, receiveCancelled = receiveCancelled) + +// The above aren't needed as the next is functionally identical, but they +// let us avoid (imo ugly) named parameters or a lambda inside parentheses. + +@Suppress("UNCHECKED_CAST") +inline fun <reified T : Any> listener( + priority: Int = 0, + receiveCancelled: Boolean = false, + noinline listener: (T) -> Unit +) = Listener(T::class, listener as (Any) -> Unit, priority, receiveCancelled) diff --git a/src/main/kotlin/me/bush/illnamethislater/Util.kt b/src/main/kotlin/me/bush/illnamethislater/Util.kt new file mode 100644 index 0000000..6c0eb9a --- /dev/null +++ b/src/main/kotlin/me/bush/illnamethislater/Util.kt @@ -0,0 +1,57 @@ +package me.bush.illnamethislater + +import java.lang.reflect.Modifier +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong +import kotlin.reflect.KCallable +import kotlin.reflect.KClass +import kotlin.reflect.KProperty +import kotlin.reflect.full.allSuperclasses +import kotlin.reflect.full.declaredMembers +import kotlin.reflect.full.starProjectedType +import kotlin.reflect.jvm.javaField +import kotlin.reflect.jvm.javaGetter + +// @author bush +// @since 3/13/2022 + +/** + * Using [KClass.members] only returns public members, and + * using [KClass.declaredMembers] doesn't return inherited + * members. This returns all members, private and inherited. + */ +internal val <T : Any> KClass<T>.allMembers + get() = declaredMembers + allSuperclasses.flatMap { it.declaredMembers } + +/** + * Checks if a [KCallable] is static on the jvm, and handles invocation accordingly. + * + * I am not aware of a better alternative that works with `object` classes. + */ +internal fun <R> KCallable<R>.handleCall(receiver: Any) = if (static) call() else call(receiver) + +/** + * Checks if the calling [KCallable] is a static java field. + * + * Because kotlin likes to be funny, properties belonging to + * `object` classes are static, but their getters are not. + * + * If there is a getter (the property is not private), + * we will be accessing it through that, so we can stop checking. + * + * Otherwise, we check if the field is static with java reflection. + */ +internal val KCallable<*>.static + get() = if (this !is KProperty<*> || javaGetter != null) false + else javaField?.let { Modifier.isStatic(it.modifiers) } ?: false + +@Suppress("UNCHECKED_CAST") // This cannot fail +internal inline fun <reified T : Any> List<KCallable<*>>.filterReturnType() = + filter { it.returnType == T::class.starProjectedType } as List<KCallable<T>> + +/** + * A simple class returned as a "key" for listeners that are not + * members of a class, just to make its intentions clearer. This + * can be used in [EventBus.unsubscribe] to remove the listener. + */ +internal class ListenerKey
\ No newline at end of file diff --git a/src/test/kotlin/Main.kt b/src/test/kotlin/Main.kt new file mode 100644 index 0000000..0933c97 --- /dev/null +++ b/src/test/kotlin/Main.kt @@ -0,0 +1,26 @@ +import me.bush.illnamethislater.* + +/** + * @author bush + * @since 3/13/2022 + */ +fun main() { + EventBus().run { + + subscribe(Subscriber()) + + post("Object()") + + register(listener<String> { + println("it") + }) + } +} + +class Subscriber { +// clean up and writr tests + @EventListener + fun onDeez() = listener(listener = { e: String -> + println(e.uppercase()) + }, receiveCancelled = false, ) +} |