Skip to content

Commit

Permalink
remove async tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
MukjepScarlet committed Feb 24, 2025
1 parent 974179c commit 991d953
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/main/java/net/ccbluex/liquidbounce/event/EventManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import net.ccbluex.liquidbounce.event.async.LoopManager
import net.ccbluex.liquidbounce.event.async.TickScheduler
import net.ccbluex.liquidbounce.event.async.waitTicks
import net.ccbluex.liquidbounce.utils.client.ClientUtils
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArrayList
import kotlin.coroutines.cancellation.CancellationException

Expand Down Expand Up @@ -46,13 +48,41 @@ object EventManager : CoroutineScope by CoroutineScope(SupervisorJob()) {
CopyOnWriteArrayList<EventHook<in Event>>()
}

private class AsyncTask(val owner: EventHook<*>, val job: Job)

private val jobs = CopyOnWriteArrayList<AsyncTask>()

init {
LoopManager
TickScheduler

LoopManager.loopHandler {
jobs.removeIf { !it.owner.isActive || !it.job.isActive }
waitTicks(1)
}
}

fun Listenable.cancelAsyncJobs() {
jobs.removeIf {
if (it.owner.owner === this) {
it.job.cancel()
true
} else {
false
}
}
}

fun <T : Event> unregisterEventHook(eventClass: Class<out T>, eventHook: EventHook<in T>) {
registry[eventClass]!!.remove(eventHook)
jobs.removeIf {
if (it.owner === eventHook) {
it.job.cancel()
true
} else {
false
}
}
}

fun <T : Event> registerEventHook(eventClass: Class<out T>, eventHook: EventHook<T>): EventHook<T> {
Expand Down Expand Up @@ -101,13 +131,14 @@ object EventManager : CoroutineScope by CoroutineScope(SupervisorJob()) {
}

is EventHook.Async -> {
launch(dispatcher) {
val job = launch(dispatcher) {
try {
action(this, event)
} catch (e: Exception) {
ClientUtils.LOGGER.error("Exception during call event (async)", e)
}
}
jobs += AsyncTask(this, job)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package net.ccbluex.liquidbounce.features.module

import net.ccbluex.liquidbounce.LiquidBounce.isStarting
import net.ccbluex.liquidbounce.config.Configurable
import net.ccbluex.liquidbounce.event.EventManager.cancelAsyncJobs
import net.ccbluex.liquidbounce.event.Listenable
import net.ccbluex.liquidbounce.features.module.modules.misc.GameDetector
import net.ccbluex.liquidbounce.file.FileManager.modulesConfig
Expand Down Expand Up @@ -133,6 +134,7 @@ open class Module(

if (canBeEnabled) field = true
} else {
cancelAsyncJobs()
onDisable()
field = false
}
Expand Down

0 comments on commit 991d953

Please sign in to comment.