Using state to pass parameters to a runIn method

Is anyone else using state to get around the limitation of not being able to pass parameters to a method when using runIn()? Is there a better approach, or is this OK?

void handleIncomingCommand(delay, value) {
    state.newMode = value
    runIn(delay, changeMode)
}
    
void changeMode() {
    if (location.mode != state.newMode) {
        if (location.modes?.find{it.name == state.newMode}) {
            setLocationMode(state.newMode)
        }  else {
            log.warn "Tried to change to undefined mode '${state.newMode}'"
        }
    }
}

That seems like a decent approach to me, though I’m not really a guru yet. One thing you may want to consider is using atomicState instead of state. If the app will call the changeMode method multiple times during a single execution, you’ll get some strange results without using atomicState.

2 Likes