File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -2250,6 +2250,28 @@ public void onNext(T t) {
22502250 return lift (new OperatorDoOnEach <T >(observer ));
22512251 }
22522252
2253+ /**
2254+ * Modifies the source {@code Single} so that it invokes the given action when it is subscribed from
2255+ * its subscribers. Each subscription will result in an invocation of the given action except when the
2256+ * source {@code Single} is reference counted, in which case the source {@code Single} will invoke
2257+ * the given action for the first subscription.
2258+ * <p>
2259+ * <img width="640" height="390" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/doOnSubscribe.png" alt="">
2260+ * <dl>
2261+ * <dt><b>Scheduler:</b></dt>
2262+ * <dd>{@code doOnSubscribe} does not operate by default on a particular {@link Scheduler}.</dd>
2263+ * </dl>
2264+ *
2265+ * @param subscribe
2266+ * the action that gets called when an observer subscribes to this {@code Single}
2267+ * @return the source {@code Single} modified so as to call this Action when appropriate
2268+ * @see <a href="http://reactivex.io/documentation/operators/do.html">ReactiveX operators documentation: Do</a>
2269+ */
2270+ @ Experimental
2271+ public final Single <T > doOnSubscribe (final Action0 subscribe ) {
2272+ return lift (new OperatorDoOnSubscribe <T >(subscribe ));
2273+ }
2274+
22532275 /**
22542276 * Returns an Single that emits the items emitted by the source Single shifted forward in time by a
22552277 * specified delay. Error notifications from the source Single are not delayed.
Original file line number Diff line number Diff line change @@ -878,6 +878,43 @@ public void doOnSuccessShouldNotSwallowExceptionThrownByAction() {
878878 verify (action ).call (eq ("value" ));
879879 }
880880
881+ @ Test
882+ public void doOnSubscribeShouldInvokeAction () {
883+ Action0 action = mock (Action0 .class );
884+ Single <Integer > single = Single .just (1 ).doOnSubscribe (action );
885+
886+ verifyZeroInteractions (action );
887+
888+ single .subscribe ();
889+ single .subscribe ();
890+
891+ verify (action , times (2 )).call ();
892+ }
893+
894+ @ Test
895+ public void doOnSubscribeShouldInvokeActionBeforeSubscriberSubscribes () {
896+ final List <String > callSequence = new ArrayList <String >(2 );
897+
898+ Single <Integer > single = Single .create (new OnSubscribe <Integer >() {
899+ @ Override
900+ public void call (SingleSubscriber <? super Integer > singleSubscriber ) {
901+ callSequence .add ("onSubscribe" );
902+ singleSubscriber .onSuccess (1 );
903+ }
904+ }).doOnSubscribe (new Action0 () {
905+ @ Override
906+ public void call () {
907+ callSequence .add ("doOnSubscribe" );
908+ }
909+ });
910+
911+ single .subscribe ();
912+
913+ assertEquals (2 , callSequence .size ());
914+ assertEquals ("doOnSubscribe" , callSequence .get (0 ));
915+ assertEquals ("onSubscribe" , callSequence .get (1 ));
916+ }
917+
881918 @ Test
882919 public void delayWithSchedulerShouldDelayCompletion () {
883920 TestScheduler scheduler = new TestScheduler ();
You can’t perform that action at this time.
0 commit comments