Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxScala: Add convenience method for adding unsubscription callback #1300

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}

}