Skip to content

Instantly share code, notes, and snippets.

@bearprada
Last active January 2, 2019 07:30
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 bearprada/41ca00b3ee618bfefaaf15fbefc6f0f5 to your computer and use it in GitHub Desktop.
Save bearprada/41ca00b3ee618bfefaaf15fbefc6f0f5 to your computer and use it in GitHub Desktop.
TakeUntil and TakeLast Behavior
fun unsubsribeFirst() {
val input = PublishSubject.create<Int>()
val lifetime = CompletableSubject.create()
input.ignoreElements().subscribe(lifetime)
input.takeLast(2)
.subscribeUntil(lifetime) { println("$it") }
println("start")
input.onNext(1)
input.onNext(2)
input.onNext(3)
input.onComplete()
println("end")
}
fun unsubscribeLater() {
val input = PublishSubject.create<Int>()
val lifetime = CompletableSubject.create()
input.takeLast(2)
.subscribeUntil(lifetime) { println("$it") }
input.ignoreElements().subscribe(lifetime)
println("start")
input.onNext(1)
input.onNext(2)
input.onNext(3)
input.onComplete()
println("end")
}
@bearprada
Copy link
Author

log from unscubscribeFirst()

start
end

logs from unsubscribeLater()

start
2
3
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment