Last active
May 10, 2016 21:56
-
-
Save ookami-kb/f0a1f24abb5963dce66e4d4aff552123 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
// inspired by http://www.andreamaglie.com/rxjava-listener-to-observable/ | |
interface ValueUpdateListener { | |
fun onValueChanged(value: String) | |
} | |
class ValueUpdater { | |
fun registerListener(listener: ValueUpdateListener) { | |
// ... | |
} | |
fun unregisterListener(listener: ValueUpdateListener) { | |
// ... | |
} | |
} | |
fun observableWrapper(): Observable<String> { | |
return observable<String> { | |
subscriber -> | |
val listener = object : ValueUpdateListener { | |
override fun onValueChanged(value: String) { | |
if (subscriber.isUnsubscribed) { | |
valueUpdater.unregisterListener(this) | |
} else { | |
subscriber.onNext(value) | |
} | |
} | |
} | |
valueUpdater.registerListener(listener) | |
} | |
} | |
observableWrapper().subscribe { | |
// do smth with value | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment