Alex Yordanov
09/05/2022, 3:57 AMcallbackFlow
in Either
? I came up with that but it looks kind of funky:
val flow = callbackFlow<Unit> {
delay(2000)
cancel(CancellationException("Boom"))
}
Either.catch {
flow
.catch { throw RuntimeException(it.message) }
.collect()
}.mapLeft { it.message?.let { msg -> FlowError.Canceled(msg) } }
simon.vergauwen
09/05/2022, 6:48 AMFlow
would result in many results, but Either
only has a single result. Are you just interested in capturing the cancelled event, or success? I think here your final result is Either<FlowError.Canceled?, Unit>
.Sam
09/05/2022, 7:42 AMAlex Yordanov
09/05/2022, 12:18 PMsimon.vergauwen
09/05/2022, 12:34 PMsuspend fun
with suspendCoroutineCancellable
? Then you could do something like:
either<Error, Res> {
val res: Res = guaranteeCase({ action() }) { exitCase ->
when(exitCase) {
ExitCase.Completed -> Unit
ExitCase.Failure -> Unit
ExitCase.Cancelled -> shift(FlowError.Canceled(it.message ?: ""))
}
}
}
simon.vergauwen
09/05/2022, 12:35 PMeither<Error, Res> {
val res: Res = onCancel({ action() }) {
shift(..)
}
}
Alex Yordanov
09/05/2022, 12:40 PM