mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-06-01 23:45:28 +00:00
38 lines
1.0 KiB
Kotlin
38 lines
1.0 KiB
Kotlin
@file:Suppress("NOTHING_TO_INLINE")
|
|
|
|
package dev.inmo.micro_utils.coroutines
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.flow.*
|
|
|
|
/**
|
|
* Shortcut for chain if [Flow.onEach] and [Flow.launchIn]
|
|
*/
|
|
inline fun <T> Flow<T>.subscribe(scope: CoroutineScope, noinline block: suspend (T) -> Unit) = onEach(block).launchIn(scope)
|
|
|
|
/**
|
|
* Use [subscribe], but all [block]s will be called inside of [safely] function.
|
|
* Use [onException] to set up your reaction for [Throwable]s
|
|
*/
|
|
inline fun <T> Flow<T>.subscribeSafely(
|
|
scope: CoroutineScope,
|
|
noinline onException: ExceptionHandler<Unit> = defaultSafelyExceptionHandler,
|
|
noinline block: suspend (T) -> Unit
|
|
) = subscribe(scope) {
|
|
safely(onException) {
|
|
block(it)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Use [subscribeSafelyWithoutExceptions], but all exceptions inside of [safely] will be skipped
|
|
*/
|
|
inline fun <T> Flow<T>.subscribeSafelyWithoutExceptions(
|
|
scope: CoroutineScope,
|
|
noinline block: suspend (T) -> Unit
|
|
) = subscribeSafely(
|
|
scope,
|
|
{},
|
|
block
|
|
)
|