mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-09 01:30:17 +08:00
Cancel event listener when channel is closed
This commit is contained in:
parent
baa3c2109e
commit
b17c30853c
@ -11,6 +11,7 @@
|
||||
|
||||
package net.mamoe.mirai.event
|
||||
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.channels.ReceiveChannel
|
||||
@ -172,9 +173,12 @@ inline fun <reified E : Event> CoroutineScope.incoming(
|
||||
capacity: Int = Channel.UNLIMITED
|
||||
): ReceiveChannel<E> {
|
||||
return Channel<E>(capacity).apply {
|
||||
subscribeAlways<E>(coroutineContext) {
|
||||
val listener = subscribeAlways<E>(coroutineContext) {
|
||||
send(this)
|
||||
}
|
||||
this.invokeOnClose {
|
||||
listener.cancel(CancellationException("ReceiveChannel closed", it))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user