Skip to content

Commit

Permalink
Merge branch 'lavakord-merged-events' of https://github.com/botlabs-g…
Browse files Browse the repository at this point in the history
…g/Lavalink.kt into botlabs-gg-lavakord-merged-events
  • Loading branch information
DRSchlaubi committed Nov 30, 2023
2 parents 167b184 + b1ef00f commit 42fdf83
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
13 changes: 9 additions & 4 deletions core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package dev.schlaubi.lavakord

import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.audio.RestNode
import dev.schlaubi.lavakord.audio.*
import io.ktor.http.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow

/**
* Representation of a Lavalink cluster.
Expand All @@ -13,11 +12,17 @@ import kotlinx.coroutines.CoroutineScope
* @property userId the id of the Discord bot user
* @property options Configuration options (See [LavaKordOptions]
*/
public interface LavaKord : CoroutineScope {
public interface LavaKord : CoroutineScope, EventSource<Event> {
public val nodes: List<Node>
public val userId: ULong
public val options: LavaKordOptions

/** A merged [Flow] of [Event]s produced by this instance's [Node]s */
public override val events: Flow<Event>

/** This simply returns [this][LavaKord]. It is required for implementations of [EventSource]*/
public override val coroutineScope: CoroutineScope get() = this

/**
* Returns the corresponding [Link] for the [guildId].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import dev.arbjerg.lavalink.protocol.v4.*
import dev.schlaubi.lavakord.LavaKord
import dev.schlaubi.lavakord.LavaKordOptions
import dev.schlaubi.lavakord.RestException
import dev.schlaubi.lavakord.audio.Link
import dev.schlaubi.lavakord.audio.Node
import dev.schlaubi.lavakord.audio.RestNode
import dev.schlaubi.lavakord.audio.*
import dev.schlaubi.lavakord.internal.HttpEngine
import dev.schlaubi.lavakord.internal.RestNodeImpl
import dev.schlaubi.lavakord.rest.updatePlayer
Expand All @@ -22,6 +20,10 @@ import io.ktor.http.*
import io.ktor.serialization.kotlinx.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.atomicfu.atomic
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.contextual
Expand Down Expand Up @@ -56,6 +58,10 @@ public abstract class AbstractLavakord internal constructor(
private val nodesMap = mutableMapOf<String, Node>()
protected val linksMap: MutableMap<ULong, Link> = mutableMapOf()

private val eventPublisher: MutableSharedFlow<Event> = MutableSharedFlow(extraBufferCapacity = Channel.UNLIMITED)
override val events: SharedFlow<Event>
get() = eventPublisher.asSharedFlow()

internal val json = kotlinx.serialization.json.Json {
ignoreUnknownKeys = true
serializersModule = LavalinkSerializersModule + SerializersModule {
Expand Down Expand Up @@ -144,6 +150,7 @@ public abstract class AbstractLavakord internal constructor(
val finalName = name ?: "Lavalink_Node_#${nodeCounter.incrementAndGet()}"
val node =
NodeImpl(serverUri, finalName, password, this)
node.on<Event> { eventPublisher.tryEmit(this) }
nodesMap[finalName] = node
launch {
node.check()
Expand Down

0 comments on commit 42fdf83

Please sign in to comment.