| 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 org.reactivestreams.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 19 |
import io.reactivex.rxjava3.core.FlowableSubscriber; |
| 20 |
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper; |
| 21 |
import io.reactivex.rxjava3.internal.util.*; |
| 22 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 23 |
|
| 24 |
/** |
| 25 |
* Serializes access to the {@link Subscriber#onNext(Object)}, {@link Subscriber#onError(Throwable)} and |
| 26 |
* {@link Subscriber#onComplete()} methods of another {@link Subscriber}. |
| 27 |
* |
| 28 |
* <p>Note that {@link #onSubscribe(Subscription)} is not serialized in respect of the other methods so |
| 29 |
* make sure the {@code onSubscribe} is called with a non-{@code null} {@link Subscription} |
| 30 |
* before any of the other methods are called. |
| 31 |
* |
| 32 |
* <p>The implementation assumes that the actual {@code Subscriber}'s methods don't throw. |
| 33 |
* |
| 34 |
* @param <T> the value type |
| 35 |
*/ |
| 36 |
public final class SerializedSubscriber<T> implements FlowableSubscriber<T>, Subscription { |
| 37 |
final Subscriber<? super T> downstream; |
| 38 |
final boolean delayError; |
| 39 |
|
| 40 |
static final int QUEUE_LINK_SIZE = 4; |
| 41 |
|
| 42 |
Subscription upstream; |
| 43 |
|
| 44 |
boolean emitting; |
| 45 |
AppendOnlyLinkedArrayList<Object> queue; |
| 46 |
|
| 47 |
volatile boolean done; |
| 48 |
|
| 49 |
/** |
| 50 |
* Construct a {@code SerializedSubscriber} by wrapping the given actual {@link Subscriber}. |
| 51 |
* @param downstream the actual {@code Subscriber}, not null (not verified) |
| 52 |
*/ |
| 53 |
public SerializedSubscriber(Subscriber<? super T> downstream) { |
| 54 |
this(downstream, false); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Construct a {@code SerializedSubscriber} by wrapping the given actual {@link Subscriber} and |
| 59 |
* optionally delaying the errors till all regular values have been emitted |
| 60 |
* from the internal buffer. |
| 61 |
* @param actual the actual {@code Subscriber}, not {@code null} (not verified) |
| 62 |
* @param delayError if {@code true}, errors are emitted after regular values have been emitted |
| 63 |
*/ |
| 64 |
public SerializedSubscriber(@NonNull Subscriber<? super T> actual, boolean delayError) { |
| 65 |
this.downstream = actual; |
| 66 |
this.delayError = delayError; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public void onSubscribe(@NonNull Subscription s) { |
| 71 |
if (SubscriptionHelper.validate(this.upstream, s)) { |
| 72 |
this.upstream = s; |
| 73 |
downstream.onSubscribe(this); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void onNext(@NonNull T t) { |
| 79 |
if (done) { |
| 80 |
return; |
| 81 |
} |
| 82 |
if (t == null) { |
| 83 |
upstream.cancel(); |
| 84 |
onError(ExceptionHelper.createNullPointerException("onNext called with a null value.")); |
| 85 |
return; |
| 86 |
} |
| 87 |
synchronized (this) { |
| 88 |
if (done) { |
| 89 |
return; |
| 90 |
} |
| 91 |
if (emitting) { |
| 92 |
AppendOnlyLinkedArrayList<Object> q = queue; |
| 93 |
if (q == null) { |
| 94 |
q = new AppendOnlyLinkedArrayList<>(QUEUE_LINK_SIZE); |
| 95 |
queue = q; |
| 96 |
} |
| 97 |
q.add(NotificationLite.next(t)); |
| 98 |
return; |
| 99 |
} |
| 100 |
emitting = true; |
| 101 |
} |
| 102 |
|
| 103 |
downstream.onNext(t); |
| 104 |
|
| 105 |
emitLoop(); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void onError(Throwable t) { |
| 110 |
if (done) { |
| 111 |
RxJavaPlugins.onError(t); |
| 112 |
return; |
| 113 |
} |
| 114 |
boolean reportError; |
| 115 |
synchronized (this) { |
| 116 |
if (done) { |
| 117 |
reportError = true; |
| 118 |
} else |
| 119 |
if (emitting) { |
| 120 |
done = true; |
| 121 |
AppendOnlyLinkedArrayList<Object> q = queue; |
| 122 |
if (q == null) { |
| 123 |
q = new AppendOnlyLinkedArrayList<>(QUEUE_LINK_SIZE); |
| 124 |
queue = q; |
| 125 |
} |
| 126 |
Object err = NotificationLite.error(t); |
| 127 |
if (delayError) { |
| 128 |
q.add(err); |
| 129 |
} else { |
| 130 |
q.setFirst(err); |
| 131 |
} |
| 132 |
return; |
| 133 |
} else { |
| 134 |
done = true; |
| 135 |
emitting = true; |
| 136 |
reportError = false; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if (reportError) { |
| 141 |
RxJavaPlugins.onError(t); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
downstream.onError(t); |
| 146 |
// no need to loop because this onError is the last event |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void onComplete() { |
| 151 |
if (done) { |
| 152 |
return; |
| 153 |
} |
| 154 |
synchronized (this) { |
| 155 |
if (done) { |
| 156 |
return; |
| 157 |
} |
| 158 |
if (emitting) { |
| 159 |
AppendOnlyLinkedArrayList<Object> q = queue; |
| 160 |
if (q == null) { |
| 161 |
q = new AppendOnlyLinkedArrayList<>(QUEUE_LINK_SIZE); |
| 162 |
queue = q; |
| 163 |
} |
| 164 |
q.add(NotificationLite.complete()); |
| 165 |
return; |
| 166 |
} |
| 167 |
done = true; |
| 168 |
emitting = true; |
| 169 |
} |
| 170 |
|
| 171 |
downstream.onComplete(); |
| 172 |
// no need to loop because this onComplete is the last event |
| 173 |
} |
| 174 |
|
| 175 |
void emitLoop() { |
| 176 |
for (;;) { |
| 177 |
AppendOnlyLinkedArrayList<Object> q; |
| 178 |
synchronized (this) { |
| 179 |
q = queue; |
| 180 |
if (q == null) { |
| 181 |
emitting = false; |
| 182 |
return; |
| 183 |
} |
| 184 |
queue = null; |
| 185 |
} |
| 186 |
|
| 187 |
if (q.accept(downstream)) { |
| 188 |
return; |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public void request(long n) { |
| 195 |
upstream.request(n); |
| 196 |
} |
| 197 |
|
| 198 |
@Override |
| 199 |
public void cancel() { |
| 200 |
upstream.cancel(); |
| 201 |
} |
| 202 |
} |