GlobalEventChannel

object GlobalEventChannel : EventChannel<Event>

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

See also

Functions

asChannel
Link copied to clipboard
fun asChannel(capacity: Int = Channel.RENDEZVOUS, coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.NORMAL): Channel<out Event>

创建事件监听并将监听结果发送在 Channel. 将返回值 关闭Channel.close 时将会同时关闭事件监听.

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

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

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

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

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

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

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

EventChannel.filter 的 Java 版本.

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

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

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

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

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

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

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

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

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

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

registerListenerHost
Link copied to clipboard
fun registerListenerHost(host: ListenerHost, coroutineContext: CoroutineContext = EmptyCoroutineContext)

注册 ListenerHost 中的所有 EventHandler 标注的方法到这个 EventChannel. 查看 EventHandler.

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

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

fun <E : Event> subscribe(eventClass: <ERROR CLASS><out E>, coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.NORMAL, handler: <ERROR CLASS><E, ListeningStatus>): Listener<E>

Java API. 查看 subscribe 获取更多信息.

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 泛型. 通常推荐使用具体化类型参数.

subscribeAlways
Link copied to clipboard
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 都会被执行.

fun <E : Event> subscribeAlways(eventClass: <ERROR CLASS><out E>, coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.NORMAL, handler: <ERROR CLASS><E>): Listener<E>

Java API. 查看 subscribeAlways 获取更多信息.

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>
subscribeOnce
Link copied to clipboard
inline fun <E : Event> subscribeOnce(coroutineContext: CoroutineContext = EmptyCoroutineContext, priority: EventPriority = EventPriority.NORMAL, noinline handler: suspend E.(E) -> Unit): Listener<E>

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

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

fun <E : Event> subscribeOnce(eventClass: <ERROR CLASS><out E>, coroutineContext: CoroutineContext = EmptyCoroutineContext, concurrency: ConcurrencyKind = CONCURRENT, priority: EventPriority = EventPriority.NORMAL, handler: <ERROR CLASS><E>): Listener<E>

Java API. 查看 subscribeOnce 获取更多信息.

Properties

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

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