Skip to content

Instantly share code, notes, and snippets.

@romansl
Created May 31, 2019 12:56
Show Gist options
  • Save romansl/d8d30a1e96870ad448c9c8326a269fbd to your computer and use it in GitHub Desktop.
Save romansl/d8d30a1e96870ad448c9c8326a269fbd to your computer and use it in GitHub Desktop.
Эта функция должна преобразовывать обычный flow во flow, значение которой обёрнуто в Result
fun <T> Flow<T>.toResultFlow(): Flow<Result<T>> {
return flow {
while (true) {
var fromDownstream = false
try {
collect {
try {
emit(Result.success(it))
} catch (e: Throwable) {
fromDownstream = true
throw e
}
}
} catch (e: Throwable) {
if (fromDownstream) throw e
emit(Result.failure(e)) // <-- проблема тут! после вызова этой функции запустится ещё одна итерация цикла
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment