|
78 | 78 | import rx.plugins.RxJavaObservableExecutionHook;
|
79 | 79 | import rx.plugins.RxJavaPlugins;
|
80 | 80 | import rx.subjects.PublishSubject;
|
| 81 | +import rx.subjects.ReplaySubject; |
81 | 82 | import rx.subjects.Subject;
|
82 | 83 | import rx.subscriptions.BooleanSubscription;
|
83 | 84 | import rx.subscriptions.Subscriptions;
|
@@ -1666,6 +1667,17 @@ public static <T> Observable<T> onErrorReturn(final Observable<T> that, Func1<Ex
|
1666 | 1667 | return create(OperationOnErrorReturn.onErrorReturn(that, resumeFunction));
|
1667 | 1668 | }
|
1668 | 1669 |
|
| 1670 | + /** |
| 1671 | + * Returns a connectable observable sequence that shares a single subscription to the underlying sequence replaying all notifications. |
| 1672 | + * |
| 1673 | + * @param that |
| 1674 | + * the source Observable |
| 1675 | + * @return a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject. |
| 1676 | + */ |
| 1677 | + public static <T> ConnectableObservable<T> replay(final Observable<T> that) { |
| 1678 | + return OperationMulticast.multicast(that, ReplaySubject.<T> create()); |
| 1679 | + } |
| 1680 | + |
1669 | 1681 | /**
|
1670 | 1682 | * Returns a connectable observable sequence that shares a single subscription to the underlying sequence.
|
1671 | 1683 | *
|
@@ -3199,13 +3211,22 @@ public Observable<T> reduce(Func2<T, T, T> accumulator) {
|
3199 | 3211 | return reduce(this, accumulator);
|
3200 | 3212 | }
|
3201 | 3213 |
|
| 3214 | + /** |
| 3215 | + * Returns a connectable observable sequence that shares a single subscription to the underlying sequence replaying all notifications. |
| 3216 | + * |
| 3217 | + * @return a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject. |
| 3218 | + */ |
| 3219 | + public ConnectableObservable<T> replay() { |
| 3220 | + return replay(this); |
| 3221 | + } |
| 3222 | + |
3202 | 3223 | /**
|
3203 | 3224 | * Returns a connectable observable sequence that shares a single subscription to the underlying sequence.
|
3204 | 3225 | *
|
3205 | 3226 | * @return a connectable observable sequence that upon connection causes the source sequence to push results into the specified subject.
|
3206 | 3227 | */
|
3207 | 3228 | public ConnectableObservable<T> publish() {
|
3208 |
| - return OperationMulticast.multicast(this, PublishSubject.<T> create()); |
| 3229 | + return publish(this); |
3209 | 3230 | }
|
3210 | 3231 |
|
3211 | 3232 | /**
|
@@ -4174,10 +4195,10 @@ public Subscription call(final Observer<String> observer) {
|
4174 | 4195 |
|
4175 | 4196 | @Override
|
4176 | 4197 | public void run() {
|
| 4198 | + counter.incrementAndGet(); |
4177 | 4199 | System.out.println("published observable being executed");
|
4178 | 4200 | observer.onNext("one");
|
4179 | 4201 | observer.onCompleted();
|
4180 |
| - counter.incrementAndGet(); |
4181 | 4202 | }
|
4182 | 4203 | }).start();
|
4183 | 4204 | return subscription;
|
@@ -4219,6 +4240,66 @@ public void call(String v) {
|
4219 | 4240 | }
|
4220 | 4241 | }
|
4221 | 4242 |
|
| 4243 | + @Test |
| 4244 | + public void testReplay() throws InterruptedException { |
| 4245 | + final AtomicInteger counter = new AtomicInteger(); |
| 4246 | + ConnectableObservable<String> o = Observable.create(new Func1<Observer<String>, Subscription>() { |
| 4247 | + |
| 4248 | + @Override |
| 4249 | + public Subscription call(final Observer<String> observer) { |
| 4250 | + final BooleanSubscription subscription = new BooleanSubscription(); |
| 4251 | + new Thread(new Runnable() { |
| 4252 | + |
| 4253 | + @Override |
| 4254 | + public void run() { |
| 4255 | + System.out.println("published observable being executed"); |
| 4256 | + observer.onNext("one"); |
| 4257 | + observer.onCompleted(); |
| 4258 | + counter.incrementAndGet(); |
| 4259 | + } |
| 4260 | + }).start(); |
| 4261 | + return subscription; |
| 4262 | + } |
| 4263 | + }).replay(); |
| 4264 | + |
| 4265 | + // we connect immediately and it will emit the value |
| 4266 | + Subscription s = o.connect(); |
| 4267 | + try { |
| 4268 | + |
| 4269 | + // we then expect the following 2 subscriptions to get that same value |
| 4270 | + final CountDownLatch latch = new CountDownLatch(2); |
| 4271 | + |
| 4272 | + // subscribe once |
| 4273 | + o.subscribe(new Action1<String>() { |
| 4274 | + |
| 4275 | + @Override |
| 4276 | + public void call(String v) { |
| 4277 | + assertEquals("one", v); |
| 4278 | + System.out.println("v: " + v); |
| 4279 | + latch.countDown(); |
| 4280 | + } |
| 4281 | + }); |
| 4282 | + |
| 4283 | + // subscribe again |
| 4284 | + o.subscribe(new Action1<String>() { |
| 4285 | + |
| 4286 | + @Override |
| 4287 | + public void call(String v) { |
| 4288 | + assertEquals("one", v); |
| 4289 | + System.out.println("v: " + v); |
| 4290 | + latch.countDown(); |
| 4291 | + } |
| 4292 | + }); |
| 4293 | + |
| 4294 | + if (!latch.await(1000, TimeUnit.MILLISECONDS)) { |
| 4295 | + fail("subscriptions did not receive values"); |
| 4296 | + } |
| 4297 | + assertEquals(1, counter.get()); |
| 4298 | + } finally { |
| 4299 | + s.unsubscribe(); |
| 4300 | + } |
| 4301 | + } |
| 4302 | + |
4222 | 4303 | private static class TestException extends RuntimeException {
|
4223 | 4304 | private static final long serialVersionUID = 1L;
|
4224 | 4305 | }
|
|
0 commit comments