0

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?

1 Answer 1

1

I would use a when statement for this:

mystream.events.collectLatest { event ->
    when (event) {
        is UserLogin -> handleLogin(event)
        is UserLogout -> handleLogout(event)
        //...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.