Last active
February 19, 2018 02:13
-
-
Save tcw165/0cc59237688625707cc183b1e407eaaa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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