269 lines
7.6 KiB
Java
269 lines
7.6 KiB
Java
package io.reactivex.subjects;
|
|
|
|
import io.reactivex.Observable;
|
|
import io.reactivex.Observer;
|
|
import io.reactivex.disposables.Disposable;
|
|
import io.reactivex.internal.disposables.EmptyDisposable;
|
|
import io.reactivex.internal.functions.ObjectHelper;
|
|
import io.reactivex.internal.fuseable.SimpleQueue;
|
|
import io.reactivex.internal.observers.BasicIntQueueDisposable;
|
|
import io.reactivex.internal.queue.SpscLinkedArrayQueue;
|
|
import io.reactivex.plugins.RxJavaPlugins;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
/* loaded from: classes2.dex */
|
|
public final class UnicastSubject<T> extends Subject<T> {
|
|
final SpscLinkedArrayQueue<T> a;
|
|
final AtomicReference<Observer<? super T>> b;
|
|
final AtomicReference<Runnable> c;
|
|
final boolean d;
|
|
volatile boolean e;
|
|
volatile boolean f;
|
|
Throwable g;
|
|
final AtomicBoolean h;
|
|
final BasicIntQueueDisposable<T> i;
|
|
boolean j;
|
|
|
|
final class UnicastQueueDisposable extends BasicIntQueueDisposable<T> {
|
|
UnicastQueueDisposable() {
|
|
}
|
|
|
|
@Override // io.reactivex.internal.fuseable.SimpleQueue
|
|
public void clear() {
|
|
UnicastSubject.this.a.clear();
|
|
}
|
|
|
|
@Override // io.reactivex.disposables.Disposable
|
|
public void dispose() {
|
|
if (UnicastSubject.this.e) {
|
|
return;
|
|
}
|
|
UnicastSubject unicastSubject = UnicastSubject.this;
|
|
unicastSubject.e = true;
|
|
unicastSubject.b();
|
|
UnicastSubject.this.b.lazySet(null);
|
|
if (UnicastSubject.this.i.getAndIncrement() == 0) {
|
|
UnicastSubject.this.b.lazySet(null);
|
|
UnicastSubject.this.a.clear();
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.internal.fuseable.SimpleQueue
|
|
public boolean isEmpty() {
|
|
return UnicastSubject.this.a.isEmpty();
|
|
}
|
|
|
|
@Override // io.reactivex.internal.fuseable.SimpleQueue
|
|
public T poll() throws Exception {
|
|
return UnicastSubject.this.a.poll();
|
|
}
|
|
|
|
@Override // io.reactivex.internal.fuseable.QueueFuseable
|
|
public int requestFusion(int i) {
|
|
if ((i & 2) == 0) {
|
|
return 0;
|
|
}
|
|
UnicastSubject.this.j = true;
|
|
return 2;
|
|
}
|
|
}
|
|
|
|
UnicastSubject(int i, boolean z) {
|
|
ObjectHelper.a(i, "capacityHint");
|
|
this.a = new SpscLinkedArrayQueue<>(i);
|
|
this.c = new AtomicReference<>();
|
|
this.d = z;
|
|
this.b = new AtomicReference<>();
|
|
this.h = new AtomicBoolean();
|
|
this.i = new UnicastQueueDisposable();
|
|
}
|
|
|
|
public static <T> UnicastSubject<T> a(int i) {
|
|
return new UnicastSubject<>(i, true);
|
|
}
|
|
|
|
public static <T> UnicastSubject<T> d() {
|
|
return new UnicastSubject<>(Observable.bufferSize(), true);
|
|
}
|
|
|
|
void b() {
|
|
Runnable runnable = this.c.get();
|
|
if (runnable == null || !this.c.compareAndSet(runnable, null)) {
|
|
return;
|
|
}
|
|
runnable.run();
|
|
}
|
|
|
|
void c(Observer<? super T> observer) {
|
|
this.b.lazySet(null);
|
|
Throwable th = this.g;
|
|
if (th != null) {
|
|
observer.onError(th);
|
|
} else {
|
|
observer.onComplete();
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onComplete() {
|
|
if (this.f || this.e) {
|
|
return;
|
|
}
|
|
this.f = true;
|
|
b();
|
|
c();
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onError(Throwable th) {
|
|
ObjectHelper.a(th, "onError called with null. Null values are generally not allowed in 2.x operators and sources.");
|
|
if (this.f || this.e) {
|
|
RxJavaPlugins.b(th);
|
|
return;
|
|
}
|
|
this.g = th;
|
|
this.f = true;
|
|
b();
|
|
c();
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onNext(T t) {
|
|
ObjectHelper.a((Object) t, "onNext called with null. Null values are generally not allowed in 2.x operators and sources.");
|
|
if (this.f || this.e) {
|
|
return;
|
|
}
|
|
this.a.offer(t);
|
|
c();
|
|
}
|
|
|
|
@Override // io.reactivex.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
if (this.f || this.e) {
|
|
disposable.dispose();
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.Observable
|
|
protected void subscribeActual(Observer<? super T> observer) {
|
|
if (this.h.get() || !this.h.compareAndSet(false, true)) {
|
|
EmptyDisposable.error(new IllegalStateException("Only a single observer allowed."), observer);
|
|
return;
|
|
}
|
|
observer.onSubscribe(this.i);
|
|
this.b.lazySet(observer);
|
|
if (this.e) {
|
|
this.b.lazySet(null);
|
|
} else {
|
|
c();
|
|
}
|
|
}
|
|
|
|
public static <T> UnicastSubject<T> a(int i, Runnable runnable) {
|
|
return new UnicastSubject<>(i, runnable, true);
|
|
}
|
|
|
|
void a(Observer<? super T> observer) {
|
|
SpscLinkedArrayQueue<T> spscLinkedArrayQueue = this.a;
|
|
int i = 1;
|
|
boolean z = !this.d;
|
|
while (!this.e) {
|
|
boolean z2 = this.f;
|
|
if (z && z2 && a(spscLinkedArrayQueue, observer)) {
|
|
return;
|
|
}
|
|
observer.onNext(null);
|
|
if (z2) {
|
|
c(observer);
|
|
return;
|
|
} else {
|
|
i = this.i.addAndGet(-i);
|
|
if (i == 0) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
this.b.lazySet(null);
|
|
spscLinkedArrayQueue.clear();
|
|
}
|
|
|
|
void b(Observer<? super T> observer) {
|
|
SpscLinkedArrayQueue<T> spscLinkedArrayQueue = this.a;
|
|
boolean z = !this.d;
|
|
boolean z2 = true;
|
|
int i = 1;
|
|
while (!this.e) {
|
|
boolean z3 = this.f;
|
|
T poll = this.a.poll();
|
|
boolean z4 = poll == null;
|
|
if (z3) {
|
|
if (z && z2) {
|
|
if (a(spscLinkedArrayQueue, observer)) {
|
|
return;
|
|
} else {
|
|
z2 = false;
|
|
}
|
|
}
|
|
if (z4) {
|
|
c(observer);
|
|
return;
|
|
}
|
|
}
|
|
if (z4) {
|
|
i = this.i.addAndGet(-i);
|
|
if (i == 0) {
|
|
return;
|
|
}
|
|
} else {
|
|
observer.onNext(poll);
|
|
}
|
|
}
|
|
this.b.lazySet(null);
|
|
spscLinkedArrayQueue.clear();
|
|
}
|
|
|
|
void c() {
|
|
if (this.i.getAndIncrement() != 0) {
|
|
return;
|
|
}
|
|
Observer<? super T> observer = this.b.get();
|
|
int i = 1;
|
|
while (observer == null) {
|
|
i = this.i.addAndGet(-i);
|
|
if (i == 0) {
|
|
return;
|
|
} else {
|
|
observer = this.b.get();
|
|
}
|
|
}
|
|
if (this.j) {
|
|
a(observer);
|
|
} else {
|
|
b(observer);
|
|
}
|
|
}
|
|
|
|
UnicastSubject(int i, Runnable runnable, boolean z) {
|
|
ObjectHelper.a(i, "capacityHint");
|
|
this.a = new SpscLinkedArrayQueue<>(i);
|
|
ObjectHelper.a(runnable, "onTerminate");
|
|
this.c = new AtomicReference<>(runnable);
|
|
this.d = z;
|
|
this.b = new AtomicReference<>();
|
|
this.h = new AtomicBoolean();
|
|
this.i = new UnicastQueueDisposable();
|
|
}
|
|
|
|
boolean a(SimpleQueue<T> simpleQueue, Observer<? super T> observer) {
|
|
Throwable th = this.g;
|
|
if (th == null) {
|
|
return false;
|
|
}
|
|
this.b.lazySet(null);
|
|
simpleQueue.clear();
|
|
observer.onError(th);
|
|
return true;
|
|
}
|
|
}
|