diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt index e9b2ccbf1..c906c75a4 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/event/internal/InternalEventListeners.kt @@ -188,31 +188,6 @@ internal object EventListenerManger { internal suspend fun E.broadcastInternal(): E { if (EventDisabled) return this - suspend fun callListeners(listeners: EventListeners) { - suspend fun callAndRemoveIfRequired() = listeners.inlinedRemoveIf { - if (it.lock.tryLock()) { - try { - it.onEvent(this) == ListeningStatus.STOPPED - } finally { - it.lock.unlock() - } - } else false - } - - //自己持有, 则是在一个事件中 - if (listeners.mainMutex.holdsLock(listeners)) { - callAndRemoveIfRequired() - } else { - while (!listeners.mainMutex.tryLock(listeners)) { - delay(10) - } - try { - callAndRemoveIfRequired() - } finally { - listeners.mainMutex.unlock(listeners) - } - } - } callListeners(this::class.listeners()) @@ -220,6 +195,32 @@ internal suspend fun E.broadcastInternal(): E { return this } +private suspend inline fun E.callListeners(listeners: EventListeners) { + //自己持有, 则是在一个事件中 + if (listeners.mainMutex.holdsLock(listeners)) { + callAndRemoveIfRequired(listeners) + } else { + while (!listeners.mainMutex.tryLock(listeners)) { + delay(10) + } + try { + callAndRemoveIfRequired(listeners) + } finally { + listeners.mainMutex.unlock(listeners) + } + } +} + +private suspend inline fun E.callAndRemoveIfRequired(listeners: EventListeners) = listeners.inlinedRemoveIf { + if (it.lock.tryLock()) { + try { + it.onEvent(this) == ListeningStatus.STOPPED + } finally { + it.lock.unlock() + } + } else false +} + /** * apply [block] to all the [EventListeners] in [clazz]'s superclasses */