jimu-decompiled/sources/io/reactivex/subjects/PublishSubject.java
2025-05-13 19:24:51 +02:00

178 lines
5.9 KiB
Java

package io.reactivex.subjects;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.plugins.RxJavaPlugins;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
/* loaded from: classes2.dex */
public final class PublishSubject<T> extends Subject<T> {
static final PublishDisposable[] c = new PublishDisposable[0];
static final PublishDisposable[] d = new PublishDisposable[0];
final AtomicReference<PublishDisposable<T>[]> a = new AtomicReference<>(d);
Throwable b;
PublishSubject() {
}
public static <T> PublishSubject<T> b() {
return new PublishSubject<>();
}
boolean a(PublishDisposable<T> publishDisposable) {
PublishDisposable<T>[] publishDisposableArr;
PublishDisposable<T>[] publishDisposableArr2;
do {
publishDisposableArr = this.a.get();
if (publishDisposableArr == c) {
return false;
}
int length = publishDisposableArr.length;
publishDisposableArr2 = new PublishDisposable[length + 1];
System.arraycopy(publishDisposableArr, 0, publishDisposableArr2, 0, length);
publishDisposableArr2[length] = publishDisposable;
} while (!this.a.compareAndSet(publishDisposableArr, publishDisposableArr2));
return true;
}
@Override // io.reactivex.Observer
public void onComplete() {
PublishDisposable<T>[] publishDisposableArr = this.a.get();
PublishDisposable<T>[] publishDisposableArr2 = c;
if (publishDisposableArr == publishDisposableArr2) {
return;
}
for (PublishDisposable<T> publishDisposable : this.a.getAndSet(publishDisposableArr2)) {
publishDisposable.b();
}
}
@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.");
PublishDisposable<T>[] publishDisposableArr = this.a.get();
PublishDisposable<T>[] publishDisposableArr2 = c;
if (publishDisposableArr == publishDisposableArr2) {
RxJavaPlugins.b(th);
return;
}
this.b = th;
for (PublishDisposable<T> publishDisposable : this.a.getAndSet(publishDisposableArr2)) {
publishDisposable.a(th);
}
}
@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.");
for (PublishDisposable<T> publishDisposable : this.a.get()) {
publishDisposable.a((PublishDisposable<T>) t);
}
}
@Override // io.reactivex.Observer
public void onSubscribe(Disposable disposable) {
if (this.a.get() == c) {
disposable.dispose();
}
}
@Override // io.reactivex.Observable
protected void subscribeActual(Observer<? super T> observer) {
PublishDisposable<T> publishDisposable = new PublishDisposable<>(observer, this);
observer.onSubscribe(publishDisposable);
if (a(publishDisposable)) {
if (publishDisposable.a()) {
b(publishDisposable);
}
} else {
Throwable th = this.b;
if (th != null) {
observer.onError(th);
} else {
observer.onComplete();
}
}
}
static final class PublishDisposable<T> extends AtomicBoolean implements Disposable {
final Observer<? super T> a;
final PublishSubject<T> b;
PublishDisposable(Observer<? super T> observer, PublishSubject<T> publishSubject) {
this.a = observer;
this.b = publishSubject;
}
public void a(T t) {
if (get()) {
return;
}
this.a.onNext(t);
}
public void b() {
if (get()) {
return;
}
this.a.onComplete();
}
@Override // io.reactivex.disposables.Disposable
public void dispose() {
if (compareAndSet(false, true)) {
this.b.b(this);
}
}
public void a(Throwable th) {
if (get()) {
RxJavaPlugins.b(th);
} else {
this.a.onError(th);
}
}
public boolean a() {
return get();
}
}
void b(PublishDisposable<T> publishDisposable) {
PublishDisposable<T>[] publishDisposableArr;
PublishDisposable<T>[] publishDisposableArr2;
do {
publishDisposableArr = this.a.get();
if (publishDisposableArr == c || publishDisposableArr == d) {
return;
}
int length = publishDisposableArr.length;
int i = -1;
int i2 = 0;
while (true) {
if (i2 >= length) {
break;
}
if (publishDisposableArr[i2] == publishDisposable) {
i = i2;
break;
}
i2++;
}
if (i < 0) {
return;
}
if (length == 1) {
publishDisposableArr2 = d;
} else {
PublishDisposable<T>[] publishDisposableArr3 = new PublishDisposable[length - 1];
System.arraycopy(publishDisposableArr, 0, publishDisposableArr3, 0, i);
System.arraycopy(publishDisposableArr, i + 1, publishDisposableArr3, i, (length - i) - 1);
publishDisposableArr2 = publishDisposableArr3;
}
} while (!this.a.compareAndSet(publishDisposableArr, publishDisposableArr2));
}
}