GlobalEventChannel

object GlobalEventChannel : EventChannel<Event>

全局事件通道. 此通道包含来自所有 Bot 的所有类型的事件. 可通过 EventChannel.filter 过滤得到范围更小的 EventChannel.

See also

Functions

Link copied to clipboard
open override fun asFlow(): Flow<Event>

通过 Flow 接收此通道内的所有事件.

Link copied to clipboard
open override fun context(vararg coroutineContexts: CoroutineContext): EventChannel<Event>

创建一个新的 EventChannel, 该 EventChannel 包含 this.coroutineContext 和添加的 coroutineContexts. coroutineContexts 会覆盖 defaultCoroutineContext 中的重复元素.

Link copied to clipboard
expect fun exceptionHandler(coroutineExceptionHandler: (exception: Throwable) -> Unit): EventChannel<Event>

创建一个新的 EventChannel, 该 EventChannel 包含 this.coroutineContext 和添加的 coroutineExceptionHandler

expect fun exceptionHandler(coroutineExceptionHandler: CoroutineExceptionHandler): EventChannel<Event>

创建一个新的 EventChannel, 该 EventChannel 包含 this.coroutineContext 和添加的 coroutineExceptionHandler

Link copied to clipboard
expect fun filter(filter: (event: Event) -> Boolean): EventChannel<Event>

EventChannel.filter 的 Java 版本.

expect fun filter(filter: suspend (event: Event) -> Boolean): EventChannel<Event>

添加一个过滤器. 过滤器将在收到任何事件之后, 传递给通过 EventChannel.subscribe 注册的监听器之前调用.

Link copied to clipboard
expect inline fun <E : Event> filterIsInstance(): EventChannel<E>
expect fun <E : Event> filterIsInstance(kClass: KClass<out E>): EventChannel<E>

过滤事件的类型. 返回一个只包含 E 类型事件的 EventChannel

Link copied to clipboard
expect fun forwardToChannel(    channel: SendChannel<Event>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     priority: EventPriority = EventPriority.MONITOR): Listener<Event>

创建事件监听并将监听结果转发到 channel. 当 Channel.send 抛出 ClosedSendChannelException 时停止 Listener 监听和转发.

Link copied to clipboard
expect fun parentJob(job: Job): EventChannel<Event>

指定协程父 Job. 之后在此 EventChannel 下创建的事件监听器都会成为 job 的子任务, 当 job 被取消时, 所有的事件监听器都会被取消.

Link copied to clipboard
expect fun parentScope(coroutineScope: CoroutineScope): EventChannel<Event>

coroutineScope 作为这个 EventChannel 的父作用域.

Link copied to clipboard
expect inline fun <E : Event> subscribe(    coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = LOCKED,     priority: EventPriority = EventPriority.NORMAL,     noinline handler: suspend E.(E) -> ListeningStatus): Listener<E>

创建一个事件监听器, 监听事件通道中所有 E 及其子类事件.

expect fun <E : Event> subscribe(    eventClass: KClass<out E>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = LOCKED,     priority: EventPriority = EventPriority.NORMAL,     handler: suspend E.(E) -> ListeningStatus): Listener<E>

subscribe 的区别是接受 eventClass 参数, 而不使用 reified 泛型. 通常推荐使用具体化类型参数.

Link copied to clipboard
expect inline fun <E : Event> subscribeAlways(    coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.NORMAL,     noinline handler: suspend E.(E) -> Unit): Listener<E>

创建一个事件监听器, 监听事件通道中所有 E 及其子类事件. 每当 事件广播 时, handler 都会被执行.

expect fun <E : Event> subscribeAlways(    eventClass: KClass<out E>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     concurrency: ConcurrencyKind = CONCURRENT,     priority: EventPriority = EventPriority.NORMAL,     handler: suspend E.(E) -> Unit): Listener<E>
Link copied to clipboard
expect inline fun <E : Event> subscribeOnce(    coroutineContext: CoroutineContext = EmptyCoroutineContext,     priority: EventPriority = EventPriority.NORMAL,     noinline handler: suspend E.(E) -> Unit): Listener<E>

创建一个事件监听器, 监听事件通道中所有 E 及其子类事件, 只监听一次. 当 事件广播 时, handler 会被执行.

expect fun <E : Event> subscribeOnce(    eventClass: KClass<out E>,     coroutineContext: CoroutineContext = EmptyCoroutineContext,     priority: EventPriority = EventPriority.NORMAL,     handler: suspend E.(E) -> Unit): Listener<E>

Properties

Link copied to clipboard
expect val baseEventClass: KClass<out Event>
Link copied to clipboard
expect val defaultCoroutineContext: CoroutineContext

此事件通道的默认 CoroutineScope.coroutineContext. 将会被添加给所有注册的事件监听器.