Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main/java/rx/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,15 @@ public final Single<T> timeout(long timeout, TimeUnit timeUnit, Single<? extends
*/
public final Single<T> timeout(long timeout, TimeUnit timeUnit, Single<? extends T> other, Scheduler scheduler) {
if (other == null) {
other = Single.<T> error(new TimeoutException());
// Use a defer instead of simply other = Single.error(new TimeoutException())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd inline this into SingleTimeout, that way, there are no extra instances unless necessary.

// since instantiating an exception will cause the current stack trace to be inspected
// and we only want to incur that overhead when a timeout actually happens.
other = Single.<T>defer(new Func0<Single<T>>() {
@Override
public Single<T> call() {
return Single.<T>error(new TimeoutException());
}
});
}
return create(new SingleTimeout<T>(onSubscribe, timeout, timeUnit, scheduler, other.onSubscribe));
}
Expand Down