-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal kotlin flow repeatUntil operator #2751
Comments
Thanks for the detailed write-up! Could you please elaborate on why do you need to re-collect the flow while it still has |
Sure!
// Each collect on `Flow<Message>` calls `GET/STATUS` endpoint.
fun getTransformationStatus(): Flow<Message> { ... } |
Thanks! Also, related to #1850 |
It makes sense to consider them together because |
Thanks for clarifying 😁 |
@jeziellago I had similar problem. I couldn't get your code to work though. In your code you used I did something similar in my project: RetryMap Proposalfun <T, R> Flow<T>.retryMap(
map: suspend (T) -> R,
predicate: suspend FlowCollector<R>.(item: T, result: R, attempt: Long) -> Boolean
): Flow<R> {
tailrec suspend fun FlowCollector<R>.retryCollect(item: T, attempt: Long) {
val result = map(item)
if (predicate(item, result, attempt)) {
retryCollect(item, attempt = attempt + 1)
} else {
emit(result)
}
}
return flow {
collect { item ->
retryCollect(item, attempt = 1L)
}
}
} Usage exampleobject Request
sealed class Result {
object Success : Result()
object Failure : Result()
object Loading : Result()
}
suspend fun getResult(request: Request) : Result = TODO()
flowOf(Request)
.retryMap(::getResult) { _, result, attempt ->
result is Result.Failure && attempt <= 3
}
.onStart { emit(Result.Loading) }
.collect { println(it) } Advanced usageThanks to |
Yeah, the initial problem statement by @jeziellago seems strange to me too, because it looks like fun getTransformationStatus(): Flow<Message> { ... }
// should be
suspend fun getTransformationStatus(): Message { ... } So it doesn't feel like a use case for a flow operator (maybe for a flow generator though) |
repeatUntil would be quite helpful and time saving for those who migrate from Rx. |
Hello folks 👋🏼
I have a pooling use-case and I'd like some operator for repeat operations.
My use-case
The
Flow<Message>
should complete withCompleted
orFailure
status, otherwise must repeat (withdelay()
and some attempts).We have the
retry
andretryWhen
operators, but I don't have exceptions propagation to control my flow.Proposal operator repeatUntil
Inspired by
retryWhen
operator, I created the extensionrepeatUntil
.Using repeatUntil in my use-case:
It works well for me, but I'd like to know if there is a better way to do that.
The text was updated successfully, but these errors were encountered: