Created
February 16, 2021 08:46
-
-
Save StuStirling/94225ba25d40199dfa30aade50df5e4f to your computer and use it in GitHub Desktop.
Flowable that emits previous item as well as latest.
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
data class PreviousAndLatest<T>( | |
var previous: T?, | |
var latest: T? | |
) { | |
fun addLatest(newLatest: T): PreviousAndLatest<T> { | |
if (latest != null) previous = latest | |
latest = newLatest | |
return this | |
} | |
} | |
flowable | |
.distinctUntilChanged() | |
.scanWith({ PreviousAndLatest<RandomClass>(null, null) }, { previous, latest -> | |
previous.addLatest(latest) | |
}) | |
.filter { it.latest != null } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment