mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-08 09:10:11 +08:00
Implement event priority for syncFromEvent
and nextEvent
This commit is contained in:
parent
15e6bb4b8b
commit
47df80d3ea
@ -33,17 +33,18 @@ import kotlin.reflect.KClass
|
|||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend inline fun <reified E : Event, R : Any> syncFromEvent(
|
suspend inline fun <reified E : Event, R : Any> syncFromEvent(
|
||||||
timeoutMillis: Long = -1,
|
timeoutMillis: Long = -1,
|
||||||
|
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
|
||||||
crossinline mapper: suspend E.(E) -> R?
|
crossinline mapper: suspend E.(E) -> R?
|
||||||
): R {
|
): R {
|
||||||
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
||||||
|
|
||||||
return if (timeoutMillis == -1L) {
|
return if (timeoutMillis == -1L) {
|
||||||
coroutineScope {
|
coroutineScope {
|
||||||
syncFromEventImpl<E, R>(E::class, this, mapper)
|
syncFromEventImpl<E, R>(E::class, this, priority, mapper)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
withTimeout(timeoutMillis) {
|
withTimeout(timeoutMillis) {
|
||||||
syncFromEventImpl<E, R>(E::class, this, mapper)
|
syncFromEventImpl<E, R>(E::class, this, priority, mapper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -65,12 +66,13 @@ suspend inline fun <reified E : Event, R : Any> syncFromEvent(
|
|||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
|
suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
|
||||||
timeoutMillis: Long,
|
timeoutMillis: Long,
|
||||||
|
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
|
||||||
crossinline mapper: suspend E.(E) -> R?
|
crossinline mapper: suspend E.(E) -> R?
|
||||||
): R? {
|
): R? {
|
||||||
require(timeoutMillis > 0) { "timeoutMillis must be > 0" }
|
require(timeoutMillis > 0) { "timeoutMillis must be > 0" }
|
||||||
|
|
||||||
return withTimeoutOrNull(timeoutMillis) {
|
return withTimeoutOrNull(timeoutMillis) {
|
||||||
syncFromEventImpl<E, R>(E::class, this, mapper)
|
syncFromEventImpl<E, R>(E::class, this, priority, mapper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,11 +95,12 @@ suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
|
|||||||
inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(
|
inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(
|
||||||
timeoutMillis: Long,
|
timeoutMillis: Long,
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||||
|
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
|
||||||
crossinline mapper: suspend E.(E) -> R?
|
crossinline mapper: suspend E.(E) -> R?
|
||||||
): Deferred<R?> {
|
): Deferred<R?> {
|
||||||
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
||||||
return this.async(coroutineContext) {
|
return this.async(coroutineContext) {
|
||||||
syncFromEventOrNull(timeoutMillis, mapper)
|
syncFromEventOrNull(timeoutMillis, priority, mapper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,11 +124,12 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEventOrNull(
|
|||||||
inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
|
inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
|
||||||
timeoutMillis: Long = -1,
|
timeoutMillis: Long = -1,
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
coroutineContext: CoroutineContext = EmptyCoroutineContext,
|
||||||
|
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
|
||||||
crossinline mapper: suspend E.(E) -> R?
|
crossinline mapper: suspend E.(E) -> R?
|
||||||
): Deferred<R> {
|
): Deferred<R> {
|
||||||
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
||||||
return this.async(coroutineContext) {
|
return this.async(coroutineContext) {
|
||||||
syncFromEvent(timeoutMillis, mapper)
|
syncFromEvent(timeoutMillis, priority, mapper)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -139,9 +143,10 @@ inline fun <reified E : Event, R : Any> CoroutineScope.asyncFromEvent(
|
|||||||
internal suspend inline fun <E : Event, R> syncFromEventImpl(
|
internal suspend inline fun <E : Event, R> syncFromEventImpl(
|
||||||
eventClass: KClass<E>,
|
eventClass: KClass<E>,
|
||||||
coroutineScope: CoroutineScope,
|
coroutineScope: CoroutineScope,
|
||||||
|
priority: Listener.EventPriority,
|
||||||
crossinline mapper: suspend E.(E) -> R?
|
crossinline mapper: suspend E.(E) -> R?
|
||||||
): R = suspendCancellableCoroutine { cont ->
|
): R = suspendCancellableCoroutine { cont ->
|
||||||
coroutineScope.subscribe(eventClass) {
|
coroutineScope.subscribe(eventClass, priority = priority) {
|
||||||
try {
|
try {
|
||||||
cont.resumeWith(kotlin.runCatching {
|
cont.resumeWith(kotlin.runCatching {
|
||||||
mapper.invoke(this, it) ?: return@subscribe ListeningStatus.LISTENING
|
mapper.invoke(this, it) ?: return@subscribe ListeningStatus.LISTENING
|
||||||
|
@ -29,11 +29,12 @@ import kotlin.reflect.KClass
|
|||||||
*/
|
*/
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend inline fun <reified E : Event> nextEvent(
|
suspend inline fun <reified E : Event> nextEvent(
|
||||||
timeoutMillis: Long = -1
|
timeoutMillis: Long = -1,
|
||||||
|
priority: Listener.EventPriority = Listener.EventPriority.MONITOR
|
||||||
): E {
|
): E {
|
||||||
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
||||||
return withTimeoutOrCoroutineScope(timeoutMillis) {
|
return withTimeoutOrCoroutineScope(timeoutMillis) {
|
||||||
nextEventImpl(E::class, this)
|
nextEventImpl(E::class, this, priority)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,11 +51,12 @@ suspend inline fun <reified E : Event> nextEvent(
|
|||||||
*/
|
*/
|
||||||
@JvmSynthetic
|
@JvmSynthetic
|
||||||
suspend inline fun <reified E : BotEvent> Bot.nextEvent(
|
suspend inline fun <reified E : BotEvent> Bot.nextEvent(
|
||||||
timeoutMillis: Long = -1
|
timeoutMillis: Long = -1,
|
||||||
|
priority: Listener.EventPriority = Listener.EventPriority.MONITOR
|
||||||
): E {
|
): E {
|
||||||
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
|
||||||
return withTimeoutOrCoroutineScope(timeoutMillis) {
|
return withTimeoutOrCoroutineScope(timeoutMillis) {
|
||||||
nextBotEventImpl(this@nextEvent, E::class, this)
|
nextBotEventImpl(this@nextEvent, E::class, this, priority)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,9 +65,10 @@ suspend inline fun <reified E : BotEvent> Bot.nextEvent(
|
|||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal suspend inline fun <E : Event> nextEventImpl(
|
internal suspend inline fun <E : Event> nextEventImpl(
|
||||||
eventClass: KClass<E>,
|
eventClass: KClass<E>,
|
||||||
coroutineScope: CoroutineScope
|
coroutineScope: CoroutineScope,
|
||||||
|
priority: Listener.EventPriority
|
||||||
): E = suspendCancellableCoroutine { cont ->
|
): E = suspendCancellableCoroutine { cont ->
|
||||||
coroutineScope.subscribe(eventClass) {
|
coroutineScope.subscribe(eventClass, priority = priority) {
|
||||||
try {
|
try {
|
||||||
cont.resume(this)
|
cont.resume(this)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -79,9 +82,10 @@ internal suspend inline fun <E : Event> nextEventImpl(
|
|||||||
internal suspend inline fun <E : BotEvent> nextBotEventImpl(
|
internal suspend inline fun <E : BotEvent> nextBotEventImpl(
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
eventClass: KClass<E>,
|
eventClass: KClass<E>,
|
||||||
coroutineScope: CoroutineScope
|
coroutineScope: CoroutineScope,
|
||||||
|
priority: Listener.EventPriority
|
||||||
): E = suspendCancellableCoroutine { cont ->
|
): E = suspendCancellableCoroutine { cont ->
|
||||||
coroutineScope.subscribe(eventClass) {
|
coroutineScope.subscribe(eventClass, priority = priority) {
|
||||||
try {
|
try {
|
||||||
if (this.bot == bot) cont.resume(this)
|
if (this.bot == bot) cont.resume(this)
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
Loading…
Reference in New Issue
Block a user