Skip to content

Instantly share code, notes, and snippets.

@tcw165
Last active February 19, 2018 02:13
Show Gist options
  • Select an option

  • Save tcw165/0cc59237688625707cc183b1e407eaaa to your computer and use it in GitHub Desktop.

Select an option

Save tcw165/0cc59237688625707cc183b1e407eaaa to your computer and use it in GitHub Desktop.
private val mDisposablesOnCreate = CompositeDisposable()
// Activity onCreate().
override fun onCreate() {
// Long computation operation.
mDisposablesOnCreate.add(
// Share button.
onClickShare()
.switchMap { _ ->
// First, switchMap convert the click to an action observable.
// Whenever a new click is received, switchMap interrupt and
// kills the existing ongoing observable and replace it with
// the new one.
// Second, subscribe to the action observable with a cancel
// throttle where the takeUntil self terminates and also kill
// the action observable when a cancel signal is received.
toShareAction()
.takeUntil(mCancelSrc)
}
.subscribe { _ ->
Log.d("xyz", "all finished!")
})
// Cancel signal.
mDisposablesOnCreate.add(
onClickCancel()
.subscribe { _ ->
mCancelSrc.onNext(0)
})
}
// Activity onDestroy()
override fun onDestroy() {
mDisposablesOnCreate.clear()
// "Share" button observable.
fun onClickShare(): Observable<Any> { ... }
// "Cancel" button observable.
fun onClickCancel(): Observable<Any> { ... }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment