package com.discord.utilities.rx; import androidx.recyclerview.widget.RecyclerView; import c.q.a.k.a; import java.util.Objects; import java.util.concurrent.TimeUnit; import rx.Observable; import rx.Scheduler; import rx.Subscriber; import rx.functions.Action0; import rx.observers.SerializedSubscriber; import rx.subscriptions.SerialSubscription; public class LeadingEdgeThrottle implements Observable.b { public final Scheduler scheduler; public final long timeInMilliseconds; /* renamed from: com.discord.utilities.rx.LeadingEdgeThrottle$1 reason: invalid class name */ public class AnonymousClass1 extends Subscriber { public long lastOnNext = -1; public final Subscriber self = this; public final DebounceState state = new DebounceState<>(); public final /* synthetic */ SerializedSubscriber val$s; public final /* synthetic */ SerialSubscription val$serial; public final /* synthetic */ Scheduler.Worker val$worker; /* renamed from: com.discord.utilities.rx.LeadingEdgeThrottle$1$1 reason: invalid class name */ public class AnonymousClass1 implements Action0 { public final /* synthetic */ int val$index; public AnonymousClass1(int i) { this.val$index = i; } @Override // rx.functions.Action0 public void call() { AnonymousClass1 r0 = AnonymousClass1.this; r0.state.emit(this.val$index, r0.val$s, r0.self); } } /* JADX INFO: super call moved to the top of the method (can break code semantics) */ public AnonymousClass1(Subscriber subscriber, SerializedSubscriber serializedSubscriber, SerialSubscription serialSubscription, Scheduler.Worker worker) { super(subscriber); this.val$s = serializedSubscriber; this.val$serial = serialSubscription; this.val$worker = worker; } @Override // j0.g public void onCompleted() { this.state.emitAndComplete(this.val$s, this); } @Override // j0.g public void onError(Throwable th) { this.val$s.i.onError(th); unsubscribe(); this.state.clear(); } @Override // j0.g public void onNext(T t) { Objects.requireNonNull(LeadingEdgeThrottle.this.scheduler); long currentTimeMillis = System.currentTimeMillis(); int next = this.state.next(t); long j = this.lastOnNext; if (j == -1 || currentTimeMillis - j >= LeadingEdgeThrottle.this.timeInMilliseconds) { this.lastOnNext = currentTimeMillis; this.state.emit(next, this.val$s, this.self); return; } this.val$serial.a(this.val$worker.b(new AnonymousClass1(next), LeadingEdgeThrottle.this.timeInMilliseconds, TimeUnit.MILLISECONDS)); } @Override // rx.Subscriber public void onStart() { request(RecyclerView.FOREVER_NS); } } public static final class DebounceState { public boolean emitting; public boolean hasValue; public int index; public boolean terminate; public T value; public synchronized void clear() { this.index++; this.value = null; this.hasValue = false; } public void emit(int i, Subscriber subscriber, Subscriber subscriber2) { T t; synchronized (this) { if (!this.emitting && this.hasValue) { if (i == this.index) { t = this.value; this.value = null; this.hasValue = false; this.emitting = true; } } return; } try { subscriber.onNext(t); synchronized (this) { if (!this.terminate) { this.emitting = false; } else { subscriber.onCompleted(); } } } catch (Throwable th) { a.b0(th, subscriber2, t); } } public void emitAndComplete(Subscriber subscriber, Subscriber subscriber2) { T t; boolean z2; synchronized (this) { if (this.emitting) { this.terminate = true; return; } t = this.value; z2 = this.hasValue; this.value = null; this.hasValue = false; this.emitting = true; } if (z2) { try { subscriber.onNext(t); } catch (Throwable th) { a.b0(th, subscriber2, t); return; } } subscriber.onCompleted(); } public synchronized int next(T t) { int i; this.value = t; this.hasValue = true; i = this.index + 1; this.index = i; return i; } } public LeadingEdgeThrottle(long j, TimeUnit timeUnit) { this(j, timeUnit, j0.p.a.a()); } public LeadingEdgeThrottle(long j, TimeUnit timeUnit, Scheduler scheduler) { this.timeInMilliseconds = timeUnit.toMillis(j); this.scheduler = scheduler; } @Override // j0.k.b public /* bridge */ /* synthetic */ Object call(Object obj) { return call((Subscriber) ((Subscriber) obj)); } public Subscriber call(Subscriber subscriber) { Scheduler.Worker a = this.scheduler.a(); SerializedSubscriber serializedSubscriber = new SerializedSubscriber(subscriber); SerialSubscription serialSubscription = new SerialSubscription(); serializedSubscriber.add(a); serializedSubscriber.add(serialSubscription); return new AnonymousClass1(subscriber, serializedSubscriber, serialSubscription, a); } }