From 5732acd75e4bec427eda002fe096dc36cea90882 Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Fri, 2 Jan 2026 07:54:09 +0100 Subject: [PATCH] try to improve flow sample/debounce timing --- .../components/Events.kt | 81 +++++++++++-------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/weblibre/flutter_mozilla_components/components/Events.kt b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/weblibre/flutter_mozilla_components/components/Events.kt index d1317e18..60003d67 100644 --- a/packages/flutter_mozilla_components/android/src/main/kotlin/eu/weblibre/flutter_mozilla_components/components/Events.kt +++ b/packages/flutter_mozilla_components/android/src/main/kotlin/eu/weblibre/flutter_mozilla_components/components/Events.kt @@ -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 @@ -34,12 +35,14 @@ class Events( private val flutterEvents: GeckoStateEvents, ) { val readerViewEvents by lazy { ReaderViewEventsImpl() } - + @OptIn(FlowPreview::class) fun registerFlowEvents(stateFlow: Store) { 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(),