Skip to content

Oolong

Oolong is an Elm inspired Model-View-Update (MVU) implementation for Kotlin multiplatform. As the name implies, three core concepts comprise the foundation of this architecture:

  • Model - a type to represent the program state

  • View - a function to map the state to view properties

  • Update - a function to update the state

By applying this simple pattern you can create composable, testable programs that can run on any platform. Oolong enables a common codebase for all platforms by using a render function which is implemented by each frontend.

An example

Here is a simple example in which a number can be incremented or decremented.

data class Model(
    val count: Int = 0
)

sealed class Msg {
    object Increment : Msg()
    object Decrement : Msg()
}

class Props(
    val count: Int,
    val increment: () -> Unit,
    val decrement: () -> Unit,
)

val init: () -> Pair<Model, Effect<Msg>> = { 
    Model() to none()
}

val update: (Msg, Model) -> Pair<Model, Effect<Msg>> = { msg, model ->
    when (msg) {
        Msg.Increment -> next(model.copy(count = model.count + 1))
        Msg.Decrement -> next(model.copy(count = model.count - 1))
    }
}

val view: (Model, Dispatch<Msg>) -> Props = { model ->
    Props(
        model.count,
        { dispatch(Msg.Increment) },
        { dispatch(Msg.Decrement) },
    )
}