diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/DownloadManager.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/DownloadManager.kt index 32a07f84..79295ad1 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/DownloadManager.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/download/DownloadManager.kt @@ -119,16 +119,13 @@ object DownloadManager { private val notifyFlow = MutableSharedFlow(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) - private val statusFlow = MutableSharedFlow() - val status = - statusFlow.onStart { emit(Unit) } - .map { getStatus() } + private val statusFlow = MutableSharedFlow() + val status = statusFlow.onStart { emit(getStatus()) } init { scope.launch { notifyFlow.sample(1.seconds).collect { - statusFlow.emit(Unit) - sendStatusToAllClients() + notifyAllClients(immediate = true) } } } @@ -139,23 +136,26 @@ object DownloadManager { saveQueueFlow.onEach { saveDownloadQueue() }.launchIn(scope) } - private fun sendStatusToAllClients() { - val status = getStatus() + private fun sendStatusToAllClients(status: DownloadStatus) { clients.forEach { it.value.send(status) } } private fun notifyAllClients(immediate: Boolean = false) { + if (immediate) { + val status = getStatus() + + scope.launch { + statusFlow.emit(status) + sendStatusToAllClients(status) + } + + return + } + scope.launch { notifyFlow.emit(Unit) - - if (immediate) { - statusFlow.emit(Unit) - } - } - if (immediate) { - sendStatusToAllClients() } }