commonMain.cache.CachingGateway.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kord-core Show documentation
Show all versions of kord-core Show documentation
Idiomatic Kotlin Wrapper for The Discord API
package dev.kord.core.cache
import dev.kord.cache.api.DataCache
import dev.kord.gateway.Close
import dev.kord.gateway.Gateway
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlin.coroutines.CoroutineContext
/**
* A bridge between [DataCache] and [Gateway] that automatically empties cache on disconnect.
*/
public class CachingGateway(
private val cache: DataCache,
private val gateway: Gateway,
dispatcher: CoroutineDispatcher = Dispatchers.Default
) : DataCache by cache, Gateway by gateway, CoroutineScope {
override val coroutineContext: CoroutineContext = SupervisorJob() + dispatcher
init {
gateway.events.filterIsInstance().onEach { removeKordData() }.launchIn(this)
}
override fun toString(): String {
return "CachingGateway(cache=$cache, gateway=$gateway)"
}
}