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

rx.internal.operators.SingleOnSubscribeUsing Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/**
 * Copyright 2016 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package rx.internal.operators;

import java.util.Arrays;

import rx.*;
import rx.exceptions.*;
import rx.functions.*;
import rx.plugins.RxJavaHooks;

/**
 * Generates a resource, derives a Single from it and disposes that resource once the
 * Single terminates.
 * @param  the value type of the Single
 * @param  the resource type
 */
public final class SingleOnSubscribeUsing implements Single.OnSubscribe {
    final Func0 resourceFactory;
    final Func1> singleFactory;
    final Action1 disposeAction; 
    final boolean disposeEagerly;

    public SingleOnSubscribeUsing(Func0 resourceFactory,
            Func1> observableFactory,
            Action1 disposeAction, boolean disposeEagerly) {
        this.resourceFactory = resourceFactory;
        this.singleFactory = observableFactory;
        this.disposeAction = disposeAction;
        this.disposeEagerly = disposeEagerly;
    }
    
    @Override
    public void call(final SingleSubscriber child) {
        final Resource resource; // NOPMD 
        
        try {
            resource = resourceFactory.call();
        } catch (Throwable ex) {
            Exceptions.throwIfFatal(ex);
            child.onError(ex);
            return;
        }
        
        Single single;
        
        try {
            single = singleFactory.call(resource);
        } catch (Throwable ex) {
            handleSubscriptionTimeError(child, resource, ex);
            return;
        }
        
        if (single == null) {
            handleSubscriptionTimeError(child, resource, new NullPointerException("The single"));
            return;
        }
        
        SingleSubscriber parent = new SingleSubscriber() {
            @Override
            public void onSuccess(T value) {
                if (disposeEagerly) {
                    try {
                        disposeAction.call(resource);
                    } catch (Throwable ex) {
                        Exceptions.throwIfFatal(ex);
                        
                        child.onError(ex);
                        return;
                    }
                }
                
                child.onSuccess(value);
                
                if (!disposeEagerly) {
                    try {
                        disposeAction.call(resource);
                    } catch (Throwable ex2) {
                        Exceptions.throwIfFatal(ex2);
                        RxJavaHooks.onError(ex2);
                    }
                }
            }
            
            @Override
            public void onError(Throwable error) {
                handleSubscriptionTimeError(child, resource, error);
            }
        };
        child.add(parent);
        
        single.subscribe(parent);
    }

    void handleSubscriptionTimeError(SingleSubscriber t, Resource resource, Throwable ex) {
        Exceptions.throwIfFatal(ex);

        if (disposeEagerly) {
            try {
                disposeAction.call(resource);
            } catch (Throwable ex2) {
                Exceptions.throwIfFatal(ex2);
                ex = new CompositeException(Arrays.asList(ex, ex2));
            }
        }
        
        t.onError(ex);
        
        if (!disposeEagerly) {
            try {
                disposeAction.call(resource);
            } catch (Throwable ex2) {
                Exceptions.throwIfFatal(ex2);
                RxJavaHooks.onError(ex2);
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy