Skip to content

Commit

Permalink
Add preview to all LiveEntity code
Browse files Browse the repository at this point in the history
  • Loading branch information
BartArys committed Dec 30, 2019
1 parent 52f995c commit 0674cda
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 4 deletions.
19 changes: 17 additions & 2 deletions core/src/main/kotlin/com/gitlab/kordlib/core/live/LiveEntity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gitlab.kordlib.core.live

import com.gitlab.kordlib.common.annotation.KordPreview
import com.gitlab.kordlib.core.Kord
import com.gitlab.kordlib.core.behavior.MessageBehavior
import com.gitlab.kordlib.core.cache.data.ReactionData
import com.gitlab.kordlib.core.entity.Entity
Expand All @@ -11,13 +12,22 @@ import com.gitlab.kordlib.core.event.channel.ChannelDeleteEvent
import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent
import com.gitlab.kordlib.core.event.message.*
import com.gitlab.kordlib.core.kordLogger
import com.gitlab.kordlib.core.on
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.update
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock

/**
* A Discord entity that only emits events *related* to this entity.
*
* For example, a [LiveMessage] will only emit [MessageUpdateEvents][MessageUpdateEvent] of that message, and only emit
* [reactions][ReactionAddEvent] to that message.
*/
@KordPreview
interface LiveEntity : Entity {
val events: Flow<Event>

Expand All @@ -41,9 +51,14 @@ abstract class AbstractLiveEntity : LiveEntity {

}

inline fun <reified T : Event> LiveEntity.on(noinline consumer: suspend (T) -> Unit) =
/**
* Convenience method that will invoke the [consumer] on every event [T], the consumer is launched in the given [scope]
* or [Kord] by default and will not propagate any exceptions.
*/
@KordPreview
inline fun <reified T : Event> LiveEntity.on(scope: CoroutineScope = kord, noinline consumer: suspend (T) -> Unit) =
events.buffer(Channel.UNLIMITED).filterIsInstance<T>().onEach {
runCatching { consumer(it) }.onFailure { kordLogger.catching(it) }
}.catch { kordLogger.catching(it) }.launchIn(kord)
}.catch { kordLogger.catching(it) }.launchIn(scope)


Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.CategoryDeleteEvent
import com.gitlab.kordlib.core.event.channel.CategoryUpdateEvent
import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent

@KordPreview
fun Category.live() = LiveCategory(this)

@KordPreview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import com.gitlab.kordlib.core.event.guild.GuildUpdateEvent
import com.gitlab.kordlib.core.event.message.*
import com.gitlab.kordlib.core.live.AbstractLiveEntity

fun Channel.live() = when(this) {
@KordPreview
fun Channel.live() = when (this) {
is DmChannel -> this.live()
is NewsChannel -> this.live()
is StoreChannel -> this.live()
Expand All @@ -27,7 +28,7 @@ abstract class LiveChannel : AbstractLiveEntity() {

abstract val channel: Channel

override fun filter(event: Event): Boolean = when(event) {
override fun filter(event: Event): Boolean = when (event) {
is VoiceStateUpdateEvent -> event.state.channelId == channel.id

is ReactionAddEvent -> event.channelId == channel.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.DMChannelDeleteEvent
import com.gitlab.kordlib.core.event.channel.DMChannelUpdateEvent
import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent

@KordPreview
fun DmChannel.live() = LiveDmChannel(this)

@KordPreview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.NewsChannelDeleteEvent
import com.gitlab.kordlib.core.event.channel.NewsChannelUpdateEvent
import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent

@KordPreview
fun NewsChannel.live() = LiveNewsChannel(this)

@KordPreview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.gitlab.kordlib.core.event.channel.StoreChannelDeleteEvent
import com.gitlab.kordlib.core.event.channel.StoreChannelUpdateEvent
import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent

@KordPreview
fun StoreChannel.live() = LiveStoreChannel(this)

@KordPreview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.gitlab.kordlib.core.event.channel.TextChannelDeleteEvent
import com.gitlab.kordlib.core.event.channel.TextChannelUpdateEvent
import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent

@KordPreview
fun TextChannel.live() = LiveTextChannel(this)

@KordPreview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.gitlab.kordlib.core.event.channel.VoiceChannelDeleteEvent
import com.gitlab.kordlib.core.event.channel.VoiceChannelUpdateEvent
import com.gitlab.kordlib.core.event.guild.GuildDeleteEvent

@KordPreview
fun VoiceChannel.live() = LiveVoiceChannel(this)

@KordPreview
Expand Down

0 comments on commit 0674cda

Please sign in to comment.