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

autodispose2.AutoDispose Maven / Gradle / Ivy

There is a newer version: 2.2.1
Show newest version
/*
 * Copyright (C) 2019. 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 autodispose2;

import static autodispose2.AutoDisposeUtil.checkNotNull;
import static autodispose2.Scopes.completableOf;

import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.CompletableObserver;
import io.reactivex.rxjava3.core.CompletableSource;
import io.reactivex.rxjava3.core.Flowable;
import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.MaybeObserver;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableConverter;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleObserver;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.functions.BiConsumer;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.observers.TestObserver;
import io.reactivex.rxjava3.parallel.ParallelFlowable;
import io.reactivex.rxjava3.subscribers.TestSubscriber;
import org.reactivestreams.Subscriber;

/**
 * Factories for autodispose converters that can be used with RxJava types' corresponding {@code
 * as(...)} methods to transform them into auto-disposing streams.
 *
 * 

There are several static {@code autoDisposable(...)} entry points, with the most basic being a * simple {@link #autoDisposable(CompletableSource)}. The provided {@link CompletableSource} 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 onComplete emission of * this {@link Completable}. * *

This is structured in such a way to be friendly to autocompletion in IDEs, where the * no-parameter generic method will autocomplete with the appropriate generic parameters in Java <7, * or implicitly in >=8. * * @see Flowable#to(io.reactivex.rxjava3.core.FlowableConverter) * @see Observable#to(io.reactivex.rxjava3.core.ObservableConverter) * @see Maybe#to(io.reactivex.rxjava3.core.MaybeConverter) * @see Single#to(io.reactivex.rxjava3.core.SingleConverter) * @see Completable#to(io.reactivex.rxjava3.core.CompletableConverter) */ public final class AutoDispose { /** * Entry point for auto-disposing streams from a {@link ScopeProvider}. * *

Example usage: * *


   *   Observable.just(1)
   *        .to(autoDisposable(scope)) // Static import
   *        .subscribe(...)
   * 
* * @param provider the target scope provider * @param the stream type. * @return an {@link AutoDisposeConverter} to transform with operators like {@link * Observable#to(ObservableConverter)} */ public static AutoDisposeConverter autoDisposable(final ScopeProvider provider) { checkNotNull(provider, "provider == null"); return autoDisposable(completableOf(provider)); } /** * Entry point for auto-disposing streams from a {@link CompletableSource}. * *

Example usage: * *


   *   Observable.just(1)
   *        .to(autoDisposable(scope)) // Static import
   *        .subscribe(...)
   * 
* * @param scope the target scope * @param the stream type. * @return an {@link AutoDisposeConverter} to transform with operators like {@link * Observable#to(ObservableConverter)} */ public static AutoDisposeConverter autoDisposable(final CompletableSource scope) { checkNotNull(scope, "scope == null"); return new AutoDisposeConverter() { @Override public ParallelFlowableSubscribeProxy apply(final ParallelFlowable upstream) { if (!AutoDisposePlugins.hideProxies) { return new AutoDisposeParallelFlowable<>(upstream, scope); } return subscribers -> new AutoDisposeParallelFlowable<>(upstream, scope).subscribe(subscribers); } @Override public CompletableSubscribeProxy apply(final Completable upstream) { if (!AutoDisposePlugins.hideProxies) { return new AutoDisposeCompletable(upstream, scope); } return new CompletableSubscribeProxy() { @Override public Disposable subscribe() { return new AutoDisposeCompletable(upstream, scope).subscribe(); } @Override public Disposable subscribe(Action action) { return new AutoDisposeCompletable(upstream, scope).subscribe(action); } @Override public Disposable subscribe(Action action, Consumer onError) { return new AutoDisposeCompletable(upstream, scope).subscribe(action, onError); } @Override public void subscribe(CompletableObserver observer) { new AutoDisposeCompletable(upstream, scope).subscribe(observer); } @Override public E subscribeWith(E observer) { return new AutoDisposeCompletable(upstream, scope).subscribeWith(observer); } @Override public TestObserver test() { TestObserver observer = new TestObserver<>(); subscribe(observer); return observer; } @Override public TestObserver test(boolean dispose) { TestObserver observer = new TestObserver<>(); if (dispose) { observer.dispose(); } subscribe(observer); return observer; } }; } @Override public FlowableSubscribeProxy apply(final Flowable upstream) { if (!AutoDisposePlugins.hideProxies) { return new AutoDisposeFlowable<>(upstream, scope); } return new FlowableSubscribeProxy() { @Override public Disposable subscribe() { return new AutoDisposeFlowable<>(upstream, scope).subscribe(); } @Override public Disposable subscribe(Consumer onNext) { return new AutoDisposeFlowable<>(upstream, scope).subscribe(onNext); } @Override public Disposable subscribe( Consumer onNext, Consumer onError) { return new AutoDisposeFlowable<>(upstream, scope).subscribe(onNext, onError); } @Override public Disposable subscribe( Consumer onNext, Consumer onError, Action onComplete) { return new AutoDisposeFlowable<>(upstream, scope) .subscribe(onNext, onError, onComplete); } @Override public void subscribe(Subscriber observer) { new AutoDisposeFlowable<>(upstream, scope).subscribe(observer); } @Override public > E subscribeWith(E observer) { return new AutoDisposeFlowable<>(upstream, scope).subscribeWith(observer); } @Override public TestSubscriber test() { TestSubscriber ts = new TestSubscriber<>(); subscribe(ts); return ts; } @Override public TestSubscriber test(long initialRequest) { TestSubscriber ts = new TestSubscriber<>(initialRequest); subscribe(ts); return ts; } @Override public TestSubscriber test(long initialRequest, boolean cancel) { TestSubscriber ts = new TestSubscriber<>(initialRequest); if (cancel) { ts.cancel(); } subscribe(ts); return ts; } }; } @Override public MaybeSubscribeProxy apply(final Maybe upstream) { if (!AutoDisposePlugins.hideProxies) { return new AutoDisposeMaybe<>(upstream, scope); } return new MaybeSubscribeProxy() { @Override public Disposable subscribe() { return new AutoDisposeMaybe<>(upstream, scope).subscribe(); } @Override public Disposable subscribe(Consumer onSuccess) { return new AutoDisposeMaybe<>(upstream, scope).subscribe(onSuccess); } @Override public Disposable subscribe( Consumer onSuccess, Consumer onError) { return new AutoDisposeMaybe<>(upstream, scope).subscribe(onSuccess, onError); } @Override public Disposable subscribe( Consumer onSuccess, Consumer onError, Action onComplete) { return new AutoDisposeMaybe<>(upstream, scope) .subscribe(onSuccess, onError, onComplete); } @Override public void subscribe(MaybeObserver observer) { new AutoDisposeMaybe<>(upstream, scope).subscribe(observer); } @Override public > E subscribeWith(E observer) { return new AutoDisposeMaybe<>(upstream, scope).subscribeWith(observer); } @Override public TestObserver test() { TestObserver observer = new TestObserver<>(); subscribe(observer); return observer; } @Override public TestObserver test(boolean dispose) { TestObserver observer = new TestObserver<>(); if (dispose) { observer.dispose(); } subscribe(observer); return observer; } }; } @Override public ObservableSubscribeProxy apply(final Observable upstream) { if (!AutoDisposePlugins.hideProxies) { return new AutoDisposeObservable<>(upstream, scope); } return new ObservableSubscribeProxy() { @Override public Disposable subscribe() { return new AutoDisposeObservable<>(upstream, scope).subscribe(); } @Override public Disposable subscribe(Consumer onNext) { return new AutoDisposeObservable<>(upstream, scope).subscribe(onNext); } @Override public Disposable subscribe( Consumer onNext, Consumer onError) { return new AutoDisposeObservable<>(upstream, scope).subscribe(onNext, onError); } @Override public Disposable subscribe( Consumer onNext, Consumer onError, Action onComplete) { return new AutoDisposeObservable<>(upstream, scope) .subscribe(onNext, onError, onComplete); } @Override public void subscribe(Observer observer) { new AutoDisposeObservable<>(upstream, scope).subscribe(observer); } @Override public > E subscribeWith(E observer) { return new AutoDisposeObservable<>(upstream, scope).subscribeWith(observer); } @Override public TestObserver test() { TestObserver observer = new TestObserver<>(); subscribe(observer); return observer; } @Override public TestObserver test(boolean dispose) { TestObserver observer = new TestObserver<>(); if (dispose) { observer.dispose(); } subscribe(observer); return observer; } }; } @Override public SingleSubscribeProxy apply(final Single upstream) { if (!AutoDisposePlugins.hideProxies) { return new AutoDisposeSingle<>(upstream, scope); } return new SingleSubscribeProxy() { @Override public Disposable subscribe() { return new AutoDisposeSingle<>(upstream, scope).subscribe(); } @Override public Disposable subscribe(Consumer onSuccess) { return new AutoDisposeSingle<>(upstream, scope).subscribe(onSuccess); } @Override public Disposable subscribe(BiConsumer biConsumer) { return new AutoDisposeSingle<>(upstream, scope).subscribe(biConsumer); } @Override public Disposable subscribe( Consumer onSuccess, Consumer onError) { return new AutoDisposeSingle<>(upstream, scope).subscribe(onSuccess, onError); } @Override public void subscribe(SingleObserver observer) { new AutoDisposeSingle<>(upstream, scope).subscribe(observer); } @Override public > E subscribeWith(E observer) { return new AutoDisposeSingle<>(upstream, scope).subscribeWith(observer); } @Override public TestObserver test() { TestObserver observer = new TestObserver<>(); subscribe(observer); return observer; } @Override public TestObserver test(boolean dispose) { TestObserver observer = new TestObserver<>(); if (dispose) { observer.dispose(); } subscribe(observer); return observer; } }; } }; } private AutoDispose() { throw new AssertionError("No instances"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy