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

Simplify Uni to Multi bridge (again) #632

Merged
merged 1 commit into from
Jul 20, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

import java.util.concurrent.atomic.AtomicReference;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.operators.AbstractMulti;
import io.smallrye.mutiny.operators.AbstractUni;
import io.smallrye.mutiny.subscription.UniSubscriber;
import io.smallrye.mutiny.subscription.UniSubscription;

public final class UniToMultiPublisher<T> extends AbstractMulti<T> {
public final class UniToMultiPublisher<T> implements Publisher<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason to not be a Multi directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it's safer to have the Infrastructure involved again


private final Uni<T> uni;

Expand All @@ -30,6 +30,7 @@ private static class UniToMultiSubscription<T> implements UniSubscription, Subsc

private final Uni<T> uni;
private final Subscriber<? super T> downstream;

private final AtomicReference<UniSubscription> upstream = new AtomicReference<>();

private UniToMultiSubscription(Uni<T> uni, Subscriber<? super T> downstream) {
Expand All @@ -39,27 +40,22 @@ private UniToMultiSubscription(Uni<T> uni, Subscriber<? super T> downstream) {

@Override
public void cancel() {
UniSubscription sub;
synchronized (upstream) {
sub = upstream.getAndSet(CANCELLED);
}
UniSubscription sub = upstream.getAndSet(CANCELLED);
if (sub != null) {
sub.cancel();
}
}

@Override
public void request(long n) {
synchronized (upstream) {
if (n <= 0L) {
downstream.onError(new IllegalArgumentException("Invalid request"));
return;
}
if (upstream.get() == CANCELLED) {
return;
}
AbstractUni.subscribe(uni, this);
if (n <= 0L) {
downstream.onError(new IllegalArgumentException("Invalid request"));
return;
}
if (upstream.get() == CANCELLED) {
return;
}
AbstractUni.subscribe(uni, this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public UniConvert<T> convert() {

@Override
public Multi<T> toMulti() {
return new UniToMultiPublisher<>(this);
return Multi.createFrom().safePublisher(new UniToMultiPublisher<>(this));
}

@Override
Expand Down