diff options
Diffstat (limited to 'android/lib/common/src')
| -rw-r--r-- | android/lib/common/src/main/kotlin/net/mullvad/mullvadvpn/lib/common/util/CommonFlowUtils.kt | 17 |
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 } |
