mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-20 18:44:42 +08:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
db13a867cf
@ -28,6 +28,8 @@ import kotlin.contracts.contract
|
||||
|
||||
/**
|
||||
* 订阅来自所有 [Bot] 的所有联系人的消息事件. 联系人可以是任意群或任意好友或临时会话.
|
||||
*
|
||||
* @see CoroutineScope.incoming
|
||||
*/
|
||||
@UseExperimental(ExperimentalContracts::class)
|
||||
@MessageDsl
|
||||
@ -49,6 +51,8 @@ inline fun <R> CoroutineScope.subscribeMessages(crossinline listeners: MessageSu
|
||||
|
||||
/**
|
||||
* 订阅来自所有 [Bot] 的所有群消息事件
|
||||
*
|
||||
* @see CoroutineScope.incoming
|
||||
*/
|
||||
@UseExperimental(ExperimentalContracts::class)
|
||||
@MessageDsl
|
||||
@ -65,6 +69,8 @@ inline fun <R> CoroutineScope.subscribeGroupMessages(crossinline listeners: Mess
|
||||
|
||||
/**
|
||||
* 订阅来自所有 [Bot] 的所有好友消息事件
|
||||
*
|
||||
* @see CoroutineScope.incoming
|
||||
*/
|
||||
@UseExperimental(ExperimentalContracts::class)
|
||||
@MessageDsl
|
||||
@ -81,6 +87,8 @@ inline fun <R> CoroutineScope.subscribeFriendMessages(crossinline listeners: Mes
|
||||
|
||||
/**
|
||||
* 订阅来自这个 [Bot] 的所有联系人的消息事件. 联系人可以是任意群或任意好友或临时会话.
|
||||
*
|
||||
* @see CoroutineScope.incoming
|
||||
*/
|
||||
@UseExperimental(ExperimentalContracts::class)
|
||||
@MessageDsl
|
||||
@ -97,6 +105,8 @@ inline fun <R> Bot.subscribeMessages(crossinline listeners: MessageSubscribersBu
|
||||
|
||||
/**
|
||||
* 订阅来自这个 [Bot] 的所有群消息事件
|
||||
*
|
||||
* @see CoroutineScope.incoming
|
||||
*/
|
||||
@UseExperimental(ExperimentalContracts::class)
|
||||
@MessageDsl
|
||||
@ -113,6 +123,8 @@ inline fun <R> Bot.subscribeGroupMessages(crossinline listeners: MessageSubscrib
|
||||
|
||||
/**
|
||||
* 订阅来自这个 [Bot] 的所有好友消息事件.
|
||||
*
|
||||
* @see CoroutineScope.incoming
|
||||
*/
|
||||
@UseExperimental(ExperimentalContracts::class)
|
||||
@MessageDsl
|
||||
@ -129,9 +141,15 @@ inline fun <R> Bot.subscribeFriendMessages(crossinline listeners: MessageSubscri
|
||||
|
||||
/**
|
||||
* 返回一个指定事件的接收通道
|
||||
*
|
||||
* @param capacity 同 [Channel] 的参数, 参见 [Channel.Factory] 中的常量.
|
||||
*
|
||||
* @see subscribeFriendMessages
|
||||
* @see subscribeMessages
|
||||
* @see subscribeGroupMessages
|
||||
*/
|
||||
inline fun <reified E : Event> Bot.incoming(): ReceiveChannel<E> {
|
||||
return Channel<E>(8).apply {
|
||||
inline fun <reified E : Event> CoroutineScope.incoming(capacity: Int = Channel.RENDEZVOUS): ReceiveChannel<E> {
|
||||
return Channel<E>(capacity).apply {
|
||||
subscribeAlways<E> {
|
||||
send(this)
|
||||
}
|
||||
|
@ -90,6 +90,15 @@ internal object EventListenerManager {
|
||||
|
||||
private val lock = atomic(false)
|
||||
|
||||
private fun setLockValue(value: Boolean) {
|
||||
lock.value = value
|
||||
}
|
||||
|
||||
@Suppress("BooleanLiteralArgument")
|
||||
private fun trySetLockTrue(): Boolean {
|
||||
return lock.compareAndSet(false, true)
|
||||
}
|
||||
|
||||
@Suppress("UNCHECKED_CAST", "BooleanLiteralArgument")
|
||||
internal tailrec fun <E : Event> get(clazz: KClass<out E>): EventListeners<E> {
|
||||
registries.forEach {
|
||||
@ -97,10 +106,10 @@ internal object EventListenerManager {
|
||||
return it.listeners as EventListeners<E>
|
||||
}
|
||||
}
|
||||
if (lock.compareAndSet(false, true)) {
|
||||
if (trySetLockTrue()) {
|
||||
val registry = Registry(clazz, EventListeners())
|
||||
registries.addLast(registry)
|
||||
lock.value = false
|
||||
setLockValue(false)
|
||||
return registry.listeners as EventListeners<E>
|
||||
}
|
||||
return get(clazz)
|
||||
|
Loading…
Reference in New Issue
Block a user