mosaic icon indicating copy to clipboard operation
mosaic copied to clipboard

Use effects to denote scope of work

Open JakeWharton opened this issue 4 years ago • 3 comments

Currently we build our own coroutine scope to encapsulate all of the work required to render. When this scope ends, rendering ends and the program exits cleanly (modulo internal nonsense to make this work).

Ideally effects inside the composition are used to scope the composition instead. Compose internals currently prevent this, which is tracked as https://issuetracker.google.com/issues/169425431.

JakeWharton avatar Nov 20 '20 04:11 JakeWharton

Today:

fun main() = runMosaic {
  val countValue = mutableStateOf(0)

  setContent {
    val count by remember { countValue }
    Text("The count is: $count")
  }

  for (i in 1..20) {
    delay(250)
    countValue.value = i
  }
}

Tomorrow:

fun main() = runMosaic {
  val count by remember { mutableStateOf(0) }
  Text("The count is: $count")

  LaunchedEffect(null) {
    for (i in 1..20) {
      delay(250)
      count++
    }
  }
}

JakeWharton avatar Nov 21 '20 05:11 JakeWharton

I was doing some Compose UI for Desktop recently, and it seems like they support this internally. Need to investigate.

JakeWharton avatar Nov 13 '23 18:11 JakeWharton

I think what you need is something like FlushCoroutineDispatcher from Compose Multiplatform.

m-sasha avatar Apr 12 '24 12:04 m-sasha

Done in #284

JakeWharton avatar Aug 20 '24 19:08 JakeWharton