All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.ControllerStateRunning.kt Maven / Gradle / Ivy

package kt.mobius

import kt.mobius.functions.Consumer

class ControllerStateRunning(
    private val actions: ControllerActions,
    private val renderer: Connection,
    loopFactory: MobiusLoop.Factory,
    private val startModel: M
) : ControllerStateBase() {

    private val loop = loopFactory.startFrom(startModel)

    override val stateName: String
        get() = "running"

    override val isRunning: Boolean
        get() = true

    fun start() {
        loop.observe(
            Consumer { model ->
                actions.postUpdateView(model)
            })
    }

    override fun onDispatchEvent(event: E) {
        loop.dispatchEvent(event)
    }

    override fun onUpdateView(model: M) {
        renderer.accept(model)
    }

    override fun onStop() {
        loop.dispose()
        val mostRecentModel = loop.mostRecentModel
        actions.goToStateCreated(renderer, mostRecentModel)
    }

    override fun onGetModel(): M {
        val model = loop.mostRecentModel
        return model ?: startModel
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy