-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: Single add doOnEach #4461
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
Merged
Merged
1.x: Single add doOnEach #4461
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2406,27 +2406,54 @@ public final <T2, R> Single<R> zipWith(Single<? extends T2> other, Func2<? super | |
* the action to invoke if the source {@link Single} calls {@code onError} | ||
* @return the source {@link Single} with the side-effecting behavior applied | ||
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a> | ||
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number) | ||
*/ | ||
@Experimental | ||
public final Single<T> doOnError(final Action1<Throwable> onError) { | ||
Observer<T> observer = new Observer<T>() { | ||
if (onError == null) { | ||
throw new IllegalArgumentException("onError is null"); | ||
} | ||
|
||
return Single.create(new SingleDoOnEvent<T>(this, Actions.empty(), new Action1<Throwable>() { | ||
@Override | ||
public void onCompleted() { | ||
// deliberately ignored | ||
public void call(final Throwable throwable) { | ||
onError.call(throwable); | ||
} | ||
})); | ||
} | ||
|
||
/** | ||
* Modifies the source {@link Single} so that it invokes an action when it calls {@code onSuccess} or {@code onError}. | ||
* <p> | ||
* <img width="640" height="310" src="https://raw.githubusercontent.com/wiki/ReactiveX/RxJava/images/rx-operators/doOnEach.png" alt=""> | ||
* <dl> | ||
* <dt><b>Scheduler:</b></dt> | ||
* <dd>{@code doOnEach} does not operate by default on a particular {@link Scheduler}.</dd> | ||
* </dl> | ||
* | ||
* @param onNotification | ||
* the action to invoke when the source {@link Single} calls {@code onSuccess} or {@code onError}. | ||
* @return the source {@link Single} with the side-effecting behavior applied | ||
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a> | ||
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number) | ||
*/ | ||
@Experimental | ||
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. javadoc? |
||
public final Single<T> doOnEach(final Action1<Notification<? extends T>> onNotification) { | ||
if (onNotification == null) { | ||
throw new IllegalArgumentException("onNotification is null"); | ||
} | ||
|
||
return Single.create(new SingleDoOnEvent<T>(this, new Action1<T>() { | ||
@Override | ||
public void onError(Throwable e) { | ||
onError.call(e); | ||
public void call(final T t) { | ||
onNotification.call(Notification.<T>createOnNext(t)); | ||
} | ||
|
||
}, new Action1<Throwable>() { | ||
@Override | ||
public void onNext(T t) { | ||
// deliberately ignored | ||
public void call(final Throwable throwable) { | ||
onNotification.call(Notification.<T>createOnError(throwable)); | ||
} | ||
}; | ||
|
||
return Observable.create(new OnSubscribeDoOnEach<T>(this.toObservable(), observer)).toSingle(); | ||
})); | ||
} | ||
|
||
/** | ||
|
@@ -2442,27 +2469,25 @@ public void onNext(T t) { | |
* the action to invoke when the source {@link Single} calls {@code onSuccess} | ||
* @return the source {@link Single} with the side-effecting behavior applied | ||
* @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a> | ||
* @since (if this graduates from Experimental/Beta to supported, replace this parenthetical with the release number) | ||
*/ | ||
@Experimental | ||
public final Single<T> doOnSuccess(final Action1<? super T> onSuccess) { | ||
Observer<T> observer = new Observer<T>() { | ||
@Override | ||
public void onCompleted() { | ||
// deliberately ignored | ||
} | ||
if (onSuccess == null) { | ||
throw new IllegalArgumentException("onSuccess is null"); | ||
} | ||
|
||
return Single.create(new SingleDoOnEvent<T>(this, new Action1<T>() { | ||
@Override | ||
public void onError(Throwable e) { | ||
// deliberately ignored | ||
public void call(final T t) { | ||
onSuccess.call(t); | ||
} | ||
|
||
}, new Action1<Throwable>() { | ||
@Override | ||
public void onNext(T t) { | ||
onSuccess.call(t); | ||
public void call(final Throwable throwable) { | ||
// Do nothing. | ||
} | ||
}; | ||
|
||
return Observable.create(new OnSubscribeDoOnEach<T>(this.toObservable(), observer)).toSingle(); | ||
})); | ||
} | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Copyright 2014 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package rx.internal.operators; | ||
|
||
import rx.Single; | ||
import rx.SingleSubscriber; | ||
import rx.exceptions.CompositeException; | ||
import rx.exceptions.Exceptions; | ||
import rx.functions.Action1; | ||
|
||
public final class SingleDoOnEvent<T> implements Single.OnSubscribe<T> { | ||
final Single<T> source; | ||
final Action1<? super T> onSuccess; | ||
final Action1<Throwable> onError; | ||
|
||
public SingleDoOnEvent(Single<T> source, Action1<? super T> onSuccess, Action1<Throwable> onError) { | ||
this.source = source; | ||
this.onSuccess = onSuccess; | ||
this.onError = onError; | ||
} | ||
|
||
@Override | ||
public void call(SingleSubscriber<? super T> actual) { | ||
SingleDoOnEventSubscriber<T> parent = new SingleDoOnEventSubscriber<T>(actual, onSuccess, onError); | ||
actual.add(parent); | ||
source.subscribe(parent); | ||
} | ||
|
||
static final class SingleDoOnEventSubscriber<T> extends SingleSubscriber<T> { | ||
final SingleSubscriber<? super T> actual; | ||
final Action1<? super T> onSuccess; | ||
final Action1<Throwable> onError; | ||
|
||
SingleDoOnEventSubscriber(SingleSubscriber<? super T> actual, Action1<? super T> onSuccess, Action1<Throwable> onError) { | ||
this.actual = actual; | ||
this.onSuccess = onSuccess; | ||
this.onError = onError; | ||
} | ||
|
||
@Override | ||
public void onSuccess(T value) { | ||
try { | ||
onSuccess.call(value); | ||
} catch (Throwable e) { | ||
Exceptions.throwOrReport(e, this, value); | ||
return; | ||
} | ||
|
||
actual.onSuccess(value); | ||
} | ||
|
||
@Override | ||
public void onError(Throwable error) { | ||
try { | ||
onError.call(error); | ||
} catch (Throwable e) { | ||
Exceptions.throwIfFatal(e); | ||
actual.onError(new CompositeException(error, e)); | ||
return; | ||
} | ||
|
||
actual.onError(error); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might need a new image here since there's no onNext