aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/me/bush/illnamethislater/EventBus.kt
blob: 01d90cd962ae10bee9149a3539e476ad9c4fb19a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package me.bush.illnamethislater

import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import kotlin.reflect.KClass

// TODO: 3/30/2022 Refactor some stuff

/**
 * [A simple event dispatcher](http://github.com/therealbush/eventbus-kotlin)
 *
 * @author bush
 * @since 1.0.0
 */
class EventBus( // todo encapsulation??
    internal val logger: Logger = LogManager.getLogger("EVENTBUS"),
    internal val externalSupport: Boolean = true
) {
    private val listeners = hashMapOf<KClass<*>, ListenerGroup>()
    private val subscribers = mutableSetOf<Any>()

    /**
     * doc
     */
    infix fun subscribe(subscriber: Any): Boolean {
        return if (subscriber in subscribers) false
        else runCatching {
            subscriber::class.listeners.forEach {
                register(it.handleCall(subscriber), subscriber)
            }
            true
        }.getOrElse {
            logger.error("Unable to register listeners for subscriber $subscriber", it)
            false
        }
    }

    /**
     * Registers a listener (which may not belong to any subscriber) to this [EventBus]. If no object
     * is given, a key will be returned which can be used in [unsubscribe] to remove the listener.
     *
     * The
     */
    fun register(listener: Listener, subscriber: Any = Any()): Any {
        listener.subscriber = subscriber
        listeners.computeIfAbsent(listener.type) { ListenerGroup(CancelledState.cancelStateOf(listener.type, this)) }
            .addListener(listener)
        subscribers += subscriber
        return subscriber
    }

    /**
     * doc
     */
    infix fun unsubscribe(subscriber: Any) = subscribers.remove(subscriber).apply {
        if (this) listeners.entries.removeIf {
            it.value.removeFrom(subscriber)
            it.value.isEmpty
        }
    }

    /**
     * Posts an event. doc
     */
    infix fun post(event: Any) = listeners[event::class]?.let { group ->
        // TODO: 3/30/2022 rewrite this all lol priority isn't even set up yet
        group.sequential.forEach {
            if (!group.cancelState.isCancelled(event) || it.receiveCancelled) {
                it.listener(event)
            }
        }
    }

    /**
     * Logs the subscriber count, total listener count, and listener count
     * for every event type with at least one subscriber to [logger].
     * Per-event counts are sorted from greatest to least listeners.
     * ```
     * Subscribers: 5
     * Listeners: 8 sequential, 4 parallel
     * SomeInnerClass: 4, 2
     * OtherEvent: 3, 1
     * String: 1, 1
     */
    // do lol

    fun debugInfo() {
        logger.info(StringBuilder().apply {
            append("\nSubscribers: ${subscribers.size}")
            append("\nListeners: ${listeners.values.sumOf { it.sequential.size }} sequential, ${listeners.values.sumOf { it.parallel.size }} parallel")
            listeners.entries.sortedByDescending { it.value.sequential.size + it.value.parallel.size }.forEach {
                append("${it.key.simpleName}: ${it.value.sequential.size}, ${it.value.parallel.size}")
            }
        }.toString())
    }
}