Skip to content

2.x: Reuse SingleDoOnEvent #4483

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions src/main/java/io/reactivex/Single.java
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ public final Single<T> doOnSubscribe(final Consumer<? super Disposable> onSubscr
*/
public final Single<T> doOnSuccess(final Consumer<? super T> onSuccess) {
ObjectHelper.requireNonNull(onSuccess, "onSuccess is null");
return RxJavaPlugins.onAssembly(new SingleDoOnSuccess<T>(this, onSuccess));
return RxJavaPlugins.onAssembly(new SingleDoOnEvent<T>(this, onSuccess, Functions.emptyConsumer()));
Copy link
Member

Choose a reason for hiding this comment

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

It was inlined nicely, this now allocates 2 consumers.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

True I just thought of reusing it since that's also the way it is done in Completable

Copy link
Member

Choose a reason for hiding this comment

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

Single and Completable are practically a year old and although re-architected, they still run with extra allocations. If I can get there eventually, I'll tidy them up.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alright cool then I'll close this and help you out with avoiding the extra costs of allocation

}

/**
Expand All @@ -1604,7 +1604,17 @@ public final Single<T> doOnSuccess(final Consumer<? super T> onSuccess) {
*/
public final Single<T> doOnEvent(final BiConsumer<? super T, ? super Throwable> onEvent) {
ObjectHelper.requireNonNull(onEvent, "onEvent is null");
return RxJavaPlugins.onAssembly(new SingleDoOnEvent<T>(this, onEvent));
return RxJavaPlugins.onAssembly(new SingleDoOnEvent<T>(this, new Consumer<T>() {
@Override
public void accept(final T t) throws Exception {
onEvent.accept(t, null);
}
}, new Consumer<Throwable>() {
@Override
public void accept(final Throwable throwable) throws Exception {
onEvent.accept(null, throwable);
}
}));
}

/**
Expand All @@ -1620,7 +1630,7 @@ public final Single<T> doOnEvent(final BiConsumer<? super T, ? super Throwable>
*/
public final Single<T> doOnError(final Consumer<? super Throwable> onError) {
ObjectHelper.requireNonNull(onError, "onError is null");
return RxJavaPlugins.onAssembly(new SingleDoOnError<T>(this, onError));
return RxJavaPlugins.onAssembly(new SingleDoOnEvent<T>(this, Functions.emptyConsumer(), onError));
}

/**
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@
import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.CompositeException;
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.BiConsumer;
import io.reactivex.functions.Consumer;

public final class SingleDoOnEvent<T> extends Single<T> {
final SingleSource<T> source;

final BiConsumer<? super T, ? super Throwable> onEvent;
final Consumer<? super T> onSuccess;
final Consumer<? super Throwable> onError;

public SingleDoOnEvent(SingleSource<T> source, BiConsumer<? super T, ? super Throwable> onEvent) {
public SingleDoOnEvent(final SingleSource<T> source, final Consumer<? super T> onSuccess, final Consumer<? super Throwable> onError) {
this.source = source;
this.onEvent = onEvent;
this.onSuccess = onSuccess;
this.onError = onError;
}

@Override
Expand All @@ -43,7 +45,7 @@ public void onSubscribe(Disposable d) {
@Override
public void onSuccess(T value) {
try {
onEvent.accept(value, null);
onSuccess.accept(value);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
s.onError(ex);
Expand All @@ -56,7 +58,7 @@ public void onSuccess(T value) {
@Override
public void onError(Throwable e) {
try {
onEvent.accept(null, e);
onError.accept(e);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
e = new CompositeException(ex, e);
Expand Down

This file was deleted.