| 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.internal.operators.observable; |
| 15 |
|
| 16 |
import java.util.concurrent.atomic.*; |
| 17 |
|
| 18 |
import io.reactivex.rxjava3.core.*; |
| 19 |
import io.reactivex.rxjava3.disposables.Disposable; |
| 20 |
import io.reactivex.rxjava3.exceptions.Exceptions; |
| 21 |
import io.reactivex.rxjava3.functions.Cancellable; |
| 22 |
import io.reactivex.rxjava3.internal.disposables.*; |
| 23 |
import io.reactivex.rxjava3.internal.util.*; |
| 24 |
import io.reactivex.rxjava3.operators.SimpleQueue; |
| 25 |
import io.reactivex.rxjava3.operators.SpscLinkedArrayQueue; |
| 26 |
import io.reactivex.rxjava3.plugins.RxJavaPlugins; |
| 27 |
|
| 28 |
public final class ObservableCreate<T> extends Observable<T> { |
| 29 |
final ObservableOnSubscribe<T> source; |
| 30 |
|
| 31 |
public ObservableCreate(ObservableOnSubscribe<T> source) { |
| 32 |
this.source = source; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
protected void subscribeActual(Observer<? super T> observer) { |
| 37 |
CreateEmitter<T> parent = new CreateEmitter<>(observer); |
| 38 |
observer.onSubscribe(parent); |
| 39 |
|
| 40 |
try { |
| 41 |
source.subscribe(parent); |
| 42 |
} catch (Throwable ex) { |
| 43 |
Exceptions.throwIfFatal(ex); |
| 44 |
parent.onError(ex); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
static final class CreateEmitter<T> |
| 49 |
extends AtomicReference<Disposable> |
| 50 |
implements ObservableEmitter<T>, Disposable { |
| 51 |
|
| 52 |
private static final long serialVersionUID = -3434801548987643227L; |
| 53 |
|
| 54 |
final Observer<? super T> observer; |
| 55 |
|
| 56 |
CreateEmitter(Observer<? super T> observer) { |
| 57 |
this.observer = observer; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void onNext(T t) { |
| 62 |
if (t == null) { |
| 63 |
onError(ExceptionHelper.createNullPointerException("onNext called with a null value.")); |
| 64 |
return; |
| 65 |
} |
| 66 |
if (!isDisposed()) { |
| 67 |
observer.onNext(t); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public void onError(Throwable t) { |
| 73 |
if (!tryOnError(t)) { |
| 74 |
RxJavaPlugins.onError(t); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public boolean tryOnError(Throwable t) { |
| 80 |
if (t == null) { |
| 81 |
t = ExceptionHelper.createNullPointerException("onError called with a null Throwable."); |
| 82 |
} |
| 83 |
if (!isDisposed()) { |
| 84 |
try { |
| 85 |
observer.onError(t); |
| 86 |
} finally { |
| 87 |
dispose(); |
| 88 |
} |
| 89 |
return true; |
| 90 |
} |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onComplete() { |
| 96 |
if (!isDisposed()) { |
| 97 |
try { |
| 98 |
observer.onComplete(); |
| 99 |
} finally { |
| 100 |
dispose(); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void setDisposable(Disposable d) { |
| 107 |
DisposableHelper.set(this, d); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
public void setCancellable(Cancellable c) { |
| 112 |
setDisposable(new CancellableDisposable(c)); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public ObservableEmitter<T> serialize() { |
| 117 |
return new SerializedEmitter<>(this); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public void dispose() { |
| 122 |
DisposableHelper.dispose(this); |
| 123 |
} |
| 124 |
|
| 125 |
@Override |
| 126 |
public boolean isDisposed() { |
| 127 |
return DisposableHelper.isDisposed(get()); |
| 128 |
} |
| 129 |
|
| 130 |
@Override |
| 131 |
public String toString() { |
| 132 |
return String.format("%s{%s}", getClass().getSimpleName(), super.toString()); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Serializes calls to onNext, onError and onComplete. |
| 138 |
* |
| 139 |
* @param <T> the value type |
| 140 |
*/ |
| 141 |
static final class SerializedEmitter<T> |
| 142 |
extends AtomicInteger |
| 143 |
implements ObservableEmitter<T> { |
| 144 |
|
| 145 |
private static final long serialVersionUID = 4883307006032401862L; |
| 146 |
|
| 147 |
final ObservableEmitter<T> emitter; |
| 148 |
|
| 149 |
final AtomicThrowable errors; |
| 150 |
|
| 151 |
final SpscLinkedArrayQueue<T> queue; |
| 152 |
|
| 153 |
volatile boolean done; |
| 154 |
|
| 155 |
SerializedEmitter(ObservableEmitter<T> emitter) { |
| 156 |
this.emitter = emitter; |
| 157 |
this.errors = new AtomicThrowable(); |
| 158 |
this.queue = new SpscLinkedArrayQueue<>(16); |
| 159 |
} |
| 160 |
|
| 161 |
@Override |
| 162 |
public void onNext(T t) { |
| 163 |
if (done || emitter.isDisposed()) { |
| 164 |
return; |
| 165 |
} |
| 166 |
if (t == null) { |
| 167 |
onError(ExceptionHelper.createNullPointerException("onNext called with a null value.")); |
| 168 |
return; |
| 169 |
} |
| 170 |
if (get() == 0 && compareAndSet(0, 1)) { |
| 171 |
emitter.onNext(t); |
| 172 |
if (decrementAndGet() == 0) { |
| 173 |
return; |
| 174 |
} |
| 175 |
} else { |
| 176 |
SimpleQueue<T> q = queue; |
| 177 |
synchronized (q) { |
| 178 |
q.offer(t); |
| 179 |
} |
| 180 |
if (getAndIncrement() != 0) { |
| 181 |
return; |
| 182 |
} |
| 183 |
} |
| 184 |
drainLoop(); |
| 185 |
} |
| 186 |
|
| 187 |
@Override |
| 188 |
public void onError(Throwable t) { |
| 189 |
if (!tryOnError(t)) { |
| 190 |
RxJavaPlugins.onError(t); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public boolean tryOnError(Throwable t) { |
| 196 |
if (done || emitter.isDisposed()) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
if (t == null) { |
| 200 |
t = ExceptionHelper.createNullPointerException("onError called with a null Throwable."); |
| 201 |
} |
| 202 |
if (errors.tryAddThrowable(t)) { |
| 203 |
done = true; |
| 204 |
drain(); |
| 205 |
return true; |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void onComplete() { |
| 212 |
if (done || emitter.isDisposed()) { |
| 213 |
return; |
| 214 |
} |
| 215 |
done = true; |
| 216 |
drain(); |
| 217 |
} |
| 218 |
|
| 219 |
void drain() { |
| 220 |
if (getAndIncrement() == 0) { |
| 221 |
drainLoop(); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
void drainLoop() { |
| 226 |
ObservableEmitter<T> e = emitter; |
| 227 |
SpscLinkedArrayQueue<T> q = queue; |
| 228 |
AtomicThrowable errors = this.errors; |
| 229 |
int missed = 1; |
| 230 |
for (;;) { |
| 231 |
|
| 232 |
for (;;) { |
| 233 |
if (e.isDisposed()) { |
| 234 |
q.clear(); |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
if (errors.get() != null) { |
| 239 |
q.clear(); |
| 240 |
errors.tryTerminateConsumer(e); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
boolean d = done; |
| 245 |
T v = q.poll(); |
| 246 |
|
| 247 |
boolean empty = v == null; |
| 248 |
|
| 249 |
if (d && empty) { |
| 250 |
e.onComplete(); |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
if (empty) { |
| 255 |
break; |
| 256 |
} |
| 257 |
|
| 258 |
e.onNext(v); |
| 259 |
} |
| 260 |
|
| 261 |
missed = addAndGet(-missed); |
| 262 |
if (missed == 0) { |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public void setDisposable(Disposable d) { |
| 270 |
emitter.setDisposable(d); |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
public void setCancellable(Cancellable c) { |
| 275 |
emitter.setCancellable(c); |
| 276 |
} |
| 277 |
|
| 278 |
@Override |
| 279 |
public boolean isDisposed() { |
| 280 |
return emitter.isDisposed(); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public ObservableEmitter<T> serialize() { |
| 285 |
return this; |
| 286 |
} |
| 287 |
|
| 288 |
@Override |
| 289 |
public String toString() { |
| 290 |
return emitter.toString(); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
} |