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! * NOTE: This is run on the EventDispatch!
*/ */
fun notifyConnect(connection: CONNECTION, onCompleteFunction: () -> Unit = {}) { fun notifyConnect(connection: CONNECTION, onCompleteFunction: () -> Unit = {}) {
val list = onConnectList.value
EventDispatcher.CONNECT.launch { EventDispatcher.CONNECT.launch {
val list = onConnectList.value
list.forEach { list.forEach {
try { try {
it(connection) it(connection)
@ -414,26 +414,26 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
* NOTE: This is run on the EventDispatch! * NOTE: This is run on the EventDispatch!
*/ */
fun notifyDisconnect(connection: Connection) { fun notifyDisconnect(connection: Connection) {
EventDispatcher.DISCONNECT.launch { connection.notifyDisconnect()
connection.notifyDisconnect()
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
directNotifyDisconnect(connection as CONNECTION) directNotifyDisconnect(connection as CONNECTION)
}
} }
/** /**
* This is invoked by either a GLOBAL listener manager, or for a SPECIFIC CONNECTION listener manager. * 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 val list = onDisconnectList.value
list.forEach { EventDispatcher.DISCONNECT.launch {
try { list.forEach {
it(connection) try {
} catch (t: Throwable) { it(connection)
// NOTE: when we remove stuff, we ONLY want to remove the "tail" of the stacktrace, not ALL parts of the stacktrace } catch (t: Throwable) {
t.cleanStackTrace() // NOTE: when we remove stuff, we ONLY want to remove the "tail" of the stacktrace, not ALL parts of the stacktrace
logger.error("Connection ${connection.id} error", t) t.cleanStackTrace()
logger.error("Connection ${connection.id} error", t)
}
} }
} }
} }
@ -449,8 +449,8 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
fun notifyError(connection: CONNECTION, exception: Throwable) { fun notifyError(connection: CONNECTION, exception: Throwable) {
logger.error("Error with connection $connection", exception) logger.error("Error with connection $connection", exception)
val list = onErrorList.value
EventDispatcher.ERROR.launch { EventDispatcher.ERROR.launch {
val list = onErrorList.value
list.forEach { list.forEach {
try { try {
it(connection, exception) it(connection, exception)
@ -471,8 +471,8 @@ internal class ListenerManager<CONNECTION: Connection>(private val logger: KLogg
fun notifyError(exception: Throwable) { fun notifyError(exception: Throwable) {
logger.error("Global error", exception) logger.error("Global error", exception)
val list = onErrorGlobalList.value
EventDispatcher.ERROR.launch { EventDispatcher.ERROR.launch {
val list = onErrorGlobalList.value
list.forEach { list.forEach {
try { try {
it(exception) it(exception)