-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Add onDropped callback for throttleFirst - addresses #7481 #7482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,27 +19,36 @@ | |
import io.reactivex.rxjava3.core.*; | ||
import io.reactivex.rxjava3.core.Scheduler.Worker; | ||
import io.reactivex.rxjava3.disposables.Disposable; | ||
import io.reactivex.rxjava3.exceptions.Exceptions; | ||
import io.reactivex.rxjava3.functions.Consumer; | ||
import io.reactivex.rxjava3.internal.disposables.DisposableHelper; | ||
import io.reactivex.rxjava3.observers.SerializedObserver; | ||
|
||
public final class ObservableThrottleFirstTimed<T> extends AbstractObservableWithUpstream<T, T> { | ||
final long timeout; | ||
final TimeUnit unit; | ||
final Scheduler scheduler; | ||
|
||
public ObservableThrottleFirstTimed(ObservableSource<T> source, | ||
long timeout, TimeUnit unit, Scheduler scheduler) { | ||
final Consumer<? super T> onDropped; | ||
|
||
public ObservableThrottleFirstTimed( | ||
ObservableSource<T> source, | ||
long timeout, | ||
TimeUnit unit, | ||
Scheduler scheduler, | ||
Consumer<? super T> onDropped) { | ||
super(source); | ||
this.timeout = timeout; | ||
this.unit = unit; | ||
this.scheduler = scheduler; | ||
this.onDropped = onDropped; | ||
} | ||
|
||
@Override | ||
public void subscribeActual(Observer<? super T> t) { | ||
source.subscribe(new DebounceTimedObserver<>( | ||
new SerializedObserver<>(t), | ||
timeout, unit, scheduler.createWorker())); | ||
timeout, unit, scheduler.createWorker(), | ||
onDropped)); | ||
} | ||
|
||
static final class DebounceTimedObserver<T> | ||
|
@@ -51,16 +60,21 @@ static final class DebounceTimedObserver<T> | |
final long timeout; | ||
final TimeUnit unit; | ||
final Scheduler.Worker worker; | ||
|
||
final Consumer<? super T> onDropped; | ||
Disposable upstream; | ||
|
||
volatile boolean gate; | ||
|
||
DebounceTimedObserver(Observer<? super T> actual, long timeout, TimeUnit unit, Worker worker) { | ||
DebounceTimedObserver( | ||
Observer<? super T> actual, | ||
long timeout, | ||
TimeUnit unit, | ||
Worker worker, | ||
Consumer<? super T> onDropped) { | ||
this.downstream = actual; | ||
this.timeout = timeout; | ||
this.unit = unit; | ||
this.worker = worker; | ||
this.onDropped = onDropped; | ||
} | ||
|
||
@Override | ||
|
@@ -83,6 +97,15 @@ public void onNext(T t) { | |
d.dispose(); | ||
} | ||
DisposableHelper.replace(this, worker.schedule(this, timeout, unit)); | ||
} else if (onDropped != null) { | ||
try { | ||
onDropped.accept(t); | ||
} catch (Throwable ex) { | ||
Exceptions.throwIfFatal(ex); | ||
downstream.onError(ex); | ||
worker.dispose(); | ||
upstream.dispose(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will hang the sequence because the error is swallowed. Also there is no cleanup of the worker. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So we have to do downstream.onError(t) to propagate this, right? And .dispose() of the worker too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be addressed now, thx for the review |
||
} | ||
} | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.