Dispatch events in Bot scope

This commit is contained in:
Him188 2021-04-29 12:19:15 +08:00
parent b62e39b4d1
commit d14ed0cecd
2 changed files with 14 additions and 7 deletions

View File

@ -139,7 +139,7 @@ internal open class QQAndroidBot constructor(
set(
PacketHandler, PacketHandlerChain(
LoggingPacketHandlerAdapter(get(PacketLoggingStrategy), networkLogger),
EventBroadcasterPacketHandler(networkLogger),
EventBroadcasterPacketHandler(bot, networkLogger),
CallPacketFactoryPacketHandler(bot)
)
)

View File

@ -9,6 +9,9 @@
package net.mamoe.mirai.internal.network.components
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import net.mamoe.mirai.event.BroadcastControllable
import net.mamoe.mirai.event.CancellableEvent
import net.mamoe.mirai.event.Event
@ -61,6 +64,7 @@ internal class LoggingPacketHandlerAdapter(
}
internal class EventBroadcasterPacketHandler(
private val targetScope: CoroutineScope,
private val logger: MiraiLogger,
) : PacketHandler {
@ -69,6 +73,7 @@ internal class EventBroadcasterPacketHandler(
impl(data)
}
private val coroutineName = CoroutineName("Mirai-EventDispatcher-${logger.identity}")
private suspend fun impl(packet: Packet) {
if (packet is MultiPacket<*>) {
for (p in packet) {
@ -79,12 +84,14 @@ internal class EventBroadcasterPacketHandler(
packet is CancellableEvent && packet.isCancelled -> return
packet is BroadcastControllable && !packet.shouldBroadcast -> return
packet is Event -> {
try {
packet.broadcast()
} catch (e: Throwable) {
if (logger.isEnabled) {
val msg = optimizeEventToString(packet)
logger.error(IllegalStateException("Exception while broadcasting event '$msg'", e))
targetScope.launch(coroutineName) {
try {
packet.broadcast()
} catch (e: Throwable) {
if (logger.isEnabled) {
val msg = optimizeEventToString(packet)
logger.error(IllegalStateException("Exception while broadcasting event '$msg'", e))
}
}
}
}