Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Jul 18, 2024
1 parent 5b848e9 commit 2939012
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/src/main/java/io/legado/app/utils/FlowExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,17 @@ fun <T> Flow<T>.flowWithLifecycleAndDatabaseChange(
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
table: String
): Flow<T> = callbackFlow {
var update = 0
val channel = appDb.invalidationTrackerFlow(table)
.conflate()
.onEach { update++ }
.produceIn(this)
lifecycle.repeatOnLifecycle(minActiveState) {
channel.receive()
if (update == 0) {
channel.receive()
}
this@flowWithLifecycleAndDatabaseChange.collect {
update = 0
send(it)
}
}
Expand All @@ -231,16 +236,21 @@ fun <T> Flow<T>.flowWithLifecycleAndDatabaseChangeFirst(
minActiveState: Lifecycle.State = Lifecycle.State.STARTED,
table: String
): Flow<T> = callbackFlow {
var update = 0
val isActive = lifecycle.currentState.isAtLeast(minActiveState)
val channel = appDb.invalidationTrackerFlow(table, emitInitialState = isActive)
.conflate()
.onEach { update++ }
.produceIn(this)
if (!isActive) {
send(first())
}
lifecycle.repeatOnLifecycle(minActiveState) {
channel.receive()
if (update == 0) {
channel.receive()
}
this@flowWithLifecycleAndDatabaseChangeFirst.collect {
update = 0
send(it)
}
}
Expand Down

0 comments on commit 2939012

Please sign in to comment.