org.jboss.resteasy.reactive.client.AsyncResultUni Maven / Gradle / Ivy
package org.jboss.resteasy.reactive.client;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.infrastructure.Infrastructure;
import io.smallrye.mutiny.operators.AbstractUni;
import io.smallrye.mutiny.subscription.UniSubscriber;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
public class AsyncResultUni extends AbstractUni implements Uni {
private final Consumer>> subscriptionConsumer;
public static Uni toUni(Consumer>> subscriptionConsumer) {
return new AsyncResultUni<>(subscriptionConsumer);
}
public AsyncResultUni(Consumer>> subscriptionConsumer) {
this.subscriptionConsumer = Infrastructure.decorate(subscriptionConsumer);
}
@Override
public void subscribe(UniSubscriber super T> downstream) {
AtomicBoolean terminated = new AtomicBoolean();
downstream.onSubscribe(() -> terminated.set(true));
if (!terminated.get()) {
try {
subscriptionConsumer.accept(ar -> {
if (!terminated.getAndSet(true)) {
if (ar.succeeded()) {
T val = ar.result();
downstream.onItem(val);
} else if (ar.failed()) {
downstream.onFailure(ar.cause());
}
}
});
} catch (Exception e) {
if (!terminated.getAndSet(true)) {
downstream.onFailure(e);
}
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy