Skip to content
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

Align the Uni::eventually operators to onTermination() semantics #1743

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions documentation/docs/guides/shortcut-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ This is useful to execute an asynchronous action without modifying incoming item

The following table lists the available shortcuts available by the `Uni` class:

| Shortcut | Equivalent |
|----------------------------------------------------------|-----------------------------------------------------------------------------------------------------|
| `uni.map(x -> y)` | `uni.onItem().transform(x -> y)` |
| `uni.flatMap(x -> uni2)` | `uni.onItem().transformToUni(x -> uni2)` |
| `uni.chain(x -> uni2)` | `uni.onItem().transformToUni(x -> uni2)` |
| `uni.invoke(x -> System.out.println(x))` | `uni.onItem().invoke(x -> System.out.println(x))` |
| `uni.call(x -> uni2)` | `uni.onItem().call(x -> uni2)` |
| `uni.eventually(() -> System.out.println("eventually"))` | `uni.onItemOrFailure().invoke((ignoredItem, ignoredException) -> System.out.println("eventually"))` |
| `uni.eventually(() -> uni2)` | `uni.onItemOrFailure().call((ignoredItem, ignoredException) -> uni2)` |
| `uni.replaceWith(x)` | `uni.onItem().transform(ignored -> x)` |
| `uni.replaceWith(uni2)` | `uni.onItem().transformToUni(ignored -> uni2)` |
| `uni.replaceIfNullWith(x)` | `uni.onItem().ifNull().continueWith(x)` |
| Shortcut | Equivalent |
|----------------------------------------------------------|--------------------------------------------------------------------------------------|
| `uni.map(x -> y)` | `uni.onItem().transform(x -> y)` |
| `uni.flatMap(x -> uni2)` | `uni.onItem().transformToUni(x -> uni2)` |
| `uni.chain(x -> uni2)` | `uni.onItem().transformToUni(x -> uni2)` |
| `uni.invoke(x -> System.out.println(x))` | `uni.onItem().invoke(x -> System.out.println(x))` |
| `uni.call(x -> uni2)` | `uni.onItem().call(x -> uni2)` |
| `uni.eventually(() -> System.out.println("eventually"))` | `uni.onTermination().invoke(() -> System.out.println("eventually"))` |
| `uni.eventually(() -> uni2)` | `uni.onTermination().call((ignoredItem, ignoredError, ignoredCancellation) -> uni2)` |
| `uni.replaceWith(x)` | `uni.onItem().transform(ignored -> x)` |
| `uni.replaceWith(uni2)` | `uni.onItem().transformToUni(ignored -> uni2)` |
| `uni.replaceIfNullWith(x)` | `uni.onItem().ifNull().continueWith(x)` |

10 changes: 6 additions & 4 deletions implementation/src/main/java/io/smallrye/mutiny/Uni.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.smallrye.mutiny.subscription.UniEmitter;
import io.smallrye.mutiny.subscription.UniSubscriber;
import io.smallrye.mutiny.subscription.UniSubscription;
import io.smallrye.mutiny.tuples.Functions;

/**
* A {@link Uni} represents a lazy asynchronous action. It follows the subscription pattern, meaning that the action
Expand Down Expand Up @@ -551,7 +552,8 @@ default Uni<T> eventually(Runnable action) {
}

/**
* When this {@link Uni} emits an item or a failure, invoke a {@link Uni} supplier then invoke the supplied {@link Uni}.
* When this {@link Uni} emits an item, a failure or is being cancelled, invoke a {@link Uni} supplier then invoke the
* supplied {@link Uni}.
* When the supplied {@link Uni} emits an item then it is ignored, and when it emits a failure it is reported.
* <p>
* This is equivalent to a {@code finally} block in Java.
Expand All @@ -566,8 +568,8 @@ default Uni<T> eventually(Runnable action) {
* }
* </pre>
* <p>
* This method is a shortcut for {@link UniOnItemOrFailure#call(BiFunction)}:
* {@code onItemOrFailure().call((item, err) -> supplier.get())}
* This method is a shortcut for {@link UniOnTerminate#call(Functions.Function3)}:
* {@code onTermination().call((item, err, cancelled) -> actual.get())}
*
* @param supplier a {@link Uni} supplier, cannot be {@code null} and cannot return {@code null}.
* @param <O> the type of the item
Expand All @@ -577,7 +579,7 @@ default Uni<T> eventually(Runnable action) {
@CheckReturnValue
default <O> Uni<T> eventually(Supplier<Uni<? extends O>> supplier) {
Supplier<Uni<? extends O>> actual = nonNull(supplier, "supplier");
return onItemOrFailure().call((item, err) -> actual.get());
return onTermination().call((item, err, cancelled) -> actual.get());
}

/**
Expand Down