Skip to content

timeout+collector operator #4250

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

Closed
CLOVIS-AI opened this issue Oct 15, 2024 · 1 comment
Closed

timeout+collector operator #4250

CLOVIS-AI opened this issue Oct 15, 2024 · 1 comment

Comments

@CLOVIS-AI
Copy link
Contributor

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".

@CLOVIS-AI CLOVIS-AI changed the title timeoutOr operator timeout+collector operator Oct 15, 2024
@dkhalanskyjb
Copy link
Collaborator

Duplicate of #4143

@dkhalanskyjb dkhalanskyjb marked this as a duplicate of #4143 Oct 15, 2024
@dkhalanskyjb dkhalanskyjb closed this as not planned Won't fix, can't repro, duplicate, stale Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants