mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-13 03:16:05 +08:00
Reduce suspend fun calls
This commit is contained in:
parent
4587f7aeff
commit
cd6c8183f2
@ -188,31 +188,6 @@ internal object EventListenerManger {
|
||||
|
||||
internal suspend fun <E : Subscribable> E.broadcastInternal(): E {
|
||||
if (EventDisabled) return this
|
||||
suspend fun callListeners(listeners: EventListeners<in E>) {
|
||||
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 : Subscribable> E.broadcastInternal(): E {
|
||||
return this
|
||||
}
|
||||
|
||||
private suspend inline fun <E : Subscribable> E.callListeners(listeners: EventListeners<in E>) {
|
||||
//自己持有, 则是在一个事件中
|
||||
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 : Subscribable> E.callAndRemoveIfRequired(listeners: EventListeners<in E>) = 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
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user