Reference of existing list is kept before dispatching into new threads

This commit is contained in:
Robinson 2023-06-25 17:22:59 +02:00
parent 9a3e49bca4
commit 3a07b6bf86
No known key found for this signature in database
GPG Key ID: 8E7DB78588BD6F5C

View File

@ -390,8 +390,8 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
* NOTE: This is run on the EventDispatch!
*/
fun notifyConnect(connection: CONNECTION, onCompleteFunction: () -> Unit = {}) {
EventDispatcher.CONNECT.launch {
val list = onConnectList.value
EventDispatcher.CONNECT.launch {
list.forEach {
try {
it(connection)
@ -414,19 +414,18 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
* NOTE: This is run on the EventDispatch!
*/
fun notifyDisconnect(connection: Connection) {
EventDispatcher.DISCONNECT.launch {
connection.notifyDisconnect()
@Suppress("UNCHECKED_CAST")
directNotifyDisconnect(connection as CONNECTION)
}
}
/**
* This is invoked by either a GLOBAL listener manager, or for a SPECIFIC CONNECTION listener manager.
*/
suspend fun directNotifyDisconnect(connection: CONNECTION) {
fun directNotifyDisconnect(connection: CONNECTION) {
val list = onDisconnectList.value
EventDispatcher.DISCONNECT.launch {
list.forEach {
try {
it(connection)
@ -437,6 +436,7 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
}
}
}
}
/**
@ -449,8 +449,8 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
fun notifyError(connection: CONNECTION, exception: Throwable) {
logger.error("Error with connection $connection", exception)
EventDispatcher.ERROR.launch {
val list = onErrorList.value
EventDispatcher.ERROR.launch {
list.forEach {
try {
it(connection, exception)
@ -471,8 +471,8 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
fun notifyError(exception: Throwable) {
logger.error("Global error", exception)
EventDispatcher.ERROR.launch {
val list = onErrorGlobalList.value
EventDispatcher.ERROR.launch {
list.forEach {
try {
it(exception)