I am new to Kotlin and have stumbled upon this simple problem. I have an event stream where I consume Login events like this:
mystream.events
.filter { event -> event is UserLogin }.collectLatest { event -> handleLogin(event) }
Now in the same stream, I could also receive a Logout event, which I also want to handle in the same manner - but by calling a different handling function.
I can duplicate the same lines of code to do the same for the Logout event as well, but instead is there a way to do it in the same lambda/block of code. Sort of like an if/else?
CodePudding user response:
I would use a when statement for this:
mystream.events.collectLatest { event ->
when (event) {
is UserLogin -> handleLogin(event)
is UserLogout -> handleLogout(event)
//...
}
}