File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change 83
83
import rx .plugins .RxJavaErrorHandler ;
84
84
import rx .plugins .RxJavaObservableExecutionHook ;
85
85
import rx .plugins .RxJavaPlugins ;
86
+ import rx .subjects .AsyncSubject ;
86
87
import rx .subjects .PublishSubject ;
87
88
import rx .subjects .ReplaySubject ;
88
89
import rx .subjects .Subject ;
@@ -3634,6 +3635,14 @@ public ConnectableObservable<T> publish() {
3634
3635
return OperationMulticast .multicast (this , PublishSubject .<T > create ());
3635
3636
}
3636
3637
3638
+ /**
3639
+ * Returns a {@link ConnectableObservable} that shares a single subscription that contains the last notification only.
3640
+ * @return a {@link ConnectableObservable}
3641
+ */
3642
+ public ConnectableObservable <T > publishLast () {
3643
+ return OperationMulticast .multicast (this , AsyncSubject .<T > create ());
3644
+ }
3645
+
3637
3646
/**
3638
3647
* Synonymous with <code>reduce()</code>.
3639
3648
* <p>
Original file line number Diff line number Diff line change @@ -487,6 +487,48 @@ public void call(String v) {
487
487
}
488
488
}
489
489
490
+ @ Test
491
+ public void testPublishLast () throws InterruptedException {
492
+ final AtomicInteger count = new AtomicInteger ();
493
+ ConnectableObservable <String > connectable = Observable .create (new OnSubscribeFunc <String >() {
494
+ @ Override
495
+ public Subscription onSubscribe (final Observer <? super String > observer ) {
496
+ count .incrementAndGet ();
497
+ final BooleanSubscription subscription = new BooleanSubscription ();
498
+ new Thread (new Runnable () {
499
+ @ Override
500
+ public void run () {
501
+ observer .onNext ("first" );
502
+ observer .onNext ("last" );
503
+ observer .onCompleted ();
504
+ }
505
+ }).start ();
506
+ return subscription ;
507
+ }
508
+ }).publishLast ();
509
+
510
+ // subscribe once
511
+ final CountDownLatch latch = new CountDownLatch (1 );
512
+ connectable .subscribe (new Action1 <String >() {
513
+ @ Override
514
+ public void call (String value ) {
515
+ assertEquals ("last" , value );
516
+ latch .countDown ();
517
+ }
518
+ });
519
+
520
+ // subscribe twice
521
+ connectable .subscribe (new Action1 <String >() {
522
+ @ Override
523
+ public void call (String _) {}
524
+ });
525
+
526
+ Subscription subscription = connectable .connect ();
527
+ assertTrue (latch .await (1000 , TimeUnit .MILLISECONDS ));
528
+ assertEquals (1 , count .get ());
529
+ subscription .unsubscribe ();
530
+ }
531
+
490
532
@ Test
491
533
public void testReplay () throws InterruptedException {
492
534
final AtomicInteger counter = new AtomicInteger ();
You can’t perform that action at this time.
0 commit comments