mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-21 05:13:20 +01:00
364 lines
13 KiB
Java
364 lines
13 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.AppendOnlyLinkedArrayList;
|
|
import io.reactivex.rxjava3.internal.util.ExceptionHelper;
|
|
import io.reactivex.rxjava3.internal.util.NotificationLite;
|
|
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
|
|
import java.util.Objects;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
import java.util.concurrent.locks.Lock;
|
|
import java.util.concurrent.locks.ReadWriteLock;
|
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes10.dex */
|
|
public final class BehaviorSubject<T> extends Subject<T> {
|
|
static final BehaviorDisposable[] EMPTY = new BehaviorDisposable[0];
|
|
static final BehaviorDisposable[] TERMINATED = new BehaviorDisposable[0];
|
|
long index;
|
|
final ReadWriteLock lock;
|
|
final AtomicReference<BehaviorDisposable<T>[]> observers;
|
|
final Lock readLock;
|
|
final AtomicReference<Throwable> terminalEvent;
|
|
final AtomicReference<Object> value;
|
|
final Lock writeLock;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public static final class BehaviorDisposable<T> implements Disposable, AppendOnlyLinkedArrayList.NonThrowingPredicate<Object> {
|
|
volatile boolean cancelled;
|
|
final Observer<? super T> downstream;
|
|
boolean emitting;
|
|
boolean fastPath;
|
|
long index;
|
|
boolean next;
|
|
AppendOnlyLinkedArrayList<Object> queue;
|
|
final BehaviorSubject<T> state;
|
|
|
|
public BehaviorDisposable(Observer<? super T> observer, BehaviorSubject<T> behaviorSubject) {
|
|
this.downstream = observer;
|
|
this.state = behaviorSubject;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.disposables.Disposable
|
|
public void dispose() {
|
|
if (this.cancelled) {
|
|
return;
|
|
}
|
|
this.cancelled = true;
|
|
this.state.remove(this);
|
|
}
|
|
|
|
public void emitFirst() {
|
|
if (this.cancelled) {
|
|
return;
|
|
}
|
|
synchronized (this) {
|
|
try {
|
|
if (this.cancelled) {
|
|
return;
|
|
}
|
|
if (this.next) {
|
|
return;
|
|
}
|
|
BehaviorSubject<T> behaviorSubject = this.state;
|
|
Lock lock = behaviorSubject.readLock;
|
|
lock.lock();
|
|
this.index = behaviorSubject.index;
|
|
Object obj = behaviorSubject.value.get();
|
|
lock.unlock();
|
|
this.emitting = obj != null;
|
|
this.next = true;
|
|
if (obj == null || test(obj)) {
|
|
return;
|
|
}
|
|
emitLoop();
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void emitLoop() {
|
|
AppendOnlyLinkedArrayList<Object> appendOnlyLinkedArrayList;
|
|
while (!this.cancelled) {
|
|
synchronized (this) {
|
|
try {
|
|
appendOnlyLinkedArrayList = this.queue;
|
|
if (appendOnlyLinkedArrayList == null) {
|
|
this.emitting = false;
|
|
return;
|
|
}
|
|
this.queue = null;
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
appendOnlyLinkedArrayList.forEachWhile(this);
|
|
}
|
|
}
|
|
|
|
public void emitNext(Object obj, long j) {
|
|
if (this.cancelled) {
|
|
return;
|
|
}
|
|
if (!this.fastPath) {
|
|
synchronized (this) {
|
|
try {
|
|
if (this.cancelled) {
|
|
return;
|
|
}
|
|
if (this.index == j) {
|
|
return;
|
|
}
|
|
if (this.emitting) {
|
|
AppendOnlyLinkedArrayList<Object> appendOnlyLinkedArrayList = this.queue;
|
|
if (appendOnlyLinkedArrayList == null) {
|
|
appendOnlyLinkedArrayList = new AppendOnlyLinkedArrayList<>(4);
|
|
this.queue = appendOnlyLinkedArrayList;
|
|
}
|
|
appendOnlyLinkedArrayList.add(obj);
|
|
return;
|
|
}
|
|
this.next = true;
|
|
this.fastPath = true;
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
test(obj);
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.disposables.Disposable
|
|
public boolean isDisposed() {
|
|
return this.cancelled;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.internal.util.AppendOnlyLinkedArrayList.NonThrowingPredicate, io.reactivex.rxjava3.functions.Predicate
|
|
public boolean test(Object obj) {
|
|
return this.cancelled || NotificationLite.accept(obj, this.downstream);
|
|
}
|
|
}
|
|
|
|
public BehaviorSubject(T t) {
|
|
ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
|
|
this.lock = reentrantReadWriteLock;
|
|
this.readLock = reentrantReadWriteLock.readLock();
|
|
this.writeLock = reentrantReadWriteLock.writeLock();
|
|
this.observers = new AtomicReference<>(EMPTY);
|
|
this.value = new AtomicReference<>(t);
|
|
this.terminalEvent = new AtomicReference<>();
|
|
}
|
|
|
|
@CheckReturnValue
|
|
@NonNull
|
|
public static <T> BehaviorSubject<T> create() {
|
|
return new BehaviorSubject<>(null);
|
|
}
|
|
|
|
@CheckReturnValue
|
|
@NonNull
|
|
public static <T> BehaviorSubject<T> createDefault(T t) {
|
|
Objects.requireNonNull(t, "defaultValue is null");
|
|
return new BehaviorSubject<>(t);
|
|
}
|
|
|
|
public boolean add(BehaviorDisposable<T> behaviorDisposable) {
|
|
while (true) {
|
|
BehaviorDisposable<T>[] behaviorDisposableArr = this.observers.get();
|
|
if (behaviorDisposableArr == TERMINATED) {
|
|
return false;
|
|
}
|
|
int length = behaviorDisposableArr.length;
|
|
BehaviorDisposable<T>[] behaviorDisposableArr2 = new BehaviorDisposable[length + 1];
|
|
System.arraycopy(behaviorDisposableArr, 0, behaviorDisposableArr2, 0, length);
|
|
behaviorDisposableArr2[length] = behaviorDisposable;
|
|
AtomicReference<BehaviorDisposable<T>[]> atomicReference = this.observers;
|
|
while (!atomicReference.compareAndSet(behaviorDisposableArr, behaviorDisposableArr2)) {
|
|
if (atomicReference.get() != behaviorDisposableArr) {
|
|
break;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
@Nullable
|
|
public Throwable getThrowable() {
|
|
Object obj = this.value.get();
|
|
if (NotificationLite.isError(obj)) {
|
|
return NotificationLite.getError(obj);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@CheckReturnValue
|
|
@Nullable
|
|
public T getValue() {
|
|
Object obj = this.value.get();
|
|
if (NotificationLite.isComplete(obj) || NotificationLite.isError(obj)) {
|
|
return null;
|
|
}
|
|
return (T) NotificationLite.getValue(obj);
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
public boolean hasComplete() {
|
|
return NotificationLite.isComplete(this.value.get());
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
public boolean hasObservers() {
|
|
return this.observers.get().length != 0;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.subjects.Subject
|
|
@CheckReturnValue
|
|
public boolean hasThrowable() {
|
|
return NotificationLite.isError(this.value.get());
|
|
}
|
|
|
|
@CheckReturnValue
|
|
public boolean hasValue() {
|
|
Object obj = this.value.get();
|
|
return (obj == null || NotificationLite.isComplete(obj) || NotificationLite.isError(obj)) ? false : true;
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onComplete() {
|
|
AtomicReference<Throwable> atomicReference = this.terminalEvent;
|
|
Throwable th = ExceptionHelper.TERMINATED;
|
|
while (!atomicReference.compareAndSet(null, th)) {
|
|
if (atomicReference.get() != null) {
|
|
return;
|
|
}
|
|
}
|
|
Object complete = NotificationLite.complete();
|
|
for (BehaviorDisposable<T> behaviorDisposable : terminate(complete)) {
|
|
behaviorDisposable.emitNext(complete, this.index);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onError(Throwable th) {
|
|
ExceptionHelper.nullCheck(th, "onError called with a null Throwable.");
|
|
AtomicReference<Throwable> atomicReference = this.terminalEvent;
|
|
while (!atomicReference.compareAndSet(null, th)) {
|
|
if (atomicReference.get() != null) {
|
|
RxJavaPlugins.onError(th);
|
|
return;
|
|
}
|
|
}
|
|
Object error = NotificationLite.error(th);
|
|
for (BehaviorDisposable<T> behaviorDisposable : terminate(error)) {
|
|
behaviorDisposable.emitNext(error, this.index);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onNext(T t) {
|
|
ExceptionHelper.nullCheck(t, "onNext called with a null value.");
|
|
if (this.terminalEvent.get() != null) {
|
|
return;
|
|
}
|
|
Object next = NotificationLite.next(t);
|
|
setCurrent(next);
|
|
for (BehaviorDisposable<T> behaviorDisposable : this.observers.get()) {
|
|
behaviorDisposable.emitNext(next, this.index);
|
|
}
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observer
|
|
public void onSubscribe(Disposable disposable) {
|
|
if (this.terminalEvent.get() != null) {
|
|
disposable.dispose();
|
|
}
|
|
}
|
|
|
|
public void remove(BehaviorDisposable<T> behaviorDisposable) {
|
|
BehaviorDisposable<T>[] behaviorDisposableArr;
|
|
while (true) {
|
|
BehaviorDisposable<T>[] behaviorDisposableArr2 = this.observers.get();
|
|
int length = behaviorDisposableArr2.length;
|
|
if (length == 0) {
|
|
return;
|
|
}
|
|
int i = 0;
|
|
while (true) {
|
|
if (i >= length) {
|
|
i = -1;
|
|
break;
|
|
} else if (behaviorDisposableArr2[i] == behaviorDisposable) {
|
|
break;
|
|
} else {
|
|
i++;
|
|
}
|
|
}
|
|
if (i < 0) {
|
|
return;
|
|
}
|
|
if (length == 1) {
|
|
behaviorDisposableArr = EMPTY;
|
|
} else {
|
|
BehaviorDisposable<T>[] behaviorDisposableArr3 = new BehaviorDisposable[length - 1];
|
|
System.arraycopy(behaviorDisposableArr2, 0, behaviorDisposableArr3, 0, i);
|
|
System.arraycopy(behaviorDisposableArr2, i + 1, behaviorDisposableArr3, i, (length - i) - 1);
|
|
behaviorDisposableArr = behaviorDisposableArr3;
|
|
}
|
|
AtomicReference<BehaviorDisposable<T>[]> atomicReference = this.observers;
|
|
while (!atomicReference.compareAndSet(behaviorDisposableArr2, behaviorDisposableArr)) {
|
|
if (atomicReference.get() != behaviorDisposableArr2) {
|
|
break;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
public void setCurrent(Object obj) {
|
|
this.writeLock.lock();
|
|
this.index++;
|
|
this.value.lazySet(obj);
|
|
this.writeLock.unlock();
|
|
}
|
|
|
|
@Override // io.reactivex.rxjava3.core.Observable
|
|
public void subscribeActual(Observer<? super T> observer) {
|
|
BehaviorDisposable<T> behaviorDisposable = new BehaviorDisposable<>(observer, this);
|
|
observer.onSubscribe(behaviorDisposable);
|
|
if (add(behaviorDisposable)) {
|
|
if (behaviorDisposable.cancelled) {
|
|
remove(behaviorDisposable);
|
|
return;
|
|
} else {
|
|
behaviorDisposable.emitFirst();
|
|
return;
|
|
}
|
|
}
|
|
Throwable th = this.terminalEvent.get();
|
|
if (th == ExceptionHelper.TERMINATED) {
|
|
observer.onComplete();
|
|
} else {
|
|
observer.onError(th);
|
|
}
|
|
}
|
|
|
|
@CheckReturnValue
|
|
public int subscriberCount() {
|
|
return this.observers.get().length;
|
|
}
|
|
|
|
public BehaviorDisposable<T>[] terminate(Object obj) {
|
|
setCurrent(obj);
|
|
return this.observers.getAndSet(TERMINATED);
|
|
}
|
|
}
|