try to improve flow sample/debounce timing

This commit is contained in:
Fabian Freund
2026-01-02 07:54:09 +01:00
parent e068445daa
commit 5732acd75e
@@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.sample
import mozilla.components.browser.state.action.BrowserAction
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.feature.addons.logger
@@ -40,6 +41,8 @@ class Events(
stateFlow.flowScoped { flow ->
flow.map { state -> state.selectedTabId }
.distinctUntilChanged()
// Make sure this is sent after tabadded action and tab list change
.debounce { 50 }
.collect { tabId ->
flutterEvents.onSelectedTabChange(
System.currentTimeMillis(),
@@ -48,13 +51,23 @@ class Events(
}
}
stateFlow.flowScoped { flow ->
flow.mapNotNull { state -> state.tabs.map { tab -> tab.id } }
.distinctUntilChanged()
// Make sure this is sent after tabadded action
.debounce { 25 }
.collect { tabs ->
flutterEvents.onTabListChange(System.currentTimeMillis(), tabs) { _ -> }
}
}
stateFlow.flowScoped { flow ->
flow.mapNotNull { state -> state.tabs }
.filterChanged {
it.content
}
.ifAnyChanged { arrayOf (it.content.icon) }
.debounce { 16 }
.ifAnyChanged { arrayOf(it.content.icon) }
.sample(50)
.collect { tab ->
val iconBytes = tab.content.icon?.toWebPBytes()
flutterEvents.onIconChange(
@@ -70,7 +83,7 @@ class Events(
.filterChanged {
it.content.securityInfo
}
.debounce { 16 }
.sample(50)
.collect { tab ->
flutterEvents.onSecurityInfoStateChange(
System.currentTimeMillis(),
@@ -89,12 +102,13 @@ class Events(
.filterChanged {
it.readerState
}
.ifAnyChanged { arrayOf(
it.readerState.readerable,
it.readerState.active,
)
.ifAnyChanged {
arrayOf(
it.readerState.readerable,
it.readerState.active,
)
}
.debounce { 16 }
.sample(50)
.collect { tab ->
flutterEvents.onReaderableStateChange(
System.currentTimeMillis(),
@@ -112,22 +126,25 @@ class Events(
.filterChanged {
it.content
}
.ifAnyChanged { arrayOf(
it.content.history,
it.content.canGoBack,
it.content.canGoForward,
)
.ifAnyChanged {
arrayOf(
it.content.history,
it.content.canGoBack,
it.content.canGoForward,
)
}
.debounce { 16 }
.sample(50)
.collect { tab ->
flutterEvents.onHistoryStateChange(
System.currentTimeMillis(),
tab.id,
HistoryState(
items = tab.content.history.items.map { item -> HistoryItem(
url = item.uri,
title = item.title
) },
items = tab.content.history.items.map { item ->
HistoryItem(
url = item.uri,
title = item.title
)
},
currentIndex = tab.content.history.currentIndex.toLong(),
canGoBack = tab.content.canGoBack,
canGoForward = tab.content.canGoForward,
@@ -136,28 +153,22 @@ class Events(
}
}
stateFlow.flowScoped { flow ->
flow.mapNotNull { state -> state.tabs.map {tab -> tab.id} }
.distinctUntilChanged()
.collect { tabs ->
flutterEvents.onTabListChange(System.currentTimeMillis(), tabs) { _ -> }
}
}
stateFlow.flowScoped { flow ->
flow.mapNotNull { state -> state.tabs }
.filterChanged {
it.content
}
.ifAnyChanged { arrayOf(
it.content.url,
it.content.title,
it.content.private,
it.content.fullScreen,
it.content.progress,
it.content.loading)
.ifAnyChanged {
arrayOf(
it.content.url,
it.content.title,
it.content.private,
it.content.fullScreen,
it.content.progress,
it.content.loading
)
}
.debounce { 16 }
.sample(50)
.collect { tab ->
flutterEvents.onTabContentStateChange(
System.currentTimeMillis(),