Parcourir la source

Fix the issue EventChannel.context loses `intercepted`, fix #956

(cherry picked from commit 842549fd448a18ea36e6699551823fc6f862e41a)
Him188 il y a 5 ans
Parent
commit
a70d928ff1
1 fichiers modifiés avec 15 ajouts et 5 suppressions
  1. 15 5
      mirai-core-api/src/commonMain/kotlin/event/EventChannel.kt

+ 15 - 5
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) [email protected](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)