All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.reactive.client.AsyncResultUni Maven / Gradle / Ivy

There is a newer version: 3.17.5
Show newest version
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 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