override fun subtask()

in hot-reload-core/src/main/kotlin/org/jetbrains/compose/reload/core/Task.kt [224:259]


    override fun <T> subtask(
        name: String?, context: CoroutineContext, body: suspend Task<T>.() -> T
    ): Task<T> {

        val subtask = TaskImpl(name, context.withAsyncTraces("subtask($name)"), body)

        state.update { currentState ->
            when (currentState) {
                is State.Idle -> error("Cannot call subtask() from the 'Idle' state")
                is State.Running -> currentState.copy(children = currentState.children + subtask)
                is State.Stopping -> currentState.copy(children = currentState.children + subtask)
                is State.Finished -> {
                    subtask.reject()
                    return subtask
                }
            }
        }

        subtask.invokeOnStop { exception ->
            stop(exception)
        }

        subtask.invokeOnFinish {
            state.update { currentState ->
                when (currentState) {
                    is State.Running -> currentState.copy(children = currentState.children - subtask)
                    is State.Stopping -> currentState.copy(children = currentState.children - subtask)
                    is State.Finished -> currentState
                    is State.Idle -> currentState
                }
            }
        }

        subtask.start()
        return subtask
    }