timeout+collector operator
CLOVIS-AI opened this issue · 1 comments
CLOVIS-AI commented
Use case
Flow
has the timeout
operator. When the timeout is reached, a TimeoutCancellationException
is thrown into the Flow
. Typical usage seems to be to catch the thrown exception to perform additional processing (from the .timeout
kdoc):
.timeout(100.milliseconds).catch { exception ->
if (exception is TimeoutCancellationException) {
// Catch the TimeoutCancellationException emitted above.
// Emit desired item on timeout.
emit(-1)
} else {
// Throw other exceptions.
throw exception
}
}
This is a bit unwieldy. What about having an overload for this specific case?
Declaration:
fun <T> Flow<T>.timeout(duration: Duration, block: FlowCollector<T>.() -> Unit): Flow<T>
Usage (same behavior as above):
.timeout(100.milliseconds) {
emit(-1)
}
This came up as a user of one of my libraries (Pedestal Cache), which is entirely Flow
-based, attempted to represent "Get the latest value from the cache, substituting a default value if the request takes too long".
dkhalanskyjb commented
Duplicate of #4143