Skip to content

Commit eb60e3c

Browse files
committed
RxScala: Add mapping to RxJava doOnUnsubscribe
1 parent ffe9968 commit eb60e3c

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

language-adaptors/rxjava-scala/src/examples/scala/rx/lang/scala/examples/RxScalaDemo.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,16 @@ class RxScalaDemo extends JUnitSuite {
843843
// onCompleted
844844
}
845845

846+
@Test def doOnUnsubscribeExample(): Unit = {
847+
val o = List("red", "green", "blue").toObservable.doOnUnsubscribe { println("unsubscribed") }
848+
o.subscribe(v => println(v), e => e.printStackTrace, () => println("onCompleted"))
849+
// red
850+
// green
851+
// blue
852+
// onCompleted
853+
// unsubscribed
854+
}
855+
846856
@Test def finallyDoExample(): Unit = {
847857
val o = List("red", "green", "blue").toObservable.finallyDo { println("finally") }
848858
o.subscribe(v => println(v), e => e.printStackTrace, () => println("onCompleted"))

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3608,6 +3608,26 @@ trait Observable[+T]
36083608
toScalaObservable[T](asJavaObservable.doOnTerminate(() => onTerminate))
36093609
}
36103610

3611+
/**
3612+
* Modifies the source {@code Observable} so that it invokes the given action when it is unsubscribed from
3613+
* its subscribers. Each un-subscription will result in an invocation of the given action except when the
3614+
* source {@code Observable} is reference counted, in which case the source {@code Observable} will invoke
3615+
* the given action for the very last un-subscription.
3616+
* <p>
3617+
* <img width="640" height="310" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/doOnUnsubscribe.png" alt="">
3618+
* <dl>
3619+
* <dt><b>Scheduler:</b></dt>
3620+
* <dd>{@code doOnUnsubscribe} does not operate by default on a particular {@link Scheduler}.</dd>
3621+
* </dl>
3622+
*
3623+
* @param onUnsubscribe the action that gets called when this { @code Observable} is unsubscribed
3624+
* @return the source { @code Observable} modified so as to call this Action when appropriate
3625+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#doonunsubscribe">RxJava wiki: doOnUnsubscribe</a>
3626+
*/
3627+
def doOnUnsubscribe(onUnsubscribe: => Unit): Observable[T] = {
3628+
toScalaObservable[T](asJavaObservable.doOnUnsubscribe(() => onUnsubscribe))
3629+
}
3630+
36113631
/**
36123632
* Given two Observables, mirror the one that first emits an item.
36133633
*

0 commit comments

Comments
 (0)