From b1ef00fbfd1240f6306f1cf1e501be18f082b8fe Mon Sep 17 00:00:00 2001 From: Freya Arbjerg Date: Tue, 28 Nov 2023 11:54:04 +0100 Subject: [PATCH] Make AbstractLavakord emit all node events --- .../kotlin/dev/schlaubi/lavakord/LavaKord.kt | 13 +++++++++---- .../lavakord/audio/internal/AbstractLavakord.kt | 13 ++++++++++--- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt index 87895a98..e9b865f2 100644 --- a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt +++ b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/LavaKord.kt @@ -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. @@ -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 { public val nodes: List 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 + + /** 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]. */ diff --git a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt index d573066d..e04823e1 100644 --- a/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt +++ b/core/src/commonMain/kotlin/dev/schlaubi/lavakord/audio/internal/AbstractLavakord.kt @@ -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 @@ -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 @@ -56,6 +58,10 @@ public abstract class AbstractLavakord internal constructor( private val nodesMap = mutableMapOf() protected val linksMap: MutableMap = mutableMapOf() + private val eventPublisher: MutableSharedFlow = MutableSharedFlow(extraBufferCapacity = Channel.UNLIMITED) + override val events: SharedFlow + get() = eventPublisher.asSharedFlow() + internal val json = kotlinx.serialization.json.Json { ignoreUnknownKeys = true serializersModule = LavalinkSerializersModule + SerializersModule { @@ -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 { eventPublisher.tryEmit(this) } nodesMap[finalName] = node launch { node.check()