Cleanup Preference.asHotFlow() (#9257)

* Drop duplicate initial call in Preference.asHotFlow

Preference.changes() always starts by returning the current value of
the preference, so asHotFlow calls block twice on the initial value.

Possible breaking change: As implemented, asHotFlow ran block(get())
before returning the flow. After this change, the first call to block
will run within the flow collection. This might cause concurrency
issues if the flow collection is late to execute.

* Inline Preference.asHotFlow

The Preference.changes().onEach().launchIn() pattern is used widely,
so the asHotFlow extension method is redundant.
This commit is contained in:
Two-Ai
2023-03-26 11:52:54 -04:00
committed by GitHub
parent 0bcc22822d
commit 35d381144d
4 changed files with 19 additions and 27 deletions

View File

@@ -1,8 +1,6 @@
package eu.kanade.tachiyomi.util.preference
import android.widget.CompoundButton
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach
import tachiyomi.core.preference.Preference
/**
@@ -13,12 +11,6 @@ fun CompoundButton.bindToPreference(pref: Preference<Boolean>) {
setOnCheckedChangeListener { _, isChecked -> pref.set(isChecked) }
}
fun <T> Preference<T>.asHotFlow(block: (T) -> Unit): Flow<T> {
block(get())
return changes()
.onEach { block(it) }
}
operator fun <T> Preference<Set<T>>.plusAssign(item: T) {
set(get() + item)
}