From c9423ef425e327e3908b2ab4cbc2febcc4d139e2 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sat, 16 Sep 2023 19:07:43 +0200 Subject: [PATCH] Send every download status change to the subscriber (#684) Flow::stateIn has "Strong equality-based conflation" (see documentation). Thus, it omits every value in case it's equal to the previous one. Since the DownloadManger::getStatus function returns a status with a queue, that contains all current "DownloadChapters" by reference, the equality check was always true. Thus, progress changes of downloads were never sent to subscribers. Subscriber were only notified about finished downloads (size of queue changed) or downloader status changes --- .../suwayomi/tachidesk/graphql/queries/DownloadQuery.kt | 9 +++++++-- .../tachidesk/manga/impl/download/DownloadManager.kt | 9 +++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/DownloadQuery.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/DownloadQuery.kt index 038e8765..3cb75d79 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/DownloadQuery.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/queries/DownloadQuery.kt @@ -1,11 +1,16 @@ package suwayomi.tachidesk.graphql.queries +import kotlinx.coroutines.flow.first import suwayomi.tachidesk.graphql.types.DownloadStatus import suwayomi.tachidesk.manga.impl.download.DownloadManager +import suwayomi.tachidesk.server.JavalinSetup.future +import java.util.concurrent.CompletableFuture class DownloadQuery { - fun downloadStatus(): DownloadStatus { - return DownloadStatus(DownloadManager.status.value) + fun downloadStatus(): CompletableFuture { + return future { + DownloadStatus(DownloadManager.status.first()) + } } } 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 4d44c9f6..042054f7 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 @@ -19,12 +19,11 @@ import kotlinx.coroutines.awaitAll import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.flow.sample -import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.serialization.Serializable import mu.KotlinLogging @@ -118,10 +117,8 @@ object DownloadManager { private val notifyFlow = MutableSharedFlow(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) val status = notifyFlow.sample(1.seconds) - .map { - getStatus() - } - .stateIn(scope, SharingStarted.Eagerly, getStatus()) + .onStart { emit(Unit) } + .map { getStatus() } init { scope.launch {