Commit cd6c8183 authored by Him188's avatar Him188

Reduce suspend fun calls

parent 4587f7ae
......@@ -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
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment