Commit b17c3085 authored by Him188's avatar Him188

Cancel event listener when channel is closed

parent baa3c210
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
package net.mamoe.mirai.event package net.mamoe.mirai.event
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel import kotlinx.coroutines.channels.ReceiveChannel
...@@ -172,9 +173,12 @@ inline fun <reified E : Event> CoroutineScope.incoming( ...@@ -172,9 +173,12 @@ inline fun <reified E : Event> CoroutineScope.incoming(
capacity: Int = Channel.UNLIMITED capacity: Int = Channel.UNLIMITED
): ReceiveChannel<E> { ): ReceiveChannel<E> {
return Channel<E>(capacity).apply { return Channel<E>(capacity).apply {
subscribeAlways<E>(coroutineContext) { val listener = subscribeAlways<E>(coroutineContext) {
send(this) send(this)
} }
this.invokeOnClose {
listener.cancel(CancellationException("ReceiveChannel closed", it))
}
} }
} }
......
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