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

com.salesforce.rxgrpc.stub.SubscribeOnlyOnceSingleOperator Maven / Gradle / Ivy

There is a newer version: 1.2.4
Show newest version
/*
 *  Copyright (c) 2019, Salesforce.com, Inc.
 *  All rights reserved.
 *  Licensed under the BSD 3-Clause license.
 *  For full license text, see LICENSE.txt file in the repo root  or https://opensource.org/licenses/BSD-3-Clause
 */

package com.salesforce.rxgrpc.stub;

import io.reactivex.SingleObserver;
import io.reactivex.SingleOperator;
import io.reactivex.disposables.Disposable;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * SubscribeOnlyOnceSingleOperator throws an exception if a user attempts to subscribe more than once to a
 * {@link io.reactivex.Single}.
 *
 * @param  T
 */
public class SubscribeOnlyOnceSingleOperator implements SingleOperator {
    private AtomicBoolean subscribedOnce = new AtomicBoolean(false);

    @Override
    public SingleObserver apply(final SingleObserver observer) {
        return new SingleObserver() {
            @Override
            public void onSubscribe(Disposable d) {
                if (subscribedOnce.getAndSet(true)) {
                    throw new NullPointerException("You cannot directly subscribe to a gRPC service multiple times " +
                            "concurrently. Use Flowable.share() instead.");
                } else {
                    observer.onSubscribe(d);
                }
            }

            @Override
            public void onSuccess(T t) {
                observer.onSuccess(t);
            }

            @Override
            public void onError(Throwable e) {
                observer.onError(e);
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy