书源菜单问题
This commit is contained in:
1 file changed
+19
-6
@@ -236,16 +236,29 @@ fun <T> Flow<T>.flowWithLifecycleAndDatabaseChange(
|
|||||||
|
|
||||||
fun <T> Flow<T>.flowWithLifecycleAndDatabaseChangeFirst(
|
fun <T> Flow<T>.flowWithLifecycleAndDatabaseChangeFirst(
|
||||||
lifecycle: Lifecycle,
|
lifecycle: Lifecycle,
|
||||||
minActiveState: Lifecycle.State = Lifecycle.State.RESUMED,
|
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
|
||||||
table: String
|
table: String
|
||||||
): Flow<T> = callbackFlow {
|
): Flow<T> = callbackFlow {
|
||||||
val invalidationFlow = appDb.invalidationTracker
|
var update = 0
|
||||||
.createFlow(table, emitInitialState = true)
|
val isActive = lifecycle.currentState.isAtLeast(minActiveState)
|
||||||
|
val channel = appDb.invalidationTracker
|
||||||
|
.createFlow(table, emitInitialState = isActive)
|
||||||
.conflate()
|
.conflate()
|
||||||
|
.onEach { update++ }
|
||||||
|
.produceIn(this)
|
||||||
|
if (!isActive) {
|
||||||
|
firstOrNull()?.let {
|
||||||
|
send(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
lifecycle.repeatOnLifecycle(minActiveState) {
|
lifecycle.repeatOnLifecycle(minActiveState) {
|
||||||
combine(this@flowWithLifecycleAndDatabaseChangeFirst, invalidationFlow) { data, _ -> data }
|
if (update == 0) {
|
||||||
.collect { send(it) }
|
channel.receive()
|
||||||
|
}
|
||||||
|
this@flowWithLifecycleAndDatabaseChangeFirst.collect {
|
||||||
|
update = 0
|
||||||
|
send(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
close()
|
close()
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user