Skip to content

Commit

Permalink
Merge pull request #1301 from jbripley/subscription-block-expression
Browse files Browse the repository at this point in the history
RxScala: Add convenience method for adding unsubscription callback
  • Loading branch information
benjchristensen committed Jun 1, 2014
2 parents 5b5d58f + 9c74545 commit 400f69b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ trait Subscriber[-T] extends Observer[T] with Subscription {
asJavaSubscriber.add(s.asJavaSubscription)
}

/**
* Register a callback to be run when Subscriber is unsubscribed
*
* @param u callback to run when unsubscribed
*/
final def add(u: => Unit): Unit = {
asJavaSubscriber.add(Subscription(u).asJavaSubscription)
}

override final def unsubscribe(): Unit = {
asJavaSubscriber.unsubscribe()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,19 @@ class SubscriberTests extends JUnitSuite {
assertTrue(innerSubscriber.isUnsubscribed)
}

@Test def testBlockCallbackOnlyOnce() {
var called = false
val o = Observable[Int](subscriber => {
subscriber.add({ called = !called })
})

val subscription = o.subscribe()
subscription.unsubscribe()
subscription.unsubscribe()

// Even if called multiple times, callback is only called once
assertTrue(called)
assertTrue(subscription.isUnsubscribed)
}

}

0 comments on commit 400f69b

Please sign in to comment.