Implement event priority for syncFromEvent and nextEvent

This commit is contained in:
Him188 2020-05-06 12:53:24 +08:00
parent 15e6bb4b8b
commit 47df80d3ea
2 changed files with 23 additions and 14 deletions

View File

@ -33,17 +33,18 @@ import kotlin.reflect.KClass
@JvmSynthetic
suspend inline fun <reified E : Event, R : Any> syncFromEvent(
timeoutMillis: Long = -1,
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
crossinline mapper: suspend E.(E) -> R?
): R {
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
return if (timeoutMillis == -1L) {
coroutineScope {
syncFromEventImpl<E, R>(E::class, this, mapper)
syncFromEventImpl<E, R>(E::class, this, priority, mapper)
}
} else {
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
suspend inline fun <reified E : Event, R : Any> syncFromEventOrNull(
timeoutMillis: Long,
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
crossinline mapper: suspend E.(E) -> R?
): R? {
require(timeoutMillis > 0) { "timeoutMillis must be > 0" }
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(
timeoutMillis: Long,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
crossinline mapper: suspend E.(E) -> R?
): Deferred<R?> {
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
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(
timeoutMillis: Long = -1,
coroutineContext: CoroutineContext = EmptyCoroutineContext,
priority: Listener.EventPriority = Listener.EventPriority.MONITOR,
crossinline mapper: suspend E.(E) -> R?
): Deferred<R> {
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
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(
eventClass: KClass<E>,
coroutineScope: CoroutineScope,
priority: Listener.EventPriority,
crossinline mapper: suspend E.(E) -> R?
): R = suspendCancellableCoroutine { cont ->
coroutineScope.subscribe(eventClass) {
coroutineScope.subscribe(eventClass, priority = priority) {
try {
cont.resumeWith(kotlin.runCatching {
mapper.invoke(this, it) ?: return@subscribe ListeningStatus.LISTENING

View File

@ -29,11 +29,12 @@ import kotlin.reflect.KClass
*/
@JvmSynthetic
suspend inline fun <reified E : Event> nextEvent(
timeoutMillis: Long = -1
timeoutMillis: Long = -1,
priority: Listener.EventPriority = Listener.EventPriority.MONITOR
): E {
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
return withTimeoutOrCoroutineScope(timeoutMillis) {
nextEventImpl(E::class, this)
nextEventImpl(E::class, this, priority)
}
}
@ -50,11 +51,12 @@ suspend inline fun <reified E : Event> nextEvent(
*/
@JvmSynthetic
suspend inline fun <reified E : BotEvent> Bot.nextEvent(
timeoutMillis: Long = -1
timeoutMillis: Long = -1,
priority: Listener.EventPriority = Listener.EventPriority.MONITOR
): E {
require(timeoutMillis == -1L || timeoutMillis > 0) { "timeoutMillis must be -1 or > 0" }
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
internal suspend inline fun <E : Event> nextEventImpl(
eventClass: KClass<E>,
coroutineScope: CoroutineScope
coroutineScope: CoroutineScope,
priority: Listener.EventPriority
): E = suspendCancellableCoroutine { cont ->
coroutineScope.subscribe(eventClass) {
coroutineScope.subscribe(eventClass, priority = priority) {
try {
cont.resume(this)
} catch (e: Exception) {
@ -79,9 +82,10 @@ internal suspend inline fun <E : Event> nextEventImpl(
internal suspend inline fun <E : BotEvent> nextBotEventImpl(
bot: Bot,
eventClass: KClass<E>,
coroutineScope: CoroutineScope
coroutineScope: CoroutineScope,
priority: Listener.EventPriority
): E = suspendCancellableCoroutine { cont ->
coroutineScope.subscribe(eventClass) {
coroutineScope.subscribe(eventClass, priority = priority) {
try {
if (this.bot == bot) cont.resume(this)
} catch (e: Exception) {