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

commonMain.com.copperleaf.ballast.test.internal.vm.TestInputHandler.kt Maven / Gradle / Ivy

There is a newer version: 4.2.1
Show newest version
package com.copperleaf.ballast.test.internal.vm

import com.copperleaf.ballast.InputHandler
import com.copperleaf.ballast.InputHandlerScope

internal class TestInputHandler(
    private val logger: (String) -> Unit,
    private val inputHandlerDelegate: InputHandler,
) : InputHandler, Events, State> {

    override suspend fun InputHandlerScope, Events, State>.handleInput(
        input: TestViewModel.Inputs
    ) {
        try {
            doHandleInput(input)
        } catch (t: Throwable) {
            recoverFromError(input)
            throw t
        }
    }

    private suspend fun InputHandlerScope, Events, State>.doHandleInput(
        input: TestViewModel.Inputs
    ) {
        when (input) {
            is TestViewModel.Inputs.ProcessInput -> {
                logger("            before handling normal input")
                input.processingStarted.complete(Unit)

                val scopeDelegate = TestInputHandlerScope(this)
                with(inputHandlerDelegate) {
                    scopeDelegate.handleInput(input.normalInput)
                }
                logger("            after handling normal input")
                Unit
            }
            is TestViewModel.Inputs.AwaitInput -> {
                logger("            before handling normal input")
                val scopeDelegate = TestInputHandlerScope(this)
                with(inputHandlerDelegate) {
                    scopeDelegate.handleInput(input.normalInput)
                }
                input.processingFinished.complete(Unit)
                logger("            after handling normal input")
                Unit
            }
            is TestViewModel.Inputs.TestCompleted -> {
                logger("            before completing test")
                input.processingFinished.complete(Unit)
                logger("            after completing test")
                noOp()
                Unit
            }
        }
    }

    private suspend fun InputHandlerScope, Events, State>.recoverFromError(
        input: TestViewModel.Inputs
    ) {
        when (input) {
            is TestViewModel.Inputs.ProcessInput -> {
                input.processingStarted.complete(Unit)
                Unit
            }
            is TestViewModel.Inputs.AwaitInput -> {
                input.processingFinished.complete(Unit)
                Unit
            }
            is TestViewModel.Inputs.TestCompleted -> {
                input.processingFinished.complete(Unit)
                Unit
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy