Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add possibility to define Job parent to create live entities #304

Merged
merged 7 commits into from
Jun 13, 2021
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/LiveGuild.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@ import dev.kord.core.event.role.RoleUpdateEvent
import dev.kord.core.event.user.PresenceUpdateEvent
import dev.kord.core.event.user.VoiceStateUpdateEvent
import dev.kord.core.live.exception.LiveCancellationException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun Guild.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
): LiveGuild = LiveGuild(this, dispatcher, parent)

@KordPreview
inline fun Guild.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
block: LiveGuild.() -> Unit,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = this.live(dispatcher, parent).apply(block)

@KordPreview
Expand Down Expand Up @@ -143,7 +140,7 @@ fun LiveGuild.onGuildDelete(block: suspend (GuildDeleteEvent) -> Unit) = on(cons
class LiveGuild(
guild: Guild,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = guild.kord.coroutineContext.job
parent: CoroutineScope = guild.kord
) : AbstractLiveKordEntity(guild.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/kotlin/live/LiveKordEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ interface LiveKordEntity : KordEntity, CoroutineScope {
abstract class AbstractLiveKordEntity(
override val kord: Kord,
dispatcher: CoroutineDispatcher,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) : LiveKordEntity {

override val coroutineContext: CoroutineContext = dispatcher + SupervisorJob(parent)
override val coroutineContext: CoroutineContext = dispatcher + SupervisorJob(parent.coroutineContext.job)
Distractic marked this conversation as resolved.
Show resolved Hide resolved

private val mutex = Mutex()

Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/LiveMember.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import dev.kord.core.event.guild.MemberLeaveEvent
import dev.kord.core.event.guild.MemberUpdateEvent
import dev.kord.core.live.channel.LiveGuildChannel
import dev.kord.core.live.exception.LiveCancellationException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun Member.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveMember(this, dispatcher, parent)

@KordPreview
inline fun Member.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveMember.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -72,7 +69,7 @@ fun LiveGuildChannel.onGuildDelete(block: suspend (GuildDeleteEvent) -> Unit) =
class LiveMember(
member: Member,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = member.kord.coroutineContext.job
parent: CoroutineScope = member.kord
) : AbstractLiveKordEntity(member.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/LiveMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.event.message.*
import dev.kord.core.live.exception.LiveCancellationException
import dev.kord.core.supplier.EntitySupplyStrategy
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
suspend fun Message.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveMessage(this, withStrategy(EntitySupplyStrategy.cacheWithRestFallback).getGuildOrNull()?.id, dispatcher, parent)

@KordPreview
suspend fun Message.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveMessage.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -123,7 +120,7 @@ class LiveMessage(
message: Message,
val guildId: Snowflake?,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = message.kord.coroutineContext.job
parent: CoroutineScope = message.kord
) : AbstractLiveKordEntity(message.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/LiveRole.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,18 @@ import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.event.role.RoleDeleteEvent
import dev.kord.core.event.role.RoleUpdateEvent
import dev.kord.core.live.exception.LiveCancellationException
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun Role.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveRole(this, dispatcher, parent)

@KordPreview
inline fun Role.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveRole.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -62,7 +59,7 @@ fun LiveRole.onGuildDelete(block: suspend (GuildDeleteEvent) -> Unit) = on(consu
class LiveRole(
role: Role,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = role.kord.coroutineContext.job
parent: CoroutineScope = role.kord
) : AbstractLiveKordEntity(role.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/LiveUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ import dev.kord.core.entity.KordEntity
import dev.kord.core.entity.User
import dev.kord.core.event.Event
import dev.kord.core.event.user.UserUpdateEvent
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun User.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveUser(this, dispatcher, parent)

@KordPreview
inline fun User.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveUser.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand All @@ -31,7 +28,7 @@ fun LiveUser.onUpdate(block: suspend (UserUpdateEvent) -> Unit) = on(consumer =
class LiveUser(
user: User,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = user.kord.coroutineContext.job
parent: CoroutineScope = user.kord
) : AbstractLiveKordEntity(user.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/channel/LiveCategory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import dev.kord.core.event.channel.CategoryUpdateEvent
import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.live.exception.LiveCancellationException
import dev.kord.core.live.on
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun Category.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveCategory(this, dispatcher, parent)

@KordPreview
inline fun Category.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveCategory.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -72,7 +69,7 @@ fun LiveCategory.onGuildDelete(block: suspend (GuildDeleteEvent) -> Unit) = on(c
class LiveCategory(
channel: Category,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = channel.kord.coroutineContext.job,
parent: CoroutineScope = channel.kord,
) : LiveChannel(channel.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/kotlin/live/channel/LiveChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ import dev.kord.core.event.message.*
import dev.kord.core.event.user.VoiceStateUpdateEvent
import dev.kord.core.live.AbstractLiveKordEntity
import dev.kord.core.live.on
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun Channel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = when (this) {
is DmChannel -> this.live(dispatcher, parent)
is NewsChannel -> this.live(dispatcher, parent)
Expand All @@ -34,8 +31,11 @@ fun Channel.live(
}

@KordPreview
inline fun Channel.live(dispatcher: CoroutineDispatcher = Dispatchers.Default, block: LiveChannel.() -> Unit) =
this.live(dispatcher).apply(block)
inline fun Channel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: CoroutineScope = kord,
block: LiveChannel.() -> Unit
) = this.live(dispatcher, parent).apply(block)

@KordPreview
fun LiveChannel.onVoiceStateUpdate(block: suspend (VoiceStateUpdateEvent) -> Unit) = on(consumer = block)
Expand Down Expand Up @@ -112,7 +112,7 @@ fun LiveChannel.onGuildUpdate(block: suspend (GuildUpdateEvent) -> Unit) = on(co
abstract class LiveChannel(
kord: Kord,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) : AbstractLiveKordEntity(kord, dispatcher, parent) {

abstract val channel: Channel
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/channel/LiveDmChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import dev.kord.core.event.channel.DMChannelUpdateEvent
import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.live.exception.LiveCancellationException
import dev.kord.core.live.on
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun DmChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveDmChannel(this, dispatcher, parent)

@KordPreview
inline fun DmChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveDmChannel.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -72,7 +69,7 @@ fun LiveDmChannel.onGuildDelete(block: suspend (GuildDeleteEvent) -> Unit) = on(
class LiveDmChannel(
channel: DmChannel,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = channel.kord.coroutineContext.job
parent: CoroutineScope = channel.kord
) : LiveChannel(channel.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/channel/LiveGuildChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ import dev.kord.core.event.channel.ChannelUpdateEvent
import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.live.exception.LiveCancellationException
import dev.kord.core.live.on
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun GuildChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveGuildChannel(this, dispatcher, parent)

@KordPreview
inline fun GuildChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveGuildChannel.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -73,7 +70,7 @@ fun LiveGuildChannel.onGuildDelete(block: suspend (GuildDeleteEvent) -> Unit) =
class LiveGuildChannel(
channel: GuildChannel,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = channel.kord.coroutineContext.job
parent: CoroutineScope = channel.kord
) : LiveChannel(channel.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/channel/LiveGuildMessageChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import dev.kord.core.event.channel.ChannelUpdateEvent
import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.live.exception.LiveCancellationException
import dev.kord.core.live.on
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun GuildMessageChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveGuildMessageChannel(this, dispatcher, parent)

@KordPreview
inline fun GuildMessageChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveGuildMessageChannel.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -72,7 +69,7 @@ fun LiveGuildMessageChannel.onDelete(block: suspend (GuildDeleteEvent) -> Unit)
class LiveGuildMessageChannel(
channel: GuildMessageChannel,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = channel.kord.coroutineContext.job
parent: CoroutineScope = channel.kord
) : LiveChannel(channel.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down
11 changes: 4 additions & 7 deletions core/src/main/kotlin/live/channel/LiveVoiceChannel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,18 @@ import dev.kord.core.event.channel.VoiceChannelUpdateEvent
import dev.kord.core.event.guild.GuildDeleteEvent
import dev.kord.core.live.exception.LiveCancellationException
import dev.kord.core.live.on
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.job
import kotlinx.coroutines.*

@KordPreview
fun VoiceChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job
parent: CoroutineScope = kord
) = LiveVoiceChannel(this, dispatcher, parent)

@KordPreview
inline fun VoiceChannel.live(
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = kord.coroutineContext.job,
parent: CoroutineScope = kord,
block: LiveVoiceChannel.() -> Unit
) = this.live(dispatcher, parent).apply(block)

Expand Down Expand Up @@ -72,7 +69,7 @@ fun LiveVoiceChannel.onGuildDelete(block: suspend (GuildDeleteEvent) -> Unit) =
class LiveVoiceChannel(
channel: VoiceChannel,
dispatcher: CoroutineDispatcher = Dispatchers.Default,
parent: Job? = channel.kord.coroutineContext.job
parent: CoroutineScope = channel.kord
) : LiveChannel(channel.kord, dispatcher, parent), KordEntity {

override val id: Snowflake
Expand Down