|
| 1 | +/** |
| 2 | + * Copyright 2016 Netflix, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package rx.observers; |
| 17 | + |
| 18 | +import java.util.concurrent.atomic.AtomicReference; |
| 19 | + |
| 20 | +import rx.Completable.CompletableSubscriber; |
| 21 | +import rx.Subscription; |
| 22 | +import rx.annotations.Experimental; |
| 23 | +import rx.internal.util.RxJavaPluginUtils; |
| 24 | + |
| 25 | +/** |
| 26 | + * An abstract base class for CompletableSubscriber implementations that want to expose an unsubscription |
| 27 | + * capability. |
| 28 | + * <p> |
| 29 | + * Calling {@link #unsubscribe()} and {@link #isUnsubscribed()} is threadsafe and can happen at any time, even |
| 30 | + * before or during an active {@link rx.Completable#subscribe(CompletableSubscriber)} call. |
| 31 | + * <p> |
| 32 | + * Override the {@link #onStart()} method to execute custom logic on the very first successful onSubscribe call. |
| 33 | + * <p> |
| 34 | + * If one wants to remain consistent regarding {@link #isUnsubscribed()} and being terminated, |
| 35 | + * the {@link #clear()} method should be called from the implementing onError and onCompleted methods. |
| 36 | + * <p> |
| 37 | + * <pre><code> |
| 38 | + * public final class MyCompletableSubscriber extends AsyncCompletableSubscriber { |
| 39 | + * @Override |
| 40 | + * public void onStart() { |
| 41 | + * System.out.println("Started!"); |
| 42 | + * } |
| 43 | + * |
| 44 | + * @Override |
| 45 | + * public void onCompleted() { |
| 46 | + * System.out.println("Completed!"); |
| 47 | + * clear(); |
| 48 | + * } |
| 49 | + * |
| 50 | + * @Override |
| 51 | + * public void onError(Throwable e) { |
| 52 | + * e.printStackTrace(); |
| 53 | + * clear(); |
| 54 | + * } |
| 55 | + * } |
| 56 | + * </code></pre> |
| 57 | + * @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number) |
| 58 | + */ |
| 59 | +@Experimental |
| 60 | +public abstract class AsyncCompletableSubscriber implements CompletableSubscriber, Subscription { |
| 61 | + |
| 62 | + /** |
| 63 | + * Holds onto a deferred subscription and allows asynchronous cancellation before the call |
| 64 | + * to onSubscribe() by the upstream. |
| 65 | + */ |
| 66 | + private final AtomicReference<Subscription> upstream = new AtomicReference<Subscription>(); |
| 67 | + |
| 68 | + @Override |
| 69 | + public final void onSubscribe(Subscription d) { |
| 70 | + if (!upstream.compareAndSet(null, d)) { |
| 71 | + d.unsubscribe(); |
| 72 | + if (upstream.get() != UNSUBSCRIBED) { |
| 73 | + RxJavaPluginUtils.handleException(new IllegalStateException("Subscription already set!")); |
| 74 | + } |
| 75 | + } else { |
| 76 | + onStart(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Called before the first onSubscribe() call succeeds. |
| 82 | + */ |
| 83 | + protected void onStart() { |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public final boolean isUnsubscribed() { |
| 88 | + return upstream.get() == UNSUBSCRIBED; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Call to clear the upstream's subscription without unsubscribing it. |
| 93 | + */ |
| 94 | + protected final void clear() { |
| 95 | + upstream.set(UNSUBSCRIBED); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public final void unsubscribe() { |
| 100 | + Subscription current = upstream.get(); |
| 101 | + if (current != UNSUBSCRIBED) { |
| 102 | + current = upstream.getAndSet(UNSUBSCRIBED); |
| 103 | + if (current != null && current != UNSUBSCRIBED) { |
| 104 | + current.unsubscribe(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Indicates the unsubscribed state. |
| 112 | + */ |
| 113 | + static final Unsubscribed UNSUBSCRIBED = new Unsubscribed(); |
| 114 | + |
| 115 | + static final class Unsubscribed implements Subscription { |
| 116 | + |
| 117 | + @Override |
| 118 | + public void unsubscribe() { |
| 119 | + // deliberately no op |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public boolean isUnsubscribed() { |
| 124 | + return true; |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | +} |
0 commit comments