Created
May 31, 2019 12:56
-
-
Save romansl/d8d30a1e96870ad448c9c8326a269fbd to your computer and use it in GitHub Desktop.
Эта функция должна преобразовывать обычный flow во flow, значение которой обёрнуто в Result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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