Skip to content

Commit 1b4e2a8

Browse files
committed
Add 'doOnTerminate' to RxScala
1 parent d5074f4 commit 1b4e2a8

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,26 @@ class RxScalaDemo extends JUnitSuite {
560560
obs.toBlockingObservable.toIterable.last
561561
}
562562

563+
@Test def doOnTerminateExample(): Unit = {
564+
val o = List("red", "green", "blue").toObservable.doOnTerminate(() => println("terminate"))
565+
o.subscribe(v => println(v), e => e.printStackTrace, () => println("onCompleted"))
566+
// red
567+
// green
568+
// blud
569+
// terminate
570+
// onCompleted
571+
}
572+
573+
@Test def finallyDoExample(): Unit = {
574+
val o = List("red", "green", "blue").toObservable.finallyDo(() => println("finally"))
575+
o.subscribe(v => println(v), e => e.printStackTrace, () => println("onCompleted"))
576+
// red
577+
// green
578+
// blud
579+
// onCompleted
580+
// finally
581+
}
582+
563583
@Test def timeoutExample(): Unit = {
564584
val other = List(100L, 200L, 300L).toObservable
565585
val result = Observable.interval(100 millis).timeout(50 millis, other).toBlockingObservable.toList

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2461,6 +2461,22 @@ trait Observable[+T]
24612461
toScalaObservable[T](asJavaObservable.doOnEach(Observer(onNext, onError,onCompleted)))
24622462
}
24632463

2464+
/**
2465+
* Modifies an Observable so that it invokes an action when it calls `onCompleted` or `onError`.
2466+
* <p>
2467+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/doOnTerminate.png">
2468+
* <p>
2469+
* This differs from `finallyDo` in that this happens BEFORE onCompleted/onError are emitted.
2470+
*
2471+
* @param onTerminate the action to invoke when the source Observable calls `onCompleted` or `onError`
2472+
* @return the source Observable with the side-effecting behavior applied
2473+
* @see <a href="https://github.com/Netflix/RxJava/wiki/Observable-Utility-Operators#wiki-doonterminate">RxJava Wiki: doOnTerminate()</a>
2474+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229804.aspx">MSDN: Observable.Do</a>
2475+
*/
2476+
def doOnTerminate(onTerminate: () => Unit): Observable[T] = {
2477+
toScalaObservable[T](asJavaObservable.doOnTerminate(onTerminate))
2478+
}
2479+
24642480
/**
24652481
* Given two Observables, mirror the one that first emits an item.
24662482
*

0 commit comments

Comments
 (0)