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

com.uber.autodispose.ObservableScoper Maven / Gradle / Ivy

There is a newer version: 1.4.1
Show newest version
/*
 * Copyright (c) 2017. Uber Technologies
 *
 * 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 com.uber.autodispose;

import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * Entry point for auto-disposing {@link Observable}s.
 * 

* The basic flow stencil might look like this: *


 *   myThingObservable
 *        .to(new ObservableScoper(...))
 *        .subscribe(...)
 * 
*

* There are several constructor overloads, with the most basic being a simple {@link * #ObservableScoper(Maybe)}. The provided {@link Maybe} is ultimately what every scope resolves to * under the hood, and AutoDispose has some built-in understanding for predefined types. The scope * is considered ended upon onSuccess emission of this {@link Maybe}. The most common use case would * probably be {@link #ObservableScoper(ScopeProvider)}. * * @param the stream type. */ public class ObservableScoper extends Scoper implements Function, ObservableSubscribeProxy> { public ObservableScoper(ScopeProvider provider) { super(provider); } public ObservableScoper(LifecycleScopeProvider provider) { super(provider); } public ObservableScoper(Maybe lifecycle) { super(lifecycle); } @Override public ObservableSubscribeProxy apply(final Observable observableSource) throws Exception { return new ObservableSubscribeProxy() { @Override public Disposable subscribe() { return new AutoDisposeObservable<>(observableSource, scope()).subscribe(); } @Override public Disposable subscribe(Consumer onNext) { return new AutoDisposeObservable<>(observableSource, scope()).subscribe(onNext); } @Override public Disposable subscribe(Consumer onNext, Consumer onError) { return new AutoDisposeObservable<>(observableSource, scope()).subscribe(onNext, onError); } @Override public Disposable subscribe(Consumer onNext, Consumer onError, Action onComplete) { return new AutoDisposeObservable<>(observableSource, scope()).subscribe(onNext, onError, onComplete); } @Override public Disposable subscribe(Consumer onNext, Consumer onError, Action onComplete, Consumer onSubscribe) { return new AutoDisposeObservable<>(observableSource, scope()).subscribe(onNext, onError, onComplete, onSubscribe); } @Override public void subscribe(Observer observer) { new AutoDisposeObservable<>(observableSource, scope()).subscribe(observer); } @Override public > E subscribeWith(E observer) { return new AutoDisposeObservable<>(observableSource, scope()).subscribeWith(observer); } }; } static final class AutoDisposeObservable extends Observable { private final ObservableSource source; private final Maybe scope; AutoDisposeObservable(ObservableSource source, Maybe scope) { this.source = source; this.scope = scope; } @Override protected void subscribeActual(Observer observer) { source.subscribe(new AutoDisposingObserverImpl<>(scope, observer)); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy