/** * 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.observers; import java.util.concurrent.atomic.AtomicReference; import rx.CompletableSubscriber; import rx.Subscription; import rx.annotations.Experimental; import rx.plugins.RxJavaHooks; /** * An abstract base class for CompletableSubscriber implementations that want to expose an unsubscription * capability. *
* Calling {@link #unsubscribe()} and {@link #isUnsubscribed()} is threadsafe and can happen at any time, even * before or during an active {@link rx.Completable#subscribe(CompletableSubscriber)} call. *
* Override the {@link #onStart()} method to execute custom logic on the very first successful onSubscribe call. *
* If one wants to remain consistent regarding {@link #isUnsubscribed()} and being terminated, * the {@link #clear()} method should be called from the implementing onError and onCompleted methods. *
*
* public final class MyCompletableSubscriber extends AsyncCompletableSubscriber {
* @Override
* public void onStart() {
* System.out.println("Started!");
* }
*
* @Override
* public void onCompleted() {
* System.out.println("Completed!");
* clear();
* }
*
* @Override
* public void onError(Throwable e) {
* e.printStackTrace();
* clear();
* }
* }
*
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number)
*/
@Experimental
public abstract class AsyncCompletableSubscriber implements CompletableSubscriber, Subscription {
/**
* Indicates the unsubscribed state.
*/
static final Unsubscribed UNSUBSCRIBED = new Unsubscribed();
/**
* Holds onto a deferred subscription and allows asynchronous cancellation before the call
* to onSubscribe() by the upstream.
*/
private final AtomicReference