Skip to content

Commit 67df049

Browse files
Merge pull request #1720 from abersnaze/dematerize-reWhen
Change repeatWhen and retryWhen signatures.
2 parents edbdf6c + bb8be53 commit 67df049

File tree

2 files changed

+80
-37
lines changed

2 files changed

+80
-37
lines changed

src/main/java/rx/Observable.java

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5801,8 +5801,19 @@ public final Observable<T> repeat(final long count, Scheduler scheduler) {
58015801
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#repeatwhen">RxJava Wiki: repeatWhen()</a>
58025802
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229428.aspx">MSDN: Observable.Repeat</a>
58035803
*/
5804-
public final Observable<T> repeatWhen(Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> notificationHandler, Scheduler scheduler) {
5805-
return OnSubscribeRedo.repeat(this, notificationHandler, scheduler);
5804+
public final Observable<T> repeatWhen(final Func1<? super Observable<? extends Void>, ? extends Observable<?>> notificationHandler, Scheduler scheduler) {
5805+
Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> dematerializedNotificationHandler = new Func1<Observable<? extends Notification<?>>, Observable<?>>() {
5806+
@Override
5807+
public Observable<?> call(Observable<? extends Notification<?>> notifications) {
5808+
return notificationHandler.call(notifications.map(new Func1<Notification<?>, Void>() {
5809+
@Override
5810+
public Void call(Notification<?> notification) {
5811+
return null;
5812+
}
5813+
}));
5814+
}
5815+
};
5816+
return OnSubscribeRedo.repeat(this, dematerializedNotificationHandler, scheduler);
58065817
}
58075818

58085819
/**
@@ -5825,8 +5836,19 @@ public final Observable<T> repeatWhen(Func1<? super Observable<? extends Notific
58255836
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#repeatwhen">RxJava Wiki: repeatWhen()</a>
58265837
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229428.aspx">MSDN: Observable.Repeat</a>
58275838
*/
5828-
public final Observable<T> repeatWhen(Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> notificationHandler) {
5829-
return OnSubscribeRedo.repeat(this, notificationHandler);
5839+
public final Observable<T> repeatWhen(final Func1<? super Observable<? extends Void>, ? extends Observable<?>> notificationHandler) {
5840+
Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> dematerializedNotificationHandler = new Func1<Observable<? extends Notification<?>>, Observable<?>>() {
5841+
@Override
5842+
public Observable<?> call(Observable<? extends Notification<?>> notifications) {
5843+
return notificationHandler.call(notifications.map(new Func1<Notification<?>, Void>() {
5844+
@Override
5845+
public Void call(Notification<?> notification) {
5846+
return null;
5847+
}
5848+
}));
5849+
}
5850+
};
5851+
return OnSubscribeRedo.repeat(this, dematerializedNotificationHandler);
58305852
}
58315853

58325854
/**
@@ -6541,8 +6563,19 @@ public final Observable<T> retry(Func2<Integer, Throwable, Boolean> predicate) {
65416563
* @return the source Observable modified with retry logic
65426564
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Error-Handling-Operators#retrywhen">RxJava Wiki: retryWhen()</a>
65436565
*/
6544-
public final Observable<T> retryWhen(Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> notificationHandler) {
6545-
return OnSubscribeRedo.<T> retry(this, notificationHandler);
6566+
public final Observable<T> retryWhen(final Func1<? super Observable<? extends Throwable>, ? extends Observable<?>> notificationHandler) {
6567+
Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> dematerializedNotificationHandler = new Func1<Observable<? extends Notification<?>>, Observable<?>>() {
6568+
@Override
6569+
public Observable<?> call(Observable<? extends Notification<?>> notifications) {
6570+
return notificationHandler.call(notifications.map(new Func1<Notification<?>, Throwable>() {
6571+
@Override
6572+
public Throwable call(Notification<?> notification) {
6573+
return notification.getThrowable();
6574+
}
6575+
}));
6576+
}
6577+
};
6578+
return OnSubscribeRedo.<T> retry(this, dematerializedNotificationHandler);
65466579
}
65476580

65486581
/**
@@ -6566,8 +6599,19 @@ public final Observable<T> retryWhen(Func1<? super Observable<? extends Notifica
65666599
* @return the source Observable modified with retry logic
65676600
* @see <a href="https://github.com/ReactiveX/RxJava/wiki/Error-Handling-Operators#retrywhen">RxJava Wiki: retryWhen()</a>
65686601
*/
6569-
public final Observable<T> retryWhen(Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> notificationHandler, Scheduler scheduler) {
6570-
return OnSubscribeRedo.<T> retry(this, notificationHandler, scheduler);
6602+
public final Observable<T> retryWhen(final Func1<? super Observable<? extends Throwable>, ? extends Observable<?>> notificationHandler, Scheduler scheduler) {
6603+
Func1<? super Observable<? extends Notification<?>>, ? extends Observable<?>> dematerializedNotificationHandler = new Func1<Observable<? extends Notification<?>>, Observable<?>>() {
6604+
@Override
6605+
public Observable<?> call(Observable<? extends Notification<?>> notifications) {
6606+
return notificationHandler.call(notifications.map(new Func1<Notification<?>, Throwable>() {
6607+
@Override
6608+
public Throwable call(Notification<?> notification) {
6609+
return notification.getThrowable();
6610+
}
6611+
}));
6612+
}
6613+
};
6614+
return OnSubscribeRedo.<T> retry(this, dematerializedNotificationHandler, scheduler);
65716615
}
65726616

65736617
/**

src/test/java/rx/internal/operators/OperatorRetryTest.java

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,12 @@
2424
import static org.mockito.Mockito.never;
2525
import static org.mockito.Mockito.times;
2626

27-
import java.util.concurrent.CountDownLatch;
28-
import java.util.concurrent.TimeUnit;
29-
import java.util.concurrent.atomic.AtomicBoolean;
30-
import java.util.concurrent.atomic.AtomicInteger;
31-
3227
import org.junit.Test;
3328
import org.mockito.InOrder;
3429
import org.mockito.Mockito;
3530

3631
import rx.Observable;
3732
import rx.Observable.OnSubscribe;
38-
import rx.Notification;
3933
import rx.Observer;
4034
import rx.Subscriber;
4135
import rx.Subscription;
@@ -49,6 +43,11 @@
4943
import rx.subjects.PublishSubject;
5044
import rx.subscriptions.Subscriptions;
5145

46+
import java.util.concurrent.CountDownLatch;
47+
import java.util.concurrent.TimeUnit;
48+
import java.util.concurrent.atomic.AtomicBoolean;
49+
import java.util.concurrent.atomic.AtomicInteger;
50+
5251
public class OperatorRetryTest {
5352

5453
@Test
@@ -73,15 +72,15 @@ public void call(Subscriber<? super String> t1) {
7372

7473
});
7574
TestSubscriber<String> ts = new TestSubscriber<String>(consumer);
76-
producer.retryWhen(new Func1<Observable<? extends Notification<?>>, Observable<?>>() {
75+
producer.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
7776

7877
@Override
79-
public Observable<?> call(Observable<? extends Notification<?>> attempts) {
78+
public Observable<?> call(Observable<? extends Throwable> attempts) {
8079
// Worker w = Schedulers.computation().createWorker();
8180
return attempts
82-
.map(new Func1<Notification<?>, Tuple>() {
81+
.map(new Func1<Throwable, Tuple>() {
8382
@Override
84-
public Tuple call(Notification<?> n) {
83+
public Tuple call(Throwable n) {
8584
return new Tuple(new Long(1), n);
8685
}})
8786
.scan(new Func2<Tuple, Tuple, Tuple>(){
@@ -94,7 +93,7 @@ public Tuple call(Tuple t, Tuple n) {
9493
public Observable<Long> call(Tuple t) {
9594
System.out.println("Retry # "+t.count);
9695
return t.count > 20 ?
97-
Observable.<Long>error(t.n.getThrowable()) :
96+
Observable.<Long>error(t.n) :
9897
Observable.timer(t.count *1L, TimeUnit.MILLISECONDS);
9998
}});
10099
}
@@ -112,9 +111,9 @@ public Observable<Long> call(Tuple t) {
112111

113112
public static class Tuple {
114113
Long count;
115-
Notification<?> n;
114+
Throwable n;
116115

117-
Tuple(Long c, Notification<?> n) {
116+
Tuple(Long c, Throwable n) {
118117
count = c;
119118
this.n = n;
120119
}
@@ -147,15 +146,15 @@ public void testSchedulingNotificationHandler() {
147146
int NUM_RETRIES = 2;
148147
Observable<String> origin = Observable.create(new FuncWithErrors(NUM_RETRIES));
149148
TestSubscriber<String> subscriber = new TestSubscriber<String>(observer);
150-
origin.retryWhen(new Func1<Observable<? extends Notification<?>>, Observable<? extends Notification<?>>>() {
149+
origin.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
151150
@Override
152-
public Observable<? extends Notification<?>> call(Observable<? extends Notification<?>> t1) {
153-
return t1.observeOn(Schedulers.computation()).map(new Func1<Notification<?>, Notification<?>>() {
151+
public Observable<?> call(Observable<? extends Throwable> t1) {
152+
return t1.observeOn(Schedulers.computation()).map(new Func1<Throwable, Void>() {
154153
@Override
155-
public Notification<?> call(Notification<?> t1) {
156-
return Notification.createOnNext(null);
154+
public Void call(Throwable t1) {
155+
return null;
157156
}
158-
}).startWith(Notification.createOnNext(null));
157+
}).startWith((Void) null);
159158
}
160159
}).subscribe(subscriber);
161160

@@ -178,16 +177,16 @@ public void testOnNextFromNotificationHandler() {
178177
Observer<String> observer = mock(Observer.class);
179178
int NUM_RETRIES = 2;
180179
Observable<String> origin = Observable.create(new FuncWithErrors(NUM_RETRIES));
181-
origin.retryWhen(new Func1<Observable<? extends Notification<?>>, Observable<? extends Notification<?>>>() {
180+
origin.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
182181
@Override
183-
public Observable<? extends Notification<?>> call(Observable<? extends Notification<?>> t1) {
184-
return t1.map(new Func1<Notification<?>, Notification<?>>() {
182+
public Observable<?> call(Observable<? extends Throwable> t1) {
183+
return t1.map(new Func1<Throwable, Void>() {
185184

186185
@Override
187-
public Notification<?> call(Notification<?> t1) {
188-
return Notification.createOnNext(null);
186+
public Void call(Throwable t1) {
187+
return null;
189188
}
190-
}).startWith(Notification.createOnNext(null));
189+
}).startWith((Void) null);
191190
}
192191
}).subscribe(observer);
193192

@@ -209,9 +208,9 @@ public void testOnCompletedFromNotificationHandler() {
209208
Observer<String> observer = mock(Observer.class);
210209
Observable<String> origin = Observable.create(new FuncWithErrors(1));
211210
TestSubscriber<String> subscriber = new TestSubscriber<String>(observer);
212-
origin.retryWhen(new Func1<Observable<? extends Notification<?>>, Observable<? extends Notification<?>>>() {
211+
origin.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
213212
@Override
214-
public Observable<? extends Notification<?>> call(Observable<? extends Notification<?>> t1) {
213+
public Observable<?> call(Observable<? extends Throwable> t1) {
215214
return Observable.empty();
216215
}
217216
}).subscribe(subscriber);
@@ -229,9 +228,9 @@ public void testOnErrorFromNotificationHandler() {
229228
@SuppressWarnings("unchecked")
230229
Observer<String> observer = mock(Observer.class);
231230
Observable<String> origin = Observable.create(new FuncWithErrors(2));
232-
origin.retryWhen(new Func1<Observable<? extends Notification<?>>, Observable<? extends Notification<?>>>() {
231+
origin.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
233232
@Override
234-
public Observable<? extends Notification<?>> call(Observable<? extends Notification<?>> t1) {
233+
public Observable<?> call(Observable<? extends Throwable> t1) {
235234
return Observable.error(new RuntimeException());
236235
}
237236
}).subscribe(observer);

0 commit comments

Comments
 (0)