Commit a5167646 authored by Him188's avatar Him188

Simplify expressions

parent 069a88e8
...@@ -79,6 +79,7 @@ suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull( ...@@ -79,6 +79,7 @@ suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
* @param coroutineContext 额外的 [CoroutineContext] * @param coroutineContext 额外的 [CoroutineContext]
* @param mapper 过滤转换器. 返回非 `null` 则代表得到了需要的值. [syncFromEvent] 会返回这个值 * @param mapper 过滤转换器. 返回非 `null` 则代表得到了需要的值. [syncFromEvent] 会返回这个值
*/ */
@JvmSynthetic
@Suppress("DeferredIsResult") @Suppress("DeferredIsResult")
@SinceMirai("0.38.0") @SinceMirai("0.38.0")
inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull( inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(
...@@ -102,6 +103,7 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull( ...@@ -102,6 +103,7 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(
* @param coroutineContext 额外的 [CoroutineContext] * @param coroutineContext 额外的 [CoroutineContext]
* @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值. [syncFromEvent] 会返回这个值 * @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值. [syncFromEvent] 会返回这个值
*/ */
@JvmSynthetic
@Suppress("DeferredIsResult") @Suppress("DeferredIsResult")
@SinceMirai("0.38.0") @SinceMirai("0.38.0")
inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent( inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
...@@ -126,13 +128,10 @@ internal suspend fun <E : Event, R> syncFromEventOrNullImpl( ...@@ -126,13 +128,10 @@ internal suspend fun <E : Event, R> syncFromEventOrNullImpl(
coroutineScope: CoroutineScope, coroutineScope: CoroutineScope,
mapper: suspend E.(E) -> R? mapper: suspend E.(E) -> R?
): R? = suspendCoroutine { cont -> ): R? = suspendCoroutine { cont ->
var listener: Listener<E>? = null coroutineScope.subscribe(eventClass) {
@Suppress("DuplicatedCode") // for better performance
listener = coroutineScope.subscribe(eventClass) {
cont.resumeWith(kotlin.runCatching { cont.resumeWith(kotlin.runCatching {
mapper.invoke(this, it) ?: return@subscribe ListeningStatus.LISTENING mapper.invoke(this, it) ?: return@subscribe ListeningStatus.LISTENING
}) })
listener!!.complete()
return@subscribe ListeningStatus.STOPPED return@subscribe ListeningStatus.STOPPED
} }
} }
\ No newline at end of file
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