mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-28 16:53:22 +01:00
222 lines
8 KiB
Java
222 lines
8 KiB
Java
package io.reactivex.rxjava3.subjects;
|
|
|
|
import io.reactivex.rxjava3.annotations.CheckReturnValue;
|
|
import io.reactivex.rxjava3.annotations.NonNull;
|
|
import io.reactivex.rxjava3.annotations.Nullable;
|
|
import io.reactivex.rxjava3.core.Observer;
|
|
import io.reactivex.rxjava3.disposables.Disposable;
|
|
import io.reactivex.rxjava3.internal.util.ExceptionHelper;
|
|
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes10.dex */
|
|
public final class PublishSubject<T> extends Subject<T> {
|
|
Throwable error;
|
|
final AtomicReference<PublishDisposable<T>[]> subscribers = new AtomicReference<>(EMPTY);
|
|
static final PublishDisposable[] TERMINATED = new PublishDisposable[0];
|
|
static final PublishDisposable[] EMPTY = new PublishDisposable[0];
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public static final class PublishDisposable<T> extends AtomicBoolean implements Disposable {
|
|
private static final long serialVersionUID = 3562861878281475070L;
|
|
final Observer<? super T> downstream;
|
|
final PublishSubject<T> parent;
|
|
|
|
public PublishDisposable(Observer<? super T> observer, PublishSubject<T> publishSubject) {
|
|
this.downstream = observer;
|
|
this.parent = publishSubject;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.disposables.Disposable
|
|
public void dispose() {
|
|
if (compareAndSet(false, true)) {
|
|
this.parent.remove(this);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.disposables.Disposable
|
|
public boolean isDisposed() {
|
|
return get();
|
|
}
|
|
|
|
public void onComplete() {
|
|
if (get()) {
|
|
return;
|
|
}
|
|
this.downstream.onComplete();
|
|
}
|
|
|
|
public void onError(Throwable th) {
|
|
if (get()) {
|
|
RxJavaPlugins.onError(th);
|
|
} else {
|
|
this.downstream.onError(th);
|
|
}
|
|
}
|
|
|
|
public void onNext(T t) {
|
|
if (get()) {
|
|
return;
|
|
}
|
|
this.downstream.onNext(t);
|
|
}
|
|
}
|
|
|
|
@CheckReturnValue
|
|
@NonNull
|
|
public static <T> PublishSubject<T> create() {
|
|
return new PublishSubject<>();
|
|
}
|
|
|
|
public boolean add(PublishDisposable<T> publishDisposable) {
|
|
while (true) {
|
|
PublishDisposable<T>[] publishDisposableArr = this.subscribers.get();
|
|
if (publishDisposableArr == TERMINATED) {
|
|
return false;
|
|
}
|
|
int length = publishDisposableArr.length;
|
|
PublishDisposable<T>[] publishDisposableArr2 = new PublishDisposable[length + 1];
|
|
System.arraycopy(publishDisposableArr, 0, publishDisposableArr2, 0, length);
|
|
publishDisposableArr2[length] = publishDisposable;
|
|
AtomicReference<PublishDisposable<T>[]> atomicReference = this.subscribers;
|
|
while (!atomicReference.compareAndSet(publishDisposableArr, publishDisposableArr2)) {
|
|
if (atomicReference.get() != publishDisposableArr) {
|
|
break;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
@Nullable
|
|
public Throwable getThrowable() {
|
|
if (this.subscribers.get() == TERMINATED) {
|
|
return this.error;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
public boolean hasComplete() {
|
|
return this.subscribers.get() == TERMINATED && this.error == null;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
public boolean hasObservers() {
|
|
return this.subscribers.get().length != 0;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
public boolean hasThrowable() {
|
|
return this.subscribers.get() == TERMINATED && this.error != null;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onComplete() {
|
|
PublishDisposable<T>[] publishDisposableArr = this.subscribers.get();
|
|
PublishDisposable<T>[] publishDisposableArr2 = TERMINATED;
|
|
if (publishDisposableArr == publishDisposableArr2) {
|
|
return;
|
|
}
|
|
PublishDisposable<T>[] andSet = this.subscribers.getAndSet(publishDisposableArr2);
|
|
for (PublishDisposable<T> publishDisposable : andSet) {
|
|
publishDisposable.onComplete();
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onError(Throwable th) {
|
|
ExceptionHelper.nullCheck(th, "onError called with a null Throwable.");
|
|
PublishDisposable<T>[] publishDisposableArr = this.subscribers.get();
|
|
PublishDisposable<T>[] publishDisposableArr2 = TERMINATED;
|
|
if (publishDisposableArr == publishDisposableArr2) {
|
|
RxJavaPlugins.onError(th);
|
|
return;
|
|
}
|
|
this.error = th;
|
|
PublishDisposable<T>[] andSet = this.subscribers.getAndSet(publishDisposableArr2);
|
|
for (PublishDisposable<T> publishDisposable : andSet) {
|
|
publishDisposable.onError(th);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onNext(T t) {
|
|
ExceptionHelper.nullCheck(t, "onNext called with a null value.");
|
|
for (PublishDisposable<T> publishDisposable : this.subscribers.get()) {
|
|
publishDisposable.onNext(t);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
if (this.subscribers.get() == TERMINATED) {
|
|
disposable.dispose();
|
|
}
|
|
}
|
|
|
|
public void remove(PublishDisposable<T> publishDisposable) {
|
|
PublishDisposable<T>[] publishDisposableArr;
|
|
while (true) {
|
|
PublishDisposable<T>[] publishDisposableArr2 = this.subscribers.get();
|
|
if (publishDisposableArr2 == TERMINATED || publishDisposableArr2 == EMPTY) {
|
|
return;
|
|
}
|
|
int length = publishDisposableArr2.length;
|
|
int i = 0;
|
|
while (true) {
|
|
if (i >= length) {
|
|
i = -1;
|
|
break;
|
|
} else if (publishDisposableArr2[i] == publishDisposable) {
|
|
break;
|
|
} else {
|
|
i++;
|
|
}
|
|
}
|
|
if (i < 0) {
|
|
return;
|
|
}
|
|
if (length == 1) {
|
|
publishDisposableArr = EMPTY;
|
|
} else {
|
|
PublishDisposable<T>[] publishDisposableArr3 = new PublishDisposable[length - 1];
|
|
System.arraycopy(publishDisposableArr2, 0, publishDisposableArr3, 0, i);
|
|
System.arraycopy(publishDisposableArr2, i + 1, publishDisposableArr3, i, (length - i) - 1);
|
|
publishDisposableArr = publishDisposableArr3;
|
|
}
|
|
AtomicReference<PublishDisposable<T>[]> atomicReference = this.subscribers;
|
|
while (!atomicReference.compareAndSet(publishDisposableArr2, publishDisposableArr)) {
|
|
if (atomicReference.get() != publishDisposableArr2) {
|
|
break;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observable
|
|
public void subscribeActual(Observer<? super T> observer) {
|
|
PublishDisposable<T> publishDisposable = new PublishDisposable<>(observer, this);
|
|
observer.onSubscribe(publishDisposable);
|
|
if (add(publishDisposable)) {
|
|
if (publishDisposable.isDisposed()) {
|
|
remove(publishDisposable);
|
|
}
|
|
} else {
|
|
Throwable th = this.error;
|
|
if (th != null) {
|
|
observer.onError(th);
|
|
} else {
|
|
observer.onComplete();
|
|
}
|
|
}
|
|
}
|
|
}
|