File tree Expand file tree Collapse file tree 3 files changed +23
-7
lines changed
test/java/rx/internal/operators Expand file tree Collapse file tree 3 files changed +23
-7
lines changed Original file line number Diff line number Diff line change @@ -3739,17 +3739,20 @@ public final Observable<T> defaultIfEmpty(T defaultValue) {
3739
3739
/**
3740
3740
* Returns an Observable that emits the items emitted by the source Observable or the items of an alternate Observable if the source Observable
3741
3741
* is empty.
3742
- * <p>
3742
+ * <p/ >
3743
3743
* <dl>
3744
3744
* <dt><b>Scheduler:</b></dt>
3745
3745
* <dd>{@code switchIfEmpty} does not operate by default on a particular {@link Scheduler}.</dd>
3746
+ * <dt><b>Beta:</b></dt>
3747
+ * <dd>{@code switchIfEmpty} is currently in {@link rx.annotations.Beta} and subject to change.</dd>
3746
3748
* </dl>
3747
3749
*
3748
3750
* @param alternate
3749
3751
* the alternate Observable to subscribe to if the source does not emit any items
3750
3752
* @return an Observable that emits the items emitted by the source Observable or the items of an alternate Observable if the source Observable
3751
3753
* is empty.
3752
3754
*/
3755
+ @ Beta
3753
3756
public final Observable <T > switchIfEmpty (Observable <T > alternate ) {
3754
3757
return lift (new OperatorSwitchIfEmpty <T >(alternate ));
3755
3758
}
Original file line number Diff line number Diff line change @@ -35,7 +35,9 @@ public OperatorSwitchIfEmpty(Observable<T> alternate) {
35
35
36
36
@ Override
37
37
public Subscriber <? super T > call (Subscriber <? super T > child ) {
38
- return new SwitchIfEmptySubscriber (child );
38
+ final SwitchIfEmptySubscriber parent = new SwitchIfEmptySubscriber (child );
39
+ child .add (parent );
40
+ return parent ;
39
41
}
40
42
41
43
private class SwitchIfEmptySubscriber extends Subscriber <T > {
@@ -46,8 +48,6 @@ private class SwitchIfEmptySubscriber extends Subscriber<T> {
46
48
private final Subscriber <? super T > child ;
47
49
48
50
public SwitchIfEmptySubscriber (Subscriber <? super T > child ) {
49
- super (child );
50
-
51
51
this .child = child ;
52
52
}
53
53
@@ -69,12 +69,13 @@ public void onCompleted() {
69
69
if (!empty ) {
70
70
child .onCompleted ();
71
71
} else if (!child .isUnsubscribed ()) {
72
+ unsubscribe ();
72
73
subscribeToAlternate ();
73
74
}
74
75
}
75
76
76
77
private void subscribeToAlternate () {
77
- add (alternate .unsafeSubscribe (new Subscriber <T >() {
78
+ child . add (alternate .unsafeSubscribe (new Subscriber <T >() {
78
79
@ Override
79
80
public void onStart () {
80
81
final long capacity = consumerCapacity .get ();
Original file line number Diff line number Diff line change 21
21
import rx .Subscriber ;
22
22
import rx .Subscription ;
23
23
import rx .functions .Action0 ;
24
- import rx .functions .Action1 ;
25
- import rx .schedulers .Schedulers ;
26
24
import rx .subscriptions .Subscriptions ;
27
25
28
26
import java .util .Arrays ;
@@ -115,4 +113,18 @@ public void onNext(Long aLong) {
115
113
assertTrue (empty .isUnsubscribed ());
116
114
assertTrue (sub .isUnsubscribed ());
117
115
}
116
+
117
+ @ Test
118
+ public void testSwitchShouldTriggerUnsubscribe () {
119
+ final Subscription s = Subscriptions .empty ();
120
+
121
+ Observable .create (new Observable .OnSubscribe <Long >() {
122
+ @ Override
123
+ public void call (final Subscriber <? super Long > subscriber ) {
124
+ subscriber .add (s );
125
+ subscriber .onCompleted ();
126
+ }
127
+ }).switchIfEmpty (Observable .<Long >never ()).subscribe ();
128
+ assertTrue (s .isUnsubscribed ());
129
+ }
118
130
}
You can’t perform that action at this time.
0 commit comments