Last active
March 18, 2019 10:58
-
-
Save pchmielowski/44dd6fc5b72d6d5ea74bf901f76c7d40 to your computer and use it in GitHub Desktop.
RxConnectable playground
This file contains 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
fun main() { | |
val shared = Observable.interval(1, TimeUnit.SECONDS) | |
.doOnSubscribe { println("Subscribed.") } | |
.doOnDispose { println("Disposed.") } | |
.replay() // or .publish() | |
.autoConnect() // or .refcount() | |
Thread { | |
shared | |
.take(3) | |
.subscribe { println("A: $it") } | |
}.start() | |
Thread { | |
shared | |
.take(6) | |
.subscribe { println("B: $it") } | |
}.start() | |
Thread { | |
Thread.sleep(3_000) | |
shared | |
.take(6) | |
.subscribe { println("C: $it") } | |
}.start() | |
Thread { | |
Thread.sleep(7_000) // Starts after A, B and C finish - in case of refCount, resubscribes original observable, in case of autoconnect - not | |
shared | |
.take(6) | |
.subscribe { println("D: $it") } | |
}.start() | |
Thread.sleep(20_000) // Wait till everything finish. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment