From 3b5b9a1ae5803466e9b18846aae6fcd16bf8a637 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Wed, 26 Oct 2022 19:21:35 +0600 Subject: [PATCH] Use `Channel` in Download cache (#8292) * Use `Channel` in Download cache Co-Authored-By: Andreas <6576096+ghostbear@users.noreply.github.com> * Use Unlimited capacity Co-authored-by: stevenyomi <95685115+stevenyomi@users.noreply.github.com> Co-authored-by: Andreas <6576096+ghostbear@users.noreply.github.com> Co-authored-by: stevenyomi <95685115+stevenyomi@users.noreply.github.com> --- .../tachiyomi/data/download/DownloadCache.kt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt index 195ae4605..9523d9ffc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadCache.kt @@ -10,16 +10,18 @@ import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.util.lang.launchIO +import eu.kanade.tachiyomi.util.lang.launchNonCancellable import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.delay -import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach +import kotlinx.coroutines.flow.onStart +import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.withTimeout import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.api.get @@ -41,10 +43,8 @@ class DownloadCache( private val downloadPreferences: DownloadPreferences = Injekt.get(), ) { - // This is just a mechanism of notifying consumers of updates to the cache, the value itself - // is meaningless. - private val _state: MutableStateFlow = MutableStateFlow(0L) - val changes = _state.asStateFlow() + private val _changes: Channel = Channel(Channel.UNLIMITED) + val changes = _changes.receiveAsFlow().onStart { emit(Unit) } private val scope = CoroutineScope(Dispatchers.IO) @@ -300,7 +300,9 @@ class DownloadCache( } private fun notifyChanges() { - _state.value += 1 + scope.launchNonCancellable { + _changes.send(Unit) + } } /**