summaryrefslogtreecommitdiffhomepage
path: root/android/lib/common/src
diff options
context:
space:
mode:
authorDavid Göransson <david.goransson@mullvad.net>2025-03-03 23:10:19 +0100
committerDavid Göransson <david.goransson@mullvad.net>2025-03-03 23:10:19 +0100
commitf31658c25760b796dd2775fcdbf7d8b980618d9d (patch)
treecff01ab58fafdd6da6402f2b9f0056584516109f /android/lib/common/src
parent0199b5872f8268a7017aa202c309b50dafe2c704 (diff)
parent9eb15e878f8a5aec1cb0963d33b1492308b6a4f7 (diff)
downloadmullvadvpn-f31658c25760b796dd2775fcdbf7d8b980618d9d.tar.xz
mullvadvpn-f31658c25760b796dd2775fcdbf7d8b980618d9d.zip
Merge branch 'fix-broken-connectivitylistener-on-main-branch-droid-1856'
Diffstat (limited to 'android/lib/common/src')
-rw-r--r--android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt17
1 files changed, 17 insertions, 0 deletions
diff --git a/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt b/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt
index bf94c80778..863fc32251 100644
--- a/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt
+++ b/android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt
@@ -1,9 +1,26 @@
package net.mullvad.mullvadvpn.lib.common.util
+import kotlin.time.Duration
+import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.firstOrNull
+import kotlinx.coroutines.flow.map
+import kotlinx.coroutines.flow.withIndex
import kotlinx.coroutines.withTimeoutOrNull
suspend fun <T> Flow<T>.firstOrNullWithTimeout(timeMillis: Long): T? {
return withTimeoutOrNull(timeMillis) { firstOrNull() }
}
+
+@OptIn(FlowPreview::class)
+fun <T> Flow<T>.debounceFirst(timeout: Duration): Flow<T> =
+ withIndex()
+ .debounce {
+ if (it.index == 0) {
+ timeout
+ } else {
+ Duration.ZERO
+ }
+ }
+ .map { it.value }