mirror of
https://github.com/mamoe/mirai.git
synced 2025-04-25 04:50:26 +08:00
Add coroutineContext
params
This commit is contained in:
parent
1c2d8cea36
commit
cfd5946cd1
@ -25,14 +25,15 @@ import kotlin.coroutines.EmptyCoroutineContext
|
||||
*
|
||||
* @see subscribingGetAsync 本函数的异步版本
|
||||
*/
|
||||
@SinceMirai("0.28.0")
|
||||
@SinceMirai("0.29.0")
|
||||
@MiraiExperimentalAPI
|
||||
suspend inline fun <reified E : Event, R : Any> subscribingGet(
|
||||
timeoutMillis: Long = -1,
|
||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||
noinline mapper: suspend E.(E) -> R? // 不要 crossinline: crossinline 后 stacktrace 会不正常
|
||||
): R {
|
||||
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
||||
return subscribingGetOrNull(timeoutMillis, mapper) ?: error("timeout subscribingGet")
|
||||
return subscribingGetOrNull(timeoutMillis, coroutineContext, mapper) ?: error("timeout subscribingGet")
|
||||
}
|
||||
|
||||
/**
|
||||
@ -45,10 +46,11 @@ suspend inline fun <reified E : Event, R : Any> subscribingGet(
|
||||
*
|
||||
* @see subscribingGetAsync 本函数的异步版本
|
||||
*/
|
||||
@SinceMirai("0.28.0")
|
||||
@SinceMirai("0.29.0")
|
||||
@MiraiExperimentalAPI
|
||||
suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
|
||||
timeoutMillis: Long = -1,
|
||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||
noinline mapper: suspend E.(E) -> R? // 不要 crossinline: crossinline 后 stacktrace 会不正常
|
||||
): R? {
|
||||
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
||||
@ -59,7 +61,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
|
||||
@Suppress("DuplicatedCode") // for better performance
|
||||
coroutineScope {
|
||||
var listener: Listener<E>? = null
|
||||
listener = this.subscribe {
|
||||
listener = this.subscribe(coroutineContext) {
|
||||
val value = try {
|
||||
mapper.invoke(this, it)
|
||||
} catch (e: Exception) {
|
||||
@ -77,7 +79,7 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
|
||||
withTimeoutOrNull(timeoutMillis) {
|
||||
var listener: Listener<E>? = null
|
||||
@Suppress("DuplicatedCode") // for better performance
|
||||
listener = this.subscribe {
|
||||
listener = this.subscribe(coroutineContext) {
|
||||
val value = try {
|
||||
mapper.invoke(this, it)
|
||||
} catch (e: Exception) {
|
||||
@ -105,11 +107,12 @@ suspend inline fun <reified E : Event, R : Any> subscribingGetOrNull(
|
||||
* @param coroutineContext 额外的 [CoroutineContext]
|
||||
* @param mapper 过滤转换器. 返回非 null 则代表得到了需要的值. [subscribingGet] 会返回这个值
|
||||
*/
|
||||
@SinceMirai("0.29.0")
|
||||
@MiraiExperimentalAPI
|
||||
inline fun <reified E : Event, R : Any> CoroutineScope.subscribingGetAsync(
|
||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||
timeoutMillis: Long = -1,
|
||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||
noinline mapper: suspend E.(E) -> R? // 不要 crossinline: crossinline 后 stacktrace 会不正常
|
||||
): Deferred<R> = this.async(coroutineContext) {
|
||||
subscribingGet(timeoutMillis, mapper)
|
||||
subscribingGet(timeoutMillis, this.coroutineContext, mapper)
|
||||
}
|
Loading…
Reference in New Issue
Block a user