-
Notifications
You must be signed in to change notification settings - Fork 7.6k
1.x: implement OperatorDoOnEmpty, with Observable.doOnEmpty() operator #3624
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
f07de13
42cce06
bc5df50
0b240aa
605d4d1
4150aab
4f8fa97
d0f95c2
02ffb8c
70372f2
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 |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package rx.internal.operators; | ||
|
|
||
| import rx.Observable; | ||
| import rx.Subscriber; | ||
| import rx.exceptions.Exceptions; | ||
| import rx.functions.Action0; | ||
|
|
||
| public final class OperatorDoOnEmpty<T> implements Observable.Operator<T, T> { | ||
|
|
||
| private final Action0 onEmpty; | ||
|
|
||
| public OperatorDoOnEmpty(Action0 onEmpty) { | ||
| this.onEmpty = onEmpty; | ||
| } | ||
|
|
||
| @Override | ||
| public Subscriber<? super T> call(final Subscriber<? super T> child) { | ||
|
|
||
| return new Subscriber<T>(child) { | ||
|
|
||
| private boolean isEmpty = true; | ||
| private boolean done = false; | ||
|
|
||
| @Override | ||
| public void onCompleted() { | ||
| if (done) { | ||
| return; | ||
| } | ||
| if (isEmpty) { | ||
| try { | ||
| onEmpty.call(); | ||
| } catch (Throwable e) { | ||
| Exceptions.throwOrReport(e,this); | ||
| return; | ||
| } | ||
| if (!isUnsubscribed()) { | ||
| child.onCompleted(); | ||
| } | ||
| } else { | ||
| child.onCompleted(); | ||
| } | ||
| done = true; | ||
| } | ||
|
Collaborator
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. Because @Override
public void onCompleted() {
if (done) {
return;
}
if (isEmpty) {
try {
onEmpty.call();
} catch (Throwable e) {
Exceptions.throwOrReport(e,this);
return;
}
if (!isUnsubscribed()) {
child.onCompleted();
}
} else {
child.onCompleted();
}
done = true;
} |
||
|
|
||
| @Override | ||
| public void onError(Throwable e) { | ||
| if (done) { | ||
| return; | ||
| } | ||
| child.onError(e); | ||
| } | ||
|
|
||
| @Override | ||
| public void onNext(T t) { | ||
| if (done) { | ||
| return; | ||
| } | ||
| isEmpty = false; | ||
| child.onNext(t); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| /** | ||
| * Copyright 2016 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 org.junit.Test; | ||
| import rx.Observable; | ||
| import rx.Subscription; | ||
| import rx.functions.Action0; | ||
| import rx.functions.Func0; | ||
| import rx.observers.TestSubscriber; | ||
| import rx.subjects.PublishSubject; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.assertFalse; | ||
|
|
||
| public final class OperatorDoOnEmptyTest { | ||
|
|
||
| @Test | ||
| public void testNonEmpty() { | ||
| Observable<String> source = Observable.just("Chicago", "Houston", "Phoenix"); | ||
|
|
||
| final AtomicBoolean wasCalled = new AtomicBoolean(false); | ||
|
|
||
| source.doOnEmpty(new Action0() { | ||
| @Override | ||
| public void call() { | ||
| wasCalled.set(true); | ||
| } | ||
| }).subscribe(); | ||
|
|
||
| assertFalse(wasCalled.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEmpty() { | ||
| Observable<String> source = Observable.empty(); | ||
|
|
||
| final AtomicBoolean wasCalled = new AtomicBoolean(false); | ||
|
|
||
| source.doOnEmpty(new Action0() { | ||
| @Override | ||
| public void call() { | ||
| wasCalled.set(true); | ||
| } | ||
| }).subscribe(); | ||
|
|
||
| assertTrue(wasCalled.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnsubscription() { | ||
| final AtomicBoolean wasCalled = new AtomicBoolean(false); | ||
|
|
||
| PublishSubject<Integer> source = PublishSubject.create(); | ||
|
|
||
| Subscription subscription = source.doOnEmpty(new Action0() { | ||
| @Override | ||
| public void call() { | ||
| wasCalled.set(true); | ||
| } | ||
| }).take(3).subscribe(); | ||
|
|
||
| assertTrue(source.hasObservers()); | ||
|
|
||
| source.onNext(0); | ||
| source.onNext(1); | ||
|
|
||
| assertTrue(source.hasObservers()); | ||
|
|
||
| source.onNext(2); | ||
|
|
||
| assertFalse(source.hasObservers()); | ||
|
|
||
| subscription.unsubscribe(); | ||
|
|
||
| assertFalse(wasCalled.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testBackPressure() { | ||
|
|
||
| final AtomicBoolean wasCalled = new AtomicBoolean(false); | ||
|
|
||
| Observable<Integer> source = Observable.range(0,1000).doOnEmpty(new Action0() { | ||
| @Override | ||
| public void call() { | ||
| wasCalled.set(true); | ||
| } | ||
| }); | ||
|
|
||
| TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>(0); | ||
|
|
||
| source.subscribe(subscriber); | ||
|
|
||
| subscriber.requestMore(1); | ||
|
|
||
| assertTrue(subscriber.getOnNextEvents().size() == 1); | ||
| assertTrue(subscriber.getOnCompletedEvents().isEmpty()); | ||
| assertTrue(subscriber.getOnErrorEvents().size() == 0); | ||
| assertFalse(wasCalled.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void subscriberStateTest() { | ||
| final AtomicInteger counter = new AtomicInteger(0); | ||
|
|
||
| final AtomicInteger callCount = new AtomicInteger(0); | ||
|
|
||
| Observable<Integer> o = Observable.defer(new Func0<Observable<Integer>>() { | ||
| @Override | ||
| public Observable<Integer> call() { | ||
| return Observable.range(1, counter.getAndIncrement() % 2); | ||
| } | ||
| }).doOnEmpty(new Action0() { | ||
| @Override | ||
| public void call() { | ||
| callCount.incrementAndGet(); | ||
| } | ||
| }); | ||
|
|
||
| o.subscribe(); | ||
| o.subscribe(); | ||
| o.subscribe(); | ||
| o.subscribe(); | ||
| o.subscribe(); | ||
|
|
||
| assert(callCount.get() == 3); | ||
| } | ||
|
Collaborator
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. a unit test for unsubscription would be good
Member
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. Please add unit test that verifies backpressure composes through.
Author
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. Okay will do. |
||
|
|
||
| } | ||
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.
add
return;after call toExceptions.throwOrReport. Thomas I suggest you read the comments on the github site because it seems like you are reacting to truncated comments (perhaps email notifications).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.
You're right I was reading email notifications.