Skip to content

Commit

Permalink
condence subcommand (#465)
Browse files Browse the repository at this point in the history
Co-authored-by: Ash Davies <[email protected]>
  • Loading branch information
ashdavies and ashdavies authored Aug 12, 2023
1 parent 3029d96 commit 1c311de
Showing 1 changed file with 7 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ public fun ArgParser.Subcommand(
onExecute: suspend CoroutineScope.() -> Unit = { },
content: @Composable () -> Unit = { },
) {
val coroutineScope: CoroutineScope = rememberCoroutineScope()
val coroutineScope = rememberCoroutineScope()

val subcommand: Subcommand = rememberSubcommand(name, actionDescription) {
coroutineScope.launch { onExecute() }
val subcommand = remember(name, actionDescription) {
object : Subcommand(name, actionDescription) {
override fun execute() {
coroutineScope.launch { onExecute() }
}
}
}

LaunchedEffect(subcommand) {
Expand All @@ -30,15 +34,3 @@ public fun ArgParser.Subcommand(

content()
}

@Composable
@ExperimentalCli
private fun rememberSubcommand(
name: String,
actionDescription: String,
block: (Subcommand) -> Unit,
): Subcommand = remember(name, actionDescription) {
object : Subcommand(name, actionDescription) {
override fun execute() = block(this)
}
}

0 comments on commit 1c311de

Please sign in to comment.