0

To complete a main operation, it is necessary to complete several sub operations. I can say that each operation has its own tasks such as starting, processing and stopping.

The structure for a single operation as below:

sealed class DenemeState {
    object Operation1Start: DenemeState()
    object Operation1WaitingToStart: DenemeState()
    object GetDataFromOperation1: DenemeState()
    object Operation1Stop: DenemeState()
    object Operation1WaitingToStop: DenemeState()
}

Functions that start and stop operations take a callback function and forward the status in the future.

fun startOperation1(cb: ((status:String) -> Unit)) {
}

fun stopOperation1(cb: ((status:String) -> Unit)) {
}

My main target is, before starting the operation, setting the state from Operation1Start to Operation1WaitingToStart, then start the operation, then reading the operations from the callback in Operation1WaitingToStart inside the when.

Unfortunately, the code below did not work, do you think such a thing is possible or how can I achieve it in a similar way?

sealed class DenemeState {
    var Operation1StartListener : ((status: String) -> Unit)? = null

    object Operation1Start: DenemeState()
    object Operation1WaitingToStart: DenemeState()
    object GetDataFromOperation1: DenemeState()
    object Operation1Stop: DenemeState()
    object Operation1WaitingToStop: DenemeState()
}


fun startOperation1(cb: ((status:String) -> Unit)) {
}

fun stopOperation1(cb: ((status:String) -> Unit)) {
}

var denemeState: DenemeState = DenemeState.Operation1Start
fun run1() {
    when (val value = denemeState) {
        DenemeState.Operation1Start -> {
            denemeState = DenemeState.Operation1WaitingToStart
            startOperation1(denemeState.Operation1StartListener!!)
        }
        DenemeState.Operation1WaitingToStart -> {
            denemeState.Operation1StartListener {
                
            }
        }
        DenemeState.GetDataFromOperation1 -> {
        }
        DenemeState.Operation1Stop -> {
        }
        DenemeState.Operation1WaitingToStop -> {
        }        
    }    
}

Error

Reference has a nullable type '((String) -> Unit)?', use explicit '?.invoke()' to make a function-like call instead
Type mismatch: inferred type is () -> Unit but String was expected
  • You probably wanted to do this instead: `denemeState.Operation1StartListener = {`. But to be honest, I don't really understand your idea. States are singletons, they're like enums. Why do you store a listener in them? This listener property is similar to a static field, it is shared across the application. – broot Jul 12 '23 at 09:22
  • @broot 1-)Actually, my goal is to bring the state to "Operation1WaitingToStart" (so that if run1 is called, startOperation1 will not run again). – Metropol_Tilkisi Jul 12 '23 at 11:51
  • @broot 2-)Bringing state to GetDataFromOperation1 with updates from callback while state is in Operation1WaitingToStart. – Metropol_Tilkisi Jul 12 '23 at 11:52
  • @broot For example, if I get an error starting operation1 from the callback, I will put it back in Operation1Start state. – Metropol_Tilkisi Jul 12 '23 at 11:53
  • @broot Actually, my goal is to provide this, I'm open to other ways to do this. after trying with your suggestion, it didn't happen in this way anyway, it gave a nullpointer error. – Metropol_Tilkisi Jul 12 '23 at 11:54
  • Your explanation is already based on your current implementation. What do you **really** try to achieve? Invoke callback-based operations (`startOperation1()`, `stopOperation1()`) in a specific order? – broot Jul 12 '23 at 13:04
  • I'm just trying to understand if this state machine-ish is at all needed if we mostly move forward in a predictable way. Restarting of failed operations could justify using a state machine though. – broot Jul 12 '23 at 13:15

0 Answers0