From 842549fd448a18ea36e6699551823fc6f862e41a Mon Sep 17 00:00:00 2001
From: Him188 <Him188@mamoe.net>
Date: Wed, 3 Feb 2021 19:40:46 +0800
Subject: [PATCH] Fix the issue EventChannel.context loses `intercepted`, fix
 #956

---
 .../commonMain/kotlin/event/EventChannel.kt   | 20 ++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt b/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt
index 9ae74df3e..d22b209ef 100644
--- a/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt
+++ b/mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt
@@ -145,7 +145,7 @@ public open class EventChannel<out BaseEvent : Event> @JvmOverloads internal con
                     if (filterResult) this@intercepted.invoke(ev)
                     else ListeningStatus.LISTENING
                 }
-                return parent.run { thisIntercepted.intercepted() }
+                return parent.intercept(thisIntercepted)
             }
         }
     }
@@ -222,11 +222,17 @@ public open class EventChannel<out BaseEvent : Event> @JvmOverloads internal con
      *
      * 此操作不会修改 [`this.coroutineContext`][defaultCoroutineContext], 只会创建一个新的 [EventChannel].
      */
-    public fun context(vararg coroutineContexts: CoroutineContext): EventChannel<BaseEvent> =
-        EventChannel(
+    public fun context(vararg coroutineContexts: CoroutineContext): EventChannel<BaseEvent> {
+        val origin = this
+        return object : EventChannel<BaseEvent>(
             baseEventClass,
             coroutineContexts.fold(this.defaultCoroutineContext) { acc, element -> acc + element }
-        )
+        ) {
+            override fun <E : Event> (suspend (E) -> ListeningStatus).intercepted(): suspend (E) -> ListeningStatus {
+                return origin.intercept(this)
+            }
+        }
+    }
 
     /**
      * 创建一个新的 [EventChannel], 该 [EventChannel] 包含 [this.coroutineContext][defaultCoroutineContext] 和添加的 [coroutineExceptionHandler]
@@ -574,7 +580,11 @@ public open class EventChannel<out BaseEvent : Event> @JvmOverloads internal con
         return this
     }
 
-    internal fun <L : Listener<E>, E : Event> subscribeInternal(eventClass: KClass<out E>, listener: L): L {
+    private fun <E: Event> intercept(listener: (suspend (E) -> ListeningStatus)): suspend (E) -> ListeningStatus {
+        return listener.intercepted()
+    }
+
+    private fun <L : Listener<E>, E : Event> subscribeInternal(eventClass: KClass<out E>, listener: L): L {
         with(GlobalEventListeners[listener.priority]) {
             @Suppress("UNCHECKED_CAST")
             val node = ListenerRegistry(listener as Listener<Event>, eventClass)