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.distinctUntilChanged
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.sample
import mozilla.components.browser.state.action.BrowserAction import mozilla.components.browser.state.action.BrowserAction
import mozilla.components.browser.state.state.BrowserState import mozilla.components.browser.state.state.BrowserState
import mozilla.components.feature.addons.logger import mozilla.components.feature.addons.logger
@@ -40,6 +41,8 @@ class Events(
stateFlow.flowScoped { flow -> stateFlow.flowScoped { flow ->
flow.map { state -> state.selectedTabId } flow.map { state -> state.selectedTabId }
.distinctUntilChanged() .distinctUntilChanged()
// Make sure this is sent after tabadded action and tab list change
.debounce { 50 }
.collect { tabId -> .collect { tabId ->
flutterEvents.onSelectedTabChange( flutterEvents.onSelectedTabChange(
System.currentTimeMillis(), 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 -> stateFlow.flowScoped { flow ->
flow.mapNotNull { state -> state.tabs } flow.mapNotNull { state -> state.tabs }
.filterChanged { .filterChanged {
it.content it.content
} }
.ifAnyChanged { arrayOf (it.content.icon) } .ifAnyChanged { arrayOf(it.content.icon) }
.debounce { 16 } .sample(50)
.collect { tab -> .collect { tab ->
val iconBytes = tab.content.icon?.toWebPBytes() val iconBytes = tab.content.icon?.toWebPBytes()
flutterEvents.onIconChange( flutterEvents.onIconChange(
@@ -70,7 +83,7 @@ class Events(
.filterChanged { .filterChanged {
it.content.securityInfo it.content.securityInfo
} }
.debounce { 16 } .sample(50)
.collect { tab -> .collect { tab ->
flutterEvents.onSecurityInfoStateChange( flutterEvents.onSecurityInfoStateChange(
System.currentTimeMillis(), System.currentTimeMillis(),
@@ -89,12 +102,13 @@ class Events(
.filterChanged { .filterChanged {
it.readerState it.readerState
} }
.ifAnyChanged { arrayOf( .ifAnyChanged {
it.readerState.readerable, arrayOf(
it.readerState.active, it.readerState.readerable,
) it.readerState.active,
)
} }
.debounce { 16 } .sample(50)
.collect { tab -> .collect { tab ->
flutterEvents.onReaderableStateChange( flutterEvents.onReaderableStateChange(
System.currentTimeMillis(), System.currentTimeMillis(),
@@ -112,22 +126,25 @@ class Events(
.filterChanged { .filterChanged {
it.content it.content
} }
.ifAnyChanged { arrayOf( .ifAnyChanged {
it.content.history, arrayOf(
it.content.canGoBack, it.content.history,
it.content.canGoForward, it.content.canGoBack,
) it.content.canGoForward,
)
} }
.debounce { 16 } .sample(50)
.collect { tab -> .collect { tab ->
flutterEvents.onHistoryStateChange( flutterEvents.onHistoryStateChange(
System.currentTimeMillis(), System.currentTimeMillis(),
tab.id, tab.id,
HistoryState( HistoryState(
items = tab.content.history.items.map { item -> HistoryItem( items = tab.content.history.items.map { item ->
url = item.uri, HistoryItem(
title = item.title url = item.uri,
) }, title = item.title
)
},
currentIndex = tab.content.history.currentIndex.toLong(), currentIndex = tab.content.history.currentIndex.toLong(),
canGoBack = tab.content.canGoBack, canGoBack = tab.content.canGoBack,
canGoForward = tab.content.canGoForward, 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 -> stateFlow.flowScoped { flow ->
flow.mapNotNull { state -> state.tabs } flow.mapNotNull { state -> state.tabs }
.filterChanged { .filterChanged {
it.content it.content
} }
.ifAnyChanged { arrayOf( .ifAnyChanged {
it.content.url, arrayOf(
it.content.title, it.content.url,
it.content.private, it.content.title,
it.content.fullScreen, it.content.private,
it.content.progress, it.content.fullScreen,
it.content.loading) it.content.progress,
it.content.loading
)
} }
.debounce { 16 } .sample(50)
.collect { tab -> .collect { tab ->
flutterEvents.onTabContentStateChange( flutterEvents.onTabContentStateChange(
System.currentTimeMillis(), System.currentTimeMillis(),