| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.subscribers; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.Subscription; |
| 20 |
|
| 21 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 22 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 23 |
import io.reactivex.rxjava3.internal.disposables.ListCompositeDisposable; |
| 24 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 25 |
import io.reactivex.rxjava3.internal.util.EndConsumerHelper; |
| 26 |
|
| 27 |
/** |
| 28 |
* An abstract Subscriber that allows asynchronous cancellation of its |
| 29 |
* subscription and associated resources. |
| 30 |
* |
| 31 |
* <p>All pre-implemented final methods are thread-safe. |
| 32 |
* |
| 33 |
* <p>To release the associated resources, one has to call {@link #dispose()} |
| 34 |
* in {@code onError()} and {@code onComplete()} explicitly. |
| 35 |
* |
| 36 |
* <p>Use {@link #add(Disposable)} to associate resources (as {@link io.reactivex.rxjava3.disposables.Disposable Disposable}s) |
| 37 |
* with this {@code ResourceSubscriber} that will be cleaned up when {@link #dispose()} is called. |
| 38 |
* Removing previously associated resources is not possible but one can create a |
| 39 |
* {@link io.reactivex.rxjava3.disposables.CompositeDisposable CompositeDisposable}, associate it with this |
| 40 |
* {@code ResourceSubscriber} and then add/remove resources to/from the {@code CompositeDisposable} |
| 41 |
* freely. |
| 42 |
* |
| 43 |
* <p>The default {@link #onStart()} requests {@link Long#MAX_VALUE} by default. Override |
| 44 |
* the method to request a custom <em>positive</em> amount. Use the protected {@link #request(long)} |
| 45 |
* to request more items and {@link #dispose()} to cancel the sequence from within an |
| 46 |
* {@code onNext} implementation. |
| 47 |
* |
| 48 |
* <p>Note that calling {@link #request(long)} from {@link #onStart()} may trigger |
| 49 |
* an immediate, asynchronous emission of data to {@link #onNext(Object)}. Make sure |
| 50 |
* all initialization happens before the call to {@code request()} in {@code onStart()}. |
| 51 |
* Calling {@link #request(long)} inside {@link #onNext(Object)} can happen at any time |
| 52 |
* because by design, {@code onNext} calls from upstream are non-reentrant and non-overlapping. |
| 53 |
* |
| 54 |
* <p>Like all other consumers, {@code ResourceSubscriber} can be subscribed only once. |
| 55 |
* Any subsequent attempt to subscribe it to a new source will yield an |
| 56 |
* {@link IllegalStateException} with message {@code "It is not allowed to subscribe with a(n) <class name> multiple times."}. |
| 57 |
* |
| 58 |
* <p>Implementation of {@link #onStart()}, {@link #onNext(Object)}, {@link #onError(Throwable)} |
| 59 |
* and {@link #onComplete()} are not allowed to throw any unchecked exceptions. |
| 60 |
* If for some reason this can't be avoided, use {@link io.reactivex.rxjava3.core.Flowable#safeSubscribe(org.reactivestreams.Subscriber)} |
| 61 |
* instead of the standard {@code subscribe()} method. |
| 62 |
* |
| 63 |
* <p>Example<pre><code> |
| 64 |
* Disposable d = |
| 65 |
* Flowable.range(1, 5) |
| 66 |
* .subscribeWith(new ResourceSubscriber<Integer>() { |
| 67 |
* @Override public void onStart() { |
| 68 |
* add(Schedulers.single() |
| 69 |
* .scheduleDirect(() -> System.out.println("Time!"), |
| 70 |
* 2, TimeUnit.SECONDS)); |
| 71 |
* request(1); |
| 72 |
* } |
| 73 |
* @Override public void onNext(Integer t) { |
| 74 |
* if (t == 3) { |
| 75 |
* dispose(); |
| 76 |
* } |
| 77 |
* System.out.println(t); |
| 78 |
* request(1); |
| 79 |
* } |
| 80 |
* @Override public void onError(Throwable t) { |
| 81 |
* t.printStackTrace(); |
| 82 |
* dispose(); |
| 83 |
* } |
| 84 |
* @Override public void onComplete() { |
| 85 |
* System.out.println("Done!"); |
| 86 |
* dispose(); |
| 87 |
* } |
| 88 |
* }); |
| 89 |
* // ... |
| 90 |
* d.dispose(); |
| 91 |
* </code></pre> |
| 92 |
* |
| 93 |
* @param <T> the value type |
| 94 |
*/ |
| 95 |
public abstract class ResourceSubscriber<T> implements FlowableSubscriber<T>, Disposable { |
| 96 |
/** The active subscription. */ |
| 97 |
private final AtomicReference<Subscription> upstream = new AtomicReference<>(); |
| 98 |
|
| 99 |
/** The resource composite, can never be null. */ |
| 100 |
private final ListCompositeDisposable resources = new ListCompositeDisposable(); |
| 101 |
|
| 102 |
/** Remembers the request(n) counts until a subscription arrives. */ |
| 103 |
private final AtomicLong missedRequested = new AtomicLong(); |
| 104 |
|
| 105 |
/** |
| 106 |
* Adds a resource to this {@code ResourceSubscriber}. |
| 107 |
* |
| 108 |
* @param resource the resource to add |
| 109 |
* |
| 110 |
* @throws NullPointerException if {@code resource} is {@code null} |
| 111 |
*/ |
| 112 |
public final void add(Disposable resource) { |
| 113 |
Objects.requireNonNull(resource, "resource is null"); |
| 114 |
resources.add(resource); |
| 115 |
} |
| 116 |
|
| 117 |
@Override |
| 118 |
public final void onSubscribe(Subscription s) { |
| 119 |
if (EndConsumerHelper.setOnce(this.upstream, s, getClass())) { |
| 120 |
long r = missedRequested.getAndSet(0L); |
| 121 |
if (r != 0L) { |
| 122 |
s.request(r); |
| 123 |
} |
| 124 |
onStart(); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Called once the upstream sets a {@link Subscription} on this {@code ResourceSubscriber}. |
| 130 |
* |
| 131 |
* <p>You can perform initialization at this moment. The default |
| 132 |
* implementation requests {@link Long#MAX_VALUE} from upstream. |
| 133 |
*/ |
| 134 |
protected void onStart() { |
| 135 |
request(Long.MAX_VALUE); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Request the specified amount of elements from upstream. |
| 140 |
* |
| 141 |
* <p>This method can be called before the upstream calls {@link #onSubscribe(Subscription)}. |
| 142 |
* When the subscription happens, all missed requests are requested. |
| 143 |
* |
| 144 |
* @param n the request amount, must be positive |
| 145 |
*/ |
| 146 |
protected final void request(long n) { |
| 147 |
SubscriptionHelper.deferredRequest(upstream, missedRequested, n); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Cancels the subscription (if any) and disposes the resources associated with |
| 152 |
* this {@code ResourceSubscriber} (if any). |
| 153 |
* |
| 154 |
* <p>This method can be called before the upstream calls {@link #onSubscribe(Subscription)} at which |
| 155 |
* case the {@link Subscription} will be immediately cancelled. |
| 156 |
*/ |
| 157 |
@Override |
| 158 |
public final void dispose() { |
| 159 |
if (SubscriptionHelper.cancel(upstream)) { |
| 160 |
resources.dispose(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Returns true if this {@code ResourceSubscriber} has been disposed/cancelled. |
| 166 |
* @return true if this {@code ResourceSubscriber} has been disposed/cancelled |
| 167 |
*/ |
| 168 |
@Override |
| 169 |
public final boolean isDisposed() { |
| 170 |
return upstream.get() == SubscriptionHelper.CANCELLED; |
| 171 |
} |
| 172 |
} |