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

io.neow3j.protocol.core.RemoteCall Maven / Gradle / Ivy

package io.neow3j.protocol.core;

import io.neow3j.utils.Async;
import io.reactivex.Observable;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;

/**
 * A common type for wrapping remote requests.
 *
 * @param  Our return type.
 */
public class RemoteCall {

    private Callable callable;

    public RemoteCall(Callable callable) {
        this.callable = callable;
    }

    /**
     * Perform request synchronously.
     *
     * @return result of enclosed function
     * @throws Exception if the function throws an exception
     */
    public T send() throws Exception {
        return callable.call();
    }

    /**
     * Perform request asynchronously with a future.
     *
     * @return a future containing our function
     */
    public CompletableFuture sendAsync() {
        return Async.run(this::send);
    }

    /**
     * Provide an observable to emit result from our function.
     *
     * @return an observable
     */
    public Observable observable() {
        return Observable.create(
                subscriber -> {
                    try {
                        subscriber.onNext(send());
                        subscriber.onComplete();
                    } catch (Exception e) {
                        subscriber.onError(e);
                    }
                }
        );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy