Skip to content

Instantly share code, notes, and snippets.

@pchmielowski
Last active March 18, 2019 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pchmielowski/44dd6fc5b72d6d5ea74bf901f76c7d40 to your computer and use it in GitHub Desktop.
Save pchmielowski/44dd6fc5b72d6d5ea74bf901f76c7d40 to your computer and use it in GitHub Desktop.
RxConnectable playground
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